Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / tests / phpunit / includes / deferred / LinksUpdateTest.php
blob9cc3ffdab78b84272f0846f0c693b73c0e57fb89
1 <?php
3 /**
4 * @group LinksUpdate
5 * @group Database
6 * ^--- make sure temporary tables are used.
7 */
8 class LinksUpdateTest extends MediaWikiLangTestCase {
9 protected static $testingPageId;
11 function __construct( $name = null, array $data = [], $dataName = '' ) {
12 parent::__construct( $name, $data, $dataName );
14 $this->tablesUsed = array_merge( $this->tablesUsed,
16 'interwiki',
17 'page_props',
18 'pagelinks',
19 'categorylinks',
20 'langlinks',
21 'externallinks',
22 'imagelinks',
23 'templatelinks',
24 'iwlinks',
25 'recentchanges',
30 protected function setUp() {
31 parent::setUp();
32 $dbw = wfGetDB( DB_MASTER );
33 $dbw->replace(
34 'interwiki',
35 [ 'iw_prefix' ],
37 'iw_prefix' => 'linksupdatetest',
38 'iw_url' => 'http://testing.com/wiki/$1',
39 'iw_api' => 'http://testing.com/w/api.php',
40 'iw_local' => 0,
41 'iw_trans' => 0,
42 'iw_wikiid' => 'linksupdatetest',
45 $this->setMwGlobals( 'wgRCWatchCategoryMembership', true );
48 public function addDBDataOnce() {
49 $res = $this->insertPage( 'Testing' );
50 self::$testingPageId = $res['id'];
51 $this->insertPage( 'Some_other_page' );
52 $this->insertPage( 'Template:TestingTemplate' );
55 protected function makeTitleAndParserOutput( $name, $id ) {
56 $t = Title::newFromText( $name );
57 $t->mArticleID = $id; # XXX: this is fugly
59 $po = new ParserOutput();
60 $po->setTitleText( $t->getPrefixedText() );
62 return [ $t, $po ];
65 /**
66 * @covers ParserOutput::addLink
68 public function testUpdate_pagelinks() {
69 /** @var Title $t */
70 /** @var ParserOutput $po */
71 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
73 $po->addLink( Title::newFromText( "Foo" ) );
74 $po->addLink( Title::newFromText( "Special:Foo" ) ); // special namespace should be ignored
75 $po->addLink( Title::newFromText( "linksupdatetest:Foo" ) ); // interwiki link should be ignored
76 $po->addLink( Title::newFromText( "#Foo" ) ); // hash link should be ignored
78 $update = $this->assertLinksUpdate(
79 $t,
80 $po,
81 'pagelinks',
82 'pl_namespace,
83 pl_title',
84 'pl_from = ' . self::$testingPageId,
85 [ [ NS_MAIN, 'Foo' ] ]
87 $this->assertArrayEquals( [
88 Title::makeTitle( NS_MAIN, 'Foo' ), // newFromText doesn't yield the same internal state....
89 ], $update->getAddedLinks() );
91 $po = new ParserOutput();
92 $po->setTitleText( $t->getPrefixedText() );
94 $po->addLink( Title::newFromText( "Bar" ) );
95 $po->addLink( Title::newFromText( "Talk:Bar" ) );
97 $update = $this->assertLinksUpdate(
98 $t,
99 $po,
100 'pagelinks',
101 'pl_namespace,
102 pl_title',
103 'pl_from = ' . self::$testingPageId,
105 [ NS_MAIN, 'Bar' ],
106 [ NS_TALK, 'Bar' ],
109 $this->assertArrayEquals( [
110 Title::makeTitle( NS_MAIN, 'Bar' ),
111 Title::makeTitle( NS_TALK, 'Bar' ),
112 ], $update->getAddedLinks() );
113 $this->assertArrayEquals( [
114 Title::makeTitle( NS_MAIN, 'Foo' ),
115 ], $update->getRemovedLinks() );
119 * @covers ParserOutput::addExternalLink
121 public function testUpdate_externallinks() {
122 /** @var ParserOutput $po */
123 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
125 $po->addExternalLink( "http://testing.com/wiki/Foo" );
127 $this->assertLinksUpdate(
129 $po,
130 'externallinks',
131 'el_to, el_index',
132 'el_from = ' . self::$testingPageId,
134 [ 'http://testing.com/wiki/Foo', 'http://com.testing./wiki/Foo' ],
140 * @covers ParserOutput::addCategory
142 public function testUpdate_categorylinks() {
143 /** @var ParserOutput $po */
144 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
146 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
148 $po->addCategory( "Foo", "FOO" );
150 $this->assertLinksUpdate(
152 $po,
153 'categorylinks',
154 'cl_to, cl_sortkey',
155 'cl_from = ' . self::$testingPageId,
156 [ [ 'Foo', "FOO\nTESTING" ] ]
160 public function testOnAddingAndRemovingCategory_recentChangesRowIsAdded() {
161 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
163 $title = Title::newFromText( 'Testing' );
164 $wikiPage = new WikiPage( $title );
165 $wikiPage->doEditContent( new WikitextContent( '[[Category:Foo]]' ), 'added category' );
166 $this->runAllRelatedJobs();
168 $this->assertRecentChangeByCategorization(
169 $title,
170 $wikiPage->getParserOutput( new ParserOptions() ),
171 Title::newFromText( 'Category:Foo' ),
172 [ [ 'Foo', '[[:Testing]] added to category' ] ]
175 $wikiPage->doEditContent( new WikitextContent( '[[Category:Bar]]' ), 'replaced category' );
176 $this->runAllRelatedJobs();
178 $this->assertRecentChangeByCategorization(
179 $title,
180 $wikiPage->getParserOutput( new ParserOptions() ),
181 Title::newFromText( 'Category:Foo' ),
183 [ 'Foo', '[[:Testing]] added to category' ],
184 [ 'Foo', '[[:Testing]] removed from category' ],
188 $this->assertRecentChangeByCategorization(
189 $title,
190 $wikiPage->getParserOutput( new ParserOptions() ),
191 Title::newFromText( 'Category:Bar' ),
193 [ 'Bar', '[[:Testing]] added to category' ],
198 public function testOnAddingAndRemovingCategoryToTemplates_embeddingPagesAreIgnored() {
199 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
201 $templateTitle = Title::newFromText( 'Template:TestingTemplate' );
202 $templatePage = new WikiPage( $templateTitle );
204 $wikiPage = new WikiPage( Title::newFromText( 'Testing' ) );
205 $wikiPage->doEditContent( new WikitextContent( '{{TestingTemplate}}' ), 'added template' );
206 $this->runAllRelatedJobs();
208 $otherWikiPage = new WikiPage( Title::newFromText( 'Some_other_page' ) );
209 $otherWikiPage->doEditContent( new WikitextContent( '{{TestingTemplate}}' ), 'added template' );
210 $this->runAllRelatedJobs();
212 $this->assertRecentChangeByCategorization(
213 $templateTitle,
214 $templatePage->getParserOutput( new ParserOptions() ),
215 Title::newFromText( 'Baz' ),
219 $templatePage->doEditContent( new WikitextContent( '[[Category:Baz]]' ), 'added category' );
220 $this->runAllRelatedJobs();
222 $this->assertRecentChangeByCategorization(
223 $templateTitle,
224 $templatePage->getParserOutput( new ParserOptions() ),
225 Title::newFromText( 'Baz' ),
227 'Baz',
228 '[[:Template:TestingTemplate]] added to category, ' .
229 '[[Special:WhatLinksHere/Template:TestingTemplate|this page is included within other pages]]'
235 * @covers ParserOutput::addInterwikiLink
237 public function testUpdate_iwlinks() {
238 /** @var ParserOutput $po */
239 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
241 $target = Title::makeTitleSafe( NS_MAIN, "Foo", '', 'linksupdatetest' );
242 $po->addInterwikiLink( $target );
244 $this->assertLinksUpdate(
246 $po,
247 'iwlinks',
248 'iwl_prefix, iwl_title',
249 'iwl_from = ' . self::$testingPageId,
250 [ [ 'linksupdatetest', 'Foo' ] ]
255 * @covers ParserOutput::addTemplate
257 public function testUpdate_templatelinks() {
258 /** @var ParserOutput $po */
259 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
261 $po->addTemplate( Title::newFromText( "Template:Foo" ), 23, 42 );
263 $this->assertLinksUpdate(
265 $po,
266 'templatelinks',
267 'tl_namespace,
268 tl_title',
269 'tl_from = ' . self::$testingPageId,
270 [ [ NS_TEMPLATE, 'Foo' ] ]
275 * @covers ParserOutput::addImage
277 public function testUpdate_imagelinks() {
278 /** @var ParserOutput $po */
279 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
281 $po->addImage( "Foo.png" );
283 $this->assertLinksUpdate(
285 $po,
286 'imagelinks',
287 'il_to',
288 'il_from = ' . self::$testingPageId,
289 [ [ 'Foo.png' ] ]
294 * @covers ParserOutput::addLanguageLink
296 public function testUpdate_langlinks() {
297 $this->setMwGlobals( [
298 'wgCapitalLinks' => true,
299 ] );
301 /** @var ParserOutput $po */
302 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
304 $po->addLanguageLink( Title::newFromText( "en:Foo" )->getFullText() );
306 $this->assertLinksUpdate(
308 $po,
309 'langlinks',
310 'll_lang, ll_title',
311 'll_from = ' . self::$testingPageId,
312 [ [ 'En', 'Foo' ] ]
317 * @covers ParserOutput::setProperty
319 public function testUpdate_page_props() {
320 global $wgPagePropsHaveSortkey;
322 /** @var ParserOutput $po */
323 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
325 $fields = [ 'pp_propname', 'pp_value' ];
326 $expected = [];
328 $po->setProperty( "bool", true );
329 $expected[] = [ "bool", true ];
331 $po->setProperty( "float", 4.0 + 1.0 / 4.0 );
332 $expected[] = [ "float", 4.0 + 1.0 / 4.0 ];
334 $po->setProperty( "int", -7 );
335 $expected[] = [ "int", -7 ];
337 $po->setProperty( "string", "33 bar" );
338 $expected[] = [ "string", "33 bar" ];
340 // compute expected sortkey values
341 if ( $wgPagePropsHaveSortkey ) {
342 $fields[] = 'pp_sortkey';
344 foreach ( $expected as &$row ) {
345 $value = $row[1];
347 if ( is_int( $value ) || is_float( $value ) || is_bool( $value ) ) {
348 $row[] = floatval( $value );
349 } else {
350 $row[] = null;
355 $this->assertLinksUpdate(
356 $t, $po, 'page_props', $fields, 'pp_page = ' . self::$testingPageId, $expected );
359 public function testUpdate_page_props_without_sortkey() {
360 $this->setMwGlobals( 'wgPagePropsHaveSortkey', false );
362 $this->testUpdate_page_props();
365 // @todo test recursive, too!
367 protected function assertLinksUpdate( Title $title, ParserOutput $parserOutput,
368 $table, $fields, $condition, array $expectedRows
370 $update = new LinksUpdate( $title, $parserOutput );
372 $update->doUpdate();
374 $this->assertSelect( $table, $fields, $condition, $expectedRows );
375 return $update;
378 protected function assertRecentChangeByCategorization(
379 Title $pageTitle, ParserOutput $parserOutput, Title $categoryTitle, $expectedRows
381 $this->assertSelect(
382 'recentchanges',
383 'rc_title, rc_comment',
385 'rc_type' => RC_CATEGORIZE,
386 'rc_namespace' => NS_CATEGORY,
387 'rc_title' => $categoryTitle->getDBkey()
389 $expectedRows
393 private function runAllRelatedJobs() {
394 $queueGroup = JobQueueGroup::singleton();
395 while ( $job = $queueGroup->pop( 'refreshLinksPrioritized' ) ) {
396 $job->run();
397 $queueGroup->ack( $job );
399 while ( $job = $queueGroup->pop( 'categoryMembershipChange' ) ) {
400 $job->run();
401 $queueGroup->ack( $job );