88use Symfony \Component \Security \Core \Authentication \Token \TokenInterface ;
99use Symfony \Component \Security \Core \Authorization \AuthorizationCheckerInterface ;
1010use Symfony \Component \Security \Core \Role \Role ;
11+ use Symfony \Component \Validator \Validator \ValidatorInterface ;
1112use Symfony \Component \Workflow \EventListener \ExpressionLanguage ;
1213use Symfony \Component \Workflow \EventListener \GuardListener ;
1314use Symfony \Component \Workflow \Event \GuardEvent ;
1617
1718class 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
72- /**
73- * @expectedException \Symfony\Component\Workflow\Exception\InvalidTokenConfigurationException
74- * @expectedExceptionMessage There are no tokens available for workflow unnamed.
75- */
76- public function testWithNoTokensInTokenStorage ()
89+ public function testWithValidatorSupportedEventAndAccept ()
7790 {
7891 $ event = $ this ->createEvent ();
79- $ this ->tokenStorage ->setToken (null );
92+ $ this ->configureValidator (true , true );
93+
94+ $ this ->listener ->onTransition ($ event , 'test_is_valid ' );
8095
81- $ this ->listener -> onTransition ($ event, ' event_name_a ' );
96+ $ this ->assertFalse ($ event-> isBlocked () );
8297 }
8398
8499 private function createEvent ()
@@ -90,28 +105,39 @@ private function createEvent()
90105 return new GuardEvent ($ subject , $ subject ->marking , $ transition );
91106 }
92107
93- private function configureTokenStorage ( $ hasUser )
108+ private function configureAuthenticationChecker ( $ isUsed , $ granted = true )
94109 {
95- if (!$ hasUser ) {
96- $ this ->tokenStorage
110+ if (!$ isUsed ) {
111+ $ this ->authenticationChecker
97112 ->expects ($ this ->never ())
98- ->method ('getToken ' )
113+ ->method ('isGranted ' )
99114 ;
100115
101116 return ;
102117 }
103118
104- $ token = $ this ->getMockBuilder (TokenInterface::class)->getMock ();
105- $ token
119+ $ this ->authenticationChecker
106120 ->expects ($ this ->once ())
107- ->method ('getRoles ' )
108- ->willReturn (array ( new Role ( ' ROLE_ADMIN ' )) )
121+ ->method ('isGranted ' )
122+ ->willReturn ($ granted )
109123 ;
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+ }
110136
111- $ this ->tokenStorage
137+ $ this ->validator
112138 ->expects ($ this ->once ())
113- ->method ('getToken ' )
114- ->willReturn ($ token )
139+ ->method ('validate ' )
140+ ->willReturn ($ valid ? array () : array ( ' a violation ' ) )
115141 ;
116142 }
117143}
0 commit comments