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
|
|
|
|
|
|
|
use ApiPageSet;
|
|
|
|
use ApiQueryPageImages;
|
2016-11-17 16:28:08 +00:00
|
|
|
use FakeResultWrapper;
|
|
|
|
use PageImages;
|
2015-10-07 15:18:54 +00:00
|
|
|
use PHPUnit_Framework_TestCase;
|
2016-11-10 00:02:00 +00:00
|
|
|
use TestingAccessWrapper;
|
2015-10-02 08:58:54 +00:00
|
|
|
use Title;
|
|
|
|
|
|
|
|
class ApiPageSetStub extends ApiPageSet {
|
|
|
|
|
|
|
|
public function __construct( $goodTitles, $missingTitlesByNamespace ) {
|
|
|
|
$this->goodTitles = $goodTitles;
|
|
|
|
$this->missingTitlesByNamespace = $missingTitlesByNamespace;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getGoodTitles() {
|
|
|
|
return $this->goodTitles;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getMissingTitlesByNamespace() {
|
|
|
|
return $this->missingTitlesByNamespace;
|
|
|
|
}
|
2015-10-07 15:18:54 +00:00
|
|
|
|
2015-10-02 08:58:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ApiQueryPageImagesProxy extends ApiQueryPageImages {
|
|
|
|
|
|
|
|
public function __construct( ApiPageSet $pageSet ) {
|
|
|
|
$this->pageSet = $pageSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPageSet() {
|
|
|
|
return $this->pageSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTitles() {
|
|
|
|
return parent::getTitles();
|
|
|
|
}
|
2015-10-07 15:18:54 +00:00
|
|
|
|
2016-11-10 00:02:00 +00:00
|
|
|
/** inheritdoc */
|
|
|
|
public static function getPropNames( $license ) {
|
|
|
|
return parent::getPropNames( $license );
|
|
|
|
}
|
2015-10-02 08:58:54 +00:00
|
|
|
}
|
|
|
|
|
2015-10-07 15:18:54 +00:00
|
|
|
/**
|
|
|
|
* @covers ApiQueryPageImages
|
|
|
|
*
|
|
|
|
* @group PageImages
|
|
|
|
*
|
2015-11-16 14:59:34 +00:00
|
|
|
* @license WTFPL 2.0
|
|
|
|
* @author Sam Smith
|
2015-10-07 15:18:54 +00:00
|
|
|
* @author Thiemo Mättig
|
|
|
|
*/
|
2015-10-02 08:58:54 +00:00
|
|
|
class ApiQueryPageImagesTest extends PHPUnit_Framework_TestCase {
|
|
|
|
|
2015-10-26 10:41:13 +00:00
|
|
|
private function newInstance() {
|
2015-10-07 15:18:54 +00:00
|
|
|
$context = $this->getMockBuilder( 'IContextSource' )
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$main = $this->getMockBuilder( 'ApiMain' )
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$main->expects( $this->once() )
|
|
|
|
->method( 'getContext' )
|
|
|
|
->will( $this->returnValue( $context ) );
|
|
|
|
|
|
|
|
$query = $this->getMockBuilder( 'ApiQuery' )
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$query->expects( $this->once() )
|
|
|
|
->method( 'getMain' )
|
|
|
|
->will( $this->returnValue( $main ) );
|
|
|
|
|
|
|
|
return new ApiQueryPageImages( $query, '' );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConstructor() {
|
2015-10-26 10:41:13 +00:00
|
|
|
$instance = $this->newInstance();
|
|
|
|
$this->assertInstanceOf( 'ApiQueryPageImages', $instance );
|
2015-10-07 15:18:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetDescription() {
|
2015-10-26 10:41:13 +00:00
|
|
|
$instance = $this->newInstance();
|
|
|
|
$description = $instance->getDescription();
|
2015-10-07 15:18:54 +00:00
|
|
|
$this->assertInternalType( 'string', $description );
|
|
|
|
$this->assertNotEmpty( $description );
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2015-10-07 15:18:54 +00:00
|
|
|
$this->assertInternalType( 'array', $params );
|
|
|
|
$this->assertNotEmpty( $params );
|
|
|
|
$this->assertContainsOnly( 'array', $params );
|
2016-11-10 00:02:00 +00:00
|
|
|
$this->assertArrayHasKey( 'license', $params );
|
2016-12-02 00:49:13 +00:00
|
|
|
$this->assertEquals( $params['license'][\ApiBase::PARAM_TYPE], [ 'free', 'any' ] );
|
2016-12-01 23:20:31 +00:00
|
|
|
$this->assertEquals( $params['license'][\ApiBase::PARAM_DFLT], 'any' );
|
2016-11-10 00:02:00 +00:00
|
|
|
$this->assertEquals( $params['license'][\ApiBase::PARAM_ISMULTI], false );
|
2015-10-07 15:18:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetParamDescription() {
|
2015-10-26 10:41:13 +00:00
|
|
|
$instance = $this->newInstance();
|
|
|
|
$descriptions = $instance->getParamDescription();
|
2015-10-07 15:18:54 +00:00
|
|
|
$this->assertInternalType( 'array', $descriptions );
|
|
|
|
$this->assertNotEmpty( $descriptions );
|
|
|
|
}
|
|
|
|
|
2015-10-02 08:58:54 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideGetTitles
|
|
|
|
*/
|
|
|
|
public function testGetTitles( $titles, $missingTitlesByNamespace, $expected ) {
|
|
|
|
$pageSet = new ApiPageSetStub( $titles, $missingTitlesByNamespace );
|
|
|
|
$queryPageImages = new ApiQueryPageImagesProxy( $pageSet );
|
|
|
|
|
|
|
|
$this->assertEquals( $expected, $queryPageImages->getTitles() );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideGetTitles() {
|
2016-12-02 00:49:13 +00:00
|
|
|
return [
|
|
|
|
[
|
|
|
|
[ Title::newFromText( 'Foo' ) ],
|
|
|
|
[],
|
|
|
|
[ Title::newFromText( 'Foo' ) ],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ Title::newFromText( 'Foo' ) ],
|
|
|
|
[
|
|
|
|
NS_TALK => [
|
2015-10-02 08:58:54 +00:00
|
|
|
'Bar' => -1,
|
2016-12-02 00:49:13 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
[ Title::newFromText( 'Foo' ) ],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[ Title::newFromText( 'Foo' ) ],
|
|
|
|
[
|
|
|
|
NS_FILE => [
|
2015-10-02 08:58:54 +00:00
|
|
|
'Bar' => -1,
|
2016-12-02 00:49:13 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
2015-10-02 08:58:54 +00:00
|
|
|
0 => Title::newFromText( 'Foo' ),
|
|
|
|
-1 => Title::newFromText( 'Bar', NS_FILE ),
|
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
|
|
|
|
*/
|
|
|
|
public function testExecute( $requestParams, $titles, $queryPageIds, $queryResults, $setResultValueCount ) {
|
2016-11-10 00:02:00 +00:00
|
|
|
$mock = TestingAccessWrapper::newFromObject(
|
|
|
|
$this->getMockBuilder( ApiQueryPageImages::class )
|
|
|
|
->disableOriginalConstructor()
|
2016-12-02 00:49:13 +00:00
|
|
|
-> setMethods( [ 'extractRequestParams', 'getTitles', 'setContinueParameter', 'dieUsage',
|
|
|
|
'addTables', 'addFields', 'addWhere', 'select', 'setResultValues' ] )
|
2016-11-10 00:02:00 +00:00
|
|
|
->getMock()
|
|
|
|
);
|
2016-11-17 16:28:08 +00:00
|
|
|
$mock->expects( $this->any() )
|
|
|
|
->method( 'extractRequestParams' )
|
|
|
|
->willReturn( $requestParams );
|
|
|
|
$mock->expects( $this->any() )
|
|
|
|
->method( 'getTitles' )
|
|
|
|
->willReturn( $titles );
|
|
|
|
$mock->expects( $this->any() )
|
|
|
|
->method( 'select' )
|
|
|
|
->willReturn( new FakeResultWrapper( $queryResults ) );
|
|
|
|
|
|
|
|
// continue page ID is not found
|
|
|
|
if ( isset( $requestParams['continue'] ) && $requestParams['continue'] > count( $titles ) ) {
|
|
|
|
$mock->expects( $this->exactly( 1 ) )
|
|
|
|
->method( 'dieUsage' );
|
|
|
|
}
|
|
|
|
|
2016-11-10 00:02:00 +00:00
|
|
|
$license = isset( $requestParams['license'] ) ? $requestParams['license'] : 'free';
|
|
|
|
if ( $license == ApiQueryPageImages::PARAM_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 [
|
|
|
|
[
|
2016-12-02 00:49:13 +00:00
|
|
|
[ 'prop' => [ 'thumbnail' ], 'thumbsize' => 100, 'limit' => 10, 'license' => 'any' ],
|
|
|
|
[ Title::newFromText( 'Page 1' ), Title::newFromText( 'Page 2' ) ],
|
|
|
|
[ 0, 1 ],
|
2016-11-17 16:28:08 +00:00
|
|
|
[
|
2016-12-02 00:49:13 +00:00
|
|
|
(object) [ 'pp_page' => 0, 'pp_value' => 'A_Free.jpg', 'pp_propname' => PageImages::PROP_NAME_FREE ],
|
|
|
|
(object) [ 'pp_page' => 0, 'pp_value' => 'A.jpg', 'pp_propname' => PageImages::PROP_NAME ],
|
|
|
|
(object) [ 'pp_page' => 1, 'pp_value' => 'B.jpg', 'pp_propname' => PageImages::PROP_NAME ],
|
2016-11-17 16:28:08 +00:00
|
|
|
],
|
|
|
|
2
|
|
|
|
],
|
|
|
|
[
|
2016-12-02 00:49:13 +00:00
|
|
|
[ 'prop' => [ 'thumbnail' ], 'thumbsize' => 200, 'limit' => 10 ],
|
2016-11-17 16:28:08 +00:00
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
0
|
|
|
|
],
|
|
|
|
[
|
2016-12-02 00:49:13 +00:00
|
|
|
[ 'prop' => [ 'thumbnail' ], 'continue' => 1, 'thumbsize' => 400, 'limit' => 10, 'license' => 'any' ],
|
|
|
|
[ Title::newFromText( 'Page 1' ), Title::newFromText( 'Page 2' ) ],
|
|
|
|
[ 1 ],
|
2016-11-10 00:02:00 +00:00
|
|
|
[
|
2016-12-02 00:49:13 +00:00
|
|
|
(object) [ 'pp_page' => 1, 'pp_value' => 'B_Free.jpg', 'pp_propname' => PageImages::PROP_NAME_FREE ],
|
|
|
|
(object) [ 'pp_page' => 1, 'pp_value' => 'B.jpg', 'pp_propname' => PageImages::PROP_NAME ],
|
2016-11-10 00:02:00 +00:00
|
|
|
],
|
|
|
|
1
|
|
|
|
],
|
|
|
|
[
|
2016-12-02 00:49:13 +00:00
|
|
|
[ 'prop' => [ 'thumbnail' ], 'thumbsize' => 500, 'limit' => 10, 'license' => 'any' ],
|
|
|
|
[ Title::newFromText( 'Page 1' ), Title::newFromText( 'Page 2' ) ],
|
|
|
|
[ 0, 1 ],
|
2016-11-10 00:02:00 +00:00
|
|
|
[
|
2016-12-02 00:49:13 +00:00
|
|
|
(object) [ 'pp_page' => 1, 'pp_value' => 'B_Free.jpg', 'pp_propname' => PageImages::PROP_NAME ],
|
2016-11-10 00:02:00 +00:00
|
|
|
],
|
|
|
|
1
|
|
|
|
],
|
|
|
|
[
|
2016-12-02 00:49:13 +00:00
|
|
|
[ 'prop' => [ 'thumbnail' ], 'continue' => 1, 'thumbsize' => 500, 'limit' => 10, 'license' => 'any' ],
|
|
|
|
[ Title::newFromText( 'Page 1' ), Title::newFromText( 'Page 2' ) ],
|
|
|
|
[ 1 ],
|
2016-11-17 16:28:08 +00:00
|
|
|
[
|
2016-12-02 00:49:13 +00:00
|
|
|
(object) [ 'pp_page' => 1, 'pp_value' => 'B_Free.jpg', 'pp_propname' => PageImages::PROP_NAME_FREE ],
|
2016-11-17 16:28:08 +00:00
|
|
|
],
|
|
|
|
1
|
|
|
|
],
|
2016-11-10 00:02:00 +00:00
|
|
|
[
|
2016-12-02 00:49:13 +00:00
|
|
|
[ 'prop' => [ 'thumbnail' ], 'thumbsize' => 510, 'limit' => 10, 'license' => 'free' ],
|
|
|
|
[ Title::newFromText( 'Page 1' ), Title::newFromText( 'Page 2' ) ],
|
|
|
|
[ 0, 1 ],
|
2016-11-10 00:02:00 +00:00
|
|
|
[],
|
|
|
|
0
|
|
|
|
],
|
|
|
|
[
|
2016-12-02 00:49:13 +00:00
|
|
|
[ 'prop' => [ 'thumbnail' ], 'thumbsize' => 510, 'limit' => 10, 'license' => 'free' ],
|
|
|
|
[ Title::newFromText( 'Page 1' ), Title::newFromText( 'Page 2' ) ],
|
|
|
|
[ 0, 1 ],
|
2016-11-10 00:02:00 +00:00
|
|
|
[
|
2016-12-02 00:49:13 +00:00
|
|
|
(object) [ 'pp_page' => 0, 'pp_value' => 'A_Free.jpg', 'pp_propname' => PageImages::PROP_NAME_FREE ],
|
|
|
|
(object) [ 'pp_page' => 1, 'pp_value' => 'B_Free.jpg', 'pp_propname' => PageImages::PROP_NAME_FREE ],
|
2016-11-10 00:02:00 +00:00
|
|
|
],
|
|
|
|
2
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideGetPropName
|
|
|
|
* @param string $license
|
|
|
|
* @param string $expected
|
|
|
|
*/
|
|
|
|
public function testGetPropName( $license, $expected ) {
|
|
|
|
$this->assertEquals( $expected, ApiQueryPageImagesProxy::getPropNames( $license ) );
|
|
|
|
}
|
|
|
|
|
2016-12-02 00:49:13 +00:00
|
|
|
public function provideGetPropName() {
|
|
|
|
|
2016-11-10 00:02:00 +00:00
|
|
|
return [
|
|
|
|
[ 'free', \PageImages::PROP_NAME_FREE ],
|
|
|
|
[ 'any', [ \PageImages::PROP_NAME_FREE, \PageImages::PROP_NAME ] ]
|
2016-11-17 16:28:08 +00:00
|
|
|
];
|
|
|
|
}
|
2015-10-02 08:58:54 +00:00
|
|
|
}
|