Merge "Fix language code output for firstHeading"
[lhc/web/wiklou.git] / tests / phpunit / includes / UserTest.php
1 <?php
2
3 define( 'NS_UNITTEST', 5600 );
4 define( 'NS_UNITTEST_TALK', 5601 );
5
6 /**
7 * @group Database
8 */
9 class UserTest extends MediaWikiTestCase {
10 /**
11 * @var User
12 */
13 protected $user;
14
15 protected function setUp() {
16 parent::setUp();
17
18 $this->setMwGlobals( array(
19 'wgGroupPermissions' => array(),
20 'wgRevokePermissions' => array(),
21 ) );
22
23 $this->setUpPermissionGlobals();
24
25 $this->user = new User;
26 $this->user->addGroup( 'unittesters' );
27 }
28
29 private function setUpPermissionGlobals() {
30 global $wgGroupPermissions, $wgRevokePermissions;
31
32 # Data for regular $wgGroupPermissions test
33 $wgGroupPermissions['unittesters'] = array(
34 'test' => true,
35 'runtest' => true,
36 'writetest' => false,
37 'nukeworld' => false,
38 );
39 $wgGroupPermissions['testwriters'] = array(
40 'test' => true,
41 'writetest' => true,
42 'modifytest' => true,
43 );
44
45 # Data for regular $wgRevokePermissions test
46 $wgRevokePermissions['formertesters'] = array(
47 'runtest' => true,
48 );
49
50 # For the options test
51 $wgGroupPermissions['*'] = array(
52 'editmyoptions' => true,
53 );
54 }
55
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 );
62
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 );
68 }
69
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 );
76 }
77
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 );
84 }
85
86 /**
87 * @dataProvider provideGetGroupsWithPermission
88 */
89 public function testGetGroupsWithPermission( $expected, $right ) {
90 $result = User::getGroupsWithPermission( $right );
91 sort( $result );
92 sort( $expected );
93
94 $this->assertEquals( $expected, $result, "Groups with permission $right" );
95 }
96
97 public static function provideGetGroupsWithPermission() {
98 return array(
99 array(
100 array( 'unittesters', 'testwriters' ),
101 'test'
102 ),
103 array(
104 array( 'unittesters' ),
105 'runtest'
106 ),
107 array(
108 array( 'testwriters' ),
109 'writetest'
110 ),
111 array(
112 array( 'testwriters' ),
113 'modifytest'
114 ),
115 );
116 }
117
118 /**
119 * @dataProvider provideUserNames
120 */
121 public function testIsValidUserName( $username, $result, $message ) {
122 $this->assertEquals( $this->user->isValidUserName( $username ), $result, $message );
123 }
124
125 public static function provideUserNames() {
126 return array(
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' ),
140 );
141 }
142
143 /**
144 * Test, if for all rights a right- message exist,
145 * which is used on Special:ListGroupRights as help text
146 * Extensions and core
147 */
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' );
152
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-' ) );
158 }
159 }
160
161 sort( $allRights );
162 sort( $rightsWithMessage );
163
164 $this->assertEquals(
165 $allRights,
166 $rightsWithMessage,
167 'Each user rights (core/extensions) has a corresponding right- message.'
168 );
169 }
170
171 /**
172 * Test User::editCount
173 * @group medium
174 */
175 public function testEditCount() {
176 $user = User::newFromName( 'UnitTestUser' );
177 $user->loadDefaults();
178 $user->addToDatabase();
179
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 );
184 }
185
186 $user->clearInstanceCache();
187 $this->assertEquals( 3, $user->getEditCount(), 'After three edits, the user edit count should be 3' );
188
189 // increase the edit count and clear the cache
190 $user->incEditCount();
191
192 $user->clearInstanceCache();
193 $this->assertEquals( 4, $user->getEditCount(), 'After increasing the edit count manually, the user edit count should be 4' );
194 }
195
196 /**
197 * Test changing user options.
198 */
199 public function testOptions() {
200 $user = User::newFromName( 'UnitTestUser' );
201 $user->addToDatabase();
202
203 $user->setOption( 'someoption', 'test' );
204 $user->setOption( 'cols', 200 );
205 $user->saveSettings();
206
207 $user = User::newFromName( 'UnitTestUser' );
208 $this->assertEquals( 'test', $user->getOption( 'someoption' ) );
209 $this->assertEquals( 200, $user->getOption( 'cols' ) );
210 }
211
212 /**
213 * Bug 37963
214 * Make sure defaults are loaded when setOption is called.
215 */
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' ) );
221 }
222 }