3 namespace Drupal\KernelTests\Core\Entity;
5 use Drupal\Core\Entity\FieldableEntityInterface;
6 use Drupal\KernelTests\KernelTestBase;
7 use Drupal\node\Entity\NodeType;
8 use Drupal\taxonomy\Entity\Vocabulary;
11 * Tests the ContentEntityStorageBase::createWithSampleValues method.
13 * @coversDefaultClass \Drupal\Core\Entity\ContentEntityStorageBase
16 class CreateSampleEntityTest extends KernelTestBase {
19 * The entity type manager.
21 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
23 protected $entityTypeManager;
28 public static $modules = ['system', 'field', 'filter', 'text', 'file', 'user', 'node', 'comment', 'taxonomy'];
33 protected function setUp() {
36 $this->installEntitySchema('file');
37 $this->installEntitySchema('user');
38 $this->installEntitySchema('node');
39 $this->installEntitySchema('node_type');
40 $this->installEntitySchema('file');
41 $this->installEntitySchema('comment');
42 $this->installEntitySchema('comment_type');
43 $this->installEntitySchema('taxonomy_vocabulary');
44 $this->installEntitySchema('taxonomy_term');
45 $this->entityTypeManager = $this->container->get('entity_type.manager');
46 NodeType::create(['type' => 'article', 'name' => 'Article'])->save();
47 NodeType::create(['type' => 'page', 'name' => 'Page'])->save();
48 Vocabulary::create(['name' => 'Tags', 'vid' => 'tags'])->save();
52 * Tests sample value content entity creation of all types.
54 * @covers ::createWithSampleValues
56 public function testSampleValueContentEntity() {
57 foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $definition) {
58 if ($definition->entityClassImplements(FieldableEntityInterface::class)) {
59 $label = $definition->getKey('label');
62 $title = $this->randomString();
63 $values[$label] = $title;
65 // Create sample entities with bundles.
66 if ($bundle_type = $definition->getBundleEntityType()) {
67 foreach ($this->entityTypeManager->getStorage($bundle_type)->loadMultiple() as $bundle) {
68 $entity = $this->entityTypeManager->getStorage($entity_type_id)->createWithSampleValues($bundle->id(), $values);
69 $violations = $entity->validate();
70 $this->assertCount(0, $violations);
72 $this->assertEquals($title, $entity->label());
76 // Create sample entities without bundles.
78 $entity = $this->entityTypeManager->getStorage($entity_type_id)->createWithSampleValues(FALSE, $values);
79 $violations = $entity->validate();
80 $this->assertCount(0, $violations);
82 $this->assertEquals($title, $entity->label());