Merge "Set Language in SpecialSearchTest.php"
[lhc/web/wiklou.git] / tests / phpunit / includes / deferred / LinksUpdateTest.php
1 <?php
2
3 /**
4 * @group LinksUpdate
5 * @group Database
6 * ^--- make sure temporary tables are used.
7 */
8 class LinksUpdateTest extends MediaWikiLangTestCase {
9 protected $testingPageId;
10
11 function __construct( $name = null, array $data = array(), $dataName = '' ) {
12 parent::__construct( $name, $data, $dataName );
13
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',
26 )
27 );
28 }
29
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',
43 )
44 );
45 $this->setMwGlobals( 'wgRCWatchCategoryMembership', true );
46 }
47
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' );
53 }
54
55 protected function makeTitleAndParserOutput( $name, $id ) {
56 $t = Title::newFromText( $name );
57 $t->mArticleID = $id; # XXX: this is fugly
58
59 $po = new ParserOutput();
60 $po->setTitleText( $t->getPrefixedText() );
61
62 return array( $t, $po );
63 }
64
65 /**
66 * @covers ParserOutput::addLink
67 */
68 public function testUpdate_pagelinks() {
69 /** @var Title $t */
70 /** @var ParserOutput $po */
71 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", $this->testingPageId );
72
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
77
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' ) )
86 );
87 $this->assertArrayEquals( array(
88 Title::makeTitle( NS_MAIN, 'Foo' ), // newFromText doesn't yield the same internal state....
89 ), $update->getAddedLinks() );
90
91 $po = new ParserOutput();
92 $po->setTitleText( $t->getPrefixedText() );
93
94 $po->addLink( Title::newFromText( "Bar" ) );
95 $po->addLink( Title::newFromText( "Talk:Bar" ) );
96
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' ),
107 )
108 );
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() );
116 }
117
118 /**
119 * @covers ParserOutput::addExternalLink
120 */
121 public function testUpdate_externallinks() {
122 /** @var ParserOutput $po */
123 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", $this->testingPageId );
124
125 $po->addExternalLink( "http://testing.com/wiki/Foo" );
126
127 $this->assertLinksUpdate(
128 $t,
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' ),
135 )
136 );
137 }
138
139 /**
140 * @covers ParserOutput::addCategory
141 */
142 public function testUpdate_categorylinks() {
143 /** @var ParserOutput $po */
144 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
145
146 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", $this->testingPageId );
147
148 $po->addCategory( "Foo", "FOO" );
149
150 $this->assertLinksUpdate(
151 $t,
152 $po,
153 'categorylinks',
154 'cl_to, cl_sortkey',
155 'cl_from = ' . $this->testingPageId,
156 array( array( 'Foo', "FOO\nTESTING" ) )
157 );
158 }
159
160 public function testOnAddingAndRemovingCategory_recentChangesRowIsAdded() {
161 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
162
163 $title = Title::newFromText( 'Testing' );
164 $wikiPage = new WikiPage( $title );
165 $wikiPage->doEditContent( new WikitextContent( '[[Category:Foo]]' ), 'added category' );
166 $this->runAllRelatedJobs();
167
168 $this->assertRecentChangeByCategorization(
169 $title,
170 $wikiPage->getParserOutput( new ParserOptions() ),
171 Title::newFromText( 'Category:Foo' ),
172 array( array( 'Foo', '[[:Testing]] added to category' ) )
173 );
174
175 $wikiPage->doEditContent( new WikitextContent( '[[Category:Bar]]' ), 'replaced category' );
176 $this->runAllRelatedJobs();
177
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' ),
185 )
186 );
187
188 $this->assertRecentChangeByCategorization(
189 $title,
190 $wikiPage->getParserOutput( new ParserOptions() ),
191 Title::newFromText( 'Category:Bar' ),
192 array(
193 array( 'Bar', '[[:Testing]] added to category' ),
194 )
195 );
196 }
197
198 public function testOnAddingAndRemovingCategoryToTemplates_embeddingPagesAreIgnored() {
199 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
200
201 $templateTitle = Title::newFromText( 'Template:TestingTemplate' );
202 $templatePage = new WikiPage( $templateTitle );
203
204 $wikiPage = new WikiPage( Title::newFromText( 'Testing' ) );
205 $wikiPage->doEditContent( new WikitextContent( '{{TestingTemplate}}' ), 'added template' );
206 $this->runAllRelatedJobs();
207
208 $otherWikiPage = new WikiPage( Title::newFromText( 'Some_other_page' ) );
209 $otherWikiPage->doEditContent( new WikitextContent( '{{TestingTemplate}}' ), 'added template' );
210 $this->runAllRelatedJobs();
211
212 $this->assertRecentChangeByCategorization(
213 $templateTitle,
214 $templatePage->getParserOutput( new ParserOptions() ),
215 Title::newFromText( 'Baz' ),
216 array()
217 );
218
219 $templatePage->doEditContent( new WikitextContent( '[[Category:Baz]]' ), 'added category' );
220 $this->runAllRelatedJobs();
221
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' ) )
227 );
228 }
229
230 /**
231 * @covers ParserOutput::addInterwikiLink
232 */
233 public function testUpdate_iwlinks() {
234 /** @var ParserOutput $po */
235 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", $this->testingPageId );
236
237 $target = Title::makeTitleSafe( NS_MAIN, "Foo", '', 'linksupdatetest' );
238 $po->addInterwikiLink( $target );
239
240 $this->assertLinksUpdate(
241 $t,
242 $po,
243 'iwlinks',
244 'iwl_prefix, iwl_title',
245 'iwl_from = ' . $this->testingPageId,
246 array( array( 'linksupdatetest', 'Foo' ) )
247 );
248 }
249
250 /**
251 * @covers ParserOutput::addTemplate
252 */
253 public function testUpdate_templatelinks() {
254 /** @var ParserOutput $po */
255 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", $this->testingPageId );
256
257 $po->addTemplate( Title::newFromText( "Template:Foo" ), 23, 42 );
258
259 $this->assertLinksUpdate(
260 $t,
261 $po,
262 'templatelinks',
263 'tl_namespace,
264 tl_title',
265 'tl_from = ' . $this->testingPageId,
266 array( array( NS_TEMPLATE, 'Foo' ) )
267 );
268 }
269
270 /**
271 * @covers ParserOutput::addImage
272 */
273 public function testUpdate_imagelinks() {
274 /** @var ParserOutput $po */
275 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", $this->testingPageId );
276
277 $po->addImage( "Foo.png" );
278
279 $this->assertLinksUpdate(
280 $t,
281 $po,
282 'imagelinks',
283 'il_to',
284 'il_from = ' . $this->testingPageId,
285 array( array( 'Foo.png' ) )
286 );
287 }
288
289 /**
290 * @covers ParserOutput::addLanguageLink
291 */
292 public function testUpdate_langlinks() {
293 $this->setMwGlobals( array(
294 'wgCapitalLinks' => true,
295 ) );
296
297 /** @var ParserOutput $po */
298 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", $this->testingPageId );
299
300 $po->addLanguageLink( Title::newFromText( "en:Foo" )->getFullText() );
301
302 $this->assertLinksUpdate(
303 $t,
304 $po,
305 'langlinks',
306 'll_lang, ll_title',
307 'll_from = ' . $this->testingPageId,
308 array( array( 'En', 'Foo' ) )
309 );
310 }
311
312 /**
313 * @covers ParserOutput::setProperty
314 */
315 public function testUpdate_page_props() {
316 global $wgPagePropsHaveSortkey;
317
318 /** @var ParserOutput $po */
319 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", $this->testingPageId );
320
321 $fields = array( 'pp_propname', 'pp_value' );
322 $expected = array();
323
324 $po->setProperty( "bool", true );
325 $expected[] = array( "bool", true );
326
327 $po->setProperty( "float", 4.0 + 1.0 / 4.0 );
328 $expected[] = array( "float", 4.0 + 1.0 / 4.0 );
329
330 $po->setProperty( "int", -7 );
331 $expected[] = array( "int", -7 );
332
333 $po->setProperty( "string", "33 bar" );
334 $expected[] = array( "string", "33 bar" );
335
336 // compute expected sortkey values
337 if ( $wgPagePropsHaveSortkey ) {
338 $fields[] = 'pp_sortkey';
339
340 foreach ( $expected as &$row ) {
341 $value = $row[1];
342
343 if ( is_int( $value ) || is_float( $value ) || is_bool( $value ) ) {
344 $row[] = floatval( $value );
345 } else {
346 $row[] = null;
347 }
348 }
349 }
350
351 $this->assertLinksUpdate(
352 $t, $po, 'page_props', $fields, 'pp_page = ' . $this->testingPageId, $expected );
353 }
354
355 public function testUpdate_page_props_without_sortkey() {
356 $this->setMwGlobals( 'wgPagePropsHaveSortkey', false );
357
358 $this->testUpdate_page_props();
359 }
360
361 // @todo test recursive, too!
362
363 protected function assertLinksUpdate( Title $title, ParserOutput $parserOutput,
364 $table, $fields, $condition, array $expectedRows
365 ) {
366 $update = new LinksUpdate( $title, $parserOutput );
367
368 // NOTE: make sure LinksUpdate does not generate warnings when called inside a transaction.
369 $update->beginTransaction();
370 $update->doUpdate();
371 $update->commitTransaction();
372
373 $this->assertSelect( $table, $fields, $condition, $expectedRows );
374 return $update;
375 }
376
377 protected function assertRecentChangeByCategorization(
378 Title $pageTitle, ParserOutput $parserOutput, Title $categoryTitle, $expectedRows
379 ) {
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()
387 ),
388 $expectedRows
389 );
390 }
391
392 private function runAllRelatedJobs() {
393 $queueGroup = JobQueueGroup::singleton();
394 while ( $job = $queueGroup->pop( 'refreshLinksPrioritized' ) ) {
395 $job->run();
396 $queueGroup->ack( $job );
397 }
398 while ( $job = $queueGroup->pop( 'categoryMembershipChange' ) ) {
399 $job->run();
400 $queueGroup->ack( $job );
401 }
402 }
403 }