|
3
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the FOSUserBundle package. |
|
|
5 |
* |
|
|
6 |
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace FOS\UserBundle\Tests\Util; |
|
|
13 |
|
|
|
14 |
use FOS\UserBundle\Util\UserManipulator; |
|
|
15 |
use FOS\UserBundle\Tests\TestUser; |
|
|
16 |
use Symfony\Component\Security\Acl\Domain\Acl; |
|
|
17 |
use Symfony\Component\Security\Acl\Domain\ObjectIdentity; |
|
|
18 |
use Symfony\Component\Security\Acl\Domain\PermissionGrantingStrategy; |
|
|
19 |
|
|
|
20 |
class UserManipulatorTest extends \PHPUnit_Framework_TestCase |
|
|
21 |
{ |
|
|
22 |
public function testCreate() |
|
|
23 |
{ |
|
|
24 |
$userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface'); |
|
|
25 |
$user = new TestUser(); |
|
|
26 |
|
|
|
27 |
$username = 'test_username'; |
|
|
28 |
$password = 'test_password'; |
|
|
29 |
$email = 'test@email.org'; |
|
|
30 |
$active = true; // it is enabled |
|
|
31 |
$superadmin = false; |
|
|
32 |
|
|
|
33 |
$userManagerMock->expects($this->once()) |
|
|
34 |
->method('createUser') |
|
|
35 |
->will($this->returnValue($user)); |
|
|
36 |
|
|
|
37 |
$userManagerMock->expects($this->once()) |
|
|
38 |
->method('updateUser') |
|
|
39 |
->will($this->returnValue($user)) |
|
|
40 |
->with($this->isInstanceOf('FOS\UserBundle\Tests\TestUser')); |
|
|
41 |
|
|
|
42 |
$manipulator = new UserManipulator($userManagerMock); |
|
|
43 |
$manipulator->create($username, $password, $email, $active, $superadmin); |
|
|
44 |
|
|
|
45 |
$this->assertEquals($username, $user->getUsername()); |
|
|
46 |
$this->assertEquals($password, $user->getPlainPassword()); |
|
|
47 |
$this->assertEquals($email, $user->getEmail()); |
|
|
48 |
$this->assertEquals($active, $user->isEnabled()); |
|
|
49 |
$this->assertEquals($superadmin, $user->isSuperAdmin()); |
|
|
50 |
} |
|
|
51 |
|
|
|
52 |
public function testActivateWithValidUsername() |
|
|
53 |
{ |
|
|
54 |
$userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface'); |
|
|
55 |
$username = 'test_username'; |
|
|
56 |
|
|
|
57 |
$user = new TestUser(); |
|
|
58 |
$user->setUsername($username); |
|
|
59 |
$user->setEnabled(false); |
|
|
60 |
|
|
|
61 |
$userManagerMock->expects($this->once()) |
|
|
62 |
->method('findUserByUsername') |
|
|
63 |
->will($this->returnValue($user)) |
|
|
64 |
->with($this->equalTo($username)); |
|
|
65 |
|
|
|
66 |
$userManagerMock->expects($this->once()) |
|
|
67 |
->method('updateUser') |
|
|
68 |
->will($this->returnValue($user)) |
|
|
69 |
->with($this->isInstanceOf('FOS\UserBundle\Tests\TestUser')); |
|
|
70 |
|
|
|
71 |
$manipulator = new UserManipulator($userManagerMock); |
|
|
72 |
$manipulator->activate($username); |
|
|
73 |
|
|
|
74 |
$this->assertEquals($username, $user->getUsername()); |
|
|
75 |
$this->assertEquals(true, $user->isEnabled()); |
|
|
76 |
} |
|
|
77 |
|
|
|
78 |
/** |
|
|
79 |
* @expectedException \InvalidArgumentException |
|
|
80 |
*/ |
|
|
81 |
public function testActivateWithInvalidUsername() |
|
|
82 |
{ |
|
|
83 |
$userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface'); |
|
|
84 |
$invalidusername = 'invalid_username'; |
|
|
85 |
|
|
|
86 |
$userManagerMock->expects($this->once()) |
|
|
87 |
->method('findUserByUsername') |
|
|
88 |
->will($this->returnValue(null)) |
|
|
89 |
->with($this->equalTo($invalidusername)); |
|
|
90 |
|
|
|
91 |
$userManagerMock->expects($this->never()) |
|
|
92 |
->method('updateUser'); |
|
|
93 |
|
|
|
94 |
$manipulator = new UserManipulator($userManagerMock); |
|
|
95 |
$manipulator->activate($invalidusername); |
|
|
96 |
} |
|
|
97 |
|
|
|
98 |
public function testDeactivateWithValidUsername() |
|
|
99 |
{ |
|
|
100 |
$userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface'); |
|
|
101 |
$username = 'test_username'; |
|
|
102 |
|
|
|
103 |
$user = new TestUser(); |
|
|
104 |
$user->setUsername($username); |
|
|
105 |
$user->setEnabled(true); |
|
|
106 |
|
|
|
107 |
$userManagerMock->expects($this->once()) |
|
|
108 |
->method('findUserByUsername') |
|
|
109 |
->will($this->returnValue($user)) |
|
|
110 |
->with($this->equalTo($username)); |
|
|
111 |
|
|
|
112 |
$userManagerMock->expects($this->once()) |
|
|
113 |
->method('updateUser') |
|
|
114 |
->will($this->returnValue($user)) |
|
|
115 |
->with($this->isInstanceOf('FOS\UserBundle\Tests\TestUser')); |
|
|
116 |
|
|
|
117 |
$manipulator = new UserManipulator($userManagerMock); |
|
|
118 |
$manipulator->deactivate($username); |
|
|
119 |
|
|
|
120 |
$this->assertEquals($username, $user->getUsername()); |
|
|
121 |
$this->assertEquals(false, $user->isEnabled()); |
|
|
122 |
} |
|
|
123 |
|
|
|
124 |
/** |
|
|
125 |
* @expectedException \InvalidArgumentException |
|
|
126 |
*/ |
|
|
127 |
public function testDeactivateWithInvalidUsername() |
|
|
128 |
{ |
|
|
129 |
$userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface'); |
|
|
130 |
$invalidusername = 'invalid_username'; |
|
|
131 |
|
|
|
132 |
$userManagerMock->expects($this->once()) |
|
|
133 |
->method('findUserByUsername') |
|
|
134 |
->will($this->returnValue(null)) |
|
|
135 |
->with($this->equalTo($invalidusername)); |
|
|
136 |
|
|
|
137 |
$userManagerMock->expects($this->never()) |
|
|
138 |
->method('updateUser'); |
|
|
139 |
|
|
|
140 |
$manipulator = new UserManipulator($userManagerMock); |
|
|
141 |
$manipulator->deactivate($invalidusername); |
|
|
142 |
} |
|
|
143 |
|
|
|
144 |
public function testPromoteWithValidUsername() |
|
|
145 |
{ |
|
|
146 |
$userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface'); |
|
|
147 |
$username = 'test_username'; |
|
|
148 |
|
|
|
149 |
$user = new TestUser(); |
|
|
150 |
$user->setUsername($username); |
|
|
151 |
$user->setSuperAdmin(false); |
|
|
152 |
|
|
|
153 |
$userManagerMock->expects($this->once()) |
|
|
154 |
->method('findUserByUsername') |
|
|
155 |
->will($this->returnValue($user)) |
|
|
156 |
->with($this->equalTo($username)); |
|
|
157 |
|
|
|
158 |
$userManagerMock->expects($this->once()) |
|
|
159 |
->method('updateUser') |
|
|
160 |
->will($this->returnValue($user)) |
|
|
161 |
->with($this->isInstanceOf('FOS\UserBundle\Tests\TestUser')); |
|
|
162 |
|
|
|
163 |
$manipulator = new UserManipulator($userManagerMock); |
|
|
164 |
$manipulator->promote($username); |
|
|
165 |
|
|
|
166 |
$this->assertEquals($username, $user->getUsername()); |
|
|
167 |
$this->assertEquals(true, $user->isSuperAdmin()); |
|
|
168 |
} |
|
|
169 |
|
|
|
170 |
/** |
|
|
171 |
* @expectedException \InvalidArgumentException |
|
|
172 |
*/ |
|
|
173 |
public function testPromoteWithInvalidUsername() |
|
|
174 |
{ |
|
|
175 |
$userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface'); |
|
|
176 |
$invalidusername = 'invalid_username'; |
|
|
177 |
|
|
|
178 |
$userManagerMock->expects($this->once()) |
|
|
179 |
->method('findUserByUsername') |
|
|
180 |
->will($this->returnValue(null)) |
|
|
181 |
->with($this->equalTo($invalidusername)); |
|
|
182 |
|
|
|
183 |
$userManagerMock->expects($this->never()) |
|
|
184 |
->method('updateUser'); |
|
|
185 |
|
|
|
186 |
$manipulator = new UserManipulator($userManagerMock); |
|
|
187 |
$manipulator->promote($invalidusername); |
|
|
188 |
} |
|
|
189 |
|
|
|
190 |
public function testDemoteWithValidUsername() |
|
|
191 |
{ |
|
|
192 |
$userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface'); |
|
|
193 |
$username = 'test_username'; |
|
|
194 |
|
|
|
195 |
$user = new TestUser(); |
|
|
196 |
$user->setUsername($username); |
|
|
197 |
$user->setSuperAdmin(true); |
|
|
198 |
|
|
|
199 |
$userManagerMock->expects($this->once()) |
|
|
200 |
->method('findUserByUsername') |
|
|
201 |
->will($this->returnValue($user)) |
|
|
202 |
->with($this->equalTo($username)); |
|
|
203 |
|
|
|
204 |
$userManagerMock->expects($this->once()) |
|
|
205 |
->method('updateUser') |
|
|
206 |
->will($this->returnValue($user)) |
|
|
207 |
->with($this->isInstanceOf('FOS\UserBundle\Tests\TestUser')); |
|
|
208 |
|
|
|
209 |
$manipulator = new UserManipulator($userManagerMock); |
|
|
210 |
$manipulator->demote($username); |
|
|
211 |
|
|
|
212 |
$this->assertEquals($username, $user->getUsername()); |
|
|
213 |
$this->assertEquals(false, $user->isSuperAdmin()); |
|
|
214 |
} |
|
|
215 |
|
|
|
216 |
/** |
|
|
217 |
* @expectedException \InvalidArgumentException |
|
|
218 |
*/ |
|
|
219 |
public function testDemoteWithInvalidUsername() |
|
|
220 |
{ |
|
|
221 |
$userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface'); |
|
|
222 |
$invalidusername = 'invalid_username'; |
|
|
223 |
|
|
|
224 |
$userManagerMock->expects($this->once()) |
|
|
225 |
->method('findUserByUsername') |
|
|
226 |
->will($this->returnValue(null)) |
|
|
227 |
->with($this->equalTo($invalidusername)); |
|
|
228 |
|
|
|
229 |
$userManagerMock->expects($this->never()) |
|
|
230 |
->method('updateUser'); |
|
|
231 |
|
|
|
232 |
$manipulator = new UserManipulator($userManagerMock); |
|
|
233 |
$manipulator->demote($invalidusername); |
|
|
234 |
} |
|
|
235 |
|
|
|
236 |
public function testChangePasswordWithValidUsername() |
|
|
237 |
{ |
|
|
238 |
$userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface'); |
|
|
239 |
|
|
|
240 |
$user = new TestUser(); |
|
|
241 |
$username = 'test_username'; |
|
|
242 |
$password = 'test_password'; |
|
|
243 |
$oldpassword = 'old_password'; |
|
|
244 |
|
|
|
245 |
$user->setUsername($username); |
|
|
246 |
$user->setPlainPassword($oldpassword); |
|
|
247 |
|
|
|
248 |
$userManagerMock->expects($this->once()) |
|
|
249 |
->method('findUserByUsername') |
|
|
250 |
->will($this->returnValue($user)) |
|
|
251 |
->with($this->equalTo($username)); |
|
|
252 |
|
|
|
253 |
$userManagerMock->expects($this->once()) |
|
|
254 |
->method('updateUser') |
|
|
255 |
->will($this->returnValue($user)) |
|
|
256 |
->with($this->isInstanceOf('FOS\UserBundle\Tests\TestUser')); |
|
|
257 |
|
|
|
258 |
$manipulator = new UserManipulator($userManagerMock); |
|
|
259 |
$manipulator->changePassword($username, $password); |
|
|
260 |
|
|
|
261 |
$this->assertEquals($username, $user->getUsername()); |
|
|
262 |
$this->assertEquals($password, $user->getPlainPassword()); |
|
|
263 |
} |
|
|
264 |
|
|
|
265 |
/** |
|
|
266 |
* @expectedException \InvalidArgumentException |
|
|
267 |
*/ |
|
|
268 |
public function testChangePasswordWithInvalidUsername() |
|
|
269 |
{ |
|
|
270 |
$userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface'); |
|
|
271 |
|
|
|
272 |
$invalidusername = 'invalid_username'; |
|
|
273 |
$password = 'test_password'; |
|
|
274 |
|
|
|
275 |
$userManagerMock->expects($this->once()) |
|
|
276 |
->method('findUserByUsername') |
|
|
277 |
->will($this->returnValue(null)) |
|
|
278 |
->with($this->equalTo($invalidusername)); |
|
|
279 |
|
|
|
280 |
$userManagerMock->expects($this->never()) |
|
|
281 |
->method('updateUser'); |
|
|
282 |
|
|
|
283 |
$manipulator = new UserManipulator($userManagerMock); |
|
|
284 |
$manipulator->changePassword($invalidusername, $password); |
|
|
285 |
} |
|
|
286 |
} |