Merge "Add SPARQL client to core"
[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 = $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
41 /**
42 * @todo give this test a real name explaining what is being tested here
43 */
44 public function testBug29408() {
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
67 /**
68 * @dataProvider provideInvalidRequests
69 */
70 public function testValidRequestWithInvalidRequests( $request ) {
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 /**
88 * @dataProvider provideValidRequests
89 */
90 public function testValidRequestWithValidRequests( $request ) {
91 $this->assertTrue( UploadFromStash::isValidRequest( $request ) );
92 }
93
94 public function testExceptionWhenStoreTempFails() {
95 $mockRepoStoreStatusResult = Status::newFatal( 'TEST_ERROR' );
96 $mockRepo = $this->getMockBuilder( FileRepo::class )
97 ->disableOriginalConstructor()
98 ->getMock();
99 $mockRepo->expects( $this->once() )
100 ->method( 'storeTemp' )
101 ->willReturn( $mockRepoStoreStatusResult );
102
103 $stash = new UploadStash( $mockRepo );
104 try {
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 ) );
111 }
112 }
113 }