0e209d533da26535a102180c11e855edd1e91a2f
[lhc/web/wiklou.git] / tests / phpunit / includes / changetags / ChangeTagsTest.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4
5 /**
6 * @covers ChangeTags
7 * @group Database
8 */
9 class ChangeTagsTest extends MediaWikiTestCase {
10
11 public function setUp() {
12 parent::setUp();
13
14 $this->tablesUsed[] = 'change_tag';
15 $this->tablesUsed[] = 'change_tag_def';
16
17 // Truncate these to avoid the supposed-to-be-unused IDs in tests here turning
18 // out to be used, leading ChangeTags::updateTags() to pick up bogus rc_id,
19 // log_id, or rev_id values and run into unique constraint violations.
20 $this->tablesUsed[] = 'recentchanges';
21 $this->tablesUsed[] = 'logging';
22 $this->tablesUsed[] = 'revision';
23 $this->tablesUsed[] = 'archive';
24 }
25
26 // TODO only modifyDisplayQuery and getSoftwareTags are tested, nothing else is
27
28 /** @dataProvider provideModifyDisplayQuery */
29 public function testModifyDisplayQuery( $origQuery, $filter_tag, $useTags, $modifiedQuery ) {
30 $this->setMwGlobals( 'wgUseTagFilter', $useTags );
31 $rcId = 123;
32 ChangeTags::updateTags( [ 'foo', 'bar' ], [], $rcId );
33 // HACK resolve deferred group concats (see comment in provideModifyDisplayQuery)
34 if ( isset( $modifiedQuery['fields']['ts_tags'] ) ) {
35 $modifiedQuery['fields']['ts_tags'] = call_user_func_array(
36 [ wfGetDB( DB_REPLICA ), 'buildGroupConcatField' ],
37 $modifiedQuery['fields']['ts_tags']
38 );
39 }
40 if ( isset( $modifiedQuery['exception'] ) ) {
41 $this->setExpectedException( $modifiedQuery['exception'] );
42 }
43 ChangeTags::modifyDisplayQuery(
44 $origQuery['tables'],
45 $origQuery['fields'],
46 $origQuery['conds'],
47 $origQuery['join_conds'],
48 $origQuery['options'],
49 $filter_tag
50 );
51 if ( !isset( $modifiedQuery['exception'] ) ) {
52 $this->assertArrayEquals(
53 $modifiedQuery,
54 $origQuery,
55 /* ordered = */ false,
56 /* named = */ true
57 );
58 }
59 }
60
61 public function provideModifyDisplayQuery() {
62 // HACK if we call $dbr->buildGroupConcatField() now, it will return the wrong table names
63 // We have to have the test runner call it instead
64 $baseConcats = [ ',', [ 'change_tag', 'change_tag_def' ], 'ctd_name' ];
65 $joinConds = [ 'change_tag_def' => [ 'JOIN', 'ct_tag_id=ctd_id' ] ];
66 $groupConcats = [
67 'recentchanges' => array_merge( $baseConcats, [ 'ct_rc_id=rc_id', $joinConds ] ),
68 'logging' => array_merge( $baseConcats, [ 'ct_log_id=log_id', $joinConds ] ),
69 'revision' => array_merge( $baseConcats, [ 'ct_rev_id=rev_id', $joinConds ] ),
70 'archive' => array_merge( $baseConcats, [ 'ct_rev_id=ar_rev_id', $joinConds ] ),
71 ];
72
73 return [
74 'simple recentchanges query' => [
75 [
76 'tables' => [ 'recentchanges' ],
77 'fields' => [ 'rc_id', 'rc_timestamp' ],
78 'conds' => [ "rc_timestamp > '20170714183203'" ],
79 'join_conds' => [],
80 'options' => [ 'ORDER BY' => 'rc_timestamp DESC' ],
81 ],
82 '', // no tag filter
83 true, // tag filtering enabled
84 [
85 'tables' => [ 'recentchanges' ],
86 'fields' => [ 'rc_id', 'rc_timestamp', 'ts_tags' => $groupConcats['recentchanges'] ],
87 'conds' => [ "rc_timestamp > '20170714183203'" ],
88 'join_conds' => [],
89 'options' => [ 'ORDER BY' => 'rc_timestamp DESC' ],
90 ]
91 ],
92 'simple query with strings' => [
93 [
94 'tables' => 'recentchanges',
95 'fields' => 'rc_id',
96 'conds' => "rc_timestamp > '20170714183203'",
97 'join_conds' => [],
98 'options' => 'ORDER BY rc_timestamp DESC',
99 ],
100 '', // no tag filter
101 true, // tag filtering enabled
102 [
103 'tables' => [ 'recentchanges' ],
104 'fields' => [ 'rc_id', 'ts_tags' => $groupConcats['recentchanges'] ],
105 'conds' => [ "rc_timestamp > '20170714183203'" ],
106 'join_conds' => [],
107 'options' => [ 'ORDER BY rc_timestamp DESC' ],
108 ]
109 ],
110 'recentchanges query with single tag filter' => [
111 [
112 'tables' => [ 'recentchanges' ],
113 'fields' => [ 'rc_id', 'rc_timestamp' ],
114 'conds' => [ "rc_timestamp > '20170714183203'" ],
115 'join_conds' => [],
116 'options' => [ 'ORDER BY' => 'rc_timestamp DESC' ],
117 ],
118 'foo',
119 true, // tag filtering enabled
120 [
121 'tables' => [ 'recentchanges', 'change_tag' ],
122 'fields' => [ 'rc_id', 'rc_timestamp', 'ts_tags' => $groupConcats['recentchanges'] ],
123 'conds' => [ "rc_timestamp > '20170714183203'", 'ct_tag_id' => [ 1 ] ],
124 'join_conds' => [ 'change_tag' => [ 'JOIN', 'ct_rc_id=rc_id' ] ],
125 'options' => [ 'ORDER BY' => 'rc_timestamp DESC' ],
126 ]
127 ],
128 'logging query with single tag filter and strings' => [
129 [
130 'tables' => 'logging',
131 'fields' => 'log_id',
132 'conds' => "log_timestamp > '20170714183203'",
133 'join_conds' => [],
134 'options' => 'ORDER BY log_timestamp DESC',
135 ],
136 'foo',
137 true, // tag filtering enabled
138 [
139 'tables' => [ 'logging', 'change_tag' ],
140 'fields' => [ 'log_id', 'ts_tags' => $groupConcats['logging'] ],
141 'conds' => [ "log_timestamp > '20170714183203'", 'ct_tag_id' => [ 1 ] ],
142 'join_conds' => [ 'change_tag' => [ 'JOIN', 'ct_log_id=log_id' ] ],
143 'options' => [ 'ORDER BY log_timestamp DESC' ],
144 ]
145 ],
146 'revision query with single tag filter' => [
147 [
148 'tables' => [ 'revision' ],
149 'fields' => [ 'rev_id', 'rev_timestamp' ],
150 'conds' => [ "rev_timestamp > '20170714183203'" ],
151 'join_conds' => [],
152 'options' => [ 'ORDER BY' => 'rev_timestamp DESC' ],
153 ],
154 'foo',
155 true, // tag filtering enabled
156 [
157 'tables' => [ 'revision', 'change_tag' ],
158 'fields' => [ 'rev_id', 'rev_timestamp', 'ts_tags' => $groupConcats['revision'] ],
159 'conds' => [ "rev_timestamp > '20170714183203'", 'ct_tag_id' => [ 1 ] ],
160 'join_conds' => [ 'change_tag' => [ 'JOIN', 'ct_rev_id=rev_id' ] ],
161 'options' => [ 'ORDER BY' => 'rev_timestamp DESC' ],
162 ]
163 ],
164 'archive query with single tag filter' => [
165 [
166 'tables' => [ 'archive' ],
167 'fields' => [ 'ar_id', 'ar_timestamp' ],
168 'conds' => [ "ar_timestamp > '20170714183203'" ],
169 'join_conds' => [],
170 'options' => [ 'ORDER BY' => 'ar_timestamp DESC' ],
171 ],
172 'foo',
173 true, // tag filtering enabled
174 [
175 'tables' => [ 'archive', 'change_tag' ],
176 'fields' => [ 'ar_id', 'ar_timestamp', 'ts_tags' => $groupConcats['archive'] ],
177 'conds' => [ "ar_timestamp > '20170714183203'", 'ct_tag_id' => [ 1 ] ],
178 'join_conds' => [ 'change_tag' => [ 'JOIN', 'ct_rev_id=ar_rev_id' ] ],
179 'options' => [ 'ORDER BY' => 'ar_timestamp DESC' ],
180 ]
181 ],
182 'unsupported table name throws exception (even without tag filter)' => [
183 [
184 'tables' => [ 'foobar' ],
185 'fields' => [ 'fb_id', 'fb_timestamp' ],
186 'conds' => [ "fb_timestamp > '20170714183203'" ],
187 'join_conds' => [],
188 'options' => [ 'ORDER BY' => 'fb_timestamp DESC' ],
189 ],
190 '',
191 true, // tag filtering enabled
192 [ 'exception' => MWException::class ]
193 ],
194 'tag filter ignored when tag filtering is disabled' => [
195 [
196 'tables' => [ 'archive' ],
197 'fields' => [ 'ar_id', 'ar_timestamp' ],
198 'conds' => [ "ar_timestamp > '20170714183203'" ],
199 'join_conds' => [],
200 'options' => [ 'ORDER BY' => 'ar_timestamp DESC' ],
201 ],
202 'foo',
203 false, // tag filtering disabled
204 [
205 'tables' => [ 'archive' ],
206 'fields' => [ 'ar_id', 'ar_timestamp', 'ts_tags' => $groupConcats['archive'] ],
207 'conds' => [ "ar_timestamp > '20170714183203'" ],
208 'join_conds' => [],
209 'options' => [ 'ORDER BY' => 'ar_timestamp DESC' ],
210 ]
211 ],
212 'recentchanges query with multiple tag filter' => [
213 [
214 'tables' => [ 'recentchanges' ],
215 'fields' => [ 'rc_id', 'rc_timestamp' ],
216 'conds' => [ "rc_timestamp > '20170714183203'" ],
217 'join_conds' => [],
218 'options' => [ 'ORDER BY' => 'rc_timestamp DESC' ],
219 ],
220 [ 'foo', 'bar' ],
221 true, // tag filtering enabled
222 [
223 'tables' => [ 'recentchanges', 'change_tag' ],
224 'fields' => [ 'rc_id', 'rc_timestamp', 'ts_tags' => $groupConcats['recentchanges'] ],
225 'conds' => [ "rc_timestamp > '20170714183203'", 'ct_tag_id' => [ 1, 2 ] ],
226 'join_conds' => [ 'change_tag' => [ 'JOIN', 'ct_rc_id=rc_id' ] ],
227 'options' => [ 'ORDER BY' => 'rc_timestamp DESC', 'DISTINCT' ],
228 ]
229 ],
230 'recentchanges query with multiple tag filter that already has DISTINCT' => [
231 [
232 'tables' => [ 'recentchanges' ],
233 'fields' => [ 'rc_id', 'rc_timestamp' ],
234 'conds' => [ "rc_timestamp > '20170714183203'" ],
235 'join_conds' => [],
236 'options' => [ 'DISTINCT', 'ORDER BY' => 'rc_timestamp DESC' ],
237 ],
238 [ 'foo', 'bar' ],
239 true, // tag filtering enabled
240 [
241 'tables' => [ 'recentchanges', 'change_tag' ],
242 'fields' => [ 'rc_id', 'rc_timestamp', 'ts_tags' => $groupConcats['recentchanges'] ],
243 'conds' => [ "rc_timestamp > '20170714183203'", 'ct_tag_id' => [ 1, 2 ] ],
244 'join_conds' => [ 'change_tag' => [ 'JOIN', 'ct_rc_id=rc_id' ] ],
245 'options' => [ 'DISTINCT', 'ORDER BY' => 'rc_timestamp DESC' ],
246 ]
247 ],
248 'recentchanges query with multiple tag filter with strings' => [
249 [
250 'tables' => 'recentchanges',
251 'fields' => 'rc_id',
252 'conds' => "rc_timestamp > '20170714183203'",
253 'join_conds' => [],
254 'options' => 'ORDER BY rc_timestamp DESC',
255 ],
256 [ 'foo', 'bar' ],
257 true, // tag filtering enabled
258 [
259 'tables' => [ 'recentchanges', 'change_tag' ],
260 'fields' => [ 'rc_id', 'ts_tags' => $groupConcats['recentchanges'] ],
261 'conds' => [ "rc_timestamp > '20170714183203'", 'ct_tag_id' => [ 1, 2 ] ],
262 'join_conds' => [ 'change_tag' => [ 'JOIN', 'ct_rc_id=rc_id' ] ],
263 'options' => [ 'ORDER BY rc_timestamp DESC', 'DISTINCT' ],
264 ]
265 ],
266 ];
267 }
268
269 public static function dataGetSoftwareTags() {
270 return [
271 [
272 [
273 'mw-contentModelChange' => true,
274 'mw-redirect' => true,
275 'mw-rollback' => true,
276 'mw-blank' => true,
277 'mw-replace' => true
278 ],
279 [
280 'mw-rollback',
281 'mw-replace',
282 'mw-blank'
283 ]
284 ],
285
286 [
287 [
288 'mw-contentmodelchanged' => true,
289 'mw-replace' => true,
290 'mw-new-redirects' => true,
291 'mw-changed-redirect-target' => true,
292 'mw-rolback' => true,
293 'mw-blanking' => false
294 ],
295 [
296 'mw-replace',
297 'mw-changed-redirect-target'
298 ]
299 ],
300
301 [
302 [
303 null,
304 false,
305 'Lorem ipsum',
306 'mw-translation'
307 ],
308 []
309 ],
310
311 [
312 [],
313 []
314 ]
315 ];
316 }
317
318 /**
319 * @dataProvider dataGetSoftwareTags
320 * @covers ChangeTags::getSoftwareTags
321 */
322 public function testGetSoftwareTags( $softwareTags, $expected ) {
323 $this->setMwGlobals( 'wgSoftwareTags', $softwareTags );
324
325 $actual = ChangeTags::getSoftwareTags();
326 // Order of tags in arrays is not important
327 sort( $expected );
328 sort( $actual );
329 $this->assertEquals( $expected, $actual );
330 }
331
332 public function testUpdateTags() {
333 // FIXME: fails under postgres
334 $this->markTestSkippedIfDbType( 'postgres' );
335
336 $dbw = wfGetDB( DB_MASTER );
337 $dbw->delete( 'change_tag', '*' );
338 $dbw->delete( 'change_tag_def', '*' );
339
340 $rcId = 123;
341 $revId = 341;
342 ChangeTags::updateTags( [ 'tag1', 'tag2' ], [], $rcId, $revId );
343
344 $dbr = wfGetDB( DB_REPLICA );
345
346 $expected = [
347 (object)[
348 'ctd_name' => 'tag1',
349 'ctd_id' => 1,
350 'ctd_count' => 1
351 ],
352 (object)[
353 'ctd_name' => 'tag2',
354 'ctd_id' => 2,
355 'ctd_count' => 1
356 ],
357 ];
358 $res = $dbr->select( 'change_tag_def', [ 'ctd_name', 'ctd_id', 'ctd_count' ], '' );
359 $this->assertEquals( $expected, iterator_to_array( $res, false ) );
360
361 $expected2 = [
362 (object)[
363 'ct_tag_id' => 1,
364 'ct_rc_id' => 123,
365 'ct_rev_id' => 341
366 ],
367 (object)[
368 'ct_tag_id' => 2,
369 'ct_rc_id' => 123,
370 'ct_rev_id' => 341
371 ],
372 ];
373 $res2 = $dbr->select( 'change_tag', [ 'ct_tag_id', 'ct_rc_id', 'ct_rev_id' ], '' );
374 $this->assertEquals( $expected2, iterator_to_array( $res2, false ) );
375
376 $rcId = 124;
377 $revId = 342;
378 ChangeTags::updateTags( [ 'tag1' ], [], $rcId, $revId );
379 ChangeTags::updateTags( [ 'tag3' ], [], $rcId, $revId );
380
381 $dbr = wfGetDB( DB_REPLICA );
382
383 $expected = [
384 (object)[
385 'ctd_name' => 'tag1',
386 'ctd_id' => 1,
387 'ctd_count' => 2
388 ],
389 (object)[
390 'ctd_name' => 'tag2',
391 'ctd_id' => 2,
392 'ctd_count' => 1
393 ],
394 (object)[
395 'ctd_name' => 'tag3',
396 'ctd_id' => 3,
397 'ctd_count' => 1
398 ],
399 ];
400 $res = $dbr->select( 'change_tag_def', [ 'ctd_name', 'ctd_id', 'ctd_count' ], '' );
401 $this->assertEquals( $expected, iterator_to_array( $res, false ) );
402
403 $expected2 = [
404 (object)[
405 'ct_tag_id' => 1,
406 'ct_rc_id' => 123,
407 'ct_rev_id' => 341
408 ],
409 (object)[
410 'ct_tag_id' => 1,
411 'ct_rc_id' => 124,
412 'ct_rev_id' => 342
413 ],
414 (object)[
415 'ct_tag_id' => 2,
416 'ct_rc_id' => 123,
417 'ct_rev_id' => 341
418 ],
419 (object)[
420 'ct_tag_id' => 3,
421 'ct_rc_id' => 124,
422 'ct_rev_id' => 342
423 ],
424 ];
425 $res2 = $dbr->select( 'change_tag', [ 'ct_tag_id', 'ct_rc_id', 'ct_rev_id' ], '' );
426 $this->assertEquals( $expected2, iterator_to_array( $res2, false ) );
427 }
428
429 public function testUpdateTagsSkipDuplicates() {
430 // FIXME: fails under postgres
431 $this->markTestSkippedIfDbType( 'postgres' );
432
433 $dbw = wfGetDB( DB_MASTER );
434 $dbw->delete( 'change_tag', '*' );
435 $dbw->delete( 'change_tag_def', '*' );
436
437 $rcId = 123;
438 ChangeTags::updateTags( [ 'tag1', 'tag2' ], [], $rcId );
439 ChangeTags::updateTags( [ 'tag2', 'tag3' ], [], $rcId );
440
441 $dbr = wfGetDB( DB_REPLICA );
442
443 $expected = [
444 (object)[
445 'ctd_name' => 'tag1',
446 'ctd_id' => 1,
447 'ctd_count' => 1
448 ],
449 (object)[
450 'ctd_name' => 'tag2',
451 'ctd_id' => 2,
452 'ctd_count' => 1
453 ],
454 (object)[
455 'ctd_name' => 'tag3',
456 'ctd_id' => 3,
457 'ctd_count' => 1
458 ],
459 ];
460 $res = $dbr->select( 'change_tag_def', [ 'ctd_name', 'ctd_id', 'ctd_count' ], '' );
461 $this->assertEquals( $expected, iterator_to_array( $res, false ) );
462
463 $expected2 = [
464 (object)[
465 'ct_tag_id' => 1,
466 'ct_rc_id' => 123
467 ],
468 (object)[
469 'ct_tag_id' => 2,
470 'ct_rc_id' => 123
471 ],
472 (object)[
473 'ct_tag_id' => 3,
474 'ct_rc_id' => 123
475 ],
476 ];
477 $res2 = $dbr->select( 'change_tag', [ 'ct_tag_id', 'ct_rc_id' ], '' );
478 $this->assertEquals( $expected2, iterator_to_array( $res2, false ) );
479 }
480
481 public function testUpdateTagsDoNothingOnRepeatedCall() {
482 // FIXME: fails under postgres
483 $this->markTestSkippedIfDbType( 'postgres' );
484
485 $dbw = wfGetDB( DB_MASTER );
486 $dbw->delete( 'change_tag', '*' );
487 $dbw->delete( 'change_tag_def', '*' );
488
489 $rcId = 123;
490 ChangeTags::updateTags( [ 'tag1', 'tag2' ], [], $rcId );
491 $res = ChangeTags::updateTags( [ 'tag2', 'tag1' ], [], $rcId );
492 $this->assertEquals( [ [], [], [ 'tag1', 'tag2' ] ], $res );
493
494 $dbr = wfGetDB( DB_REPLICA );
495
496 $expected = [
497 (object)[
498 'ctd_name' => 'tag1',
499 'ctd_id' => 1,
500 'ctd_count' => 1
501 ],
502 (object)[
503 'ctd_name' => 'tag2',
504 'ctd_id' => 2,
505 'ctd_count' => 1
506 ],
507 ];
508 $res = $dbr->select( 'change_tag_def', [ 'ctd_name', 'ctd_id', 'ctd_count' ], '' );
509 $this->assertEquals( $expected, iterator_to_array( $res, false ) );
510
511 $expected2 = [
512 (object)[
513 'ct_tag_id' => 1,
514 'ct_rc_id' => 123
515 ],
516 (object)[
517 'ct_tag_id' => 2,
518 'ct_rc_id' => 123
519 ],
520 ];
521 $res2 = $dbr->select( 'change_tag', [ 'ct_tag_id', 'ct_rc_id' ], '' );
522 $this->assertEquals( $expected2, iterator_to_array( $res2, false ) );
523 }
524
525 public function testDeleteTags() {
526 $dbw = wfGetDB( DB_MASTER );
527 $dbw->delete( 'change_tag', '*' );
528 $dbw->delete( 'change_tag_def', '*' );
529 MediaWikiServices::getInstance()->resetServiceForTesting( 'NameTableStoreFactory' );
530
531 $rcId = 123;
532 ChangeTags::updateTags( [ 'tag1', 'tag2' ], [], $rcId );
533
534 ChangeTags::updateTags( [], [ 'tag2' ], $rcId );
535
536 $dbr = wfGetDB( DB_REPLICA );
537
538 $expected = [
539 (object)[
540 'ctd_name' => 'tag1',
541 'ctd_id' => 1,
542 'ctd_count' => 1
543 ],
544 ];
545 $res = $dbr->select( 'change_tag_def', [ 'ctd_name', 'ctd_id', 'ctd_count' ], '' );
546 $this->assertEquals( $expected, iterator_to_array( $res, false ) );
547
548 $expected2 = [
549 (object)[
550 'ct_tag_id' => 1,
551 'ct_rc_id' => 123
552 ]
553 ];
554 $res2 = $dbr->select( 'change_tag', [ 'ct_tag_id', 'ct_rc_id' ], '' );
555 $this->assertEquals( $expected2, iterator_to_array( $res2, false ) );
556 }
557
558 public function testTagUsageStatistics() {
559 $dbw = wfGetDB( DB_MASTER );
560 $dbw->delete( 'change_tag', '*' );
561 $dbw->delete( 'change_tag_def', '*' );
562 MediaWikiServices::getInstance()->resetServiceForTesting( 'NameTableStoreFactory' );
563
564 $rcId = 123;
565 ChangeTags::updateTags( [ 'tag1', 'tag2' ], [], $rcId );
566
567 $rcId = 124;
568 ChangeTags::updateTags( [ 'tag1' ], [], $rcId );
569
570 $this->assertEquals( [ 'tag1' => 2, 'tag2' => 1 ], ChangeTags::tagUsageStatistics() );
571 }
572
573 public function testListExplicitlyDefinedTags() {
574 $dbw = wfGetDB( DB_MASTER );
575 $dbw->delete( 'change_tag', '*' );
576 $dbw->delete( 'change_tag_def', '*' );
577
578 $rcId = 123;
579 ChangeTags::updateTags( [ 'tag1', 'tag2' ], [], $rcId );
580 ChangeTags::defineTag( 'tag2' );
581
582 $this->assertEquals( [ 'tag2' ], ChangeTags::listExplicitlyDefinedTags() );
583 $dbr = wfGetDB( DB_REPLICA );
584
585 $expected = [
586 (object)[
587 'ctd_name' => 'tag1',
588 'ctd_user_defined' => 0
589 ],
590 (object)[
591 'ctd_name' => 'tag2',
592 'ctd_user_defined' => 1
593 ],
594 ];
595 $res = $dbr->select( 'change_tag_def', [ 'ctd_name', 'ctd_user_defined' ], '' );
596 $this->assertEquals( $expected, iterator_to_array( $res, false ) );
597 }
598 }