MediaWiki REL1_30
ChangesListStringOptionsFilterGroupTest.php
Go to the documentation of this file.
1<?php
2
3use Wikimedia\TestingAccessWrapper;
4
12 public function testIsFullCoverage() {
13 $falseGroup = TestingAccessWrapper::newFromObject(
15 'name' => 'group',
16 'filters' => [],
17 'isFullCoverage' => false,
18 'queryCallable' => function () {
19 }
20 ] )
21 );
22
23 $this->assertSame(
24 false,
25 $falseGroup->isFullCoverage
26 );
27
28 // Should throw due to missing isFullCoverage
29 $undefinedFullCoverageGroup = new ChangesListStringOptionsFilterGroup( [
30 'name' => 'othergroup',
31 'filters' => [],
32 ] );
33 }
34
42 public function testModifyQuery( $filterDefinitions, $expectedValues, $input ) {
43 $queryCallable = function (
44 $className,
45 $ctx,
46 $dbr,
47 &$tables,
48 &$fields,
49 &$conds,
50 &$query_options,
51 &$join_conds,
52 $actualSelectedValues
53 ) use ( $expectedValues ) {
54 $this->assertSame(
55 $expectedValues,
56 $actualSelectedValues
57 );
58 };
59
60 $groupDefinition = [
61 'name' => 'group',
62 'default' => '',
63 'isFullCoverage' => true,
64 'filters' => $filterDefinitions,
65 'queryCallable' => $queryCallable,
66 ];
67
68 $this->modifyQueryHelper( $groupDefinition, $input );
69 }
70
71 public function provideModifyQuery() {
72 $mixedFilters = [
73 [
74 'name' => 'foo',
75 ],
76 [
77 'name' => 'baz',
78 ],
79 [
80 'name' => 'goo'
81 ],
82 ];
83
84 return [
85 [
86 $mixedFilters,
87 [ 'baz', 'foo', ],
88 'foo;bar;BaZ;invalid',
89 ],
90
91 [
92 $mixedFilters,
93 [ 'baz', 'foo', 'goo' ],
94 'all',
95 ],
96 ];
97 }
98
106 public function testNoOpModifyQuery( $filterDefinitions, $input, $message ) {
107 $noFiltersAllowedCallable = function (
108 $className,
109 $ctx,
110 $dbr,
111 &$tables,
112 &$fields,
113 &$conds,
114 &$query_options,
115 &$join_conds,
116 $actualSelectedValues
117 ) use ( $message ) {
118 throw new MWException( $message );
119 };
120
121 $groupDefinition = [
122 'name' => 'group',
123 'default' => '',
124 'isFullCoverage' => true,
125 'filters' => $filterDefinitions,
126 'queryCallable' => $noFiltersAllowedCallable,
127 ];
128
129 $this->modifyQueryHelper( $groupDefinition, $input );
130
131 $this->assertTrue(
132 true,
133 'Test successfully completed without calling queryCallable'
134 );
135 }
136
137 public function provideNoOpModifyQuery() {
138 $noFilters = [];
139
140 $normalFilters = [
141 [
142 'name' => 'foo',
143 ],
144 [
145 'name' => 'bar',
146 ]
147 ];
148
149 return [
150 [
151 $noFilters,
152 'disallowed1;disallowed3',
153 'The queryCallable should not be called if there are no filters',
154 ],
155
156 [
157 $normalFilters,
158 '',
159 'The queryCallable should not be called if no filters are selected',
160 ],
161
162 [
163 $normalFilters,
164 'invalid1',
165 'The queryCallable should not be called if no valid filters are selected',
166 ],
167 ];
168 }
169
170 protected function getSpecialPage() {
171 return $this->getMockBuilder( 'ChangesListSpecialPage' )
172 ->setConstructorArgs( [
173 'ChangesListSpecialPage',
174 '',
175 ] )
176 ->getMockForAbstractClass();
177 }
178
185 protected function modifyQueryHelper( $groupDefinition, $input ) {
186 $ctx = $this->createMock( 'IContextSource' );
187 $dbr = $this->createMock( 'IDatabase' );
188 $tables = $fields = $conds = $query_options = $join_conds = [];
189
190 $group = new ChangesListStringOptionsFilterGroup( $groupDefinition );
191
192 $specialPage = $this->getSpecialPage();
193
194 $group->modifyQuery(
195 $dbr,
196 $specialPage,
197 $tables,
198 $fields,
199 $conds,
200 $query_options,
201 $join_conds,
202 $input
203 );
204 }
205
206 public function testGetJsData() {
207 $definition = [
208 'name' => 'some-group',
209 'title' => 'some-group-title',
210 'default' => 'foo',
211 'priority' => 1,
212 'isFullCoverage' => false,
213 'queryCallable' => function () {
214 },
215 'filters' => [
216 [
217 'name' => 'foo',
218 'label' => 'foo-label',
219 'description' => 'foo-description',
220 'priority' => 2,
221 ],
222 [
223 'name' => 'bar',
224 'label' => 'bar-label',
225 'description' => 'bar-description',
226 'priority' => 4,
227 ]
228 ],
229 ];
230
231 $group = new ChangesListStringOptionsFilterGroup( $definition );
232
233 $this->assertArrayEquals(
234 [
235 'name' => 'some-group',
236 'title' => 'some-group-title',
238 'default' => 'foo',
239 'priority' => 1,
240 'fullCoverage' => false,
241 'filters' => [
242 [
243 'name' => 'bar',
244 'label' => 'bar-label',
245 'description' => 'bar-description',
246 'priority' => 4,
247 'cssClass' => null,
248 'conflicts' => [],
249 'subset' => [],
250 ],
251 [
252 'name' => 'foo',
253 'label' => 'foo-label',
254 'description' => 'foo-description',
255 'priority' => 2,
256 'cssClass' => null,
257 'conflicts' => [],
258 'subset' => [],
259 ],
260 ],
261 'conflicts' => [],
262 'separator' => ';',
263 'messageKeys' => [
264 'some-group-title',
265 'bar-label',
266 'bar-description',
267 'foo-label',
268 'foo-description',
269 ],
270 ],
271 $group->getJsData(),
272 false,
273 true
274 );
275 }
276}
testModifyQuery( $filterDefinitions, $expectedValues, $input)
Represents a filter group with multiple string options.
MediaWiki exception.
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
if(! $regexes) $dbr
Definition cleanup.php:94
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist & $tables
Definition hooks.txt:1013
if(is_array($mode)) switch( $mode) $input