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