@@ -83,6 +83,38 @@ public function getIsGrantedTests()
8383 );
8484 }
8585
86+ public function testCsrfTokenWithCsrfTokenManager ()
87+ {
88+ $ tokenId = 'foo ' ;
89+ $ tokenValue = 'xsrf ' ;
90+ $ content = "{' $ tokenId'|csrf_token} " ;
91+ $ template = 'csrf_token_manager_test.html.tpl ' ;
92+
93+ // symfony 2.3+
94+ $ this ->engine ->setTemplate ($ template , $ content );
95+ $ context = $ this ->createSecurityContext ();
96+ $ csrfTokenManager = $ this ->createCsrfTokenManager ($ tokenId , $ tokenValue );
97+ $ this ->engine ->addExtension (new SecurityExtension ($ context , $ csrfTokenManager ));
98+
99+ $ this ->assertEquals ($ tokenValue , $ this ->engine ->render ($ template ));
100+ }
101+
102+ public function testCsrfTokenWithCsrfProvider ()
103+ {
104+ $ tokenId = 'bar ' ;
105+ $ tokenValue = 'xsrf ' ;
106+ $ content = "{' $ tokenId'|csrf_token} " ;
107+ $ template = 'csrf_provider_test.html.tpl ' ;
108+
109+ // symfony 2.1
110+ $ this ->engine ->setTemplate ($ template , $ content );
111+ $ context = $ this ->createSecurityContext ();
112+ $ csrfProvider = $ this ->createCsrfProvider ($ tokenId , $ tokenValue );
113+ $ this ->engine ->addExtension (new SecurityExtension ($ context , $ csrfProvider ));
114+
115+ $ this ->assertEquals ($ tokenValue , $ this ->engine ->render ($ template ));
116+ }
117+
86118 protected function createSecurityContext ($ granted = false )
87119 {
88120 $ authManager = $ this ->getMock ('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface ' );
@@ -99,4 +131,22 @@ protected function createSecurityContext($granted = false)
99131
100132 return $ context ;
101133 }
134+
135+ protected function createCsrfTokenManager ($ tokenId , $ value )
136+ {
137+ $ csrfToken = $ this ->getMock ('stdClass ' , array ('getValue ' ));
138+ $ csrfToken ->expects ($ this ->any ())->method ('getValue ' )->will ($ this ->returnValue ($ value ));
139+ $ csrfTokenManager = $ this ->getMock ('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface ' , array ('getToken ' , 'refreshToken ' , 'removeToken ' , 'isTokenValid ' ));
140+ $ csrfTokenManager ->expects ($ this ->any ())->method ('getToken ' )->will ($ this ->returnValue ($ csrfToken ));
141+
142+ return $ csrfTokenManager ;
143+ }
144+
145+ protected function createCsrfProvider ($ tokenId , $ value )
146+ {
147+ $ csrfProvider = $ this ->getMock ('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface ' , array ('generateCsrfToken ' ));
148+ $ csrfProvider ->expects ($ this ->any ())->method ('generateCsrfToken ' )->will ($ this ->returnValue ($ value ));
149+
150+ return $ csrfProvider ;
151+ }
102152}
0 commit comments