Merge "cleanup action=tokens"
[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
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 );
57
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 );
63 }
64
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 );
71 }
72
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 );
79 }
80
81 /**
82 * @dataProvider provideGetGroupsWithPermission
83 */
84 public function testGetGroupsWithPermission( $expected, $right ) {
85 $result = User::getGroupsWithPermission( $right );
86 sort( $result );
87 sort( $expected );
88
89 $this->assertEquals( $expected, $result, "Groups with permission $right" );
90 }
91
92 public static function provideGetGroupsWithPermission() {
93 return array(
94 array(
95 array( 'unittesters', 'testwriters' ),
96 'test'
97 ),
98 array(
99 array( 'unittesters' ),
100 'runtest'
101 ),
102 array(
103 array( 'testwriters' ),
104 'writetest'
105 ),
106 array(
107 array( 'testwriters' ),
108 'modifytest'
109 ),
110 );
111 }
112
113 /**
114 * @dataProvider provideUserNames
115 */
116 public function testIsValidUserName( $username, $result, $message ) {
117 $this->assertEquals( $this->user->isValidUserName( $username ), $result, $message );
118 }
119
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' ),
135 );
136 }
137
138 /**
139 * Test, if for all rights a right- message exist,
140 * which is used on Special:ListGroupRights as help text
141 * Extensions and core
142 */
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' );
147
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-' ) );
153 }
154 }
155
156 sort( $allRights );
157 sort( $rightsWithMessage );
158
159 $this->assertEquals(
160 $allRights,
161 $rightsWithMessage,
162 'Each user rights (core/extensions) has a corresponding right- message.'
163 );
164 }
165
166 /**
167 * Test User::editCount
168 * @group medium
169 */
170 public function testEditCount() {
171 $user = User::newFromName( 'UnitTestUser' );
172 $user->loadDefaults();
173 $user->addToDatabase();
174
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 );
179 }
180
181 $user->clearInstanceCache();
182 $this->assertEquals( 3, $user->getEditCount(), 'After three edits, the user edit count should be 3' );
183
184 // increase the edit count and clear the cache
185 $user->incEditCount();
186
187 $user->clearInstanceCache();
188 $this->assertEquals( 4, $user->getEditCount(), 'After increasing the edit count manually, the user edit count should be 4' );
189 }
190
191 /**
192 * Test changing user options.
193 */
194 public function testOptions() {
195 $user = User::newFromName( 'UnitTestUser' );
196 $user->addToDatabase();
197
198 $user->setOption( 'someoption', 'test' );
199 $user->setOption( 'cols', 200 );
200 $user->saveSettings();
201
202 $user = User::newFromName( 'UnitTestUser' );
203 $this->assertEquals( 'test', $user->getOption( 'someoption' ) );
204 $this->assertEquals( 200, $user->getOption( 'cols' ) );
205 }
206
207 /**
208 * Bug 37963
209 * Make sure defaults are loaded when setOption is called.
210 */
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' ) );
216 }
217 }