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 public function setUp() {
20 $this->setMwGlobals( [
21 'wgStructuredChangeFiltersShowPreference' => true,
25 protected function getPage() {
26 $mock = $this->getMockBuilder( ChangesListSpecialPage
::class )
29 'ChangesListSpecialPage',
33 ->setMethods( [ 'getPageTitle' ] )
34 ->getMockForAbstractClass();
36 $mock->method( 'getPageTitle' )->willReturn(
37 Title
::makeTitle( NS_SPECIAL
, 'ChangesListSpecialPage' )
40 $mock = TestingAccessWrapper
::newFromObject(
47 private function buildQuery(
48 $requestOptions = null,
51 $context = new RequestContext
;
52 $context->setRequest( new FauxRequest( $requestOptions ) );
54 $context->setUser( $user );
57 $this->changesListSpecialPage
->setContext( $context );
58 $this->changesListSpecialPage
->filterGroups
= [];
59 $formOptions = $this->changesListSpecialPage
->setup( null );
61 # Filter out rc_timestamp conditions which depends on the test runtime
62 # This condition is not needed as of march 2, 2011 -- hashar
63 # @todo FIXME: Find a way to generate the correct rc_timestamp
67 $queryConditions = [];
72 [ $this->changesListSpecialPage
, 'buildQuery' ],
83 $queryConditions = array_filter(
85 'ChangesListSpecialPageTest::filterOutRcTimestampCondition'
88 return $queryConditions;
91 /** helper to test SpecialRecentchanges::buildQuery() */
92 private function assertConditions(
94 $requestOptions = null,
98 $queryConditions = $this->buildQuery( $requestOptions, $user );
101 self
::normalizeCondition( $expected ),
102 self
::normalizeCondition( $queryConditions ),
107 private static function normalizeCondition( $conds ) {
108 $normalized = array_map(
109 function ( $k, $v ) {
110 return is_numeric( $k ) ?
$v : "$k = $v";
112 array_keys( $conds ),
119 /** return false if condition begin with 'rc_timestamp ' */
120 private static function filterOutRcTimestampCondition( $var ) {
121 return ( false === strpos( $var, 'rc_timestamp ' ) );
124 public function testRcNsFilter() {
125 $this->assertConditions(
127 "rc_namespace = '0'",
130 'namespace' => NS_MAIN
,
132 "rc conditions with one namespace"
136 public function testRcNsFilterInversion() {
137 $this->assertConditions(
139 "rc_namespace != '0'",
142 'namespace' => NS_MAIN
,
145 "rc conditions with namespace inverted"
149 public function testRcNsFilterMultiple() {
150 $this->assertConditions(
152 "rc_namespace IN ('1','2','3')",
155 'namespace' => '1;2;3',
157 "rc conditions with multiple namespaces"
161 public function testRcNsFilterMultipleAssociated() {
162 $this->assertConditions(
164 "rc_namespace IN ('0','1','4','5','6','7')",
167 'namespace' => '1;4;7',
170 "rc conditions with multiple namespaces and associated"
174 public function testRcNsFilterMultipleAssociatedInvert() {
175 $this->assertConditions(
177 "rc_namespace NOT IN ('2','3','8','9')",
180 'namespace' => '2;3;9',
184 "rc conditions with multiple namespaces, associated and inverted"
188 public function testRcNsFilterMultipleInvert() {
189 $this->assertConditions(
191 "rc_namespace NOT IN ('1','2','3')",
194 'namespace' => '1;2;3',
197 "rc conditions with multiple namespaces inverted"
201 public function testRcHidemyselfFilter() {
202 $user = $this->getTestUser()->getUser();
203 $this->assertConditions(
205 "rc_user_text != '{$user->getName()}'",
210 "rc conditions: hidemyself=1 (logged in)",
214 $user = User
::newFromName( '10.11.12.13', false );
215 $this->assertConditions(
217 "rc_user_text != '10.11.12.13'",
222 "rc conditions: hidemyself=1 (anon)",
227 public function testRcHidebyothersFilter() {
228 $user = $this->getTestUser()->getUser();
229 $this->assertConditions(
231 "rc_user_text = '{$user->getName()}'",
236 "rc conditions: hidebyothers=1 (logged in)",
240 $user = User
::newFromName( '10.11.12.13', false );
241 $this->assertConditions(
243 "rc_user_text = '10.11.12.13'",
248 "rc conditions: hidebyothers=1 (anon)",
253 public function testRcHidepageedits() {
254 $this->assertConditions(
259 'hidepageedits' => 1,
261 "rc conditions: hidepageedits=1"
265 public function testRcHidenewpages() {
266 $this->assertConditions(
273 "rc conditions: hidenewpages=1"
277 public function testRcHidelog() {
278 $this->assertConditions(
285 "rc conditions: hidelog=1"
289 public function testRcHidehumans() {
290 $this->assertConditions(
298 "rc conditions: hidebots=0 hidehumans=1"
302 public function testRcHidepatrolledDisabledFilter() {
303 $this->setMwGlobals( 'wgUseRCPatrol', false );
304 $user = $this->getTestUser()->getUser();
305 $this->assertConditions(
309 'hidepatrolled' => 1,
311 "rc conditions: hidepatrolled=1 (user not allowed)",
316 public function testRcHideunpatrolledDisabledFilter() {
317 $this->setMwGlobals( 'wgUseRCPatrol', false );
318 $user = $this->getTestUser()->getUser();
319 $this->assertConditions(
323 'hideunpatrolled' => 1,
325 "rc conditions: hideunpatrolled=1 (user not allowed)",
329 public function testRcHidepatrolledFilter() {
330 $user = $this->getTestSysop()->getUser();
331 $this->assertConditions(
336 'hidepatrolled' => 1,
338 "rc conditions: hidepatrolled=1",
343 public function testRcHideunpatrolledFilter() {
344 $user = $this->getTestSysop()->getUser();
345 $this->assertConditions(
350 'hideunpatrolled' => 1,
352 "rc conditions: hideunpatrolled=1",
357 public function testRcHideminorFilter() {
358 $this->assertConditions(
365 "rc conditions: hideminor=1"
369 public function testRcHidemajorFilter() {
370 $this->assertConditions(
377 "rc conditions: hidemajor=1"
381 public function testHideCategorization() {
382 $this->assertConditions(
388 'hidecategorization' => 1
390 "rc conditions: hidecategorization=1"
394 public function testFilterUserExpLevelAll() {
395 $this->assertConditions(
400 'userExpLevel' => 'registered;unregistered;newcomer;learner;experienced',
402 "rc conditions: userExpLevel=registered;unregistered;newcomer;learner;experienced"
406 public function testFilterUserExpLevelRegisteredUnregistered() {
407 $this->assertConditions(
412 'userExpLevel' => 'registered;unregistered',
414 "rc conditions: userExpLevel=registered;unregistered"
418 public function testFilterUserExpLevelRegisteredUnregisteredLearner() {
419 $this->assertConditions(
424 'userExpLevel' => 'registered;unregistered;learner',
426 "rc conditions: userExpLevel=registered;unregistered;learner"
430 public function testFilterUserExpLevelAllExperienceLevels() {
431 $this->assertConditions(
437 'userExpLevel' => 'newcomer;learner;experienced',
439 "rc conditions: userExpLevel=newcomer;learner;experienced"
443 public function testFilterUserExpLevelRegistrered() {
444 $this->assertConditions(
450 'userExpLevel' => 'registered',
452 "rc conditions: userExpLevel=registered"
456 public function testFilterUserExpLevelUnregistrered() {
457 $this->assertConditions(
463 'userExpLevel' => 'unregistered',
465 "rc conditions: userExpLevel=unregistered"
469 public function testFilterUserExpLevelRegistreredOrLearner() {
470 $this->assertConditions(
476 'userExpLevel' => 'registered;learner',
478 "rc conditions: userExpLevel=registered;learner"
482 public function testFilterUserExpLevelUnregistreredOrExperienced() {
483 $conds = $this->buildQuery( [ 'userExpLevel' => 'unregistered;experienced' ] );
486 '/\(rc_user = 0\) OR \(\(user_editcount >= 500\) AND \(user_registration <= \'[^\']+\'\)\)/',
488 "rc conditions: userExpLevel=unregistered;experienced"
492 public function testFilterUserExpLevel() {
494 $this->setMwGlobals( [
495 'wgLearnerEdits' => 10,
496 'wgLearnerMemberSince' => 4,
497 'wgExperiencedUserEdits' => 500,
498 'wgExperiencedUserMemberSince' => 30,
501 $this->createUsers( [
502 'Newcomer1' => [ 'edits' => 2, 'days' => 2 ],
503 'Newcomer2' => [ 'edits' => 12, 'days' => 3 ],
504 'Newcomer3' => [ 'edits' => 8, 'days' => 5 ],
505 'Learner1' => [ 'edits' => 15, 'days' => 10 ],
506 'Learner2' => [ 'edits' => 450, 'days' => 20 ],
507 'Learner3' => [ 'edits' => 460, 'days' => 33 ],
508 'Learner4' => [ 'edits' => 525, 'days' => 28 ],
509 'Experienced1' => [ 'edits' => 538, 'days' => 33 ],
513 $this->assertArrayEquals(
514 [ 'Newcomer1', 'Newcomer2', 'Newcomer3' ],
515 $this->fetchUsers( [ 'newcomer' ], $now )
518 // newcomers and learner
519 $this->assertArrayEquals(
521 'Newcomer1', 'Newcomer2', 'Newcomer3',
522 'Learner1', 'Learner2', 'Learner3', 'Learner4',
524 $this->fetchUsers( [ 'newcomer', 'learner' ], $now )
527 // newcomers and more learner
528 $this->assertArrayEquals(
530 'Newcomer1', 'Newcomer2', 'Newcomer3',
533 $this->fetchUsers( [ 'newcomer', 'experienced' ], $now )
537 $this->assertArrayEquals(
538 [ 'Learner1', 'Learner2', 'Learner3', 'Learner4' ],
539 $this->fetchUsers( [ 'learner' ], $now )
542 // more experienced only
543 $this->assertArrayEquals(
545 $this->fetchUsers( [ 'experienced' ], $now )
548 // learner and more experienced
549 $this->assertArrayEquals(
551 'Learner1', 'Learner2', 'Learner3', 'Learner4',
554 $this->fetchUsers( [ 'learner', 'experienced' ], $now ),
555 'Learner and more experienced'
559 private function createUsers( $specs, $now ) {
560 $dbw = wfGetDB( DB_MASTER
);
561 foreach ( $specs as $name => $spec ) {
565 'editcount' => $spec['edits'],
566 'registration' => $dbw->timestamp( $this->daysAgo( $spec['days'], $now ) ),
573 private function fetchUsers( $filters, $now ) {
582 call_user_func_array(
583 [ $this->changesListSpecialPage
, 'filterOnUserExperienceLevel' ],
585 get_class( $this->changesListSpecialPage
),
586 $this->changesListSpecialPage
->getContext(),
587 $this->changesListSpecialPage
->getDB(),
598 $result = wfGetDB( DB_MASTER
)->select(
601 array_filter( $conds ) +
[ 'user_email' => 'ut' ]
605 foreach ( $result as $row ) {
606 $usernames[] = $row->user_name
;
612 private function daysAgo( $days, $now ) {
613 $secondsPerDay = 86400;
614 return $now - $days * $secondsPerDay;
617 public function testGetFilterGroupDefinitionFromLegacyCustomFilters() {
620 'msg' => 'showhidefoo',
625 'msg' => 'showhidebar',
632 'name' => 'unstructured',
633 'class' => ChangesListBooleanFilterGroup
::class,
638 'showHide' => 'showhidefoo',
643 'showHide' => 'showhidebar',
648 $this->changesListSpecialPage
->getFilterGroupDefinitionFromLegacyCustomFilters(
654 public function testGetStructuredFilterJsData() {
655 $this->changesListSpecialPage
->filterGroups
= [];
659 'name' => 'gub-group',
660 'title' => 'gub-group-title',
661 'class' => ChangesListBooleanFilterGroup
::class,
665 'label' => 'foo-label',
666 'description' => 'foo-description',
668 'showHide' => 'showhidefoo',
673 'label' => 'bar-label',
674 'description' => 'bar-description',
682 'name' => 'des-group',
683 'title' => 'des-group-title',
684 'class' => ChangesListStringOptionsFilterGroup
::class,
685 'isFullCoverage' => true,
689 'label' => 'grault-label',
690 'description' => 'grault-description',
694 'label' => 'garply-label',
695 'description' => 'garply-description',
698 'queryCallable' => function () {
700 'default' => ChangesListStringOptionsFilterGroup
::NONE
,
704 'name' => 'unstructured',
705 'class' => ChangesListBooleanFilterGroup
::class,
708 'name' => 'hidethud',
709 'showHide' => 'showhidethud',
715 'showHide' => 'showhidemos',
723 $this->changesListSpecialPage
->registerFiltersFromDefinitions( $definition );
725 $this->assertArrayEquals(
727 // Filters that only display in the unstructured UI are
728 // are not included, and neither are groups that would
729 // be empty due to the above.
732 'name' => 'gub-group',
733 'title' => 'gub-group-title',
734 'type' => ChangesListBooleanFilterGroup
::TYPE
,
739 'label' => 'bar-label',
740 'description' => 'bar-description',
746 'defaultHighlightColor' => null
750 'label' => 'foo-label',
751 'description' => 'foo-description',
757 'defaultHighlightColor' => null
760 'fullCoverage' => true,
765 'name' => 'des-group',
766 'title' => 'des-group-title',
767 'type' => ChangesListStringOptionsFilterGroup
::TYPE
,
769 'fullCoverage' => true,
773 'label' => 'grault-label',
774 'description' => 'grault-description',
779 'defaultHighlightColor' => null
783 'label' => 'garply-label',
784 'description' => 'garply-description',
789 'defaultHighlightColor' => null
794 'default' => ChangesListStringOptionsFilterGroup
::NONE
,
805 'grault-description',
807 'garply-description',
810 $this->changesListSpecialPage
->getStructuredFilterJsData(),
811 /** ordered= */ false,
816 public function provideParseParameters() {
818 [ 'hidebots', [ 'hidebots' => true ] ],
820 [ 'bots', [ 'hidebots' => false ] ],
822 [ 'hideminor', [ 'hideminor' => true ] ],
824 [ 'minor', [ 'hideminor' => false ] ],
826 [ 'hidemajor', [ 'hidemajor' => true ] ],
828 [ 'hideliu', [ 'hideliu' => true ] ],
830 [ 'hidepatrolled', [ 'hidepatrolled' => true ] ],
832 [ 'hideunpatrolled', [ 'hideunpatrolled' => true ] ],
834 [ 'hideanons', [ 'hideanons' => true ] ],
836 [ 'hidemyself', [ 'hidemyself' => true ] ],
838 [ 'hidebyothers', [ 'hidebyothers' => true ] ],
840 [ 'hidehumans', [ 'hidehumans' => true ] ],
842 [ 'hidepageedits', [ 'hidepageedits' => true ] ],
844 [ 'pagedits', [ 'hidepageedits' => false ] ],
846 [ 'hidenewpages', [ 'hidenewpages' => true ] ],
848 [ 'hidecategorization', [ 'hidecategorization' => true ] ],
850 [ 'hidelog', [ 'hidelog' => true ] ],
853 'userExpLevel=learner;experienced',
855 'userExpLevel' => 'learner;experienced'
859 // A few random combos
861 'bots,hideliu,hidemyself',
865 'hidemyself' => true,
870 'minor,hideanons,categorization',
872 'hideminor' => false,
874 'hidecategorization' => false,
879 'hidehumans,bots,hidecategorization',
881 'hidehumans' => true,
883 'hidecategorization' => true,
888 'hidemyself,userExpLevel=newcomer;learner,hideminor',
890 'hidemyself' => true,
892 'userExpLevel' => 'newcomer;learner',
898 public function provideGetFilterConflicts() {
902 "expectedConflicts" => false,
907 "userExpLevel" => "newcomer",
909 "expectedConflicts" => false,
914 "userExpLevel" => "learner",
916 "expectedConflicts" => false,
921 "hidenewpages" => true,
922 "hidepageedits" => true,
923 "hidecategorization" => false,
925 "hideWikidata" => true,
927 "expectedConflicts" => true,
932 "hidenewpages" => false,
933 "hidepageedits" => true,
934 "hidecategorization" => false,
936 "hideWikidata" => true,
938 "expectedConflicts" => true,
943 "hidenewpages" => false,
944 "hidepageedits" => false,
945 "hidecategorization" => true,
947 "hideWikidata" => true,
949 "expectedConflicts" => false,
954 "hidenewpages" => true,
955 "hidepageedits" => true,
956 "hidecategorization" => false,
958 "hideWikidata" => true,
960 "expectedConflicts" => false,
966 * @dataProvider provideGetFilterConflicts
968 public function testGetFilterConflicts( $parameters, $expectedConflicts ) {
969 $context = new RequestContext
;
970 $context->setRequest( new FauxRequest( $parameters ) );
971 $this->changesListSpecialPage
->setContext( $context );
975 $this->changesListSpecialPage
->areFiltersInConflict()
979 public function validateOptionsProvider() {
982 [ 'hideanons' => 1, 'hideliu' => 1, 'hidebots' => 1 ],
984 [ 'userExpLevel' => 'unregistered', 'hidebots' => 1, ],
987 [ 'hideanons' => 1, 'hideliu' => 1, 'hidebots' => 0 ],
989 [ 'hidebots' => 0, 'hidehumans' => 1 ],
992 [ 'hideanons' => 1 ],
994 [ 'userExpLevel' => 'registered' ]
999 [ 'userExpLevel' => 'unregistered' ]
1002 [ 'hideanons' => 1, 'hidebots' => 1 ],
1004 [ 'userExpLevel' => 'registered', 'hidebots' => 1 ]
1007 [ 'hideliu' => 1, 'hidebots' => 0 ],
1009 [ 'userExpLevel' => 'unregistered', 'hidebots' => 0 ]
1012 [ 'hidemyself' => 1, 'hidebyothers' => 1 ],
1017 [ 'hidebots' => 1, 'hidehumans' => 1 ],
1022 [ 'hidepatrolled' => 1, 'hideunpatrolled' => 1 ],
1027 [ 'hideminor' => 1, 'hidemajor' => 1 ],
1033 [ 'hidepageedits' => 1, 'hidenewpages' => 1, 'hidecategorization' => 1, 'hidelog' => 1, ],