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. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityApiTest.php \Drupal\KernelTests\Core\Entity\EntityApiTest::assertCRUD()
  3. 10 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.

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 53

Class

EntityApiTest
Tests basic CRUD functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

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, "{$entity_type}: Created and loaded entity");
    $this->assertEquals('test', $entities[1]->name->value, "{$entity_type}: Created and loaded entity");
    // Test loading a single entity.
    $loaded_entity = $storage->load($entity->id());
    $this->assertEquals($entity->id(), $loaded_entity->id(), "{$entity_type}: Loaded a single entity by id.");
    // Test deleting an entity.
    $entities = array_values($storage->loadByProperties([
        'name' => 'test2',
    ]));
    $entities[0]->delete();
    $entities = array_values($storage->loadByProperties([
        'name' => 'test2',
    ]));
    $this->assertEquals([], $entities, "{$entity_type}: Entity deleted.");
    // 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, "{$entity_type}: Entity updated.");
    // 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');
    }
}

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