Move ResultWrapper subclasses to Rdbms
[mediawiki.git] / tests / phpunit / includes / api / ApiPageSetTest.php
blobad1deee5ce747f6b172aad4fd4f7a4f82f1f9863
1 <?php
3 /**
4 * @group API
5 * @group medium
6 * @group Database
7 */
8 class ApiPageSetTest extends ApiTestCase {
9 public static function provideRedirectMergePolicy() {
10 return [
11 'By default nothing is merged' => [
12 null,
16 'A simple merge policy adds the redirect data in' => [
17 function( $current, $new ) {
18 if ( !isset( $current['index'] ) || $new['index'] < $current['index'] ) {
19 $current['index'] = $new['index'];
21 return $current;
23 [ 'index' => 1 ],
28 /**
29 * @dataProvider provideRedirectMergePolicy
31 public function testRedirectMergePolicyWithArrayResult( $mergePolicy, $expect ) {
32 list( $target, $pageSet ) = $this->createPageSetWithRedirect();
33 $pageSet->setRedirectMergePolicy( $mergePolicy );
34 $result = [
35 $target->getArticleID() => []
37 $pageSet->populateGeneratorData( $result );
38 $this->assertEquals( $expect, $result[$target->getArticleID()] );
41 /**
42 * @dataProvider provideRedirectMergePolicy
44 public function testRedirectMergePolicyWithApiResult( $mergePolicy, $expect ) {
45 list( $target, $pageSet ) = $this->createPageSetWithRedirect();
46 $pageSet->setRedirectMergePolicy( $mergePolicy );
47 $result = new ApiResult( false );
48 $result->addValue( null, 'pages', [
49 $target->getArticleID() => []
50 ] );
51 $pageSet->populateGeneratorData( $result, [ 'pages' ] );
52 $this->assertEquals(
53 $expect,
54 $result->getResultData( [ 'pages', $target->getArticleID() ] )
58 protected function createPageSetWithRedirect() {
59 $target = Title::makeTitle( NS_MAIN, 'UTRedirectTarget' );
60 $sourceA = Title::makeTitle( NS_MAIN, 'UTRedirectSourceA' );
61 $sourceB = Title::makeTitle( NS_MAIN, 'UTRedirectSourceB' );
62 self::editPage( 'UTRedirectTarget', 'api page set test' );
63 self::editPage( 'UTRedirectSourceA', '#REDIRECT [[UTRedirectTarget]]' );
64 self::editPage( 'UTRedirectSourceB', '#REDIRECT [[UTRedirectTarget]]' );
66 $request = new FauxRequest( [ 'redirects' => 1 ] );
67 $context = new RequestContext();
68 $context->setRequest( $request );
69 $main = new ApiMain( $context );
70 $pageSet = new ApiPageSet( $main );
72 $pageSet->setGeneratorData( $sourceA, [ 'index' => 1 ] );
73 $pageSet->setGeneratorData( $sourceB, [ 'index' => 3 ] );
74 $pageSet->populateFromTitles( [ $sourceA, $sourceB ] );
76 return [ $target, $pageSet ];
79 public function testHandleNormalization() {
80 $context = new RequestContext();
81 $context->setRequest( new FauxRequest( [ 'titles' => "a|B|a\xcc\x8a" ] ) );
82 $main = new ApiMain( $context );
83 $pageSet = new ApiPageSet( $main );
84 $pageSet->execute();
86 $this->assertSame(
87 [ 0 => [ 'A' => -1, 'B' => -2, 'Å' => -3 ] ],
88 $pageSet->getAllTitlesByNamespace()
90 $this->assertSame(
92 [ 'fromencoded' => true, 'from' => 'a%CC%8A', 'to' => 'å' ],
93 [ 'fromencoded' => false, 'from' => 'a', 'to' => 'A' ],
94 [ 'fromencoded' => false, 'from' => 'å', 'to' => 'Å' ],
96 $pageSet->getNormalizedTitlesAsResult()