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(
51 public function testGroupPermissions() {
52 $rights = User
::getGroupPermissions( array( 'unittesters' ) );
53 $this->assertContains( 'runtest', $rights );
54 $this->assertNotContains( 'writetest', $rights );
55 $this->assertNotContains( 'modifytest', $rights );
56 $this->assertNotContains( 'nukeworld', $rights );
58 $rights = User
::getGroupPermissions( array( 'unittesters', 'testwriters' ) );
59 $this->assertContains( 'runtest', $rights );
60 $this->assertContains( 'writetest', $rights );
61 $this->assertContains( 'modifytest', $rights );
62 $this->assertNotContains( 'nukeworld', $rights );
65 public function testRevokePermissions() {
66 $rights = User
::getGroupPermissions( array( 'unittesters', 'formertesters' ) );
67 $this->assertNotContains( 'runtest', $rights );
68 $this->assertNotContains( 'writetest', $rights );
69 $this->assertNotContains( 'modifytest', $rights );
70 $this->assertNotContains( 'nukeworld', $rights );
73 public function testUserPermissions() {
74 $rights = $this->user
->getRights();
75 $this->assertContains( 'runtest', $rights );
76 $this->assertNotContains( 'writetest', $rights );
77 $this->assertNotContains( 'modifytest', $rights );
78 $this->assertNotContains( 'nukeworld', $rights );
82 * @dataProvider provideGetGroupsWithPermission
84 public function testGetGroupsWithPermission( $expected, $right ) {
85 $result = User
::getGroupsWithPermission( $right );
89 $this->assertEquals( $expected, $result, "Groups with permission $right" );
92 public static function provideGetGroupsWithPermission() {
95 array( 'unittesters', 'testwriters' ),
99 array( 'unittesters' ),
103 array( 'testwriters' ),
107 array( 'testwriters' ),
114 * @dataProvider provideUserNames
116 public function testIsValidUserName( $username, $result, $message ) {
117 $this->assertEquals( $this->user
->isValidUserName( $username ), $result, $message );
120 public static function provideUserNames() {
122 array( '', false, 'Empty string' ),
123 array( ' ', false, 'Blank space' ),
124 array( 'abcd', false, 'Starts with small letter' ),
125 array( 'Ab/cd', false, 'Contains slash' ),
126 array( 'Ab cd', true, 'Whitespace' ),
127 array( '192.168.1.1', false, 'IP' ),
128 array( 'User:Abcd', false, 'Reserved Namespace' ),
129 array( '12abcd232', true, 'Starts with Numbers' ),
130 array( '?abcd', true, 'Start with ? mark' ),
131 array( '#abcd', false, 'Start with #' ),
132 array( 'Abcdകഖഗഘ', true, ' Mixed scripts' ),
133 array( 'ജോസ്തോമസ്', false, 'ZWNJ- Format control character' ),
134 array( 'Ab cd', false, ' Ideographic space' ),
139 * Test, if for all rights a right- message exist,
140 * which is used on Special:ListGroupRights as help text
141 * Extensions and core
143 public function testAllRightsWithMessage() {
144 //Getting all user rights, for core: User::$mCoreRights, for extensions: $wgAvailableRights
145 $allRights = User
::getAllRights();
146 $allMessageKeys = Language
::getMessageKeysFor( 'en' );
148 $rightsWithMessage = array();
149 foreach ( $allMessageKeys as $message ) {
150 // === 0: must be at beginning of string (position 0)
151 if ( strpos( $message, 'right-' ) === 0 ) {
152 $rightsWithMessage[] = substr( $message, strlen( 'right-' ) );
157 sort( $rightsWithMessage );
162 'Each user rights (core/extensions) has a corresponding right- message.'
167 * Test User::editCount
170 public function testEditCount() {
171 $user = User
::newFromName( 'UnitTestUser' );
172 $user->loadDefaults();
173 $user->addToDatabase();
175 // let the user have a few (3) edits
176 $page = WikiPage
::factory( Title
::newFromText( 'Help:UserTest_EditCount' ) );
177 for ( $i = 0; $i < 3; $i++
) {
178 $page->doEdit( (string)$i, 'test', 0, false, $user );
181 $user->clearInstanceCache();
182 $this->assertEquals( 3, $user->getEditCount(), 'After three edits, the user edit count should be 3' );
184 // increase the edit count and clear the cache
185 $user->incEditCount();
187 $user->clearInstanceCache();
188 $this->assertEquals( 4, $user->getEditCount(), 'After increasing the edit count manually, the user edit count should be 4' );
192 * Test changing user options.
194 public function testOptions() {
195 $user = User
::newFromName( 'UnitTestUser' );
196 $user->addToDatabase();
198 $user->setOption( 'someoption', 'test' );
199 $user->setOption( 'cols', 200 );
200 $user->saveSettings();
202 $user = User
::newFromName( 'UnitTestUser' );
203 $this->assertEquals( 'test', $user->getOption( 'someoption' ) );
204 $this->assertEquals( 200, $user->getOption( 'cols' ) );
209 * Make sure defaults are loaded when setOption is called.
211 public function testAnonOptions() {
212 global $wgDefaultUserOptions;
213 $this->user
->setOption( 'someoption', 'test' );
214 $this->assertEquals( $wgDefaultUserOptions['cols'], $this->user
->getOption( 'cols' ) );
215 $this->assertEquals( 'test', $this->user
->getOption( 'someoption' ) );