Newer
Older
Dries Buytaert
committed
<?php
/**
* @file
* Contains \Drupal\system\Tests\Entity\EntityUUIDTest.
Dries Buytaert
committed
*/
namespace Drupal\system\Tests\Entity;
Dries Buytaert
committed
/**
* Tests creation, saving, and loading of entity UUIDs.
*
* @group Entity
Dries Buytaert
committed
*/
class EntityUUIDTest extends EntityUnitTestBase {
Alex Pott
committed
protected function setUp() {
Angie Byron
committed
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);
}
}
Angie Byron
committed
}
Dries Buytaert
committed
/**
* 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) {
Dries Buytaert
committed
// Verify that no UUID is auto-generated when passing one for creation.
$uuid_service = $this->container->get('uuid');
Dries Buytaert
committed
$uuid = $uuid_service->generate();
$custom_entity = $this->container->get('entity_type.manager')
->getStorage($entity_type)
->create(array(
'name' => $this->randomMachineName(),
'uuid' => $uuid,
));
$this->assertIdentical($custom_entity->uuid(), $uuid);
Dries Buytaert
committed
// 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 = $this->container->get('entity_type.manager')
->getStorage($entity_type)
->create(array('name' => $this->randomMachineName()));
$uuid = $entity->uuid();
Dries Buytaert
committed
$this->assertTrue($uuid);
// Verify that the new UUID is different.
$this->assertNotEqual($custom_entity->uuid(), $uuid);
Dries Buytaert
committed
// Verify that the UUID is retained upon saving.
$entity->save();
$this->assertIdentical($entity->uuid(), $uuid);
Dries Buytaert
committed
// Verify that the UUID is retained upon loading.
$entity_loaded = entity_load($entity_type, $entity->id(), TRUE);
$this->assertIdentical($entity_loaded->uuid(), $uuid);
Dries Buytaert
committed
// Verify that \Drupal::entityManager()->loadEntityByUuid() loads the same entity.
$entity_loaded_by_uuid = \Drupal::entityManager()->loadEntityByUuid($entity_type, $uuid, TRUE);
$this->assertIdentical($entity_loaded_by_uuid->uuid(), $uuid);
Dries Buytaert
committed
$this->assertEqual($entity_loaded_by_uuid->id(), $entity_loaded->id());
catch
committed
// Creating a duplicate needs to result in a new UUID.
$entity_duplicate = $entity->createDuplicate();
Angie Byron
committed
foreach ($entity->getFields() as $property => $value) {
Angie Byron
committed
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;
Angie Byron
committed
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;
Angie Byron
committed
default:
Dries Buytaert
committed
$this->assertEqual($entity_duplicate->{$property}->getValue(), $entity->{$property}->getValue());
Angie Byron
committed
}
}
catch
committed
$entity_duplicate->save();
$this->assertNotEqual($entity->id(), $entity_duplicate->id());