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
122 public function testUpdate_externallinks() {
123 /** @var ParserOutput $po */
124 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self
::$testingPageId );
126 $po->addExternalLink( "http://testing.com/wiki/Foo" );
128 $this->assertLinksUpdate(
133 'el_from = ' . self
::$testingPageId,
135 [ 'http://testing.com/wiki/Foo', 'http://com.testing./wiki/Foo' ],
141 * @covers ParserOutput::addCategory
143 public function testUpdate_categorylinks() {
144 /** @var ParserOutput $po */
145 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
147 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self
::$testingPageId );
149 $po->addCategory( "Foo", "FOO" );
151 $this->assertLinksUpdate(
156 'cl_from = ' . self
::$testingPageId,
157 [ [ 'Foo', "FOO\nTESTING" ] ]
161 public function testOnAddingAndRemovingCategory_recentChangesRowIsAdded() {
162 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
164 $title = Title
::newFromText( 'Testing' );
165 $wikiPage = new WikiPage( $title );
166 $wikiPage->doEditContent( new WikitextContent( '[[Category:Foo]]' ), 'added category' );
167 $this->runAllRelatedJobs();
169 $this->assertRecentChangeByCategorization(
171 $wikiPage->getParserOutput( ParserOptions
::newCanonical() ),
172 Title
::newFromText( 'Category:Foo' ),
173 [ [ 'Foo', '[[:Testing]] added to category' ] ]
176 $wikiPage->doEditContent( new WikitextContent( '[[Category:Bar]]' ), 'replaced category' );
177 $this->runAllRelatedJobs();
179 $this->assertRecentChangeByCategorization(
181 $wikiPage->getParserOutput( ParserOptions
::newCanonical() ),
182 Title
::newFromText( 'Category:Foo' ),
184 [ 'Foo', '[[:Testing]] added to category' ],
185 [ 'Foo', '[[:Testing]] removed from category' ],
189 $this->assertRecentChangeByCategorization(
191 $wikiPage->getParserOutput( ParserOptions
::newCanonical() ),
192 Title
::newFromText( 'Category:Bar' ),
194 [ 'Bar', '[[:Testing]] added to category' ],
199 public function testOnAddingAndRemovingCategoryToTemplates_embeddingPagesAreIgnored() {
200 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
202 $templateTitle = Title
::newFromText( 'Template:TestingTemplate' );
203 $templatePage = new WikiPage( $templateTitle );
205 $wikiPage = new WikiPage( Title
::newFromText( 'Testing' ) );
206 $wikiPage->doEditContent( new WikitextContent( '{{TestingTemplate}}' ), 'added template' );
207 $this->runAllRelatedJobs();
209 $otherWikiPage = new WikiPage( Title
::newFromText( 'Some_other_page' ) );
210 $otherWikiPage->doEditContent( new WikitextContent( '{{TestingTemplate}}' ), 'added template' );
211 $this->runAllRelatedJobs();
213 $this->assertRecentChangeByCategorization(
215 $templatePage->getParserOutput( ParserOptions
::newCanonical() ),
216 Title
::newFromText( 'Baz' ),
220 $templatePage->doEditContent( new WikitextContent( '[[Category:Baz]]' ), 'added category' );
221 $this->runAllRelatedJobs();
223 $this->assertRecentChangeByCategorization(
225 $templatePage->getParserOutput( ParserOptions
::newCanonical() ),
226 Title
::newFromText( 'Baz' ),
229 '[[:Template:TestingTemplate]] added to category, ' .
230 '[[Special:WhatLinksHere/Template:TestingTemplate|this page is included within other pages]]'
236 * @covers ParserOutput::addInterwikiLink
238 public function testUpdate_iwlinks() {
239 /** @var ParserOutput $po */
240 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self
::$testingPageId );
242 $target = Title
::makeTitleSafe( NS_MAIN
, "Foo", '', 'linksupdatetest' );
243 $po->addInterwikiLink( $target );
245 $this->assertLinksUpdate(
249 'iwl_prefix, iwl_title',
250 'iwl_from = ' . self
::$testingPageId,
251 [ [ 'linksupdatetest', 'Foo' ] ]
256 * @covers ParserOutput::addTemplate
258 public function testUpdate_templatelinks() {
259 /** @var ParserOutput $po */
260 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self
::$testingPageId );
262 $po->addTemplate( Title
::newFromText( "Template:Foo" ), 23, 42 );
264 $this->assertLinksUpdate(
270 'tl_from = ' . self
::$testingPageId,
271 [ [ NS_TEMPLATE
, 'Foo' ] ]
276 * @covers ParserOutput::addImage
278 public function testUpdate_imagelinks() {
279 /** @var ParserOutput $po */
280 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self
::$testingPageId );
282 $po->addImage( "Foo.png" );
284 $this->assertLinksUpdate(
289 'il_from = ' . self
::$testingPageId,
295 * @covers ParserOutput::addLanguageLink
297 public function testUpdate_langlinks() {
298 $this->setMwGlobals( [
299 'wgCapitalLinks' => true,
302 /** @var ParserOutput $po */
303 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self
::$testingPageId );
305 $po->addLanguageLink( Title
::newFromText( "en:Foo" )->getFullText() );
307 $this->assertLinksUpdate(
312 'll_from = ' . self
::$testingPageId,
318 * @covers ParserOutput::setProperty
320 public function testUpdate_page_props() {
321 global $wgPagePropsHaveSortkey;
323 /** @var ParserOutput $po */
324 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self
::$testingPageId );
326 $fields = [ 'pp_propname', 'pp_value' ];
329 $po->setProperty( "bool", true );
330 $expected[] = [ "bool", true ];
332 $po->setProperty( "float", 4.0 +
1.0 / 4.0 );
333 $expected[] = [ "float", 4.0 +
1.0 / 4.0 ];
335 $po->setProperty( "int", -7 );
336 $expected[] = [ "int", -7 ];
338 $po->setProperty( "string", "33 bar" );
339 $expected[] = [ "string", "33 bar" ];
341 // compute expected sortkey values
342 if ( $wgPagePropsHaveSortkey ) {
343 $fields[] = 'pp_sortkey';
345 foreach ( $expected as &$row ) {
348 if ( is_int( $value ) ||
is_float( $value ) ||
is_bool( $value ) ) {
349 $row[] = floatval( $value );
356 $this->assertLinksUpdate(
357 $t, $po, 'page_props', $fields, 'pp_page = ' . self
::$testingPageId, $expected );
360 public function testUpdate_page_props_without_sortkey() {
361 $this->setMwGlobals( 'wgPagePropsHaveSortkey', false );
363 $this->testUpdate_page_props();
366 // @todo test recursive, too!
368 protected function assertLinksUpdate( Title
$title, ParserOutput
$parserOutput,
369 $table, $fields, $condition, array $expectedRows
371 $update = new LinksUpdate( $title, $parserOutput );
375 $this->assertSelect( $table, $fields, $condition, $expectedRows );
379 protected function assertRecentChangeByCategorization(
380 Title
$pageTitle, ParserOutput
$parserOutput, Title
$categoryTitle, $expectedRows
382 global $wgCommentTableSchemaMigrationStage;
384 if ( $wgCommentTableSchemaMigrationStage <= MIGRATION_WRITE_BOTH
) {
387 'rc_title, rc_comment',
389 'rc_type' => RC_CATEGORIZE
,
390 'rc_namespace' => NS_CATEGORY
,
391 'rc_title' => $categoryTitle->getDBkey()
396 if ( $wgCommentTableSchemaMigrationStage >= MIGRATION_WRITE_BOTH
) {
398 [ 'recentchanges', 'comment' ],
399 'rc_title, comment_text',
401 'rc_type' => RC_CATEGORIZE
,
402 'rc_namespace' => NS_CATEGORY
,
403 'rc_title' => $categoryTitle->getDBkey(),
404 'comment_id = rc_comment_id',
411 private function runAllRelatedJobs() {
412 $queueGroup = JobQueueGroup
::singleton();
413 while ( $job = $queueGroup->pop( 'refreshLinksPrioritized' ) ) {
415 $queueGroup->ack( $job );
417 while ( $job = $queueGroup->pop( 'categoryMembershipChange' ) ) {
419 $queueGroup->ack( $job );