1212use Symfony \Component \Security \Core \Authentication \Token \TokenInterface ;
1313use Symfony \Component \Security \Core \Authorization \AccessDecisionManagerInterface ;
1414use Symfony \Component \Security \Http \AccessMapInterface ;
15+ use Symfony \Component \Security \Http \Firewall \AccessListener ;
1516use Symfony \Component \Security \Http \HttpUtils ;
1617use Symfony \Component \Security \Http \Logout \LogoutUrlGenerator ;
1718
@@ -20,6 +21,7 @@ class TwoFactorAccessDeciderTest extends TestCase
2021 private const BASE_URL = '/app_dev.php ' ;
2122 private const LOGOUT_PATH = '/logout ' ;
2223 private const LOGOUT_PATH_WITH_BASE_URL = self ::BASE_URL .self ::LOGOUT_PATH ;
24+ private const ACCESS_MAP_ATTRIBUTES = [TwoFactorInProgressVoter::IS_AUTHENTICATED_2FA_IN_PROGRESS ];
2325
2426 /**
2527 * @var MockObject|Request
@@ -36,6 +38,11 @@ class TwoFactorAccessDeciderTest extends TestCase
3638 */
3739 private $ accessMap ;
3840
41+ /**
42+ * @var string[]
43+ */
44+ private $ attributes ;
45+
3946 /**
4047 * @var MockObject|AccessDecisionManagerInterface
4148 */
@@ -64,15 +71,17 @@ protected function setUp(): void
6471 $ this ->accessDecisionManager = $ this ->createMock (AccessDecisionManagerInterface::class);
6572 $ this ->httpUtils = $ this ->createMock (HttpUtils::class);
6673 $ this ->logoutUrlGenerator = $ this ->createMock (LogoutUrlGenerator::class);
74+ $ this ->accessDecider = new TwoFactorAccessDecider ($ this ->accessMap , $ this ->accessDecisionManager , $ this ->httpUtils , $ this ->logoutUrlGenerator );
75+ }
6776
68- // Stub an access rule
77+ private function stubAccessMapReturnsAttributes (array $ attributes ): void
78+ {
79+ $ this ->attributes = $ attributes ;
6980 $ this ->accessMap
7081 ->expects ($ this ->any ())
7182 ->method ('getPatterns ' )
7283 ->with ($ this ->request )
73- ->willReturn ([[TwoFactorInProgressVoter::IS_AUTHENTICATED_2FA_IN_PROGRESS ], 'https ' ]);
74-
75- $ this ->accessDecider = new TwoFactorAccessDecider ($ this ->accessMap , $ this ->accessDecisionManager , $ this ->httpUtils , $ this ->logoutUrlGenerator );
84+ ->willReturn ([$ attributes , 'https ' ]);
7685 }
7786
7887 private function whenGeneratedLogoutPath (string $ generatedLogoutPath ): void
@@ -96,7 +105,7 @@ private function whenPathAccess(bool $accessGranted): void
96105 $ this ->accessDecisionManager
97106 ->expects ($ this ->any ())
98107 ->method ('decide ' )
99- ->with ($ this ->isInstanceOf (TokenInterface::class), [TwoFactorInProgressVoter:: IS_AUTHENTICATED_2FA_IN_PROGRESS ] , $ this ->request )
108+ ->with ($ this ->isInstanceOf (TokenInterface::class), $ this -> attributes , $ this ->request )
100109 ->willReturn ($ accessGranted );
101110 }
102111
@@ -114,18 +123,37 @@ private function whenIsLogoutPath(bool $accessGranted): void
114123 */
115124 public function isAccessible_pathAccessGranted_returnTrue (): void
116125 {
126+ $ this ->stubAccessMapReturnsAttributes (self ::ACCESS_MAP_ATTRIBUTES );
117127 $ this ->whenPathAccess (true );
118128 $ this ->whenIsLogoutPath (false );
119129
120130 $ returnValue = $ this ->accessDecider ->isAccessible ($ this ->request , $ this ->token );
121131 $ this ->assertTrue ($ returnValue );
122132 }
123133
134+ /**
135+ * @test
136+ */
137+ public function isAccessible_isPublic_returnTrue (): void
138+ {
139+ $ this ->requireSymfony5_1 ();
140+
141+ $ this ->stubAccessMapReturnsAttributes ([AccessListener::PUBLIC_ACCESS ]);
142+ $ this ->whenRequestBaseUrl ('' );
143+ $ this ->whenGeneratedLogoutPath (self ::LOGOUT_PATH );
144+ $ this ->whenPathAccess (false );
145+ $ this ->whenIsLogoutPath (false );
146+
147+ $ returnValue = $ this ->accessDecider ->isAccessible ($ this ->request , $ this ->token );
148+ $ this ->assertTrue ($ returnValue );
149+ }
150+
124151 /**
125152 * @test
126153 */
127154 public function isAccessible_isLogoutPathNoBasePath_returnTrue (): void
128155 {
156+ $ this ->stubAccessMapReturnsAttributes (self ::ACCESS_MAP_ATTRIBUTES );
129157 $ this ->whenRequestBaseUrl ('' );
130158 $ this ->whenGeneratedLogoutPath (self ::LOGOUT_PATH );
131159 $ this ->whenPathAccess (false );
@@ -140,6 +168,7 @@ public function isAccessible_isLogoutPathNoBasePath_returnTrue(): void
140168 */
141169 public function isAccessible_isLogoutPathWithBasePath_returnTrue (): void
142170 {
171+ $ this ->stubAccessMapReturnsAttributes (self ::ACCESS_MAP_ATTRIBUTES );
143172 $ this ->whenRequestBaseUrl (self ::BASE_URL );
144173 $ this ->whenGeneratedLogoutPath (self ::LOGOUT_PATH_WITH_BASE_URL );
145174 $ this ->whenPathAccess (false );
@@ -154,6 +183,7 @@ public function isAccessible_isLogoutPathWithBasePath_returnTrue(): void
154183 */
155184 public function isAccessible_isNotAccessible_returnFalse (): void
156185 {
186+ $ this ->stubAccessMapReturnsAttributes (self ::ACCESS_MAP_ATTRIBUTES );
157187 $ this ->whenRequestBaseUrl ('' );
158188 $ this ->whenGeneratedLogoutPath (self ::LOGOUT_PATH );
159189 $ this ->whenPathAccess (false );
0 commit comments