]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - tests/phpunit/includes/upload/UploadStashTest.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / tests / phpunit / includes / upload / UploadStashTest.php
1 <?php
2
3 /**
4  * @group Database
5  *
6  * @covers UploadStash
7  */
8 class UploadStashTest extends MediaWikiTestCase {
9         /**
10          * @var TestUser[] Array of UploadStashTestUser
11          */
12         public static $users;
13
14         /**
15          * @var string
16          */
17         private $bug29408File;
18
19         protected function setUp() {
20                 parent::setUp();
21
22                 // Setup a file for T31408
23                 $this->bug29408File = wfTempDir() . '/bug29408';
24                 file_put_contents( $this->bug29408File, "\x00" );
25
26                 self::$users = [
27                         'sysop' => new TestUser(
28                                 'Uploadstashtestsysop',
29                                 'Upload Stash Test Sysop',
30                                 'upload_stash_test_sysop@example.com',
31                                 [ 'sysop' ]
32                         ),
33                         'uploader' => new TestUser(
34                                 'Uploadstashtestuser',
35                                 'Upload Stash Test User',
36                                 'upload_stash_test_user@example.com',
37                                 []
38                         )
39                 ];
40         }
41
42         protected function tearDown() {
43                 if ( file_exists( $this->bug29408File . "." ) ) {
44                         unlink( $this->bug29408File . "." );
45                 }
46
47                 if ( file_exists( $this->bug29408File ) ) {
48                         unlink( $this->bug29408File );
49                 }
50
51                 parent::tearDown();
52         }
53
54         /**
55          * @todo give this test a real name explaining what is being tested here
56          */
57         public function testBug29408() {
58                 $this->setMwGlobals( 'wgUser', self::$users['uploader']->getUser() );
59
60                 $repo = RepoGroup::singleton()->getLocalRepo();
61                 $stash = new UploadStash( $repo );
62
63                 // Throws exception caught by PHPUnit on failure
64                 $file = $stash->stashFile( $this->bug29408File );
65                 // We'll never reach this point if we hit T31408
66                 $this->assertTrue( true, 'Unrecognized file without extension' );
67
68                 $stash->removeFile( $file->getFileKey() );
69         }
70
71         public static function provideInvalidRequests() {
72                 return [
73                         'Check failure on bad wpFileKey' =>
74                                 [ new FauxRequest( [ 'wpFileKey' => 'foo' ] ) ],
75                         'Check failure on bad wpSessionKey' =>
76                                 [ new FauxRequest( [ 'wpSessionKey' => 'foo' ] ) ],
77                 ];
78         }
79
80         /**
81          * @dataProvider provideInvalidRequests
82          */
83         public function testValidRequestWithInvalidRequests( $request ) {
84                 $this->assertFalse( UploadFromStash::isValidRequest( $request ) );
85         }
86
87         public static function provideValidRequests() {
88                 return [
89                         'Check good wpFileKey' =>
90                                 [ new FauxRequest( [ 'wpFileKey' => 'testkey-test.test' ] ) ],
91                         'Check good wpSessionKey' =>
92                                 [ new FauxRequest( [ 'wpFileKey' => 'testkey-test.test' ] ) ],
93                         'Check key precedence' =>
94                                 [ new FauxRequest( [
95                                         'wpFileKey' => 'testkey-test.test',
96                                         'wpSessionKey' => 'foo'
97                                 ] ) ],
98                 ];
99         }
100         /**
101          * @dataProvider provideValidRequests
102          */
103         public function testValidRequestWithValidRequests( $request ) {
104                 $this->assertTrue( UploadFromStash::isValidRequest( $request ) );
105         }
106
107 }