Merge "Add 3D filetype for STL files"
[lhc/web/wiklou.git] / tests / phpunit / includes / changes / ChangesListStringOptionsFilterGroupTest.php
1 <?php
2
3 use Wikimedia\TestingAccessWrapper;
4
5 /**
6 * @covers ChangesListStringOptionsFilterGroup
7 */
8 class ChangesListStringOptionsFilterGroupTest extends MediaWikiTestCase {
9 /**
10 * @expectedException MWException
11 */
12 public function testIsFullCoverage() {
13 $falseGroup = TestingAccessWrapper::newFromObject(
14 new ChangesListStringOptionsFilterGroup( [
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
35 /**
36 * @param array $filterDefinitions Array of filter definitions
37 * @param array $expectedValues Array of values callback should receive
38 * @param string $input Value in URL
39 *
40 * @dataProvider provideModifyQuery
41 */
42 public function testModifyQuery( $filterDefinitions, $expectedValues, $input ) {
43 $self = $this;
44
45 $queryCallable = function (
46 $className,
47 $ctx,
48 $dbr,
49 &$tables,
50 &$fields,
51 &$conds,
52 &$query_options,
53 &$join_conds,
54 $actualSelectedValues
55 ) use ( $self, $expectedValues ) {
56 $self->assertSame(
57 $expectedValues,
58 $actualSelectedValues
59 );
60 };
61
62 $groupDefinition = [
63 'name' => 'group',
64 'default' => '',
65 'isFullCoverage' => true,
66 'filters' => $filterDefinitions,
67 'queryCallable' => $queryCallable,
68 ];
69
70 $this->modifyQueryHelper( $groupDefinition, $input );
71 }
72
73 public function provideModifyQuery() {
74 $mixedFilters = [
75 [
76 'name' => 'foo',
77 ],
78 [
79 'name' => 'baz',
80 ],
81 [
82 'name' => 'goo'
83 ],
84 ];
85
86 return [
87 [
88 $mixedFilters,
89 [ 'baz', 'foo', ],
90 'foo;bar;BaZ;invalid',
91 ],
92
93 [
94 $mixedFilters,
95 [ 'baz', 'foo', 'goo' ],
96 'all',
97 ],
98 ];
99 }
100
101 /**
102 * @param array $filterDefinitions Array of filter definitions
103 * @param string $input Value in URL
104 * @param string $message Message thrown by exception
105 *
106 * @dataProvider provideNoOpModifyQuery
107 */
108 public function testNoOpModifyQuery( $filterDefinitions, $input, $message ) {
109 $noFiltersAllowedCallable = function (
110 $className,
111 $ctx,
112 $dbr,
113 &$tables,
114 &$fields,
115 &$conds,
116 &$query_options,
117 &$join_conds,
118 $actualSelectedValues
119 ) use ( $message ) {
120 throw new MWException( $message );
121 };
122
123 $groupDefinition = [
124 'name' => 'group',
125 'default' => '',
126 'isFullCoverage' => true,
127 'filters' => $filterDefinitions,
128 'queryCallable' => $noFiltersAllowedCallable,
129 ];
130
131 $this->modifyQueryHelper( $groupDefinition, $input );
132
133 $this->assertTrue(
134 true,
135 'Test successfully completed without calling queryCallable'
136 );
137 }
138
139 public function provideNoOpModifyQuery() {
140 $noFilters = [];
141
142 $normalFilters = [
143 [
144 'name' => 'foo',
145 ],
146 [
147 'name' => 'bar',
148 ]
149 ];
150
151 return [
152 [
153 $noFilters,
154 'disallowed1;disallowed3',
155 'The queryCallable should not be called if there are no filters',
156 ],
157
158 [
159 $normalFilters,
160 '',
161 'The queryCallable should not be called if no filters are selected',
162 ],
163
164 [
165 $normalFilters,
166 'invalid1',
167 'The queryCallable should not be called if no valid filters are selected',
168 ],
169 ];
170 }
171
172 protected function getSpecialPage() {
173 return $this->getMockBuilder( 'ChangesListSpecialPage' )
174 ->setConstructorArgs( [
175 'ChangesListSpecialPage',
176 '',
177 ] )
178 ->getMockForAbstractClass();
179 }
180
181 /**
182 * @param array $groupDefinition Group definition
183 * @param string $input Value in URL
184 *
185 * @dataProvider provideModifyQuery
186 */
187 protected function modifyQueryHelper( $groupDefinition, $input ) {
188 $ctx = $this->createMock( 'IContextSource' );
189 $dbr = $this->createMock( 'IDatabase' );
190 $tables = $fields = $conds = $query_options = $join_conds = [];
191
192 $group = new ChangesListStringOptionsFilterGroup( $groupDefinition );
193
194 $specialPage = $this->getSpecialPage();
195
196 $group->modifyQuery(
197 $dbr,
198 $specialPage,
199 $tables,
200 $fields,
201 $conds,
202 $query_options,
203 $join_conds,
204 $input
205 );
206 }
207
208 public function testGetJsData() {
209 $definition = [
210 'name' => 'some-group',
211 'title' => 'some-group-title',
212 'default' => 'foo',
213 'priority' => 1,
214 'isFullCoverage' => false,
215 'queryCallable' => function () {
216 },
217 'filters' => [
218 [
219 'name' => 'foo',
220 'label' => 'foo-label',
221 'description' => 'foo-description',
222 'priority' => 2,
223 ],
224 [
225 'name' => 'bar',
226 'label' => 'bar-label',
227 'description' => 'bar-description',
228 'priority' => 4,
229 ]
230 ],
231 ];
232
233 $group = new ChangesListStringOptionsFilterGroup( $definition );
234
235 $this->assertArrayEquals(
236 [
237 'name' => 'some-group',
238 'title' => 'some-group-title',
239 'type' => ChangesListStringOptionsFilterGroup::TYPE,
240 'default' => 'foo',
241 'priority' => 1,
242 'fullCoverage' => false,
243 'filters' => [
244 [
245 'name' => 'bar',
246 'label' => 'bar-label',
247 'description' => 'bar-description',
248 'priority' => 4,
249 'cssClass' => null,
250 'conflicts' => [],
251 'subset' => [],
252 ],
253 [
254 'name' => 'foo',
255 'label' => 'foo-label',
256 'description' => 'foo-description',
257 'priority' => 2,
258 'cssClass' => null,
259 'conflicts' => [],
260 'subset' => [],
261 ],
262 ],
263 'conflicts' => [],
264 'separator' => ';',
265 'messageKeys' => [
266 'some-group-title',
267 'bar-label',
268 'bar-description',
269 'foo-label',
270 'foo-description',
271 ],
272 ],
273 $group->getJsData(),
274 /** ordered= */ false,
275 /** named= */ true
276 );
277 }
278 }