EntityUUIDTest.php 3.46 KB
Newer Older
1 2 3 4
<?php

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

8
namespace Drupal\system\Tests\Entity;
9 10 11

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

17
  protected function setUp() {
18 19
    parent::setUp();

20 21 22
    $this->installEntitySchema('entity_test_rev');
    $this->installEntitySchema('entity_test_mul');
    $this->installEntitySchema('entity_test_mulrev');
23 24
  }

25 26 27 28
  /**
   * Tests UUID generation in entity CRUD operations.
   */
  function testCRUD() {
29 30 31 32 33 34 35 36 37 38 39 40 41
    // 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) {
42
    // Verify that no UUID is auto-generated when passing one for creation.
43
    $uuid_service = $this->container->get('uuid');
44
    $uuid = $uuid_service->generate();
45
    $custom_entity = entity_create($entity_type, array(
46
      'name' => $this->randomMachineName(),
47 48
      'uuid' => $uuid,
    ));
49
    $this->assertIdentical($custom_entity->uuid(), $uuid);
50 51 52 53
    // Save this entity, so we have more than one later.
    $custom_entity->save();

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

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

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

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

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

    // Creating a duplicate needs to result in a new UUID.
    $entity_duplicate = $entity->createDuplicate();
76
    foreach ($entity->getFields() as $property => $value) {
77 78 79 80 81 82 83 84 85 86 87
      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;
88 89 90 91 92 93
        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;
94
        default:
95
          $this->assertEqual($entity_duplicate->{$property}->getValue(), $entity->{$property}->getValue());
96 97
      }
    }
98 99
    $entity_duplicate->save();
    $this->assertNotEqual($entity->id(), $entity_duplicate->id());
100 101
  }
}