MediaWiki  REL1_31
ChangeTagsTest.php
Go to the documentation of this file.
1 <?php
2 
7 
8  // TODO only modifyDisplayQuery and getSoftwareTags are tested, nothing else is
9 
11  public function testModifyDisplayQuery( $origQuery, $filter_tag, $useTags, $modifiedQuery ) {
12  $this->setMwGlobals( 'wgUseTagFilter', $useTags );
13  // HACK resolve deferred group concats (see comment in provideModifyDisplayQuery)
14  if ( isset( $modifiedQuery['fields']['ts_tags'] ) ) {
15  $modifiedQuery['fields']['ts_tags'] = call_user_func_array(
16  [ wfGetDB( DB_REPLICA ), 'buildGroupConcatField' ],
17  $modifiedQuery['fields']['ts_tags']
18  );
19  }
20  if ( isset( $modifiedQuery['exception'] ) ) {
21  $this->setExpectedException( $modifiedQuery['exception'] );
22  }
24  $origQuery['tables'],
25  $origQuery['fields'],
26  $origQuery['conds'],
27  $origQuery['join_conds'],
28  $origQuery['options'],
29  $filter_tag
30  );
31  if ( !isset( $modifiedQuery['exception'] ) ) {
32  $this->assertArrayEquals(
33  $modifiedQuery,
34  $origQuery,
35  /* ordered = */ false,
36  /* named = */ true
37  );
38  }
39  }
40 
41  public function provideModifyDisplayQuery() {
42  // HACK if we call $dbr->buildGroupConcatField() now, it will return the wrong table names
43  // We have to have the test runner call it instead
44  $groupConcats = [
45  'recentchanges' => [ ',', 'change_tag', 'ct_tag', 'ct_rc_id=rc_id' ],
46  'logging' => [ ',', 'change_tag', 'ct_tag', 'ct_log_id=log_id' ],
47  'revision' => [ ',', 'change_tag', 'ct_tag', 'ct_rev_id=rev_id' ],
48  'archive' => [ ',', 'change_tag', 'ct_tag', 'ct_rev_id=ar_rev_id' ],
49  ];
50 
51  return [
52  'simple recentchanges query' => [
53  [
54  'tables' => [ 'recentchanges' ],
55  'fields' => [ 'rc_id', 'rc_timestamp' ],
56  'conds' => [ "rc_timestamp > '20170714183203'" ],
57  'join_conds' => [],
58  'options' => [ 'ORDER BY' => 'rc_timestamp DESC' ],
59  ],
60  '', // no tag filter
61  true, // tag filtering enabled
62  [
63  'tables' => [ 'recentchanges' ],
64  'fields' => [ 'rc_id', 'rc_timestamp', 'ts_tags' => $groupConcats['recentchanges'] ],
65  'conds' => [ "rc_timestamp > '20170714183203'" ],
66  'join_conds' => [],
67  'options' => [ 'ORDER BY' => 'rc_timestamp DESC' ],
68  ]
69  ],
70  'simple query with strings' => [
71  [
72  'tables' => 'recentchanges',
73  'fields' => 'rc_id',
74  'conds' => "rc_timestamp > '20170714183203'",
75  'join_conds' => [],
76  'options' => 'ORDER BY rc_timestamp DESC',
77  ],
78  '', // no tag filter
79  true, // tag filtering enabled
80  [
81  'tables' => [ 'recentchanges' ],
82  'fields' => [ 'rc_id', 'ts_tags' => $groupConcats['recentchanges'] ],
83  'conds' => [ "rc_timestamp > '20170714183203'" ],
84  'join_conds' => [],
85  'options' => [ 'ORDER BY rc_timestamp DESC' ],
86  ]
87  ],
88  'recentchanges query with single tag filter' => [
89  [
90  'tables' => [ 'recentchanges' ],
91  'fields' => [ 'rc_id', 'rc_timestamp' ],
92  'conds' => [ "rc_timestamp > '20170714183203'" ],
93  'join_conds' => [],
94  'options' => [ 'ORDER BY' => 'rc_timestamp DESC' ],
95  ],
96  'foo',
97  true, // tag filtering enabled
98  [
99  'tables' => [ 'recentchanges', 'change_tag' ],
100  'fields' => [ 'rc_id', 'rc_timestamp', 'ts_tags' => $groupConcats['recentchanges'] ],
101  'conds' => [ "rc_timestamp > '20170714183203'", 'ct_tag' => 'foo' ],
102  'join_conds' => [ 'change_tag' => [ 'INNER JOIN', 'ct_rc_id=rc_id' ] ],
103  'options' => [ 'ORDER BY' => 'rc_timestamp DESC' ],
104  ]
105  ],
106  'logging query with single tag filter and strings' => [
107  [
108  'tables' => 'logging',
109  'fields' => 'log_id',
110  'conds' => "log_timestamp > '20170714183203'",
111  'join_conds' => [],
112  'options' => 'ORDER BY log_timestamp DESC',
113  ],
114  'foo',
115  true, // tag filtering enabled
116  [
117  'tables' => [ 'logging', 'change_tag' ],
118  'fields' => [ 'log_id', 'ts_tags' => $groupConcats['logging'] ],
119  'conds' => [ "log_timestamp > '20170714183203'", 'ct_tag' => 'foo' ],
120  'join_conds' => [ 'change_tag' => [ 'INNER JOIN', 'ct_log_id=log_id' ] ],
121  'options' => [ 'ORDER BY log_timestamp DESC' ],
122  ]
123  ],
124  'revision query with single tag filter' => [
125  [
126  'tables' => [ 'revision' ],
127  'fields' => [ 'rev_id', 'rev_timestamp' ],
128  'conds' => [ "rev_timestamp > '20170714183203'" ],
129  'join_conds' => [],
130  'options' => [ 'ORDER BY' => 'rev_timestamp DESC' ],
131  ],
132  'foo',
133  true, // tag filtering enabled
134  [
135  'tables' => [ 'revision', 'change_tag' ],
136  'fields' => [ 'rev_id', 'rev_timestamp', 'ts_tags' => $groupConcats['revision'] ],
137  'conds' => [ "rev_timestamp > '20170714183203'", 'ct_tag' => 'foo' ],
138  'join_conds' => [ 'change_tag' => [ 'INNER JOIN', 'ct_rev_id=rev_id' ] ],
139  'options' => [ 'ORDER BY' => 'rev_timestamp DESC' ],
140  ]
141  ],
142  'archive query with single tag filter' => [
143  [
144  'tables' => [ 'archive' ],
145  'fields' => [ 'ar_id', 'ar_timestamp' ],
146  'conds' => [ "ar_timestamp > '20170714183203'" ],
147  'join_conds' => [],
148  'options' => [ 'ORDER BY' => 'ar_timestamp DESC' ],
149  ],
150  'foo',
151  true, // tag filtering enabled
152  [
153  'tables' => [ 'archive', 'change_tag' ],
154  'fields' => [ 'ar_id', 'ar_timestamp', 'ts_tags' => $groupConcats['archive'] ],
155  'conds' => [ "ar_timestamp > '20170714183203'", 'ct_tag' => 'foo' ],
156  'join_conds' => [ 'change_tag' => [ 'INNER JOIN', 'ct_rev_id=ar_rev_id' ] ],
157  'options' => [ 'ORDER BY' => 'ar_timestamp DESC' ],
158  ]
159  ],
160  'unsupported table name throws exception (even without tag filter)' => [
161  [
162  'tables' => [ 'foobar' ],
163  'fields' => [ 'fb_id', 'fb_timestamp' ],
164  'conds' => [ "fb_timestamp > '20170714183203'" ],
165  'join_conds' => [],
166  'options' => [ 'ORDER BY' => 'fb_timestamp DESC' ],
167  ],
168  '',
169  true, // tag filtering enabled
170  [ 'exception' => MWException::class ]
171  ],
172  'tag filter ignored when tag filtering is disabled' => [
173  [
174  'tables' => [ 'archive' ],
175  'fields' => [ 'ar_id', 'ar_timestamp' ],
176  'conds' => [ "ar_timestamp > '20170714183203'" ],
177  'join_conds' => [],
178  'options' => [ 'ORDER BY' => 'ar_timestamp DESC' ],
179  ],
180  'foo',
181  false, // tag filtering disabled
182  [
183  'tables' => [ 'archive' ],
184  'fields' => [ 'ar_id', 'ar_timestamp', 'ts_tags' => $groupConcats['archive'] ],
185  'conds' => [ "ar_timestamp > '20170714183203'" ],
186  'join_conds' => [],
187  'options' => [ 'ORDER BY' => 'ar_timestamp DESC' ],
188  ]
189  ],
190  'recentchanges query with multiple tag filter' => [
191  [
192  'tables' => [ 'recentchanges' ],
193  'fields' => [ 'rc_id', 'rc_timestamp' ],
194  'conds' => [ "rc_timestamp > '20170714183203'" ],
195  'join_conds' => [],
196  'options' => [ 'ORDER BY' => 'rc_timestamp DESC' ],
197  ],
198  [ 'foo', 'bar' ],
199  true, // tag filtering enabled
200  [
201  'tables' => [ 'recentchanges', 'change_tag' ],
202  'fields' => [ 'rc_id', 'rc_timestamp', 'ts_tags' => $groupConcats['recentchanges'] ],
203  'conds' => [ "rc_timestamp > '20170714183203'", 'ct_tag' => [ 'foo', 'bar' ] ],
204  'join_conds' => [ 'change_tag' => [ 'INNER JOIN', 'ct_rc_id=rc_id' ] ],
205  'options' => [ 'ORDER BY' => 'rc_timestamp DESC', 'DISTINCT' ],
206  ]
207  ],
208  'recentchanges query with multiple tag filter that already has DISTINCT' => [
209  [
210  'tables' => [ 'recentchanges' ],
211  'fields' => [ 'rc_id', 'rc_timestamp' ],
212  'conds' => [ "rc_timestamp > '20170714183203'" ],
213  'join_conds' => [],
214  'options' => [ 'DISTINCT', 'ORDER BY' => 'rc_timestamp DESC' ],
215  ],
216  [ 'foo', 'bar' ],
217  true, // tag filtering enabled
218  [
219  'tables' => [ 'recentchanges', 'change_tag' ],
220  'fields' => [ 'rc_id', 'rc_timestamp', 'ts_tags' => $groupConcats['recentchanges'] ],
221  'conds' => [ "rc_timestamp > '20170714183203'", 'ct_tag' => [ 'foo', 'bar' ] ],
222  'join_conds' => [ 'change_tag' => [ 'INNER JOIN', 'ct_rc_id=rc_id' ] ],
223  'options' => [ 'DISTINCT', 'ORDER BY' => 'rc_timestamp DESC' ],
224  ]
225  ],
226  'recentchanges query with multiple tag filter with strings' => [
227  [
228  'tables' => 'recentchanges',
229  'fields' => 'rc_id',
230  'conds' => "rc_timestamp > '20170714183203'",
231  'join_conds' => [],
232  'options' => 'ORDER BY rc_timestamp DESC',
233  ],
234  [ 'foo', 'bar' ],
235  true, // tag filtering enabled
236  [
237  'tables' => [ 'recentchanges', 'change_tag' ],
238  'fields' => [ 'rc_id', 'ts_tags' => $groupConcats['recentchanges'] ],
239  'conds' => [ "rc_timestamp > '20170714183203'", 'ct_tag' => [ 'foo', 'bar' ] ],
240  'join_conds' => [ 'change_tag' => [ 'INNER JOIN', 'ct_rc_id=rc_id' ] ],
241  'options' => [ 'ORDER BY rc_timestamp DESC', 'DISTINCT' ],
242  ]
243  ],
244  ];
245  }
246 
247  public static function dataGetSoftwareTags() {
248  return [
249  [
250  [
251  'mw-contentModelChange' => true,
252  'mw-redirect' => true,
253  'mw-rollback' => true,
254  'mw-blank' => true,
255  'mw-replace' => true
256  ],
257  [
258  'mw-rollback',
259  'mw-replace',
260  'mw-blank'
261  ]
262  ],
263 
264  [
265  [
266  'mw-contentmodelchanged' => true,
267  'mw-replace' => true,
268  'mw-new-redirects' => true,
269  'mw-changed-redirect-target' => true,
270  'mw-rolback' => true,
271  'mw-blanking' => false
272  ],
273  [
274  'mw-replace',
275  'mw-changed-redirect-target'
276  ]
277  ],
278 
279  [
280  [
281  null,
282  false,
283  'Lorem ipsum',
284  'mw-translation'
285  ],
286  []
287  ],
288 
289  [
290  [],
291  []
292  ]
293  ];
294  }
295 
300  public function testGetSoftwareTags( $softwareTags, $expected ) {
301  $this->setMwGlobals( 'wgSoftwareTags', $softwareTags );
302 
303  $actual = ChangeTags::getSoftwareTags();
304  // Order of tags in arrays is not important
305  sort( $expected );
306  sort( $actual );
307  $this->assertEquals( $expected, $actual );
308  }
309 }
MediaWikiTestCase\assertArrayEquals
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
Definition: MediaWikiTestCase.php:1771
ChangeTagsTest
ChangeTags.
Definition: ChangeTagsTest.php:6
ChangeTagsTest\provideModifyDisplayQuery
provideModifyDisplayQuery()
Definition: ChangeTagsTest.php:41
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:2006
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
ChangeTags\modifyDisplayQuery
static modifyDisplayQuery(&$tables, &$fields, &$conds, &$join_conds, &$options, $filter_tag='')
Applies all tags-related changes to a query.
Definition: ChangeTags.php:723
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2812
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:678
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
ChangeTags\getSoftwareTags
static getSoftwareTags( $all=false)
Loads defined core tags, checks for invalid types (if not array), and filters for supported and enabl...
Definition: ChangeTags.php:53
ChangeTagsTest\testGetSoftwareTags
testGetSoftwareTags( $softwareTags, $expected)
dataGetSoftwareTags ChangeTags::getSoftwareTags
Definition: ChangeTagsTest.php:300
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
ChangeTagsTest\dataGetSoftwareTags
static dataGetSoftwareTags()
Definition: ChangeTagsTest.php:247
ChangeTagsTest\testModifyDisplayQuery
testModifyDisplayQuery( $origQuery, $filter_tag, $useTags, $modifiedQuery)
provideModifyDisplayQuery
Definition: ChangeTagsTest.php:11
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:56
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187