Unverified Commit f17f0cea authored by Alex Pott's avatar Alex Pott
Browse files

task: #3525392 Add constraint for ResponseStatus Condition status code so it...

task: #3525392 Add constraint for ResponseStatus Condition status code so it can be FullyValidatable

By: penyaskito
By: irafah
(cherry picked from commit 65c72ba0)
parent 46780e03
Loading
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -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
+44 −0
Original line number Diff line number Diff line
@@ -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;
@@ -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);
  }

}