Issue #1720784 by Berdir: Add method for getting an entity's UUID.
parent
2c0893e845
commit
3b308be763
|
@ -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().
|
||||
*/
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue