Debian Codegraph @

tests/phpunit/includes/specialpage/ChangesListSpecialPageTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\SpecialPage;
4
5 use ChangesListBooleanFilterGroup;
6 use ChangesListStringOptionsFilterGroup;
7 use MediaWiki\Context\RequestContext;
8 use MediaWiki\MainConfigNames;
9 use MediaWiki\Request\FauxRequest;
10 use MediaWiki\SpecialPage\ChangesListSpecialPage;
11 use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
12 use MediaWiki\Title\Title;
13 use MediaWiki\User\User;
14 use Wikimedia\Rdbms\Database;
15 use Wikimedia\Rdbms\IExpression;
16 use Wikimedia\TestingAccessWrapper;
17 use Wikimedia\Timestamp\ConvertibleTimestamp;
18
19 /**
20 * Test class for ChangesListSpecialPage class
21 *
22 * Copyright © 2011-, Antoine Musso, Stephane Bisson, Matthew Flaschen
23 *
24 * @author Antoine Musso
25 * @author Stephane Bisson
26 * @author Matthew Flaschen
27 * @group Database
28 *
29 * @covers \MediaWiki\SpecialPage\ChangesListSpecialPage
30 */
31 class ChangesListSpecialPageTest extends AbstractChangesListSpecialPageTestCase {
32
33 use TempUserTestTrait {
34 enableAutoCreateTempUser as _enableAutoCreateTempUser;
35 disableAutoCreateTempUser as _disableAutoCreateTempUser;
36 }
37
38 protected function setUp(): void {
39 parent::setUp();
40 $this->clearHooks();
41 }
42
43 /**
44 * @return ChangesListSpecialPage
45 */
46 protected function getPageAccessWrapper() {
47 $mock = $this->getMockBuilder( ChangesListSpecialPage::class )
48 ->setConstructorArgs(
49 [
50 'ChangesListSpecialPage',
51 '',
52 $this->getServiceContainer()->getUserIdentityUtils(),
53 $this->getServiceContainer()->getTempUserConfig()
54 ]
55 )
56 ->onlyMethods( [ 'getPageTitle' ] )
57 ->getMockForAbstractClass();
58
59 $mock->method( 'getPageTitle' )->willReturn(
60 Title::makeTitle( NS_SPECIAL, 'ChangesListSpecialPage' )
61 );
62
63 $mock = TestingAccessWrapper::newFromObject(
64 $mock
65 );
66
67 return $mock;
68 }
69
70 private function buildQuery(
71 array $requestOptions,
72 ?User $user = null
73 ): array {
74 $context = new RequestContext;
75 $context->setRequest( new FauxRequest( $requestOptions ) );
76 if ( $user ) {
77 $context->setUser( $user );
78 }
79
80 $this->changesListSpecialPage->setContext( $context );
81 $this->changesListSpecialPage->filterGroups = [];
82 $formOptions = $this->changesListSpecialPage->setup( null );
83
84 # Filter out rc_timestamp conditions which depends on the test runtime
85 # This condition is not needed as of march 2, 2011 -- hashar
86 # @todo FIXME: Find a way to generate the correct rc_timestamp
87
88 $tables = [];
89 $fields = [];
90 $queryConditions = [];
91 $query_options = [];
92 $join_conds = [];
93
94 call_user_func_array(
95 [ $this->changesListSpecialPage, 'buildQuery' ],
96 [
97 &$tables,
98 &$fields,
99 &$queryConditions,
100 &$query_options,
101 &$join_conds,
102 $formOptions
103 ]
104 );
105
106 $queryConditions = array_filter(
107 $queryConditions,
108 [ __CLASS__, 'filterOutRcTimestampCondition' ]
109 );
110
111 return $queryConditions;
112 }
113
114 /**
115 * helper to test SpecialRecentchanges::buildQuery()
116 * @param array $expected
117 * @param array $requestOptions
118 * @param string $message
119 * @param User|null $user
120 */
121 private function assertConditions(
122 array $expected,
123 array $requestOptions,
124 string $message,
125 ?User $user = null
126 ) {
127 $queryConditions = $this->buildQuery( $requestOptions, $user );
128
129 $this->assertEquals(
130 $this->normalizeCondition( $expected ),
131 $this->normalizeCondition( $queryConditions ),
132 $message
133 );
134 }
135
136 private function normalizeCondition( array $conds ): array {
137 $dbr = $this->getDb();
138 $normalized = array_map(
139 static function ( $k, $v ) use ( $dbr ) {
140 if ( is_array( $v ) ) {
141 sort( $v );
142 }
143 // (Ab)use makeList() to format only this entry
144 return $dbr->makeList( [ $k => $v ], Database::LIST_AND );
145 },
146 array_keys( $conds ),
147 $conds
148 );
149 sort( $normalized );
150 return $normalized;
151 }
152
153 /**
154 * @param array|string|IExpression $var
155 * @return bool false if condition begins with 'rc_timestamp '
156 */
157 private static function filterOutRcTimestampCondition( $var ): bool {
158 if ( $var instanceof IExpression ) {
159 $var = $var->toGeneralizedSql();
160 }
161 return is_array( $var ) || !str_contains( (string)$var, 'rc_timestamp ' );
162 }
163
164 public function testRcNsFilter() {
165 $this->assertConditions(
166 [ # expected
167 'rc_namespace = 0',
168 ],
169 [
170 'namespace' => NS_MAIN,
171 ],
172 "rc conditions with one namespace"
173 );
174 }
175
176 public function testRcNsFilterInversion() {
177 $this->assertConditions(
178 [ # expected
179 'rc_namespace != 0',
180 ],
181 [
182 'namespace' => NS_MAIN,
183 'invert' => 1,
184 ],
185 "rc conditions with namespace inverted"
186 );
187 }
188
189 public function testRcNsFilterMultiple() {
190 $this->assertConditions(
191 [ # expected
192 'rc_namespace IN (1,2,3)',
193 ],
194 [
195 'namespace' => '1;2;3',
196 ],
197 "rc conditions with multiple namespaces"
198 );
199 }
200
201 public function testRcNsFilterMultipleAssociated() {
202 $this->assertConditions(
203 [ # expected
204 'rc_namespace IN (0,1,4,5,6,7)',
205 ],
206 [
207 'namespace' => '1;4;7',
208 'associated' => 1,
209 ],
210 "rc conditions with multiple namespaces and associated"
211 );
212 }
213
214 public function testRcNsFilterAssociatedSpecial() {
215 $this->assertConditions(
216 [ # expected
217 'rc_namespace IN (-1,0,1)',
218 ],
219 [
220 'namespace' => '1;-1',
221 'associated' => 1,
222 ],
223 "rc conditions with associated and special namespace"
224 );
225 }
226
227 public function testRcNsFilterMultipleAssociatedInvert() {
228 $this->assertConditions(
229 [ # expected
230 'rc_namespace NOT IN (2,3,8,9)',
231 ],
232 [
233 'namespace' => '2;3;9',
234 'associated' => 1,
235 'invert' => 1
236 ],
237 "rc conditions with multiple namespaces, associated and inverted"
238 );
239 }
240
241 public function testRcNsFilterMultipleInvert() {
242 $this->assertConditions(
243 [ # expected
244 'rc_namespace NOT IN (1,2,3)',
245 ],
246 [
247 'namespace' => '1;2;3',
248 'invert' => 1,
249 ],
250 "rc conditions with multiple namespaces inverted"
251 );
252 }
253
254 public function testRcNsFilterAllContents() {
255 $namespaces = $this->getServiceContainer()->getNamespaceInfo()->getSubjectNamespaces();
256 $this->assertConditions(
257 [ # expected
258 'rc_namespace IN (' . $this->getDb()->makeList( $namespaces ) . ')',
259 ],
260 [
261 'namespace' => 'all-contents',
262 ],
263 "rc conditions with all-contents"
264 );
265 }
266
267 public function testRcNsFilterInvalid() {
268 $this->assertConditions(
269 [ # expected
270 ],
271 [
272 'namespace' => 'invalid',
273 ],
274 "rc conditions with invalid namespace"
275 );
276 }
277
278 public function testRcNsFilterPartialInvalid() {
279 $namespaces = array_merge(
280 [ 1 ],
281 $this->getServiceContainer()->getNamespaceInfo()->getSubjectNamespaces()
282 );
283 sort( $namespaces );
284 $this->assertConditions(
285 [ # expected
286 'rc_namespace IN (' . $this->getDb()->makeList( $namespaces ) . ')',
287 ],
288 [
289 'namespace' => 'all-contents;1;invalid',
290 ],
291 "rc conditions with invalid namespace"
292 );
293 }
294
295 public function testRcHidemyselfFilter() {
296 $user = $this->getTestUser()->getUser();
297 $this->assertConditions(
298 [ # expected
299 $this->getDb()->expr( 'actor_name', '!=', $user->getName() ),
300 ],
301 [
302 'hidemyself' => 1,
303 ],
304 "rc conditions: hidemyself=1 (logged in)",
305 $user
306 );
307
308 $user = User::newFromName( '10.11.12.13', false );
309 $this->assertConditions(
310 [ # expected
311 "actor_name != '10.11.12.13'",
312 ],
313 [
314 'hidemyself' => 1,
315 ],
316 "rc conditions: hidemyself=1 (anon)",
317 $user
318 );
319 }
320
321 public function testRcHidebyothersFilter() {
322 $user = $this->getTestUser()->getUser();
323 $this->assertConditions(
324 [ # expected
325 'actor_user' => $user->getId(),
326 ],
327 [
328 'hidebyothers' => 1,
329 ],
330 "rc conditions: hidebyothers=1 (logged in)",
331 $user
332 );
333
334 $user = User::newFromName( '10.11.12.13', false );
335 $this->assertConditions(
336 [ # expected
337 'actor_name' => '10.11.12.13',
338 ],
339 [
340 'hidebyothers' => 1,
341 ],
342 "rc conditions: hidebyothers=1 (anon)",
343 $user
344 );
345 }
346
347 public function testRcHidepageedits() {
348 $this->assertConditions(
349 [ # expected
350 "rc_type != 0",
351 ],
352 [
353 'hidepageedits' => 1,
354 ],
355 "rc conditions: hidepageedits=1"
356 );
357 }
358
359 public function testRcHidenewpages() {
360 $this->assertConditions(
361 [ # expected
362 "rc_type != 1",
363 ],
364 [
365 'hidenewpages' => 1,
366 ],
367 "rc conditions: hidenewpages=1"
368 );
369 }
370
371 public function testRcHidelog() {
372 $this->assertConditions(
373 [ # expected
374 "rc_type != 3",
375 ],
376 [
377 'hidelog' => 1,
378 ],
379 "rc conditions: hidelog=1"
380 );
381 }
382
383 public function testRcHidehumans() {
384 $this->assertConditions(
385 [ # expected
386 'rc_bot' => 1,
387 ],
388 [
389 'hidebots' => 0,
390 'hidehumans' => 1,
391 ],
392 "rc conditions: hidebots=0 hidehumans=1"
393 );
394 }
395
396 public function testRcHidepatrolledDisabledFilter() {
397 $this->overrideConfigValue( MainConfigNames::UseRCPatrol, false );
398 $user = $this->getTestUser()->getUser();
399 $this->assertConditions(
400 [ # expected
401 ],
402 [
403 'hidepatrolled' => 1,
404 ],
405 "rc conditions: hidepatrolled=1 (user not allowed)",
406 $user
407 );
408 }
409
410 public function testRcHideunpatrolledDisabledFilter() {
411 $this->overrideConfigValue( MainConfigNames::UseRCPatrol, false );
412 $user = $this->getTestUser()->getUser();
413 $this->assertConditions(
414 [ # expected
415 ],
416 [
417 'hideunpatrolled' => 1,
418 ],
419 "rc conditions: hideunpatrolled=1 (user not allowed)",
420 $user
421 );
422 }
423
424 public function testRcHidepatrolledFilter() {
425 $user = $this->getTestSysop()->getUser();
426 $this->assertConditions(
427 [ # expected
428 'rc_patrolled' => 0,
429 ],
430 [
431 'hidepatrolled' => 1,
432 ],
433 "rc conditions: hidepatrolled=1",
434 $user
435 );
436 }
437
438 public function testRcHideunpatrolledFilter() {
439 $user = $this->getTestSysop()->getUser();
440 $this->assertConditions(
441 [ # expected
442 'rc_patrolled' => [ 1, 2 ],
443 ],
444 [
445 'hideunpatrolled' => 1,
446 ],
447 "rc conditions: hideunpatrolled=1",
448 $user
449 );
450 }
451
452 public function testRcReviewStatusFilter() {
453 $user = $this->getTestSysop()->getUser();
454 $this->assertConditions(
455 [ # expected
456 'rc_patrolled' => 1,
457 ],
458 [
459 'reviewStatus' => 'manual'
460 ],
461 "rc conditions: reviewStatus=manual",
462 $user
463 );
464 $this->assertConditions(
465 [ # expected
466 'rc_patrolled' => [ 0, 2 ],
467 ],
468 [
469 'reviewStatus' => 'unpatrolled;auto'
470 ],
471 "rc conditions: reviewStatus=unpatrolled;auto",
472 $user
473 );
474 }
475
476 public function testRcHideminorFilter() {
477 $this->assertConditions(
478 [ # expected
479 'rc_minor = 0',
480 ],
481 [
482 'hideminor' => 1,
483 ],
484 "rc conditions: hideminor=1"
485 );
486 }
487
488 public function testRcHidemajorFilter() {
489 $this->assertConditions(
490 [ # expected
491 'rc_minor = 1',
492 ],
493 [
494 'hidemajor' => 1,
495 ],
496 "rc conditions: hidemajor=1"
497 );
498 }
499
500 public function testHideCategorization() {
501 $this->assertConditions(
502 [
503 # expected
504 "rc_type != 6"
505 ],
506 [
507 'hidecategorization' => 1
508 ],
509 "rc conditions: hidecategorization=1"
510 );
511 }
512
513 /** @see TempUserTestTrait::enableAutoCreateTempUser */
514 protected function enableAutoCreateTempUser( array $configOverrides = [] ): void {
515 $this->_enableAutoCreateTempUser( $configOverrides );
516 $this->changesListSpecialPage->setTempUserConfig( $this->getServiceContainer()->getTempUserConfig() );
517 }
518
519 /** @see TempUserTestTrait::disableAutoCreateTempUser */
520 protected function disableAutoCreateTempUser( array $configOverrides = [] ): void {
521 $this->_disableAutoCreateTempUser( $configOverrides );
522 $this->changesListSpecialPage->setTempUserConfig( $this->getServiceContainer()->getTempUserConfig() );
523 }
524
525 public function testRegistrationHideliu() {
526 $this->enableAutoCreateTempUser();
527 $tempUserMatchPattern = $this->getServiceContainer()->getTempUserConfig()
528 ->getMatchCondition( $this->getDb(), 'actor_name', IExpression::LIKE )
529 ->toSql( $this->getDb() );
530 $this->assertConditions(
531 [
532 "((actor_user IS NULL OR $tempUserMatchPattern))",
533 ],
534 [
535 'hideliu' => 1,
536 ],
537 "rc conditions: hideliu=1"
538 );
539 }
540
541 public function testRegistrationHideanons() {
542 $this->enableAutoCreateTempUser();
543 $tempUserMatchPattern = $this->getServiceContainer()->getTempUserConfig()
544 ->getMatchCondition( $this->getDb(), 'actor_name', IExpression::NOT_LIKE )
545 ->toSql( $this->getDb() );
546 $this->assertConditions(
547 [
548 "((actor_user IS NOT NULL AND $tempUserMatchPattern))",
549 ],
550 [
551 'hideanons' => 1,
552 ],
553 "rc conditions: hideanons=1"
554 );
555 }
556
557 public function testFilterUserExpLevelAll() {
558 $this->assertConditions(
559 [
560 # expected
561 ],
562 [
563 'userExpLevel' => 'registered;unregistered;newcomer;learner;experienced',
564 ],
565 "rc conditions: userExpLevel=registered;unregistered;newcomer;learner;experienced"
566 );
567 }
568
569 public function testFilterUserExpLevelRegisteredUnregistered() {
570 $this->assertConditions(
571 [
572 # expected
573 ],
574 [
575 'userExpLevel' => 'registered;unregistered',
576 ],
577 "rc conditions: userExpLevel=registered;unregistered"
578 );
579 }
580
581 public function testFilterUserExpLevelRegisteredUnregisteredLearner() {
582 $this->assertConditions(
583 [
584 # expected
585 ],
586 [
587 'userExpLevel' => 'registered;unregistered;learner',
588 ],
589 "rc conditions: userExpLevel=registered;unregistered;learner"
590 );
591 }
592
593 public function testFilterUserExpLevelAllExperienceLevels() {
594 $this->disableAutoCreateTempUser();
595 $this->assertConditions(
596 [
597 # expected
598 '(actor_user IS NOT NULL)',
599 ],
600 [
601 'userExpLevel' => 'newcomer;learner;experienced',
602 ],
603 "rc conditions: userExpLevel=newcomer;learner;experienced"
604 );
605 }
606
607 public function testFilterUserExpLevelRegistered() {
608 $this->disableAutoCreateTempUser();
609 $this->assertConditions(
610 [
611 # expected
612 '(actor_user IS NOT NULL)',
613 ],
614 [
615 'userExpLevel' => 'registered',
616 ],
617 "rc conditions: userExpLevel=registered"
618 );
619 }
620
621 public function testFilterUserExpLevelRegisteredTempAccountsEnabled() {
622 $this->enableAutoCreateTempUser();
623 $tempUserMatchPattern = $this->getServiceContainer()->getTempUserConfig()
624 ->getMatchCondition( $this->getDb(), 'actor_name', IExpression::NOT_LIKE )
625 ->toSql( $this->getDb() );
626 $this->assertConditions(
627 [
628 # expected
629 "((actor_user IS NOT NULL AND $tempUserMatchPattern))",
630 ],
631 [
632 'userExpLevel' => 'registered',
633 ],
634 "rc conditions: userExpLevel=registered"
635 );
636 }
637
638 public function testFilterUserExpLevelUnregistered() {
639 $this->disableAutoCreateTempUser();
640 $this->assertConditions(
641 [
642 # expected
643 '(actor_user IS NULL)'
644 ],
645 [
646 'userExpLevel' => 'unregistered',
647 ],
648 "rc conditions: userExpLevel=unregistered"
649 );
650 }
651
652 public function testFilterUserExpLevelUnregisteredTempAccountsEnabled() {
653 $this->enableAutoCreateTempUser();
654 $tempUserMatchPattern = $this->getServiceContainer()->getTempUserConfig()
655 ->getMatchCondition( $this->getDb(), 'actor_name', IExpression::LIKE )
656 ->toSql( $this->getDb() );
657 $this->assertConditions(
658 [
659 # expected
660 "((actor_user IS NULL OR $tempUserMatchPattern))",
661 ],
662 [
663 'userExpLevel' => 'unregistered',
664 ],
665 "rc conditions: userExpLevel=unregistered"
666 );
667 }
668
669 public function testFilterUserExpLevelRegisteredOrLearner() {
670 $this->disableAutoCreateTempUser();
671 $this->assertConditions(
672 [
673 # expected
674 '(actor_user IS NOT NULL)',
675 ],
676 [
677 'userExpLevel' => 'registered;learner',
678 ],
679 "rc conditions: userExpLevel=registered;learner"
680 );
681 }
682
683 public function testFilterUserExpLevelLearner() {
684 $this->disableAutoCreateTempUser();
685 ConvertibleTimestamp::setFakeTime( '20201231000000' );
686 $this->assertConditions(
687 [
688 # expected
689 "((actor_user IS NOT NULL AND "
690 . "(user_editcount >= 10 AND (user_registration IS NULL OR user_registration <= '{$this->getDb()->timestamp( '20201227000000' )}')) AND "
691 . "(user_editcount < 500 OR user_registration > '{$this->getDb()->timestamp( '20201201000000' )}')"
692 . "))"
693 ],
694 [
695 'userExpLevel' => 'learner'
696 ],
697 "rc conditions: userExpLevel=learner"
698 );
699 }
700
701 public function testFilterUserExpLevelLearnerWhenTemporaryAccountsEnabled() {
702 $this->enableAutoCreateTempUser();
703 ConvertibleTimestamp::setFakeTime( '20201231000000' );
704
705 $notLikeTempUserMatchExpression = $this->getServiceContainer()->getTempUserConfig()
706 ->getMatchCondition( $this->getDb(), 'actor_name', IExpression::NOT_LIKE )
707 ->toSql( $this->getDb() );
708
709 $this->assertConditions(
710 [
711 # expected
712 "(((actor_user IS NOT NULL AND $notLikeTempUserMatchExpression) AND "
713 . "(user_editcount >= 10 AND (user_registration IS NULL OR user_registration <= '{$this->getDb()->timestamp( '20201227000000' )}')) AND "
714 . "(user_editcount < 500 OR user_registration > '{$this->getDb()->timestamp( '20201201000000' )}')"
715 . "))"
716 ],
717 [
718 'userExpLevel' => 'learner'
719 ],
720 "rc conditions: userExpLevel=learner"
721 );
722 }
723
724 public function testFilterUserExpLevelUnregisteredOrExperienced() {
725 $this->disableAutoCreateTempUser();
726 ConvertibleTimestamp::setFakeTime( '20201231000000' );
727 $this->assertConditions(
728 [
729 # expected
730 "(actor_user IS NULL OR "
731 . "(actor_user IS NOT NULL AND "
732 . "(user_editcount >= 500 AND (user_registration IS NULL OR user_registration <= '{$this->getDb()->timestamp( '20201201000000' )}'))"
733 . "))"
734 ],
735 [
736 'userExpLevel' => 'unregistered;experienced'
737 ],
738 "rc conditions: userExpLevel=unregistered;experienced"
739 );
740 }
741
742 public function testFilterUserExpLevelUnregisteredOrExperiencedWhenTemporaryAccountsEnabled() {
743 $this->enableAutoCreateTempUser();
744 ConvertibleTimestamp::setFakeTime( '20201231000000' );
745
746 $notLikeTempUserMatchExpression = $this->getServiceContainer()->getTempUserConfig()
747 ->getMatchCondition( $this->getDb(), 'actor_name', IExpression::NOT_LIKE )
748 ->toSql( $this->getDb() );
749 $likeTempUserMatchExpression = $this->getServiceContainer()->getTempUserConfig()
750 ->getMatchCondition( $this->getDb(), 'actor_name', IExpression::LIKE )
751 ->toSql( $this->getDb() );
752
753 $this->assertConditions(
754 [
755 # expected
756 "((actor_user IS NULL OR $likeTempUserMatchExpression) OR "
757 . "((actor_user IS NOT NULL AND $notLikeTempUserMatchExpression) AND "
758 . "(user_editcount >= 500 AND (user_registration IS NULL OR user_registration <= '{$this->getDb()->timestamp( '20201201000000' )}'))"
759 . "))"
760 ],
761 [
762 'userExpLevel' => 'unregistered;experienced'
763 ],
764 "rc conditions: userExpLevel=unregistered;experienced"
765 );
766 }
767
768 public function testFilterUserExpLevel() {
769 $now = time();
770 $this->overrideConfigValues( [
771 MainConfigNames::LearnerEdits => 10,
772 MainConfigNames::LearnerMemberSince => 4,
773 MainConfigNames::ExperiencedUserEdits => 500,
774 MainConfigNames::ExperiencedUserMemberSince => 30,
775 ] );
776
777 $this->createUsers( [
778 'Newcomer1' => [ 'edits' => 2, 'days' => 2 ],
779 'Newcomer2' => [ 'edits' => 12, 'days' => 3 ],
780 'Newcomer3' => [ 'edits' => 8, 'days' => 5 ],
781 'Learner1' => [ 'edits' => 15, 'days' => 10 ],
782 'Learner2' => [ 'edits' => 450, 'days' => 20 ],
783 'Learner3' => [ 'edits' => 460, 'days' => 33 ],
784 'Learner4' => [ 'edits' => 525, 'days' => 28 ],
785 'Experienced1' => [ 'edits' => 538, 'days' => 33 ],
786 ], $now );
787
788 // newcomers only
789 $this->assertArrayEquals(
790 [ 'Newcomer1', 'Newcomer2', 'Newcomer3' ],
791 $this->fetchUsers( [ 'newcomer' ], $now )
792 );
793
794 // newcomers and learner
795 $this->assertArrayEquals(
796 [
797 'Newcomer1', 'Newcomer2', 'Newcomer3',
798 'Learner1', 'Learner2', 'Learner3', 'Learner4',
799 ],
800 $this->fetchUsers( [ 'newcomer', 'learner' ], $now )
801 );
802
803 // newcomers and more learner
804 $this->assertArrayEquals(
805 [
806 'Newcomer1', 'Newcomer2', 'Newcomer3',
807 'Experienced1',
808 ],
809 $this->fetchUsers( [ 'newcomer', 'experienced' ], $now )
810 );
811
812 // learner only
813 $this->assertArrayEquals(
814 [ 'Learner1', 'Learner2', 'Learner3', 'Learner4' ],
815 $this->fetchUsers( [ 'learner' ], $now )
816 );
817
818 // more experienced only
819 $this->assertArrayEquals(
820 [ 'Experienced1' ],
821 $this->fetchUsers( [ 'experienced' ], $now )
822 );
823
824 // learner and more experienced
825 $this->assertArrayEquals(
826 [
827 'Learner1', 'Learner2', 'Learner3', 'Learner4',
828 'Experienced1',
829 ],
830 $this->fetchUsers( [ 'learner', 'experienced' ], $now )
831 );
832 }
833
834 private function createUsers( array $specs, int $now ) {
835 $dbw = $this->getDb();
836 foreach ( $specs as $name => $spec ) {
837 User::createNew(
838 $name,
839 [
840 'editcount' => $spec['edits'],
841 'registration' => $dbw->timestamp( $this->daysAgo( $spec['days'], $now ) ),
842 'email' => 'ut',
843 ]
844 );
845 }
846 }
847
848 private function fetchUsers( array $filters, int $now ): array {
849 $tables = [];
850 $conds = [];
851 $fields = [];
852 $query_options = [];
853 $join_conds = [];
854
855 sort( $filters );
856
857 call_user_func_array(
858 [ $this->changesListSpecialPage, 'filterOnUserExperienceLevel' ],
859 [
860 get_class( $this->changesListSpecialPage ),
861 $this->changesListSpecialPage->getContext(),
862 $this->changesListSpecialPage->getDB(),
863 &$tables,
864 &$fields,
865 &$conds,
866 &$query_options,
867 &$join_conds,
868 $filters,
869 $now
870 ]
871 );
872
873 // @todo: This is not at all safe or sensible. It just blindly assumes
874 // nothing in $conds depends on any other tables.
875 $result = $this->getDb()->newSelectQueryBuilder()
876 ->select( 'user_name' )
877 ->from( 'user' )
878 ->leftJoin( 'actor', null, 'actor_user=user_id' )
879 ->where( $conds )
880 ->andWhere( [ 'user_email' => 'ut' ] )
881 ->fetchResultSet();
882
883 $usernames = [];
884 foreach ( $result as $row ) {
885 $usernames[] = $row->user_name;
886 }
887
888 return $usernames;
889 }
890
891 private function daysAgo( int $days, int $now ): int {
892 $secondsPerDay = 86400;
893 return $now - $days * $secondsPerDay;
894 }
895
896 public function testGetStructuredFilterJsData() {
897 $this->changesListSpecialPage->filterGroups = [];
898
899 $definition = [
900 [
901 'name' => 'gub-group',
902 'title' => 'gub-group-title',
903 'class' => ChangesListBooleanFilterGroup::class,
904 'filters' => [
905 [
906 'name' => 'hidefoo',
907 'label' => 'foo-label',
908 'description' => 'foo-description',
909 'default' => true,
910 'showHide' => 'showhidefoo',
911 'priority' => 2,
912 ],
913 [
914 'name' => 'hidebar',
915 'label' => 'bar-label',
916 'description' => 'bar-description',
917 'default' => false,
918 'priority' => 4,
919 ]
920 ],
921 ],
922
923 [
924 'name' => 'des-group',
925 'title' => 'des-group-title',
926 'class' => ChangesListStringOptionsFilterGroup::class,
927 'isFullCoverage' => true,
928 'filters' => [
929 [
930 'name' => 'grault',
931 'label' => 'grault-label',
932 'description' => 'grault-description',
933 ],
934 [
935 'name' => 'garply',
936 'label' => 'garply-label',
937 'description' => 'garply-description',
938 ],
939 ],
940 'queryCallable' => static function () {
941 },
942 'default' => ChangesListStringOptionsFilterGroup::NONE,
943 ],
944
945 [
946 'name' => 'unstructured',
947 'class' => ChangesListBooleanFilterGroup::class,
948 'filters' => [
949 [
950 'name' => 'hidethud',
951 'showHide' => 'showhidethud',
952 'default' => true,
953 ],
954
955 [
956 'name' => 'hidemos',
957 'showHide' => 'showhidemos',
958 'default' => false,
959 ],
960 ],
961 ],
962
963 ];
964
965 $this->changesListSpecialPage->registerFiltersFromDefinitions( $definition );
966
967 $this->assertArrayEquals(
968 [
969 // Filters that only display in the unstructured UI are
970 // are not included, and neither are groups that would
971 // be empty due to the above.
972 'groups' => [
973 [
974 'name' => 'gub-group',
975 'title' => 'gub-group-title',
976 'type' => ChangesListBooleanFilterGroup::TYPE,
977 'priority' => -1,
978 'filters' => [
979 [
980 'name' => 'hidebar',
981 'label' => 'bar-label',
982 'description' => 'bar-description',
983 'default' => false,
984 'priority' => 4,
985 'cssClass' => null,
986 'conflicts' => [],
987 'subset' => [],
988 'defaultHighlightColor' => null
989 ],
990 [
991 'name' => 'hidefoo',
992 'label' => 'foo-label',
993 'description' => 'foo-description',
994 'default' => true,
995 'priority' => 2,
996 'cssClass' => null,
997 'conflicts' => [],
998 'subset' => [],
999 'defaultHighlightColor' => null
1000 ],
1001 ],
1002 'fullCoverage' => true,
1003 'conflicts' => [],
1004 ],
1005
1006 [
1007 'name' => 'des-group',
1008 'title' => 'des-group-title',
1009 'type' => ChangesListStringOptionsFilterGroup::TYPE,
1010 'priority' => -2,
1011 'fullCoverage' => true,
1012 'filters' => [
1013 [
1014 'name' => 'grault',
1015 'label' => 'grault-label',
1016 'description' => 'grault-description',
1017 'cssClass' => null,
1018 'priority' => -2,
1019 'conflicts' => [],
1020 'subset' => [],
1021 'defaultHighlightColor' => null
1022 ],
1023 [
1024 'name' => 'garply',
1025 'label' => 'garply-label',
1026 'description' => 'garply-description',
1027 'cssClass' => null,
1028 'priority' => -3,
1029 'conflicts' => [],
1030 'subset' => [],
1031 'defaultHighlightColor' => null
1032 ],
1033 ],
1034 'conflicts' => [],
1035 'separator' => ';',
1036 'default' => ChangesListStringOptionsFilterGroup::NONE,
1037 ],
1038 ],
1039 'messageKeys' => [
1040 'gub-group-title',
1041 'bar-label',
1042 'bar-description',
1043 'foo-label',
1044 'foo-description',
1045 'des-group-title',
1046 'grault-label',
1047 'grault-description',
1048 'garply-label',
1049 'garply-description',
1050 ],
1051 ],
1052 $this->changesListSpecialPage->getStructuredFilterJsData(),
1053 /** ordered= */ false,
1054 /** named= */ true
1055 );
1056 }
1057
1058 public function provideParseParameters() {
1059 return [
1060 [ 'hidebots', [ 'hidebots' => true ] ],
1061
1062 [ 'bots', [ 'hidebots' => false ] ],
1063
1064 [ 'hideminor', [ 'hideminor' => true ] ],
1065
1066 [ 'minor', [ 'hideminor' => false ] ],
1067
1068 [ 'hidemajor', [ 'hidemajor' => true ] ],
1069
1070 [ 'hideliu', [ 'hideliu' => true ] ],
1071
1072 [ 'hidepatrolled', [ 'hidepatrolled' => true ] ],
1073
1074 [ 'hideunpatrolled', [ 'hideunpatrolled' => true ] ],
1075
1076 [ 'hideanons', [ 'hideanons' => true ] ],
1077
1078 [ 'hidemyself', [ 'hidemyself' => true ] ],
1079
1080 [ 'hidebyothers', [ 'hidebyothers' => true ] ],
1081
1082 [ 'hidehumans', [ 'hidehumans' => true ] ],
1083
1084 [ 'hidepageedits', [ 'hidepageedits' => true ] ],
1085
1086 [ 'pagedits', [ 'hidepageedits' => false ] ],
1087
1088 [ 'hidenewpages', [ 'hidenewpages' => true ] ],
1089
1090 [ 'hidecategorization', [ 'hidecategorization' => true ] ],
1091
1092 [ 'hidelog', [ 'hidelog' => true ] ],
1093
1094 [
1095 'userExpLevel=learner;experienced',
1096 [
1097 'userExpLevel' => 'learner;experienced'
1098 ],
1099 ],
1100
1101 // A few random combos
1102 [
1103 'bots,hideliu,hidemyself',
1104 [
1105 'hidebots' => false,
1106 'hideliu' => true,
1107 'hidemyself' => true,
1108 ],
1109 ],
1110
1111 [
1112 'minor,hideanons,categorization',
1113 [
1114 'hideminor' => false,
1115 'hideanons' => true,
1116 'hidecategorization' => false,
1117 ]
1118 ],
1119
1120 [
1121 'hidehumans,bots,hidecategorization',
1122 [
1123 'hidehumans' => true,
1124 'hidebots' => false,
1125 'hidecategorization' => true,
1126 ],
1127 ],
1128
1129 [
1130 'hidemyself,userExpLevel=newcomer;learner,hideminor',
1131 [
1132 'hidemyself' => true,
1133 'hideminor' => true,
1134 'userExpLevel' => 'newcomer;learner',
1135 ],
1136 ],
1137 ];
1138 }
1139
1140 public static function provideGetFilterConflicts() {
1141 return [
1142 [
1143 "parameters" => [],
1144 "expectedConflicts" => false,
1145 ],
1146 [
1147 "parameters" => [
1148 "hideliu" => true,
1149 "userExpLevel" => "newcomer",
1150 ],
1151 "expectedConflicts" => false,
1152 ],
1153 [
1154 "parameters" => [
1155 "hideanons" => true,
1156 "userExpLevel" => "learner",
1157 ],
1158 "expectedConflicts" => false,
1159 ],
1160 [
1161 "parameters" => [
1162 "hidemajor" => true,
1163 "hidenewpages" => true,
1164 "hidepageedits" => true,
1165 "hidecategorization" => false,
1166 "hidelog" => true,
1167 "hideWikidata" => true,
1168 ],
1169 "expectedConflicts" => true,
1170 ],
1171 [
1172 "parameters" => [
1173 "hidemajor" => true,
1174 "hidenewpages" => false,
1175 "hidepageedits" => true,
1176 "hidecategorization" => false,
1177 "hidelog" => false,
1178 "hideWikidata" => true,
1179 ],
1180 "expectedConflicts" => true,
1181 ],
1182 [
1183 "parameters" => [
1184 "hidemajor" => true,
1185 "hidenewpages" => false,
1186 "hidepageedits" => false,
1187 "hidecategorization" => true,
1188 "hidelog" => true,
1189 "hideWikidata" => true,
1190 ],
1191 "expectedConflicts" => false,
1192 ],
1193 [
1194 "parameters" => [
1195 "hideminor" => true,
1196 "hidenewpages" => true,
1197 "hidepageedits" => true,
1198 "hidecategorization" => false,
1199 "hidelog" => true,
1200 "hideWikidata" => true,
1201 ],
1202 "expectedConflicts" => false,
1203 ],
1204 ];
1205 }
1206
1207 /**
1208 * @dataProvider provideGetFilterConflicts
1209 */
1210 public function testGetFilterConflicts( $parameters, $expectedConflicts ) {
1211 $context = new RequestContext;
1212 $context->setRequest( new FauxRequest( $parameters ) );
1213 $this->changesListSpecialPage->setContext( $context );
1214
1215 $this->assertEquals(
1216 $expectedConflicts,
1217 $this->changesListSpecialPage->areFiltersInConflict()
1218 );
1219 }
1220
1221 public function validateOptionsProvider() {
1222 return [
1223 [
1224 [ 'hideanons' => 1, 'hideliu' => 1, 'hidebots' => 1 ],
1225 true,
1226 [ 'userExpLevel' => 'unregistered', 'hidebots' => 1, ],
1227 true,
1228 ],
1229 [
1230 [ 'hideanons' => 1, 'hideliu' => 1, 'hidebots' => 0 ],
1231 true,
1232 [ 'hidebots' => 0, 'hidehumans' => 1 ],
1233 true,
1234 ],
1235 [
1236 [ 'hideanons' => 1 ],
1237 true,
1238 [ 'userExpLevel' => 'registered' ],
1239 true,
1240 ],
1241 [
1242 [ 'hideliu' => 1 ],
1243 true,
1244 [ 'userExpLevel' => 'unregistered' ],
1245 true,
1246 ],
1247 [
1248 [ 'hideanons' => 1, 'hidebots' => 1 ],
1249 true,
1250 [ 'userExpLevel' => 'registered', 'hidebots' => 1 ],
1251 true,
1252 ],
1253 [
1254 [ 'hideliu' => 1, 'hidebots' => 0 ],
1255 true,
1256 [ 'userExpLevel' => 'unregistered', 'hidebots' => 0 ],
1257 true,
1258 ],
1259 [
1260 [ 'hidemyself' => 1, 'hidebyothers' => 1 ],
1261 true,
1262 [],
1263 true,
1264 ],
1265 [
1266 [ 'hidebots' => 1, 'hidehumans' => 1 ],
1267 true,
1268 [],
1269 true,
1270 ],
1271 [
1272 [ 'hidepatrolled' => 1, 'hideunpatrolled' => 1 ],
1273 true,
1274 [],
1275 true,
1276 ],
1277 [
1278 [ 'hideminor' => 1, 'hidemajor' => 1 ],
1279 true,
1280 [],
1281 true,
1282 ],
1283 [
1284 // changeType
1285 [ 'hidepageedits' => 1, 'hidenewpages' => 1, 'hidecategorization' => 1, 'hidelog' => 1, 'hidenewuserlog' => 1 ],
1286 true,
1287 [],
1288 true,
1289 ],
1290 ];
1291 }
1292 }