MediaWiki
master
|
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 $this->assertLinksUpdate( $t, $po, 'pagelinks', 'pl_namespace, pl_title', 'pl_from = 111', array( 00064 array( NS_MAIN, 'Foo' ), 00065 ) ); 00066 00067 $po = new ParserOutput(); 00068 $po->setTitleText( $t->getPrefixedText() ); 00069 00070 $po->addLink( Title::newFromText( "Bar" ) ); 00071 00072 $this->assertLinksUpdate( $t, $po, 'pagelinks', 'pl_namespace, pl_title', 'pl_from = 111', array( 00073 array( NS_MAIN, 'Bar' ), 00074 ) ); 00075 } 00076 00077 public function testUpdate_externallinks() { 00078 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 ); 00079 00080 $po->addExternalLink( "http://testing.com/wiki/Foo" ); 00081 00082 $this->assertLinksUpdate( $t, $po, 'externallinks', 'el_to, el_index', 'el_from = 111', array( 00083 array( 'http://testing.com/wiki/Foo', 'http://com.testing./wiki/Foo' ), 00084 ) ); 00085 } 00086 00087 public function testUpdate_categorylinks() { 00088 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 ); 00089 00090 $po->addCategory( "Foo", "FOO" ); 00091 00092 $this->assertLinksUpdate( $t, $po, 'categorylinks', 'cl_to, cl_sortkey', 'cl_from = 111', array( 00093 array( 'Foo', "FOO\nTESTING" ), 00094 ) ); 00095 } 00096 00097 public function testUpdate_iwlinks() { 00098 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 ); 00099 00100 $target = Title::makeTitleSafe( NS_MAIN, "Foo", '', 'linksupdatetest' ); 00101 $po->addInterwikiLink( $target ); 00102 00103 $this->assertLinksUpdate( $t, $po, 'iwlinks', 'iwl_prefix, iwl_title', 'iwl_from = 111', array( 00104 array( 'linksupdatetest', 'Foo' ), 00105 ) ); 00106 } 00107 00108 public function testUpdate_templatelinks() { 00109 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 ); 00110 00111 $po->addTemplate( Title::newFromText( "Template:Foo" ), 23, 42 ); 00112 00113 $this->assertLinksUpdate( $t, $po, 'templatelinks', 'tl_namespace, tl_title', 'tl_from = 111', array( 00114 array( NS_TEMPLATE, 'Foo' ), 00115 ) ); 00116 } 00117 00118 public function testUpdate_imagelinks() { 00119 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 ); 00120 00121 $po->addImage( "Foo.png" ); 00122 00123 00124 $this->assertLinksUpdate( $t, $po, 'imagelinks', 'il_to', 'il_from = 111', array( 00125 array( 'Foo.png' ), 00126 ) ); 00127 } 00128 00129 public function testUpdate_langlinks() { 00130 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 ); 00131 00132 $po->addLanguageLink( Title::newFromText( "en:Foo" ) ); 00133 00134 00135 $this->assertLinksUpdate( $t, $po, 'langlinks', 'll_lang, ll_title', 'll_from = 111', array( 00136 array( 'En', 'Foo' ), 00137 ) ); 00138 } 00139 00140 public function testUpdate_page_props() { 00141 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 ); 00142 00143 $po->setProperty( "foo", "bar" ); 00144 00145 $this->assertLinksUpdate( $t, $po, 'page_props', 'pp_propname, pp_value', 'pp_page = 111', array( 00146 array( 'foo', 'bar' ), 00147 ) ); 00148 } 00149 00150 #@todo: test recursive, too! 00151 00152 protected function assertLinksUpdate( Title $title, ParserOutput $parserOutput, $table, $fields, $condition, array $expectedRows ) { 00153 $update = new LinksUpdate( $title, $parserOutput ); 00154 00155 //NOTE: make sure LinksUpdate does not generate warnings when called inside a transaction. 00156 $update->beginTransaction(); 00157 $update->doUpdate(); 00158 $update->commitTransaction(); 00159 00160 $this->assertSelect( $table, $fields, $condition, $expectedRows ); 00161 } 00162 } 00163