summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/UploadWizard/tests/phpunit/ApiFlickrBlacklistTest.php
blob: 520b2bcb778d1ad85618acf91540a0979e32354c (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php

/**
 * Test the Flickr blacklist API.
 * Note that these tests trigger actual API requests to Flickr. The relevant settings need to
 * be configured (see the Flickr upload section of the UploadWizard docs).
 *
 * @group API
 * @group medium
 * @covers ApiFlickrBlacklist
 */
class ApiFlickrBlacklistTest extends ApiTestCase {
	/**
	 * Page used to store the blacklist. Will be created by the tests.
	 */
	const BLACKLIST_PAGE = 'TestFlickrBlacklistPage';

	/**
	 * NSID of the test user (actually, a real Flickr user chosen randomly
	 * from the blacklist at Commons).
	 */
	const PASFAM_NSID = '26011645@N00';

	/**
	 * Username of the test Flickr user.
	 */
	const PASFAM_USERNAME = 'pasfam';

	/**
	 * A photo from the test user.
	 */
	const PASFAM_IMAGE_PHOTO = 'http://www.flickr.com/photos/pasfam/147426941/';

	/**
	 * Static image URL for the test photo.
	 */
	const PASFAM_IMAGE_STATIC = 'http://farm1.staticflickr.com/44/147426941_98baf36fd1_o.jpg';

	/**
	 * A non-existing userid that won't match anything.
	 */
	const FAKE_NSID = '11111111@N00';

	public function testBlacklistMatchByNsid() {
		$this->checkApiSetup();
		$this->setFlickrBlacklistPage( self::BLACKLIST_PAGE );
		$this->editPage( self::BLACKLIST_PAGE, self::PASFAM_NSID );

		list( $response, , ) = $this->doApiRequest( [
			'action' => 'flickrblacklist',
			'url' => self::PASFAM_IMAGE_STATIC,
		] );
		$this->assertBlacklistMatch( $response );

		list( $response, , ) = $this->doApiRequest( [
			'action' => 'flickrblacklist',
			'url' => self::PASFAM_IMAGE_PHOTO,
		] );
		$this->assertBlacklistMatch( $response );
	}

	public function testBlacklistMatchByUsername() {
		$this->checkApiSetup();
		$this->setFlickrBlacklistPage( self::BLACKLIST_PAGE );
		$this->editPage( self::BLACKLIST_PAGE, self::PASFAM_USERNAME );

		list( $response, , ) = $this->doApiRequest( [
			'action' => 'flickrblacklist',
			'url' => self::PASFAM_IMAGE_STATIC,
		] );
		$this->assertBlacklistMatch( $response );
	}

	public function testBlacklistMatchWithMultipleItems() {
		$this->checkApiSetup();
		$this->setFlickrBlacklistPage( self::BLACKLIST_PAGE );
		$this->editPage( self::BLACKLIST_PAGE, 'foo bar ' . self::PASFAM_NSID . ' baz' );

		list( $response, , ) = $this->doApiRequest( [
			'action' => 'flickrblacklist',
			'url' => self::PASFAM_IMAGE_STATIC,
		] );
		$this->assertBlacklistMatch( $response );
	}

	public function testBlacklistNoMatch() {
		$this->checkApiSetup();
		$this->setFlickrBlacklistPage( self::BLACKLIST_PAGE );
		$this->editPage( self::BLACKLIST_PAGE, self::FAKE_NSID );

		list( $response, , ) = $this->doApiRequest( [
			'action' => 'flickrblacklist',
			'url' => self::PASFAM_IMAGE_STATIC,
		] );
		$this->assertNotBlacklistMatch( $response );

		list( $response, , ) = $this->doApiRequest( [
			'action' => 'flickrblacklist',
			'url' => self::PASFAM_IMAGE_PHOTO,
		] );
		$this->assertNotBlacklistMatch( $response );
	}

	/**
	 * Lines starting with # are comments.
	 */
	public function testBlacklistComment() {
		$this->checkApiSetup();
		$this->setFlickrBlacklistPage( self::BLACKLIST_PAGE );
		$this->editPage( self::BLACKLIST_PAGE, '# ' . self::PASFAM_NSID );

		list( $response, , ) = $this->doApiRequest( [
			'action' => 'flickrblacklist',
			'url' => self::PASFAM_IMAGE_STATIC,
		] );
		$this->assertNotBlacklistMatch( $response );
	}

	public function testGetFullBlacklist() {
		$this->checkApiSetup();
		$this->setFlickrBlacklistPage( self::BLACKLIST_PAGE );
		$this->editPage( self::BLACKLIST_PAGE, '26011645@N00' );

		list( $response, , ) = $this->doApiRequest( [
			'action' => 'flickrblacklist',
			'list' => 1,
		] );

		$this->assertArrayHasKey( 'flickrblacklist', $response );
		$this->assertArrayHasKey( 'list', $response['flickrblacklist'] );
		$this->assertContains( self::PASFAM_NSID, $response['flickrblacklist']['list'] );
	}

	/**
	 * When the blacklist page does not exist, things should not break.
	 * This is the last test, to make sure that the blacklist cache (which is a static property so
	 * changes to it survive between tests) is left empty.
	 */
	public function testNoBlacklist() {
		$this->checkApiSetup();
		$this->setFlickrBlacklistPage( 'TestFlickrBlacklistPageDoesNotExist' );

		list( $response, , ) = $this->doApiRequest( [
			'action' => 'flickrblacklist',
			'url' => self::PASFAM_IMAGE_STATIC,
		] );

		$this->assertNotBlacklistMatch( $response );
		// there should be no API call to Flickr; we have no good way of asserting that
	}

	protected function checkApiSetup() {
		// this is needed to initialize the global $wgUploadWizardConfig
		$wgUploadWizardConfig = UploadWizardConfig::getConfig();

		if ( !isset( $wgUploadWizardConfig['flickrApiKey'] ) ) {
			$this->markTestSkipped( 'This test needs a Flickr API key to work' );
		}
		if ( !isset( $wgUploadWizardConfig['flickrApiUrl'] )
			|| Http::get( $wgUploadWizardConfig['flickrApiUrl'] ) === false
		) {
			// Http::get returns false if the server is unreachable.
			// Sometimes unit tests may be run in places without network access.
			$this->markTestSkipped( $wgUploadWizardConfig['flickrApiUrl'] . ' is unreachable.' );
		}
	}
	/**
	 * Changes global parameter for blacklist page in such a way that the change can be
	 * unrolled after the test. Also clears the cache for the blacklist.
	 * @param string $page
	 */
	protected function setFlickrBlacklistPage( $page ) {
		global $wgUploadWizardConfig;
		$this->setMwGlobals( [
			'wgUploadWizardConfig' => array_merge( $wgUploadWizardConfig, [
				'flickrBlacklistPage' => $page,
			] ),
		] );

		// clear blacklist cache
		$reflection = new ReflectionClass( 'UploadWizardFlickrBlacklist' );
		$property = $reflection->getProperty( 'blacklist' );
		$property->setAccessible( true );
		$property->setValue( null, null );
	}

	/**
	 * @param array $result api call result
	 * @param string $message
	 */
	protected function assertBlacklistMatch( $result, $message = '' ) {
		$this->assertArrayHasKey( 'flickrblacklist', $result, $message ?: 'API result missing' );
		$this->assertArrayHasKey(
			'result',
			$result['flickrblacklist'],
			$message ?: 'API result missing'
		);
		$this->assertEquals(
			'bad',
			$result['flickrblacklist']['result'],
			$message ?: 'blacklist does not match'
		);
	}

	/**
	 * @param array $result api call result
	 * @param string $message
	 */
	protected function assertNotBlacklistMatch( $result, $message = '' ) {
		$this->assertArrayHasKey( 'flickrblacklist', $result, $message ?: 'API result missing' );
		$this->assertArrayHasKey(
			'result',
			$result['flickrblacklist'],
			$message ?: 'API result missing'
		);
		$this->assertEquals(
			'ok',
			$result['flickrblacklist']['result'],
			$message ?: 'blacklist does match'
		);
	}
}