function EntityApiTest::assertCRUD

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityApiTest.php \Drupal\KernelTests\Core\Entity\EntityApiTest::assertCRUD()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityApiTest.php \Drupal\KernelTests\Core\Entity\EntityApiTest::assertCRUD()
  3. 11.x 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.

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/tests/Drupal/KernelTests/Core/Entity/EntityApiTest.php
Tests basic CRUD functionality of the Entity API.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityApiTest.php, line 50

Class

EntityApiTest
Tests basic CRUD functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

protected function assertCRUD($entity_type, UserInterface $user1) {
    // 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->assertEqual($entities[0]->name->value, 'test', new FormattableMarkup('%entity_type: Created and loaded entity', [
        '%entity_type' => $entity_type,
    ]));
    $this->assertEqual($entities[1]->name->value, 'test', new FormattableMarkup('%entity_type: Created and loaded entity', [
        '%entity_type' => $entity_type,
    ]));
    // Test loading a single entity.
    $loaded_entity = $storage->load($entity->id());
    $this->assertEqual($loaded_entity->id(), $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->assertEqual($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->assertEqual($entity->name->value, 'test3', 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->assertTrue(empty($all), new FormattableMarkup('%entity_type: Deleted all entities.', [
        '%entity_type' => $entity_type,
    ]));
    // Verify that all data got deleted.
    $definition = \Drupal::entityTypeManager()->getDefinition($entity_type);
    $connection = Database::getConnection();
    $this->assertEqual(0, $connection->query('SELECT COUNT(*) FROM {' . $definition->getBaseTable() . '}')
        ->fetchField(), 'Base table was emptied');
    if ($data_table = $definition->getDataTable()) {
        $this->assertEqual(0, $connection->query('SELECT COUNT(*) FROM {' . $data_table . '}')
            ->fetchField(), 'Data table was emptied');
    }
    if ($revision_table = $definition->getRevisionTable()) {
        $this->assertEqual(0, $connection->query('SELECT COUNT(*) FROM {' . $revision_table . '}')
            ->fetchField(), 'Data table was emptied');
    }
    if ($revision_data_table = $definition->getRevisionDataTable()) {
        $this->assertEqual(0, $connection->query('SELECT COUNT(*) FROM {' . $revision_data_table . '}')
            ->fetchField(), '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->assertTrue(empty($all), new FormattableMarkup('%entity_type: Deleted all entities.', [
        '%entity_type' => $entity_type,
    ]));
    // Verify that all data got deleted from the tables.
    $definition = \Drupal::entityTypeManager()->getDefinition($entity_type);
    $this->assertEqual(0, $connection->query('SELECT COUNT(*) FROM {' . $definition->getBaseTable() . '}')
        ->fetchField(), 'Base table was emptied');
    if ($data_table = $definition->getDataTable()) {
        $this->assertEqual(0, $connection->query('SELECT COUNT(*) FROM {' . $data_table . '}')
            ->fetchField(), 'Data table was emptied');
    }
    if ($revision_table = $definition->getRevisionTable()) {
        $this->assertEqual(0, $connection->query('SELECT COUNT(*) FROM {' . $revision_table . '}')
            ->fetchField(), 'Data table was emptied');
    }
    if ($revision_data_table = $definition->getRevisionDataTable()) {
        $this->assertEqual(0, $connection->query('SELECT COUNT(*) FROM {' . $revision_data_table . '}')
            ->fetchField(), 'Data table was emptied');
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.