Documentation and whitespace
[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 protected $savedGroupPermissions, $savedRevokedPermissions;
11
12 /**
13 * @var User
14 */
15 protected $user;
16
17 public function setUp() {
18 parent::setUp();
19
20 $this->savedGroupPermissions = $GLOBALS['wgGroupPermissions'];
21 $this->savedRevokedPermissions = $GLOBALS['wgRevokePermissions'];
22
23 $this->setUpPermissionGlobals();
24 $this->setUpUser();
25 }
26 private function setUpPermissionGlobals() {
27 global $wgGroupPermissions, $wgRevokePermissions;
28
29 # Data for regular $wgGroupPermissions test
30 $wgGroupPermissions['unittesters'] = array(
31 'test' => true,
32 'runtest' => true,
33 'writetest' => false,
34 'nukeworld' => false,
35 );
36 $wgGroupPermissions['testwriters'] = array(
37 'test' => true,
38 'writetest' => true,
39 'modifytest' => true,
40 );
41 # Data for regular $wgRevokePermissions test
42 $wgRevokePermissions['formertesters'] = array(
43 'runtest' => true,
44 );
45 }
46 private function setUpUser() {
47 $this->user = new User;
48 $this->user->addGroup( 'unittesters' );
49 }
50
51 public function tearDown() {
52 parent::tearDown();
53
54 $GLOBALS['wgGroupPermissions'] = $this->savedGroupPermissions;
55 $GLOBALS['wgRevokePermissions'] = $this->savedRevokedPermissions;
56 }
57
58 public function testGroupPermissions() {
59 $rights = User::getGroupPermissions( array( 'unittesters' ) );
60 $this->assertContains( 'runtest', $rights );
61 $this->assertNotContains( 'writetest', $rights );
62 $this->assertNotContains( 'modifytest', $rights );
63 $this->assertNotContains( 'nukeworld', $rights );
64
65 $rights = User::getGroupPermissions( array( 'unittesters', 'testwriters' ) );
66 $this->assertContains( 'runtest', $rights );
67 $this->assertContains( 'writetest', $rights );
68 $this->assertContains( 'modifytest', $rights );
69 $this->assertNotContains( 'nukeworld', $rights );
70 }
71 public function testRevokePermissions() {
72 $rights = User::getGroupPermissions( array( 'unittesters', 'formertesters' ) );
73 $this->assertNotContains( 'runtest', $rights );
74 $this->assertNotContains( 'writetest', $rights );
75 $this->assertNotContains( 'modifytest', $rights );
76 $this->assertNotContains( 'nukeworld', $rights );
77 }
78
79 public function testUserPermissions() {
80 $rights = $this->user->getRights();
81 $this->assertContains( 'runtest', $rights );
82 $this->assertNotContains( 'writetest', $rights );
83 $this->assertNotContains( 'modifytest', $rights );
84 $this->assertNotContains( 'nukeworld', $rights );
85 }
86
87 /**
88 * @dataProvider provideGetGroupsWithPermission
89 */
90 public function testGetGroupsWithPermission( $expected, $right ) {
91 $result = User::getGroupsWithPermission( $right );
92 sort( $result );
93 sort( $expected );
94
95 $this->assertEquals( $expected, $result, "Groups with permission $right" );
96 }
97
98 public function provideGetGroupsWithPermission() {
99 return array(
100 array(
101 array( 'unittesters', 'testwriters' ),
102 'test'
103 ),
104 array(
105 array( 'unittesters' ),
106 'runtest'
107 ),
108 array(
109 array( 'testwriters' ),
110 'writetest'
111 ),
112 array(
113 array( 'testwriters' ),
114 'modifytest'
115 ),
116 );
117 }
118
119 /**
120 * @dataProvider provideUserNames
121 */
122 public function testIsValidUserName( $username, $result, $message ) {
123 $this->assertEquals( $this->user->isValidUserName( $username ), $result, $message );
124 }
125
126 public function provideUserNames() {
127 return array(
128 array( '', false, 'Empty string' ),
129 array( ' ', false, 'Blank space' ),
130 array( 'abcd', false, 'Starts with small letter' ),
131 array( 'Ab/cd', false, 'Contains slash' ),
132 array( 'Ab cd' , true, 'Whitespace' ),
133 array( '192.168.1.1', false, 'IP' ),
134 array( 'User:Abcd', false, 'Reserved Namespace' ),
135 array( '12abcd232' , true , 'Starts with Numbers' ),
136 array( '?abcd' , true, 'Start with ? mark' ),
137 array( '#abcd', false, 'Start with #' ),
138 array( 'Abcdകഖഗഘ', true, ' Mixed scripts' ),
139 array( 'ജോസ്‌തോമസ്', false, 'ZWNJ- Format control character' ),
140 array( 'Ab cd', false, ' Ideographic space' ),
141 );
142 }
143 }