3 define( 'NS_UNITTEST', 5600 );
4 define( 'NS_UNITTEST_TALK', 5601 );
9 class UserTest
extends MediaWikiTestCase
{
15 protected function setUp() {
18 $this->setMwGlobals( array(
19 'wgGroupPermissions' => array(),
20 'wgRevokePermissions' => array(),
23 $this->setUpPermissionGlobals();
25 $this->user
= new User
;
26 $this->user
->addGroup( 'unittesters' );
29 private function setUpPermissionGlobals() {
30 global $wgGroupPermissions, $wgRevokePermissions;
32 # Data for regular $wgGroupPermissions test
33 $wgGroupPermissions['unittesters'] = array(
39 $wgGroupPermissions['testwriters'] = array(
45 # Data for regular $wgRevokePermissions test
46 $wgRevokePermissions['formertesters'] = array(
50 # For the options test
51 $wgGroupPermissions['*'] = array(
52 'editmyoptions' => true,
56 public function testGroupPermissions() {
57 $rights = User
::getGroupPermissions( array( 'unittesters' ) );
58 $this->assertContains( 'runtest', $rights );
59 $this->assertNotContains( 'writetest', $rights );
60 $this->assertNotContains( 'modifytest', $rights );
61 $this->assertNotContains( 'nukeworld', $rights );
63 $rights = User
::getGroupPermissions( array( 'unittesters', 'testwriters' ) );
64 $this->assertContains( 'runtest', $rights );
65 $this->assertContains( 'writetest', $rights );
66 $this->assertContains( 'modifytest', $rights );
67 $this->assertNotContains( 'nukeworld', $rights );
70 public function testRevokePermissions() {
71 $rights = User
::getGroupPermissions( array( 'unittesters', 'formertesters' ) );
72 $this->assertNotContains( 'runtest', $rights );
73 $this->assertNotContains( 'writetest', $rights );
74 $this->assertNotContains( 'modifytest', $rights );
75 $this->assertNotContains( 'nukeworld', $rights );
78 public function testUserPermissions() {
79 $rights = $this->user
->getRights();
80 $this->assertContains( 'runtest', $rights );
81 $this->assertNotContains( 'writetest', $rights );
82 $this->assertNotContains( 'modifytest', $rights );
83 $this->assertNotContains( 'nukeworld', $rights );
87 * @dataProvider provideGetGroupsWithPermission
89 public function testGetGroupsWithPermission( $expected, $right ) {
90 $result = User
::getGroupsWithPermission( $right );
94 $this->assertEquals( $expected, $result, "Groups with permission $right" );
97 public static function provideGetGroupsWithPermission() {
100 array( 'unittesters', 'testwriters' ),
104 array( 'unittesters' ),
108 array( 'testwriters' ),
112 array( 'testwriters' ),
119 * @dataProvider provideUserNames
121 public function testIsValidUserName( $username, $result, $message ) {
122 $this->assertEquals( $this->user
->isValidUserName( $username ), $result, $message );
125 public static function provideUserNames() {
127 array( '', false, 'Empty string' ),
128 array( ' ', false, 'Blank space' ),
129 array( 'abcd', false, 'Starts with small letter' ),
130 array( 'Ab/cd', false, 'Contains slash' ),
131 array( 'Ab cd', true, 'Whitespace' ),
132 array( '192.168.1.1', false, 'IP' ),
133 array( 'User:Abcd', false, 'Reserved Namespace' ),
134 array( '12abcd232', true, 'Starts with Numbers' ),
135 array( '?abcd', true, 'Start with ? mark' ),
136 array( '#abcd', false, 'Start with #' ),
137 array( 'Abcdകഖഗഘ', true, ' Mixed scripts' ),
138 array( 'ജോസ്തോമസ്', false, 'ZWNJ- Format control character' ),
139 array( 'Ab cd', false, ' Ideographic space' ),
144 * Test, if for all rights a right- message exist,
145 * which is used on Special:ListGroupRights as help text
146 * Extensions and core
148 public function testAllRightsWithMessage() {
149 //Getting all user rights, for core: User::$mCoreRights, for extensions: $wgAvailableRights
150 $allRights = User
::getAllRights();
151 $allMessageKeys = Language
::getMessageKeysFor( 'en' );
153 $rightsWithMessage = array();
154 foreach ( $allMessageKeys as $message ) {
155 // === 0: must be at beginning of string (position 0)
156 if ( strpos( $message, 'right-' ) === 0 ) {
157 $rightsWithMessage[] = substr( $message, strlen( 'right-' ) );
162 sort( $rightsWithMessage );
167 'Each user rights (core/extensions) has a corresponding right- message.'
172 * Test User::editCount
175 public function testEditCount() {
176 $user = User
::newFromName( 'UnitTestUser' );
177 $user->loadDefaults();
178 $user->addToDatabase();
180 // let the user have a few (3) edits
181 $page = WikiPage
::factory( Title
::newFromText( 'Help:UserTest_EditCount' ) );
182 for ( $i = 0; $i < 3; $i++
) {
183 $page->doEdit( (string)$i, 'test', 0, false, $user );
186 $user->clearInstanceCache();
187 $this->assertEquals( 3, $user->getEditCount(), 'After three edits, the user edit count should be 3' );
189 // increase the edit count and clear the cache
190 $user->incEditCount();
192 $user->clearInstanceCache();
193 $this->assertEquals( 4, $user->getEditCount(), 'After increasing the edit count manually, the user edit count should be 4' );
197 * Test changing user options.
199 public function testOptions() {
200 $user = User
::newFromName( 'UnitTestUser' );
201 $user->addToDatabase();
203 $user->setOption( 'someoption', 'test' );
204 $user->setOption( 'cols', 200 );
205 $user->saveSettings();
207 $user = User
::newFromName( 'UnitTestUser' );
208 $this->assertEquals( 'test', $user->getOption( 'someoption' ) );
209 $this->assertEquals( 200, $user->getOption( 'cols' ) );
214 * Make sure defaults are loaded when setOption is called.
216 public function testAnonOptions() {
217 global $wgDefaultUserOptions;
218 $this->user
->setOption( 'someoption', 'test' );
219 $this->assertEquals( $wgDefaultUserOptions['cols'], $this->user
->getOption( 'cols' ) );
220 $this->assertEquals( 'test', $this->user
->getOption( 'someoption' ) );