2015-10-02 08:58:54 +00:00
|
|
|
<?php
|
|
|
|
|
2015-10-07 15:18:54 +00:00
|
|
|
namespace PageImages\Tests;
|
2015-10-02 08:58:54 +00:00
|
|
|
|
2021-03-20 05:22:04 +00:00
|
|
|
use ApiBase;
|
2020-04-19 22:41:19 +00:00
|
|
|
use PageImages\ApiQueryPageImages;
|
|
|
|
use PageImages\PageImages;
|
2021-03-20 05:22:04 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2015-10-02 08:58:54 +00:00
|
|
|
use Title;
|
2018-01-04 22:41:54 +00:00
|
|
|
use Wikimedia\Rdbms\FakeResultWrapper;
|
2017-04-20 07:48:48 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
2015-10-02 08:58:54 +00:00
|
|
|
|
2015-10-07 15:18:54 +00:00
|
|
|
/**
|
2020-10-22 20:04:23 +00:00
|
|
|
* @covers \PageImages\ApiQueryPageImages
|
2015-10-07 15:18:54 +00:00
|
|
|
*
|
|
|
|
* @group PageImages
|
|
|
|
*
|
2018-05-25 04:42:29 +00:00
|
|
|
* @license WTFPL
|
2015-11-16 14:59:34 +00:00
|
|
|
* @author Sam Smith
|
2017-11-24 07:33:49 +00:00
|
|
|
* @author Thiemo Kreuz
|
2015-10-07 15:18:54 +00:00
|
|
|
*/
|
2021-03-20 05:22:04 +00:00
|
|
|
class ApiQueryPageImagesTest extends TestCase {
|
2015-10-02 08:58:54 +00:00
|
|
|
|
2015-10-26 10:41:13 +00:00
|
|
|
private function newInstance() {
|
2017-05-20 14:29:31 +00:00
|
|
|
$config = new \HashConfig( [
|
|
|
|
'PageImagesAPIDefaultLicense' => 'free'
|
2017-05-30 19:49:44 +00:00
|
|
|
] );
|
2017-05-20 14:29:31 +00:00
|
|
|
|
2021-05-04 14:48:45 +00:00
|
|
|
$context = $this->createMock( \IContextSource::class );
|
2015-10-07 15:18:54 +00:00
|
|
|
|
2021-05-04 14:48:45 +00:00
|
|
|
$context->method( 'getConfig' )
|
2017-05-20 14:29:31 +00:00
|
|
|
->willReturn( $config );
|
|
|
|
|
2021-04-08 19:15:01 +00:00
|
|
|
$main = $this->getMockBuilder( \ApiMain::class )
|
2015-10-07 15:18:54 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$main->expects( $this->once() )
|
|
|
|
->method( 'getContext' )
|
2021-03-20 05:22:04 +00:00
|
|
|
->willReturn( $context );
|
2015-10-07 15:18:54 +00:00
|
|
|
|
2021-04-08 19:15:01 +00:00
|
|
|
$query = $this->getMockBuilder( \ApiQuery::class )
|
2015-10-07 15:18:54 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$query->expects( $this->once() )
|
|
|
|
->method( 'getMain' )
|
2021-03-20 05:22:04 +00:00
|
|
|
->willReturn( $main );
|
2015-10-07 15:18:54 +00:00
|
|
|
|
|
|
|
return new ApiQueryPageImages( $query, '' );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConstructor() {
|
2015-10-26 10:41:13 +00:00
|
|
|
$instance = $this->newInstance();
|
2020-04-19 22:41:19 +00:00
|
|
|
$this->assertInstanceOf( ApiQueryPageImages::class, $instance );
|
2015-10-07 15:18:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetCacheMode() {
|
2015-10-26 10:41:13 +00:00
|
|
|
$instance = $this->newInstance();
|
2016-12-02 00:49:13 +00:00
|
|
|
$this->assertSame( 'public', $instance->getCacheMode( [] ) );
|
2015-10-07 15:18:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetAllowedParams() {
|
2015-10-26 10:41:13 +00:00
|
|
|
$instance = $this->newInstance();
|
|
|
|
$params = $instance->getAllowedParams();
|
2021-03-20 05:22:04 +00:00
|
|
|
|
2020-01-14 08:47:58 +00:00
|
|
|
$this->assertIsArray( $params );
|
2015-10-07 15:18:54 +00:00
|
|
|
$this->assertNotEmpty( $params );
|
|
|
|
$this->assertContainsOnly( 'array', $params );
|
2017-05-19 13:04:26 +00:00
|
|
|
$this->assertArrayHasKey( 'limit', $params );
|
2021-05-04 16:41:49 +00:00
|
|
|
$this->assertSame( 50, $params['limit'][ApiBase::PARAM_DFLT] );
|
|
|
|
$this->assertSame( 'limit', $params['limit'][ApiBase::PARAM_TYPE] );
|
|
|
|
$this->assertSame( 1, $params['limit'][ApiBase::PARAM_MIN] );
|
|
|
|
$this->assertSame( 50, $params['limit'][ApiBase::PARAM_MAX] );
|
|
|
|
$this->assertSame( 100, $params['limit'][ApiBase::PARAM_MAX2] );
|
2016-11-10 00:02:00 +00:00
|
|
|
$this->assertArrayHasKey( 'license', $params );
|
2021-05-04 16:41:49 +00:00
|
|
|
$this->assertSame( [ 'free', 'any' ], $params['license'][ApiBase::PARAM_TYPE] );
|
|
|
|
$this->assertSame( 'free', $params['license'][ApiBase::PARAM_DFLT] );
|
2021-05-04 14:48:45 +00:00
|
|
|
$this->assertFalse( $params['license'][ApiBase::PARAM_ISMULTI] );
|
2015-10-07 15:18:54 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 08:58:54 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideGetTitles
|
|
|
|
*/
|
|
|
|
public function testGetTitles( $titles, $missingTitlesByNamespace, $expected ) {
|
2021-04-08 19:15:01 +00:00
|
|
|
$pageSet = $this->getMockBuilder( \ApiPageSet::class )
|
2017-12-06 23:19:01 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2021-05-04 14:48:45 +00:00
|
|
|
$pageSet->method( 'getGoodTitles' )
|
2021-03-20 05:22:04 +00:00
|
|
|
->willReturn( $titles );
|
2021-05-04 14:48:45 +00:00
|
|
|
$pageSet->method( 'getMissingTitlesByNamespace' )
|
2021-03-20 05:22:04 +00:00
|
|
|
->willReturn( $missingTitlesByNamespace );
|
2017-12-21 04:50:27 +00:00
|
|
|
$queryPageImages = new ApiQueryPageImagesProxyMock( $pageSet );
|
2015-10-02 08:58:54 +00:00
|
|
|
|
|
|
|
$this->assertEquals( $expected, $queryPageImages->getTitles() );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideGetTitles() {
|
2016-12-02 00:49:13 +00:00
|
|
|
return [
|
|
|
|
[
|
2019-10-31 21:16:14 +00:00
|
|
|
[ Title::makeTitle( NS_MAIN, 'Foo' ) ],
|
2016-12-02 00:49:13 +00:00
|
|
|
[],
|
2019-10-31 21:16:14 +00:00
|
|
|
[ Title::makeTitle( NS_MAIN, 'Foo' ) ],
|
2016-12-02 00:49:13 +00:00
|
|
|
],
|
|
|
|
[
|
2019-10-31 21:16:14 +00:00
|
|
|
[ Title::makeTitle( NS_MAIN, 'Foo' ) ],
|
2016-12-02 00:49:13 +00:00
|
|
|
[
|
|
|
|
NS_TALK => [
|
2015-10-02 08:58:54 +00:00
|
|
|
'Bar' => -1,
|
2016-12-02 00:49:13 +00:00
|
|
|
],
|
|
|
|
],
|
2019-10-31 21:16:14 +00:00
|
|
|
[ Title::makeTitle( NS_MAIN, 'Foo' ) ],
|
2016-12-02 00:49:13 +00:00
|
|
|
],
|
|
|
|
[
|
2019-10-31 21:16:14 +00:00
|
|
|
[ Title::makeTitle( NS_MAIN, 'Foo' ) ],
|
2016-12-02 00:49:13 +00:00
|
|
|
[
|
|
|
|
NS_FILE => [
|
2015-10-02 08:58:54 +00:00
|
|
|
'Bar' => -1,
|
2016-12-02 00:49:13 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
2019-10-31 21:16:14 +00:00
|
|
|
0 => Title::makeTitle( NS_MAIN, 'Foo' ),
|
|
|
|
-1 => Title::makeTitle( NS_FILE, 'Bar' ),
|
2016-12-02 00:49:13 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
2015-10-02 08:58:54 +00:00
|
|
|
}
|
2015-10-07 15:18:54 +00:00
|
|
|
|
2016-11-17 16:28:08 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideExecute
|
|
|
|
* @param array $requestParams Request parameters to the API
|
|
|
|
* @param array $titles Page titles passed to the API
|
|
|
|
* @param array $queryPageIds Page IDs that will be used for querying the DB.
|
|
|
|
* @param array $queryResults Results of the DB select query
|
|
|
|
* @param int $setResultValueCount The number results the API returned
|
|
|
|
*/
|
2017-05-30 19:49:44 +00:00
|
|
|
public function testExecute( $requestParams, $titles, $queryPageIds,
|
|
|
|
$queryResults, $setResultValueCount
|
|
|
|
) {
|
2016-11-10 00:02:00 +00:00
|
|
|
$mock = TestingAccessWrapper::newFromObject(
|
|
|
|
$this->getMockBuilder( ApiQueryPageImages::class )
|
|
|
|
->disableOriginalConstructor()
|
2021-05-12 09:23:38 +00:00
|
|
|
->onlyMethods( [ 'extractRequestParams', 'getTitles', 'dieWithError',
|
2016-12-02 00:49:13 +00:00
|
|
|
'addTables', 'addFields', 'addWhere', 'select', 'setResultValues' ] )
|
2016-11-10 00:02:00 +00:00
|
|
|
->getMock()
|
|
|
|
);
|
2021-05-04 14:48:45 +00:00
|
|
|
$mock->method( 'extractRequestParams' )
|
2016-11-17 16:28:08 +00:00
|
|
|
->willReturn( $requestParams );
|
2021-05-04 14:48:45 +00:00
|
|
|
$mock->method( 'getTitles' )
|
2016-11-17 16:28:08 +00:00
|
|
|
->willReturn( $titles );
|
2021-05-04 14:48:45 +00:00
|
|
|
$mock->method( 'select' )
|
2016-11-17 16:28:08 +00:00
|
|
|
->willReturn( new FakeResultWrapper( $queryResults ) );
|
|
|
|
|
|
|
|
// continue page ID is not found
|
2017-05-30 19:49:44 +00:00
|
|
|
if ( isset( $requestParams['continue'] )
|
|
|
|
&& $requestParams['continue'] > count( $titles )
|
|
|
|
) {
|
2021-05-04 14:48:45 +00:00
|
|
|
$mock->expects( $this->once() )
|
2021-05-12 09:23:38 +00:00
|
|
|
->method( 'dieWithError' );
|
2016-11-17 16:28:08 +00:00
|
|
|
}
|
|
|
|
|
2016-12-02 22:09:55 +00:00
|
|
|
$originalRequested = in_array( 'original', $requestParams['prop'] );
|
|
|
|
$this->assertTrue( $this->hasExpectedProperties( $queryResults, $originalRequested ) );
|
|
|
|
|
2021-05-04 14:51:18 +00:00
|
|
|
$license = $requestParams['license'] ?? 'free';
|
2020-04-20 13:34:12 +00:00
|
|
|
if ( $license == PageImages::LICENSE_ANY ) {
|
2016-12-02 00:49:13 +00:00
|
|
|
$propName = [ PageImages::getPropName( true ), PageImages::getPropName( false ) ];
|
2016-11-10 00:02:00 +00:00
|
|
|
} else {
|
|
|
|
$propName = PageImages::getPropName( true );
|
|
|
|
}
|
2016-12-02 00:49:13 +00:00
|
|
|
$mock->expects( $this->exactly( count( $queryPageIds ) > 0 ? 1 : 0 ) )
|
2016-11-17 16:28:08 +00:00
|
|
|
->method( 'addWhere' )
|
2016-12-02 00:49:13 +00:00
|
|
|
->with( [ 'pp_page' => $queryPageIds, 'pp_propname' => $propName ] );
|
2016-11-17 16:28:08 +00:00
|
|
|
|
|
|
|
$mock->expects( $this->exactly( $setResultValueCount ) )
|
|
|
|
->method( 'setResultValues' );
|
|
|
|
|
|
|
|
$mock->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideExecute() {
|
|
|
|
return [
|
|
|
|
[
|
2020-08-12 10:32:53 +00:00
|
|
|
[ 'prop' => [ 'thumbnail' ], 'thumbsize' => 100, 'limit' => 10,
|
|
|
|
'license' => 'any', 'langcode' => null ],
|
2016-12-02 00:49:13 +00:00
|
|
|
[ Title::newFromText( 'Page 1' ), Title::newFromText( 'Page 2' ) ],
|
|
|
|
[ 0, 1 ],
|
2016-11-17 16:28:08 +00:00
|
|
|
[
|
2017-06-20 07:17:38 +00:00
|
|
|
(object)[ 'pp_page' => 0, 'pp_value' => 'A_Free.jpg',
|
2017-05-30 19:49:44 +00:00
|
|
|
'pp_propname' => PageImages::PROP_NAME_FREE ],
|
2017-06-20 07:17:38 +00:00
|
|
|
(object)[ 'pp_page' => 0, 'pp_value' => 'A.jpg',
|
2017-05-30 19:49:44 +00:00
|
|
|
'pp_propname' => PageImages::PROP_NAME ],
|
2017-06-20 07:17:38 +00:00
|
|
|
(object)[ 'pp_page' => 1, 'pp_value' => 'B.jpg',
|
2017-05-30 19:49:44 +00:00
|
|
|
'pp_propname' => PageImages::PROP_NAME ],
|
2016-11-17 16:28:08 +00:00
|
|
|
],
|
|
|
|
2
|
|
|
|
],
|
|
|
|
[
|
2020-08-12 10:32:53 +00:00
|
|
|
[ 'prop' => [ 'thumbnail' ], 'thumbsize' => 200, 'limit' => 10, 'langcode' => null ],
|
2016-11-17 16:28:08 +00:00
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
0
|
|
|
|
],
|
|
|
|
[
|
2017-05-30 19:49:44 +00:00
|
|
|
[ 'prop' => [ 'thumbnail' ], 'continue' => 1, 'thumbsize' => 400,
|
2020-08-12 10:32:53 +00:00
|
|
|
'limit' => 10, 'license' => 'any', 'langcode' => null ],
|
2016-12-02 00:49:13 +00:00
|
|
|
[ Title::newFromText( 'Page 1' ), Title::newFromText( 'Page 2' ) ],
|
|
|
|
[ 1 ],
|
2016-11-10 00:02:00 +00:00
|
|
|
[
|
2017-06-20 07:17:38 +00:00
|
|
|
(object)[ 'pp_page' => 1, 'pp_value' => 'B_Free.jpg',
|
2017-05-30 19:49:44 +00:00
|
|
|
'pp_propname' => PageImages::PROP_NAME_FREE ],
|
2017-06-20 07:17:38 +00:00
|
|
|
(object)[ 'pp_page' => 1, 'pp_value' => 'B.jpg',
|
2017-05-30 19:49:44 +00:00
|
|
|
'pp_propname' => PageImages::PROP_NAME ],
|
2016-11-10 00:02:00 +00:00
|
|
|
],
|
|
|
|
1
|
|
|
|
],
|
|
|
|
[
|
2020-08-12 10:32:53 +00:00
|
|
|
[ 'prop' => [ 'thumbnail' ], 'thumbsize' => 500, 'limit' => 10,
|
|
|
|
'license' => 'any', 'langcode' => 'en' ],
|
2016-12-02 00:49:13 +00:00
|
|
|
[ Title::newFromText( 'Page 1' ), Title::newFromText( 'Page 2' ) ],
|
|
|
|
[ 0, 1 ],
|
2016-11-10 00:02:00 +00:00
|
|
|
[
|
2017-06-20 07:17:38 +00:00
|
|
|
(object)[ 'pp_page' => 1, 'pp_value' => 'B_Free.jpg',
|
2017-05-30 19:49:44 +00:00
|
|
|
'pp_propname' => PageImages::PROP_NAME ],
|
2016-11-10 00:02:00 +00:00
|
|
|
],
|
|
|
|
1
|
|
|
|
],
|
|
|
|
[
|
2017-05-30 19:49:44 +00:00
|
|
|
[ 'prop' => [ 'thumbnail' ], 'continue' => 1, 'thumbsize' => 500,
|
2020-08-12 10:32:53 +00:00
|
|
|
'limit' => 10, 'license' => 'any', 'langcode' => 'de' ],
|
2016-12-02 00:49:13 +00:00
|
|
|
[ Title::newFromText( 'Page 1' ), Title::newFromText( 'Page 2' ) ],
|
|
|
|
[ 1 ],
|
2016-11-17 16:28:08 +00:00
|
|
|
[
|
2017-06-20 07:17:38 +00:00
|
|
|
(object)[ 'pp_page' => 1, 'pp_value' => 'B_Free.jpg',
|
2017-05-30 19:49:44 +00:00
|
|
|
'pp_propname' => PageImages::PROP_NAME_FREE ],
|
2016-11-17 16:28:08 +00:00
|
|
|
],
|
|
|
|
1
|
|
|
|
],
|
2016-11-10 00:02:00 +00:00
|
|
|
[
|
2020-08-12 10:32:53 +00:00
|
|
|
[ 'prop' => [ 'thumbnail' ], 'thumbsize' => 510, 'limit' => 10,
|
|
|
|
'license' => 'free', 'langcode' => 'de' ],
|
2016-12-02 00:49:13 +00:00
|
|
|
[ Title::newFromText( 'Page 1' ), Title::newFromText( 'Page 2' ) ],
|
|
|
|
[ 0, 1 ],
|
2016-11-10 00:02:00 +00:00
|
|
|
[],
|
|
|
|
0
|
|
|
|
],
|
|
|
|
[
|
2020-08-12 10:32:53 +00:00
|
|
|
[ 'prop' => [ 'thumbnail' ], 'thumbsize' => 510, 'limit' => 10,
|
|
|
|
'license' => 'free', 'langcode' => 'en' ],
|
2016-12-02 00:49:13 +00:00
|
|
|
[ Title::newFromText( 'Page 1' ), Title::newFromText( 'Page 2' ) ],
|
|
|
|
[ 0, 1 ],
|
2016-11-10 00:02:00 +00:00
|
|
|
[
|
2017-06-20 07:17:38 +00:00
|
|
|
(object)[ 'pp_page' => 0, 'pp_value' => 'A_Free.jpg',
|
2017-05-30 19:49:44 +00:00
|
|
|
'pp_propname' => PageImages::PROP_NAME_FREE ],
|
2017-06-20 07:17:38 +00:00
|
|
|
(object)[ 'pp_page' => 1, 'pp_value' => 'B_Free.jpg',
|
2017-05-30 19:49:44 +00:00
|
|
|
'pp_propname' => PageImages::PROP_NAME_FREE ],
|
2016-11-10 00:02:00 +00:00
|
|
|
],
|
|
|
|
2
|
|
|
|
],
|
2016-12-02 22:09:55 +00:00
|
|
|
[
|
2017-05-30 19:49:44 +00:00
|
|
|
[ 'prop' => [ 'thumbnail', 'original' ], 'thumbsize' => 510,
|
2020-08-12 10:32:53 +00:00
|
|
|
'limit' => 10, 'license' => 'free', 'langcode' => 'en' ],
|
2017-05-30 19:49:44 +00:00
|
|
|
[ Title::newFromText( 'Page 1' ), Title::newFromText( 'Page 2' ) ],
|
|
|
|
[ 0, 1 ],
|
2016-12-02 22:09:55 +00:00
|
|
|
[
|
2017-06-20 07:17:38 +00:00
|
|
|
(object)[
|
2017-05-30 19:49:44 +00:00
|
|
|
'pp_page' => 0, 'pp_value' => 'A_Free.jpg',
|
|
|
|
'pp_value_original' => 'A_Free_original.jpg', 'pp_original_width' => 80,
|
|
|
|
'pp_original_height' => 80, 'pp_propname' => PageImages::PROP_NAME_FREE
|
|
|
|
],
|
2017-06-20 07:17:38 +00:00
|
|
|
(object)[
|
2017-05-30 19:49:44 +00:00
|
|
|
'pp_page' => 1, 'pp_value' => 'B_Free.jpg',
|
|
|
|
'pp_value_original' => 'B_Free_original.jpg', 'pp_original_width' => 80,
|
|
|
|
'pp_original_height' => 80, 'pp_propname' => PageImages::PROP_NAME_FREE
|
|
|
|
],
|
2016-12-02 22:09:55 +00:00
|
|
|
],
|
|
|
|
2
|
|
|
|
],
|
2016-11-10 00:02:00 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-12-02 22:09:55 +00:00
|
|
|
private function hasExpectedProperties( $queryResults, $originalRequested ) {
|
|
|
|
if ( $originalRequested ) {
|
|
|
|
return $this->allResultsHaveProperty( $queryResults, 'pp_value_original' )
|
|
|
|
&& $this->allResultsHaveProperty( $queryResults, 'pp_original_width' )
|
|
|
|
&& $this->allResultsHaveProperty( $queryResults, 'pp_original_height' );
|
|
|
|
} else {
|
|
|
|
return $this->noResultsHaveProperty( $queryResults, 'pp_value_original' )
|
|
|
|
&& $this->noResultsHaveProperty( $queryResults, 'pp_original_width' )
|
|
|
|
&& $this->noResultsHaveProperty( $queryResults, 'pp_original_height' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function noResultsHaveProperty( $queryResults, $propName ) {
|
2017-05-30 19:49:44 +00:00
|
|
|
foreach ( $queryResults as $result ) {
|
2016-12-02 22:09:55 +00:00
|
|
|
if ( property_exists( $result, $propName ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function allResultsHaveProperty( $queryResults, $propName ) {
|
2017-05-30 19:49:44 +00:00
|
|
|
foreach ( $queryResults as $result ) {
|
2016-12-02 22:09:55 +00:00
|
|
|
if ( !property_exists( $result, $propName ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2015-10-02 08:58:54 +00:00
|
|
|
}
|