MediaWiki  1.29.2
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  $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 
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 
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',
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(), false, true
276  );
277  }
278 }
MediaWikiTestCase\assertArrayEquals
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
Definition: MediaWikiTestCase.php:1498
$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:990
ChangesListStringOptionsFilterGroupTest\testGetJsData
testGetJsData()
Definition: ChangesListStringOptionsFilterGroupTest.php:208
ChangesListStringOptionsFilterGroupTest\testModifyQuery
testModifyQuery( $filterDefinitions, $expectedValues, $input)
Definition: ChangesListStringOptionsFilterGroupTest.php:42
ChangesListStringOptionsFilterGroupTest\getSpecialPage
getSpecialPage()
Definition: ChangesListStringOptionsFilterGroupTest.php:172
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:13
$self
$self
Definition: doMaintenance.php:56
$dbr
if(! $regexes) $dbr
Definition: cleanup.php:94
ChangesListStringOptionsFilterGroupTest\provideModifyQuery
provideModifyQuery()
Definition: ChangesListStringOptionsFilterGroupTest.php:73
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:1956
ChangesListStringOptionsFilterGroup
Represents a filter group with multiple string options.
Definition: ChangesListStringOptionsFilterGroup.php:39
ChangesListStringOptionsFilterGroupTest\modifyQueryHelper
modifyQueryHelper( $groupDefinition, $input)
Definition: ChangesListStringOptionsFilterGroupTest.php:187
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:139
ChangesListStringOptionsFilterGroupTest\testNoOpModifyQuery
testNoOpModifyQuery( $filterDefinitions, $input, $message)
Definition: ChangesListStringOptionsFilterGroupTest.php:108