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,
57 * @covers User::getGroupPermissions
59 public function testGroupPermissions() {
60 $rights = User
::getGroupPermissions( array( 'unittesters' ) );
61 $this->assertContains( 'runtest', $rights );
62 $this->assertNotContains( 'writetest', $rights );
63 $this->assertNotContains( 'modifytest', $rights );
64 $this->assertNotContains( 'nukeworld', $rights );
66 $rights = User
::getGroupPermissions( array( 'unittesters', 'testwriters' ) );
67 $this->assertContains( 'runtest', $rights );
68 $this->assertContains( 'writetest', $rights );
69 $this->assertContains( 'modifytest', $rights );
70 $this->assertNotContains( 'nukeworld', $rights );
74 * @covers User::getGroupPermissions
76 public function testRevokePermissions() {
77 $rights = User
::getGroupPermissions( array( 'unittesters', 'formertesters' ) );
78 $this->assertNotContains( 'runtest', $rights );
79 $this->assertNotContains( 'writetest', $rights );
80 $this->assertNotContains( 'modifytest', $rights );
81 $this->assertNotContains( 'nukeworld', $rights );
85 * @covers User::getRights
87 public function testUserPermissions() {
88 $rights = $this->user
->getRights();
89 $this->assertContains( 'runtest', $rights );
90 $this->assertNotContains( 'writetest', $rights );
91 $this->assertNotContains( 'modifytest', $rights );
92 $this->assertNotContains( 'nukeworld', $rights );
96 * @dataProvider provideGetGroupsWithPermission
97 * @covers User::getGroupsWithPermission
99 public function testGetGroupsWithPermission( $expected, $right ) {
100 $result = User
::getGroupsWithPermission( $right );
104 $this->assertEquals( $expected, $result, "Groups with permission $right" );
107 public static function provideGetGroupsWithPermission() {
110 array( 'unittesters', 'testwriters' ),
114 array( 'unittesters' ),
118 array( 'testwriters' ),
122 array( 'testwriters' ),
129 * @dataProvider provideUserNames
130 * @covers User::isValidUserName
132 public function testIsValidUserName( $username, $result, $message ) {
133 $this->assertEquals( $this->user
->isValidUserName( $username ), $result, $message );
136 public static function provideUserNames() {
138 array( '', false, 'Empty string' ),
139 array( ' ', false, 'Blank space' ),
140 array( 'abcd', false, 'Starts with small letter' ),
141 array( 'Ab/cd', false, 'Contains slash' ),
142 array( 'Ab cd', true, 'Whitespace' ),
143 array( '192.168.1.1', false, 'IP' ),
144 array( 'User:Abcd', false, 'Reserved Namespace' ),
145 array( '12abcd232', true, 'Starts with Numbers' ),
146 array( '?abcd', true, 'Start with ? mark' ),
147 array( '#abcd', false, 'Start with #' ),
148 array( 'Abcdകഖഗഘ', true, ' Mixed scripts' ),
149 array( 'ജോസ്തോമസ്', false, 'ZWNJ- Format control character' ),
150 array( 'Ab cd', false, ' Ideographic space' ),
155 * Test, if for all rights a right- message exist,
156 * which is used on Special:ListGroupRights as help text
157 * Extensions and core
159 public function testAllRightsWithMessage() {
160 //Getting all user rights, for core: User::$mCoreRights, for extensions: $wgAvailableRights
161 $allRights = User
::getAllRights();
162 $allMessageKeys = Language
::getMessageKeysFor( 'en' );
164 $rightsWithMessage = array();
165 foreach ( $allMessageKeys as $message ) {
166 // === 0: must be at beginning of string (position 0)
167 if ( strpos( $message, 'right-' ) === 0 ) {
168 $rightsWithMessage[] = substr( $message, strlen( 'right-' ) );
173 sort( $rightsWithMessage );
178 'Each user rights (core/extensions) has a corresponding right- message.'
183 * Test User::editCount
185 * @covers User::getEditCount
187 public function testEditCount() {
188 $user = User
::newFromName( 'UnitTestUser' );
189 $user->loadDefaults();
190 $user->addToDatabase();
192 // let the user have a few (3) edits
193 $page = WikiPage
::factory( Title
::newFromText( 'Help:UserTest_EditCount' ) );
194 for ( $i = 0; $i < 3; $i++
) {
195 $page->doEdit( (string)$i, 'test', 0, false, $user );
198 $user->clearInstanceCache();
201 $user->getEditCount(),
202 'After three edits, the user edit count should be 3'
205 // increase the edit count and clear the cache
206 $user->incEditCount();
208 $user->clearInstanceCache();
211 $user->getEditCount(),
212 'After increasing the edit count manually, the user edit count should be 4'
217 * Test changing user options.
218 * @covers User::setOption
219 * @covers User::getOption
221 public function testOptions() {
222 $user = User
::newFromName( 'UnitTestUser' );
223 $user->addToDatabase();
225 $user->setOption( 'someoption', 'test' );
226 $user->setOption( 'cols', 200 );
227 $user->saveSettings();
229 $user = User
::newFromName( 'UnitTestUser' );
230 $this->assertEquals( 'test', $user->getOption( 'someoption' ) );
231 $this->assertEquals( 200, $user->getOption( 'cols' ) );
236 * Make sure defaults are loaded when setOption is called.
237 * @covers User::loadOptions
239 public function testAnonOptions() {
240 global $wgDefaultUserOptions;
241 $this->user
->setOption( 'someoption', 'test' );
242 $this->assertEquals( $wgDefaultUserOptions['cols'], $this->user
->getOption( 'cols' ) );
243 $this->assertEquals( 'test', $this->user
->getOption( 'someoption' ) );
247 * Test password expiration.
248 * @covers User::getPasswordExpired()
250 public function testPasswordExpire() {
251 global $wgPasswordExpireGrace;
252 $wgTemp = $wgPasswordExpireGrace;
253 $wgPasswordExpireGrace = 3600 * 24 * 7; // 7 days
255 $user = User
::newFromName( 'UnitTestUser' );
256 $user->loadDefaults();
257 $this->assertEquals( false, $user->getPasswordExpired() );
259 $ts = time() - ( 3600 * 24 * 1 ); // 1 day ago
260 $user->expirePassword( $ts );
261 $this->assertEquals( 'soft', $user->getPasswordExpired() );
263 $ts = time() - ( 3600 * 24 * 10 ); // 10 days ago
264 $user->expirePassword( $ts );
265 $this->assertEquals( 'hard', $user->getPasswordExpired() );
267 $wgPasswordExpireGrace = $wgTemp;
271 * Test password validity checks. There are 3 checks in core,
272 * - ensure the password meets the minimal length
273 * - ensure the password is not the same as the username
274 * - ensure the username/password combo isn't forbidden
275 * @covers User::checkPasswordValidity()
276 * @covers User::getPasswordValidity()
277 * @covers User::isValidPassword()
279 public function testCheckPasswordValidity() {
280 $this->setMwGlobals( 'wgMinimalPasswordLength', 6 );
281 $user = User
::newFromName( 'Useruser' );
283 $this->assertTrue( $user->isValidPassword( 'Password1234' ) );
286 $this->assertFalse( $user->isValidPassword( 'a' ) );
287 $this->assertFalse( $user->checkPasswordValidity( 'a' )->isGood() );
288 $this->assertEquals( 'passwordtooshort', $user->getPasswordValidity( 'a' ) );
291 $this->assertFalse( $user->checkPasswordValidity( 'Useruser' )->isGood() );
292 $this->assertEquals( 'password-name-match', $user->getPasswordValidity( 'Useruser' ) );
294 // On the forbidden list
295 $this->assertFalse( $user->checkPasswordValidity( 'Passpass' )->isGood() );
296 $this->assertEquals( 'password-login-forbidden', $user->getPasswordValidity( 'Passpass' ) );