Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / phpunit / includes / deferred / LinksUpdateTest.php
blob016a7aaed81feeda91ddf8a5a1ddf1f9379ce62c
1 <?php
3 /**
4 * @group LinksUpdate
5 * @group Database
6 * ^--- make sure temporary tables are used.
7 */
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,
15 array(
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 array( 'iw_prefix' ),
36 array(
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 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 );
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", $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(
79 $t,
80 $po,
81 'pagelinks',
82 'pl_namespace,
83 pl_title',
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(
98 $t,
99 $po,
100 'pagelinks',
101 'pl_namespace,
102 pl_title',
103 'pl_from = ' . $this->testingPageId,
104 array(
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(
129 $po,
130 'externallinks',
131 'el_to, el_index',
132 'el_from = ' . $this->testingPageId,
133 array(
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(
152 $po,
153 'categorylinks',
154 'cl_to, cl_sortkey',
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(
169 $title,
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(
179 $title,
180 $wikiPage->getParserOutput( new ParserOptions() ),
181 Title::newFromText( 'Category:Foo' ),
182 array(
183 array( 'Foo', '[[:Testing]] added to category' ),
184 array( 'Foo', '[[:Testing]] removed from category' ),
188 $this->assertRecentChangeByCategorization(
189 $title,
190 $wikiPage->getParserOutput( new ParserOptions() ),
191 Title::newFromText( 'Category:Bar' ),
192 array(
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(
213 $templateTitle,
214 $templatePage->getParserOutput( new ParserOptions() ),
215 Title::newFromText( 'Baz' ),
216 array()
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' ),
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(
242 $po,
243 'iwlinks',
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(
261 $po,
262 'templatelinks',
263 'tl_namespace,
264 tl_title',
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(
281 $po,
282 'imagelinks',
283 'il_to',
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,
295 ) );
297 /** @var ParserOutput $po */
298 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", $this->testingPageId );
300 $po->addLanguageLink( Title::newFromText( "en:Foo" )->getFullText() );
302 $this->assertLinksUpdate(
304 $po,
305 'langlinks',
306 'll_lang, ll_title',
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' );
322 $expected = array();
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 ) {
341 $value = $row[1];
343 if ( is_int( $value ) || is_float( $value ) || is_bool( $value ) ) {
344 $row[] = floatval( $value );
345 } else {
346 $row[] = null;
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();
370 $update->doUpdate();
371 $update->commitTransaction();
373 $this->assertSelect( $table, $fields, $condition, $expectedRows );
374 return $update;
377 protected function assertRecentChangeByCategorization(
378 Title $pageTitle, ParserOutput $parserOutput, Title $categoryTitle, $expectedRows
380 $this->assertSelect(
381 'recentchanges',
382 'rc_title, rc_comment',
383 array(
384 'rc_type' => RC_CATEGORIZE,
385 'rc_namespace' => NS_CATEGORY,
386 'rc_title' => $categoryTitle->getDBkey()
388 $expectedRows
392 private function runAllRelatedJobs() {
393 $queueGroup = JobQueueGroup::singleton();
394 while ( $job = $queueGroup->pop( 'refreshLinksPrioritized' ) ) {
395 $job->run();
396 $queueGroup->ack( $job );
398 while ( $job = $queueGroup->pop( 'categoryMembershipChange' ) ) {
399 $job->run();
400 $queueGroup->ack( $job );