MediaWiki  REL1_23
LinksUpdateTest.php
Go to the documentation of this file.
00001 <?php
00002 
00007 class LinksUpdateTest extends MediaWikiTestCase {
00008 
00009     function __construct( $name = null, array $data = array(), $dataName = '' ) {
00010         parent::__construct( $name, $data, $dataName );
00011 
00012         $this->tablesUsed = array_merge( $this->tablesUsed,
00013             array(
00014                 'interwiki',
00015                 'page_props',
00016                 'pagelinks',
00017                 'categorylinks',
00018                 'langlinks',
00019                 'externallinks',
00020                 'imagelinks',
00021                 'templatelinks',
00022                 'iwlinks'
00023             )
00024         );
00025     }
00026 
00027     protected function setUp() {
00028         parent::setUp();
00029         $dbw = wfGetDB( DB_MASTER );
00030         $dbw->replace(
00031             'interwiki',
00032             array( 'iw_prefix' ),
00033             array(
00034                 'iw_prefix' => 'linksupdatetest',
00035                 'iw_url' => 'http://testing.com/wiki/$1',
00036                 'iw_api' => 'http://testing.com/w/api.php',
00037                 'iw_local' => 0,
00038                 'iw_trans' => 0,
00039                 'iw_wikiid' => 'linksupdatetest',
00040             )
00041         );
00042     }
00043 
00044     protected function makeTitleAndParserOutput( $name, $id ) {
00045         $t = Title::newFromText( $name );
00046         $t->mArticleID = $id; # XXX: this is fugly
00047 
00048         $po = new ParserOutput();
00049         $po->setTitleText( $t->getPrefixedText() );
00050 
00051         return array( $t, $po );
00052     }
00053 
00057     public function testUpdate_pagelinks() {
00059         list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
00060 
00061         $po->addLink( Title::newFromText( "Foo" ) );
00062         $po->addLink( Title::newFromText( "Special:Foo" ) ); // special namespace should be ignored
00063         $po->addLink( Title::newFromText( "linksupdatetest:Foo" ) ); // interwiki link should be ignored
00064         $po->addLink( Title::newFromText( "#Foo" ) ); // hash link should be ignored
00065 
00066         $update = $this->assertLinksUpdate( $t, $po, 'pagelinks', 'pl_namespace, pl_title', 'pl_from = 111', array(
00067             array( NS_MAIN, 'Foo' ),
00068         ) );
00069         $this->assertArrayEquals( array(
00070             Title::makeTitle( NS_MAIN, 'Foo' ),  // newFromText doesn't yield the same internal state....
00071         ), $update->getAddedLinks() );
00072 
00073         $po = new ParserOutput();
00074         $po->setTitleText( $t->getPrefixedText() );
00075 
00076         $po->addLink( Title::newFromText( "Bar" ) );
00077         $po->addLink( Title::newFromText( "Talk:Bar" ) );
00078 
00079         $update = $this->assertLinksUpdate( $t, $po, 'pagelinks', 'pl_namespace, pl_title', 'pl_from = 111', array(
00080             array( NS_MAIN, 'Bar' ),
00081             array( NS_TALK, 'Bar' ),
00082         ) );
00083         $this->assertArrayEquals( array(
00084             Title::makeTitle( NS_MAIN, 'Bar' ),
00085             Title::makeTitle( NS_TALK, 'Bar' ),
00086         ), $update->getAddedLinks() );
00087         $this->assertArrayEquals( array(
00088             Title::makeTitle( NS_MAIN, 'Foo' ),
00089         ), $update->getRemovedLinks() );
00090     }
00091 
00095     public function testUpdate_externallinks() {
00097         list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
00098 
00099         $po->addExternalLink( "http://testing.com/wiki/Foo" );
00100 
00101         $this->assertLinksUpdate( $t, $po, 'externallinks', 'el_to, el_index', 'el_from = 111', array(
00102             array( 'http://testing.com/wiki/Foo', 'http://com.testing./wiki/Foo' ),
00103         ) );
00104     }
00105 
00109     public function testUpdate_categorylinks() {
00111         $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
00112 
00113         list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
00114 
00115         $po->addCategory( "Foo", "FOO" );
00116 
00117         $this->assertLinksUpdate( $t, $po, 'categorylinks', 'cl_to, cl_sortkey', 'cl_from = 111', array(
00118             array( 'Foo', "FOO\nTESTING" ),
00119         ) );
00120     }
00121 
00125     public function testUpdate_iwlinks() {
00127         list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
00128 
00129         $target = Title::makeTitleSafe( NS_MAIN, "Foo", '', 'linksupdatetest' );
00130         $po->addInterwikiLink( $target );
00131 
00132         $this->assertLinksUpdate( $t, $po, 'iwlinks', 'iwl_prefix, iwl_title', 'iwl_from = 111', array(
00133             array( 'linksupdatetest', 'Foo' ),
00134         ) );
00135     }
00136 
00140     public function testUpdate_templatelinks() {
00142         list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
00143 
00144         $po->addTemplate( Title::newFromText( "Template:Foo" ), 23, 42 );
00145 
00146         $this->assertLinksUpdate( $t, $po, 'templatelinks', 'tl_namespace, tl_title', 'tl_from = 111', array(
00147             array( NS_TEMPLATE, 'Foo' ),
00148         ) );
00149     }
00150 
00154     public function testUpdate_imagelinks() {
00156         list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
00157 
00158         $po->addImage( "Foo.png" );
00159 
00160         $this->assertLinksUpdate( $t, $po, 'imagelinks', 'il_to', 'il_from = 111', array(
00161             array( 'Foo.png' ),
00162         ) );
00163     }
00164 
00168     public function testUpdate_langlinks() {
00170         list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
00171 
00172         $po->addLanguageLink( Title::newFromText( "en:Foo" )->getFullText() );
00173 
00174         $this->assertLinksUpdate( $t, $po, 'langlinks', 'll_lang, ll_title', 'll_from = 111', array(
00175             array( 'En', 'Foo' ),
00176         ) );
00177     }
00178 
00182     public function testUpdate_page_props() {
00184         list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
00185 
00186         $po->setProperty( "foo", "bar" );
00187 
00188         $this->assertLinksUpdate( $t, $po, 'page_props', 'pp_propname, pp_value', 'pp_page = 111', array(
00189             array( 'foo', 'bar' ),
00190         ) );
00191     }
00192 
00193     // @todo test recursive, too!
00194 
00195     protected function assertLinksUpdate( Title $title, ParserOutput $parserOutput, $table, $fields, $condition, array $expectedRows ) {
00196         $update = new LinksUpdate( $title, $parserOutput );
00197 
00198         //NOTE: make sure LinksUpdate does not generate warnings when called inside a transaction.
00199         $update->beginTransaction();
00200         $update->doUpdate();
00201         $update->commitTransaction();
00202 
00203         $this->assertSelect( $table, $fields, $condition, $expectedRows );
00204         return $update;
00205     }
00206 }