MediaWiki  REL1_24
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(
00067             $t,
00068             $po,
00069             'pagelinks',
00070             'pl_namespace,
00071             pl_title',
00072             'pl_from = 111',
00073             array( array( NS_MAIN, 'Foo' ) )
00074         );
00075         $this->assertArrayEquals( array(
00076             Title::makeTitle( NS_MAIN, 'Foo' ),  // newFromText doesn't yield the same internal state....
00077         ), $update->getAddedLinks() );
00078 
00079         $po = new ParserOutput();
00080         $po->setTitleText( $t->getPrefixedText() );
00081 
00082         $po->addLink( Title::newFromText( "Bar" ) );
00083         $po->addLink( Title::newFromText( "Talk:Bar" ) );
00084 
00085         $update = $this->assertLinksUpdate(
00086             $t,
00087             $po,
00088             'pagelinks',
00089             'pl_namespace,
00090             pl_title',
00091             'pl_from = 111',
00092             array(
00093                 array( NS_MAIN, 'Bar' ),
00094                 array( NS_TALK, 'Bar' ),
00095             )
00096         );
00097         $this->assertArrayEquals( array(
00098             Title::makeTitle( NS_MAIN, 'Bar' ),
00099             Title::makeTitle( NS_TALK, 'Bar' ),
00100         ), $update->getAddedLinks() );
00101         $this->assertArrayEquals( array(
00102             Title::makeTitle( NS_MAIN, 'Foo' ),
00103         ), $update->getRemovedLinks() );
00104     }
00105 
00109     public function testUpdate_externallinks() {
00111         list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
00112 
00113         $po->addExternalLink( "http://testing.com/wiki/Foo" );
00114 
00115         $this->assertLinksUpdate( $t, $po, 'externallinks', 'el_to, el_index', 'el_from = 111', array(
00116             array( 'http://testing.com/wiki/Foo', 'http://com.testing./wiki/Foo' ),
00117         ) );
00118     }
00119 
00123     public function testUpdate_categorylinks() {
00125         $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
00126 
00127         list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
00128 
00129         $po->addCategory( "Foo", "FOO" );
00130 
00131         $this->assertLinksUpdate( $t, $po, 'categorylinks', 'cl_to, cl_sortkey', 'cl_from = 111', array(
00132             array( 'Foo', "FOO\nTESTING" ),
00133         ) );
00134     }
00135 
00139     public function testUpdate_iwlinks() {
00141         list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
00142 
00143         $target = Title::makeTitleSafe( NS_MAIN, "Foo", '', 'linksupdatetest' );
00144         $po->addInterwikiLink( $target );
00145 
00146         $this->assertLinksUpdate( $t, $po, 'iwlinks', 'iwl_prefix, iwl_title', 'iwl_from = 111', array(
00147             array( 'linksupdatetest', 'Foo' ),
00148         ) );
00149     }
00150 
00154     public function testUpdate_templatelinks() {
00156         list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
00157 
00158         $po->addTemplate( Title::newFromText( "Template:Foo" ), 23, 42 );
00159 
00160         $this->assertLinksUpdate(
00161             $t,
00162             $po,
00163             'templatelinks',
00164             'tl_namespace,
00165             tl_title',
00166             'tl_from = 111',
00167             array( array( NS_TEMPLATE, 'Foo' ) )
00168         );
00169     }
00170 
00174     public function testUpdate_imagelinks() {
00176         list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
00177 
00178         $po->addImage( "Foo.png" );
00179 
00180         $this->assertLinksUpdate( $t, $po, 'imagelinks', 'il_to', 'il_from = 111', array(
00181             array( 'Foo.png' ),
00182         ) );
00183     }
00184 
00188     public function testUpdate_langlinks() {
00189         $this->setMwGlobals( array(
00190             'wgCapitalLinks' => true,
00191         ) );
00192 
00194         list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
00195 
00196         $po->addLanguageLink( Title::newFromText( "en:Foo" )->getFullText() );
00197 
00198         $this->assertLinksUpdate( $t, $po, 'langlinks', 'll_lang, ll_title', 'll_from = 111', array(
00199             array( 'En', 'Foo' ),
00200         ) );
00201     }
00202 
00206     public function testUpdate_page_props() {
00207         global $wgPagePropsHaveSortkey;
00208 
00210         list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
00211 
00212         $fields = array( 'pp_propname', 'pp_value' );
00213         $expected = array();
00214 
00215         $po->setProperty( "bool", true );
00216         $expected[] = array( "bool", true );
00217 
00218         $po->setProperty( "float", 4.0 + 1.0 / 4.0 );
00219         $expected[] = array( "float", 4.0 + 1.0 / 4.0 );
00220 
00221         $po->setProperty( "int", -7 );
00222         $expected[] = array( "int", -7 );
00223 
00224         $po->setProperty( "string", "33 bar" );
00225         $expected[] = array( "string", "33 bar" );
00226 
00227         // compute expected sortkey values
00228         if ( $wgPagePropsHaveSortkey ) {
00229             $fields[] = 'pp_sortkey';
00230 
00231             foreach ( $expected as &$row ) {
00232                 $value = $row[1];
00233 
00234                 if ( is_int( $value ) || is_float( $value ) || is_bool( $value ) ) {
00235                     $row[] = floatval( $value );
00236                 } else {
00237                     $row[] = null;
00238                 }
00239             }
00240         }
00241 
00242         $this->assertLinksUpdate( $t, $po, 'page_props', $fields, 'pp_page = 111', $expected );
00243     }
00244 
00245     public function testUpdate_page_props_without_sortkey() {
00246         $this->setMwGlobals( 'wgPagePropsHaveSortkey', false );
00247 
00248         $this->testUpdate_page_props();
00249     }
00250 
00251     // @todo test recursive, too!
00252 
00253     protected function assertLinksUpdate( Title $title, ParserOutput $parserOutput,
00254         $table, $fields, $condition, array $expectedRows
00255     ) {
00256         $update = new LinksUpdate( $title, $parserOutput );
00257 
00258         //NOTE: make sure LinksUpdate does not generate warnings when called inside a transaction.
00259         $update->beginTransaction();
00260         $update->doUpdate();
00261         $update->commitTransaction();
00262 
00263         $this->assertSelect( $table, $fields, $condition, $expectedRows );
00264         return $update;
00265     }
00266 }