MediaWiki  REL1_19
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 {
00010         protected $savedGroupPermissions, $savedRevokedPermissions;
00011 
00015         protected $user;
00016 
00017         public function setUp() {
00018                 parent::setUp();
00019 
00020                 $this->savedGroupPermissions = $GLOBALS['wgGroupPermissions'];
00021                 $this->savedRevokedPermissions = $GLOBALS['wgRevokePermissions'];
00022 
00023                 $this->setUpPermissionGlobals();
00024                 $this->setUpUser();
00025         }
00026         private function setUpPermissionGlobals() {
00027                 global $wgGroupPermissions, $wgRevokePermissions;
00028 
00029                 # Data for regular $wgGroupPermissions test
00030                 $wgGroupPermissions['unittesters'] = array(
00031                         'test' => true,
00032                         'runtest' => true,
00033                         'writetest' => false,
00034                         'nukeworld' => false,
00035                 );
00036                 $wgGroupPermissions['testwriters'] = array(
00037                         'test' => true,
00038                         'writetest' => true,
00039                         'modifytest' => true,
00040                 );
00041                 # Data for regular $wgRevokePermissions test
00042                 $wgRevokePermissions['formertesters'] = array(
00043                         'runtest' => true,
00044                 );
00045         }
00046         private function setUpUser() {
00047                 $this->user = new User;
00048                 $this->user->addGroup( 'unittesters' );
00049         }
00050 
00051         public function tearDown() {
00052                 parent::tearDown();
00053 
00054                 $GLOBALS['wgGroupPermissions'] = $this->savedGroupPermissions;
00055                 $GLOBALS['wgRevokePermissions'] = $this->savedRevokedPermissions;
00056         }
00057 
00058         public function testGroupPermissions() {
00059                 $rights = User::getGroupPermissions( array( 'unittesters' ) );
00060                 $this->assertContains( 'runtest', $rights );
00061                 $this->assertNotContains( 'writetest', $rights );
00062                 $this->assertNotContains( 'modifytest', $rights );
00063                 $this->assertNotContains( 'nukeworld', $rights );
00064 
00065                 $rights = User::getGroupPermissions( array( 'unittesters', 'testwriters' ) );
00066                 $this->assertContains( 'runtest', $rights );
00067                 $this->assertContains( 'writetest', $rights );
00068                 $this->assertContains( 'modifytest', $rights );
00069                 $this->assertNotContains( 'nukeworld', $rights );
00070         }
00071         public function testRevokePermissions() {
00072                 $rights = User::getGroupPermissions( array( 'unittesters', 'formertesters' ) );
00073                 $this->assertNotContains( 'runtest', $rights );
00074                 $this->assertNotContains( 'writetest', $rights );
00075                 $this->assertNotContains( 'modifytest', $rights );
00076                 $this->assertNotContains( 'nukeworld', $rights );
00077         }
00078 
00079         public function testUserPermissions() {
00080                 $rights = $this->user->getRights();
00081                 $this->assertContains( 'runtest', $rights );
00082                 $this->assertNotContains( 'writetest', $rights );
00083                 $this->assertNotContains( 'modifytest', $rights );
00084                 $this->assertNotContains( 'nukeworld', $rights );
00085         }
00086 
00090         public function testGetGroupsWithPermission( $expected, $right ) {
00091                 $result = User::getGroupsWithPermission( $right );
00092                 sort( $result );
00093                 sort( $expected );
00094 
00095                 $this->assertEquals( $expected, $result, "Groups with permission $right" );
00096         }
00097 
00098         public function provideGetGroupsWithPermission() {
00099                 return array(
00100                         array(
00101                                 array( 'unittesters', 'testwriters' ),
00102                                 'test'
00103                         ),
00104                         array(
00105                                 array( 'unittesters' ),
00106                                 'runtest'
00107                         ),
00108                         array(
00109                                 array( 'testwriters' ),
00110                                 'writetest'
00111                         ),
00112                         array(
00113                                 array( 'testwriters' ),
00114                                 'modifytest'
00115                         ),
00116                 );
00117         }
00118 
00122         public function testIsValidUserName( $username, $result, $message ) {
00123                 $this->assertEquals( $this->user->isValidUserName( $username ), $result, $message );
00124         }
00125 
00126         public function provideUserNames() {
00127                 return array(
00128                         array( '', false, 'Empty string' ),
00129                         array( ' ', false, 'Blank space' ),
00130                         array( 'abcd', false, 'Starts with small letter' ),
00131                         array( 'Ab/cd', false,  'Contains slash' ),
00132                         array( 'Ab cd' , true, 'Whitespace' ),
00133                         array( '192.168.1.1', false,  'IP' ),
00134                         array( 'User:Abcd', false, 'Reserved Namespace' ),
00135                         array( '12abcd232' , true  , 'Starts with Numbers' ),
00136                         array( '?abcd' , true,  'Start with ? mark' ),
00137                         array( '#abcd', false, 'Start with #' ),
00138                         array( 'Abcdകഖഗഘ', true,  ' Mixed scripts' ),
00139                         array( 'ജോസ്‌തോമസ്',  false, 'ZWNJ- Format control character' ),
00140                         array( 'Ab cd', false, ' Ideographic space' ),
00141                 );
00142         }
00143 }