| 1 |
<?php |
| 2 |
|
| 3 |
namespace PageImages\Tests; |
| 4 |
|
| 5 |
use MediaWiki\Api\ApiMain; |
| 6 |
use MediaWiki\Api\ApiPageSet; |
| 7 |
use MediaWiki\Api\ApiQuery; |
| 8 |
use MediaWiki\Config\HashConfig; |
| 9 |
use MediaWiki\Context\IContextSource; |
| 10 |
use MediaWiki\Page\PageReferenceValue; |
| 11 |
use MediaWikiIntegrationTestCase; |
| 12 |
use PageImages\ApiQueryPageImages; |
| 13 |
use PageImages\PageImages; |
| 14 |
use Wikimedia\ParamValidator\ParamValidator; |
| 15 |
use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
| 16 |
use Wikimedia\Rdbms\FakeResultWrapper; |
| 17 |
use Wikimedia\TestingAccessWrapper; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class ApiQueryPageImagesTest extends MediaWikiIntegrationTestCase { |
| 29 |
|
| 30 |
private function newInstance() { |
| 31 |
$config = new HashConfig( [ |
| 32 |
'PageImagesAPIDefaultLicense' => 'free' |
| 33 |
] ); |
| 34 |
|
| 35 |
$context = $this->createMock( IContextSource::class ); |
| 36 |
|
| 37 |
$context->method( 'getConfig' ) |
| 38 |
->willReturn( $config ); |
| 39 |
|
| 40 |
$main = $this->createMock( ApiMain::class ); |
| 41 |
$main->expects( $this->once() ) |
| 42 |
->method( 'getContext' ) |
| 43 |
->willReturn( $context ); |
| 44 |
|
| 45 |
$query = $this->createMock( ApiQuery::class ); |
| 46 |
$query->expects( $this->once() ) |
| 47 |
->method( 'getMain' ) |
| 48 |
->willReturn( $main ); |
| 49 |
|
| 50 |
return new ApiQueryPageImages( $query, '', $this->getServiceContainer()->getRepoGroup() ); |
| 51 |
} |
| 52 |
|
| 53 |
public function testConstructor() { |
| 54 |
$instance = $this->newInstance(); |
| 55 |
$this->assertInstanceOf( ApiQueryPageImages::class, $instance ); |
| 56 |
} |
| 57 |
|
| 58 |
public function testGetCacheMode() { |
| 59 |
$instance = $this->newInstance(); |
| 60 |
$this->assertSame( 'public', $instance->getCacheMode( [] ) ); |
| 61 |
} |
| 62 |
|
| 63 |
public function testGetAllowedParams() { |
| 64 |
$instance = $this->newInstance(); |
| 65 |
$params = $instance->getAllowedParams(); |
| 66 |
|
| 67 |
$this->assertIsArray( $params ); |
| 68 |
$this->assertNotEmpty( $params ); |
| 69 |
$this->assertContainsOnly( 'array', $params ); |
| 70 |
$this->assertArrayHasKey( 'limit', $params ); |
| 71 |
$this->assertSame( 50, $params['limit'][ParamValidator::PARAM_DEFAULT] ); |
| 72 |
$this->assertSame( 'limit', $params['limit'][ParamValidator::PARAM_TYPE] ); |
| 73 |
$this->assertSame( 1, $params['limit'][IntegerDef::PARAM_MIN] ); |
| 74 |
$this->assertSame( 50, $params['limit'][IntegerDef::PARAM_MAX] ); |
| 75 |
$this->assertSame( 100, $params['limit'][IntegerDef::PARAM_MAX2] ); |
| 76 |
$this->assertArrayHasKey( 'license', $params ); |
| 77 |
$this->assertSame( [ 'free', 'any' ], $params['license'][ParamValidator::PARAM_TYPE] ); |
| 78 |
$this->assertSame( 'free', $params['license'][ParamValidator::PARAM_DEFAULT] ); |
| 79 |
$this->assertFalse( $params['license'][ParamValidator::PARAM_ISMULTI] ); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public function testGetTitles( $pageNames, $missingTitlesByNamespace, $expectedNames ) { |
| 86 |
$titleParser = $this->getServiceContainer()->getTitleParser(); |
| 87 |
$titles = []; |
| 88 |
foreach ( $pageNames as $pageName ) { |
| 89 |
$titleValue = $titleParser->parseTitle( $pageName ); |
| 90 |
$titles[] = PageReferenceValue::localReference( $titleValue->getNamespace(), $titleValue->getDBkey() ); |
| 91 |
} |
| 92 |
$expected = []; |
| 93 |
foreach ( $expectedNames as $id => $expectedName ) { |
| 94 |
$titleValue = $titleParser->parseTitle( $expectedName ); |
| 95 |
$expected[$id] = PageReferenceValue::localReference( $titleValue->getNamespace(), $titleValue->getDBkey() ); |
| 96 |
} |
| 97 |
|
| 98 |
$pageSet = $this->createMock( ApiPageSet::class ); |
| 99 |
$pageSet->method( 'getGoodPages' ) |
| 100 |
->willReturn( $titles ); |
| 101 |
$pageSet->method( 'getMissingTitlesByNamespace' ) |
| 102 |
->willReturn( $missingTitlesByNamespace ); |
| 103 |
$queryPageImages = new ApiQueryPageImagesProxyMock( $pageSet ); |
| 104 |
|
| 105 |
$this->assertEquals( $expected, $queryPageImages->getTitles() ); |
| 106 |
} |
| 107 |
|
| 108 |
public static function provideGetTitles() { |
| 109 |
return [ |
| 110 |
[ |
| 111 |
[ 'Foo' ], |
| 112 |
[], |
| 113 |
[ 'Foo' ], |
| 114 |
], |
| 115 |
[ |
| 116 |
[ 'Foo' ], |
| 117 |
[ |
| 118 |
NS_TALK => [ |
| 119 |
'Bar' => -1, |
| 120 |
], |
| 121 |
], |
| 122 |
[ 'Foo' ], |
| 123 |
], |
| 124 |
[ |
| 125 |
[ 'Foo' ], |
| 126 |
[ |
| 127 |
NS_FILE => [ |
| 128 |
'Bar' => -1, |
| 129 |
], |
| 130 |
], |
| 131 |
[ |
| 132 |
0 => 'Foo', |
| 133 |
-1 => 'File:Bar', |
| 134 |
], |
| 135 |
], |
| 136 |
]; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public function testExecute( $requestParams, $pageNames, $queryPageIds, |
| 148 |
$queryResults, $setResultValueCount |
| 149 |
) { |
| 150 |
$titles = []; |
| 151 |
foreach ( $pageNames as $pageName ) { |
| 152 |
$titles[] = PageReferenceValue::localReference( NS_MAIN, $pageName ); |
| 153 |
} |
| 154 |
$mock = TestingAccessWrapper::newFromObject( |
| 155 |
$this->getMockBuilder( ApiQueryPageImages::class ) |
| 156 |
->disableOriginalConstructor() |
| 157 |
->onlyMethods( [ 'extractRequestParams', 'getTitles', 'dieWithError', |
| 158 |
'addTables', 'addFields', 'addWhere', 'select', 'setResultValues' ] ) |
| 159 |
->getMock() |
| 160 |
); |
| 161 |
$mock->method( 'extractRequestParams' ) |
| 162 |
->willReturn( $requestParams ); |
| 163 |
$mock->method( 'getTitles' ) |
| 164 |
->willReturn( $titles ); |
| 165 |
$mock->method( 'select' ) |
| 166 |
->willReturn( new FakeResultWrapper( $queryResults ) ); |
| 167 |
|
| 168 |
|
| 169 |
if ( isset( $requestParams['continue'] ) |
| 170 |
&& $requestParams['continue'] > count( $titles ) |
| 171 |
) { |
| 172 |
$mock->expects( $this->once() ) |
| 173 |
->method( 'dieWithError' ); |
| 174 |
} |
| 175 |
|
| 176 |
$originalRequested = in_array( 'original', $requestParams['prop'] ); |
| 177 |
$this->assertTrue( $this->hasExpectedProperties( $queryResults, $originalRequested ) ); |
| 178 |
|
| 179 |
$license = $requestParams['license'] ?? 'free'; |
| 180 |
if ( $license == PageImages::LICENSE_ANY ) { |
| 181 |
$propName = [ PageImages::getPropName( true ), PageImages::getPropName( false ) ]; |
| 182 |
} else { |
| 183 |
$propName = PageImages::getPropName( true ); |
| 184 |
} |
| 185 |
$mock->expects( $this->exactly( count( $queryPageIds ) > 0 ? 1 : 0 ) ) |
| 186 |
->method( 'addWhere' ) |
| 187 |
->with( [ 'pp_page' => $queryPageIds, 'pp_propname' => $propName ] ); |
| 188 |
|
| 189 |
$mock->expects( $this->exactly( $setResultValueCount ) ) |
| 190 |
->method( 'setResultValues' ); |
| 191 |
|
| 192 |
$mock->execute(); |
| 193 |
} |
| 194 |
|
| 195 |
public static function provideExecute() { |
| 196 |
return [ |
| 197 |
[ |
| 198 |
[ 'prop' => [ 'thumbnail' ], 'thumbsize' => 100, 'limit' => 10, |
| 199 |
'license' => 'any', 'langcode' => null ], |
| 200 |
[ 'Page 1', 'Page 2' ], |
| 201 |
[ 0, 1 ], |
| 202 |
[ |
| 203 |
(object)[ 'pp_page' => 0, 'pp_value' => 'A_Free.jpg', |
| 204 |
'pp_propname' => PageImages::PROP_NAME_FREE ], |
| 205 |
(object)[ 'pp_page' => 0, 'pp_value' => 'A.jpg', |
| 206 |
'pp_propname' => PageImages::PROP_NAME ], |
| 207 |
(object)[ 'pp_page' => 1, 'pp_value' => 'B.jpg', |
| 208 |
'pp_propname' => PageImages::PROP_NAME ], |
| 209 |
], |
| 210 |
2 |
| 211 |
], |
| 212 |
[ |
| 213 |
[ 'prop' => [ 'thumbnail' ], 'thumbsize' => 200, 'limit' => 10, 'langcode' => null ], |
| 214 |
[], |
| 215 |
[], |
| 216 |
[], |
| 217 |
0 |
| 218 |
], |
| 219 |
[ |
| 220 |
[ 'prop' => [ 'thumbnail' ], 'continue' => 1, 'thumbsize' => 400, |
| 221 |
'limit' => 10, 'license' => 'any', 'langcode' => null ], |
| 222 |
[ 'Page 1', 'Page 2' ], |
| 223 |
[ 1 ], |
| 224 |
[ |
| 225 |
(object)[ 'pp_page' => 1, 'pp_value' => 'B_Free.jpg', |
| 226 |
'pp_propname' => PageImages::PROP_NAME_FREE ], |
| 227 |
(object)[ 'pp_page' => 1, 'pp_value' => 'B.jpg', |
| 228 |
'pp_propname' => PageImages::PROP_NAME ], |
| 229 |
], |
| 230 |
1 |
| 231 |
], |
| 232 |
[ |
| 233 |
[ 'prop' => [ 'thumbnail' ], 'thumbsize' => 500, 'limit' => 10, |
| 234 |
'license' => 'any', 'langcode' => 'en' ], |
| 235 |
[ 'Page 1', 'Page 2' ], |
| 236 |
[ 0, 1 ], |
| 237 |
[ |
| 238 |
(object)[ 'pp_page' => 1, 'pp_value' => 'B_Free.jpg', |
| 239 |
'pp_propname' => PageImages::PROP_NAME ], |
| 240 |
], |
| 241 |
1 |
| 242 |
], |
| 243 |
[ |
| 244 |
[ 'prop' => [ 'thumbnail' ], 'continue' => 1, 'thumbsize' => 500, |
| 245 |
'limit' => 10, 'license' => 'any', 'langcode' => 'de' ], |
| 246 |
[ 'Page 1', 'Page 2' ], |
| 247 |
[ 1 ], |
| 248 |
[ |
| 249 |
(object)[ 'pp_page' => 1, 'pp_value' => 'B_Free.jpg', |
| 250 |
'pp_propname' => PageImages::PROP_NAME_FREE ], |
| 251 |
], |
| 252 |
1 |
| 253 |
], |
| 254 |
[ |
| 255 |
[ 'prop' => [ 'thumbnail' ], 'thumbsize' => 510, 'limit' => 10, |
| 256 |
'license' => 'free', 'langcode' => 'de' ], |
| 257 |
[ 'Page 1', 'Page 2' ], |
| 258 |
[ 0, 1 ], |
| 259 |
[], |
| 260 |
0 |
| 261 |
], |
| 262 |
[ |
| 263 |
[ 'prop' => [ 'thumbnail' ], 'thumbsize' => 510, 'limit' => 10, |
| 264 |
'license' => 'free', 'langcode' => 'en' ], |
| 265 |
[ 'Page 1', 'Page 2' ], |
| 266 |
[ 0, 1 ], |
| 267 |
[ |
| 268 |
(object)[ 'pp_page' => 0, 'pp_value' => 'A_Free.jpg', |
| 269 |
'pp_propname' => PageImages::PROP_NAME_FREE ], |
| 270 |
(object)[ 'pp_page' => 1, 'pp_value' => 'B_Free.jpg', |
| 271 |
'pp_propname' => PageImages::PROP_NAME_FREE ], |
| 272 |
], |
| 273 |
2 |
| 274 |
], |
| 275 |
[ |
| 276 |
[ 'prop' => [ 'thumbnail', 'original' ], 'thumbsize' => 510, |
| 277 |
'limit' => 10, 'license' => 'free', 'langcode' => 'en' ], |
| 278 |
[ 'Page 1', 'Page 2' ], |
| 279 |
[ 0, 1 ], |
| 280 |
[ |
| 281 |
(object)[ |
| 282 |
'pp_page' => 0, 'pp_value' => 'A_Free.jpg', |
| 283 |
'pp_value_original' => 'A_Free_original.jpg', 'pp_original_width' => 80, |
| 284 |
'pp_original_height' => 80, 'pp_propname' => PageImages::PROP_NAME_FREE |
| 285 |
], |
| 286 |
(object)[ |
| 287 |
'pp_page' => 1, 'pp_value' => 'B_Free.jpg', |
| 288 |
'pp_value_original' => 'B_Free_original.jpg', 'pp_original_width' => 80, |
| 289 |
'pp_original_height' => 80, 'pp_propname' => PageImages::PROP_NAME_FREE |
| 290 |
], |
| 291 |
], |
| 292 |
2 |
| 293 |
], |
| 294 |
]; |
| 295 |
} |
| 296 |
|
| 297 |
private function hasExpectedProperties( $queryResults, $originalRequested ) { |
| 298 |
if ( $originalRequested ) { |
| 299 |
return $this->allResultsHaveProperty( $queryResults, 'pp_value_original' ) |
| 300 |
&& $this->allResultsHaveProperty( $queryResults, 'pp_original_width' ) |
| 301 |
&& $this->allResultsHaveProperty( $queryResults, 'pp_original_height' ); |
| 302 |
} else { |
| 303 |
return $this->noResultsHaveProperty( $queryResults, 'pp_value_original' ) |
| 304 |
&& $this->noResultsHaveProperty( $queryResults, 'pp_original_width' ) |
| 305 |
&& $this->noResultsHaveProperty( $queryResults, 'pp_original_height' ); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
private function noResultsHaveProperty( $queryResults, $propName ) { |
| 310 |
foreach ( $queryResults as $result ) { |
| 311 |
if ( property_exists( $result, $propName ) ) { |
| 312 |
return false; |
| 313 |
} |
| 314 |
} |
| 315 |
return true; |
| 316 |
} |
| 317 |
|
| 318 |
private function allResultsHaveProperty( $queryResults, $propName ) { |
| 319 |
foreach ( $queryResults as $result ) { |
| 320 |
if ( !property_exists( $result, $propName ) ) { |
| 321 |
return false; |
| 322 |
} |
| 323 |
} |
| 324 |
return true; |
| 325 |
} |
| 326 |
} |