6 * ^--- make sure temporary tables are used.
8 class LinksUpdateTest
extends MediaWikiTestCase
{
10 function __construct( $name = null, array $data = array(), $dataName = '' ) {
11 parent
::__construct( $name, $data, $dataName );
13 $this->tablesUsed
= array_merge( $this->tablesUsed
,
29 protected function setUp() {
31 $dbw = wfGetDB( DB_MASTER
);
36 'iw_prefix' => 'linksupdatetest',
37 'iw_url' => 'http://testing.com/wiki/$1',
38 'iw_api' => 'http://testing.com/w/api.php',
41 'iw_wikiid' => 'linksupdatetest',
44 $this->setMwGlobals( 'wgRCWatchCategoryMembership', true );
47 public function addDBData() {
48 $this->insertPage( 'Testing' );
49 $this->insertPage( 'Some_other_page' );
50 $this->insertPage( 'Template:TestingTemplate' );
53 protected function makeTitleAndParserOutput( $name, $id ) {
54 $t = Title
::newFromText( $name );
55 $t->mArticleID
= $id; # XXX: this is fugly
57 $po = new ParserOutput();
58 $po->setTitleText( $t->getPrefixedText() );
60 return array( $t, $po );
64 * @covers ParserOutput::addLink
66 public function testUpdate_pagelinks() {
67 /** @var ParserOutput $po */
68 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
70 $po->addLink( Title
::newFromText( "Foo" ) );
71 $po->addLink( Title
::newFromText( "Special:Foo" ) ); // special namespace should be ignored
72 $po->addLink( Title
::newFromText( "linksupdatetest:Foo" ) ); // interwiki link should be ignored
73 $po->addLink( Title
::newFromText( "#Foo" ) ); // hash link should be ignored
75 $update = $this->assertLinksUpdate(
82 array( array( NS_MAIN
, 'Foo' ) )
84 $this->assertArrayEquals( array(
85 Title
::makeTitle( NS_MAIN
, 'Foo' ), // newFromText doesn't yield the same internal state....
86 ), $update->getAddedLinks() );
88 $po = new ParserOutput();
89 $po->setTitleText( $t->getPrefixedText() );
91 $po->addLink( Title
::newFromText( "Bar" ) );
92 $po->addLink( Title
::newFromText( "Talk:Bar" ) );
94 $update = $this->assertLinksUpdate(
102 array( NS_MAIN
, 'Bar' ),
103 array( NS_TALK
, 'Bar' ),
106 $this->assertArrayEquals( array(
107 Title
::makeTitle( NS_MAIN
, 'Bar' ),
108 Title
::makeTitle( NS_TALK
, 'Bar' ),
109 ), $update->getAddedLinks() );
110 $this->assertArrayEquals( array(
111 Title
::makeTitle( NS_MAIN
, 'Foo' ),
112 ), $update->getRemovedLinks() );
116 * @covers ParserOutput::addExternalLink
118 public function testUpdate_externallinks() {
119 /** @var ParserOutput $po */
120 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
122 $po->addExternalLink( "http://testing.com/wiki/Foo" );
124 $this->assertLinksUpdate( $t, $po, 'externallinks', 'el_to, el_index', 'el_from = 111', array(
125 array( 'http://testing.com/wiki/Foo', 'http://com.testing./wiki/Foo' ),
130 * @covers ParserOutput::addCategory
132 public function testUpdate_categorylinks() {
133 /** @var ParserOutput $po */
134 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
136 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
138 $po->addCategory( "Foo", "FOO" );
140 $this->assertLinksUpdate( $t, $po, 'categorylinks', 'cl_to, cl_sortkey', 'cl_from = 111', array(
141 array( 'Foo', "FOO\nTESTING" ),
145 public function testOnAddingAndRemovingCategory_recentChangesRowIsAdded() {
146 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
148 $title = Title
::newFromText( 'Testing' );
149 $wikiPage = new WikiPage( $title );
150 $wikiPage->doEditContent( new WikitextContent( '[[Category:Foo]]' ), 'added category' );
151 $this->runAllRelatedJobs();
153 $this->assertRecentChangeByCategorization(
155 $wikiPage->getParserOutput( new ParserOptions() ),
156 Title
::newFromText( 'Category:Foo' ),
157 array( array( 'Foo', '[[:Testing]] added to category' ) )
160 $wikiPage->doEditContent( new WikitextContent( '[[Category:Bar]]' ), 'replaced category' );
161 $this->runAllRelatedJobs();
163 $this->assertRecentChangeByCategorization(
165 $wikiPage->getParserOutput( new ParserOptions() ),
166 Title
::newFromText( 'Category:Foo' ),
168 array( 'Foo', '[[:Testing]] added to category' ),
169 array( 'Foo', '[[:Testing]] removed from category' ),
173 $this->assertRecentChangeByCategorization(
175 $wikiPage->getParserOutput( new ParserOptions() ),
176 Title
::newFromText( 'Category:Bar' ),
178 array( 'Bar', '[[:Testing]] added to category' ),
183 public function testOnAddingAndRemovingCategoryToTemplates_embeddingPagesAreIgnored() {
184 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
186 $templateTitle = Title
::newFromText( 'Template:TestingTemplate' );
187 $templatePage = new WikiPage( $templateTitle );
189 $wikiPage = new WikiPage( Title
::newFromText( 'Testing' ) );
190 $wikiPage->doEditContent( new WikitextContent( '{{TestingTemplate}}' ), 'added template' );
191 $this->runAllRelatedJobs();
193 $otherWikiPage = new WikiPage( Title
::newFromText( 'Some_other_page' ) );
194 $otherWikiPage->doEditContent( new WikitextContent( '{{TestingTemplate}}' ), 'added template' );
195 $this->runAllRelatedJobs();
197 $this->assertRecentChangeByCategorization(
199 $templatePage->getParserOutput( new ParserOptions() ),
200 Title
::newFromText( 'Baz' ),
204 $templatePage->doEditContent( new WikitextContent( '[[Category:Baz]]' ), 'added category' );
205 $this->runAllRelatedJobs();
207 $this->assertRecentChangeByCategorization(
209 $templatePage->getParserOutput( new ParserOptions() ),
210 Title
::newFromText( 'Baz' ),
211 array( array( 'Baz', '[[:Template:TestingTemplate]] and 2 pages added to category' ) )
216 * @covers ParserOutput::addInterwikiLink
218 public function testUpdate_iwlinks() {
219 /** @var ParserOutput $po */
220 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
222 $target = Title
::makeTitleSafe( NS_MAIN
, "Foo", '', 'linksupdatetest' );
223 $po->addInterwikiLink( $target );
225 $this->assertLinksUpdate( $t, $po, 'iwlinks', 'iwl_prefix, iwl_title', 'iwl_from = 111', array(
226 array( 'linksupdatetest', 'Foo' ),
231 * @covers ParserOutput::addTemplate
233 public function testUpdate_templatelinks() {
234 /** @var ParserOutput $po */
235 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
237 $po->addTemplate( Title
::newFromText( "Template:Foo" ), 23, 42 );
239 $this->assertLinksUpdate(
246 array( array( NS_TEMPLATE
, 'Foo' ) )
251 * @covers ParserOutput::addImage
253 public function testUpdate_imagelinks() {
254 /** @var ParserOutput $po */
255 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
257 $po->addImage( "Foo.png" );
259 $this->assertLinksUpdate( $t, $po, 'imagelinks', 'il_to', 'il_from = 111', array(
265 * @covers ParserOutput::addLanguageLink
267 public function testUpdate_langlinks() {
268 $this->setMwGlobals( array(
269 'wgCapitalLinks' => true,
272 /** @var ParserOutput $po */
273 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
275 $po->addLanguageLink( Title
::newFromText( "en:Foo" )->getFullText() );
277 $this->assertLinksUpdate( $t, $po, 'langlinks', 'll_lang, ll_title', 'll_from = 111', array(
278 array( 'En', 'Foo' ),
283 * @covers ParserOutput::setProperty
285 public function testUpdate_page_props() {
286 global $wgPagePropsHaveSortkey;
288 /** @var ParserOutput $po */
289 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
291 $fields = array( 'pp_propname', 'pp_value' );
294 $po->setProperty( "bool", true );
295 $expected[] = array( "bool", true );
297 $po->setProperty( "float", 4.0 +
1.0 / 4.0 );
298 $expected[] = array( "float", 4.0 +
1.0 / 4.0 );
300 $po->setProperty( "int", -7 );
301 $expected[] = array( "int", -7 );
303 $po->setProperty( "string", "33 bar" );
304 $expected[] = array( "string", "33 bar" );
306 // compute expected sortkey values
307 if ( $wgPagePropsHaveSortkey ) {
308 $fields[] = 'pp_sortkey';
310 foreach ( $expected as &$row ) {
313 if ( is_int( $value ) ||
is_float( $value ) ||
is_bool( $value ) ) {
314 $row[] = floatval( $value );
321 $this->assertLinksUpdate( $t, $po, 'page_props', $fields, 'pp_page = 111', $expected );
324 public function testUpdate_page_props_without_sortkey() {
325 $this->setMwGlobals( 'wgPagePropsHaveSortkey', false );
327 $this->testUpdate_page_props();
330 // @todo test recursive, too!
332 protected function assertLinksUpdate( Title
$title, ParserOutput
$parserOutput,
333 $table, $fields, $condition, array $expectedRows
335 $update = new LinksUpdate( $title, $parserOutput );
337 // NOTE: make sure LinksUpdate does not generate warnings when called inside a transaction.
338 $update->beginTransaction();
340 $update->commitTransaction();
342 $this->assertSelect( $table, $fields, $condition, $expectedRows );
346 protected function assertRecentChangeByCategorization(
347 Title
$pageTitle, ParserOutput
$parserOutput, Title
$categoryTitle, $expectedRows
351 'rc_title, rc_comment',
353 'rc_type' => RC_CATEGORIZE
,
354 'rc_namespace' => NS_CATEGORY
,
355 'rc_title' => $categoryTitle->getDBkey()
361 private function runAllRelatedJobs() {
362 $queueGroup = JobQueueGroup
::singleton();
363 while ( $job = $queueGroup->pop( 'refreshLinksPrioritized' ) ) {
365 $queueGroup->ack( $job );
367 while ( $job = $queueGroup->pop( 'categoryMembershipChange' ) ) {
369 $queueGroup->ack( $job );