7 * ^--- make sure temporary tables are used.
9 class LinksUpdateTest
extends MediaWikiLangTestCase
{
10 protected static $testingPageId;
12 function __construct( $name = null, array $data = [], $dataName = '' ) {
13 parent
::__construct( $name, $data, $dataName );
15 $this->tablesUsed
= array_merge( $this->tablesUsed
,
31 protected function setUp() {
33 $dbw = wfGetDB( DB_MASTER
);
38 'iw_prefix' => 'linksupdatetest',
39 'iw_url' => 'http://testing.com/wiki/$1',
40 'iw_api' => 'http://testing.com/w/api.php',
43 'iw_wikiid' => 'linksupdatetest',
46 $this->setMwGlobals( 'wgRCWatchCategoryMembership', true );
49 public function addDBDataOnce() {
50 $res = $this->insertPage( 'Testing' );
51 self
::$testingPageId = $res['id'];
52 $this->insertPage( 'Some_other_page' );
53 $this->insertPage( 'Template:TestingTemplate' );
56 protected function makeTitleAndParserOutput( $name, $id ) {
57 $t = Title
::newFromText( $name );
58 $t->mArticleID
= $id; # XXX: this is fugly
60 $po = new ParserOutput();
61 $po->setTitleText( $t->getPrefixedText() );
67 * @covers ParserOutput::addLink
69 public function testUpdate_pagelinks() {
71 /** @var ParserOutput $po */
72 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self
::$testingPageId );
74 $po->addLink( Title
::newFromText( "Foo" ) );
75 $po->addLink( Title
::newFromText( "Special:Foo" ) ); // special namespace should be ignored
76 $po->addLink( Title
::newFromText( "linksupdatetest:Foo" ) ); // interwiki link should be ignored
77 $po->addLink( Title
::newFromText( "#Foo" ) ); // hash link should be ignored
79 $update = $this->assertLinksUpdate(
85 'pl_from = ' . self
::$testingPageId,
86 [ [ NS_MAIN
, 'Foo' ] ]
88 $this->assertArrayEquals( [
89 Title
::makeTitle( NS_MAIN
, 'Foo' ), // newFromText doesn't yield the same internal state....
90 ], $update->getAddedLinks() );
92 $po = new ParserOutput();
93 $po->setTitleText( $t->getPrefixedText() );
95 $po->addLink( Title
::newFromText( "Bar" ) );
96 $po->addLink( Title
::newFromText( "Talk:Bar" ) );
98 $update = $this->assertLinksUpdate(
104 'pl_from = ' . self
::$testingPageId,
110 $this->assertArrayEquals( [
111 Title
::makeTitle( NS_MAIN
, 'Bar' ),
112 Title
::makeTitle( NS_TALK
, 'Bar' ),
113 ], $update->getAddedLinks() );
114 $this->assertArrayEquals( [
115 Title
::makeTitle( NS_MAIN
, 'Foo' ),
116 ], $update->getRemovedLinks() );
120 * @covers ParserOutput::addExternalLink
121 * @covers LinksUpdate::getAddedExternalLinks
122 * @covers LinksUpdate::getRemovedExternalLinks
124 public function testUpdate_externallinks() {
125 /** @var ParserOutput $po */
126 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self
::$testingPageId );
128 $po->addExternalLink( "http://testing.com/wiki/Foo" );
130 $update = $this->assertLinksUpdate(
135 'el_from = ' . self
::$testingPageId,
137 [ 'http://testing.com/wiki/Foo', 'http://com.testing./wiki/Foo' ],
141 $this->assertArrayEquals( [
142 "http://testing.com/wiki/Foo"
143 ], $update->getAddedExternalLinks() );
145 $po = new ParserOutput();
146 $po->setTitleText( $t->getPrefixedText() );
147 $po->addExternalLink( 'http://testing.com/wiki/Bar' );
148 $update = $this->assertLinksUpdate(
153 'el_from = ' . self
::$testingPageId,
155 [ 'http://testing.com/wiki/Bar', 'http://com.testing./wiki/Bar' ],
159 $this->assertArrayEquals( [
160 "http://testing.com/wiki/Bar"
161 ], $update->getAddedExternalLinks() );
162 $this->assertArrayEquals( [
163 "http://testing.com/wiki/Foo"
164 ], $update->getRemovedExternalLinks() );
168 * @covers ParserOutput::addCategory
170 public function testUpdate_categorylinks() {
171 /** @var ParserOutput $po */
172 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
174 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self
::$testingPageId );
176 $po->addCategory( "Foo", "FOO" );
178 $this->assertLinksUpdate(
183 'cl_from = ' . self
::$testingPageId,
184 [ [ 'Foo', "FOO\nTESTING" ] ]
188 public function testOnAddingAndRemovingCategory_recentChangesRowIsAdded() {
189 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
191 $title = Title
::newFromText( 'Testing' );
192 $wikiPage = new WikiPage( $title );
193 $wikiPage->doEditContent( new WikitextContent( '[[Category:Foo]]' ), 'added category' );
194 $this->runAllRelatedJobs();
196 $this->assertRecentChangeByCategorization(
198 $wikiPage->getParserOutput( ParserOptions
::newCanonical() ),
199 Title
::newFromText( 'Category:Foo' ),
200 [ [ 'Foo', '[[:Testing]] added to category' ] ]
203 $wikiPage->doEditContent( new WikitextContent( '[[Category:Bar]]' ), 'replaced category' );
204 $this->runAllRelatedJobs();
206 $this->assertRecentChangeByCategorization(
208 $wikiPage->getParserOutput( ParserOptions
::newCanonical() ),
209 Title
::newFromText( 'Category:Foo' ),
211 [ 'Foo', '[[:Testing]] added to category' ],
212 [ 'Foo', '[[:Testing]] removed from category' ],
216 $this->assertRecentChangeByCategorization(
218 $wikiPage->getParserOutput( ParserOptions
::newCanonical() ),
219 Title
::newFromText( 'Category:Bar' ),
221 [ 'Bar', '[[:Testing]] added to category' ],
226 public function testOnAddingAndRemovingCategoryToTemplates_embeddingPagesAreIgnored() {
227 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
229 $templateTitle = Title
::newFromText( 'Template:TestingTemplate' );
230 $templatePage = new WikiPage( $templateTitle );
232 $wikiPage = new WikiPage( Title
::newFromText( 'Testing' ) );
233 $wikiPage->doEditContent( new WikitextContent( '{{TestingTemplate}}' ), 'added template' );
234 $this->runAllRelatedJobs();
236 $otherWikiPage = new WikiPage( Title
::newFromText( 'Some_other_page' ) );
237 $otherWikiPage->doEditContent( new WikitextContent( '{{TestingTemplate}}' ), 'added template' );
238 $this->runAllRelatedJobs();
240 $this->assertRecentChangeByCategorization(
242 $templatePage->getParserOutput( ParserOptions
::newCanonical() ),
243 Title
::newFromText( 'Baz' ),
247 $templatePage->doEditContent( new WikitextContent( '[[Category:Baz]]' ), 'added category' );
248 $this->runAllRelatedJobs();
250 $this->assertRecentChangeByCategorization(
252 $templatePage->getParserOutput( ParserOptions
::newCanonical() ),
253 Title
::newFromText( 'Baz' ),
256 '[[:Template:TestingTemplate]] added to category, ' .
257 '[[Special:WhatLinksHere/Template:TestingTemplate|this page is included within other pages]]'
263 * @covers ParserOutput::addInterwikiLink
265 public function testUpdate_iwlinks() {
266 /** @var ParserOutput $po */
267 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self
::$testingPageId );
269 $target = Title
::makeTitleSafe( NS_MAIN
, "Foo", '', 'linksupdatetest' );
270 $po->addInterwikiLink( $target );
272 $this->assertLinksUpdate(
276 'iwl_prefix, iwl_title',
277 'iwl_from = ' . self
::$testingPageId,
278 [ [ 'linksupdatetest', 'Foo' ] ]
283 * @covers ParserOutput::addTemplate
285 public function testUpdate_templatelinks() {
286 /** @var ParserOutput $po */
287 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self
::$testingPageId );
289 $po->addTemplate( Title
::newFromText( "Template:Foo" ), 23, 42 );
291 $this->assertLinksUpdate(
297 'tl_from = ' . self
::$testingPageId,
298 [ [ NS_TEMPLATE
, 'Foo' ] ]
303 * @covers ParserOutput::addImage
305 public function testUpdate_imagelinks() {
306 /** @var ParserOutput $po */
307 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self
::$testingPageId );
309 $po->addImage( "Foo.png" );
311 $this->assertLinksUpdate(
316 'il_from = ' . self
::$testingPageId,
322 * @covers ParserOutput::addLanguageLink
324 public function testUpdate_langlinks() {
325 $this->setMwGlobals( [
326 'wgCapitalLinks' => true,
329 /** @var ParserOutput $po */
330 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self
::$testingPageId );
332 $po->addLanguageLink( Title
::newFromText( "en:Foo" )->getFullText() );
334 $this->assertLinksUpdate(
339 'll_from = ' . self
::$testingPageId,
345 * @covers ParserOutput::setProperty
347 public function testUpdate_page_props() {
348 global $wgPagePropsHaveSortkey;
350 /** @var ParserOutput $po */
351 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self
::$testingPageId );
353 $fields = [ 'pp_propname', 'pp_value' ];
356 $po->setProperty( "bool", true );
357 $expected[] = [ "bool", true ];
359 $po->setProperty( "float", 4.0 +
1.0 / 4.0 );
360 $expected[] = [ "float", 4.0 +
1.0 / 4.0 ];
362 $po->setProperty( "int", -7 );
363 $expected[] = [ "int", -7 ];
365 $po->setProperty( "string", "33 bar" );
366 $expected[] = [ "string", "33 bar" ];
368 // compute expected sortkey values
369 if ( $wgPagePropsHaveSortkey ) {
370 $fields[] = 'pp_sortkey';
372 foreach ( $expected as &$row ) {
375 if ( is_int( $value ) ||
is_float( $value ) ||
is_bool( $value ) ) {
376 $row[] = floatval( $value );
383 $this->assertLinksUpdate(
384 $t, $po, 'page_props', $fields, 'pp_page = ' . self
::$testingPageId, $expected );
387 public function testUpdate_page_props_without_sortkey() {
388 $this->setMwGlobals( 'wgPagePropsHaveSortkey', false );
390 $this->testUpdate_page_props();
393 // @todo test recursive, too!
395 protected function assertLinksUpdate( Title
$title, ParserOutput
$parserOutput,
396 $table, $fields, $condition, array $expectedRows
398 $update = new LinksUpdate( $title, $parserOutput );
402 $this->assertSelect( $table, $fields, $condition, $expectedRows );
406 protected function assertRecentChangeByCategorization(
407 Title
$pageTitle, ParserOutput
$parserOutput, Title
$categoryTitle, $expectedRows
410 [ 'recentchanges', 'comment' ],
411 'rc_title, comment_text',
413 'rc_type' => RC_CATEGORIZE
,
414 'rc_namespace' => NS_CATEGORY
,
415 'rc_title' => $categoryTitle->getDBkey(),
416 'comment_id = rc_comment_id',
422 private function runAllRelatedJobs() {
423 $queueGroup = JobQueueGroup
::singleton();
424 while ( $job = $queueGroup->pop( 'refreshLinksPrioritized' ) ) {
426 $queueGroup->ack( $job );
428 while ( $job = $queueGroup->pop( 'categoryMembershipChange' ) ) {
430 $queueGroup->ack( $job );