Merge "define RC_EXTERNAL type for recent changes"
[lhc/web/wiklou.git] / tests / phpunit / includes / TitlePermissionTest.php
index 67913c6..f7387e1 100644 (file)
@@ -3,27 +3,45 @@
 /**
  * @group Database
  */
-class TitlePermissionTest extends MediaWikiTestCase {
+class TitlePermissionTest extends MediaWikiLangTestCase {
+
+       /**
+        * @var string
+        */
+       protected $userName, $altUserName;
+
+       /**
+        * @var Title
+        */
        protected $title;
-       protected $user;
-       protected $anonUser;
-       protected $userUser;
-       protected $altUser;
-       protected $userName;
-       protected $altUserName;
-
-       function setUp() {
-               global $wgLocaltimezone, $wgLocalTZoffset, $wgMemc, $wgContLang, $wgLang;
-
-               if(!$wgMemc) {
-                       $wgMemc = new EmptyBagOStuff;
-               }
-               $wgContLang = $wgLang = Language::factory( 'en' );
 
-               $this->userName = "Useruser";
-               $this->altUserName = "Altuseruser";
-               date_default_timezone_set( $wgLocaltimezone );
-               $wgLocalTZoffset = date( "Z" ) / 60;
+       /**
+        * @var User
+        */
+       protected $user, $anonUser, $userUser, $altUser;
+
+       protected function setUp() {
+               parent::setUp();
+
+               $langObj = Language::factory( 'en' );
+               $localZone = 'UTC';
+               $localOffset = date( 'Z' ) / 60;
+
+               $this->setMwGlobals( array(
+                       'wgMemc' => new EmptyBagOStuff,
+                       'wgContLang' => $langObj,
+                       'wgLang' => $langObj,
+                       'wgLocaltimezone' => $localZone,
+                       'wgLocalTZoffset' => $localOffset,
+                       'wgNamespaceProtection' => array(
+                               NS_MEDIAWIKI => 'editinterface',
+                       ),
+                       'wgUser' => null,
+               ) );
+
+               $this->userName = 'Useruser';
+               $this->altUserName = 'Altuseruser';
+               date_default_timezone_set( $localZone );
 
                $this->title = Title::makeTitle( NS_MAIN, "Main Page" );
                if ( !isset( $this->userUser ) || !( $this->userUser instanceOf User ) ) {
@@ -48,9 +66,12 @@ class TitlePermissionTest extends MediaWikiTestCase {
 
                        $this->user = $this->userUser;
                }
+
        }
 
        function setUserPerm( $perm ) {
+               // Setting member variables is evil!!!
+
                if ( is_array( $perm ) ) {
                        $this->user->mRights = $perm;
                } else {
@@ -63,15 +84,16 @@ class TitlePermissionTest extends MediaWikiTestCase {
        }
 
        function setUser( $userName = null ) {
+               global $wgUser;
+
                if ( $userName === 'anon' ) {
                        $this->user = $this->anonUser;
-               } else if ( $userName === null || $userName === $this->userName ) {
+               } elseif ( $userName === null || $userName === $this->userName ) {
                        $this->user = $this->userUser;
                } else {
                        $this->user = $this->altUser;
                }
 
-               global $wgUser;
                $wgUser = $this->user;
        }
 
@@ -214,30 +236,35 @@ class TitlePermissionTest extends MediaWikiTestCase {
                $this->runGroupPermissions( 'move', array( array( 'movenotallowedfile' ), array( 'movenotallowed' ) ),
                        array( array( 'movenotallowedfile' ), array( 'movenologintext' ) ) );
 
-               $this->setTitle( NS_MAIN );
-               $this->setUser( 'anon' );
-               $this->setUserPerm( "move" );
-               $this->runGroupPermissions( 'move', array(  ) );
+               if ( $this->isWikitextNS( NS_MAIN ) ) {
+                       //NOTE: some content models don't allow moving
+                       //@todo: find a Wikitext namespace for testing
 
-               $this->setUserPerm( "" );
-               $this->runGroupPermissions( 'move', array( array( 'movenotallowed' ) ),
-                       array( array( 'movenologintext' ) ) );
+                       $this->setTitle( NS_MAIN );
+                       $this->setUser( 'anon' );
+                       $this->setUserPerm( "move" );
+                       $this->runGroupPermissions( 'move', array(  ) );
 
-               $this->setUser( $this->userName );
-               $this->setUserPerm( "" );
-               $this->runGroupPermissions( 'move', array( array( 'movenotallowed' ) ) );
+                       $this->setUserPerm( "" );
+                       $this->runGroupPermissions( 'move', array( array( 'movenotallowed' ) ),
+                               array( array( 'movenologintext' ) ) );
 
-               $this->setUserPerm( "move" );
-               $this->runGroupPermissions( 'move', array( ) );
+                       $this->setUser( $this->userName );
+                       $this->setUserPerm( "" );
+                       $this->runGroupPermissions( 'move', array( array( 'movenotallowed' ) ) );
 
-               $this->setUser( 'anon' );
-               $this->setUserPerm( 'move' );
-               $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
-               $this->assertEquals( array( ), $res );
+                       $this->setUserPerm( "move" );
+                       $this->runGroupPermissions( 'move', array( ) );
 
-               $this->setUserPerm( '' );
-               $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
-               $this->assertEquals( array( array( 'movenotallowed' ) ), $res );
+                       $this->setUser( 'anon' );
+                       $this->setUserPerm( 'move' );
+                       $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
+                       $this->assertEquals( array( ), $res );
+
+                       $this->setUserPerm( '' );
+                       $res = $this->title->getUserPermissionsErrors( 'move-target', $this->user );
+                       $this->assertEquals( array( array( 'movenotallowed' ) ), $res );
+               }
 
                $this->setTitle( NS_USER );
                $this->setUser( $this->userName );
@@ -294,7 +321,7 @@ class TitlePermissionTest extends MediaWikiTestCase {
                        $this->assertEquals( $check[$action][3],
                                $this->title->userCan( $action, true ) );
                        $this->assertEquals( $check[$action][3],
-                               $this->title->quickUserCan( $action, false ) );
+                               $this->title->quickUserCan( $action ) );
 
                        # count( User::getGroupsWithPermissions( $action ) ) < 1
                }
@@ -326,11 +353,9 @@ class TitlePermissionTest extends MediaWikiTestCase {
                $this->assertEquals( $result2, $res );
        }
 
-       function testPermissionHooks() { }
        function testSpecialsAndNSPermissions() {
+               global $wgNamespaceProtection;
                $this->setUser( $this->userName );
-               global $wgUser;
-               $wgUser = $this->user;
 
                $this->setTitle( NS_SPECIAL );
 
@@ -349,8 +374,8 @@ class TitlePermissionTest extends MediaWikiTestCase {
                $this->assertEquals( array( array( 'badaccess-group0' ) ),
                                                         $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
 
-               global $wgNamespaceProtection;
-               $wgNamespaceProtection[NS_USER] = array ( 'bogus' );
+               $wgNamespaceProtection[NS_USER] = array( 'bogus' );
+
                $this->setTitle( NS_USER );
                $this->setUserPerm( '' );
                $this->assertEquals( array( array( 'badaccess-group0' ), array( 'namespaceprotected', 'User' ) ),
@@ -367,6 +392,7 @@ class TitlePermissionTest extends MediaWikiTestCase {
                                                         $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
 
                $wgNamespaceProtection = null;
+
                $this->setUserPerm( 'bogus' );
                $this->assertEquals( array( ),
                                                         $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
@@ -380,10 +406,8 @@ class TitlePermissionTest extends MediaWikiTestCase {
                                                         $this->title->userCan( 'bogus' ) );
        }
 
-       function testCSSandJSPermissions() {
+       function testCssAndJavascriptPermissions() {
                $this->setUser( $this->userName );
-               global $wgUser;
-               $wgUser = $this->user;
 
                $this->setTitle( NS_USER, $this->altUserName . '/test.js' );
                $this->runCSSandJSPermissions(
@@ -447,7 +471,7 @@ class TitlePermissionTest extends MediaWikiTestCase {
                                                                                                                                         $this->user ) );
 
                $this->assertEquals( true,
-                                                        $this->title->quickUserCan( 'edit', false ) );
+                                                        $this->title->quickUserCan( 'edit' ) );
                $this->title->mRestrictions = array( "edit" => array( 'bogus', "sysop", "protect", "" ),
                                                                                   "bogus" => array( 'bogus', "sysop", "protect", "" ) );
 
@@ -487,9 +511,9 @@ class TitlePermissionTest extends MediaWikiTestCase {
                                                                                                                                         $this->user ) );
                $this->title->mCascadeRestriction = true;
                $this->assertEquals( false,
-                                                        $this->title->quickUserCan( 'bogus', false ) );
+                                                        $this->title->quickUserCan( 'bogus' ) );
                $this->assertEquals( false,
-                                                        $this->title->quickUserCan( 'edit', false ) );
+                                                        $this->title->quickUserCan( 'edit' ) );
                $this->assertEquals( array( array( 'badaccess-group0' ),
                                                                        array( 'protectedpagetext', 'bogus' ),
                                                                        array( 'protectedpagetext', 'protect' ),
@@ -533,7 +557,7 @@ class TitlePermissionTest extends MediaWikiTestCase {
                $this->setTitle( NS_MAIN, "test page" );
                $this->title->mTitleProtection['pt_create_perm'] = '';
                $this->title->mTitleProtection['pt_user'] = $this->user->getID();
-               $this->title->mTitleProtection['pt_expiry'] = Block::infinity();
+               $this->title->mTitleProtection['pt_expiry'] = wfGetDB( DB_SLAVE )->getInfinity();
                $this->title->mTitleProtection['pt_reason'] = 'test';
                $this->title->mCascadeRestriction = false;
 
@@ -563,14 +587,14 @@ class TitlePermissionTest extends MediaWikiTestCase {
                $this->assertEquals( array( array( 'immobile-source-namespace', 'Media' ) ),
                                                         $this->title->getUserPermissionsErrors( 'move', $this->user ) );
 
-               $this->setTitle( NS_MAIN, "test page" );
+               $this->setTitle( NS_HELP, "test page" );
                $this->assertEquals( array( ),
                                                         $this->title->getUserPermissionsErrors( 'move', $this->user ) );
                $this->assertEquals( true,
                                                         $this->title->userCan( 'move' ) );
 
                $this->title->mInterwiki = "no";
-               $this->assertEquals( array( array( 'immobile-page' ) ),
+               $this->assertEquals( array( array( 'immobile-source-page' ) ),
                                                         $this->title->getUserPermissionsErrors( 'move', $this->user ) );
                $this->assertEquals( false,
                                                         $this->title->userCan( 'move' ) );
@@ -581,7 +605,7 @@ class TitlePermissionTest extends MediaWikiTestCase {
                $this->assertEquals( array( array( 'immobile-target-namespace', 'Media' ) ),
                                                         $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
 
-               $this->setTitle( NS_MAIN, "test page" );
+               $this->setTitle( NS_HELP, "test page" );
                $this->assertEquals( array( ),
                                                         $this->title->getUserPermissionsErrors( 'move-target', $this->user ) );
                $this->assertEquals( true,
@@ -602,7 +626,7 @@ class TitlePermissionTest extends MediaWikiTestCase {
                $wgUser = $this->user;
 
                $this->setUserPerm( array( "createpage", "move" ) );
-               $this->setTitle( NS_MAIN, "test page" );
+               $this->setTitle( NS_HELP, "test page" );
 
                # $short
                $this->assertEquals( array( array( 'confirmedittext' ) ),
@@ -619,7 +643,7 @@ class TitlePermissionTest extends MediaWikiTestCase {
                $prev = time();
                $now = time() + 120;
                $this->user->mBlockedby = $this->user->getId();
-               $this->user->mBlock = new Block( '127.0.8.1', $this->user->getId(), $this->user->getId(),
+               $this->user->mBlock = new Block( '127.0.8.1', 0, $this->user->getId(),
                                                                                'no reason given', $prev + 3600, 1, 0 );
                $this->user->mBlock->mTimestamp = 0;
                $this->assertEquals( array( array( 'autoblockedtext',
@@ -636,7 +660,7 @@ class TitlePermissionTest extends MediaWikiTestCase {
                global $wgLocalTZoffset;
                $wgLocalTZoffset = -60;
                $this->user->mBlockedby = $this->user->getName();
-               $this->user->mBlock = new Block( '127.0.8.1', 2, 1, 'no reason given', $now, 0, 10 );
+               $this->user->mBlock = new Block( '127.0.8.1', 0, 1, 'no reason given', $now, 0, 10 );
                $this->assertEquals( array( array( 'blockedtext',
                        '[[User:Useruser|Useruser]]', 'no reason given', '127.0.0.1',
                        'Useruser', null, '23:00, 31 December 1969', '127.0.8.1',