function ExtensionAvailableConstraintValidatorTest::testValidationProfile

Tests the ExtensionAvailable constraint validator.

File

core/tests/Drupal/KernelTests/Core/Extension/ExtensionAvailableConstraintValidatorTest.php, line 33

Class

ExtensionAvailableConstraintValidatorTest
Tests the ExtensionAvailable constraint validator.

Namespace

Drupal\KernelTests\Core\Extension

Code

public function testValidationProfile() : void {
  // Create a data definition that specifies the value must be a string with
  // the name of an available module.
  $definition = DataDefinition::create('string');
  /** @var \Drupal\Core\TypedData\TypedDataManagerInterface $typed_data */
  $typed_data = $this->container
    ->get('typed_data_manager');
  $definition->setConstraints([
    'ExtensionAvailable' => 'profile',
  ]);
  $data = $typed_data->create($definition, 'minimal');
  // Assuming 'minimal' profile is available.
  $violations = $data->validate();
  $this->assertCount(0, $violations);
  // Check an unavailable profile by setting a fake profile name.
  $data->setValue('fake_profile');
  $violations = $data->validate();
  $this->assertCount(1, $violations);
  $this->assertSame("Profile 'fake_profile' is not available.", (string) $violations->get(0)
    ->getMessage());
  // NULL should not trigger a validation error: a value may be nullable.
  $data->setValue(NULL);
  $validate = $data->validate();
  $this->assertCount(0, $validate);
}

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