6 * ^--- make sure temporary tables are used.
8 class LinksUpdateTest
extends MediaWikiLangTestCase
{
9 protected $testingPageId;
11 function __construct( $name = null, array $data = array(), $dataName = '' ) {
12 parent
::__construct( $name, $data, $dataName );
14 $this->tablesUsed
= array_merge( $this->tablesUsed
,
30 protected function setUp() {
32 $dbw = wfGetDB( DB_MASTER
);
37 'iw_prefix' => 'linksupdatetest',
38 'iw_url' => 'http://testing.com/wiki/$1',
39 'iw_api' => 'http://testing.com/w/api.php',
42 'iw_wikiid' => 'linksupdatetest',
45 $this->setMwGlobals( 'wgRCWatchCategoryMembership', true );
48 public function addDBData() {
49 $res = $this->insertPage( 'Testing' );
50 $this->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 array( $t, $po );
66 * @covers ParserOutput::addLink
68 public function testUpdate_pagelinks() {
70 /** @var ParserOutput $po */
71 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", $this->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(
84 'pl_from = ' . $this->testingPageId
,
85 array( array( NS_MAIN
, 'Foo' ) )
87 $this->assertArrayEquals( array(
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(
103 'pl_from = ' . $this->testingPageId
,
105 array( NS_MAIN
, 'Bar' ),
106 array( NS_TALK
, 'Bar' ),
109 $this->assertArrayEquals( array(
110 Title
::makeTitle( NS_MAIN
, 'Bar' ),
111 Title
::makeTitle( NS_TALK
, 'Bar' ),
112 ), $update->getAddedLinks() );
113 $this->assertArrayEquals( array(
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", $this->testingPageId
);
125 $po->addExternalLink( "http://testing.com/wiki/Foo" );
127 $this->assertLinksUpdate(
132 'el_from = ' . $this->testingPageId
,
134 array( '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", $this->testingPageId
);
148 $po->addCategory( "Foo", "FOO" );
150 $this->assertLinksUpdate(
155 'cl_from = ' . $this->testingPageId
,
156 array( array( '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(
170 $wikiPage->getParserOutput( new ParserOptions() ),
171 Title
::newFromText( 'Category:Foo' ),
172 array( array( 'Foo', '[[:Testing]] added to category' ) )
175 $wikiPage->doEditContent( new WikitextContent( '[[Category:Bar]]' ), 'replaced category' );
176 $this->runAllRelatedJobs();
178 $this->assertRecentChangeByCategorization(
180 $wikiPage->getParserOutput( new ParserOptions() ),
181 Title
::newFromText( 'Category:Foo' ),
183 array( 'Foo', '[[:Testing]] added to category' ),
184 array( 'Foo', '[[:Testing]] removed from category' ),
188 $this->assertRecentChangeByCategorization(
190 $wikiPage->getParserOutput( new ParserOptions() ),
191 Title
::newFromText( 'Category:Bar' ),
193 array( '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(
214 $templatePage->getParserOutput( new ParserOptions() ),
215 Title
::newFromText( 'Baz' ),
219 $templatePage->doEditContent( new WikitextContent( '[[Category:Baz]]' ), 'added category' );
220 $this->runAllRelatedJobs();
222 $this->assertRecentChangeByCategorization(
224 $templatePage->getParserOutput( new ParserOptions() ),
225 Title
::newFromText( 'Baz' ),
226 array( array( 'Baz', '[[:Template:TestingTemplate]] and 2 pages added to category' ) )
231 * @covers ParserOutput::addInterwikiLink
233 public function testUpdate_iwlinks() {
234 /** @var ParserOutput $po */
235 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", $this->testingPageId
);
237 $target = Title
::makeTitleSafe( NS_MAIN
, "Foo", '', 'linksupdatetest' );
238 $po->addInterwikiLink( $target );
240 $this->assertLinksUpdate(
244 'iwl_prefix, iwl_title',
245 'iwl_from = ' . $this->testingPageId
,
246 array( array( 'linksupdatetest', 'Foo' ) )
251 * @covers ParserOutput::addTemplate
253 public function testUpdate_templatelinks() {
254 /** @var ParserOutput $po */
255 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", $this->testingPageId
);
257 $po->addTemplate( Title
::newFromText( "Template:Foo" ), 23, 42 );
259 $this->assertLinksUpdate(
265 'tl_from = ' . $this->testingPageId
,
266 array( array( NS_TEMPLATE
, 'Foo' ) )
271 * @covers ParserOutput::addImage
273 public function testUpdate_imagelinks() {
274 /** @var ParserOutput $po */
275 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", $this->testingPageId
);
277 $po->addImage( "Foo.png" );
279 $this->assertLinksUpdate(
284 'il_from = ' . $this->testingPageId
,
285 array( array( 'Foo.png' ) )
290 * @covers ParserOutput::addLanguageLink
292 public function testUpdate_langlinks() {
293 $this->setMwGlobals( array(
294 'wgCapitalLinks' => true,
297 /** @var ParserOutput $po */
298 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", $this->testingPageId
);
300 $po->addLanguageLink( Title
::newFromText( "en:Foo" )->getFullText() );
302 $this->assertLinksUpdate(
307 'll_from = ' . $this->testingPageId
,
308 array( array( 'En', 'Foo' ) )
313 * @covers ParserOutput::setProperty
315 public function testUpdate_page_props() {
316 global $wgPagePropsHaveSortkey;
318 /** @var ParserOutput $po */
319 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", $this->testingPageId
);
321 $fields = array( 'pp_propname', 'pp_value' );
324 $po->setProperty( "bool", true );
325 $expected[] = array( "bool", true );
327 $po->setProperty( "float", 4.0 +
1.0 / 4.0 );
328 $expected[] = array( "float", 4.0 +
1.0 / 4.0 );
330 $po->setProperty( "int", -7 );
331 $expected[] = array( "int", -7 );
333 $po->setProperty( "string", "33 bar" );
334 $expected[] = array( "string", "33 bar" );
336 // compute expected sortkey values
337 if ( $wgPagePropsHaveSortkey ) {
338 $fields[] = 'pp_sortkey';
340 foreach ( $expected as &$row ) {
343 if ( is_int( $value ) ||
is_float( $value ) ||
is_bool( $value ) ) {
344 $row[] = floatval( $value );
351 $this->assertLinksUpdate(
352 $t, $po, 'page_props', $fields, 'pp_page = ' . $this->testingPageId
, $expected );
355 public function testUpdate_page_props_without_sortkey() {
356 $this->setMwGlobals( 'wgPagePropsHaveSortkey', false );
358 $this->testUpdate_page_props();
361 // @todo test recursive, too!
363 protected function assertLinksUpdate( Title
$title, ParserOutput
$parserOutput,
364 $table, $fields, $condition, array $expectedRows
366 $update = new LinksUpdate( $title, $parserOutput );
368 // NOTE: make sure LinksUpdate does not generate warnings when called inside a transaction.
369 $update->beginTransaction();
371 $update->commitTransaction();
373 $this->assertSelect( $table, $fields, $condition, $expectedRows );
377 protected function assertRecentChangeByCategorization(
378 Title
$pageTitle, ParserOutput
$parserOutput, Title
$categoryTitle, $expectedRows
382 'rc_title, rc_comment',
384 'rc_type' => RC_CATEGORIZE
,
385 'rc_namespace' => NS_CATEGORY
,
386 'rc_title' => $categoryTitle->getDBkey()
392 private function runAllRelatedJobs() {
393 $queueGroup = JobQueueGroup
::singleton();
394 while ( $job = $queueGroup->pop( 'refreshLinksPrioritized' ) ) {
396 $queueGroup->ack( $job );
398 while ( $job = $queueGroup->pop( 'categoryMembershipChange' ) ) {
400 $queueGroup->ack( $job );