protected function EntityApiTest::assertCRUD in Drupal 10
Same name and namespace in other branches
- 8 core/tests/Drupal/KernelTests/Core/Entity/EntityApiTest.php \Drupal\KernelTests\Core\Entity\EntityApiTest::assertCRUD()
- 9 core/tests/Drupal/KernelTests/Core/Entity/EntityApiTest.php \Drupal\KernelTests\Core\Entity\EntityApiTest::assertCRUD()
Executes a test set for a defined entity type and user.
@internal
Parameters
string $entity_type: The entity type to run the tests with.
\Drupal\user\UserInterface $user1: The user to run the tests with.
File
- core/
tests/ Drupal/ KernelTests/ Core/ Entity/ EntityApiTest.php, line 52
Class
- EntityApiTest
- Tests basic CRUD functionality.
Namespace
Drupal\KernelTests\Core\EntityCode
protected function assertCRUD(string $entity_type, UserInterface $user1) : void {
// Create some test entities.
$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();
/** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
$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,
]));
// Test loading a single entity.
$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,
]));
// Test deleting an entity.
$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,
]));
// Test updating an entity.
$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,
]));
// Try deleting multiple test entities by deleting all.
$entities = $storage
->loadMultiple();
$storage
->delete($entities);
$all = $storage
->loadMultiple();
$this
->assertEmpty($all, "All entities of type '{$entity_type}' should have been deleted.");
// Verify that all data got 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');
}
// Test deleting a list of entities not indexed by entity id.
$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);
// Verify that entities got deleted.
$all = $storage
->loadMultiple();
$this
->assertEmpty($all, "All entities of type '{$entity_type}' should have been deleted.");
// Verify that all data got deleted from the tables.
$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');
}
}