class CssSelectorConstraintValidatorTest

Tests Css Selector Constraint Validator.

Attributes

#[Group('design_token')] #[Group('Validation')] #[CoversClass(CssSelectorConstraint::class)] #[CoversClass(CssSelectorConstraintValidator::class)] #[RunTestsInSeparateProcesses]

Hierarchy

Expanded class hierarchy of CssSelectorConstraintValidatorTest

File

core/tests/Drupal/KernelTests/Core/Theme/DesignToken/CssSelectorConstraintValidatorTest.php, line 20

Namespace

Drupal\KernelTests\Core\Theme\DesignToken
View source
class CssSelectorConstraintValidatorTest extends KernelTestBase {
  
  /**
   * Tests validation of CSS selector.
   */
  public function testValidation() : void {
    $definition = DataDefinition::create('string')->addConstraint('CssSelector');
    $data = $this->container
      ->get(TypedDataManagerInterface::class)
      ->create($definition);
    $cases = [
      'valid' => [
        'value' => '.root',
        'violations' => [],
      ],
      'valid_with_config_storage_character' => [
        'value' => '%root',
        'violations' => [],
      ],
      'invalid_{' => [
        'value' => '{root',
        'violations' => [
          'The CSS selector {root is invalid.',
        ],
      ],
      'invalid_}' => [
        'value' => '}root',
        'violations' => [
          'The CSS selector }root is invalid.',
        ],
      ],
      'invalid_;' => [
        'value' => '.root;',
        'violations' => [
          'The CSS selector .root; is invalid.',
        ],
      ],
    ];
    foreach ($cases as $case) {
      $data->setValue($case['value']);
      $violations = $data->validate();
      $this->assertCount(count($case['violations']), $violations);
      foreach ($case['violations'] as $violationKey => $expectedViolationMessage) {
        $this->assertSame($expectedViolationMessage, (string) $violations[$violationKey]->getMessage());
      }
    }
  }
  
  /**
   * Tests invalid type.
   */
  public function testValidatedValueMustBeAString() : void {
    $definition = DataDefinition::create('string')->addConstraint('CssSelector');
    $data = $this->container
      ->get(TypedDataManagerInterface::class)
      ->create($definition);
    $data->setValue(5);
    $this->expectException(UnexpectedValueException::class);
    $this->expectExceptionMessage('Expected argument of type "string", "int" given');
    $data->validate();
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.