3 use MediaWiki\MediaWikiServices
;
9 class ChangeTagsTest
extends MediaWikiTestCase
{
11 public function setUp() {
14 $this->tablesUsed
[] = 'change_tag';
15 $this->tablesUsed
[] = 'change_tag_def';
16 $this->tablesUsed
[] = 'tag_summary';
17 $this->tablesUsed
[] = 'valid_tag';
19 // Truncate these to avoid the supposed-to-be-unused IDs in tests here turning
20 // out to be used, leading ChangeTags::updateTags() to pick up bogus rc_id,
21 // log_id, or rev_id values and run into unique constraint violations.
22 $this->tablesUsed
[] = 'recentchanges';
23 $this->tablesUsed
[] = 'logging';
24 $this->tablesUsed
[] = 'revision';
25 $this->tablesUsed
[] = 'archive';
28 // TODO only modifyDisplayQuery and getSoftwareTags are tested, nothing else is
30 /** @dataProvider provideModifyDisplayQuery */
31 public function testModifyDisplayQuery( $origQuery, $filter_tag, $useTags, $modifiedQuery ) {
32 $this->setMwGlobals( 'wgUseTagFilter', $useTags );
34 ChangeTags
::updateTags( [ 'foo', 'bar' ], [], $rcId );
35 // HACK resolve deferred group concats (see comment in provideModifyDisplayQuery)
36 if ( isset( $modifiedQuery['fields']['ts_tags'] ) ) {
37 $modifiedQuery['fields']['ts_tags'] = call_user_func_array(
38 [ wfGetDB( DB_REPLICA
), 'buildGroupConcatField' ],
39 $modifiedQuery['fields']['ts_tags']
42 if ( isset( $modifiedQuery['exception'] ) ) {
43 $this->setExpectedException( $modifiedQuery['exception'] );
45 ChangeTags
::modifyDisplayQuery(
49 $origQuery['join_conds'],
50 $origQuery['options'],
53 if ( !isset( $modifiedQuery['exception'] ) ) {
54 $this->assertArrayEquals(
57 /* ordered = */ false,
63 public function provideModifyDisplayQuery() {
64 // HACK if we call $dbr->buildGroupConcatField() now, it will return the wrong table names
65 // We have to have the test runner call it instead
66 $baseConcats = [ ',', [ 'change_tag', 'change_tag_def' ], 'ctd_name' ];
67 $joinConds = [ 'change_tag_def' => [ 'INNER JOIN', 'ct_tag_id=ctd_id' ] ];
69 'recentchanges' => array_merge( $baseConcats, [ 'ct_rc_id=rc_id', $joinConds ] ),
70 'logging' => array_merge( $baseConcats, [ 'ct_log_id=log_id', $joinConds ] ),
71 'revision' => array_merge( $baseConcats, [ 'ct_rev_id=rev_id', $joinConds ] ),
72 'archive' => array_merge( $baseConcats, [ 'ct_rev_id=ar_rev_id', $joinConds ] ),
76 'simple recentchanges query' => [
78 'tables' => [ 'recentchanges' ],
79 'fields' => [ 'rc_id', 'rc_timestamp' ],
80 'conds' => [ "rc_timestamp > '20170714183203'" ],
82 'options' => [ 'ORDER BY' => 'rc_timestamp DESC' ],
85 true, // tag filtering enabled
87 'tables' => [ 'recentchanges' ],
88 'fields' => [ 'rc_id', 'rc_timestamp', 'ts_tags' => $groupConcats['recentchanges'] ],
89 'conds' => [ "rc_timestamp > '20170714183203'" ],
91 'options' => [ 'ORDER BY' => 'rc_timestamp DESC' ],
94 'simple query with strings' => [
96 'tables' => 'recentchanges',
98 'conds' => "rc_timestamp > '20170714183203'",
100 'options' => 'ORDER BY rc_timestamp DESC',
103 true, // tag filtering enabled
105 'tables' => [ 'recentchanges' ],
106 'fields' => [ 'rc_id', 'ts_tags' => $groupConcats['recentchanges'] ],
107 'conds' => [ "rc_timestamp > '20170714183203'" ],
109 'options' => [ 'ORDER BY rc_timestamp DESC' ],
112 'recentchanges query with single tag filter' => [
114 'tables' => [ 'recentchanges' ],
115 'fields' => [ 'rc_id', 'rc_timestamp' ],
116 'conds' => [ "rc_timestamp > '20170714183203'" ],
118 'options' => [ 'ORDER BY' => 'rc_timestamp DESC' ],
121 true, // tag filtering enabled
123 'tables' => [ 'recentchanges', 'change_tag' ],
124 'fields' => [ 'rc_id', 'rc_timestamp', 'ts_tags' => $groupConcats['recentchanges'] ],
125 'conds' => [ "rc_timestamp > '20170714183203'", 'ct_tag_id' => [ 1 ] ],
126 'join_conds' => [ 'change_tag' => [ 'INNER JOIN', 'ct_rc_id=rc_id' ] ],
127 'options' => [ 'ORDER BY' => 'rc_timestamp DESC' ],
130 'logging query with single tag filter and strings' => [
132 'tables' => 'logging',
133 'fields' => 'log_id',
134 'conds' => "log_timestamp > '20170714183203'",
136 'options' => 'ORDER BY log_timestamp DESC',
139 true, // tag filtering enabled
141 'tables' => [ 'logging', 'change_tag' ],
142 'fields' => [ 'log_id', 'ts_tags' => $groupConcats['logging'] ],
143 'conds' => [ "log_timestamp > '20170714183203'", 'ct_tag_id' => [ 1 ] ],
144 'join_conds' => [ 'change_tag' => [ 'INNER JOIN', 'ct_log_id=log_id' ] ],
145 'options' => [ 'ORDER BY log_timestamp DESC' ],
148 'revision query with single tag filter' => [
150 'tables' => [ 'revision' ],
151 'fields' => [ 'rev_id', 'rev_timestamp' ],
152 'conds' => [ "rev_timestamp > '20170714183203'" ],
154 'options' => [ 'ORDER BY' => 'rev_timestamp DESC' ],
157 true, // tag filtering enabled
159 'tables' => [ 'revision', 'change_tag' ],
160 'fields' => [ 'rev_id', 'rev_timestamp', 'ts_tags' => $groupConcats['revision'] ],
161 'conds' => [ "rev_timestamp > '20170714183203'", 'ct_tag_id' => [ 1 ] ],
162 'join_conds' => [ 'change_tag' => [ 'INNER JOIN', 'ct_rev_id=rev_id' ] ],
163 'options' => [ 'ORDER BY' => 'rev_timestamp DESC' ],
166 'archive query with single tag filter' => [
168 'tables' => [ 'archive' ],
169 'fields' => [ 'ar_id', 'ar_timestamp' ],
170 'conds' => [ "ar_timestamp > '20170714183203'" ],
172 'options' => [ 'ORDER BY' => 'ar_timestamp DESC' ],
175 true, // tag filtering enabled
177 'tables' => [ 'archive', 'change_tag' ],
178 'fields' => [ 'ar_id', 'ar_timestamp', 'ts_tags' => $groupConcats['archive'] ],
179 'conds' => [ "ar_timestamp > '20170714183203'", 'ct_tag_id' => [ 1 ] ],
180 'join_conds' => [ 'change_tag' => [ 'INNER JOIN', 'ct_rev_id=ar_rev_id' ] ],
181 'options' => [ 'ORDER BY' => 'ar_timestamp DESC' ],
184 'unsupported table name throws exception (even without tag filter)' => [
186 'tables' => [ 'foobar' ],
187 'fields' => [ 'fb_id', 'fb_timestamp' ],
188 'conds' => [ "fb_timestamp > '20170714183203'" ],
190 'options' => [ 'ORDER BY' => 'fb_timestamp DESC' ],
193 true, // tag filtering enabled
194 [ 'exception' => MWException
::class ]
196 'tag filter ignored when tag filtering is disabled' => [
198 'tables' => [ 'archive' ],
199 'fields' => [ 'ar_id', 'ar_timestamp' ],
200 'conds' => [ "ar_timestamp > '20170714183203'" ],
202 'options' => [ 'ORDER BY' => 'ar_timestamp DESC' ],
205 false, // tag filtering disabled
207 'tables' => [ 'archive' ],
208 'fields' => [ 'ar_id', 'ar_timestamp', 'ts_tags' => $groupConcats['archive'] ],
209 'conds' => [ "ar_timestamp > '20170714183203'" ],
211 'options' => [ 'ORDER BY' => 'ar_timestamp DESC' ],
214 'recentchanges query with multiple tag filter' => [
216 'tables' => [ 'recentchanges' ],
217 'fields' => [ 'rc_id', 'rc_timestamp' ],
218 'conds' => [ "rc_timestamp > '20170714183203'" ],
220 'options' => [ 'ORDER BY' => 'rc_timestamp DESC' ],
223 true, // tag filtering enabled
225 'tables' => [ 'recentchanges', 'change_tag' ],
226 'fields' => [ 'rc_id', 'rc_timestamp', 'ts_tags' => $groupConcats['recentchanges'] ],
227 'conds' => [ "rc_timestamp > '20170714183203'", 'ct_tag_id' => [ 1, 2 ] ],
228 'join_conds' => [ 'change_tag' => [ 'INNER JOIN', 'ct_rc_id=rc_id' ] ],
229 'options' => [ 'ORDER BY' => 'rc_timestamp DESC', 'DISTINCT' ],
232 'recentchanges query with multiple tag filter that already has DISTINCT' => [
234 'tables' => [ 'recentchanges' ],
235 'fields' => [ 'rc_id', 'rc_timestamp' ],
236 'conds' => [ "rc_timestamp > '20170714183203'" ],
238 'options' => [ 'DISTINCT', 'ORDER BY' => 'rc_timestamp DESC' ],
241 true, // tag filtering enabled
243 'tables' => [ 'recentchanges', 'change_tag' ],
244 'fields' => [ 'rc_id', 'rc_timestamp', 'ts_tags' => $groupConcats['recentchanges'] ],
245 'conds' => [ "rc_timestamp > '20170714183203'", 'ct_tag_id' => [ 1, 2 ] ],
246 'join_conds' => [ 'change_tag' => [ 'INNER JOIN', 'ct_rc_id=rc_id' ] ],
247 'options' => [ 'DISTINCT', 'ORDER BY' => 'rc_timestamp DESC' ],
250 'recentchanges query with multiple tag filter with strings' => [
252 'tables' => 'recentchanges',
254 'conds' => "rc_timestamp > '20170714183203'",
256 'options' => 'ORDER BY rc_timestamp DESC',
259 true, // tag filtering enabled
261 'tables' => [ 'recentchanges', 'change_tag' ],
262 'fields' => [ 'rc_id', 'ts_tags' => $groupConcats['recentchanges'] ],
263 'conds' => [ "rc_timestamp > '20170714183203'", 'ct_tag_id' => [ 1, 2 ] ],
264 'join_conds' => [ 'change_tag' => [ 'INNER JOIN', 'ct_rc_id=rc_id' ] ],
265 'options' => [ 'ORDER BY rc_timestamp DESC', 'DISTINCT' ],
271 public static function dataGetSoftwareTags() {
275 'mw-contentModelChange' => true,
276 'mw-redirect' => true,
277 'mw-rollback' => true,
290 'mw-contentmodelchanged' => true,
291 'mw-replace' => true,
292 'mw-new-redirects' => true,
293 'mw-changed-redirect-target' => true,
294 'mw-rolback' => true,
295 'mw-blanking' => false
299 'mw-changed-redirect-target'
321 * @dataProvider dataGetSoftwareTags
322 * @covers ChangeTags::getSoftwareTags
324 public function testGetSoftwareTags( $softwareTags, $expected ) {
325 $this->setMwGlobals( 'wgSoftwareTags', $softwareTags );
327 $actual = ChangeTags
::getSoftwareTags();
328 // Order of tags in arrays is not important
331 $this->assertEquals( $expected, $actual );
334 public function testUpdateTags() {
335 // FIXME: fails under postgres
336 $this->markTestSkippedIfDbType( 'postgres' );
338 $dbw = wfGetDB( DB_MASTER
);
339 $dbw->delete( 'change_tag', '*' );
340 $dbw->delete( 'change_tag_def', '*' );
343 ChangeTags
::updateTags( [ 'tag1', 'tag2' ], [], $rcId );
345 $dbr = wfGetDB( DB_REPLICA
);
349 'ctd_name' => 'tag1',
354 'ctd_name' => 'tag2',
359 $res = $dbr->select( 'change_tag_def', [ 'ctd_name', 'ctd_id', 'ctd_count' ], '' );
360 $this->assertEquals( $expected, iterator_to_array( $res, false ) );
372 $res2 = $dbr->select( 'change_tag', [ 'ct_tag_id', 'ct_rc_id' ], '' );
373 $this->assertEquals( $expected2, iterator_to_array( $res2, false ) );
376 ChangeTags
::updateTags( [ 'tag1' ], [], $rcId );
378 ChangeTags
::updateTags( [ 'tag3' ], [], $rcId );
380 $dbr = wfGetDB( DB_REPLICA
);
384 'ctd_name' => 'tag1',
389 'ctd_name' => 'tag2',
394 'ctd_name' => 'tag3',
399 $res = $dbr->select( 'change_tag_def', [ 'ctd_name', 'ctd_id', 'ctd_count' ], '' );
400 $this->assertEquals( $expected, iterator_to_array( $res, false ) );
420 $res2 = $dbr->select( 'change_tag', [ 'ct_tag_id', 'ct_rc_id' ], '' );
421 $this->assertEquals( $expected2, iterator_to_array( $res2, false ) );
424 public function testDeleteTags() {
425 $dbw = wfGetDB( DB_MASTER
);
426 $dbw->delete( 'change_tag', '*' );
427 $dbw->delete( 'change_tag_def', '*' );
428 MediaWikiServices
::getInstance()->resetServiceForTesting( 'NameTableStoreFactory' );
431 ChangeTags
::updateTags( [ 'tag1', 'tag2' ], [], $rcId );
433 ChangeTags
::updateTags( [], [ 'tag2' ], $rcId );
435 $dbr = wfGetDB( DB_REPLICA
);
439 'ctd_name' => 'tag1',
444 $res = $dbr->select( 'change_tag_def', [ 'ctd_name', 'ctd_id', 'ctd_count' ], '' );
445 $this->assertEquals( $expected, iterator_to_array( $res, false ) );
453 $res2 = $dbr->select( 'change_tag', [ 'ct_tag_id', 'ct_rc_id' ], '' );
454 $this->assertEquals( $expected2, iterator_to_array( $res2, false ) );
457 public function testTagUsageStatistics() {
458 $dbw = wfGetDB( DB_MASTER
);
459 $dbw->delete( 'change_tag', '*' );
460 $dbw->delete( 'change_tag_def', '*' );
461 MediaWikiServices
::getInstance()->resetServiceForTesting( 'NameTableStoreFactory' );
464 ChangeTags
::updateTags( [ 'tag1', 'tag2' ], [], $rcId );
467 ChangeTags
::updateTags( [ 'tag1' ], [], $rcId );
469 $this->assertEquals( [ 'tag1' => 2, 'tag2' => 1 ], ChangeTags
::tagUsageStatistics() );
472 public function testListExplicitlyDefinedTags() {
473 $dbw = wfGetDB( DB_MASTER
);
474 $dbw->delete( 'change_tag', '*' );
475 $dbw->delete( 'change_tag_def', '*' );
476 $dbw->delete( 'valid_tag', '*' );
479 ChangeTags
::updateTags( [ 'tag1', 'tag2' ], [], $rcId );
480 ChangeTags
::defineTag( 'tag2' );
482 $this->assertEquals( [ 'tag2' ], ChangeTags
::listExplicitlyDefinedTags() );
483 $dbr = wfGetDB( DB_REPLICA
);
487 'ctd_name' => 'tag1',
488 'ctd_user_defined' => 0
491 'ctd_name' => 'tag2',
492 'ctd_user_defined' => 1
495 $res = $dbr->select( 'change_tag_def', [ 'ctd_name', 'ctd_user_defined' ], '' );
496 $this->assertEquals( $expected, iterator_to_array( $res, false ) );
498 $this->assertEquals( [], $dbr->selectFieldValues( 'valid_tag', 'vt_tag', '' ) );