summaryrefslogtreecommitdiff
path: root/www/wiki/tests/phpunit/includes/upload/UploadStashTest.php
blob: 39acbb07406b4fe759f932c9b0d8a8326de3e350 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php

/**
 * @group Database
 *
 * @covers UploadStash
 */
class UploadStashTest extends MediaWikiTestCase {
	/**
	 * @var TestUser[] Array of UploadStashTestUser
	 */
	public static $users;

	/**
	 * @var string
	 */
	private $tmpFile;

	protected function setUp() {
		parent::setUp();

		$this->tmpFile = $this->getNewTempFile();
		file_put_contents( $this->tmpFile, "\x00" );

		self::$users = [
			'sysop' => new TestUser(
				'Uploadstashtestsysop',
				'Upload Stash Test Sysop',
				'upload_stash_test_sysop@example.com',
				[ 'sysop' ]
			),
			'uploader' => new TestUser(
				'Uploadstashtestuser',
				'Upload Stash Test User',
				'upload_stash_test_user@example.com',
				[]
			)
		];
	}

	/**
	 * @todo give this test a real name explaining what is being tested here
	 */
	public function testBug29408() {
		$this->setMwGlobals( 'wgUser', self::$users['uploader']->getUser() );

		$repo = RepoGroup::singleton()->getLocalRepo();
		$stash = new UploadStash( $repo );

		// Throws exception caught by PHPUnit on failure
		$file = $stash->stashFile( $this->tmpFile );
		// We'll never reach this point if we hit T31408
		$this->assertTrue( true, 'Unrecognized file without extension' );

		$stash->removeFile( $file->getFileKey() );
	}

	public static function provideInvalidRequests() {
		return [
			'Check failure on bad wpFileKey' =>
				[ new FauxRequest( [ 'wpFileKey' => 'foo' ] ) ],
			'Check failure on bad wpSessionKey' =>
				[ new FauxRequest( [ 'wpSessionKey' => 'foo' ] ) ],
		];
	}

	/**
	 * @dataProvider provideInvalidRequests
	 */
	public function testValidRequestWithInvalidRequests( $request ) {
		$this->assertFalse( UploadFromStash::isValidRequest( $request ) );
	}

	public static function provideValidRequests() {
		return [
			'Check good wpFileKey' =>
				[ new FauxRequest( [ 'wpFileKey' => 'testkey-test.test' ] ) ],
			'Check good wpSessionKey' =>
				[ new FauxRequest( [ 'wpFileKey' => 'testkey-test.test' ] ) ],
			'Check key precedence' =>
				[ new FauxRequest( [
					'wpFileKey' => 'testkey-test.test',
					'wpSessionKey' => 'foo'
				] ) ],
		];
	}
	/**
	 * @dataProvider provideValidRequests
	 */
	public function testValidRequestWithValidRequests( $request ) {
		$this->assertTrue( UploadFromStash::isValidRequest( $request ) );
	}

	public function testExceptionWhenStoreTempFails() {
		$mockRepoStoreStatusResult = Status::newFatal( 'TEST_ERROR' );
		$mockRepo = $this->getMockBuilder( FileRepo::class )
			->disableOriginalConstructor()
			->getMock();
		$mockRepo->expects( $this->once() )
			->method( 'storeTemp' )
			->willReturn( $mockRepoStoreStatusResult );

		$stash = new UploadStash( $mockRepo );
		try {
			$stash->stashFile( $this->tmpFile );
			$this->fail( 'Expected UploadStashFileException not thrown' );
		} catch ( UploadStashFileException $e ) {
			$this->assertInstanceOf( ILocalizedException::class, $e );
		} catch ( Exception $e ) {
			$this->fail( 'Unexpected exception class ' . get_class( $e ) );
		}
	}
}