d754ba5a22a6b1bf7ad30753f3289243e6ebef82
8 class UploadStashTest
extends MediaWikiTestCase
{
10 * @var TestUser[] Array of UploadStashTestUser
19 protected function setUp() {
22 $this->tmpFile
= wfTempDir() . '/' . uniqid();
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',
41 protected function tearDown() {
42 if ( file_exists( $this->tmpFile
. "." ) ) {
43 unlink( $this->tmpFile
. "." );
46 if ( file_exists( $this->tmpFile
) ) {
47 unlink( $this->tmpFile
);
54 * @todo give this test a real name explaining what is being tested here
56 public function testBug29408() {
57 $this->setMwGlobals( 'wgUser', self
::$users['uploader']->getUser() );
59 $repo = RepoGroup
::singleton()->getLocalRepo();
60 $stash = new UploadStash( $repo );
62 // Throws exception caught by PHPUnit on failure
63 $file = $stash->stashFile( $this->tmpFile
);
64 // We'll never reach this point if we hit T31408
65 $this->assertTrue( true, 'Unrecognized file without extension' );
67 $stash->removeFile( $file->getFileKey() );
70 public static function provideInvalidRequests() {
72 'Check failure on bad wpFileKey' =>
73 [ new FauxRequest( [ 'wpFileKey' => 'foo' ] ) ],
74 'Check failure on bad wpSessionKey' =>
75 [ new FauxRequest( [ 'wpSessionKey' => 'foo' ] ) ],
80 * @dataProvider provideInvalidRequests
82 public function testValidRequestWithInvalidRequests( $request ) {
83 $this->assertFalse( UploadFromStash
::isValidRequest( $request ) );
86 public static function provideValidRequests() {
88 'Check good wpFileKey' =>
89 [ new FauxRequest( [ 'wpFileKey' => 'testkey-test.test' ] ) ],
90 'Check good wpSessionKey' =>
91 [ new FauxRequest( [ 'wpFileKey' => 'testkey-test.test' ] ) ],
92 'Check key precedence' =>
94 'wpFileKey' => 'testkey-test.test',
95 'wpSessionKey' => 'foo'
100 * @dataProvider provideValidRequests
102 public function testValidRequestWithValidRequests( $request ) {
103 $this->assertTrue( UploadFromStash
::isValidRequest( $request ) );
106 public function testExceptionWhenStoreTempFails() {
107 $mockRepoStoreStatusResult = Status
::newFatal( 'TEST_ERROR' );
108 $mockRepo = $this->getMockBuilder( FileRepo
::class )
109 ->disableOriginalConstructor()
111 $mockRepo->expects( $this->once() )
112 ->method( 'storeTemp' )
113 ->willReturn( $mockRepoStoreStatusResult );
115 $stash = new UploadStash( $mockRepo );
117 $stash->stashFile( $this->tmpFile
);
118 $this->fail( 'Expected UploadStashFileException not thrown' );
119 } catch ( UploadStashFileException
$e ) {
120 $this->assertInstanceOf( 'ILocalizedException', $e );
121 } catch ( Exception
$e ) {
122 $this->fail( 'Unexpected exception class ' . get_class( $e ) );