019e2570d978567033ddc140ada0024574278ef5
[lhc/web/wiklou.git] / tests / phpunit / includes / changes / ChangesListStringOptionsFilterGroupTest.php
1 <?php
2
3 /**
4 * @covers ChangesListStringOptionsFilterGroup
5 */
6 class ChangesListStringOptionsFilterGroupTest extends MediaWikiTestCase {
7 /**
8 * @expectedException MWException
9 */
10 public function testIsFullCoverage() {
11 $falseGroup = TestingAccessWrapper::newFromObject(
12 new ChangesListStringOptionsFilterGroup( [
13 'name' => 'group',
14 'filters' => [],
15 'isFullCoverage' => false,
16 'queryCallable' => function () {
17 }
18 ] )
19 );
20
21 $this->assertSame(
22 false,
23 $falseGroup->isFullCoverage
24 );
25
26 // Should throw due to missing isFullCoverage
27 $undefinedFullCoverageGroup = new ChangesListStringOptionsFilterGroup( [
28 'name' => 'othergroup',
29 'filters' => [],
30 ] );
31 }
32
33 /**
34 * @param array $filterDefinitions Array of filter definitions
35 * @param array $expectedValues Array of values callback should receive
36 * @param string $input Value in URL
37 *
38 * @dataProvider provideModifyQuery
39 */
40 public function testModifyQuery( $filterDefinitions, $expectedValues, $input ) {
41 $self = $this;
42
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 ( $self, $expectedValues ) {
54 $self->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' => 'bar',
78 'isAllowedCallable' => function () {
79 return false;
80 },
81 ],
82 [
83 'name' => 'baz',
84 ],
85 [
86 'name' => 'goo'
87 ],
88 ];
89
90 return [
91 [
92 $mixedFilters,
93 [ 'baz', 'foo', ],
94 'foo;bar;BaZ;invalid',
95 ],
96
97 [
98 $mixedFilters,
99 [ 'baz', 'foo', 'goo' ],
100 'all',
101 ],
102 ];
103 }
104
105 /**
106 * @param array $filterDefinitions Array of filter definitions
107 * @param string $input Value in URL
108 * @param string $message Message thrown by exception
109 *
110 * @dataProvider provideNoOpModifyQuery
111 */
112 public function testNoOpModifyQuery( $filterDefinitions, $input, $message ) {
113 $noFiltersAllowedCallable = function (
114 $className,
115 $ctx,
116 $dbr,
117 &$tables,
118 &$fields,
119 &$conds,
120 &$query_options,
121 &$join_conds,
122 $actualSelectedValues
123 ) use ( $message ) {
124 throw new MWException( $message );
125 };
126
127 $groupDefinition = [
128 'name' => 'group',
129 'default' => '',
130 'isFullCoverage' => true,
131 'filters' => $filterDefinitions,
132 'queryCallable' => $noFiltersAllowedCallable,
133 ];
134
135 $this->modifyQueryHelper( $groupDefinition, $input );
136
137 $this->assertTrue(
138 true,
139 'Test successfully completed without calling queryCallable'
140 );
141 }
142
143 public function provideNoOpModifyQuery() {
144 $isAllowedFalse = [
145 'isAllowedCallable' => function () {
146 return false;
147 },
148 ];
149
150 $allDisallowedFilters = [
151 [
152 'name' => 'disallowed1',
153 ] + $isAllowedFalse,
154
155 [
156 'name' => 'disallowed2',
157 ] + $isAllowedFalse,
158
159 [
160 'name' => 'disallowed3',
161 ] + $isAllowedFalse,
162 ];
163
164 $normalFilters = [
165 [
166 'name' => 'foo',
167 ],
168 [
169 'name' => 'bar',
170 ]
171 ];
172
173 return [
174 [
175 $allDisallowedFilters,
176 'disallowed1;disallowed3',
177 'The queryCallable should not be called if no filters are allowed',
178 ],
179
180 [
181 $normalFilters,
182 '',
183 'The queryCallable should not be called if no filters are selected',
184 ],
185
186 [
187 $normalFilters,
188 'invalid1',
189 'The queryCallable should not be called if no valid filters are selected',
190 ],
191 ];
192 }
193
194 protected function getSpecialPage() {
195 return $this->getMockBuilder( 'ChangesListSpecialPage' )
196 ->setConstructorArgs( [
197 'ChangesListSpecialPage',
198 '',
199 ] )
200 ->getMockForAbstractClass();
201 }
202
203 /**
204 * @param array $groupDefinition Group definition
205 * @param string $input Value in URL
206 *
207 * @dataProvider provideModifyQuery
208 */
209 protected function modifyQueryHelper( $groupDefinition, $input ) {
210 $ctx = $this->getMock( 'IContextSource' );
211 $dbr = $this->getMock( 'IDatabase' );
212 $tables = $fields = $conds = $query_options = $join_conds = [];
213
214 $group = new ChangesListStringOptionsFilterGroup( $groupDefinition );
215
216 $specialPage = $this->getSpecialPage();
217
218 $group->modifyQuery(
219 $dbr,
220 $specialPage,
221 $tables,
222 $fields,
223 $conds,
224 $query_options,
225 $join_conds,
226 $input
227 );
228 }
229
230 public function testGetJsData() {
231 $definition = [
232 'name' => 'some-group',
233 'title' => 'some-group-title',
234 'default' => 'foo',
235 'priority' => 1,
236 'isFullCoverage' => false,
237 'queryCallable' => function () {
238 },
239 'filters' => [
240 [
241 'name' => 'foo',
242 'label' => 'foo-label',
243 'description' => 'foo-description',
244 'priority' => 2,
245 ],
246 [
247 'name' => 'bar',
248 'label' => 'bar-label',
249 'description' => 'bar-description',
250 'priority' => 4,
251 ]
252 ],
253 ];
254
255 $group = new ChangesListStringOptionsFilterGroup( $definition );
256
257 $specialPage = $this->getSpecialPage();
258
259 $this->assertArrayEquals(
260 [
261 'name' => 'some-group',
262 'title' => 'some-group-title',
263 'type' => ChangesListStringOptionsFilterGroup::TYPE,
264 'default' => 'foo',
265 'priority' => 1,
266 'fullCoverage' => false,
267 'filters' => [
268 [
269 'name' => 'bar',
270 'label' => 'bar-label',
271 'description' => 'bar-description',
272 'priority' => 4,
273 'cssClass' => null,
274 'conflicts' => [],
275 'subset' => [],
276 ],
277 [
278 'name' => 'foo',
279 'label' => 'foo-label',
280 'description' => 'foo-description',
281 'priority' => 2,
282 'cssClass' => null,
283 'conflicts' => [],
284 'subset' => [],
285 ],
286 ],
287 'conflicts' => [],
288 'separator' => ';',
289 'messageKeys' => [
290 'some-group-title',
291 'bar-label',
292 'bar-description',
293 'foo-label',
294 'foo-description',
295 ],
296 ],
297 $group->getJsData( $specialPage ),
298 /** ordered= */ false,
299 /** named= */ true
300 );
301 }
302 }