function CreateSampleEntityTest::testSampleValueContentEntity

Same name in other branches
  1. 9 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()
  3. 11.x 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 68

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

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