Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9084f89

Browse files
committed
Added test for serializing user without roles
1 parent 96d2d19 commit 9084f89

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

src/Symfony/Component/Security/Core/Tests/Authentication/Token/AbstractTokenTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,21 @@ public function testSetUserDoesNotSetAuthenticatedToFalseWhenUserDoesNotChange($
248248
$token->setUser($user);
249249
$this->assertTrue($token->isAuthenticated());
250250
}
251+
252+
public function testIsUserChangedWhenSerializing()
253+
{
254+
$token = new ConcreteToken(['ROLE_ADMIN']);
255+
$token->setAuthenticated(true);
256+
$this->assertTrue($token->isAuthenticated());
257+
258+
$user = new SerializableUser('wouter', ['ROLE_ADMIN']);
259+
$token->setUser($user);
260+
$this->assertTrue($token->isAuthenticated());
261+
262+
$token = unserialize(serialize($token));
263+
$token->setUser($user);
264+
$this->assertTrue($token->isAuthenticated());
265+
}
251266
}
252267

253268
class TestUser
@@ -265,6 +280,56 @@ public function __toString(): string
265280
}
266281
}
267282

283+
class SerializableUser implements UserInterface, \Serializable
284+
{
285+
private $roles;
286+
private $name;
287+
288+
public function __construct($name, array $roles = [])
289+
{
290+
$this->name = $name;
291+
$this->roles = $roles;
292+
}
293+
294+
public function getUsername()
295+
{
296+
return $this->name;
297+
}
298+
299+
public function getPassword()
300+
{
301+
return '***';
302+
}
303+
304+
public function getRoles()
305+
{
306+
if (empty($this->roles)) {
307+
return ['ROLE_USER'];
308+
}
309+
310+
return $this->roles;
311+
}
312+
313+
public function eraseCredentials()
314+
{
315+
}
316+
317+
public function getSalt()
318+
{
319+
return null;
320+
}
321+
322+
public function serialize()
323+
{
324+
return serialize($this->name);
325+
}
326+
327+
public function unserialize($serialized)
328+
{
329+
$this->name = unserialize($serialized);
330+
}
331+
}
332+
268333
class ConcreteToken extends AbstractToken
269334
{
270335
private $credentials = 'credentials_value';

0 commit comments

Comments
 (0)