summaryrefslogtreecommitdiffstats
path: root/core/modules/system/src/Tests/Entity/EntityUUIDTest.php
blob: f2f8aff089434b97fb01497e2e924fe4b00a4824 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php

/**
 * @file
 * Definition of Drupal\system\Tests\Entity\EntityUUIDTest.
 */

namespace Drupal\system\Tests\Entity;

/**
 * Tests creation, saving, and loading of entity UUIDs.
 *
 * @group Entity
 */
class EntityUUIDTest extends EntityUnitTestBase {

  protected function setUp() {
    parent::setUp();

    foreach (entity_test_entity_types() as $entity_type_id) {
      // The entity_test schema is installed by the parent.
      if ($entity_type_id != 'entity_test') {
        $this->installEntitySchema($entity_type_id);
      }
    }
  }

  /**
   * Tests UUID generation in entity CRUD operations.
   */
  function testCRUD() {
    // All entity variations have to have the same results.
    foreach (entity_test_entity_types() as $entity_type) {
      $this->assertCRUD($entity_type);
    }
  }

  /**
   * Executes the UUID CRUD tests for the given entity type.
   *
   * @param string $entity_type
   *   The entity type to run the tests with.
   */
  protected function assertCRUD($entity_type) {
    // Verify that no UUID is auto-generated when passing one for creation.
    $uuid_service = $this->container->get('uuid');
    $uuid = $uuid_service->generate();
    $custom_entity = entity_create($entity_type, array(
      'name' => $this->randomMachineName(),
      'uuid' => $uuid,
    ));
    $this->assertIdentical($custom_entity->uuid(), $uuid);
    // Save this entity, so we have more than one later.
    $custom_entity->save();

    // Verify that a new UUID is generated upon creating an entity.
    $entity = entity_create($entity_type, array('name' => $this->randomMachineName()));
    $uuid = $entity->uuid();
    $this->assertTrue($uuid);

    // Verify that the new UUID is different.
    $this->assertNotEqual($custom_entity->uuid(), $uuid);

    // Verify that the UUID is retained upon saving.
    $entity->save();
    $this->assertIdentical($entity->uuid(), $uuid);

    // Verify that the UUID is retained upon loading.
    $entity_loaded = entity_load($entity_type, $entity->id(), TRUE);
    $this->assertIdentical($entity_loaded->uuid(), $uuid);

    // Verify that entity_load_by_uuid() loads the same entity.
    $entity_loaded_by_uuid = entity_load_by_uuid($entity_type, $uuid, TRUE);
    $this->assertIdentical($entity_loaded_by_uuid->uuid(), $uuid);
    $this->assertEqual($entity_loaded_by_uuid->id(), $entity_loaded->id());

    // Creating a duplicate needs to result in a new UUID.
    $entity_duplicate = $entity->createDuplicate();
    foreach ($entity->getFields() as $property => $value) {
      switch($property) {
        case 'uuid':
          $this->assertNotNull($entity_duplicate->uuid());
          $this->assertNotNull($entity->uuid());
          $this->assertNotEqual($entity_duplicate->uuid(), $entity->uuid());
          break;
        case 'id':
          $this->assertNull($entity_duplicate->id());
          $this->assertNotNull($entity->id());
          $this->assertNotEqual($entity_duplicate->id(), $entity->id());
          break;
        case 'revision_id':
          $this->assertNull($entity_duplicate->getRevisionId());
          $this->assertNotNull($entity->getRevisionId());
          $this->assertNotEqual($entity_duplicate->getRevisionId(), $entity->getRevisionId());
          $this->assertNotEqual($entity_duplicate->{$property}->getValue(), $entity->{$property}->getValue());
          break;
        default:
          $this->assertEqual($entity_duplicate->{$property}->getValue(), $entity->{$property}->getValue());
      }
    }
    $entity_duplicate->save();
    $this->assertNotEqual($entity->id(), $entity_duplicate->id());
  }
}