Skip to content

Commit c3c6679

Browse files
committed
[Workflow] Added tests for the is_valid() guard expression
1 parent 6ebdf8f commit c3c6679

File tree

4 files changed

+68
-29
lines changed

4 files changed

+68
-29
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CHANGELOG
44
3.4.0
55
-----
66

7-
* Add guard `is_valid()` method support
7+
* Added guard `is_valid()` method support.
88
* Added support for `Event::getWorkflowName()` for "announce" events.
99
* Added `workflow.completed` events which are fired after a transition is completed.
1010

EventListener/ExpressionLanguage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected function registerFunctions()
3636
return sprintf('0 === count($validator->validate(%s, null, %s))', $object, $groups);
3737
}, function (array $variables, $object = null, $groups = null) {
3838
if (!$variables['validator'] instanceof ValidatorInterface) {
39-
throw new RuntimeException('Validator not defined, did you install the component?');
39+
throw new RuntimeException('"is_valid" cannot be used as the Validator component is not installed.');
4040
}
4141

4242
$errors = $variables['validator']->validate($object, null, $groups);

Tests/EventListener/GuardListenerTest.php

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
99
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
1010
use Symfony\Component\Security\Core\Role\Role;
11+
use Symfony\Component\Validator\Validator\ValidatorInterface;
1112
use Symfony\Component\Workflow\EventListener\ExpressionLanguage;
1213
use Symfony\Component\Workflow\EventListener\GuardListener;
1314
use Symfony\Component\Workflow\Event\GuardEvent;
@@ -16,59 +17,85 @@
1617

1718
class GuardListenerTest extends TestCase
1819
{
19-
private $tokenStorage;
20+
private $authenticationChecker;
21+
private $validator;
2022
private $listener;
2123

2224
protected function setUp()
2325
{
2426
$configuration = array(
25-
'event_name_a' => 'true',
26-
'event_name_b' => 'false',
27+
'test_is_granted' => 'is_granted("something")',
28+
'test_is_valid' => 'is_valid(subject)',
2729
);
28-
2930
$expressionLanguage = new ExpressionLanguage();
30-
$this->tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock();
31-
$authenticationChecker = $this->getMockBuilder(AuthorizationCheckerInterface::class)->getMock();
31+
$token = $this->getMockBuilder(TokenInterface::class)->getMock();
32+
$token->expects($this->any())->method('getRoles')->willReturn(array(new Role('ROLE_USER')));
33+
$tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock();
34+
$tokenStorage->expects($this->any())->method('getToken')->willReturn($token);
35+
$this->authenticationChecker = $this->getMockBuilder(AuthorizationCheckerInterface::class)->getMock();
3236
$trustResolver = $this->getMockBuilder(AuthenticationTrustResolverInterface::class)->getMock();
33-
34-
$this->listener = new GuardListener($configuration, $expressionLanguage, $this->tokenStorage, $authenticationChecker, $trustResolver);
37+
$this->validator = $this->getMockBuilder(ValidatorInterface::class)->getMock();
38+
$this->listener = new GuardListener($configuration, $expressionLanguage, $tokenStorage, $this->authenticationChecker, $trustResolver, null, $this->validator);
3539
}
3640

3741
protected function tearDown()
3842
{
43+
$this->authenticationChecker = null;
44+
$this->validator = null;
3945
$this->listener = null;
4046
}
4147

4248
public function testWithNotSupportedEvent()
4349
{
4450
$event = $this->createEvent();
45-
$this->configureTokenStorage(false);
51+
$this->configureAuthenticationChecker(false);
52+
$this->configureValidator(false);
4653

4754
$this->listener->onTransition($event, 'not supported');
4855

4956
$this->assertFalse($event->isBlocked());
5057
}
5158

52-
public function testWithSupportedEventAndReject()
59+
public function testWithSecuritySupportedEventAndReject()
5360
{
5461
$event = $this->createEvent();
55-
$this->configureTokenStorage(true);
62+
$this->configureAuthenticationChecker(true, false);
5663

57-
$this->listener->onTransition($event, 'event_name_a');
64+
$this->listener->onTransition($event, 'test_is_granted');
65+
66+
$this->assertTrue($event->isBlocked());
67+
}
68+
69+
public function testWithSecuritySupportedEventAndAccept()
70+
{
71+
$event = $this->createEvent();
72+
$this->configureAuthenticationChecker(true, true);
73+
74+
$this->listener->onTransition($event, 'test_is_granted');
5875

5976
$this->assertFalse($event->isBlocked());
6077
}
6178

62-
public function testWithSupportedEventAndAccept()
79+
public function testWithValidatorSupportedEventAndReject()
6380
{
6481
$event = $this->createEvent();
65-
$this->configureTokenStorage(true);
82+
$this->configureValidator(true, false);
6683

67-
$this->listener->onTransition($event, 'event_name_b');
84+
$this->listener->onTransition($event, 'test_is_valid');
6885

6986
$this->assertTrue($event->isBlocked());
7087
}
7188

89+
public function testWithValidatorSupportedEventAndAccept()
90+
{
91+
$event = $this->createEvent();
92+
$this->configureValidator(true, true);
93+
94+
$this->listener->onTransition($event, 'test_is_valid');
95+
96+
$this->assertFalse($event->isBlocked());
97+
}
98+
7299
private function createEvent()
73100
{
74101
$subject = new \stdClass();
@@ -78,28 +105,39 @@ private function createEvent()
78105
return new GuardEvent($subject, $subject->marking, $transition);
79106
}
80107

81-
private function configureTokenStorage($hasUser)
108+
private function configureAuthenticationChecker($isUsed, $granted = true)
82109
{
83-
if (!$hasUser) {
84-
$this->tokenStorage
110+
if (!$isUsed) {
111+
$this->authenticationChecker
85112
->expects($this->never())
86-
->method('getToken')
113+
->method('isGranted')
87114
;
88115

89116
return;
90117
}
91118

92-
$token = $this->getMockBuilder(TokenInterface::class)->getMock();
93-
$token
119+
$this->authenticationChecker
94120
->expects($this->once())
95-
->method('getRoles')
96-
->willReturn(array(new Role('ROLE_ADMIN')))
121+
->method('isGranted')
122+
->willReturn($granted)
97123
;
124+
}
125+
126+
private function configureValidator($isUsed, $valid = true)
127+
{
128+
if (!$isUsed) {
129+
$this->validator
130+
->expects($this->never())
131+
->method('validate')
132+
;
133+
134+
return;
135+
}
98136

99-
$this->tokenStorage
137+
$this->validator
100138
->expects($this->once())
101-
->method('getToken')
102-
->willReturn($token)
139+
->method('validate')
140+
->willReturn($valid ? array() : array('a violation'))
103141
;
104142
}
105143
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"symfony/dependency-injection": "~2.8|~3.0|~4.0",
2929
"symfony/event-dispatcher": "~2.1|~3.0|~4.0",
3030
"symfony/expression-language": "~2.8|~3.0|~4.0",
31-
"symfony/security-core": "~2.8|~3.0|~4.0"
31+
"symfony/security-core": "~2.8|~3.0|~4.0",
32+
"symfony/validator": "~2.8|~3.4|~4.0"
3233
},
3334
"autoload": {
3435
"psr-4": { "Symfony\\Component\\Workflow\\": "" }

0 commit comments

Comments
 (0)