MediaWiki REL1_31
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::class )
172 ->setConstructorArgs( [
173 'ChangesListSpecialPage',
174 '',
175 ] )
176 ->getMockForAbstractClass();
177 }
178
183 protected function modifyQueryHelper( $groupDefinition, $input ) {
184 $ctx = $this->createMock( IContextSource::class );
185 $dbr = $this->createMock( Wikimedia\Rdbms\IDatabase::class );
186 $tables = $fields = $conds = $query_options = $join_conds = [];
187
188 $group = new ChangesListStringOptionsFilterGroup( $groupDefinition );
189
190 $specialPage = $this->getSpecialPage();
191 $opts = new FormOptions();
192 $opts->add( $groupDefinition[ 'name' ], $input );
193
194 $group->modifyQuery(
195 $dbr,
196 $specialPage,
197 $tables,
198 $fields,
199 $conds,
200 $query_options,
201 $join_conds,
202 $opts,
203 true
204 );
205 }
206
207 public function testGetJsData() {
208 $definition = [
209 'name' => 'some-group',
210 'title' => 'some-group-title',
211 'default' => 'foo',
212 'priority' => 1,
213 'isFullCoverage' => false,
214 'queryCallable' => function () {
215 },
216 'filters' => [
217 [
218 'name' => 'foo',
219 'label' => 'foo-label',
220 'description' => 'foo-description',
221 'priority' => 2,
222 ],
223 [
224 'name' => 'bar',
225 'label' => 'bar-label',
226 'description' => 'bar-description',
227 'priority' => 4,
228 ]
229 ],
230 ];
231
232 $group = new ChangesListStringOptionsFilterGroup( $definition );
233
234 $this->assertArrayEquals(
235 [
236 'name' => 'some-group',
237 'title' => 'some-group-title',
239 'default' => 'foo',
240 'priority' => 1,
241 'fullCoverage' => false,
242 'filters' => [
243 [
244 'name' => 'bar',
245 'label' => 'bar-label',
246 'description' => 'bar-description',
247 'priority' => 4,
248 'cssClass' => null,
249 'conflicts' => [],
250 'subset' => [],
251 'defaultHighlightColor' => null,
252 ],
253 [
254 'name' => 'foo',
255 'label' => 'foo-label',
256 'description' => 'foo-description',
257 'priority' => 2,
258 'cssClass' => null,
259 'conflicts' => [],
260 'subset' => [],
261 'defaultHighlightColor' => null,
262 ],
263 ],
264 'conflicts' => [],
265 'separator' => ';',
266 'messageKeys' => [
267 'some-group-title',
268 'bar-label',
269 'bar-description',
270 'foo-label',
271 'foo-description',
272 ],
273 ],
274 $group->getJsData(),
false, true
277 );
278 }
279}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
testModifyQuery( $filterDefinitions, $expectedValues, $input)
Represents a filter group with multiple string options.
Helper class to keep track of options when mixing links and form elements.
MediaWiki exception.
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
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:1015
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
if(is_array($mode)) switch( $mode) $input