rdbms: Rename "memCache" to "memStash" in LBFactory
[mediawiki.git] / tests / phpunit / includes / specialpage / ChangesListSpecialPageTest.php
blobf494785b9cf824738d5444e942aed7ccf65f89d3
1 <?php
3 use Wikimedia\TestingAccessWrapper;
5 /**
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
13 * @group Database
15 * @covers ChangesListSpecialPage
17 class ChangesListSpecialPageTest extends AbstractChangesListSpecialPageTestCase {
18 protected function getPage() {
19 $mock = $this->getMockBuilder( ChangesListSpecialPage::class )
20 ->setConstructorArgs(
22 'ChangesListSpecialPage',
26 ->setMethods( [ 'getPageTitle' ] )
27 ->getMockForAbstractClass();
29 $mock->method( 'getPageTitle' )->willReturn(
30 Title::makeTitle( NS_SPECIAL, 'ChangesListSpecialPage' )
33 $mock = TestingAccessWrapper::newFromObject(
34 $mock
37 return $mock;
40 private function buildQuery(
41 $requestOptions = null,
42 $user = null
43 ) {
44 $context = new RequestContext;
45 $context->setRequest( new FauxRequest( $requestOptions ) );
46 if ( $user ) {
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
58 $tables = [];
59 $fields = [];
60 $queryConditions = [];
61 $query_options = [];
62 $join_conds = [];
64 call_user_func_array(
65 [ $this->changesListSpecialPage, 'buildQuery' ],
67 &$tables,
68 &$fields,
69 &$queryConditions,
70 &$query_options,
71 &$join_conds,
72 $formOptions
76 $queryConditions = array_filter(
77 $queryConditions,
78 'ChangesListSpecialPageTest::filterOutRcTimestampCondition'
81 return $queryConditions;
84 /** helper to test SpecialRecentchanges::buildQuery() */
85 private function assertConditions(
86 $expected,
87 $requestOptions = null,
88 $message = '',
89 $user = null
90 ) {
91 $queryConditions = $this->buildQuery( $requestOptions, $user );
93 $this->assertEquals(
94 self::normalizeCondition( $expected ),
95 self::normalizeCondition( $queryConditions ),
96 $message
100 private static function normalizeCondition( $conds ) {
101 $normalized = array_map(
102 function ( $k, $v ) {
103 return is_numeric( $k ) ? $v : "$k = $v";
105 array_keys( $conds ),
106 $conds
108 sort( $normalized );
109 return $normalized;
112 /** return false if condition begin with 'rc_timestamp ' */
113 private static function filterOutRcTimestampCondition( $var ) {
114 return ( false === strpos( $var, 'rc_timestamp ' ) );
117 public function testRcNsFilter() {
118 $this->assertConditions(
119 [ # expected
120 "rc_namespace = '0'",
123 'namespace' => NS_MAIN,
125 "rc conditions with one namespace"
129 public function testRcNsFilterInversion() {
130 $this->assertConditions(
131 [ # expected
132 "rc_namespace != '0'",
135 'namespace' => NS_MAIN,
136 'invert' => 1,
138 "rc conditions with namespace inverted"
142 public function testRcNsFilterMultiple() {
143 $this->assertConditions(
144 [ # expected
145 "rc_namespace IN ('1','2','3')",
148 'namespace' => '1;2;3',
150 "rc conditions with multiple namespaces"
154 public function testRcNsFilterMultipleAssociated() {
155 $this->assertConditions(
156 [ # expected
157 "rc_namespace IN ('0','1','4','5','6','7')",
160 'namespace' => '1;4;7',
161 'associated' => 1,
163 "rc conditions with multiple namespaces and associated"
167 public function testRcNsFilterMultipleAssociatedInvert() {
168 $this->assertConditions(
169 [ # expected
170 "rc_namespace NOT IN ('2','3','8','9')",
173 'namespace' => '2;3;9',
174 'associated' => 1,
175 'invert' => 1
177 "rc conditions with multiple namespaces, associated and inverted"
181 public function testRcNsFilterMultipleInvert() {
182 $this->assertConditions(
183 [ # expected
184 "rc_namespace NOT IN ('1','2','3')",
187 'namespace' => '1;2;3',
188 'invert' => 1,
190 "rc conditions with multiple namespaces inverted"
194 public function testRcHidemyselfFilter() {
195 $user = $this->getTestUser()->getUser();
196 $this->assertConditions(
197 [ # expected
198 "rc_user_text != '{$user->getName()}'",
201 'hidemyself' => 1,
203 "rc conditions: hidemyself=1 (logged in)",
204 $user
207 $user = User::newFromName( '10.11.12.13', false );
208 $this->assertConditions(
209 [ # expected
210 "rc_user_text != '10.11.12.13'",
213 'hidemyself' => 1,
215 "rc conditions: hidemyself=1 (anon)",
216 $user
220 public function testRcHidebyothersFilter() {
221 $user = $this->getTestUser()->getUser();
222 $this->assertConditions(
223 [ # expected
224 "rc_user_text = '{$user->getName()}'",
227 'hidebyothers' => 1,
229 "rc conditions: hidebyothers=1 (logged in)",
230 $user
233 $user = User::newFromName( '10.11.12.13', false );
234 $this->assertConditions(
235 [ # expected
236 "rc_user_text = '10.11.12.13'",
239 'hidebyothers' => 1,
241 "rc conditions: hidebyothers=1 (anon)",
242 $user
246 public function testRcHidepageedits() {
247 $this->assertConditions(
248 [ # expected
249 "rc_type != '0'",
252 'hidepageedits' => 1,
254 "rc conditions: hidepageedits=1"
258 public function testRcHidenewpages() {
259 $this->assertConditions(
260 [ # expected
261 "rc_type != '1'",
264 'hidenewpages' => 1,
266 "rc conditions: hidenewpages=1"
270 public function testRcHidelog() {
271 $this->assertConditions(
272 [ # expected
273 "rc_type != '3'",
276 'hidelog' => 1,
278 "rc conditions: hidelog=1"
282 public function testRcHidehumans() {
283 $this->assertConditions(
284 [ # expected
285 'rc_bot' => 1,
288 'hidebots' => 0,
289 'hidehumans' => 1,
291 "rc conditions: hidebots=0 hidehumans=1"
295 public function testRcHidepatrolledDisabledFilter() {
296 $user = $this->getTestUser()->getUser();
297 $this->assertConditions(
298 [ # expected
301 'hidepatrolled' => 1,
303 "rc conditions: hidepatrolled=1 (user not allowed)",
304 $user
308 public function testRcHideunpatrolledDisabledFilter() {
309 $user = $this->getTestUser()->getUser();
310 $this->assertConditions(
311 [ # expected
314 'hideunpatrolled' => 1,
316 "rc conditions: hideunpatrolled=1 (user not allowed)",
317 $user
320 public function testRcHidepatrolledFilter() {
321 $user = $this->getTestSysop()->getUser();
322 $this->assertConditions(
323 [ # expected
324 "rc_patrolled = 0",
327 'hidepatrolled' => 1,
329 "rc conditions: hidepatrolled=1",
330 $user
334 public function testRcHideunpatrolledFilter() {
335 $user = $this->getTestSysop()->getUser();
336 $this->assertConditions(
337 [ # expected
338 "rc_patrolled = 1",
341 'hideunpatrolled' => 1,
343 "rc conditions: hideunpatrolled=1",
344 $user
348 public function testRcHideminorFilter() {
349 $this->assertConditions(
350 [ # expected
351 "rc_minor = 0",
354 'hideminor' => 1,
356 "rc conditions: hideminor=1"
360 public function testRcHidemajorFilter() {
361 $this->assertConditions(
362 [ # expected
363 "rc_minor = 1",
366 'hidemajor' => 1,
368 "rc conditions: hidemajor=1"
372 public function testHideCategorization() {
373 $this->assertConditions(
375 # expected
376 "rc_type != '6'"
379 'hidecategorization' => 1
381 "rc conditions: hidecategorization=1"
385 public function testFilterUserExpLevelAll() {
386 $this->assertConditions(
388 # expected
391 'userExpLevel' => 'registered;unregistered;newcomer;learner;experienced',
393 "rc conditions: userExpLevel=registered;unregistered;newcomer;learner;experienced"
397 public function testFilterUserExpLevelRegisteredUnregistered() {
398 $this->assertConditions(
400 # expected
403 'userExpLevel' => 'registered;unregistered',
405 "rc conditions: userExpLevel=registered;unregistered"
409 public function testFilterUserExpLevelRegisteredUnregisteredLearner() {
410 $this->assertConditions(
412 # expected
415 'userExpLevel' => 'registered;unregistered;learner',
417 "rc conditions: userExpLevel=registered;unregistered;learner"
421 public function testFilterUserExpLevelAllExperienceLevels() {
422 $this->assertConditions(
424 # expected
425 'rc_user != 0',
428 'userExpLevel' => 'newcomer;learner;experienced',
430 "rc conditions: userExpLevel=newcomer;learner;experienced"
434 public function testFilterUserExpLevelRegistrered() {
435 $this->assertConditions(
437 # expected
438 'rc_user != 0',
441 'userExpLevel' => 'registered',
443 "rc conditions: userExpLevel=registered"
447 public function testFilterUserExpLevelUnregistrered() {
448 $this->assertConditions(
450 # expected
451 'rc_user' => 0,
454 'userExpLevel' => 'unregistered',
456 "rc conditions: userExpLevel=unregistered"
460 public function testFilterUserExpLevelRegistreredOrLearner() {
461 $this->assertConditions(
463 # expected
464 'rc_user != 0',
467 'userExpLevel' => 'registered;learner',
469 "rc conditions: userExpLevel=registered;learner"
473 public function testFilterUserExpLevelUnregistreredOrExperienced() {
474 $conds = $this->buildQuery( [ 'userExpLevel' => 'unregistered;experienced' ] );
476 $this->assertRegExp(
477 '/\(rc_user = 0\) OR \(\(user_editcount >= 500\) AND \(user_registration <= \'[^\']+\'\)\)/',
478 reset( $conds ),
479 "rc conditions: userExpLevel=unregistered;experienced"
483 public function testFilterUserExpLevel() {
484 $now = time();
485 $this->setMwGlobals( [
486 'wgLearnerEdits' => 10,
487 'wgLearnerMemberSince' => 4,
488 'wgExperiencedUserEdits' => 500,
489 'wgExperiencedUserMemberSince' => 30,
490 ] );
492 $this->createUsers( [
493 'Newcomer1' => [ 'edits' => 2, 'days' => 2 ],
494 'Newcomer2' => [ 'edits' => 12, 'days' => 3 ],
495 'Newcomer3' => [ 'edits' => 8, 'days' => 5 ],
496 'Learner1' => [ 'edits' => 15, 'days' => 10 ],
497 'Learner2' => [ 'edits' => 450, 'days' => 20 ],
498 'Learner3' => [ 'edits' => 460, 'days' => 33 ],
499 'Learner4' => [ 'edits' => 525, 'days' => 28 ],
500 'Experienced1' => [ 'edits' => 538, 'days' => 33 ],
501 ], $now );
503 // newcomers only
504 $this->assertArrayEquals(
505 [ 'Newcomer1', 'Newcomer2', 'Newcomer3' ],
506 $this->fetchUsers( [ 'newcomer' ], $now )
509 // newcomers and learner
510 $this->assertArrayEquals(
512 'Newcomer1', 'Newcomer2', 'Newcomer3',
513 'Learner1', 'Learner2', 'Learner3', 'Learner4',
515 $this->fetchUsers( [ 'newcomer', 'learner' ], $now )
518 // newcomers and more learner
519 $this->assertArrayEquals(
521 'Newcomer1', 'Newcomer2', 'Newcomer3',
522 'Experienced1',
524 $this->fetchUsers( [ 'newcomer', 'experienced' ], $now )
527 // learner only
528 $this->assertArrayEquals(
529 [ 'Learner1', 'Learner2', 'Learner3', 'Learner4' ],
530 $this->fetchUsers( [ 'learner' ], $now )
533 // more experienced only
534 $this->assertArrayEquals(
535 [ 'Experienced1' ],
536 $this->fetchUsers( [ 'experienced' ], $now )
539 // learner and more experienced
540 $this->assertArrayEquals(
542 'Learner1', 'Learner2', 'Learner3', 'Learner4',
543 'Experienced1',
545 $this->fetchUsers( [ 'learner', 'experienced' ], $now ),
546 'Learner and more experienced'
550 private function createUsers( $specs, $now ) {
551 $dbw = wfGetDB( DB_MASTER );
552 foreach ( $specs as $name => $spec ) {
553 User::createNew(
554 $name,
556 'editcount' => $spec['edits'],
557 'registration' => $dbw->timestamp( $this->daysAgo( $spec['days'], $now ) ),
558 'email' => 'ut',
564 private function fetchUsers( $filters, $now ) {
565 $tables = [];
566 $conds = [];
567 $fields = [];
568 $query_options = [];
569 $join_conds = [];
571 sort( $filters );
573 call_user_func_array(
574 [ $this->changesListSpecialPage, 'filterOnUserExperienceLevel' ],
576 get_class( $this->changesListSpecialPage ),
577 $this->changesListSpecialPage->getContext(),
578 $this->changesListSpecialPage->getDB(),
579 &$tables,
580 &$fields,
581 &$conds,
582 &$query_options,
583 &$join_conds,
584 $filters,
585 $now
589 $result = wfGetDB( DB_MASTER )->select(
590 $tables,
591 'user_name',
592 array_filter( $conds ) + [ 'user_email' => 'ut' ]
595 $usernames = [];
596 foreach ( $result as $row ) {
597 $usernames[] = $row->user_name;
600 return $usernames;
603 private function daysAgo( $days, $now ) {
604 $secondsPerDay = 86400;
605 return $now - $days * $secondsPerDay;
608 public function testGetFilterGroupDefinitionFromLegacyCustomFilters() {
609 $customFilters = [
610 'hidefoo' => [
611 'msg' => 'showhidefoo',
612 'default' => true,
615 'hidebar' => [
616 'msg' => 'showhidebar',
617 'default' => false,
621 $this->assertEquals(
623 'name' => 'unstructured',
624 'class' => ChangesListBooleanFilterGroup::class,
625 'priority' => -1,
626 'filters' => [
628 'name' => 'hidefoo',
629 'showHide' => 'showhidefoo',
630 'default' => true,
633 'name' => 'hidebar',
634 'showHide' => 'showhidebar',
635 'default' => false,
639 $this->changesListSpecialPage->getFilterGroupDefinitionFromLegacyCustomFilters(
640 $customFilters
645 public function testGetStructuredFilterJsData() {
646 $this->changesListSpecialPage->filterGroups = [];
648 $definition = [
650 'name' => 'gub-group',
651 'title' => 'gub-group-title',
652 'class' => ChangesListBooleanFilterGroup::class,
653 'filters' => [
655 'name' => 'hidefoo',
656 'label' => 'foo-label',
657 'description' => 'foo-description',
658 'default' => true,
659 'showHide' => 'showhidefoo',
660 'priority' => 2,
663 'name' => 'hidebar',
664 'label' => 'bar-label',
665 'description' => 'bar-description',
666 'default' => false,
667 'priority' => 4,
673 'name' => 'des-group',
674 'title' => 'des-group-title',
675 'class' => ChangesListStringOptionsFilterGroup::class,
676 'isFullCoverage' => true,
677 'filters' => [
679 'name' => 'grault',
680 'label' => 'grault-label',
681 'description' => 'grault-description',
684 'name' => 'garply',
685 'label' => 'garply-label',
686 'description' => 'garply-description',
689 'queryCallable' => function () {
691 'default' => ChangesListStringOptionsFilterGroup::NONE,
695 'name' => 'unstructured',
696 'class' => ChangesListBooleanFilterGroup::class,
697 'filters' => [
699 'name' => 'hidethud',
700 'showHide' => 'showhidethud',
701 'default' => true,
705 'name' => 'hidemos',
706 'showHide' => 'showhidemos',
707 'default' => false,
714 $this->changesListSpecialPage->registerFiltersFromDefinitions( $definition );
716 $this->assertArrayEquals(
718 // Filters that only display in the unstructured UI are
719 // are not included, and neither are groups that would
720 // be empty due to the above.
721 'groups' => [
723 'name' => 'gub-group',
724 'title' => 'gub-group-title',
725 'type' => ChangesListBooleanFilterGroup::TYPE,
726 'priority' => -1,
727 'filters' => [
729 'name' => 'hidebar',
730 'label' => 'bar-label',
731 'description' => 'bar-description',
732 'default' => false,
733 'priority' => 4,
734 'cssClass' => null,
735 'conflicts' => [],
736 'subset' => [],
739 'name' => 'hidefoo',
740 'label' => 'foo-label',
741 'description' => 'foo-description',
742 'default' => true,
743 'priority' => 2,
744 'cssClass' => null,
745 'conflicts' => [],
746 'subset' => [],
749 'fullCoverage' => true,
750 'conflicts' => [],
754 'name' => 'des-group',
755 'title' => 'des-group-title',
756 'type' => ChangesListStringOptionsFilterGroup::TYPE,
757 'priority' => -2,
758 'fullCoverage' => true,
759 'filters' => [
761 'name' => 'grault',
762 'label' => 'grault-label',
763 'description' => 'grault-description',
764 'cssClass' => null,
765 'priority' => -2,
766 'conflicts' => [],
767 'subset' => [],
770 'name' => 'garply',
771 'label' => 'garply-label',
772 'description' => 'garply-description',
773 'cssClass' => null,
774 'priority' => -3,
775 'conflicts' => [],
776 'subset' => [],
779 'conflicts' => [],
780 'separator' => ';',
781 'default' => ChangesListStringOptionsFilterGroup::NONE,
784 'messageKeys' => [
785 'gub-group-title',
786 'bar-label',
787 'bar-description',
788 'foo-label',
789 'foo-description',
790 'des-group-title',
791 'grault-label',
792 'grault-description',
793 'garply-label',
794 'garply-description',
797 $this->changesListSpecialPage->getStructuredFilterJsData(),
798 /** ordered= */ false,
799 /** named= */ true
803 public function provideParseParameters() {
804 return [
805 [ 'hidebots', [ 'hidebots' => true ] ],
807 [ 'bots', [ 'hidebots' => false ] ],
809 [ 'hideminor', [ 'hideminor' => true ] ],
811 [ 'minor', [ 'hideminor' => false ] ],
813 [ 'hidemajor', [ 'hidemajor' => true ] ],
815 [ 'hideliu', [ 'hideliu' => true ] ],
817 [ 'hidepatrolled', [ 'hidepatrolled' => true ] ],
819 [ 'hideunpatrolled', [ 'hideunpatrolled' => true ] ],
821 [ 'hideanons', [ 'hideanons' => true ] ],
823 [ 'hidemyself', [ 'hidemyself' => true ] ],
825 [ 'hidebyothers', [ 'hidebyothers' => true ] ],
827 [ 'hidehumans', [ 'hidehumans' => true ] ],
829 [ 'hidepageedits', [ 'hidepageedits' => true ] ],
831 [ 'pagedits', [ 'hidepageedits' => false ] ],
833 [ 'hidenewpages', [ 'hidenewpages' => true ] ],
835 [ 'hidecategorization', [ 'hidecategorization' => true ] ],
837 [ 'hidelog', [ 'hidelog' => true ] ],
840 'userExpLevel=learner;experienced',
842 'userExpLevel' => 'learner;experienced'
846 // A few random combos
848 'bots,hideliu,hidemyself',
850 'hidebots' => false,
851 'hideliu' => true,
852 'hidemyself' => true,
857 'minor,hideanons,categorization',
859 'hideminor' => false,
860 'hideanons' => true,
861 'hidecategorization' => false,
866 'hidehumans,bots,hidecategorization',
868 'hidehumans' => true,
869 'hidebots' => false,
870 'hidecategorization' => true,
875 'hidemyself,userExpLevel=newcomer;learner,hideminor',
877 'hidemyself' => true,
878 'hideminor' => true,
879 'userExpLevel' => 'newcomer;learner',
885 public function provideGetFilterConflicts() {
886 return [
888 "parameters" => [],
889 "expectedConflicts" => false,
892 "parameters" => [
893 "hideliu" => true,
894 "userExpLevel" => "newcomer",
896 "expectedConflicts" => false,
899 "parameters" => [
900 "hideanons" => true,
901 "userExpLevel" => "learner",
903 "expectedConflicts" => false,
906 "parameters" => [
907 "hidemajor" => true,
908 "hidenewpages" => true,
909 "hidepageedits" => true,
910 "hidecategorization" => false,
911 "hidelog" => true,
912 "hideWikidata" => true,
914 "expectedConflicts" => true,
917 "parameters" => [
918 "hidemajor" => true,
919 "hidenewpages" => false,
920 "hidepageedits" => true,
921 "hidecategorization" => false,
922 "hidelog" => false,
923 "hideWikidata" => true,
925 "expectedConflicts" => true,
928 "parameters" => [
929 "hidemajor" => true,
930 "hidenewpages" => false,
931 "hidepageedits" => false,
932 "hidecategorization" => true,
933 "hidelog" => true,
934 "hideWikidata" => true,
936 "expectedConflicts" => false,
939 "parameters" => [
940 "hideminor" => true,
941 "hidenewpages" => true,
942 "hidepageedits" => true,
943 "hidecategorization" => false,
944 "hidelog" => true,
945 "hideWikidata" => true,
947 "expectedConflicts" => false,
953 * @dataProvider provideGetFilterConflicts
955 public function testGetFilterConflicts( $parameters, $expectedConflicts ) {
956 $context = new RequestContext;
957 $context->setRequest( new FauxRequest( $parameters ) );
958 $this->changesListSpecialPage->setContext( $context );
960 $this->assertEquals(
961 $expectedConflicts,
962 $this->changesListSpecialPage->areFiltersInConflict()
966 public function validateOptionsProvider() {
967 return [
969 [ 'hideanons' => 1, 'hideliu' => 1, 'hidebots' => 1 ],
970 true,
971 [ 'hideliu' => 1, 'hidebots' => 1, ],
975 [ 'hideanons' => 1, 'hideliu' => 1, 'hidebots' => 0 ],
976 true,
977 [ 'hidebots' => 0, 'hidehumans' => 1 ],
981 [ 'hidemyself' => 1, 'hidebyothers' => 1 ],
982 true,
986 [ 'hidebots' => 1, 'hidehumans' => 1 ],
987 true,
991 [ 'hidepatrolled' => 1, 'hideunpatrolled' => 1 ],
992 true,
996 [ 'hideminor' => 1, 'hidemajor' => 1 ],
997 true,
1001 // changeType
1002 [ 'hidepageedits' => 1, 'hidenewpages' => 1, 'hidecategorization' => 1, 'hidelog' => 1, ],
1003 true,