2015-10-07 21:03:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @group API
|
|
|
|
|
* @group medium
|
|
|
|
|
* @group Database
|
|
|
|
|
*/
|
|
|
|
|
class ApiPageSetTest extends ApiTestCase {
|
|
|
|
|
public static function provideRedirectMergePolicy() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
'By default nothing is merged' => [
|
2015-10-07 21:03:15 +00:00
|
|
|
null,
|
2016-02-17 09:09:32 +00:00
|
|
|
[]
|
|
|
|
|
],
|
2015-10-07 21:03:15 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
'A simple merge policy adds the redirect data in' => [
|
2015-10-07 21:03:15 +00:00
|
|
|
function( $current, $new ) {
|
|
|
|
|
if ( !isset( $current['index'] ) || $new['index'] < $current['index'] ) {
|
|
|
|
|
$current['index'] = $new['index'];
|
|
|
|
|
}
|
|
|
|
|
return $current;
|
|
|
|
|
},
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'index' => 1 ],
|
|
|
|
|
],
|
|
|
|
|
];
|
2015-10-07 21:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideRedirectMergePolicy
|
|
|
|
|
*/
|
|
|
|
|
public function testRedirectMergePolicyWithArrayResult( $mergePolicy, $expect ) {
|
|
|
|
|
list( $target, $pageSet ) = $this->createPageSetWithRedirect();
|
|
|
|
|
$pageSet->setRedirectMergePolicy( $mergePolicy );
|
2016-02-17 09:09:32 +00:00
|
|
|
$result = [
|
|
|
|
|
$target->getArticleID() => []
|
|
|
|
|
];
|
2015-10-07 21:03:15 +00:00
|
|
|
$pageSet->populateGeneratorData( $result );
|
|
|
|
|
$this->assertEquals( $expect, $result[$target->getArticleID()] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideRedirectMergePolicy
|
|
|
|
|
*/
|
|
|
|
|
public function testRedirectMergePolicyWithApiResult( $mergePolicy, $expect ) {
|
|
|
|
|
list( $target, $pageSet ) = $this->createPageSetWithRedirect();
|
|
|
|
|
$pageSet->setRedirectMergePolicy( $mergePolicy );
|
|
|
|
|
$result = new ApiResult( false );
|
2016-02-17 09:09:32 +00:00
|
|
|
$result->addValue( null, 'pages', [
|
|
|
|
|
$target->getArticleID() => []
|
|
|
|
|
] );
|
|
|
|
|
$pageSet->populateGeneratorData( $result, [ 'pages' ] );
|
2015-10-07 21:03:15 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
$expect,
|
2016-02-17 09:09:32 +00:00
|
|
|
$result->getResultData( [ 'pages', $target->getArticleID() ] )
|
2015-10-07 21:03:15 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function createPageSetWithRedirect() {
|
|
|
|
|
$target = Title::makeTitle( NS_MAIN, 'UTRedirectTarget' );
|
|
|
|
|
$sourceA = Title::makeTitle( NS_MAIN, 'UTRedirectSourceA' );
|
|
|
|
|
$sourceB = Title::makeTitle( NS_MAIN, 'UTRedirectSourceB' );
|
|
|
|
|
self::editPage( 'UTRedirectTarget', 'api page set test' );
|
|
|
|
|
self::editPage( 'UTRedirectSourceA', '#REDIRECT [[UTRedirectTarget]]' );
|
|
|
|
|
self::editPage( 'UTRedirectSourceB', '#REDIRECT [[UTRedirectTarget]]' );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$request = new FauxRequest( [ 'redirects' => 1 ] );
|
2015-10-07 21:03:15 +00:00
|
|
|
$context = new RequestContext();
|
|
|
|
|
$context->setRequest( $request );
|
|
|
|
|
$main = new ApiMain( $context );
|
|
|
|
|
$pageSet = new ApiPageSet( $main );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$pageSet->setGeneratorData( $sourceA, [ 'index' => 1 ] );
|
|
|
|
|
$pageSet->setGeneratorData( $sourceB, [ 'index' => 3 ] );
|
|
|
|
|
$pageSet->populateFromTitles( [ $sourceA, $sourceB ] );
|
2015-10-07 21:03:15 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ $target, $pageSet ];
|
2015-10-07 21:03:15 +00:00
|
|
|
}
|
|
|
|
|
}
|