Loading core/modules/system/config/schema/system.schema.yml +7 −0 Original line number Diff line number Diff line Loading @@ -490,11 +490,18 @@ condition.plugin.request_path: condition.plugin.response_status: type: condition.plugin constraints: FullyValidatable: ~ mapping: status_codes: type: sequence sequence: type: integer constraints: # @see \Drupal\system\Plugin\Condition\ResponseStatus::buildConfigurationForm() Choice: # HTTP 200 OK, HTTP 403 FORBIDDEN, HTTP 404 NOT FOUND. choices: [200, 403, 404] system.feature_flags: type: config_object Loading core/tests/Drupal/KernelTests/Core/Plugin/Condition/ResponseStatusTest.php +44 −0 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ namespace Drupal\KernelTests\Core\Plugin\Condition; use Drupal\Core\Condition\ConditionManager; use Drupal\Core\Config\TypedConfigManagerInterface; use Drupal\KernelTests\KernelTestBase; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; Loading Loading @@ -289,4 +290,47 @@ public static function providerTestConditions() { } /** * Provides test data for ::testStatusCodesValidation(). */ public static function providerStatusCodesValidation(): \Iterator { yield 'OK: empty status_codes' => [[], []]; yield 'OK: 200' => [[Response::HTTP_OK], []]; yield 'OK: all supported status codes' => [ [Response::HTTP_OK, Response::HTTP_FORBIDDEN, Response::HTTP_NOT_FOUND], [], ]; yield 'INVALID: 418' => [ [Response::HTTP_I_AM_A_TEAPOT], ['status_codes.0' => 'The value you selected is not a valid choice.'], ]; yield 'INVALID: 200 and 418' => [ [Response::HTTP_OK, Response::HTTP_I_AM_A_TEAPOT], ['status_codes.1' => 'The value you selected is not a valid choice.'], ]; } /** * Tests the schema constraints on the `status_codes` config. */ #[DataProvider('providerStatusCodesValidation')] public function testStatusCodesValidation(array $status_codes, array $expected_messages): void { $typed_config = $this->container->get(TypedConfigManagerInterface::class); $data = [ 'id' => 'response_status', 'negate' => FALSE, 'context_mapping' => [], 'status_codes' => $status_codes, ]; $definition = $typed_config->getDefinition('condition.plugin.response_status'); $data_definition = $typed_config->buildDataDefinition($definition, $data); $violations = $typed_config->create($data_definition, $data)->validate(); $actual_messages = []; foreach ($violations as $violation) { $actual_messages[$violation->getPropertyPath()] = (string) $violation->getMessage(); } $this->assertSame($expected_messages, $actual_messages); } } Loading
core/modules/system/config/schema/system.schema.yml +7 −0 Original line number Diff line number Diff line Loading @@ -490,11 +490,18 @@ condition.plugin.request_path: condition.plugin.response_status: type: condition.plugin constraints: FullyValidatable: ~ mapping: status_codes: type: sequence sequence: type: integer constraints: # @see \Drupal\system\Plugin\Condition\ResponseStatus::buildConfigurationForm() Choice: # HTTP 200 OK, HTTP 403 FORBIDDEN, HTTP 404 NOT FOUND. choices: [200, 403, 404] system.feature_flags: type: config_object Loading
core/tests/Drupal/KernelTests/Core/Plugin/Condition/ResponseStatusTest.php +44 −0 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ namespace Drupal\KernelTests\Core\Plugin\Condition; use Drupal\Core\Condition\ConditionManager; use Drupal\Core\Config\TypedConfigManagerInterface; use Drupal\KernelTests\KernelTestBase; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; Loading Loading @@ -289,4 +290,47 @@ public static function providerTestConditions() { } /** * Provides test data for ::testStatusCodesValidation(). */ public static function providerStatusCodesValidation(): \Iterator { yield 'OK: empty status_codes' => [[], []]; yield 'OK: 200' => [[Response::HTTP_OK], []]; yield 'OK: all supported status codes' => [ [Response::HTTP_OK, Response::HTTP_FORBIDDEN, Response::HTTP_NOT_FOUND], [], ]; yield 'INVALID: 418' => [ [Response::HTTP_I_AM_A_TEAPOT], ['status_codes.0' => 'The value you selected is not a valid choice.'], ]; yield 'INVALID: 200 and 418' => [ [Response::HTTP_OK, Response::HTTP_I_AM_A_TEAPOT], ['status_codes.1' => 'The value you selected is not a valid choice.'], ]; } /** * Tests the schema constraints on the `status_codes` config. */ #[DataProvider('providerStatusCodesValidation')] public function testStatusCodesValidation(array $status_codes, array $expected_messages): void { $typed_config = $this->container->get(TypedConfigManagerInterface::class); $data = [ 'id' => 'response_status', 'negate' => FALSE, 'context_mapping' => [], 'status_codes' => $status_codes, ]; $definition = $typed_config->getDefinition('condition.plugin.response_status'); $data_definition = $typed_config->buildDataDefinition($definition, $data); $violations = $typed_config->create($data_definition, $data)->validate(); $actual_messages = []; foreach ($violations as $violation) { $actual_messages[$violation->getPropertyPath()] = (string) $violation->getMessage(); } $this->assertSame($expected_messages, $actual_messages); } }