Merge "Add more BacklinkJobUtils comments"
[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 MediaWikiTestCase {
9
10 function __construct( $name = null, array $data = array(), $dataName = '' ) {
11 parent::__construct( $name, $data, $dataName );
12
13 $this->tablesUsed = array_merge( $this->tablesUsed,
14 array(
15 'interwiki',
16 'page_props',
17 'pagelinks',
18 'categorylinks',
19 'langlinks',
20 'externallinks',
21 'imagelinks',
22 'templatelinks',
23 'iwlinks',
24 'recentchanges',
25 )
26 );
27 }
28
29 protected function setUp() {
30 parent::setUp();
31 $dbw = wfGetDB( DB_MASTER );
32 $dbw->replace(
33 'interwiki',
34 array( 'iw_prefix' ),
35 array(
36 'iw_prefix' => 'linksupdatetest',
37 'iw_url' => 'http://testing.com/wiki/$1',
38 'iw_api' => 'http://testing.com/w/api.php',
39 'iw_local' => 0,
40 'iw_trans' => 0,
41 'iw_wikiid' => 'linksupdatetest',
42 )
43 );
44 $this->setMwGlobals( 'wgRCWatchCategoryMembership', true );
45 }
46
47 public function addDBData() {
48 $this->insertPage( 'Testing' );
49 $this->insertPage( 'Some_other_page' );
50 $this->insertPage( 'Template:TestingTemplate' );
51 }
52
53 protected function makeTitleAndParserOutput( $name, $id ) {
54 $t = Title::newFromText( $name );
55 $t->mArticleID = $id; # XXX: this is fugly
56
57 $po = new ParserOutput();
58 $po->setTitleText( $t->getPrefixedText() );
59
60 return array( $t, $po );
61 }
62
63 /**
64 * @covers ParserOutput::addLink
65 */
66 public function testUpdate_pagelinks() {
67 /** @var ParserOutput $po */
68 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
69
70 $po->addLink( Title::newFromText( "Foo" ) );
71 $po->addLink( Title::newFromText( "Special:Foo" ) ); // special namespace should be ignored
72 $po->addLink( Title::newFromText( "linksupdatetest:Foo" ) ); // interwiki link should be ignored
73 $po->addLink( Title::newFromText( "#Foo" ) ); // hash link should be ignored
74
75 $update = $this->assertLinksUpdate(
76 $t,
77 $po,
78 'pagelinks',
79 'pl_namespace,
80 pl_title',
81 'pl_from = 111',
82 array( array( NS_MAIN, 'Foo' ) )
83 );
84 $this->assertArrayEquals( array(
85 Title::makeTitle( NS_MAIN, 'Foo' ), // newFromText doesn't yield the same internal state....
86 ), $update->getAddedLinks() );
87
88 $po = new ParserOutput();
89 $po->setTitleText( $t->getPrefixedText() );
90
91 $po->addLink( Title::newFromText( "Bar" ) );
92 $po->addLink( Title::newFromText( "Talk:Bar" ) );
93
94 $update = $this->assertLinksUpdate(
95 $t,
96 $po,
97 'pagelinks',
98 'pl_namespace,
99 pl_title',
100 'pl_from = 111',
101 array(
102 array( NS_MAIN, 'Bar' ),
103 array( NS_TALK, 'Bar' ),
104 )
105 );
106 $this->assertArrayEquals( array(
107 Title::makeTitle( NS_MAIN, 'Bar' ),
108 Title::makeTitle( NS_TALK, 'Bar' ),
109 ), $update->getAddedLinks() );
110 $this->assertArrayEquals( array(
111 Title::makeTitle( NS_MAIN, 'Foo' ),
112 ), $update->getRemovedLinks() );
113 }
114
115 /**
116 * @covers ParserOutput::addExternalLink
117 */
118 public function testUpdate_externallinks() {
119 /** @var ParserOutput $po */
120 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
121
122 $po->addExternalLink( "http://testing.com/wiki/Foo" );
123
124 $this->assertLinksUpdate( $t, $po, 'externallinks', 'el_to, el_index', 'el_from = 111', array(
125 array( 'http://testing.com/wiki/Foo', 'http://com.testing./wiki/Foo' ),
126 ) );
127 }
128
129 /**
130 * @covers ParserOutput::addCategory
131 */
132 public function testUpdate_categorylinks() {
133 /** @var ParserOutput $po */
134 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
135
136 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
137
138 $po->addCategory( "Foo", "FOO" );
139
140 $this->assertLinksUpdate( $t, $po, 'categorylinks', 'cl_to, cl_sortkey', 'cl_from = 111', array(
141 array( 'Foo', "FOO\nTESTING" ),
142 ) );
143 }
144
145 public function testOnAddingAndRemovingCategory_recentChangesRowIsAdded() {
146 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
147
148 $title = Title::newFromText( 'Testing' );
149 $wikiPage = new WikiPage( $title );
150 $wikiPage->doEditContent( new WikitextContent( '[[Category:Foo]]' ), 'added category' );
151 $this->runAllRelatedJobs();
152
153 $this->assertRecentChangeByCategorization(
154 $title,
155 $wikiPage->getParserOutput( new ParserOptions() ),
156 Title::newFromText( 'Category:Foo' ),
157 array( array( 'Foo', '[[:Testing]] added to category' ) )
158 );
159
160 $wikiPage->doEditContent( new WikitextContent( '[[Category:Bar]]' ), 'replaced category' );
161 $this->runAllRelatedJobs();
162
163 $this->assertRecentChangeByCategorization(
164 $title,
165 $wikiPage->getParserOutput( new ParserOptions() ),
166 Title::newFromText( 'Category:Foo' ),
167 array(
168 array( 'Foo', '[[:Testing]] added to category' ),
169 array( 'Foo', '[[:Testing]] removed from category' ),
170 )
171 );
172
173 $this->assertRecentChangeByCategorization(
174 $title,
175 $wikiPage->getParserOutput( new ParserOptions() ),
176 Title::newFromText( 'Category:Bar' ),
177 array(
178 array( 'Bar', '[[:Testing]] added to category' ),
179 )
180 );
181 }
182
183 public function testOnAddingAndRemovingCategoryToTemplates_embeddingPagesAreIgnored() {
184 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
185
186 $templateTitle = Title::newFromText( 'Template:TestingTemplate' );
187 $templatePage = new WikiPage( $templateTitle );
188
189 $wikiPage = new WikiPage( Title::newFromText( 'Testing' ) );
190 $wikiPage->doEditContent( new WikitextContent( '{{TestingTemplate}}' ), 'added template' );
191 $this->runAllRelatedJobs();
192
193 $otherWikiPage = new WikiPage( Title::newFromText( 'Some_other_page' ) );
194 $otherWikiPage->doEditContent( new WikitextContent( '{{TestingTemplate}}' ), 'added template' );
195 $this->runAllRelatedJobs();
196
197 $this->assertRecentChangeByCategorization(
198 $templateTitle,
199 $templatePage->getParserOutput( new ParserOptions() ),
200 Title::newFromText( 'Baz' ),
201 array()
202 );
203
204 $templatePage->doEditContent( new WikitextContent( '[[Category:Baz]]' ), 'added category' );
205 $this->runAllRelatedJobs();
206
207 $this->assertRecentChangeByCategorization(
208 $templateTitle,
209 $templatePage->getParserOutput( new ParserOptions() ),
210 Title::newFromText( 'Baz' ),
211 array( array( 'Baz', '[[:Template:TestingTemplate]] and 2 pages added to category' ) )
212 );
213 }
214
215 /**
216 * @covers ParserOutput::addInterwikiLink
217 */
218 public function testUpdate_iwlinks() {
219 /** @var ParserOutput $po */
220 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
221
222 $target = Title::makeTitleSafe( NS_MAIN, "Foo", '', 'linksupdatetest' );
223 $po->addInterwikiLink( $target );
224
225 $this->assertLinksUpdate( $t, $po, 'iwlinks', 'iwl_prefix, iwl_title', 'iwl_from = 111', array(
226 array( 'linksupdatetest', 'Foo' ),
227 ) );
228 }
229
230 /**
231 * @covers ParserOutput::addTemplate
232 */
233 public function testUpdate_templatelinks() {
234 /** @var ParserOutput $po */
235 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
236
237 $po->addTemplate( Title::newFromText( "Template:Foo" ), 23, 42 );
238
239 $this->assertLinksUpdate(
240 $t,
241 $po,
242 'templatelinks',
243 'tl_namespace,
244 tl_title',
245 'tl_from = 111',
246 array( array( NS_TEMPLATE, 'Foo' ) )
247 );
248 }
249
250 /**
251 * @covers ParserOutput::addImage
252 */
253 public function testUpdate_imagelinks() {
254 /** @var ParserOutput $po */
255 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
256
257 $po->addImage( "Foo.png" );
258
259 $this->assertLinksUpdate( $t, $po, 'imagelinks', 'il_to', 'il_from = 111', array(
260 array( 'Foo.png' ),
261 ) );
262 }
263
264 /**
265 * @covers ParserOutput::addLanguageLink
266 */
267 public function testUpdate_langlinks() {
268 $this->setMwGlobals( array(
269 'wgCapitalLinks' => true,
270 ) );
271
272 /** @var ParserOutput $po */
273 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
274
275 $po->addLanguageLink( Title::newFromText( "en:Foo" )->getFullText() );
276
277 $this->assertLinksUpdate( $t, $po, 'langlinks', 'll_lang, ll_title', 'll_from = 111', array(
278 array( 'En', 'Foo' ),
279 ) );
280 }
281
282 /**
283 * @covers ParserOutput::setProperty
284 */
285 public function testUpdate_page_props() {
286 global $wgPagePropsHaveSortkey;
287
288 /** @var ParserOutput $po */
289 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
290
291 $fields = array( 'pp_propname', 'pp_value' );
292 $expected = array();
293
294 $po->setProperty( "bool", true );
295 $expected[] = array( "bool", true );
296
297 $po->setProperty( "float", 4.0 + 1.0 / 4.0 );
298 $expected[] = array( "float", 4.0 + 1.0 / 4.0 );
299
300 $po->setProperty( "int", -7 );
301 $expected[] = array( "int", -7 );
302
303 $po->setProperty( "string", "33 bar" );
304 $expected[] = array( "string", "33 bar" );
305
306 // compute expected sortkey values
307 if ( $wgPagePropsHaveSortkey ) {
308 $fields[] = 'pp_sortkey';
309
310 foreach ( $expected as &$row ) {
311 $value = $row[1];
312
313 if ( is_int( $value ) || is_float( $value ) || is_bool( $value ) ) {
314 $row[] = floatval( $value );
315 } else {
316 $row[] = null;
317 }
318 }
319 }
320
321 $this->assertLinksUpdate( $t, $po, 'page_props', $fields, 'pp_page = 111', $expected );
322 }
323
324 public function testUpdate_page_props_without_sortkey() {
325 $this->setMwGlobals( 'wgPagePropsHaveSortkey', false );
326
327 $this->testUpdate_page_props();
328 }
329
330 // @todo test recursive, too!
331
332 protected function assertLinksUpdate( Title $title, ParserOutput $parserOutput,
333 $table, $fields, $condition, array $expectedRows
334 ) {
335 $update = new LinksUpdate( $title, $parserOutput );
336
337 // NOTE: make sure LinksUpdate does not generate warnings when called inside a transaction.
338 $update->beginTransaction();
339 $update->doUpdate();
340 $update->commitTransaction();
341
342 $this->assertSelect( $table, $fields, $condition, $expectedRows );
343 return $update;
344 }
345
346 protected function assertRecentChangeByCategorization(
347 Title $pageTitle, ParserOutput $parserOutput, Title $categoryTitle, $expectedRows
348 ) {
349 $this->assertSelect(
350 'recentchanges',
351 'rc_title, rc_comment',
352 array(
353 'rc_type' => RC_CATEGORIZE,
354 'rc_namespace' => NS_CATEGORY,
355 'rc_title' => $categoryTitle->getDBkey()
356 ),
357 $expectedRows
358 );
359 }
360
361 private function runAllRelatedJobs() {
362 $queueGroup = JobQueueGroup::singleton();
363 while ( $job = $queueGroup->pop( 'refreshLinksPrioritized' ) ) {
364 $job->run();
365 $queueGroup->ack( $job );
366 }
367 while ( $job = $queueGroup->pop( 'categoryMembershipChange' ) ) {
368 $job->run();
369 $queueGroup->ack( $job );
370 }
371 }
372 }