MediaWiki  1.30.0
ChangesListStringOptionsFilterGroupTest.php
Go to the documentation of this file.
1 <?php
2 
3 use 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(), false, true
274  );
275  }
276 }
MediaWikiTestCase\assertArrayEquals
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
Definition: MediaWikiTestCase.php:1552
$tables
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:988
ChangesListStringOptionsFilterGroupTest\testGetJsData
testGetJsData()
Definition: ChangesListStringOptionsFilterGroupTest.php:206
ChangesListStringOptionsFilterGroupTest\testModifyQuery
testModifyQuery( $filterDefinitions, $expectedValues, $input)
Definition: ChangesListStringOptionsFilterGroupTest.php:42
ChangesListStringOptionsFilterGroupTest\getSpecialPage
getSpecialPage()
Definition: ChangesListStringOptionsFilterGroupTest.php:170
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
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:35
MWException
MediaWiki exception.
Definition: MWException.php:26
ChangesListStringOptionsFilterGroupTest
ChangesListStringOptionsFilterGroup.
Definition: ChangesListStringOptionsFilterGroupTest.php:8
$input
if(is_array( $mode)) switch( $mode) $input
Definition: postprocess-phan.php:141
MediaWikiTestCase
Definition: MediaWikiTestCase.php:15
$dbr
if(! $regexes) $dbr
Definition: cleanup.php:94
ChangesListStringOptionsFilterGroupTest\provideModifyQuery
provideModifyQuery()
Definition: ChangesListStringOptionsFilterGroupTest.php:71
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:1965
ChangesListStringOptionsFilterGroup
Represents a filter group with multiple string options.
Definition: ChangesListStringOptionsFilterGroup.php:39
ChangesListStringOptionsFilterGroupTest\modifyQueryHelper
modifyQueryHelper( $groupDefinition, $input)
Definition: ChangesListStringOptionsFilterGroupTest.php:185
ChangesListStringOptionsFilterGroupTest\testIsFullCoverage
testIsFullCoverage()
MWException.
Definition: ChangesListStringOptionsFilterGroupTest.php:12
ChangesListStringOptionsFilterGroup\TYPE
const TYPE
Type marker, used by JavaScript.
Definition: ChangesListStringOptionsFilterGroup.php:43
ChangesListStringOptionsFilterGroupTest\provideNoOpModifyQuery
provideNoOpModifyQuery()
Definition: ChangesListStringOptionsFilterGroupTest.php:137
ChangesListStringOptionsFilterGroupTest\testNoOpModifyQuery
testNoOpModifyQuery( $filterDefinitions, $input, $message)
Definition: ChangesListStringOptionsFilterGroupTest.php:106