Simplify TitleTest::testWgWhitelistReadRegexp()
authorKunal Mehta <legoktm@gmail.com>
Sat, 28 Mar 2015 05:22:51 +0000 (22:22 -0700)
committerKunal Mehta <legoktm@gmail.com>
Sat, 28 Mar 2015 05:22:51 +0000 (22:22 -0700)
* Use setMwGlobals instead of doing it manually
* Give user no rights to avoid a database query

Change-Id: I3ab20972314e39bb81bc97c89fce7a4c0d331fe6

tests/phpunit/includes/TitleTest.php

index 01c2578..d55f958 100644 (file)
@@ -325,36 +325,19 @@ class TitleTest extends MediaWikiTestCase {
                        $whitelistRegexp = array( $whitelistRegexp );
                }
 
+               $this->setMwGlobals( array(
+                       // So User::isEveryoneAllowed( 'read' ) === false
+                       'wgGroupPermissions' => array( '*' => array( 'read' => false ) ),
+                       'wgWhitelistRead' => array( 'some random non sense title' ),
+                       'wgWhitelistReadRegexp' => $whitelistRegexp,
+               ) );
+
                $title = Title::newFromDBkey( $source );
 
-               global $wgGroupPermissions;
-               $oldPermissions = $wgGroupPermissions;
-               // Disallow all so we can ensure our regex works
-               $wgGroupPermissions = array();
-               $wgGroupPermissions['*']['read'] = false;
-
-               global $wgWhitelistRead;
-               $oldWhitelist = $wgWhitelistRead;
-               // Undo any LocalSettings explicite whitelists so they won't cause a
-               // failing test to succeed. Set it to some random non sense just
-               // to make sure we properly test Title::checkReadPermissions()
-               $wgWhitelistRead = array( 'some random non sense title' );
-
-               global $wgWhitelistReadRegexp;
-               $oldWhitelistRegexp = $wgWhitelistReadRegexp;
-               $wgWhitelistReadRegexp = $whitelistRegexp;
-
-               // Just use $wgUser which in test is a user object for '127.0.0.1'
-               global $wgUser;
-               // Invalidate user rights cache to take in account $wgGroupPermissions
-               // change above.
-               $wgUser->clearInstanceCache();
-               $errors = $title->userCan( $action, $wgUser );
-
-               // Restore globals
-               $wgGroupPermissions = $oldPermissions;
-               $wgWhitelistRead = $oldWhitelist;
-               $wgWhitelistReadRegexp = $oldWhitelistRegexp;
+               // New anonymous user with no rights
+               $user = new User;
+               $user->mRights = array();
+               $errors = $title->userCan( $action, $user );
 
                if ( is_bool( $expected ) ) {
                        # Forge the assertion message depending on the assertion expectation