EntityUUIDTest.php in Drupal 8
File
core/tests/Drupal/KernelTests/Core/Entity/EntityUUIDTest.php
View source
<?php
namespace Drupal\KernelTests\Core\Entity;
class EntityUUIDTest extends EntityKernelTestBase {
protected function setUp() {
parent::setUp();
foreach (entity_test_entity_types() as $entity_type_id) {
if ($entity_type_id != 'entity_test') {
$this
->installEntitySchema($entity_type_id);
}
}
}
public function testCRUD() {
foreach (entity_test_entity_types() as $entity_type) {
$this
->assertCRUD($entity_type);
}
}
protected function assertCRUD($entity_type) {
$uuid_service = $this->container
->get('uuid');
$uuid = $uuid_service
->generate();
$custom_entity = $this->container
->get('entity_type.manager')
->getStorage($entity_type)
->create([
'name' => $this
->randomMachineName(),
'uuid' => $uuid,
]);
$this
->assertIdentical($custom_entity
->uuid(), $uuid);
$custom_entity
->save();
$entity = $this->container
->get('entity_type.manager')
->getStorage($entity_type)
->create([
'name' => $this
->randomMachineName(),
]);
$uuid = $entity
->uuid();
$this
->assertNotEmpty($uuid);
$this
->assertNotEqual($custom_entity
->uuid(), $uuid);
$entity
->save();
$this
->assertIdentical($entity
->uuid(), $uuid);
$storage = $this->container
->get('entity_type.manager')
->getStorage($entity_type);
$storage
->resetCache([
$entity
->id(),
]);
$entity_loaded = $storage
->load($entity
->id());
$this
->assertIdentical($entity_loaded
->uuid(), $uuid);
$entity_loaded_by_uuid = \Drupal::service('entity.repository')
->loadEntityByUuid($entity_type, $uuid, TRUE);
$this
->assertIdentical($entity_loaded_by_uuid
->uuid(), $uuid);
$this
->assertEqual($entity_loaded_by_uuid
->id(), $entity_loaded
->id());
$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());
}
}