Introducing LinksUpdateTest.
authordaniel <daniel.kinzler@wikimedia.de>
Thu, 10 May 2012 20:56:34 +0000 (22:56 +0200)
committerdaniel <daniel.kinzler@wikimedia.de>
Sun, 13 May 2012 15:07:10 +0000 (17:07 +0200)
This test case is intended to test the updating of various links
tables using a LinksUpdate object, based on the information in
a ParserOutput object.

Patch set 3: Suggestions by Aaron:
             factored assertSelect() out into MediaWikiTestCase.
             Force sorting of result set under testing.

Change-Id: I2d01e67ee7396080a04e5dff637aca0dc159c65d

tests/phpunit/MediaWikiTestCase.php
tests/phpunit/includes/LinksUpdateTest.php [new file with mode: 0644]

index 0791711..8c6a411 100644 (file)
@@ -341,4 +341,65 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                wfDeprecated( $function );
                wfRestoreWarnings();
        }
+
+       /**
+        * Asserts that the given database query yields the rows given by $expectedRows.
+        * The expected rows should be given as indexed (not associative) arrays, with
+        * the values given in the order of the columns in the $fields parameter.
+        * Note that the rows are sorted by the columns given in $fields.
+        *
+        * @param $table String|Array the table(s) to query
+        * @param $fields String|Array the columns to include in the result (and to sort by)
+        * @param $condition String|Array "where" condition(s)
+        * @param $expectedRows Array - an array of arrays giving the expected rows.
+        *
+        * @throws MWException if this test cases's needsDB() method doesn't return true.
+        *         Test cases can use "@group Database" to enable database test support,
+        *         or list the tables under testing in $this->tablesUsed, or override the
+        *         needsDB() method.
+        */
+       protected function assertSelect( $table, $fields, $condition, Array $expectedRows ) {
+               if ( !$this->needsDB() ) {
+                       throw new MWException( 'When testing database state, the test cases\'s needDB()' .
+                               ' method should return true. Use @group Database or $this->tablesUsed.');
+               }
+
+               $db = wfGetDB( DB_SLAVE );
+
+               $res = $db->select( $table, $fields, $condition, array( 'ORDER BY' => $fields ) );
+               $this->assertNotEmpty( $res, "query failed: " . $db->lastError() );
+
+               $i = 0;
+
+               foreach ( $expectedRows as $expected ) {
+                       $r = $res->fetchRow();
+                       self::stripStringKeys( $r );
+
+                       $i += 1;
+                       $this->assertNotEmpty( $r, "row #$i missing" );
+
+                       $this->assertEquals( $expected, $r, "row #$i mismatches" );
+               }
+
+               $r = $res->fetchRow();
+               self::stripStringKeys( $r );
+
+               $this->assertFalse( $r, "found extra row (after #$i)" );
+       }
+
+       /**
+        * Utility function for eliminating all string keys from an array.
+        * Useful to turn a database result row as returned by fetchRow() into
+        * a pure indexed array.
+        *
+        * @static
+        * @param $r mixed the array to remove string keys from.
+        */
+       protected static function stripStringKeys( &$r ) {
+               if ( !is_array( $r ) ) return;
+
+               foreach ( $r as $k => $v ) {
+                       if ( is_string( $k ) ) unset( $r[$k] );
+               }
+       }
 }
