MediaWiki  REL1_22
UserTest.php
Go to the documentation of this file.
00001 <?php
00002 
00003 define( 'NS_UNITTEST', 5600 );
00004 define( 'NS_UNITTEST_TALK', 5601 );
00005 
00009 class UserTest extends MediaWikiTestCase {
00013     protected $user;
00014 
00015     protected function setUp() {
00016         parent::setUp();
00017 
00018         $this->setMwGlobals( array(
00019             'wgGroupPermissions' => array(),
00020             'wgRevokePermissions' => array(),
00021         ) );
00022 
00023         $this->setUpPermissionGlobals();
00024 
00025         $this->user = new User;
00026         $this->user->addGroup( 'unittesters' );
00027     }
00028 
00029     private function setUpPermissionGlobals() {
00030         global $wgGroupPermissions, $wgRevokePermissions;
00031 
00032         # Data for regular $wgGroupPermissions test
00033         $wgGroupPermissions['unittesters'] = array(
00034             'test' => true,
00035             'runtest' => true,
00036             'writetest' => false,
00037             'nukeworld' => false,
00038         );
00039         $wgGroupPermissions['testwriters'] = array(
00040             'test' => true,
00041             'writetest' => true,
00042             'modifytest' => true,
00043         );
00044 
00045         # Data for regular $wgRevokePermissions test
00046         $wgRevokePermissions['formertesters'] = array(
00047             'runtest' => true,
00048         );
00049 
00050         # For the options test
00051         $wgGroupPermissions['*'] = array(
00052             'editmyoptions' => true,
00053         );
00054     }
00055 
00059     public function testGroupPermissions() {
00060         $rights = User::getGroupPermissions( array( 'unittesters' ) );
00061         $this->assertContains( 'runtest', $rights );
00062         $this->assertNotContains( 'writetest', $rights );
00063         $this->assertNotContains( 'modifytest', $rights );
00064         $this->assertNotContains( 'nukeworld', $rights );
00065 
00066         $rights = User::getGroupPermissions( array( 'unittesters', 'testwriters' ) );
00067         $this->assertContains( 'runtest', $rights );
00068         $this->assertContains( 'writetest', $rights );
00069         $this->assertContains( 'modifytest', $rights );
00070         $this->assertNotContains( 'nukeworld', $rights );
00071     }
00072 
00076     public function testRevokePermissions() {
00077         $rights = User::getGroupPermissions( array( 'unittesters', 'formertesters' ) );
00078         $this->assertNotContains( 'runtest', $rights );
00079         $this->assertNotContains( 'writetest', $rights );
00080         $this->assertNotContains( 'modifytest', $rights );
00081         $this->assertNotContains( 'nukeworld', $rights );
00082     }
00083 
00087     public function testUserPermissions() {
00088         $rights = $this->user->getRights();
00089         $this->assertContains( 'runtest', $rights );
00090         $this->assertNotContains( 'writetest', $rights );
00091         $this->assertNotContains( 'modifytest', $rights );
00092         $this->assertNotContains( 'nukeworld', $rights );
00093     }
00094 
00099     public function testGetGroupsWithPermission( $expected, $right ) {
00100         $result = User::getGroupsWithPermission( $right );
00101         sort( $result );
00102         sort( $expected );
00103 
00104         $this->assertEquals( $expected, $result, "Groups with permission $right" );
00105     }
00106 
00107     public static function provideGetGroupsWithPermission() {
00108         return array(
00109             array(
00110                 array( 'unittesters', 'testwriters' ),
00111                 'test'
00112             ),
00113             array(
00114                 array( 'unittesters' ),
00115                 'runtest'
00116             ),
00117             array(
00118                 array( 'testwriters' ),
00119                 'writetest'
00120             ),
00121             array(
00122                 array( 'testwriters' ),
00123                 'modifytest'
00124             ),
00125         );
00126     }
00127 
00132     public function testIsValidUserName( $username, $result, $message ) {
00133         $this->assertEquals( $this->user->isValidUserName( $username ), $result, $message );
00134     }
00135 
00136     public static function provideUserNames() {
00137         return array(
00138             array( '', false, 'Empty string' ),
00139             array( ' ', false, 'Blank space' ),
00140             array( 'abcd', false, 'Starts with small letter' ),
00141             array( 'Ab/cd', false, 'Contains slash' ),
00142             array( 'Ab cd', true, 'Whitespace' ),
00143             array( '192.168.1.1', false, 'IP' ),
00144             array( 'User:Abcd', false, 'Reserved Namespace' ),
00145             array( '12abcd232', true, 'Starts with Numbers' ),
00146             array( '?abcd', true, 'Start with ? mark' ),
00147             array( '#abcd', false, 'Start with #' ),
00148             array( 'Abcdകഖഗഘ', true, ' Mixed scripts' ),
00149             array( 'ജോസ്‌തോമസ്', false, 'ZWNJ- Format control character' ),
00150             array( 'Ab cd', false, ' Ideographic space' ),
00151         );
00152     }
00153 
00159     public function testAllRightsWithMessage() {
00160         //Getting all user rights, for core: User::$mCoreRights, for extensions: $wgAvailableRights
00161         $allRights = User::getAllRights();
00162         $allMessageKeys = Language::getMessageKeysFor( 'en' );
00163 
00164         $rightsWithMessage = array();
00165         foreach ( $allMessageKeys as $message ) {
00166             // === 0: must be at beginning of string (position 0)
00167             if ( strpos( $message, 'right-' ) === 0 ) {
00168                 $rightsWithMessage[] = substr( $message, strlen( 'right-' ) );
00169             }
00170         }
00171 
00172         sort( $allRights );
00173         sort( $rightsWithMessage );
00174 
00175         $this->assertEquals(
00176             $allRights,
00177             $rightsWithMessage,
00178             'Each user rights (core/extensions) has a corresponding right- message.'
00179         );
00180     }
00181 
00187     public function testEditCount() {
00188         $user = User::newFromName( 'UnitTestUser' );
00189         $user->loadDefaults();
00190         $user->addToDatabase();
00191 
00192         // let the user have a few (3) edits
00193         $page = WikiPage::factory( Title::newFromText( 'Help:UserTest_EditCount' ) );
00194         for ( $i = 0; $i < 3; $i++ ) {
00195             $page->doEdit( (string)$i, 'test', 0, false, $user );
00196         }
00197 
00198         $user->clearInstanceCache();
00199         $this->assertEquals( 3, $user->getEditCount(), 'After three edits, the user edit count should be 3' );
00200 
00201         // increase the edit count and clear the cache
00202         $user->incEditCount();
00203 
00204         $user->clearInstanceCache();
00205         $this->assertEquals( 4, $user->getEditCount(), 'After increasing the edit count manually, the user edit count should be 4' );
00206     }
00207 
00213     public function testOptions() {
00214         $user = User::newFromName( 'UnitTestUser' );
00215         $user->addToDatabase();
00216 
00217         $user->setOption( 'someoption', 'test' );
00218         $user->setOption( 'cols', 200 );
00219         $user->saveSettings();
00220 
00221         $user = User::newFromName( 'UnitTestUser' );
00222         $this->assertEquals( 'test', $user->getOption( 'someoption' ) );
00223         $this->assertEquals( 200, $user->getOption( 'cols' ) );
00224     }
00225 
00231     public function testAnonOptions() {
00232         global $wgDefaultUserOptions;
00233         $this->user->setOption( 'someoption', 'test' );
00234         $this->assertEquals( $wgDefaultUserOptions['cols'], $this->user->getOption( 'cols' ) );
00235         $this->assertEquals( 'test', $this->user->getOption( 'someoption' ) );
00236     }
00237 }