Merge "Ensure WCAG level AA contrast"
[lhc/web/wiklou.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 $tmpFile;
18
19 protected function setUp() {
20 parent::setUp();
21
22 $this->tmpFile = wfTempDir() . '/' . uniqid();
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
41 protected function tearDown() {
42 if ( file_exists( $this->tmpFile . "." ) ) {
43 unlink( $this->tmpFile . "." );
44 }
45
46 if ( file_exists( $this->tmpFile ) ) {
47 unlink( $this->tmpFile );
48 }
49
50 parent::tearDown();
51 }
52
53 /**
54 * @todo give this test a real name explaining what is being tested here
55 */
56 public function testBug29408() {
57 $this->setMwGlobals( 'wgUser', self::$users['uploader']->getUser() );
58
59 $repo = RepoGroup::singleton()->getLocalRepo();
60 $stash = new UploadStash( $repo );
61
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' );
66
67 $stash->removeFile( $file->getFileKey() );
68 }
69
70 public static function provideInvalidRequests() {
71 return [
72 'Check failure on bad wpFileKey' =>
73 [ new FauxRequest( [ 'wpFileKey' => 'foo' ] ) ],
74 'Check failure on bad wpSessionKey' =>
75 [ new FauxRequest( [ 'wpSessionKey' => 'foo' ] ) ],
76 ];
77 }
78
79 /**
80 * @dataProvider provideInvalidRequests
81 */
82 public function testValidRequestWithInvalidRequests( $request ) {
83 $this->assertFalse( UploadFromStash::isValidRequest( $request ) );
84 }
85
86 public static function provideValidRequests() {
87 return [
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' =>
93 [ new FauxRequest( [
94 'wpFileKey' => 'testkey-test.test',
95 'wpSessionKey' => 'foo'
96 ] ) ],
97 ];
98 }
99 /**
100 * @dataProvider provideValidRequests
101 */
102 public function testValidRequestWithValidRequests( $request ) {
103 $this->assertTrue( UploadFromStash::isValidRequest( $request ) );
104 }
105
106 public function testExceptionWhenStoreTempFails() {
107 $mockRepoStoreStatusResult = Status::newFatal( 'TEST_ERROR' );
108 $mockRepo = $this->getMockBuilder( FileRepo::class )
109 ->disableOriginalConstructor()
110 ->getMock();
111 $mockRepo->expects( $this->once() )
112 ->method( 'storeTemp' )
113 ->willReturn( $mockRepoStoreStatusResult );
114
115 $stash = new UploadStash( $mockRepo );
116 try {
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 ) );
123 }
124 }
125 }