Loading core/lib/Drupal/Core/Condition/ConditionPluginBase.php +4 Changes for core/lib/Drupal/Core/Condition/ConditionPluginBase.php: 4 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -66,6 +66,10 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta * {@inheritdoc} */ public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { // In the config schema, 'negate' is a boolean. However, some form elements // such as checkbox return the value as 0/1. Cast here to ensure the data is // in the expected type. $form_state->setValue('negate', (bool) $form_state->getValue('negate')); } /** Loading core/tests/Drupal/Tests/Core/Condition/ConditionPluginBaseTest.php +71 Changes for core/tests/Drupal/Tests/Core/Condition/ConditionPluginBaseTest.php: 71 added lines, 0 removed lines. Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\Tests\Core\Condition; use Drupal\Core\Condition\ConditionPluginBase; use Drupal\Core\Form\FormState; use Drupal\Tests\UnitTestCase; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; /** * Tests Drupal\Core\Condition\ConditionPluginBase. */ #[CoversClass(ConditionPluginBase::class)] #[Group('Condition')] class ConditionPluginBaseTest extends UnitTestCase { /** * Tests that the negate form value is cast to a boolean on validation. */ #[DataProvider('providerTestValidateConfigurationForm')] public function testValidateConfigurationForm(mixed $value, bool $expected): void { $condition = new TestConditionPluginBase([], '', ''); $form = []; $form_state = new FormState(); $form_state->setValue('negate', $value); $condition->validateConfigurationForm($form, $form_state); $this->assertSame($expected, $form_state->getValue('negate')); } /** * Data provider for testValidateConfigurationForm(). */ public static function providerTestValidateConfigurationForm(): \Iterator { yield 'unchecked checkbox' => [0, FALSE]; yield 'checked checkbox' => [1, TRUE]; yield 'unchecked checkbox as string' => ['0', FALSE]; yield 'checked checkbox as string' => ['1', TRUE]; yield 'empty string' => ['', FALSE]; yield 'boolean false' => [FALSE, FALSE]; yield 'boolean true' => [TRUE, TRUE]; } } /** * Concrete condition plugin for testing. */ class TestConditionPluginBase extends ConditionPluginBase { /** * {@inheritdoc} */ public function evaluate(): bool { return TRUE; } /** * {@inheritdoc} */ public function summary(): string { return ''; } } Loading
core/lib/Drupal/Core/Condition/ConditionPluginBase.php +4 Changes for core/lib/Drupal/Core/Condition/ConditionPluginBase.php: 4 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -66,6 +66,10 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta * {@inheritdoc} */ public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { // In the config schema, 'negate' is a boolean. However, some form elements // such as checkbox return the value as 0/1. Cast here to ensure the data is // in the expected type. $form_state->setValue('negate', (bool) $form_state->getValue('negate')); } /** Loading
core/tests/Drupal/Tests/Core/Condition/ConditionPluginBaseTest.php +71 Changes for core/tests/Drupal/Tests/Core/Condition/ConditionPluginBaseTest.php: 71 added lines, 0 removed lines. Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\Tests\Core\Condition; use Drupal\Core\Condition\ConditionPluginBase; use Drupal\Core\Form\FormState; use Drupal\Tests\UnitTestCase; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; /** * Tests Drupal\Core\Condition\ConditionPluginBase. */ #[CoversClass(ConditionPluginBase::class)] #[Group('Condition')] class ConditionPluginBaseTest extends UnitTestCase { /** * Tests that the negate form value is cast to a boolean on validation. */ #[DataProvider('providerTestValidateConfigurationForm')] public function testValidateConfigurationForm(mixed $value, bool $expected): void { $condition = new TestConditionPluginBase([], '', ''); $form = []; $form_state = new FormState(); $form_state->setValue('negate', $value); $condition->validateConfigurationForm($form, $form_state); $this->assertSame($expected, $form_state->getValue('negate')); } /** * Data provider for testValidateConfigurationForm(). */ public static function providerTestValidateConfigurationForm(): \Iterator { yield 'unchecked checkbox' => [0, FALSE]; yield 'checked checkbox' => [1, TRUE]; yield 'unchecked checkbox as string' => ['0', FALSE]; yield 'checked checkbox as string' => ['1', TRUE]; yield 'empty string' => ['', FALSE]; yield 'boolean false' => [FALSE, FALSE]; yield 'boolean true' => [TRUE, TRUE]; } } /** * Concrete condition plugin for testing. */ class TestConditionPluginBase extends ConditionPluginBase { /** * {@inheritdoc} */ public function evaluate(): bool { return TRUE; } /** * {@inheritdoc} */ public function summary(): string { return ''; } }