Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Entity / EntityUUIDTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Entity;
4
5 /**
6  * Tests creation, saving, and loading of entity UUIDs.
7  *
8  * @group Entity
9  */
10 class EntityUUIDTest extends EntityKernelTestBase {
11
12   protected function setUp() {
13     parent::setUp();
14
15     foreach (entity_test_entity_types() as $entity_type_id) {
16       // The entity_test schema is installed by the parent.
17       if ($entity_type_id != 'entity_test') {
18         $this->installEntitySchema($entity_type_id);
19       }
20     }
21   }
22
23   /**
24    * Tests UUID generation in entity CRUD operations.
25    */
26   public function testCRUD() {
27     // All entity variations have to have the same results.
28     foreach (entity_test_entity_types() as $entity_type) {
29       $this->assertCRUD($entity_type);
30     }
31   }
32
33   /**
34    * Executes the UUID CRUD tests for the given entity type.
35    *
36    * @param string $entity_type
37    *   The entity type to run the tests with.
38    */
39   protected function assertCRUD($entity_type) {
40     // Verify that no UUID is auto-generated when passing one for creation.
41     $uuid_service = $this->container->get('uuid');
42     $uuid = $uuid_service->generate();
43     $custom_entity = $this->container->get('entity_type.manager')
44       ->getStorage($entity_type)
45       ->create([
46         'name' => $this->randomMachineName(),
47         'uuid' => $uuid,
48       ]);
49     $this->assertIdentical($custom_entity->uuid(), $uuid);
50     // Save this entity, so we have more than one later.
51     $custom_entity->save();
52
53     // Verify that a new UUID is generated upon creating an entity.
54     $entity = $this->container->get('entity_type.manager')
55       ->getStorage($entity_type)
56       ->create(['name' => $this->randomMachineName()]);
57     $uuid = $entity->uuid();
58     $this->assertTrue($uuid);
59
60     // Verify that the new UUID is different.
61     $this->assertNotEqual($custom_entity->uuid(), $uuid);
62
63     // Verify that the UUID is retained upon saving.
64     $entity->save();
65     $this->assertIdentical($entity->uuid(), $uuid);
66
67     // Verify that the UUID is retained upon loading.
68     /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
69     $storage = $this->container->get('entity_type.manager')
70       ->getStorage($entity_type);
71     $storage->resetCache([$entity->id()]);
72     $entity_loaded = $storage->load($entity->id());
73     $this->assertIdentical($entity_loaded->uuid(), $uuid);
74
75     // Verify that \Drupal::entityManager()->loadEntityByUuid() loads the same entity.
76     $entity_loaded_by_uuid = \Drupal::entityManager()->loadEntityByUuid($entity_type, $uuid, TRUE);
77     $this->assertIdentical($entity_loaded_by_uuid->uuid(), $uuid);
78     $this->assertEqual($entity_loaded_by_uuid->id(), $entity_loaded->id());
79
80     // Creating a duplicate needs to result in a new UUID.
81     $entity_duplicate = $entity->createDuplicate();
82     foreach ($entity->getFields() as $property => $value) {
83       switch ($property) {
84         case 'uuid':
85           $this->assertNotNull($entity_duplicate->uuid());
86           $this->assertNotNull($entity->uuid());
87           $this->assertNotEqual($entity_duplicate->uuid(), $entity->uuid());
88           break;
89         case 'id':
90           $this->assertNull($entity_duplicate->id());
91           $this->assertNotNull($entity->id());
92           $this->assertNotEqual($entity_duplicate->id(), $entity->id());
93           break;
94         case 'revision_id':
95           $this->assertNull($entity_duplicate->getRevisionId());
96           $this->assertNotNull($entity->getRevisionId());
97           $this->assertNotEqual($entity_duplicate->getRevisionId(), $entity->getRevisionId());
98           $this->assertNotEqual($entity_duplicate->{$property}->getValue(), $entity->{$property}->getValue());
99           break;
100         default:
101           $this->assertEqual($entity_duplicate->{$property}->getValue(), $entity->{$property}->getValue());
102       }
103     }
104     $entity_duplicate->save();
105     $this->assertNotEqual($entity->id(), $entity_duplicate->id());
106   }
107
108 }