function EntityValidationTest::testValidation

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php \Drupal\KernelTests\Core\Entity\EntityValidationTest::testValidation()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php \Drupal\KernelTests\Core\Entity\EntityValidationTest::testValidation()
  3. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php \Drupal\KernelTests\Core\Entity\EntityValidationTest::testValidation()
  4. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php \Drupal\KernelTests\Core\Entity\EntityValidationTest::testValidation()

Tests validating test entity types.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php, line 115

Class

EntityValidationTest
Tests the Entity Validation API.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testValidation() : void {
  // Ensure that the constraint manager is marked as cached cleared.
  // Use the protected property on the cache_clearer first to check whether
  // the constraint manager is added there.
  $plugin_cache_clearer = \Drupal::service('plugin.cache_clearer');
  $get_cached_discoveries = function () {
    return $this->cachedDiscoveries;
  };
  $get_cached_discoveries = $get_cached_discoveries->bindTo($plugin_cache_clearer, $plugin_cache_clearer);
  $cached_discoveries = $get_cached_discoveries();
  $cached_discovery_classes = [];
  foreach ($cached_discoveries as $cached_discovery) {
    $cached_discovery_classes[] = get_class($cached_discovery);
  }
  $this->assertContains('Drupal\\Core\\Validation\\ConstraintManager', $cached_discovery_classes);
  // Add an extra 'changed' field to all entity types. Use a unique name to
  // avoid conflicting with the 'changed_test' field used by some test entity
  // types.
  foreach (EntityTestHelper::getEntityTypes() as $entity_type) {
    \Drupal::state()->set("{$entity_type}.additional_base_field_definitions", [
      'changed_no_test' => BaseFieldDefinition::create('changed')->setLabel(new TranslatableMarkup('Changed'))
        ->setDescription(new TranslatableMarkup('The time that the entity was last edited.'))
        ->setTranslatable(TRUE),
    ]);
  }
  $entity_field_manager = \Drupal::service('entity_field.manager');
  assert($entity_field_manager instanceof EntityFieldManagerInterface);
  $entity_field_manager->clearCachedFieldDefinitions();
  // All entity variations have to have the same results.
  foreach (EntityTestHelper::getEntityTypes() as $entity_type) {
    $this->checkValidation($entity_type);
  }
}

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