MediaWiki REL1_34
ApiQueryPageImagesTest.php
Go to the documentation of this file.
1<?php
2
3namespace PageImages\Tests;
4
5require_once 'ApiQueryPageImagesProxyMock.php';
6
8use PageImages;
9use Title;
11use Wikimedia\TestingAccessWrapper;
12
22class ApiQueryPageImagesTest extends \PHPUnit\Framework\TestCase {
23
24 private function newInstance() {
25 $config = new \HashConfig( [
26 'PageImagesAPIDefaultLicense' => 'free'
27 ] );
28
29 $context = $this->getMockBuilder( 'IContextSource' )
30 ->disableOriginalConstructor()
31 ->getMock();
32
33 $context->expects( $this->any() )
34 ->method( 'getConfig' )
35 ->willReturn( $config );
36
37 $main = $this->getMockBuilder( 'ApiMain' )
38 ->disableOriginalConstructor()
39 ->getMock();
40 $main->expects( $this->once() )
41 ->method( 'getContext' )
42 ->will( $this->returnValue( $context ) );
43
44 $query = $this->getMockBuilder( 'ApiQuery' )
45 ->disableOriginalConstructor()
46 ->getMock();
47 $query->expects( $this->once() )
48 ->method( 'getMain' )
49 ->will( $this->returnValue( $main ) );
50
51 return new ApiQueryPageImages( $query, '' );
52 }
53
54 public function testConstructor() {
55 $instance = $this->newInstance();
56 $this->assertInstanceOf( 'ApiQueryPageImages', $instance );
57 }
58
59 public function testGetCacheMode() {
60 $instance = $this->newInstance();
61 $this->assertSame( 'public', $instance->getCacheMode( [] ) );
62 }
63
64 public function testGetAllowedParams() {
65 $instance = $this->newInstance();
66 $params = $instance->getAllowedParams();
67 $this->assertInternalType( 'array', $params );
68 $this->assertNotEmpty( $params );
69 $this->assertContainsOnly( 'array', $params );
70 $this->assertArrayHasKey( 'limit', $params );
71 $this->assertEquals( $params['limit'][\ApiBase::PARAM_DFLT], 50 );
72 $this->assertEquals( $params['limit'][\ApiBase::PARAM_TYPE], 'limit' );
73 $this->assertEquals( $params['limit'][\ApiBase::PARAM_MIN], 1 );
74 $this->assertEquals( $params['limit'][\ApiBase::PARAM_MAX], 50 );
75 $this->assertEquals( $params['limit'][\ApiBase::PARAM_MAX2], 100 );
76 $this->assertArrayHasKey( 'license', $params );
77 $this->assertEquals( $params['license'][\ApiBase::PARAM_TYPE], [ 'free', 'any' ] );
78 $this->assertEquals( $params['license'][\ApiBase::PARAM_DFLT], 'free' );
79 $this->assertEquals( $params['license'][\ApiBase::PARAM_ISMULTI], false );
80 }
81
85 public function testGetTitles( $titles, $missingTitlesByNamespace, $expected ) {
86 $pageSet = $this->getMockBuilder( 'ApiPageSet' )
87 ->disableOriginalConstructor()
88 ->getMock();
89 $pageSet->expects( $this->any() )
90 ->method( 'getGoodTitles' )
91 ->will( $this->returnValue( $titles ) );
92 $pageSet->expects( $this->any() )
93 ->method( 'getMissingTitlesByNamespace' )
94 ->will( $this->returnValue( $missingTitlesByNamespace ) );
95 $queryPageImages = new ApiQueryPageImagesProxyMock( $pageSet );
96
97 $this->assertEquals( $expected, $queryPageImages->getTitles() );
98 }
99
100 public function provideGetTitles() {
101 return [
102 [
103 [ Title::newFromText( 'Foo' ) ],
104 [],
105 [ Title::newFromText( 'Foo' ) ],
106 ],
107 [
108 [ Title::newFromText( 'Foo' ) ],
109 [
110 NS_TALK => [
111 'Bar' => -1,
112 ],
113 ],
114 [ Title::newFromText( 'Foo' ) ],
115 ],
116 [
117 [ Title::newFromText( 'Foo' ) ],
118 [
119 NS_FILE => [
120 'Bar' => -1,
121 ],
122 ],
123 [
124 0 => Title::newFromText( 'Foo' ),
125 -1 => Title::newFromText( 'Bar', NS_FILE ),
126 ],
127 ],
128 ];
129 }
130
139 public function testExecute( $requestParams, $titles, $queryPageIds,
140 $queryResults, $setResultValueCount
141 ) {
142 $mock = TestingAccessWrapper::newFromObject(
143 $this->getMockBuilder( ApiQueryPageImages::class )
144 ->disableOriginalConstructor()
145 ->setMethods( [ 'extractRequestParams', 'getTitles', 'setContinueParameter', 'dieUsage',
146 'addTables', 'addFields', 'addWhere', 'select', 'setResultValues' ] )
147 ->getMock()
148 );
149 $mock->expects( $this->any() )
150 ->method( 'extractRequestParams' )
151 ->willReturn( $requestParams );
152 $mock->expects( $this->any() )
153 ->method( 'getTitles' )
154 ->willReturn( $titles );
155 $mock->expects( $this->any() )
156 ->method( 'select' )
157 ->willReturn( new FakeResultWrapper( $queryResults ) );
158
159 // continue page ID is not found
160 if ( isset( $requestParams['continue'] )
161 && $requestParams['continue'] > count( $titles )
162 ) {
163 $mock->expects( $this->exactly( 1 ) )
164 ->method( 'dieUsage' );
165 }
166
167 $originalRequested = in_array( 'original', $requestParams['prop'] );
168 $this->assertTrue( $this->hasExpectedProperties( $queryResults, $originalRequested ) );
169
170 $license = isset( $requestParams['license'] ) ? $requestParams['license'] : 'free';
171 if ( $license == ApiQueryPageImages::PARAM_LICENSE_ANY ) {
172 $propName = [ PageImages::getPropName( true ), PageImages::getPropName( false ) ];
173 } else {
174 $propName = PageImages::getPropName( true );
175 }
176 $mock->expects( $this->exactly( count( $queryPageIds ) > 0 ? 1 : 0 ) )
177 ->method( 'addWhere' )
178 ->with( [ 'pp_page' => $queryPageIds, 'pp_propname' => $propName ] );
179
180 $mock->expects( $this->exactly( $setResultValueCount ) )
181 ->method( 'setResultValues' );
182
183 $mock->execute();
184 }
185
186 public function provideExecute() {
187 return [
188 [
189 [ 'prop' => [ 'thumbnail' ], 'thumbsize' => 100, 'limit' => 10, 'license' => 'any' ],
190 [ Title::newFromText( 'Page 1' ), Title::newFromText( 'Page 2' ) ],
191 [ 0, 1 ],
192 [
193 (object)[ 'pp_page' => 0, 'pp_value' => 'A_Free.jpg',
194 'pp_propname' => PageImages::PROP_NAME_FREE ],
195 (object)[ 'pp_page' => 0, 'pp_value' => 'A.jpg',
196 'pp_propname' => PageImages::PROP_NAME ],
197 (object)[ 'pp_page' => 1, 'pp_value' => 'B.jpg',
198 'pp_propname' => PageImages::PROP_NAME ],
199 ],
200 2
201 ],
202 [
203 [ 'prop' => [ 'thumbnail' ], 'thumbsize' => 200, 'limit' => 10 ],
204 [],
205 [],
206 [],
207 0
208 ],
209 [
210 [ 'prop' => [ 'thumbnail' ], 'continue' => 1, 'thumbsize' => 400,
211 'limit' => 10, 'license' => 'any' ],
212 [ Title::newFromText( 'Page 1' ), Title::newFromText( 'Page 2' ) ],
213 [ 1 ],
214 [
215 (object)[ 'pp_page' => 1, 'pp_value' => 'B_Free.jpg',
216 'pp_propname' => PageImages::PROP_NAME_FREE ],
217 (object)[ 'pp_page' => 1, 'pp_value' => 'B.jpg',
218 'pp_propname' => PageImages::PROP_NAME ],
219 ],
220 1
221 ],
222 [
223 [ 'prop' => [ 'thumbnail' ], 'thumbsize' => 500, 'limit' => 10, 'license' => 'any' ],
224 [ Title::newFromText( 'Page 1' ), Title::newFromText( 'Page 2' ) ],
225 [ 0, 1 ],
226 [
227 (object)[ 'pp_page' => 1, 'pp_value' => 'B_Free.jpg',
228 'pp_propname' => PageImages::PROP_NAME ],
229 ],
230 1
231 ],
232 [
233 [ 'prop' => [ 'thumbnail' ], 'continue' => 1, 'thumbsize' => 500,
234 'limit' => 10, 'license' => 'any' ],
235 [ Title::newFromText( 'Page 1' ), Title::newFromText( 'Page 2' ) ],
236 [ 1 ],
237 [
238 (object)[ 'pp_page' => 1, 'pp_value' => 'B_Free.jpg',
239 'pp_propname' => PageImages::PROP_NAME_FREE ],
240 ],
241 1
242 ],
243 [
244 [ 'prop' => [ 'thumbnail' ], 'thumbsize' => 510, 'limit' => 10, 'license' => 'free' ],
245 [ Title::newFromText( 'Page 1' ), Title::newFromText( 'Page 2' ) ],
246 [ 0, 1 ],
247 [],
248 0
249 ],
250 [
251 [ 'prop' => [ 'thumbnail' ], 'thumbsize' => 510, 'limit' => 10, 'license' => 'free' ],
252 [ Title::newFromText( 'Page 1' ), Title::newFromText( 'Page 2' ) ],
253 [ 0, 1 ],
254 [
255 (object)[ 'pp_page' => 0, 'pp_value' => 'A_Free.jpg',
256 'pp_propname' => PageImages::PROP_NAME_FREE ],
257 (object)[ 'pp_page' => 1, 'pp_value' => 'B_Free.jpg',
258 'pp_propname' => PageImages::PROP_NAME_FREE ],
259 ],
260 2
261 ],
262 [
263 [ 'prop' => [ 'thumbnail', 'original' ], 'thumbsize' => 510,
264 'limit' => 10, 'license' => 'free' ],
265 [ Title::newFromText( 'Page 1' ), Title::newFromText( 'Page 2' ) ],
266 [ 0, 1 ],
267 [
268 (object)[
269 'pp_page' => 0, 'pp_value' => 'A_Free.jpg',
270 'pp_value_original' => 'A_Free_original.jpg', 'pp_original_width' => 80,
271 'pp_original_height' => 80, 'pp_propname' => PageImages::PROP_NAME_FREE
272 ],
273 (object)[
274 'pp_page' => 1, 'pp_value' => 'B_Free.jpg',
275 'pp_value_original' => 'B_Free_original.jpg', 'pp_original_width' => 80,
276 'pp_original_height' => 80, 'pp_propname' => PageImages::PROP_NAME_FREE
277 ],
278 ],
279 2
280 ],
281 ];
282 }
283
289 public function testGetPropName( $license, $expected ) {
290 $this->assertEquals( $expected, ApiQueryPageImagesProxyMock::getPropNames( $license ) );
291 }
292
293 public function provideGetPropName() {
294 return [
295 [ 'free', \PageImages::PROP_NAME_FREE ],
296 [ 'any', [ \PageImages::PROP_NAME_FREE, \PageImages::PROP_NAME ] ]
297 ];
298 }
299
300 private function hasExpectedProperties( $queryResults, $originalRequested ) {
301 if ( $originalRequested ) {
302 return $this->allResultsHaveProperty( $queryResults, 'pp_value_original' )
303 && $this->allResultsHaveProperty( $queryResults, 'pp_original_width' )
304 && $this->allResultsHaveProperty( $queryResults, 'pp_original_height' );
305 } else {
306 return $this->noResultsHaveProperty( $queryResults, 'pp_value_original' )
307 && $this->noResultsHaveProperty( $queryResults, 'pp_original_width' )
308 && $this->noResultsHaveProperty( $queryResults, 'pp_original_height' );
309 }
310 }
311
312 private function noResultsHaveProperty( $queryResults, $propName ) {
313 foreach ( $queryResults as $result ) {
314 if ( property_exists( $result, $propName ) ) {
315 return false;
316 }
317 }
318 return true;
319 }
320
321 private function allResultsHaveProperty( $queryResults, $propName ) {
322 foreach ( $queryResults as $result ) {
323 if ( !property_exists( $result, $propName ) ) {
324 return false;
325 }
326 }
327 return true;
328 }
329}
Expose image information for a page via a new prop=pageimages API.
static getPropNames( $license)
Get property names used in page_props table.If the license is free, then only the free property name ...
testGetPropName( $license, $expected)
@dataProvider provideGetPropName
testGetTitles( $titles, $missingTitlesByNamespace, $expected)
@dataProvider provideGetTitles
hasExpectedProperties( $queryResults, $originalRequested)
testExecute( $requestParams, $titles, $queryPageIds, $queryResults, $setResultValueCount)
@dataProvider provideExecute
Represents a title within MediaWiki.
Definition Title.php:42
Overloads the relevant methods of the real ResultsWrapper so it doesn't go anywhere near an actual da...
const NS_FILE
Definition Defines.php:75
const NS_TALK
Definition Defines.php:70
$context
Definition load.php:45