8000 Visibility changes by schmittjoh · Pull Request #198 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Visibility changes #198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
6 commits merged into from
Mar 10, 2011
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[Security] added some more tests
  • Loading branch information
schmittjoh committed Mar 10, 2011
commit 13665fc113c1be82a306a691b04c9bd1b1a20e98
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* SecurityHelper provides read-only access to the security context.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Fabien Potencier <fabien@symfony.com>
*/
class SecurityHelper extends Helper
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,15 @@ public function setUser($user)
if (null === $this->user) {
$changed = false;
} else if ($this->user instanceof UserInterface) {
$changed = $this->user->equals($user);
if (!$user instanceof UserInterface) {
$changed = true;
} else {
$changed = !$this->user->equals($user);
}
} else if ($user instanceof UserInterface) {
$changed = true;
} else {
$changed = (string) $this->user === (string) $user;
$changed = (string) $this->user !== (string) $user;
}

if ($changed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,16 @@ public function __construct(UserInterface $user, $providerKey, $key, PersistentT
$this->persistentToken = $persistentToken;

$this->setUser($user);
$this->setAuthenticated(true);
parent::setAuthenticated(true);
}

public function setAuthenticated($authenticated)
{
if ($authenticated) {
throw new \RuntimeException('You cannot set this token to authenticated after creation.');
}

parent::setAuthenticated(false);
}

public function getProviderKey()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,104 @@ public function testAttributes()
}
}

/**
* @dataProvider getUsers
*/
public function testSetUser($user)
{
$token = $this->getToken();
$token->setUser($user);
$this->assertSame($user, $token->getUser());
}

public function getUsers()
{
$user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
$user
->expects($this->any())
->method('equals')
->will($this->returnValue(true))
;

return array(
array($user),
array(new TestUser('foo')),
array('foo'),
);
}

/**
* @dataProvider getUserChanges
*/
public function testSetUserSetsAuthenticatedToFalseWhenUserChanges($firstUser, $secondUser)
{
$token = $this->getToken();
$token->setAuthenticated(true);
$this->assertTrue($token->isAuthenticated());

$token->setUser($firstUser);
$this->assertTrue($token->isAuthenticated());

$token->setUser($secondUser);
$this->assertFalse($token->isAuthenticated());
}

public function getUserChanges()
{
$user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
$user
->expects($this->any())
->method('equals')
->will($this->returnValue(false))
;

return array(
array(
'foo', 'bar',
),
array(
'foo', new TestUser('bar'),
),
array(
'foo', $user,
),
array(
$user, $user,
),
array(
$user, 'foo'
),
array(
$user, new TestUser('foo'),
),
array(
new TestUser('foo'), new TestUser('bar'),
),
array(
new TestUser('foo'), 'bar',
),
array(
new TestUser('foo'), $user,
),
);
}

/**
* @dataProvider getUsers
*/
public function testSetUserDoesNotSetAuthenticatedToFalseWhenUserDoesNotChange($user)
{
$token = $this->getToken();
$token->setAuthenticated(true);
$this->assertTrue($token->isAuthenticated());

$token->setUser($user);
$this->assertTrue($token->isAuthenticated());

$token->setUser($user);
$this->assertTrue($token->isAuthenticated());
}

protected function getToken(array $roles = array())
{
return $this->getMockForAbstractClass('Symfony\Component\Security\Core\Authentication\Token\AbstractToken', array($roles));
Expand Down
0