|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file contains only the UserTest class. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Tests\Xtools; |
|
7
|
|
|
|
|
8
|
|
|
use PHPUnit_Framework_TestCase; |
|
9
|
|
|
use Xtools\Project; |
|
10
|
|
|
use Xtools\ProjectRepository; |
|
11
|
|
|
use Xtools\User; |
|
12
|
|
|
use Xtools\UserRepository; |
|
13
|
|
|
use DateTime; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Tests for the User class. |
|
17
|
|
|
*/ |
|
18
|
|
|
class UserTest extends PHPUnit_Framework_TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* A username should be given an initial capital letter in all cases. |
|
23
|
|
|
*/ |
|
24
|
|
|
public function testUsernameHasInitialCapital() |
|
25
|
|
|
{ |
|
26
|
|
|
$user = new User('lowercasename'); |
|
27
|
|
|
$this->assertEquals('Lowercasename', $user->getUsername()); |
|
28
|
|
|
$user2 = new User('UPPERCASENAME'); |
|
29
|
|
|
$this->assertEquals('UPPERCASENAME', $user2->getUsername()); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* A user has an integer identifier on a project (and this can differ from project |
|
34
|
|
|
* to project). |
|
35
|
|
|
*/ |
|
36
|
|
|
public function testUserHasIdOnProject() |
|
37
|
|
|
{ |
|
38
|
|
|
// Set up stub user and project repositories. |
|
39
|
|
|
$userRepo = $this->getMock(UserRepository::class); |
|
40
|
|
|
$userRepo->expects($this->once()) |
|
41
|
|
|
->method('getId') |
|
42
|
|
|
->willReturn(12); |
|
43
|
|
|
$projectRepo = $this->getMock(ProjectRepository::class); |
|
44
|
|
|
$projectRepo->expects($this->once()) |
|
45
|
|
|
->method('getOne') |
|
46
|
|
|
->willReturn(['dbname' => 'testWiki']); |
|
47
|
|
|
|
|
48
|
|
|
// Make sure the user has the correct ID. |
|
49
|
|
|
$user = new User('TestUser'); |
|
50
|
|
|
$user->setRepository($userRepo); |
|
51
|
|
|
$project = new Project('wiki.example.org'); |
|
52
|
|
|
$project->setRepository($projectRepo); |
|
53
|
|
|
$this->assertEquals(12, $user->getId($project)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Is a user an admin on a given project? |
|
58
|
|
|
* @dataProvider isAdminProvider |
|
59
|
|
|
* @param string $username The username. |
|
60
|
|
|
* @param string[] $groups The groups to test. |
|
61
|
|
|
* @param bool $isAdmin The desired result. |
|
62
|
|
|
*/ |
|
63
|
|
|
public function testIsAdmin($username, $groups, $isAdmin) |
|
64
|
|
|
{ |
|
65
|
|
|
$userRepo = $this->getMock(UserRepository::class); |
|
66
|
|
|
$userRepo->expects($this->once()) |
|
67
|
|
|
->method('getGroups') |
|
68
|
|
|
->willReturn($groups); |
|
69
|
|
|
$user = new User($username); |
|
70
|
|
|
$user->setRepository($userRepo); |
|
71
|
|
|
$this->assertEquals($isAdmin, $user->isAdmin(new Project('testWiki'))); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Data for self::testIsAdmin(). |
|
76
|
|
|
* @return string[] |
|
77
|
|
|
*/ |
|
78
|
|
|
public function isAdminProvider() |
|
79
|
|
|
{ |
|
80
|
|
|
return [ |
|
81
|
|
|
['AdminUser', ['sysop', 'autopatrolled'], true], |
|
82
|
|
|
['NormalUser', ['autopatrolled'], false], |
|
83
|
|
|
]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get the expiry of the current block of a user on a given project |
|
88
|
|
|
*/ |
|
89
|
|
|
public function testExpiry() |
|
90
|
|
|
{ |
|
91
|
|
|
$userRepo = $this->getMock(UserRepository::class); |
|
92
|
|
|
$userRepo->expects($this->once()) |
|
93
|
|
|
->method('getBlockExpiry') |
|
94
|
|
|
->willReturn('20500601000000'); |
|
95
|
|
|
$user = new User('TestUser'); |
|
96
|
|
|
$user->setRepository($userRepo); |
|
97
|
|
|
|
|
98
|
|
|
$projectRepo = $this->getMock(ProjectRepository::class); |
|
99
|
|
|
$project = new Project('wiki.example.org'); |
|
100
|
|
|
$project->setRepository($projectRepo); |
|
101
|
|
|
|
|
102
|
|
|
$this->assertEquals(new DateTime('20500601000000'), $user->getBlockExpiry($project)); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Is the user currently blocked on a given project? |
|
107
|
|
|
*/ |
|
108
|
|
|
public function testIsBlocked() |
|
109
|
|
|
{ |
|
110
|
|
|
$userRepo = $this->getMock(UserRepository::class); |
|
111
|
|
|
$userRepo->expects($this->once()) |
|
112
|
|
|
->method('getBlockExpiry') |
|
113
|
|
|
->willReturn('infinity'); |
|
114
|
|
|
$user = new User('TestUser'); |
|
115
|
|
|
$user->setRepository($userRepo); |
|
116
|
|
|
|
|
117
|
|
|
$projectRepo = $this->getMock(ProjectRepository::class); |
|
118
|
|
|
$project = new Project('wiki.example.org'); |
|
119
|
|
|
$project->setRepository($projectRepo); |
|
120
|
|
|
|
|
121
|
|
|
$this->assertEquals(true, $user->isBlocked($project)); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* User's non-automated edits |
|
126
|
|
|
*/ |
|
127
|
|
|
public function testGetNonAutomatedEdits() |
|
128
|
|
|
{ |
|
129
|
|
|
$userRepo = $this->getMock(UserRepository::class); |
|
130
|
|
|
$userRepo->expects($this->once()) |
|
131
|
|
|
->method('getNonAutomatedEdits') |
|
132
|
|
|
->willReturn([[ |
|
133
|
|
|
'page_title' => 'Test_page', |
|
134
|
|
|
'page_namespace' => '1', |
|
135
|
|
|
'rev_id' => '123', |
|
136
|
|
|
'timestamp' => '20170101000000', |
|
137
|
|
|
'minor' => '0', |
|
138
|
|
|
'length' => '5', |
|
139
|
|
|
'length_change' => '-5', |
|
140
|
|
|
'comment' => 'Test', |
|
141
|
|
|
]]); |
|
142
|
|
|
$user = new User('TestUser'); |
|
143
|
|
|
$user->setRepository($userRepo); |
|
144
|
|
|
|
|
145
|
|
|
$projectRepo = $this->getMock(ProjectRepository::class); |
|
146
|
|
|
$projectRepo->expects($this->once()) |
|
147
|
|
|
->method('getMetadata') |
|
148
|
|
|
->willReturn(['namespaces' => [ |
|
149
|
|
|
'0' => '', |
|
150
|
|
|
'1' => 'Talk', |
|
151
|
|
|
]]); |
|
152
|
|
|
$project = new Project('wiki.example.org'); |
|
153
|
|
|
$project->setRepository($projectRepo); |
|
154
|
|
|
|
|
155
|
|
|
$edits = $user->getNonAutomatedEdits($project, 1); |
|
156
|
|
|
|
|
157
|
|
|
// Asserts type casting and page title normalization worked |
|
158
|
|
|
$this->assertArraySubset( |
|
159
|
|
|
[ |
|
160
|
|
|
'page_title' => 'Talk:Test_page', |
|
161
|
|
|
'page_namespace' => 1, |
|
162
|
|
|
'rev_id' => 123, |
|
163
|
|
|
'timestamp' => DateTime::createFromFormat('YmdHis', '20170101000000'), |
|
164
|
|
|
'minor' => false, |
|
165
|
|
|
'length' => 5, |
|
166
|
|
|
'length_change' => -5, |
|
167
|
|
|
'comment' => 'Test', |
|
168
|
|
|
], |
|
169
|
|
|
$edits[0] |
|
170
|
|
|
); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|