Debian Codegraph @

tests/phpunit/unit/includes/user/Options/ConditionalDefaultsLookupTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\User\Options;
4
5 use MediaWiki\Config\HashConfig;
6 use MediaWiki\Config\ServiceOptions;
7 use MediaWiki\MainConfigNames;
8 use MediaWiki\User\Options\ConditionalDefaultsLookup;
9 use MediaWiki\User\Registration\UserRegistrationLookup;
10 use MediaWiki\User\UserGroupManager;
11 use MediaWiki\User\UserIdentityUtils;
12 use MediaWiki\User\UserIdentityValue;
13 use MediaWikiUnitTestCase;
14
15 /**
16 * @coversDefaultClass \MediaWiki\User\Options\ConditionalDefaultsLookup
17 */
18 class ConditionalDefaultsLookupTest extends MediaWikiUnitTestCase {
19
20 private const CONDITIONAL_USER_DEFAULTS = [
21 [
22 'new accounts',
23 [ CUDCOND_AFTER, '20231215000000' ],
24 ],
25 [
26 'anonymous users',
27 [ CUDCOND_ANON ],
28 ],
29 [
30 'named users',
31 [ CUDCOND_NAMED ],
32 ],
33 [
34 'sysop users',
35 [ CUDCOND_USERGROUP, 'sysop' ],
36 ],
37 ];
38 private const CONDITIONAL_USER_DEFAULTS_AFTER = [
39 [
40 'new accounts',
41 [ CUDCOND_AFTER, '20231215000000' ],
42 ]
43 ];
44 private const CONDITIONAL_USER_DEFAULTS_ANON = [
45 [
46 'anonymous users',
47 [ CUDCOND_ANON ],
48 ]
49 ];
50 private const CONDITIONAL_USER_DEFAULTS_NAMED = [
51 [
52 'named users',
53 [ CUDCOND_NAMED ],
54 ]
55 ];
56 private const CONDITIONAL_USER_DEFAULTS_USERGROUP = [
57 [
58 'sysop users',
59 [ CUDCOND_USERGROUP, 'sysop' ],
60 ]
61 ];
62
63 /**
64 * Construct ServiceOptions using a HashConfig
65 *
66 * @param array $configOverrides
67 * @return ServiceOptions
68 */
69 private function getServiceOptions( array $configOverrides = [] ) {
70 return new ServiceOptions(
71 ConditionalDefaultsLookup::CONSTRUCTOR_OPTIONS,
72 new HashConfig( $configOverrides + [
73 MainConfigNames::ConditionalUserOptions => [],
74 ] )
75 );
76 }
77
78 /**
79 * @covers ::hasConditionalDefault
80 * @dataProvider provideIsConditionallyDefault
81 * @param bool $expected
82 * @param string $option
83 * @return void
84 */
85 public function testIsConditionallyDefault( bool $expected, string $option ) {
86 $lookup = new ConditionalDefaultsLookup(
87 $this->getServiceOptions( [
88 MainConfigNames::ConditionalUserOptions => [
89 'foo-option' => self::CONDITIONAL_USER_DEFAULTS,
90 'bar-option' => [
91 [ 'all accounts' ]
92 ],
93 ]
94 ] ),
95 $this->createNoOpMock( UserRegistrationLookup::class ),
96 $this->createNoOpMock( UserIdentityUtils::class ),
97 static function () {
98 }
99 );
100
101 $this->assertSame(
102 $expected,
103 $lookup->hasConditionalDefault( $option )
104 );
105 }
106
107 public static function provideIsConditionallyDefault() {
108 return [
109 'foo-option' => [ true, 'foo-option' ],
110 'bar-option' => [ true, 'bar-option' ],
111 'some-option' => [ false, 'some-option' ],
112 ];
113 }
114
115 /**
116 * @covers ::getOptionDefaultForUser
117 */
118 public function testGetOptionDefaultForUser__notConditionallyDefault() {
119 $lookup = new ConditionalDefaultsLookup(
120 $this->getServiceOptions(),
121 $this->createNoOpMock( UserRegistrationLookup::class ),
122 $this->createNoOpMock( UserIdentityUtils::class ),
123 static function () {
124 }
125 );
126
127 $this->assertNull( $lookup->getOptionDefaultForUser(
128 'foo-option',
129 new UserIdentityValue( 1, 'Admin' )
130 ) );
131 }
132
133 /**
134 * @covers ::getOptionDefaultForUser
135 * @covers ::checkConditionsForUser
136 * @covers ::checkConditionForUser
137 * @dataProvider provideGetOptionDefaultForUser__registration
138 * @param string|null $expected
139 * @param string|null $registrationTS
140 * @param array $conditions
141 */
142 public function testGetOptionDefaultForUser__registration(
143 ?string $expected,
144 ?string $registrationTS,
145 array $conditions
146 ) {
147 $userIdentity = new UserIdentityValue( 1, 'User' );
148
149 $registrationLookup = $this->createMock( UserRegistrationLookup::class );
150 $registrationLookup->expects( $this->once() )
151 ->method( 'getRegistration' )
152 ->with( $userIdentity )
153 ->willReturn( $registrationTS );
154
155 $lookup = new ConditionalDefaultsLookup(
156 $this->getServiceOptions( [
157 MainConfigNames::ConditionalUserOptions => [
158 'foo-option' => $conditions,
159 ]
160 ] ),
161 $registrationLookup,
162 $this->createNoOpMock( UserIdentityUtils::class ),
163 static function () {
164 }
165 );
166
167 $this->assertSame(
168 $expected,
169 $lookup->getOptionDefaultForUser( 'foo-option', $userIdentity )
170 );
171 }
172
173 public static function provideGetOptionDefaultForUser__registration(): array {
174 return [
175 [ null, '20231101000000', self::CONDITIONAL_USER_DEFAULTS_AFTER ],
176 [ 'new accounts', '20241101000000', self::CONDITIONAL_USER_DEFAULTS_AFTER ],
177 [ null, null, self::CONDITIONAL_USER_DEFAULTS_AFTER ],
178 ];
179 }
180
181 /**
182 * @covers ::getOptionDefaultForUser
183 * @covers ::checkConditionsForUser
184 * @covers ::checkConditionForUser
185 * @dataProvider provideGetOptionDefaultForUser__anon
186 * @param int $id the user ID
187 * @param string|null $expected the default option or null if none apply
188 */
189 public function testGetOptionDefaultForUser__anon( int $id, ?string $expected ) {
190 $userIdentity = new UserIdentityValue( $id, 'test user' );
191
192 $options = $this->getServiceOptions( [
193 MainConfigNames::ConditionalUserOptions => [
194 'test-option' => self::CONDITIONAL_USER_DEFAULTS_ANON,
195 ]
196 ] );
197 $registrationLookup = $this->createNoOpMock( UserRegistrationLookup::class );
198 $userIdentityUtils = $this->createNoOpMock( UserIdentityUtils::class );
199
200 $lookup = new ConditionalDefaultsLookup( $options, $registrationLookup, $userIdentityUtils,
201 static function () {
202 } );
203
204 $this->assertSame( $expected, $lookup->getOptionDefaultForUser( 'test-option', $userIdentity ) );
205 }
206
207 public static function provideGetOptionDefaultForUser__anon(): array {
208 return [
209 [ 0, 'anonymous users' ],
210 [ 1, null ],
211 ];
212 }
213
214 /**
215 * @covers ::getOptionDefaultForUser
216 * @covers ::checkConditionsForUser
217 * @covers ::checkConditionForUser
218 * @dataProvider provideGetOptionDefaultForUser__named
219 * @param bool $isNamed whether the user is named or not (logged in and not temporary)
220 * @param string|null $expected the default option or null if none apply
221 */
222 public function testGetOptionDefaultForUser__named( bool $isNamed, ?string $expected ) {
223 $userIdentity = new UserIdentityValue( 1, 'test user' );
224
225 $options = $this->getServiceOptions( [
226 MainConfigNames::ConditionalUserOptions => [
227 'test-option' => self::CONDITIONAL_USER_DEFAULTS_NAMED,
228 ]
229 ] );
230 $registrationLookup = $this->createNoOpMock( UserRegistrationLookup::class );
231 $userIdentityUtils = $this->createMock( UserIdentityUtils::class );
232 $userIdentityUtils->expects( $this->once() )
233 ->method( 'isNamed' )
234 ->with( $userIdentity )
235 ->willReturn( $isNamed );
236
237 $lookup = new ConditionalDefaultsLookup( $options, $registrationLookup, $userIdentityUtils,
238 static function () {
239 } );
240
241 $this->assertSame( $expected, $lookup->getOptionDefaultForUser( 'test-option', $userIdentity ) );
242 }
243
244 public static function provideGetOptionDefaultForUser__named(): array {
245 return [
246 [ true, 'named users' ],
247 [ false, null ],
248 ];
249 }
250
251 /**
252 * @covers ::getOptionDefaultForUser
253 * @covers ::checkConditionsForUser
254 * @covers ::checkConditionForUser
255 * @dataProvider provideGetOptionDefaultForUser__usergroup
256 * @param array[string] $usergroups the user groups the user has
257 * @param string|null $expected the default option or null if none apply
258 */
259 public function testGetOptionDefaultForUser__usergroup( array $usergroups, ?string $expected ) {
260 $userIdentity = new UserIdentityValue( 1, 'test user' );
261
262 $options = $this->getServiceOptions( [
263 MainConfigNames::ConditionalUserOptions => [
264 'test-option' => self::CONDITIONAL_USER_DEFAULTS_USERGROUP,
265 ]
266 ] );
267 $registrationLookup = $this->createNoOpMock( UserRegistrationLookup::class );
268 $userIdentityUtils = $this->createMock( UserIdentityUtils::class );
269 $userGroupManager = $this->createMock( UserGroupManager::class );
270 $userGroupManager->expects( $this->once() )
271 ->method( 'getUserEffectiveGroups' )
272 ->with( $userIdentity )
273 ->willReturn( $usergroups );
274
275 $lookup = new ConditionalDefaultsLookup( $options, $registrationLookup, $userIdentityUtils,
276 static function () use ( $userGroupManager ) {
277 return $userGroupManager;
278 } );
279
280 $this->assertSame( $expected, $lookup->getOptionDefaultForUser( 'test-option', $userIdentity ) );
281 }
282
283 public static function provideGetOptionDefaultForUser__usergroup(): array {
284 return [
285 [ [ '*', 'user', 'sysop' ], 'sysop users' ],
286 [ [ 'user' ], null ],
287 ];
288 }
289
290 /**
291 * @covers ::getOptionDefaultForUser
292 * @covers ::checkConditionsForUser
293 * @covers ::checkConditionForUser
294 * @dataProvider provideGetOptionDefaultForUser__extraCondition
295 * @param array $configConditions An array of condition descriptors as described for $wgConditionalUserOptions
296 * @param array $extraConditions Key is the condition name and value is a callable
297 * @param string|null $expected the default option or null if none apply
298 */
299 public function testGetOptionDefaultForUser__extraCondition( array $configConditions, array $extraConditions, ?string $expected ) {
300 $userIdentity = new UserIdentityValue( 1, 'test user' );
301
302 $options = $this->getServiceOptions( [
303 MainConfigNames::ConditionalUserOptions => [
304 'test-option' => $configConditions
305 ]
306 ] );
307 $registrationLookup = $this->createNoOpMock( UserRegistrationLookup::class );
308 $userIdentityUtils = $this->createMock( UserIdentityUtils::class );
309
310 $lookup = new ConditionalDefaultsLookup( $options, $registrationLookup, $userIdentityUtils,
311 static function () {
312 },
313 $extraConditions
314 );
315
316 $this->assertSame( $expected, $lookup->getOptionDefaultForUser( 'test-option', $userIdentity ) );
317 }
318
319 public static function provideGetOptionDefaultForUser__extraCondition(): array {
320 return [
321 [
322 [
323 [ 'No', [ 'if-condition', false ] ],
324 [ 'Yes', [ 'if-condition', true ] ],
325 ],
326 [
327 'if-condition' => static function ( $userIdentity, $args ) {
328 return (bool)$args[0];
329 },
330 ],
331 'Yes'
332 ],
333 [
334 [
335 [ 'Yes', [ 'if-condition', true ] ],
336 [ 'No', [ 'if-condition', false ] ],
337 ],
338 [
339 'if-condition' => static function ( $userIdentity, $args ) {
340 return !(bool)$args[0];
341 },
342 ],
343 'No'
344 ]
345 ];
346 }
347 }