Skip to content

Commit 7c9e696

Browse files
authored
Add tests for SecurityConfigUpdater (#1245)
1 parent a5d8b54 commit 7c9e696

File tree

5 files changed

+112
-24
lines changed

5 files changed

+112
-24
lines changed

src/Security/SecurityConfigUpdater.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,22 @@ public function __construct(
3232

3333
public function updateForFormLogin(string $yamlSource, string $firewallToUpdate, string $loginPath, string $checkPath): string
3434
{
35-
$this->manipulator = new YamlSourceManipulator($yamlSource);
36-
37-
if (null !== $this->ysmLogger) {
38-
$this->manipulator->setLogger($this->ysmLogger);
39-
}
40-
41-
$this->normalizeSecurityYamlFile();
42-
43-
$newData = $this->manipulator->getData();
35+
$newData = $this->createYamlSourceManipulator($yamlSource);
4436

4537
$newData['security']['firewalls'][$firewallToUpdate]['form_login']['login_path'] = $loginPath;
4638
$newData['security']['firewalls'][$firewallToUpdate]['form_login']['check_path'] = $checkPath;
4739
$newData['security']['firewalls'][$firewallToUpdate]['form_login']['enable_csrf'] = true;
4840

49-
$this->manipulator->setData($newData);
41+
return $this->getYamlContentsFromData($newData);
42+
}
5043

51-
return $this->manipulator->getContents();
44+
public function updateForJsonLogin(string $yamlSource, string $firewallToUpdate, string $checkPath): string
45+
{
46+
$data = $this->createYamlSourceManipulator($yamlSource);
47+
48+
$data['security']['firewalls'][$firewallToUpdate]['json_login']['check_path'] = $checkPath;
49+
50+
return $this->getYamlContentsFromData($data);
5251
}
5352

5453
/**
@@ -149,7 +148,7 @@ private function configureLogout(array $securityData, string $firewallName): voi
149148
$this->manipulator->setData($securityData);
150149
}
151150

152-
private function createYamlSourceManipulator(string $yamlSource): void
151+
private function createYamlSourceManipulator(string $yamlSource): array
153152
{
154153
$this->manipulator = new YamlSourceManipulator($yamlSource);
155154

@@ -158,6 +157,15 @@ private function createYamlSourceManipulator(string $yamlSource): void
158157
}
159158

160159
$this->normalizeSecurityYamlFile();
160+
161+
return $this->manipulator->getData();
162+
}
163+
164+
private function getYamlContentsFromData(array $yamlData): string
165+
{
166+
$this->manipulator->setData($yamlData);
167+
168+
return $this->manipulator->getContents();
161169
}
162170

163171
private function normalizeSecurityYamlFile(): void

tests/Security/SecurityConfigUpdaterTest.php

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,9 @@ class SecurityConfigUpdaterTest extends TestCase
2222
/**
2323
* Set to true to enable low level debug logging during tests for
2424
* the YamlSourceManipulator.
25-
*
26-
* @var bool
2725
*/
28-
private $enableYsmLogging = false;
29-
30-
/**
31-
* @var Logger|null
32-
*/
33-
private $ysmLogger = null;
26+
private bool $enableYsmLogging = false;
27+
private ?Logger $ysmLogger = null;
3428

3529
/**
3630
* @dataProvider getUserClassTests
@@ -45,9 +39,9 @@ public function testUpdateForUserClass(UserClassConfiguration $userConfig, strin
4539
}
4640

4741
$updater = new SecurityConfigUpdater($this->ysmLogger);
48-
$source = file_get_contents(__DIR__.'/yaml_fixtures/source/'.$startingSourceFilename);
42+
$source = $this->getYamlSource($startingSourceFilename);
4943
$actualSource = $updater->updateForUserClass($source, $userConfig, $userClass);
50-
$expectedSource = file_get_contents(__DIR__.'/yaml_fixtures/expected_user_class/5.3/'.$expectedSourceFilename);
44+
$expectedSource = $this->getExpectedYaml('expected_user_class/5.3', $expectedSourceFilename);
5145

5246
$expectedSource = str_replace('{BCRYPT_OR_AUTO}', 'auto', $expectedSource);
5347

@@ -115,9 +109,9 @@ public function testUpdateForAuthenticator(string $firewallName, $entryPoint, st
115109
$this->createLogger();
116110

117111
$updater = new SecurityConfigUpdater($this->ysmLogger);
118-
$source = file_get_contents(__DIR__.'/yaml_fixtures/source/'.$startingSourceFilename);
112+
$source = $this->getYamlSource($startingSourceFilename);
119113
$actualSource = $updater->updateForAuthenticator($source, $firewallName, $entryPoint, 'App\\Security\\AppCustomAuthenticator', $logoutSetup);
120-
$expectedSource = file_get_contents(__DIR__.'/yaml_fixtures/expected_authenticator/'.$expectedSourceFilename);
114+
$expectedSource = $this->getExpectedYaml('expected_authenticator', $expectedSourceFilename);
121115

122116
$this->assertSame($expectedSource, $actualSource);
123117
}
@@ -173,6 +167,51 @@ public function getAuthenticatorTests(): \Generator
173167
];
174168
}
175169

170+
public function testUpdateForFormLogin(): void
171+
{
172+
$this->createLogger();
173+
174+
$updater = new SecurityConfigUpdater($this->ysmLogger);
175+
$source = $this->getYamlSource('empty_security.yaml');
176+
177+
$actualSource = $updater->updateForFormLogin($source, 'main', 'a_login_path', 'a_check_path');
178+
179+
$this->assertSame(
180+
$this->getExpectedYaml('expected_form_login', 'form_login.yaml'),
181+
$actualSource
182+
);
183+
}
184+
185+
public function testUpdateForJsonLogin(): void
186+
{
187+
$this->createLogger();
188+
189+
$updater = new SecurityConfigUpdater($this->ysmLogger);
190+
$source = $this->getYamlSource('empty_security.yaml');
191+
192+
$actualSource = $updater->updateForJsonLogin($source, 'main', 'a_check_path');
193+
194+
$this->assertSame(
195+
$this->getExpectedYaml('expected_json_login', 'json_login.yaml'),
196+
$actualSource
197+
);
198+
}
199+
200+
public function testUpdateForLogout(): void
201+
{
202+
$this->createLogger();
203+
204+
$updater = new SecurityConfigUpdater($this->ysmLogger);
205+
$source = $this->getYamlSource('simple_security_with_firewalls.yaml');
206+
207+
$actualSource = $updater->updateForLogout($source, 'main');
208+
209+
$this->assertSame(
210+
$this->getExpectedYaml('expected_logout', 'logout.yaml'),
211+
$actualSource
212+
);
213+
}
214+
176215
private function createLogger(): void
177216
{
178217
if (!$this->enableYsmLogging) {
@@ -194,4 +233,14 @@ private function createLogger(): void
194233
return $message."\n\n";
195234
});
196235
}
236+
237+
private function getYamlSource(string $yamlFileName): string
238+
{
239+
return file_get_contents(sprintf('%s/yaml_fixtures/source/%s', __DIR__, $yamlFileName));
240+
}
241+
242+
private function getExpectedYaml(string $subDirectory, string $yamlFileName): string
243+
{
244+
return file_get_contents(sprintf('%s/yaml_fixtures/%s/%s', __DIR__, $subDirectory, $yamlFileName));
245+
}
197246
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
security:
2+
enable_authenticator_manager: true
3+
firewalls:
4+
main:
5+
form_login:
6+
login_path: a_login_path
7+
check_path: a_check_path
8+
enable_csrf: true
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
security:
2+
enable_authenticator_manager: true
3+
firewalls:
4+
main:
5+
json_login:
6+
check_path: a_check_path
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
security:
2+
enable_authenticator_manager: true
3+
4+
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
5+
providers:
6+
in_memory: { memory: ~ }
7+
8+
firewalls:
9+
dev:
10+
pattern: ^/(_(profiler|wdt)|css|images|js)/
11+
security: false
12+
main:
13+
lazy: true
14+
logout:
15+
path: app_logout
16+
# where to redirect after logout
17+
# target: app_any_route

0 commit comments

Comments
 (0)