MediaWiki  master
UploadStashTest.php
Go to the documentation of this file.
1 <?php
2 
12  public static $users;
13 
17  private $tmpFile;
18 
19  protected function setUp() {
20  parent::setUp();
21 
22  $this->tmpFile = $this->getNewTempFile();
23  file_put_contents( $this->tmpFile, "\x00" );
24 
25  self::$users = [
26  'sysop' => new TestUser(
27  'Uploadstashtestsysop',
28  'Upload Stash Test Sysop',
29  'upload_stash_test_sysop@example.com',
30  [ 'sysop' ]
31  ),
32  'uploader' => new TestUser(
33  'Uploadstashtestuser',
34  'Upload Stash Test User',
35  'upload_stash_test_user@example.com',
36  []
37  )
38  ];
39  }
40 
44  public function testT31408() {
45  $this->setMwGlobals( 'wgUser', self::$users['uploader']->getUser() );
46 
47  $repo = RepoGroup::singleton()->getLocalRepo();
48  $stash = new UploadStash( $repo );
49 
50  // Throws exception caught by PHPUnit on failure
51  $file = $stash->stashFile( $this->tmpFile );
52  // We'll never reach this point if we hit T31408
53  $this->assertTrue( true, 'Unrecognized file without extension' );
54 
55  $stash->removeFile( $file->getFileKey() );
56  }
57 
58  public static function provideInvalidRequests() {
59  return [
60  'Check failure on bad wpFileKey' =>
61  [ new FauxRequest( [ 'wpFileKey' => 'foo' ] ) ],
62  'Check failure on bad wpSessionKey' =>
63  [ new FauxRequest( [ 'wpSessionKey' => 'foo' ] ) ],
64  ];
65  }
66 
71  $this->assertFalse( UploadFromStash::isValidRequest( $request ) );
72  }
73 
74  public static function provideValidRequests() {
75  return [
76  'Check good wpFileKey' =>
77  [ new FauxRequest( [ 'wpFileKey' => 'testkey-test.test' ] ) ],
78  'Check good wpSessionKey' =>
79  [ new FauxRequest( [ 'wpFileKey' => 'testkey-test.test' ] ) ],
80  'Check key precedence' =>
81  [ new FauxRequest( [
82  'wpFileKey' => 'testkey-test.test',
83  'wpSessionKey' => 'foo'
84  ] ) ],
85  ];
86  }
87 
92  $this->assertTrue( UploadFromStash::isValidRequest( $request ) );
93  }
94 
95  public function testExceptionWhenStoreTempFails() {
96  $mockRepoStoreStatusResult = Status::newFatal( 'TEST_ERROR' );
97  $mockRepo = $this->getMockBuilder( FileRepo::class )
98  ->disableOriginalConstructor()
99  ->getMock();
100  $mockRepo->expects( $this->once() )
101  ->method( 'storeTemp' )
102  ->willReturn( $mockRepoStoreStatusResult );
103 
104  $stash = new UploadStash( $mockRepo );
105  try {
106  $stash->stashFile( $this->tmpFile );
107  $this->fail( 'Expected UploadStashFileException not thrown' );
108  } catch ( UploadStashFileException $e ) {
109  $this->assertInstanceOf( ILocalizedException::class, $e );
110  } catch ( Exception $e ) {
111  $this->fail( 'Unexpected exception class ' . get_class( $e ) );
112  }
113  }
114 }
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Definition: router.php:42
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2146
testValidRequestWithValidRequests( $request)
provideValidRequests
testValidRequestWithInvalidRequests( $request)
provideInvalidRequests
static isValidRequest( $request)
static newFatal( $message)
Factory function for fatal errors.
Definition: StatusValue.php:68
static singleton()
Definition: RepoGroup.php:60
static TestUser [] $users
Array of UploadStashTestUser.
static provideValidRequests()
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
UploadStash is intended to accomplish a few things:
Definition: UploadStash.php:53
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
Wraps the user object, so we can also retain full access to properties like password if we log in via...
Definition: TestUser.php:9
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2620
static provideInvalidRequests()