Daniel Kinzler has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/60278


Change subject: Introducing test case for ApiQueryLangLinks.
......................................................................

Introducing test case for ApiQueryLangLinks.

This is in preparation for substantial changes to ApiQueryLangLinks.
The test case will help to assure nothing is broken.

Change-Id: I4ff0855ea11f9fc82bb122590337d3cb57a82939
---
M tests/TestsAutoLoader.php
A tests/phpunit/includes/TestPageSet.php
A tests/phpunit/includes/api/query/ApiQueryLangLinksTest.php
3 files changed, 534 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/78/60278/1

diff --git a/tests/TestsAutoLoader.php b/tests/TestsAutoLoader.php
index 83c2fcb..4c8f298 100644
--- a/tests/TestsAutoLoader.php
+++ b/tests/TestsAutoLoader.php
@@ -44,6 +44,7 @@
        'BlockTest' => "$testDir/phpunit/includes/BlockTest.php",
        'RevisionStorageTest' => 
"$testDir/phpunit/includes/RevisionStorageTest.php",
        'WikiPageTest' => "$testDir/phpunit/includes/WikiPageTest.php",
+       'TestPageSet' => "$testDir/phpunit/includes/TestPageSet.php",
 
        //db
        'ORMTableTest' => "$testDir/phpunit/includes/db/ORMTableTest.php",
