View source
<?php
namespace Drupal\KernelTests\Core\Entity;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Database\Database;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\user\UserInterface;
class EntityApiTest extends EntityKernelTestBase {
protected function setUp() : void {
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, $this
->createUser());
}
}
protected function assertCRUD(string $entity_type, UserInterface $user1) : void {
$entity = $this->container
->get('entity_type.manager')
->getStorage($entity_type)
->create([
'name' => 'test',
'user_id' => $user1
->id(),
]);
$entity
->save();
$entity = $this->container
->get('entity_type.manager')
->getStorage($entity_type)
->create([
'name' => 'test2',
'user_id' => $user1
->id(),
]);
$entity
->save();
$entity = $this->container
->get('entity_type.manager')
->getStorage($entity_type)
->create([
'name' => 'test',
'user_id' => NULL,
]);
$entity
->save();
$storage = $this->container
->get('entity_type.manager')
->getStorage($entity_type);
$entities = array_values($storage
->loadByProperties([
'name' => 'test',
]));
$this
->assertEquals('test', $entities[0]->name->value, new FormattableMarkup('%entity_type: Created and loaded entity', [
'%entity_type' => $entity_type,
]));
$this
->assertEquals('test', $entities[1]->name->value, new FormattableMarkup('%entity_type: Created and loaded entity', [
'%entity_type' => $entity_type,
]));
$loaded_entity = $storage
->load($entity
->id());
$this
->assertEquals($entity
->id(), $loaded_entity
->id(), new FormattableMarkup('%entity_type: Loaded a single entity by id.', [
'%entity_type' => $entity_type,
]));
$entities = array_values($storage
->loadByProperties([
'name' => 'test2',
]));
$entities[0]
->delete();
$entities = array_values($storage
->loadByProperties([
'name' => 'test2',
]));
$this
->assertEquals([], $entities, new FormattableMarkup('%entity_type: Entity deleted.', [
'%entity_type' => $entity_type,
]));
$entities = array_values($storage
->loadByProperties([
'name' => 'test',
]));
$entities[0]->name->value = 'test3';
$entities[0]
->save();
$entity = $storage
->load($entities[0]
->id());
$this
->assertEquals('test3', $entity->name->value, new FormattableMarkup('%entity_type: Entity updated.', [
'%entity_type' => $entity_type,
]));
$entities = $storage
->loadMultiple();
$storage
->delete($entities);
$all = $storage
->loadMultiple();
$this
->assertEmpty($all, "All entities of type '{$entity_type}' should have been deleted.");
$definition = \Drupal::entityTypeManager()
->getDefinition($entity_type);
$connection = Database::getConnection();
$this
->assertEquals(0, (int) $connection
->select($definition
->getBaseTable())
->countQuery()
->execute()
->fetchField(), 'Base table was emptied');
if ($data_table = $definition
->getDataTable()) {
$this
->assertEquals(0, (int) $connection
->select($data_table)
->countQuery()
->execute()
->fetchField(), 'Data table was emptied');
}
if ($revision_table = $definition
->getRevisionTable()) {
$this
->assertEquals(0, (int) $connection
->select($revision_table)
->countQuery()
->execute()
->fetchField(), 'Revision table was emptied');
}
if ($revision_data_table = $definition
->getRevisionDataTable()) {
$this
->assertEquals(0, (int) $connection
->select($revision_data_table)
->countQuery()
->execute()
->fetchField(), 'Revision data table was emptied');
}
$entities = [];
$entity = $storage
->create([
'name' => 'test',
'user_id' => $user1
->id(),
]);
$entity
->save();
$entities['test'] = $entity;
$entity = $storage
->create([
'name' => 'test2',
'user_id' => $user1
->id(),
]);
$entity
->save();
$entities['test2'] = $entity;
$controller = \Drupal::entityTypeManager()
->getStorage($entity_type);
$controller
->delete($entities);
$all = $storage
->loadMultiple();
$this
->assertEmpty($all, "All entities of type '{$entity_type}' should have been deleted.");
$definition = \Drupal::entityTypeManager()
->getDefinition($entity_type);
$this
->assertEquals(0, (int) $connection
->select($definition
->getBaseTable())
->countQuery()
->execute()
->fetchField(), 'Base table was emptied');
if ($data_table = $definition
->getDataTable()) {
$this
->assertEquals(0, (int) $connection
->select($data_table)
->countQuery()
->execute()
->fetchField(), 'Data table was emptied');
}
if ($revision_table = $definition
->getRevisionTable()) {
$this
->assertEquals(0, (int) $connection
->select($revision_table)
->countQuery()
->execute()
->fetchField(), 'Revision table was emptied');
}
if ($revision_data_table = $definition
->getRevisionDataTable()) {
$this
->assertEquals(0, (int) $connection
->select($revision_data_table)
->countQuery()
->execute()
->fetchField(), 'Revision data table was emptied');
}
}
public function testLoadMultiple() {
$storage = $this->container
->get('entity_type.manager')
->getStorage('entity_test');
$ids = [];
$entity = $storage
->create([
'name' => 'test',
]);
$entity
->save();
$ids[] = $entity
->id();
$entity = $storage
->create([
'name' => 'test2',
]);
$entity
->save();
$ids[] = $entity
->id();
$entities = $storage
->loadMultiple($ids);
$this
->assertEquals($ids, array_keys($entities));
$ids = array_reverse($ids);
$entities = $storage
->loadMultiple($ids);
$this
->assertEquals($ids, array_keys($entities));
$storage
->resetCache();
$ids = array_reverse($ids);
$entities = $storage
->loadMultiple($ids);
$this
->assertEquals($ids, array_keys($entities));
$storage = $this->container
->get('entity_type.manager')
->getStorage('entity_test_rev');
$ids = [];
$entity = $storage
->create([
'name' => 'test_rev',
]);
$entity
->save();
$ids[] = $entity
->getRevisionId();
$revision = $storage
->createRevision($entity, TRUE);
$revision
->save();
$ids[] = $revision
->getRevisionId();
$entities = $storage
->loadMultipleRevisions($ids);
$this
->assertEquals($ids, array_keys($entities));
$ids = array_reverse($ids);
$entities = $storage
->loadMultipleRevisions($ids);
$this
->assertEquals($ids, array_keys($entities));
$ids = array_reverse($ids);
$storage
->resetCache();
$entities = $storage
->loadMultipleRevisions($ids);
$this
->assertEquals($ids, array_keys($entities));
}
public function testEntityStorageExceptionHandling() {
$entity = EntityTest::create([
'name' => 'test',
]);
try {
$GLOBALS['entity_test_throw_exception'] = TRUE;
$entity
->save();
$this
->fail('Entity presave EntityStorageException thrown but not caught.');
} catch (EntityStorageException $e) {
$this
->assertEquals(1, $e
->getcode(), 'Entity presave EntityStorageException caught.');
}
$entity = EntityTest::create([
'name' => 'test2',
]);
try {
unset($GLOBALS['entity_test_throw_exception']);
$entity
->save();
} catch (EntityStorageException $e) {
$this
->assertNotEquals(1, $e
->getCode(), 'Entity presave EntityStorageException caught.');
}
$entity = EntityTest::create([
'name' => 'test3',
]);
$entity
->save();
try {
$GLOBALS['entity_test_throw_exception'] = TRUE;
$entity
->delete();
$this
->fail('Entity predelete EntityStorageException not thrown.');
} catch (EntityStorageException $e) {
$this
->assertEquals(2, $e
->getCode(), 'Entity predelete EntityStorageException caught.');
}
unset($GLOBALS['entity_test_throw_exception']);
$entity = EntityTest::create([
'name' => 'test4',
]);
$entity
->save();
try {
$entity
->delete();
} catch (EntityStorageException $e) {
$this
->assertNotEquals(2, $e
->getCode(), 'Entity predelete EntityStorageException thrown.');
}
}
public function testUpdateWithRevisionId() {
$storage = \Drupal::entityTypeManager()
->getStorage('entity_test_mulrev');
$entity = $storage
->create([
'name' => 'revision_test',
]);
$entity
->save();
$this
->expectException(EntityStorageException::class);
$this
->expectExceptionMessage("Update existing 'entity_test_mulrev' entity revision while changing the revision ID is not supported.");
$entity->revision_id = 60;
$entity
->save();
}
public function testUpdateWithId() {
$storage = \Drupal::entityTypeManager()
->getStorage('entity_test_mulrev');
$entity = $storage
->create([
'name' => 'revision_test',
]);
$entity
->save();
$this
->expectException(EntityStorageException::class);
$this
->expectExceptionMessage("Update existing 'entity_test_mulrev' entity while changing the ID is not supported.");
$entity->id = 60;
$entity
->save();
}
}