b5bcd487f2fdb340add1a564248cf0c076237797
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Entity / CreateSampleEntityTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Entity;
4
5 use Drupal\Core\Entity\FieldableEntityInterface;
6 use Drupal\KernelTests\KernelTestBase;
7 use Drupal\node\Entity\NodeType;
8 use Drupal\taxonomy\Entity\Vocabulary;
9
10 /**
11  * Tests the ContentEntityStorageBase::createWithSampleValues method.
12  *
13  * @coversDefaultClass \Drupal\Core\Entity\ContentEntityStorageBase
14  * @group Entity
15  */
16 class CreateSampleEntityTest extends KernelTestBase {
17
18   /**
19    * The entity type manager.
20    *
21    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
22    */
23   protected $entityTypeManager;
24
25   /**
26    * {@inheritdoc}
27    */
28   public static $modules = ['system', 'field', 'filter', 'text', 'file', 'user', 'node', 'comment', 'taxonomy'];
29
30   /**
31    * {@inheritdoc}
32    */
33   protected function setUp() {
34     parent::setup();
35
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();
49   }
50
51   /**
52    * Tests sample value content entity creation of all types.
53    *
54    * @covers ::createWithSampleValues
55    */
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');
60         $values = [];
61         if ($label) {
62           $title = $this->randomString();
63           $values[$label] = $title;
64         }
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);
71             if ($label) {
72               $this->assertEquals($title, $entity->label());
73             }
74           }
75         }
76         // Create sample entities without bundles.
77         else {
78           $entity = $this->entityTypeManager->getStorage($entity_type_id)->createWithSampleValues(FALSE, $values);
79           $violations = $entity->validate();
80           $this->assertCount(0, $violations);
81           if ($label) {
82             $this->assertEquals($title, $entity->label());
83           }
84         }
85       }
86     }
87   }
88
89 }