function CssSelectorConstraintValidatorTest::testValidation

Same name and namespace in other branches
  1. main core/tests/Drupal/KernelTests/Core/Theme/DesignToken/CssSelectorConstraintValidatorTest.php \Drupal\KernelTests\Core\Theme\DesignToken\CssSelectorConstraintValidatorTest::testValidation()

Tests validation of CSS selector.

File

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

Class

CssSelectorConstraintValidatorTest
Tests Css Selector Constraint Validator.

Namespace

Drupal\KernelTests\Core\Theme\DesignToken

Code

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());
    }
  }
}

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