MediaWiki  1.34.0
ApiQueryPageImagesTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace PageImages\Tests;
4 
5 require_once 'ApiQueryPageImagesProxyMock.php';
6 
8 use PageImages;
9 use Title;
11 use Wikimedia\TestingAccessWrapper;
12 
22 class 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 ],
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 }
PageImages\Tests\ApiQueryPageImagesTest\provideGetTitles
provideGetTitles()
Definition: ApiQueryPageImagesTest.php:100
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:316
PageImages\Tests\ApiQueryPageImagesTest\provideExecute
provideExecute()
Definition: ApiQueryPageImagesTest.php:186
PageImages\PROP_NAME_FREE
const PROP_NAME_FREE
Page property used to store the best free page image information Note changing this value is not advi...
Definition: PageImages.php:28
PageImages\Tests\ApiQueryPageImagesTest\testGetTitles
testGetTitles( $titles, $missingTitlesByNamespace, $expected)
@dataProvider provideGetTitles
Definition: ApiQueryPageImagesTest.php:85
ApiBase\PARAM_TYPE
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition: ApiBase.php:94
PageImages\Tests\ApiQueryPageImagesTest\noResultsHaveProperty
noResultsHaveProperty( $queryResults, $propName)
Definition: ApiQueryPageImagesTest.php:312
PageImages\Tests\ApiQueryPageImagesTest\testExecute
testExecute( $requestParams, $titles, $queryPageIds, $queryResults, $setResultValueCount)
@dataProvider provideExecute
Definition: ApiQueryPageImagesTest.php:139
NS_FILE
const NS_FILE
Definition: Defines.php:66
PageImages\Tests\ApiQueryPageImagesTest\testConstructor
testConstructor()
Definition: ApiQueryPageImagesTest.php:54
PageImages\Tests\ApiQueryPageImagesProxyMock
Definition: ApiQueryPageImagesProxyMock.php:8
Wikimedia\Rdbms\FakeResultWrapper
Overloads the relevant methods of the real ResultsWrapper so it doesn't go anywhere near an actual da...
Definition: FakeResultWrapper.php:11
ApiQueryPageImages\PARAM_LICENSE_ANY
const PARAM_LICENSE_ANY
@const API parameter value for images with any type of license
Definition: ApiQueryPageImages.php:24
PageImages\Tests\ApiQueryPageImagesTest\testGetCacheMode
testGetCacheMode()
Definition: ApiQueryPageImagesTest.php:59
PageImages
ApiBase\PARAM_MIN
const PARAM_MIN
(integer) Lowest value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition: ApiBase.php:106
ApiQueryPageImages
Expose image information for a page via a new prop=pageimages API.
Definition: ApiQueryPageImages.php:14
PageImages\Tests\ApiQueryPageImagesTest
@covers ApiQueryPageImages
Definition: ApiQueryPageImagesTest.php:22
PageImages\Tests\ApiQueryPageImagesTest\allResultsHaveProperty
allResultsHaveProperty( $queryResults, $propName)
Definition: ApiQueryPageImagesTest.php:321
ApiBase\PARAM_MAX
const PARAM_MAX
(integer) Max value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition: ApiBase.php:97
PageImages\Tests\ApiQueryPageImagesTest\testGetAllowedParams
testGetAllowedParams()
Definition: ApiQueryPageImagesTest.php:64
PageImages\Tests\ApiQueryPageImagesProxyMock\getPropNames
static getPropNames( $license)
Get property names used in page_props table.If the license is free, then only the free property name ...
Definition: ApiQueryPageImagesProxyMock.php:26
PageImages\Tests
Definition: ApiQueryPageImagesProxyMock.php:3
PageImages\Tests\ApiQueryPageImagesTest\newInstance
newInstance()
Definition: ApiQueryPageImagesTest.php:24
PageImages\Tests\ApiQueryPageImagesTest\hasExpectedProperties
hasExpectedProperties( $queryResults, $originalRequested)
Definition: ApiQueryPageImagesTest.php:300
PageImages\PROP_NAME
const PROP_NAME
Page property used to store the best page image information.
Definition: PageImages.php:20
$context
$context
Definition: load.php:45
PageImages\Tests\ApiQueryPageImagesTest\provideGetPropName
provideGetPropName()
Definition: ApiQueryPageImagesTest.php:293
Title
Represents a title within MediaWiki.
Definition: Title.php:42
PageImages\getPropName
static getPropName( $isFree)
Get property name used in page_props table.
Definition: PageImages.php:38
ApiBase\PARAM_DFLT
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:55
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:58
ApiBase\PARAM_MAX2
const PARAM_MAX2
(integer) Max value allowed for the parameter for users with the apihighlimits right,...
Definition: ApiBase.php:103
NS_TALK
const NS_TALK
Definition: Defines.php:61
PageImages\Tests\ApiQueryPageImagesTest\testGetPropName
testGetPropName( $license, $expected)
@dataProvider provideGetPropName
Definition: ApiQueryPageImagesTest.php:289