3 use Wikimedia\TestingAccessWrapper
;
6 * Test class for ChangesListSpecialPage class
8 * Copyright © 2011-, Antoine Musso, Stephane Bisson, Matthew Flaschen
10 * @author Antoine Musso
11 * @author Stephane Bisson
12 * @author Matthew Flaschen
15 * @covers ChangesListSpecialPage
17 class ChangesListSpecialPageTest
extends AbstractChangesListSpecialPageTestCase
{
18 protected function getPage() {
19 $mock = $this->getMockBuilder( ChangesListSpecialPage
::class )
22 'ChangesListSpecialPage',
26 ->setMethods( [ 'getPageTitle' ] )
27 ->getMockForAbstractClass();
29 $mock->method( 'getPageTitle' )->willReturn(
30 Title
::makeTitle( NS_SPECIAL
, 'ChangesListSpecialPage' )
33 $mock = TestingAccessWrapper
::newFromObject(
40 private function buildQuery(
41 $requestOptions = null,
44 $context = new RequestContext
;
45 $context->setRequest( new FauxRequest( $requestOptions ) );
47 $context->setUser( $user );
50 $this->changesListSpecialPage
->setContext( $context );
51 $this->changesListSpecialPage
->filterGroups
= [];
52 $formOptions = $this->changesListSpecialPage
->setup( null );
54 # Filter out rc_timestamp conditions which depends on the test runtime
55 # This condition is not needed as of march 2, 2011 -- hashar
56 # @todo FIXME: Find a way to generate the correct rc_timestamp
60 $queryConditions = [];
65 [ $this->changesListSpecialPage
, 'buildQuery' ],
76 $queryConditions = array_filter(
78 'ChangesListSpecialPageTest::filterOutRcTimestampCondition'
81 return $queryConditions;
84 /** helper to test SpecialRecentchanges::buildQuery() */
85 private function assertConditions(
87 $requestOptions = null,
91 $queryConditions = $this->buildQuery( $requestOptions, $user );
94 self
::normalizeCondition( $expected ),
95 self
::normalizeCondition( $queryConditions ),
100 private static function normalizeCondition( $conds ) {
101 $dbr = wfGetDB( DB_REPLICA
);
102 $normalized = array_map(
103 function ( $k, $v ) use ( $dbr ) {
104 if ( is_array( $v ) ) {
107 // (Ab)use makeList() to format only this entry
108 return $dbr->makeList( [ $k => $v ], Database
::LIST_AND
);
110 array_keys( $conds ),
117 /** return false if condition begins with 'rc_timestamp ' */
118 private static function filterOutRcTimestampCondition( $var ) {
119 return ( is_array( $var ) ||
strpos( $var, 'rc_timestamp ' ) === false );
122 public function testRcNsFilter() {
123 $this->assertConditions(
125 "rc_namespace = '0'",
128 'namespace' => NS_MAIN
,
130 "rc conditions with one namespace"
134 public function testRcNsFilterInversion() {
135 $this->assertConditions(
137 "rc_namespace != '0'",
140 'namespace' => NS_MAIN
,
143 "rc conditions with namespace inverted"
147 public function testRcNsFilterMultiple() {
148 $this->assertConditions(
150 "rc_namespace IN ('1','2','3')",
153 'namespace' => '1;2;3',
155 "rc conditions with multiple namespaces"
159 public function testRcNsFilterMultipleAssociated() {
160 $this->assertConditions(
162 "rc_namespace IN ('0','1','4','5','6','7')",
165 'namespace' => '1;4;7',
168 "rc conditions with multiple namespaces and associated"
172 public function testRcNsFilterMultipleAssociatedInvert() {
173 $this->assertConditions(
175 "rc_namespace NOT IN ('2','3','8','9')",
178 'namespace' => '2;3;9',
182 "rc conditions with multiple namespaces, associated and inverted"
186 public function testRcNsFilterMultipleInvert() {
187 $this->assertConditions(
189 "rc_namespace NOT IN ('1','2','3')",
192 'namespace' => '1;2;3',
195 "rc conditions with multiple namespaces inverted"
199 public function testRcHidemyselfFilter() {
201 'wgActorTableSchemaMigrationStage', SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_OLD
203 $this->overrideMwServices();
205 $user = $this->getTestUser()->getUser();
206 $user->getActorId( wfGetDB( DB_MASTER
) );
207 $this->assertConditions(
209 "NOT((rc_user = '{$user->getId()}'))",
214 "rc conditions: hidemyself=1 (logged in)",
218 $user = User
::newFromName( '10.11.12.13', false );
219 $id = $user->getActorId( wfGetDB( DB_MASTER
) );
220 $this->assertConditions(
222 "NOT((rc_user_text = '10.11.12.13'))",
227 "rc conditions: hidemyself=1 (anon)",
232 public function testRcHidebyothersFilter() {
234 'wgActorTableSchemaMigrationStage', SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_OLD
236 $this->overrideMwServices();
238 $user = $this->getTestUser()->getUser();
239 $user->getActorId( wfGetDB( DB_MASTER
) );
240 $this->assertConditions(
242 "(rc_user_text = '{$user->getName()}')",
247 "rc conditions: hidebyothers=1 (logged in)",
251 $user = User
::newFromName( '10.11.12.13', false );
252 $id = $user->getActorId( wfGetDB( DB_MASTER
) );
253 $this->assertConditions(
255 "(rc_user_text = '10.11.12.13')",
260 "rc conditions: hidebyothers=1 (anon)",
265 public function testRcHidepageedits() {
266 $this->assertConditions(
271 'hidepageedits' => 1,
273 "rc conditions: hidepageedits=1"
277 public function testRcHidenewpages() {
278 $this->assertConditions(
285 "rc conditions: hidenewpages=1"
289 public function testRcHidelog() {
290 $this->assertConditions(
297 "rc conditions: hidelog=1"
301 public function testRcHidehumans() {
302 $this->assertConditions(
310 "rc conditions: hidebots=0 hidehumans=1"
314 public function testRcHidepatrolledDisabledFilter() {
315 $this->setMwGlobals( 'wgUseRCPatrol', false );
316 $user = $this->getTestUser()->getUser();
317 $this->assertConditions(
321 'hidepatrolled' => 1,
323 "rc conditions: hidepatrolled=1 (user not allowed)",
328 public function testRcHideunpatrolledDisabledFilter() {
329 $this->setMwGlobals( 'wgUseRCPatrol', false );
330 $user = $this->getTestUser()->getUser();
331 $this->assertConditions(
335 'hideunpatrolled' => 1,
337 "rc conditions: hideunpatrolled=1 (user not allowed)",
341 public function testRcHidepatrolledFilter() {
342 $user = $this->getTestSysop()->getUser();
343 $this->assertConditions(
348 'hidepatrolled' => 1,
350 "rc conditions: hidepatrolled=1",
355 public function testRcHideunpatrolledFilter() {
356 $user = $this->getTestSysop()->getUser();
357 $this->assertConditions(
359 'rc_patrolled' => [ 1, 2 ],
362 'hideunpatrolled' => 1,
364 "rc conditions: hideunpatrolled=1",
369 public function testRcReviewStatusFilter() {
370 $user = $this->getTestSysop()->getUser();
371 $this->assertConditions(
376 'reviewStatus' => 'manual'
378 "rc conditions: reviewStatus=manual",
381 $this->assertConditions(
383 'rc_patrolled' => [ 0, 2 ],
386 'reviewStatus' => 'unpatrolled;auto'
388 "rc conditions: reviewStatus=unpatrolled;auto",
393 public function testRcHideminorFilter() {
394 $this->assertConditions(
401 "rc conditions: hideminor=1"
405 public function testRcHidemajorFilter() {
406 $this->assertConditions(
413 "rc conditions: hidemajor=1"
417 public function testHideCategorization() {
418 $this->assertConditions(
424 'hidecategorization' => 1
426 "rc conditions: hidecategorization=1"
430 public function testFilterUserExpLevelAll() {
431 $this->assertConditions(
436 'userExpLevel' => 'registered;unregistered;newcomer;learner;experienced',
438 "rc conditions: userExpLevel=registered;unregistered;newcomer;learner;experienced"
442 public function testFilterUserExpLevelRegisteredUnregistered() {
443 $this->assertConditions(
448 'userExpLevel' => 'registered;unregistered',
450 "rc conditions: userExpLevel=registered;unregistered"
454 public function testFilterUserExpLevelRegisteredUnregisteredLearner() {
455 $this->assertConditions(
460 'userExpLevel' => 'registered;unregistered;learner',
462 "rc conditions: userExpLevel=registered;unregistered;learner"
466 public function testFilterUserExpLevelAllExperienceLevels() {
468 'wgActorTableSchemaMigrationStage', SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_OLD
470 $this->overrideMwServices();
472 $this->assertConditions(
478 'userExpLevel' => 'newcomer;learner;experienced',
480 "rc conditions: userExpLevel=newcomer;learner;experienced"
484 public function testFilterUserExpLevelRegistrered() {
486 'wgActorTableSchemaMigrationStage', SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_OLD
488 $this->overrideMwServices();
490 $this->assertConditions(
496 'userExpLevel' => 'registered',
498 "rc conditions: userExpLevel=registered"
502 public function testFilterUserExpLevelUnregistrered() {
504 'wgActorTableSchemaMigrationStage', SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_OLD
506 $this->overrideMwServices();
508 $this->assertConditions(
514 'userExpLevel' => 'unregistered',
516 "rc conditions: userExpLevel=unregistered"
520 public function testFilterUserExpLevelRegistreredOrLearner() {
522 'wgActorTableSchemaMigrationStage', SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_OLD
524 $this->overrideMwServices();
526 $this->assertConditions(
532 'userExpLevel' => 'registered;learner',
534 "rc conditions: userExpLevel=registered;learner"
538 public function testFilterUserExpLevelUnregistreredOrExperienced() {
540 'wgActorTableSchemaMigrationStage', SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_OLD
542 $this->overrideMwServices();
544 $conds = $this->buildQuery( [ 'userExpLevel' => 'unregistered;experienced' ] );
547 '/\(rc_user = 0\) OR '
548 . '\(\(user_editcount >= 500\) AND \(user_registration <= \'[^\']+\'\)\)/',
550 "rc conditions: userExpLevel=unregistered;experienced"
554 public function testFilterUserExpLevel() {
556 $this->setMwGlobals( [
557 'wgLearnerEdits' => 10,
558 'wgLearnerMemberSince' => 4,
559 'wgExperiencedUserEdits' => 500,
560 'wgExperiencedUserMemberSince' => 30,
563 $this->createUsers( [
564 'Newcomer1' => [ 'edits' => 2, 'days' => 2 ],
565 'Newcomer2' => [ 'edits' => 12, 'days' => 3 ],
566 'Newcomer3' => [ 'edits' => 8, 'days' => 5 ],
567 'Learner1' => [ 'edits' => 15, 'days' => 10 ],
568 'Learner2' => [ 'edits' => 450, 'days' => 20 ],
569 'Learner3' => [ 'edits' => 460, 'days' => 33 ],
570 'Learner4' => [ 'edits' => 525, 'days' => 28 ],
571 'Experienced1' => [ 'edits' => 538, 'days' => 33 ],
575 $this->assertArrayEquals(
576 [ 'Newcomer1', 'Newcomer2', 'Newcomer3' ],
577 $this->fetchUsers( [ 'newcomer' ], $now )
580 // newcomers and learner
581 $this->assertArrayEquals(
583 'Newcomer1', 'Newcomer2', 'Newcomer3',
584 'Learner1', 'Learner2', 'Learner3', 'Learner4',
586 $this->fetchUsers( [ 'newcomer', 'learner' ], $now )
589 // newcomers and more learner
590 $this->assertArrayEquals(
592 'Newcomer1', 'Newcomer2', 'Newcomer3',
595 $this->fetchUsers( [ 'newcomer', 'experienced' ], $now )
599 $this->assertArrayEquals(
600 [ 'Learner1', 'Learner2', 'Learner3', 'Learner4' ],
601 $this->fetchUsers( [ 'learner' ], $now )
604 // more experienced only
605 $this->assertArrayEquals(
607 $this->fetchUsers( [ 'experienced' ], $now )
610 // learner and more experienced
611 $this->assertArrayEquals(
613 'Learner1', 'Learner2', 'Learner3', 'Learner4',
616 $this->fetchUsers( [ 'learner', 'experienced' ], $now )
620 private function createUsers( $specs, $now ) {
621 $dbw = wfGetDB( DB_MASTER
);
622 foreach ( $specs as $name => $spec ) {
626 'editcount' => $spec['edits'],
627 'registration' => $dbw->timestamp( $this->daysAgo( $spec['days'], $now ) ),
634 private function fetchUsers( $filters, $now ) {
643 call_user_func_array(
644 [ $this->changesListSpecialPage
, 'filterOnUserExperienceLevel' ],
646 get_class( $this->changesListSpecialPage
),
647 $this->changesListSpecialPage
->getContext(),
648 $this->changesListSpecialPage
->getDB(),
659 // @todo: This is not at all safe or sane. It just blindly assumes
660 // nothing in $conds depends on any other tables.
661 $result = wfGetDB( DB_MASTER
)->select(
664 array_filter( $conds ) +
[ 'user_email' => 'ut' ]
668 foreach ( $result as $row ) {
669 $usernames[] = $row->user_name
;
675 private function daysAgo( $days, $now ) {
676 $secondsPerDay = 86400;
677 return $now - $days * $secondsPerDay;
680 public function testGetStructuredFilterJsData() {
681 $this->changesListSpecialPage
->filterGroups
= [];
685 'name' => 'gub-group',
686 'title' => 'gub-group-title',
687 'class' => ChangesListBooleanFilterGroup
::class,
691 'label' => 'foo-label',
692 'description' => 'foo-description',
694 'showHide' => 'showhidefoo',
699 'label' => 'bar-label',
700 'description' => 'bar-description',
708 'name' => 'des-group',
709 'title' => 'des-group-title',
710 'class' => ChangesListStringOptionsFilterGroup
::class,
711 'isFullCoverage' => true,
715 'label' => 'grault-label',
716 'description' => 'grault-description',
720 'label' => 'garply-label',
721 'description' => 'garply-description',
724 'queryCallable' => function () {
726 'default' => ChangesListStringOptionsFilterGroup
::NONE
,
730 'name' => 'unstructured',
731 'class' => ChangesListBooleanFilterGroup
::class,
734 'name' => 'hidethud',
735 'showHide' => 'showhidethud',
741 'showHide' => 'showhidemos',
749 $this->changesListSpecialPage
->registerFiltersFromDefinitions( $definition );
751 $this->assertArrayEquals(
753 // Filters that only display in the unstructured UI are
754 // are not included, and neither are groups that would
755 // be empty due to the above.
758 'name' => 'gub-group',
759 'title' => 'gub-group-title',
760 'type' => ChangesListBooleanFilterGroup
::TYPE
,
765 'label' => 'bar-label',
766 'description' => 'bar-description',
772 'defaultHighlightColor' => null
776 'label' => 'foo-label',
777 'description' => 'foo-description',
783 'defaultHighlightColor' => null
786 'fullCoverage' => true,
791 'name' => 'des-group',
792 'title' => 'des-group-title',
793 'type' => ChangesListStringOptionsFilterGroup
::TYPE
,
795 'fullCoverage' => true,
799 'label' => 'grault-label',
800 'description' => 'grault-description',
805 'defaultHighlightColor' => null
809 'label' => 'garply-label',
810 'description' => 'garply-description',
815 'defaultHighlightColor' => null
820 'default' => ChangesListStringOptionsFilterGroup
::NONE
,
831 'grault-description',
833 'garply-description',
836 $this->changesListSpecialPage
->getStructuredFilterJsData(),
837 /** ordered= */ false,
842 public function provideParseParameters() {
844 [ 'hidebots', [ 'hidebots' => true ] ],
846 [ 'bots', [ 'hidebots' => false ] ],
848 [ 'hideminor', [ 'hideminor' => true ] ],
850 [ 'minor', [ 'hideminor' => false ] ],
852 [ 'hidemajor', [ 'hidemajor' => true ] ],
854 [ 'hideliu', [ 'hideliu' => true ] ],
856 [ 'hidepatrolled', [ 'hidepatrolled' => true ] ],
858 [ 'hideunpatrolled', [ 'hideunpatrolled' => true ] ],
860 [ 'hideanons', [ 'hideanons' => true ] ],
862 [ 'hidemyself', [ 'hidemyself' => true ] ],
864 [ 'hidebyothers', [ 'hidebyothers' => true ] ],
866 [ 'hidehumans', [ 'hidehumans' => true ] ],
868 [ 'hidepageedits', [ 'hidepageedits' => true ] ],
870 [ 'pagedits', [ 'hidepageedits' => false ] ],
872 [ 'hidenewpages', [ 'hidenewpages' => true ] ],
874 [ 'hidecategorization', [ 'hidecategorization' => true ] ],
876 [ 'hidelog', [ 'hidelog' => true ] ],
879 'userExpLevel=learner;experienced',
881 'userExpLevel' => 'learner;experienced'
885 // A few random combos
887 'bots,hideliu,hidemyself',
891 'hidemyself' => true,
896 'minor,hideanons,categorization',
898 'hideminor' => false,
900 'hidecategorization' => false,
905 'hidehumans,bots,hidecategorization',
907 'hidehumans' => true,
909 'hidecategorization' => true,
914 'hidemyself,userExpLevel=newcomer;learner,hideminor',
916 'hidemyself' => true,
918 'userExpLevel' => 'newcomer;learner',
924 public function provideGetFilterConflicts() {
928 "expectedConflicts" => false,
933 "userExpLevel" => "newcomer",
935 "expectedConflicts" => false,
940 "userExpLevel" => "learner",
942 "expectedConflicts" => false,
947 "hidenewpages" => true,
948 "hidepageedits" => true,
949 "hidecategorization" => false,
951 "hideWikidata" => true,
953 "expectedConflicts" => true,
958 "hidenewpages" => false,
959 "hidepageedits" => true,
960 "hidecategorization" => false,
962 "hideWikidata" => true,
964 "expectedConflicts" => true,
969 "hidenewpages" => false,
970 "hidepageedits" => false,
971 "hidecategorization" => true,
973 "hideWikidata" => true,
975 "expectedConflicts" => false,
980 "hidenewpages" => true,
981 "hidepageedits" => true,
982 "hidecategorization" => false,
984 "hideWikidata" => true,
986 "expectedConflicts" => false,
992 * @dataProvider provideGetFilterConflicts
994 public function testGetFilterConflicts( $parameters, $expectedConflicts ) {
995 $context = new RequestContext
;
996 $context->setRequest( new FauxRequest( $parameters ) );
997 $this->changesListSpecialPage
->setContext( $context );
1001 $this->changesListSpecialPage
->areFiltersInConflict()
1005 public function validateOptionsProvider() {
1008 [ 'hideanons' => 1, 'hideliu' => 1, 'hidebots' => 1 ],
1010 [ 'userExpLevel' => 'unregistered', 'hidebots' => 1, ],
1014 [ 'hideanons' => 1, 'hideliu' => 1, 'hidebots' => 0 ],
1016 [ 'hidebots' => 0, 'hidehumans' => 1 ],
1020 [ 'hideanons' => 1 ],
1022 [ 'userExpLevel' => 'registered' ],
1028 [ 'userExpLevel' => 'unregistered' ],
1032 [ 'hideanons' => 1, 'hidebots' => 1 ],
1034 [ 'userExpLevel' => 'registered', 'hidebots' => 1 ],
1038 [ 'hideliu' => 1, 'hidebots' => 0 ],
1040 [ 'userExpLevel' => 'unregistered', 'hidebots' => 0 ],
1044 [ 'hidemyself' => 1, 'hidebyothers' => 1 ],
1050 [ 'hidebots' => 1, 'hidehumans' => 1 ],
1056 [ 'hidepatrolled' => 1, 'hideunpatrolled' => 1 ],
1062 [ 'hideminor' => 1, 'hidemajor' => 1 ],
1069 [ 'hidepageedits' => 1, 'hidenewpages' => 1, 'hidecategorization' => 1, 'hidelog' => 1, ],