diff --git a/tests/phpunit/includes/TestPageSet.php 
b/tests/phpunit/includes/TestPageSet.php
new file mode 100644
index 0000000..d1f6919
--- /dev/null
+++ b/tests/phpunit/includes/TestPageSet.php
@@ -0,0 +1,163 @@
+<?php
+ /**
+ *
+ * Copyright © 22.04.13 by the authors listed below.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @license GPL 2+
+ * @file
+ *
+ * @author Daniel Kinzler
+ */
+
+
+/**
+ * TestPageSet is a utility class for test cases that need a list of wiki 
pages to work on.
+ * To save time, we should only set up such a list once, but we can not do 
this in the
+ * data provider evaluation phase of php unit, because at that time, the test 
database clone
+ * is not yet present (we would write pages into the real database).
+ *
+ * So we can only create the pages once the test actually runs, but we want to 
be able to
+ * name specific pages in the output of data providers, so the provider can 
generate a data
+ * set for e.g. "append to page X" without yet knowing that pages ID or even 
the final title
+ * (which may depend on the namespace setup).
+ *
+ * TestPageSet provides a solution by allowing test pages to be referred to by 
a symbolic name
+ * (a handle). Once the pages have been created, that symbolic name can then 
be resolved into
+ * a Title object to get the actual page name and ID.
+ */
+class TestPageSet {
+
+       /**
+        * @var Title[]
+        */
+       protected $titles = array();
+
+       /**
+        * @var int
+        */
+       protected $namespace;
+
+       /**
+        * @var string
+        */
+       protected $prefix;
+
+       /**
+        * @param int $namespace The namespace to place test pages in. 
Typically,
+        *        MediaWikiTestCase::getDefaultWikitextNS() is used to 
determine the namespace.
+        * @param string $prefix A prefix to use for creating titles from 
handles.
+        *        Typically the name of the test class.
+        *
+        * @example new TestPageSet( $this->getDefaultWikitextNS(), basename( 
__CLASS__ ) )
+        */
+       public function __construct( $namespace, $prefix ) {
+               $this->namespace = $namespace;
+               $this->prefix = $prefix;
+       }
+
+       /**
+        * Creates a page for testing. The title is derived from the $handle 
parameter.
+        * To get the actual title and ID of the page, use getTitle( $handle ).
+        *
+        * @note Do not call this during the data provider evaluation phase of 
phpunit! The
+        * test database has not been set up at that point! Use symbolic names 
in test case
+        * data sets, and create the required pages in the actual test case, 
using a
+        * TestPageSet object.
+        *
+        * @param string $handle
+        * @param Content $content
+        * @param string $summary
+        * @param User|null $user
+        *
+        * @return Title
+        * @throws MWException if page creation failed
+        */
+       public function createTestPage( $handle, Content $content, $summary = 
"Testing", $user = null ) {
+               $title = $this->getTitle( $handle );
+               $page = WikiPage::factory( $title );
+
+               $status = $page->doEditContent( $content, $summary, EDIT_NEW, 
false, $user );
+
+               if ( !$status->isOK() ) {
+                       throw new MWException( "Creation of test page $handle 
failed!\n" . $status->getWikiText() );
+               }
+
+               $title = $page->getTitle();
+               $this->titles[$handle] = $title;
+               return $title;
+       }
+
+       /**
+        * Returns the Title for the given test page handle.
+        *
+        * @param string $handle
+        *
+        * @return Title
+        * @throws MWException if the page is not known
+        */
+       public function getTitle( $handle ) {
+               if ( isset( $this->titles[$handle] ) ) {
+                       return $this->titles[$handle];
+               } else {
+                       return $title = Title::newFromText( $this->prefix . 
$handle, $this->namespace );
+               }
+       }
+
+       /**
+        * Converts a list of handles to a list of article IDs.
+        * May be used by test case implementation when a list of pages
+        * is given by the data provider as a list of handles.
+        *
+        * @param array $handles
+        *
+        * @return array page ids
+        */
+       public function handlesToIds( array $handles ) {
+               $this_ = $this;
+               $ids = array_map(
+                       function ( $handle ) use ( $this_ ) {
+                               return $this_->getTitle( $handle 
)->getArticleID();
+                       },
+                       $handles
+               );
+
+               return $ids;
+       }
+
+       /**
+        * Converts a list of handles to a list of article title strings.
+        * May be used by test case implementation when a list of pages
+        * is given by the data provider as a list of handles.
+        *
+        * @param array $handles
+        *
+        * @return array page titles (as strings)
+        */
+       public function handlesToTitles( array $handles ) {
+               $this_ = $this;
+               $titles = array_map(
+                       function ( $handle ) use ( $this_ ) {
+                               return $this_->getTitle( $handle 
)->getFullText();
+                       },
+                       $handles
+               );
+
+               return $titles;
+       }
+
+}
\ No newline at end of file
diff --git a/tests/phpunit/includes/api/query/ApiQueryLangLinksTest.php 
b/tests/phpunit/includes/api/query/ApiQueryLangLinksTest.php
new file mode 100644
index 0000000..2839beb
--- /dev/null
+++ b/tests/phpunit/includes/api/query/ApiQueryLangLinksTest.php
@@ -0,0 +1,370 @@
+<?php
+
+/**
+ * @group API
+ * @group Database
+ * @group medium
+ */
+class ApiQueryLangLinksTest extends ApiTestCase {
+
+       protected function initIniterwiki() {
+               // ugly hack to provide interwiki prefixes
+
+               $row = array(
+                       'iw_prefix' => 'de',
+                       'iw_url' => 'http://acme.test/$1',
+                       'iw_api' => '',
+                       'iw_wikiid' => '',
+                       'iw_local' => 1,
+                       'iw_trans' => 0,
+               );
+
+               $db = wfGetDB( DB_MASTER );
+
+               $db->insert( 'interwiki', $row, __FUNCTION__, array( 'IGNORE' ) 
);
+
+               $row['iw_prefix'] = 'en';
+               $db->insert( 'interwiki', $row, __FUNCTION__, array( 'IGNORE' ) 
);
+
+               $row['iw_prefix'] = 'fr';
+               $db->insert( 'interwiki', $row, __FUNCTION__, array( 'IGNORE' ) 
);
+
+               $db->commit( __METHOD__, 'flush' );
+       }
+
+       /**
+        * @return TestPageSet
+        * @throws MWException
+        */
+       protected function getTestPages() {
+               static $pages = null;
+
+               if ( $pages === null ) {
+                       $this->initIniterwiki();
+
+                       $pages = new TestPageSet( 
$this->getDefaultWikitextNS(), basename( __CLASS__ ) );
+
+                       $pages->createTestPage( 'Foo', new WikitextContent(
+                               '[[de:FooDe]][[en:FooEn]][[fr:FooFr]]'
+                       ) );
+
+                       $pages->createTestPage( 'Bar', new WikitextContent(
+                               '[[de:BarDe]][[en:BarEn]]'
+                       ) );
+
+                       $pages->createTestPage( 'Meh', new WikitextContent(
+                               '[[de:MehDe]]'
+                       ) );
+               }
+
+               return $pages;
+       }
+
+       /**
+        * Extracts language links from an API result, grouped by page.
+        *
+        * @param array $apiResult
+        *
+        * @return array[][] an array associating page ids with lists of 
language links,
+        *         each of which is again an array.
+        */
+       protected function extractLangLinks( $apiResult ) {
+               $links = array();
+
+               foreach ( $apiResult['query']['pages'] as $id => $page ) {
+                       $links[$id] = array();
+
+                       if ( isset( $page['langlinks'] ) ) {
+                               foreach ( $page['langlinks'] as $link ) {
+                                       $links[$id][] = $link;
+                               }
+                       }
+               }
+
+               return $links;
+       }
+
+       /**
+        * Merges two array structures that represent language links, grouped 
by page.
+        *
+        * @param array[][] $base an array associating page ids with lists of 
language links,
+        *         each of which is again an array.
+        * @param array[][] $add an array associating page ids with lists of 
language links,
+        *         each of which is again an array.
+        *
+        * @return array[][] an array associating page ids with lists of 
language links,
+        *         each of which is again an array.
+        */
+       protected function mergeLangLinks( $base, $add ) {
+               foreach ( $add as $pageId => $links ) {
+                       if ( empty( $base[$pageId] ) ) {
+                               $base[$pageId] = $links;
+                       } else {
+                               $base[$pageId] = array_merge( $base[$pageId], 
$links );
+                       }
+               }
+
+               return $base;
+       }
+
+       public static function provideQuery() {
+               // things to test:
+               // - simple get, with limit
+               // - multi-title query
+               // - multi-id query
+               // - filtering by target language and title
+               // - direction
+               // - paging (with direction)
+               // - with/without url
+               // - unknown page
+
+               return array(
+                       array( // #0: get all links from a single page
+                               array( // params
+                                       'titles' => 'Foo',
+                                       //'pageids' => '',
+                                       //'llcontinue' => ''
+                                       'lllimit' => '5',
+                                       //'lltitle' => '',
+                                       //'lllang' => '',
+                                       //'lldir' => '',
+                                       //'llurl' => '',
+                               ),
+                               array( // expected links
+                                       'Foo' => array(
+                                               array( 'lang' => 'de', '*' => 
'FooDe' ),
+                                               array( 'lang' => 'en', '*' => 
'FooEn' ),
+                                               array( 'lang' => 'fr', '*' => 
'FooFr' ),
+                                       ),
+                               ),
+                               'get all lang links from one page by title'
+                       ),
+                       array( // #1: request links from a missing page
+                               array( // params
+                                       'titles' => 'Quux', // will be expanded 
to full titles automatically
+                                       //'pageids' => '',
+                                       //'llcontinue' => ''
+                                       //'lllimit' => '5',
+                                       //'lltitle' => '',
+                                       //'lllang' => '',
+                                       //'lldir' => '',
+                                       //'llurl' => '',
+                               ),
+                               array( // expected links
+                                       'Quux' => array(),
+                               ),
+                               'request links from a non-existing page'
+                       ),
+                       array( // #2: get links from multiple pages using IDs, 
with paging
+                               array( // params
+                                       //'titles' => '',
+                                       'pageids' => 'Foo|Bar|Meh', // will be 
translated to IDs automatically
+                                       //'llcontinue' => ''
+                                       'lllimit' => '2',
+                                       //'lltitle' => '',
+                                       //'lllang' => '',
+                                       'lldir' => 'ascending',
+                                       //'llurl' => '',
+                               ),
+                               array( // expected links
+                                       'Foo' => array(
+                                               array( 'lang' => 'de', '*' => 
'FooDe' ),
+                                               array( 'lang' => 'en', '*' => 
'FooEn' ),
+                                               array( 'lang' => 'fr', '*' => 
'FooFr' ),
+                                       ),
+                                       'Bar' => array(
+                                               array( 'lang' => 'de', '*' => 
'BarDe' ),
+                                               array( 'lang' => 'en', '*' => 
'BarEn' ),
+                                       ),
+                                       'Meh' => array(
+                                               array( 'lang' => 'de', '*' => 
'MehDe' ),
+                                       ),
+                               ),
+                               'multi-id query with paging'
+                       ),
+                       array( // #3: get links from multiple pages using IDs, 
with paging
+                               array( // params
+                                       'titles' => 'Foo|Bar|Meh', // will be 
translated to full titles automatically
+                                       //'pageids' => ''
+                                       //'llcontinue' => ''
+                                       'lllimit' => '2',
+                                       //'lltitle' => '',
+                                       //'lllang' => '',
+                                       'lldir' => 'descending',
+                                       //'llurl' => '',
+                               ),
+                               array( // expected links
+                                       'Meh' => array(
+                                               array( 'lang' => 'de', '*' => 
'MehDe' ),
+                                       ),
+                                       'Bar' => array(
+                                               array( 'lang' => 'en', '*' => 
'BarEn' ),
+                                               array( 'lang' => 'de', '*' => 
'BarDe' ),
+                                       ),
+                                       'Foo' => array(
+                                               array( 'lang' => 'fr', '*' => 
'FooFr' ),
+                                               array( 'lang' => 'en', '*' => 
'FooEn' ),
+                                               array( 'lang' => 'de', '*' => 
'FooDe' ),
+                                       ),
+                               ),
+                               'multi-title query with reverse paging'
+                       ),
+                       array( // #4: filter links by language
+                               array( // params
+                                       'titles' => 'Foo|Bar|Meh', // will be 
translated to full titles automatically
+                                       //'pageids' => ''
+                                       //'llcontinue' => ''
+                                       'lllimit' => '2',
+                                       //'lltitle' => '',
+                                       'lllang' => 'en',
+                                       //'lldir' => '',
+                                       //'llurl' => '',
+                               ),
+                               array( // expected links
+                                       'Foo' => array(
+                                               array( 'lang' => 'en', '*' => 
'FooEn' ),
+                                       ),
+                                       'Bar' => array(
+                                               array( 'lang' => 'en', '*' => 
'BarEn' ),
+                                       ),
+                                       'Meh' => array(),
+                               ),
+                               'filter links by language'
+                       ),
+                       array( // #5: filter links by language and title
+                               array( // params
+                                       'titles' => 'Foo|Bar|Meh', // will be 
translated to full titles automatically
+                                       //'pageids' => ''
+                                       //'llcontinue' => ''
+                                       'lllimit' => '2',
+                                       'lltitle' => 'BarEn',
+                                       'lllang' => 'en',
+                                       'lldir' => 'descending',
+                                       'llurl' => '',
+                               ),
+                               array( // expected links
+                                       'Foo' => array(),
+                                       'Bar' => array(
+                                               array( 'lang' => 'en', '*' => 
'BarEn', 'url' => 'http://acme.test/BarEn' ),
+                                       ),
+                                       'Meh' => array(),
+                               ),
+                               'filter links by language and title'
+                       ),
+               );
+       }
+
+       /**
+        * @group medium
+        * @dataProvider provideQuery
+        */
+       function testQuery( $params, $expected, $info ) {
+               $pages = $this->getTestPages();
+
+               $result = array();
+               $continue = null;
+
+               $params['action'] = 'query';
+               $params['prop'] = 'langlinks';
+
+               if ( isset($params['titles']) ) {
+                       // titles are given as handles, convert
+                       $params['titles'] = implode( '|', 
$pages->handlesToTitles( explode( '|', $params['titles'] ) ) );
+               }
+
+               if ( isset($params['pageids']) ) {
+                       // ids are given as handles, convert
+                       $params['pageids'] = implode( '|', 
$pages->handlesToIds( explode( '|', $params['pageids'] ) ) );
+               }
+
+               do { // continuation loop
+                       // perform request
+                       list( $response, , ) = $this->doApiRequest( $params );
+
+                       $this->assertArrayHasKey( 'query', $response );
+                       $this->assertArrayHasKey( 'pages', $response['query'] );
+
+                       // check continuation
+                       if ( isset( 
$response['query-continue']['langlinks']['llcontinue'] ) ) {
+                               $params['llcontinue'] = 
$response['query-continue']['langlinks']['llcontinue'];
+                       } else {
+                               $params['llcontinue'] = null;
+                       }
+
+                       // collect links
+                       $langlinks = $this->extractLangLinks( $response );
+                       $result = $this->mergeLangLinks( $result, $langlinks );
+               } while ( $params['llcontinue'] !== null );
+
+               $missingId = 0;
+               foreach ( $expected as $page => $links ) {
+                       $pageId = $pages->getTitle( $page )->getArticleID();
+
+                       if ( $pageId === 0 ) {
+                               // if the page is missing, the API will count 
down negative IDs.
+                               $pageId = --$missingId;
+                       }
+
+                       $this->assertArrayHasKey( $pageId, $result, "missing 
result for page `$page` (ID $pageId)" );
+
+                       $this->assertLinkListsEqual( $links, $result[$pageId], 
"$info\nPage `$page`"  );
+               }
+
+               $this->assertSameSize( $expected, $result, "Result must have 
the expected number of entries" );
+       }
+
+       protected function assertLinkListsEqual( $expected, $actual, $info ) {
+               $acount = count( $actual );
+               $ecount = count( $expected );
+
+               // loop over result and compare with expected
+               $i = 0;
+               while ( true ) {
+                       if ( $i >= $acount && $i >= $ecount ) {
+                               break; // done
+                       }
+
+                       if ( $i >= $ecount ) {
+                               $this->fail( "$info\nUnexpected link in result 
at position $i: "
+                                       . self::link2string( $actual[$i] ) );
+                       }
+
+                       $expectedLink = $expected[$i];
+
+                       if ( $i >= $acount ) {
+                               $this->fail( "$info\nNo more links in the 
result at position $i, expected "
+                                       . self::link2string( $expectedLink ) );
+                       }
+
+                       $this->assertLinkEquals( $expectedLink, $actual[$i], 
"$info\nPosition $i" );
+
+                       $i++;
+               }
+       }
+
+       protected static function link2string( $link ) {
+               return $link['lang'] . ':' . $link['*'];
+       }
+
+       protected function assertLinkEquals( $expected, $actual, $info ) {
+               $fields = array_merge( array_keys( (array)$expected ), 
array_keys( (array)$actual ) );
+
+               foreach ( $fields as $field ) {
+                       $this->assertFieldEquals( $field, $expected, $actual, 
$info );
+               }
+       }
+
+       protected function assertFieldEquals( $field, $expected, $actual, $info 
) {
+               if ( isset( $expected[$field] ) && !isset( $actual[$field] ) ) {
+                       $this->fail( "$info\nMissing field `$field`." );
+               }
+
+               if ( !isset( $expected[$field] ) && isset( $actual[$field] ) ) {
+                       $this->fail( "$info\nExtra field `$field`." );
+               }
+
+               if ( isset( $expected[$field] ) && isset( $actual[$field] ) ) {
+                       $this->assertEquals( $expected[$field], 
$actual[$field], "$info\nField `$field`" );
+               }
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/60278
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I4ff0855ea11f9fc82bb122590337d3cb57a82939
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to