5 require_once
'ApiQueryPageImagesProxyMock.php';
11 use Wikimedia\TestingAccessWrapper;
25 $config = new \HashConfig( [
26 'PageImagesAPIDefaultLicense' =>
'free'
29 $context = $this->getMockBuilder(
'IContextSource' )
30 ->disableOriginalConstructor()
34 ->method(
'getConfig' )
35 ->willReturn( $config );
37 $main = $this->getMockBuilder(
'ApiMain' )
38 ->disableOriginalConstructor()
40 $main->expects( $this->once() )
41 ->method(
'getContext' )
42 ->will( $this->returnValue(
$context ) );
44 $query = $this->getMockBuilder(
'ApiQuery' )
45 ->disableOriginalConstructor()
47 $query->expects( $this->once() )
49 ->will( $this->returnValue( $main ) );
56 $this->assertInstanceOf(
'ApiQueryPageImages', $instance );
61 $this->assertSame(
'public', $instance->getCacheMode( [] ) );
66 $params = $instance->getAllowedParams();
67 $this->assertInternalType(
'array', $params );
68 $this->assertNotEmpty( $params );
69 $this->assertContainsOnly(
'array', $params );
70 $this->assertArrayHasKey(
'limit', $params );
76 $this->assertArrayHasKey(
'license', $params );
85 public function testGetTitles( $titles, $missingTitlesByNamespace, $expected ) {
86 $pageSet = $this->getMockBuilder(
'ApiPageSet' )
87 ->disableOriginalConstructor()
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 ) );
97 $this->assertEquals( $expected, $queryPageImages->getTitles() );
139 public function testExecute( $requestParams, $titles, $queryPageIds,
140 $queryResults, $setResultValueCount
142 $mock = TestingAccessWrapper::newFromObject(
143 $this->getMockBuilder( ApiQueryPageImages::class )
144 ->disableOriginalConstructor()
145 ->setMethods( [
'extractRequestParams',
'getTitles',
'setContinueParameter',
'dieUsage',
146 'addTables',
'addFields',
'addWhere',
'select',
'setResultValues' ] )
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() )
160 if ( isset( $requestParams[
'continue'] )
161 && $requestParams[
'continue'] > count( $titles )
163 $mock->expects( $this->exactly( 1 ) )
164 ->method(
'dieUsage' );
167 $originalRequested = in_array(
'original', $requestParams[
'prop'] );
170 $license = isset( $requestParams[
'license'] ) ? $requestParams[
'license'] :
'free';
176 $mock->expects( $this->exactly( count( $queryPageIds ) > 0 ? 1 : 0 ) )
177 ->method(
'addWhere' )
178 ->with( [
'pp_page' => $queryPageIds,
'pp_propname' => $propName ] );
180 $mock->expects( $this->exactly( $setResultValueCount ) )
181 ->method(
'setResultValues' );
189 [
'prop' => [
'thumbnail' ],
'thumbsize' => 100,
'limit' => 10,
'license' =>
'any' ],
193 (object)[
'pp_page' => 0,
'pp_value' =>
'A_Free.jpg',
195 (
object)[
'pp_page' => 0,
'pp_value' =>
'A.jpg',
197 (object)[
'pp_page' => 1,
'pp_value' =>
'B.jpg',
203 [
'prop' => [
'thumbnail' ],
'thumbsize' => 200,
'limit' => 10 ],
210 [
'prop' => [
'thumbnail' ],
'continue' => 1,
'thumbsize' => 400,
211 'limit' => 10,
'license' =>
'any' ],
215 (object)[
'pp_page' => 1,
'pp_value' =>
'B_Free.jpg',
217 (
object)[
'pp_page' => 1,
'pp_value' =>
'B.jpg',
223 [
'prop' => [
'thumbnail' ],
'thumbsize' => 500,
'limit' => 10,
'license' =>
'any' ],
227 (object)[
'pp_page' => 1,
'pp_value' =>
'B_Free.jpg',
233 [
'prop' => [
'thumbnail' ],
'continue' => 1,
'thumbsize' => 500,
234 'limit' => 10,
'license' =>
'any' ],
238 (object)[
'pp_page' => 1,
'pp_value' =>
'B_Free.jpg',
244 [
'prop' => [
'thumbnail' ],
'thumbsize' => 510,
'limit' => 10,
'license' =>
'free' ],
251 [
'prop' => [
'thumbnail' ],
'thumbsize' => 510,
'limit' => 10,
'license' =>
'free' ],
255 (object)[
'pp_page' => 0,
'pp_value' =>
'A_Free.jpg',
257 (
object)[
'pp_page' => 1,
'pp_value' =>
'B_Free.jpg',
263 [
'prop' => [
'thumbnail',
'original' ],
'thumbsize' => 510,
264 'limit' => 10,
'license' =>
'free' ],
269 'pp_page' => 0,
'pp_value' =>
'A_Free.jpg',
270 'pp_value_original' =>
'A_Free_original.jpg',
'pp_original_width' => 80,
274 'pp_page' => 1,
'pp_value' =>
'B_Free.jpg',
275 'pp_value_original' =>
'B_Free_original.jpg',
'pp_original_width' => 80,
301 if ( $originalRequested ) {
313 foreach ( $queryResults as $result ) {
314 if ( property_exists( $result, $propName ) ) {
322 foreach ( $queryResults as $result ) {
323 if ( !property_exists( $result, $propName ) ) {