MediaWiki  1.33.0
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 }
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
RepoGroup\singleton
static singleton()
Get a RepoGroup instance.
Definition: RepoGroup.php:61
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Definition: router.php:42
UploadStashTest\setUp
setUp()
Definition: UploadStashTest.php:19
UploadStash
UploadStash is intended to accomplish a few things:
Definition: UploadStash.php:53
StatusValue\newFatal
static newFatal( $message)
Factory function for fatal errors.
Definition: StatusValue.php:68
UploadStashTest\$users
static TestUser[] $users
Array of UploadStashTestUser.
Definition: UploadStashTest.php:12
UploadStashFileException
Definition: UploadStashFileException.php:26
UploadStashTest\testExceptionWhenStoreTempFails
testExceptionWhenStoreTempFails()
Definition: UploadStashTest.php:95
php
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
TestUser
Wraps the user object, so we can also retain full access to properties like password if we log in via...
Definition: TestUser.php:9
UploadStashTest\provideValidRequests
static provideValidRequests()
Definition: UploadStashTest.php:74
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:709
MediaWikiTestCase\getNewTempFile
getNewTempFile()
Obtains a new temporary file name.
Definition: MediaWikiTestCase.php:474
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
UploadStashTest\testValidRequestWithValidRequests
testValidRequestWithValidRequests( $request)
provideValidRequests
Definition: UploadStashTest.php:91
UploadStashTest\$tmpFile
string $tmpFile
Definition: UploadStashTest.php:17
$request
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:2636
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2162
UploadFromStash\isValidRequest
static isValidRequest( $request)
Definition: UploadFromStash.php:83
UploadStashTest
Database.
Definition: UploadStashTest.php:8
class
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
UploadStashTest\testT31408
testT31408()
Definition: UploadStashTest.php:44
UploadStashTest\provideInvalidRequests
static provideInvalidRequests()
Definition: UploadStashTest.php:58
UploadStashTest\testValidRequestWithInvalidRequests
testValidRequestWithInvalidRequests( $request)
provideInvalidRequests
Definition: UploadStashTest.php:70