You are here

public function CreateSampleEntityTest::testSampleValueContentEntity in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Entity/CreateSampleEntityTest.php \Drupal\KernelTests\Core\Entity\CreateSampleEntityTest::testSampleValueContentEntity()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/CreateSampleEntityTest.php \Drupal\KernelTests\Core\Entity\CreateSampleEntityTest::testSampleValueContentEntity()

Tests sample value content entity creation of all types.

@covers ::createWithSampleValues

File

core/tests/Drupal/KernelTests/Core/Entity/CreateSampleEntityTest.php, line 67

Class

CreateSampleEntityTest
Tests the ContentEntityStorageBase::createWithSampleValues method.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testSampleValueContentEntity() {
  foreach ($this->entityTypeManager
    ->getDefinitions() as $entity_type_id => $definition) {
    if ($definition
      ->entityClassImplements(FieldableEntityInterface::class)) {
      $label = $definition
        ->getKey('label');
      $values = [];
      if ($label) {
        $title = $this
          ->randomString();
        $values[$label] = $title;
      }

      // Create sample entities with bundles.
      if ($bundle_type = $definition
        ->getBundleEntityType()) {
        foreach ($this->entityTypeManager
          ->getStorage($bundle_type)
          ->loadMultiple() as $bundle) {
          $entity = $this->entityTypeManager
            ->getStorage($entity_type_id)
            ->createWithSampleValues($bundle
            ->id(), $values);
          $violations = $entity
            ->validate();
          $this
            ->assertCount(0, $violations);
          if ($label) {
            $this
              ->assertEquals($title, $entity
              ->label());
          }
        }
      }
      else {
        $entity = $this->entityTypeManager
          ->getStorage($entity_type_id)
          ->createWithSampleValues(FALSE, $values);
        $violations = $entity
          ->validate();
        $this
          ->assertCount(0, $violations);
        if ($label) {
          $this
            ->assertEquals($title, $entity
            ->label());
        }
      }
    }
  }
}