test: coverage recording now needs to be explicit
[mediawiki.git] / tests / phpunit / includes / UserTest.php
blobe777179a242112cfc88b2bfe8d085474ec4f7bae
1 <?php
3 define( 'NS_UNITTEST', 5600 );
4 define( 'NS_UNITTEST_TALK', 5601 );
6 /**
7 * @group Database
8 */
9 class UserTest extends MediaWikiTestCase {
10 /**
11 * @var User
13 protected $user;
15 protected function setUp() {
16 parent::setUp();
18 $this->setMwGlobals( array(
19 'wgGroupPermissions' => array(),
20 'wgRevokePermissions' => array(),
21 ) );
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(
34 'test' => true,
35 'runtest' => true,
36 'writetest' => false,
37 'nukeworld' => false,
39 $wgGroupPermissions['testwriters'] = array(
40 'test' => true,
41 'writetest' => true,
42 'modifytest' => true,
45 # Data for regular $wgRevokePermissions test
46 $wgRevokePermissions['formertesters'] = array(
47 'runtest' => true,
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 );
81 /**
82 * @dataProvider provideGetGroupsWithPermission
84 public function testGetGroupsWithPermission( $expected, $right ) {
85 $result = User::getGroupsWithPermission( $right );
86 sort( $result );
87 sort( $expected );
89 $this->assertEquals( $expected, $result, "Groups with permission $right" );
92 public static function provideGetGroupsWithPermission() {
93 return array(
94 array(
95 array( 'unittesters', 'testwriters' ),
96 'test'
98 array(
99 array( 'unittesters' ),
100 'runtest'
102 array(
103 array( 'testwriters' ),
104 'writetest'
106 array(
107 array( 'testwriters' ),
108 'modifytest'
114 * @dataProvider provideUserNames
116 public function testIsValidUserName( $username, $result, $message ) {
117 $this->assertEquals( $this->user->isValidUserName( $username ), $result, $message );
120 public static function provideUserNames() {
121 return array(
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-' ) );
156 sort( $allRights );
157 sort( $rightsWithMessage );
159 $this->assertEquals(
160 $allRights,
161 $rightsWithMessage,
162 'Each user rights (core/extensions) has a corresponding right- message.'
167 * Test User::editCount
168 * @group medium
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' ) );
208 * Bug 37963
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' ) );