protected function EntityApiTest::assertCRUD in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/system/src/Tests/Entity/EntityApiTest.php \Drupal\system\Tests\Entity\EntityApiTest::assertCRUD()
Executes a test set for a defined entity type and user.
Parameters
string $entity_type: The entity type to run the tests with.
\Drupal\user\UserInterface $user1: The user to run the tests with.
1 call to EntityApiTest::assertCRUD()
- EntityApiTest::testCRUD in core/
modules/ system/ src/ Tests/ Entity/ EntityApiTest.php - Tests basic CRUD functionality of the Entity API.
File
- core/
modules/ system/ src/ Tests/ Entity/ EntityApiTest.php, line 52 - Contains \Drupal\system\Tests\Entity\EntityApiTest.
Class
- EntityApiTest
- Tests basic CRUD functionality.
Namespace
Drupal\system\Tests\EntityCode
protected function assertCRUD($entity_type, UserInterface $user1) {
// Create some test entities.
$entity = entity_create($entity_type, array(
'name' => 'test',
'user_id' => $user1
->id(),
));
$entity
->save();
$entity = entity_create($entity_type, array(
'name' => 'test2',
'user_id' => $user1
->id(),
));
$entity
->save();
$entity = entity_create($entity_type, array(
'name' => 'test',
'user_id' => NULL,
));
$entity
->save();
$entities = array_values(entity_load_multiple_by_properties($entity_type, array(
'name' => 'test',
)));
$this
->assertEqual($entities[0]->name->value, 'test', format_string('%entity_type: Created and loaded entity', array(
'%entity_type' => $entity_type,
)));
$this
->assertEqual($entities[1]->name->value, 'test', format_string('%entity_type: Created and loaded entity', array(
'%entity_type' => $entity_type,
)));
// Test loading a single entity.
$loaded_entity = entity_load($entity_type, $entity
->id());
$this
->assertEqual($loaded_entity
->id(), $entity
->id(), format_string('%entity_type: Loaded a single entity by id.', array(
'%entity_type' => $entity_type,
)));
// Test deleting an entity.
$entities = array_values(entity_load_multiple_by_properties($entity_type, array(
'name' => 'test2',
)));
$entities[0]
->delete();
$entities = array_values(entity_load_multiple_by_properties($entity_type, array(
'name' => 'test2',
)));
$this
->assertEqual($entities, array(), format_string('%entity_type: Entity deleted.', array(
'%entity_type' => $entity_type,
)));
// Test updating an entity.
$entities = array_values(entity_load_multiple_by_properties($entity_type, array(
'name' => 'test',
)));
$entities[0]->name->value = 'test3';
$entities[0]
->save();
$entity = entity_load($entity_type, $entities[0]
->id());
$this
->assertEqual($entity->name->value, 'test3', format_string('%entity_type: Entity updated.', array(
'%entity_type' => $entity_type,
)));
// Try deleting multiple test entities by deleting all.
$ids = array_keys(entity_load_multiple($entity_type));
entity_delete_multiple($entity_type, $ids);
$all = entity_load_multiple($entity_type);
$this
->assertTrue(empty($all), format_string('%entity_type: Deleted all entities.', array(
'%entity_type' => $entity_type,
)));
// Verify that all data got deleted.
$definition = \Drupal::entityManager()
->getDefinition($entity_type);
$this
->assertEqual(0, db_query('SELECT COUNT(*) FROM {' . $definition
->getBaseTable() . '}')
->fetchField(), 'Base table was emptied');
if ($data_table = $definition
->getDataTable()) {
$this
->assertEqual(0, db_query('SELECT COUNT(*) FROM {' . $data_table . '}')
->fetchField(), 'Data table was emptied');
}
if ($revision_table = $definition
->getRevisionTable()) {
$this
->assertEqual(0, db_query('SELECT COUNT(*) FROM {' . $revision_table . '}')
->fetchField(), 'Data table was emptied');
}
// Test deleting a list of entities not indexed by entity id.
$entities = array();
$entity = entity_create($entity_type, array(
'name' => 'test',
'user_id' => $user1
->id(),
));
$entity
->save();
$entities['test'] = $entity;
$entity = entity_create($entity_type, array(
'name' => 'test2',
'user_id' => $user1
->id(),
));
$entity
->save();
$entities['test2'] = $entity;
$controller = \Drupal::entityManager()
->getStorage($entity_type);
$controller
->delete($entities);
// Verify that entities got deleted.
$all = entity_load_multiple($entity_type);
$this
->assertTrue(empty($all), format_string('%entity_type: Deleted all entities.', array(
'%entity_type' => $entity_type,
)));
// Verify that all data got deleted from the tables.
$definition = \Drupal::entityManager()
->getDefinition($entity_type);
$this
->assertEqual(0, db_query('SELECT COUNT(*) FROM {' . $definition
->getBaseTable() . '}')
->fetchField(), 'Base table was emptied');
if ($data_table = $definition
->getDataTable()) {
$this
->assertEqual(0, db_query('SELECT COUNT(*) FROM {' . $data_table . '}')
->fetchField(), 'Data table was emptied');
}
if ($revision_table = $definition
->getRevisionTable()) {
$this
->assertEqual(0, db_query('SELECT COUNT(*) FROM {' . $revision_table . '}')
->fetchField(), 'Data table was emptied');
}
}