3 define( 'NS_UNITTEST', 5600 );
4 define( 'NS_UNITTEST_TALK', 5601 );
9 class UserTest
extends MediaWikiTestCase
{
16 protected function setUp() {
19 $this->setMwGlobals( array(
20 'wgGroupPermissions' => array(),
21 'wgRevokePermissions' => array(),
24 $this->setUpPermissionGlobals();
26 $this->user
= new User
;
27 $this->user
->addGroup( 'unittesters' );
30 private function setUpPermissionGlobals() {
31 global $wgGroupPermissions, $wgRevokePermissions;
33 # Data for regular $wgGroupPermissions test
34 $wgGroupPermissions['unittesters'] = array(
40 $wgGroupPermissions['testwriters'] = array(
46 # Data for regular $wgRevokePermissions test
47 $wgRevokePermissions['formertesters'] = array(
52 public function testGroupPermissions() {
53 $rights = User
::getGroupPermissions( array( 'unittesters' ) );
54 $this->assertContains( 'runtest', $rights );
55 $this->assertNotContains( 'writetest', $rights );
56 $this->assertNotContains( 'modifytest', $rights );
57 $this->assertNotContains( 'nukeworld', $rights );
59 $rights = User
::getGroupPermissions( array( 'unittesters', 'testwriters' ) );
60 $this->assertContains( 'runtest', $rights );
61 $this->assertContains( 'writetest', $rights );
62 $this->assertContains( 'modifytest', $rights );
63 $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
169 public function testEditCount() {
170 $user = User
::newFromName( 'UnitTestUser' );
171 $user->loadDefaults();
172 $user->addToDatabase();
174 // let the user have a few (10) edits
175 $page = WikiPage
::factory( Title
::newFromText( 'UserTest_EditCount' ) );
176 for( $i = 0; $i < 10; $i++
) {
177 $page->doEdit( (string) $i, 'test', 0, false, $user );
180 $user->clearInstanceCache();
181 $this->assertEquals( 10, $user->getEditCount(), 'After ten edits, the user edit count should be 10' );
183 // increase the edit count and clear the cache
184 $user->incEditCount();
186 $user->clearInstanceCache();
187 $this->assertEquals( 11, $user->getEditCount(), 'After increasing the edit count manually, the user edit count should be 11' );