Skip to content

Commit 66cbef4

Browse files
committed
[FrameworkBundle] fixed guard event names for transitions
1 parent c946ca0 commit 66cbef4

File tree

3 files changed

+24
-32
lines changed

3 files changed

+24
-32
lines changed

EventListener/GuardExpression.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,22 @@ class GuardExpression
1919

2020
private $expression;
2121

22-
public function getTransition(): Transition
22+
/**
23+
* @param string $expression
24+
*/
25+
public function __construct(Transition $transition, $expression)
2326
{
24-
return $this->transition;
27+
$this->transition = $transition;
28+
$this->expression = $expression;
2529
}
2630

27-
public function getExpression(): string
31+
public function getTransition()
2832
{
29-
return $this->expression;
33+
return $this->transition;
3034
}
3135

32-
public function __construct(Transition $transition, string $expression)
36+
public function getExpression()
3337
{
34-
$this->transition = $transition;
35-
$this->expression = $expression;
38+
return $this->expression;
3639
}
3740
}

EventListener/GuardListener.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Symfony\Component\Validator\Validator\ValidatorInterface;
1919
use Symfony\Component\Workflow\Event\GuardEvent;
2020
use Symfony\Component\Workflow\Exception\InvalidTokenConfigurationException;
21-
use Symfony\Component\Workflow\TransitionBlocker;
2221

2322
/**
2423
* @author Grégoire Pineau <lyrixx@lyrixx.info>
@@ -63,16 +62,15 @@ public function onTransition(GuardEvent $event, $eventName)
6362
}
6463
}
6564

66-
private function validateGuardExpression(GuardEvent $event, string $expression)
65+
private function validateGuardExpression(GuardEvent $event, $expression)
6766
{
6867
if (!$this->expressionLanguage->evaluate($expression, $this->getVariables($event))) {
69-
$blocker = TransitionBlocker::createBlockedByExpressionGuardListener($expression);
70-
$event->addTransitionBlocker($blocker);
68+
$event->setBlocked(true);
7169
}
7270
}
7371

7472
// code should be sync with Symfony\Component\Security\Core\Authorization\Voter\ExpressionVoter
75-
private function getVariables(GuardEvent $event): array
73+
private function getVariables(GuardEvent $event)
7674
{
7775
$token = $this->tokenStorage->getToken();
7876

Tests/EventListener/GuardListenerTest.php

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ class GuardListenerTest extends TestCase
2121
private $authenticationChecker;
2222
private $validator;
2323
private $listener;
24-
private $transition;
24+
private $configuration;
2525

2626
protected function setUp()
2727
{
28-
$configuration = array(
28+
$this->configuration = array(
2929
'test_is_granted' => 'is_granted("something")',
3030
'test_is_valid' => 'is_valid(subject)',
3131
'test_expression' => array(
32-
new GuardExpression($this->getTransition(true), '!is_valid(subject)'),
33-
new GuardExpression($this->getTransition(true), 'is_valid(subject)'),
32+
new GuardExpression(new Transition('name', 'from', 'to'), '!is_valid(subject)'),
33+
new GuardExpression(new Transition('name', 'from', 'to'), 'is_valid(subject)'),
3434
),
3535
);
3636
$expressionLanguage = new ExpressionLanguage();
@@ -41,7 +41,7 @@ protected function setUp()
4141
$this->authenticationChecker = $this->getMockBuilder(AuthorizationCheckerInterface::class)->getMock();
4242
$trustResolver = $this->getMockBuilder(AuthenticationTrustResolverInterface::class)->getMock();
4343
$this->validator = $this->getMockBuilder(ValidatorInterface::class)->getMock();
44-
$this->listener = new GuardListener($configuration, $expressionLanguage, $tokenStorage, $this->authenticationChecker, $trustResolver, null, $this->validator);
44+
$this->listener = new GuardListener($this->configuration, $expressionLanguage, $tokenStorage, $this->authenticationChecker, $trustResolver, null, $this->validator);
4545
}
4646

4747
protected function tearDown()
@@ -104,16 +104,16 @@ public function testWithValidatorSupportedEventAndAccept()
104104

105105
public function testWithGuardExpressionWithNotSupportedTransition()
106106
{
107-
$event = $this->createEvent(true);
108-
$this->configureValidator(false, false);
107+
$event = $this->createEvent();
108+
$this->configureValidator(false);
109109
$this->listener->onTransition($event, 'test_expression');
110110

111111
$this->assertFalse($event->isBlocked());
112112
}
113113

114114
public function testWithGuardExpressionWithSupportedTransition()
115115
{
116-
$event = $this->createEvent();
116+
$event = $this->createEvent($this->configuration['test_expression'][1]->getTransition());
117117
$this->configureValidator(true, true);
118118
$this->listener->onTransition($event, 'test_expression');
119119

@@ -122,18 +122,18 @@ public function testWithGuardExpressionWithSupportedTransition()
122122

123123
public function testGuardExpressionBlocks()
124124
{
125-
$event = $this->createEvent();
125+
$event = $this->createEvent($this->configuration['test_expression'][1]->getTransition());
126126
$this->configureValidator(true, false);
127127
$this->listener->onTransition($event, 'test_expression');
128128

129129
$this->assertTrue($event->isBlocked());
130130
}
131131

132-
private function createEvent($newTransition = false)
132+
private function createEvent(Transition $transition = null)
133133
{
134134
$subject = new \stdClass();
135135
$subject->marking = new Marking();
136-
$transition = $this->getTransition($newTransition);
136+
$transition = $transition ?: new Transition('name', 'from', 'to');
137137

138138
return new GuardEvent($subject, $subject->marking, $transition);
139139
}
@@ -173,13 +173,4 @@ private function configureValidator($isUsed, $valid = true)
173173
->willReturn($valid ? array() : array('a violation'))
174174
;
175175
}
176-
177-
private function getTransition($new = false)
178-
{
179-
if ($new || !$this->transition) {
180-
$this->transition = new Transition('name', 'from', 'to');
181-
}
182-
183-
return $this->transition;
184-
}
185176
}

0 commit comments

Comments
 (0)