test system settings
This commit is contained in:
parent
c3411e5754
commit
b4ae1f9007
2 changed files with 221 additions and 0 deletions
|
@ -9,6 +9,7 @@ use Symfony\Component\Form\AbstractType;
|
||||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||||
use Symfony\Component\Form\FormBuilderInterface;
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||||
|
use Symfony\Component\Validator\Constraints\NotBlank;
|
||||||
|
|
||||||
class SystemConfigForm extends AbstractType
|
class SystemConfigForm extends AbstractType
|
||||||
{
|
{
|
||||||
|
@ -18,6 +19,9 @@ class SystemConfigForm extends AbstractType
|
||||||
->add('value', TextType::class, [
|
->add('value', TextType::class, [
|
||||||
'label' => 'Value',
|
'label' => 'Value',
|
||||||
'help' => 'The value of the system setting',
|
'help' => 'The value of the system setting',
|
||||||
|
'constraints' => [
|
||||||
|
new NotBlank(),
|
||||||
|
],
|
||||||
])
|
])
|
||||||
->add('description', TextType::class, [
|
->add('description', TextType::class, [
|
||||||
'label' => 'Description',
|
'label' => 'Description',
|
||||||
|
|
217
tests/Feature/Web/SystemConfigControllerTest.php
Normal file
217
tests/Feature/Web/SystemConfigControllerTest.php
Normal file
|
@ -0,0 +1,217 @@
|
||||||
|
<?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: ' . $config->getKey()->name);
|
||||||
|
|
||||||
|
// 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->selectButton('Update')->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->selectButton('Update')->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);
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue