8 class UploadStashTest
extends MediaWikiTestCase
{
10 * @var TestUser[] Array of UploadStashTestUser
19 protected function setUp() {
22 $this->tmpFile
= $this->getNewTempFile();
23 file_put_contents( $this->tmpFile
, "\x00" );
26 'sysop' => new TestUser(
27 'Uploadstashtestsysop',
28 'Upload Stash Test Sysop',
29 'upload_stash_test_sysop@example.com',
32 'uploader' => new TestUser(
33 'Uploadstashtestuser',
34 'Upload Stash Test User',
35 'upload_stash_test_user@example.com',
42 * @todo give this test a real name explaining what is being tested here
44 public function testBug29408() {
45 $this->setMwGlobals( 'wgUser', self
::$users['uploader']->getUser() );
47 $repo = RepoGroup
::singleton()->getLocalRepo();
48 $stash = new UploadStash( $repo );
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' );
55 $stash->removeFile( $file->getFileKey() );
58 public static function provideInvalidRequests() {
60 'Check failure on bad wpFileKey' =>
61 [ new FauxRequest( [ 'wpFileKey' => 'foo' ] ) ],
62 'Check failure on bad wpSessionKey' =>
63 [ new FauxRequest( [ 'wpSessionKey' => 'foo' ] ) ],
68 * @dataProvider provideInvalidRequests
70 public function testValidRequestWithInvalidRequests( $request ) {
71 $this->assertFalse( UploadFromStash
::isValidRequest( $request ) );
74 public static function provideValidRequests() {
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' =>
82 'wpFileKey' => 'testkey-test.test',
83 'wpSessionKey' => 'foo'
88 * @dataProvider provideValidRequests
90 public function testValidRequestWithValidRequests( $request ) {
91 $this->assertTrue( UploadFromStash
::isValidRequest( $request ) );
94 public function testExceptionWhenStoreTempFails() {
95 $mockRepoStoreStatusResult = Status
::newFatal( 'TEST_ERROR' );
96 $mockRepo = $this->getMockBuilder( FileRepo
::class )
97 ->disableOriginalConstructor()
99 $mockRepo->expects( $this->once() )
100 ->method( 'storeTemp' )
101 ->willReturn( $mockRepoStoreStatusResult );
103 $stash = new UploadStash( $mockRepo );
105 $stash->stashFile( $this->tmpFile
);
106 $this->fail( 'Expected UploadStashFileException not thrown' );
107 } catch ( UploadStashFileException
$e ) {
108 $this->assertInstanceOf( ILocalizedException
::class, $e );
109 } catch ( Exception
$e ) {
110 $this->fail( 'Unexpected exception class ' . get_class( $e ) );