Merge "Add tests for WikiMap and WikiReference"
[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 array 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 bug 29408
23 $this->bug29408File = wfTempDir() . '/bug29408';
24 file_put_contents( $this->bug29408File, "\x00" );
25
26 self::$users = array(
27 'sysop' => new TestUser(
28 'Uploadstashtestsysop',
29 'Upload Stash Test Sysop',
30 'upload_stash_test_sysop@example.com',
31 array( 'sysop' )
32 ),
33 'uploader' => new TestUser(
34 'Uploadstashtestuser',
35 'Upload Stash Test User',
36 'upload_stash_test_user@example.com',
37 array()
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']->user );
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 bug 29408
66 $this->assertTrue( true, 'Unrecognized file without extension' );
67
68 $stash->removeFile( $file->getFileKey() );
69 }
70
71 public static function provideInvalidRequests() {
72 return array(
73 'Check failure on bad wpFileKey' =>
74 array( new FauxRequest( array( 'wpFileKey' => 'foo' ) ) ),
75 'Check failure on bad wpSessionKey' =>
76 array( new FauxRequest( array( '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 array(
89 'Check good wpFileKey' =>
90 array( new FauxRequest( array( 'wpFileKey' => 'testkey-test.test' ) ) ),
91 'Check good wpSessionKey' =>
92 array( new FauxRequest( array( 'wpFileKey' => 'testkey-test.test' ) ) ),
93 'Check key precedence' =>
94 array( new FauxRequest( array(
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 }