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