Issue #1720784 by Berdir: Add method for getting an entity's UUID.

8.0.x
catch 2012-08-22 15:02:48 +02:00
parent 2c0893e845
commit 3b308be763
3 changed files with 26 additions and 8 deletions

View File

@ -65,6 +65,13 @@ class Entity implements EntityInterface {
return isset($this->id) ? $this->id : NULL;
}
/**
* Implements EntityInterface::uuid().
*/
public function uuid() {
return isset($this->uuid) ? $this->uuid : NULL;
}
/**
* Implements EntityInterface::isNew().
*/

View File

@ -32,6 +32,17 @@ interface EntityInterface {
*/
public function id();
/**
* Returns the entity UUID (Universally Unique Identifier).
*
* The UUID is guaranteed to be unique and can be used to identify an entity
* across multiple systems.
*
* @return string
* The UUID of the entity, or NULL if the entity does not have one.
*/
public function uuid();
/**
* Returns whether the entity is new.
*

View File

@ -41,35 +41,35 @@ class EntityUUIDTest extends WebTestBase {
'name' => $this->randomName(),
'uuid' => $uuid,
));
$this->assertIdentical($custom_entity->get('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_test', array('name' => $this->randomName()));
$uuid = $entity->get('uuid');
$uuid = $entity->uuid();
$this->assertTrue($uuid);
// Verify that the new UUID is different.
$this->assertNotEqual($custom_entity->get('uuid'), $uuid);
$this->assertNotEqual($custom_entity->uuid(), $uuid);
// Verify that the UUID is retained upon saving.
$entity->save();
$this->assertIdentical($entity->get('uuid'), $uuid);
$this->assertIdentical($entity->uuid(), $uuid);
// Verify that the UUID is retained upon loading.
$entity_loaded = entity_test_load($entity->id(), TRUE);
$this->assertIdentical($entity_loaded->get('uuid'), $uuid);
$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_test', $uuid, TRUE);
$this->assertIdentical($entity_loaded_by_uuid->get('uuid'), $uuid);
$this->assertIdentical($entity_loaded_by_uuid->uuid(), $uuid);
$this->assertEqual($entity_loaded_by_uuid, $entity_loaded);
// Creating a duplicate needs to result in a new UUID.
$entity_duplicate = $entity->createDuplicate();
$this->assertNotEqual($entity_duplicate->get('uuid'), $entity->get('uuid'));
$this->assertNotNull($entity_duplicate->get('uuid'));
$this->assertNotEqual($entity_duplicate->uuid(), $entity->uuid());
$this->assertNotNull($entity_duplicate->uuid());
$entity_duplicate->save();
$this->assertNotEqual($entity->id(), $entity_duplicate->id());
}