From b4ae1f90075889814ecb1e69195032d0ad0e6f92 Mon Sep 17 00:00:00 2001 From: lubiana Date: Sat, 14 Jun 2025 21:24:46 +0200 Subject: [PATCH] test system settings --- src/Form/SystemConfigForm.php | 4 + .../Web/SystemConfigControllerTest.php | 217 ++++++++++++++++++ 2 files changed, 221 insertions(+) create mode 100644 tests/Feature/Web/SystemConfigControllerTest.php diff --git a/src/Form/SystemConfigForm.php b/src/Form/SystemConfigForm.php index f138fd9..fce2162 100644 --- a/src/Form/SystemConfigForm.php +++ b/src/Form/SystemConfigForm.php @@ -9,6 +9,7 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Component\Validator\Constraints\NotBlank; class SystemConfigForm extends AbstractType { @@ -18,6 +19,9 @@ class SystemConfigForm extends AbstractType ->add('value', TextType::class, [ 'label' => 'Value', 'help' => 'The value of the system setting', + 'constraints' => [ + new NotBlank(), + ], ]) ->add('description', TextType::class, [ 'label' => 'Description', diff --git a/tests/Feature/Web/SystemConfigControllerTest.php b/tests/Feature/Web/SystemConfigControllerTest.php new file mode 100644 index 0000000..68af4bf --- /dev/null +++ b/tests/Feature/Web/SystemConfigControllerTest.php @@ -0,0 +1,217 @@ +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); +});