diff --git a/tests/phpunit/includes/LinksUpdateTest.php b/tests/phpunit/includes/LinksUpdateTest.php
new file mode 100644 (file)
index 0000000..4946200
--- /dev/null
@@ -0,0 +1,154 @@
+<?php
+
+/**
+ *
+ * @group Database
+ * ^--- make sure temporary tables are used.
+ */
+class LinksUpdateTest extends MediaWikiTestCase {
+
+       function  __construct( $name = null, array $data = array(), $dataName = '' ) {
+               parent::__construct( $name, $data, $dataName );
+
+               $this->tablesUsed = array_merge ( $this->tablesUsed,
+                                                                                       array( 'interwiki',
+
+                                                                                               'page_props',
+                                                                                               'pagelinks',
+                                                                                               'categorylinks',
+                                                                                               'langlinks',
+                                                                                               'externallinks',
+                                                                                               'imagelinks',
+                                                                                               'templatelinks',
+                                                                                               'iwlinks' ) );
+       }
+
+       function setUp() {
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->replace( 'interwiki',
+                                               array('iw_prefix'),
+                                               array( 'iw_prefix' => 'linksupdatetest',
+                                                      'iw_url' => 'http://testing.com/wiki/$1',
+                                                      'iw_api' => 'http://testing.com/w/api.php',
+                                                      'iw_local' => 0,
+                                                      'iw_trans' => 0,
+                                                      'iw_wikiid' => 'linksupdatetest',
+                                               ) );
+       }
+
+       protected function makeTitleAndParserOutput( $name, $id ) {
+               $t = Title::newFromText( $name );
+               $t->mArticleID = $id; # XXX: this is fugly
+
+               $po = new ParserOutput();
+               $po->setTitleText( $t->getPrefixedText() );
+
+               return array( $t, $po );
+       }
+
+       public function testUpdate_pagelinks() {
+               list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
+
+               $po->addLink( Title::newFromText( "Foo" ) );
+               $po->addLink( Title::newFromText( "Special:Foo" ) ); // special namespace should be ignored
+               $po->addLink( Title::newFromText( "linksupdatetest:Foo" ) ); // interwiki link should be ignored
+               $po->addLink( Title::newFromText( "#Foo" ) ); // hash link should be ignored
+
+               $this->assertLinksUpdate( $t, $po, 'pagelinks', 'pl_namespace, pl_title', 'pl_from = 111', array(
+                       array( NS_MAIN, 'Foo' ),
+               ) );
+
+               $po = new ParserOutput();
+               $po->setTitleText( $t->getPrefixedText() );
+
+               $po->addLink( Title::newFromText( "Bar" ) );
+
+               $this->assertLinksUpdate( $t, $po, 'pagelinks', 'pl_namespace, pl_title', 'pl_from = 111', array(
+                       array( NS_MAIN, 'Bar' ),
+               ) );
+       }
+
+       public function testUpdate_externallinks() {
+               list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
+
+               $po->addExternalLink( "http://testing.com/wiki/Foo" );
+
+               $this->assertLinksUpdate( $t, $po, 'externallinks', 'el_to, el_index', 'el_from = 111', array(
+                       array( 'http://testing.com/wiki/Foo', 'http://com.testing./wiki/Foo' ),
+               ) );
+       }
+
+       public function testUpdate_categorylinks() {
+               list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
+
+               $po->addCategory( "Foo", "FOO" );
+
+               $this->assertLinksUpdate( $t, $po, 'categorylinks', 'cl_to, cl_sortkey', 'cl_from = 111', array(
+                       array( 'Foo', "FOO\nTESTING" ),
+               ) );
+       }
+
+       public function testUpdate_iwlinks() {
+               list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
+
+               $target = Title::makeTitleSafe( NS_MAIN, "Foo", '', 'linksupdatetest' );
+               $po->addInterwikiLink( $target );
+
+               $this->assertLinksUpdate( $t, $po, 'iwlinks', 'iwl_prefix, iwl_title', 'iwl_from = 111', array(
+                       array( 'linksupdatetest', 'Foo' ),
+               ) );
+       }
+
+       public function testUpdate_templatelinks() {
+               list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
+
+               $po->addTemplate( Title::newFromText( "Template:Foo" ), 23, 42 );
+
+               $this->assertLinksUpdate( $t, $po, 'templatelinks', 'tl_namespace, tl_title', 'tl_from = 111', array(
+                       array( NS_TEMPLATE, 'Foo' ),
+               ) );
+       }
+
+       public function testUpdate_imagelinks() {
+               list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
+
+               $po->addImage( "Foo.png" );
+
+
+               $this->assertLinksUpdate( $t, $po, 'imagelinks', 'il_to', 'il_from = 111', array(
+                       array( 'Foo.png' ),
+               ) );
+       }
+
+       public function testUpdate_langlinks() {
+               list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
+
+               $po->addLanguageLink( Title::newFromText( "en:Foo" ) );
+
+
+               $this->assertLinksUpdate( $t, $po, 'langlinks', 'll_lang, ll_title', 'll_from = 111', array(
+                       array( 'En', 'Foo' ),
+               ) );
+       }
+
+       public function testUpdate_page_props() {
+               list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
+
+               $po->setProperty( "foo", "bar" );
+
+               $this->assertLinksUpdate( $t, $po, 'page_props', 'pp_propname, pp_value', 'pp_page = 111', array(
+                       array( 'foo', 'bar' ),
+               ) );
+       }
+
+       #@todo: test recursive, too!
+
+       protected function assertLinksUpdate( Title $title, ParserOutput $parserOutput, $table, $fields, $condition, Array $expectedRows ) {
+               $update = new LinksUpdate( $title, $parserOutput );
+
+               $update->doUpdate();
+
+               $this->assertSelect( $table, $fields, $condition, $expectedRows );
+       }
+}
+