8000 [Security] Readd the correct tests by wouterj · Pull Request #15932 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Security] Readd the correct tests #15932

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

Closed
wants to merge 2 commits into from
Closed
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
Next Next commit
Readd the correct tests
  • Loading branch information
wouterj committed Sep 27, 2015
commit df36835e7f3c4206acec26a038da9abc1bf78a7b
Original file line number Diff line number Diff line change
Expand Up @@ -13,132 +13,69 @@

use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;

/**
* @author Roman Marintšenko <inoryy@gmail.com>
*/
class AbstractVoterTest extends \PHPUnit_Framework_TestCase
{
private $token;
protected $voter;
protected $object;
protected $token;

protected function setUp()
{
$tokenMock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
$tokenMock
->expects($this->any())
->method('getUser')
->will($this->returnValue('user'));

$this->token = $tokenMock;
}

/**
* @dataProvider getData
*/
public function testVote($expectedVote, $object, $attributes, $message)
{
$voter = new VoterFixture();

$this->assertEquals($expectedVote, $voter->vote($this->token, $object, $attributes), $message);
}

/**
* @dataProvider getData
* @group legacy
*/
public function testVoteUsingDeprecatedIsGranted($expectedVote, $object, $attributes, $message)
{
$voter = new DeprecatedVoterFixture();

$this->assertEquals($expectedVote, $voter->vote($this->token, $object, $attributes), $message);
}

/**
* @group legacy
* @expectedException \BadMethodCallException
*/
public function testNoOverriddenMethodsThrowsException()
{
$voter = new DeprecatedVoterNothingImplementedFixture();
$voter->vote($this->token, new ObjectFixture(), array('foo'));
$this->voter = new AbstractVoterTest_Voter();
$this->object = $this->getMock('AbstractVoterTest_Object');
$this->token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
}

public function getData()
public function testAttributeAndClassSupported()
{
return array(
array(AbstractVoter::ACCESS_ABSTAIN, null, array(), 'ACCESS_ABSTAIN for null objects'),
array(AbstractVoter::ACCESS_ABSTAIN, new UnsupportedObjectFixture(), array(), 'ACCESS_ABSTAIN for objects with unsupported class'),
array(AbstractVoter::ACCESS_ABSTAIN, new ObjectFixture(), array(), 'ACCESS_ABSTAIN for no attributes'),
array(AbstractVoter::ACCESS_ABSTAIN, new ObjectFixture(), array('foobar'), 'ACCESS_ABSTAIN for unsupported attributes'),
array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('foo'), 'ACCESS_GRANTED if attribute grants access'),
array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('bar', 'foo'), 'ACCESS_GRANTED if *at least one* attribute grants access'),
array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('foobar', 'foo'), 'ACCESS_GRANTED if *at least one* attribute grants access'),
array(AbstractVoter::ACCESS_DENIED, new ObjectFixture(), array('bar', 'baz'), 'ACCESS_DENIED for if no attribute grants access'),
);
$this->assertEquals(VoterInterface::ACCESS_GRANTED, $this->voter->vote($this->token, $this->object, array('EDIT')), 'ACCESS_GRANTED if attribute grants access');
$this->assertEquals(VoterInterface::ACCESS_DENIED, $this->voter->vote($this->token, $this->object, array('CREATE')), 'ACESS_DENIED if attribute denies access');
}
}

class VoterFixture extends AbstractVoter
{
protected function getSupportedClasses()
public function testOneAttributeSupported()
{
return array(
'Symfony\Component\Security\Core\Tests\Authorization\Voter\ObjectFixture',
);
$this->assertEquals(VoterInterface::ACCESS_GRANTED, $this->voter->vote($this->token, $this->object, array('DELETE', 'EDIT')), 'ACCESS_GRANTED if supported attribute grants access');
$this->assertEquals(VoterInterface::ACCESS_DENIED, $this->voter->vote($this->token, $this->object, array('DELETE', 'CREATE')), 'ACCESS_DENIED if supported attribute denies access');
}

protected function getSupportedAttributes()
public function testOneAttributeGrantsAccess()
{
return array('foo', 'bar', 'baz');
$this->assertEquals(VoterInterface::ACCESS_GRANTED, $this->voter->vote($this->token, $this->object, array('CREATE', 'EDIT')), 'ACCESS_GRANTED');
}

protected function voteOnAttribute($attribute, $object, TokenInterface $token)
public function testNoAttributeSupported()
{
return $attribute === 'foo';
$this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote($this->token, $this->object, array('DELETE')), 'ACCESS_ABSTAIN');
}
}

class DeprecatedVoterFixture extends AbstractVoter
{
protected function getSupportedClasses()
public function testClassNotSupported()
{
return array(
'Symfony\Component\Security\Core\Tests\Authorization\Voter\ObjectFixture',
);
$this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote($this->token, $this->getMock('AbstractVoterTest_Object1'), array('EDIT')), 'ACCESS_ABSTAIN');
}

protected function getSupportedAttributes()
public function testNullObject()
{
return array('foo', 'bar', 'baz');
$this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote($this->token, null, array('EDIT')), 'ACCESS_ABSTAIN');
}

protected function isGranted($attribute, $object, $user = null)
public function testNoAttributes()
{
return $attribute === 'foo';
$this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote($this->token, $this->object, array()), 'ACCESS_ABSTAIN');
}
}

class DeprecatedVoterNothingImplementedFixture extends AbstractVoter
class AbstractVoterTest_Voter extends AbstractVoter
{
protected function getSupportedClasses()
protected function voteOnAttribute($attribute, $object, TokenInterface $token)
{
return array(
'Symfony\Component\Security\Core\Tests\Authorization\Voter\ObjectFixture',
);
return 'EDIT' === $attribute;
}

protected function getSupportedAttributes()
protected function supports($attribute, $class)
{
return array('foo', 'bar', 'baz');
return $this->isClassInstanceOf($class, 'AbstractVoterTest_Object')
&& in_array($attribute, array('EDIT', 'CREATE'));
}

// this is a bad voter that hasn't overridden isGranted or voteOnAttribute
}

class ObjectFixture
{
}

class UnsupportedObjectFixture
{
}
8000 92D2
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@

use Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter;

class LegacyAbstractVoterTest extends AbstractVoterTest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this class still needed if we test the legacy case in the AbstractVoterTest directly ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's better to fix the tests in 2.7 and then merge it in 2.8 (where we'll add some scenarios because of deprecations). I'll open a PR soon for 2.7.

{
protected function setUp()
{
parent::setUp();

$this->voter = new LegacyAbstractVoterTest_Voter();
}

/**
* @group legacy
* @expectedException \BadMethodCallException
*/
public function testNoOverriddenMethodsThrowsException()
{
$voter = new LegacyAbstractVoterTest_NothingImplementedVoter();
$voter->vote($this->token, $this->object, array('foo'));
}
}

class LegacyAbstractVoterTest_Voter extends AbstractVoter
{
protected function getSupportedClasses()
Expand All @@ -31,12 +51,19 @@ protected function isGranted($attribute, $object, $user = null)
}
}

class LegacyAbstractVoterTest extends AbstractVoterTest
class LegacyAbstractVoterTest_NothingImplementedVoter extends AbstractVoter
{
protected function setUp()
protected function getSupportedClasses()
{
parent::setUp();
return array(
'Symfony\Component\Security\Core\Tests\Authorization\Voter\ObjectFixture',
);
}

$this->voter = new LegacyAbstractVoterTest_Voter();
protected function getSupportedAttributes()
{
return array('EDIT', 'CREATE');
}

// this is a bad voter that hasn't overridden isGranted or voteOnAttribute
}
0