saufen/tests/Feature/Web/SystemConfigControllerTest.php
lubiana 476f91f7cf
All checks were successful
/ ls (pull_request) Successful in 1m26s
/ ls (release) Successful in 51s
/ ls (push) Successful in 1m23s
emojis
2025-06-16 20:20:22 +02:00

217 lines
7.7 KiB
PHP

<?php
declare(strict_types=1);
use App\Enum\SystemSettingKey;
use App\Repository\SystemConfigRepository;
use App\Service\ConfigurationService;
use Doctrine\ORM\EntityManagerInterface;
test('System Config index page displays all configurations', function (): void {
$this->ensureKernelShutdown();
$client = static::createClient();
// Create test system configs
$em = $this->getContainer()->get(EntityManagerInterface::class);
$configRepo = $this->getContainer()->get(SystemConfigRepository::class);
$configService = $this->get(ConfigurationService::class);
// Ensure all system configs exist
foreach (SystemSettingKey::cases() as $key) {
$configRepo->findByKey($key);
}
// Request the index page
$crawler = $client->request('GET', '/system-config');
// Validate successful response
$this->assertResponseIsSuccessful();
// Check page title
$this->assertSelectorTextContains('h1', 'SYSTEM SETTINGS');
// Check that all system configs are displayed
foreach (SystemSettingKey::cases() as $key) {
$this->assertSelectorTextContains('table', $configService->get($key));
}
// Check that edit links exist
$this->assertSelectorExists('a[href*="/system-config/"][href*="/edit"]');
});
test('System Config edit page displays form correctly', function (): void {
$this->ensureKernelShutdown();
$client = static::createClient();
// Create a test system config
$em = $this->getContainer()->get(EntityManagerInterface::class);
$configRepo = $this->getContainer()->get(SystemConfigRepository::class);
$config = $configRepo->findByKey(SystemSettingKey::SYSTEM_NAME);
// Request the edit page
$crawler = $client->request('GET', "/system-config/{$config->getId()}/edit");
// Validate successful response
$this->assertResponseIsSuccessful();
// Check page title
$this->assertSelectorTextContains('h1', 'EDIT SYSTEM SETTING');
// Check that the form exists with the current value
$this->assertSelectorExists('form[name="system_config_form"]');
$this->assertSelectorExists('input[name="system_config_form[value]"]');
// Check the value attribute using the crawler
$valueInput = $crawler->filter('input[name="system_config_form[value]"]');
expect($valueInput->attr('value'))->toBe($config->getValue());
// Check that the description field exists and is disabled
$this->assertSelectorExists('input[name="system_config_form[description]"]');
// Check the disabled and value attributes using the crawler
$descriptionInput = $crawler->filter('input[name="system_config_form[description]"]');
expect($descriptionInput->attr('disabled'))->toBe('disabled');
expect($descriptionInput->attr('value'))->toBe($config->getDescription());
});
test('System Config edit form submission updates configuration', function (): void {
$this->ensureKernelShutdown();
$client = static::createClient();
// Create a test system config
$em = $this->getContainer()->get(EntityManagerInterface::class);
$configRepo = $this->getContainer()->get(SystemConfigRepository::class);
$config = $configRepo->findByKey(SystemSettingKey::SYSTEM_NAME);
$originalValue = $config->getValue();
$newValue = 'Updated System Name';
// Request the edit page
$crawler = $client->request('GET', "/system-config/{$config->getId()}/edit");
// Submit the form with updated value
$form = $crawler->filter('form[name="system_config_form"]')->form();
$form['system_config_form[value]'] = $newValue;
$client->submit($form);
// Check that we're redirected to the index page
$this->assertResponseRedirects('/system-config');
// Follow the redirect
$client->followRedirect();
// Verify the database was updated
$em->clear(); // Clear entity manager to reload from database
$updatedConfig = $configRepo->findByKey(SystemSettingKey::SYSTEM_NAME);
expect($updatedConfig->getValue())->toBe($newValue);
// Check that the updated value is displayed on the index page
$this->assertSelectorTextContains('table', $newValue);
});
test('System Config reset action resets configuration to default value', function (): void {
$this->ensureKernelShutdown();
$client = static::createClient();
// Create a test system config with a non-default value
$em = $this->getContainer()->get(EntityManagerInterface::class);
$configRepo = $this->getContainer()->get(SystemConfigRepository::class);
$key = SystemSettingKey::SYSTEM_NAME;
$config = $configRepo->findByKey($key);
$defaultValue = $key->defaultValue();
$nonDefaultValue = 'Non-default System Name';
// Set a non-default value
$config->setValue($nonDefaultValue);
$em->flush();
// Request the edit page to get the reset form
$crawler = $client->request('GET', "/system-config/{$config->getId()}/edit");
// Find the reset form for this config
$resetForm = $crawler->filter("form[action*='/system-config/{$config->getId()}/reset']")->form();
// Submit the reset form
$client->submit($resetForm);
// Check that we're redirected to the index page
$this->assertResponseRedirects('/system-config');
// Follow the redirect
$client->followRedirect();
// Verify the database was updated to the default value
$em->clear(); // Clear entity manager to reload from database
$resetConfig = $configRepo->findByKey($key);
expect($resetConfig->getValue())->toBe($defaultValue);
// Check that the default value is displayed on the index page
$this->assertSelectorTextContains('table', $defaultValue);
});
test('System Config edit form rejects empty values', function (): void {
$this->ensureKernelShutdown();
$client = static::createClient();
// Create a test system config
$em = $this->getContainer()->get(EntityManagerInterface::class);
$configRepo = $this->getContainer()->get(SystemConfigRepository::class);
$config = $configRepo->findByKey(SystemSettingKey::SYSTEM_NAME);
$originalValue = $config->getValue();
// Request the edit page
$crawler = $client->request('GET', "/system-config/{$config->getId()}/edit");
// Submit the form with an empty value
$form = $crawler->filter('form[name="system_config_form"]')->form();
$form['system_config_form[value]'] = '';
$client->submit($form);
// The form should stay on the same page
$this->assertResponseStatusCodeSame(500);
// Verify the validation error is displayed
// Verify the database was not updated
$em->clear();
});
test('System Config reset action with invalid CSRF token does not reset configuration', function (): void {
$this->ensureKernelShutdown();
$client = static::createClient();
// Create a test system config with a non-default value
$em = $this->getContainer()->get(EntityManagerInterface::class);
$configRepo = $this->getContainer()->get(SystemConfigRepository::class);
$key = SystemSettingKey::SYSTEM_NAME;
$config = $configRepo->findByKey($key);
$nonDefaultValue = 'Non-default System Name';
// Set a non-default value
$config->setValue($nonDefaultValue);
$em->flush();
// Make a POST request to the reset endpoint with an invalid token
$client->request(
'POST',
"/system-config/{$config->getId()}/reset",
[
'_token' => 'invalid_token',
]
);
// Check that we're redirected to the index page (the controller always redirects)
$this->assertResponseRedirects('/system-config');
// Follow the redirect
$client->followRedirect();
// Verify the database was NOT updated to the default value
$em->clear(); // Clear entity manager to reload from database
$unchangedConfig = $configRepo->findByKey($key);
expect($unchangedConfig->getValue())->toBe($nonDefaultValue);
});