You are here

public function EntityCrudHookTest::testBlockHooks in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Entity/EntityCrudHookTest.php \Drupal\KernelTests\Core\Entity\EntityCrudHookTest::testBlockHooks()

Tests hook invocations for CRUD operations on blocks.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityCrudHookTest.php, line 92

Class

EntityCrudHookTest
Tests the invocation of hooks when creating, inserting, loading, updating or deleting an entity.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testBlockHooks() {
  $entity = Block::create([
    'id' => 'stark_test_html',
    'plugin' => 'test_html',
    'theme' => 'stark',
  ]);
  $this
    ->assertHookMessageOrder([
    'entity_crud_hook_test_block_create called',
    'entity_crud_hook_test_entity_create called for type block',
  ]);
  $GLOBALS['entity_crud_hook_test'] = [];
  $entity
    ->save();
  $this
    ->assertHookMessageOrder([
    'entity_crud_hook_test_block_presave called',
    'entity_crud_hook_test_entity_presave called for type block',
    'entity_crud_hook_test_block_insert called',
    'entity_crud_hook_test_entity_insert called for type block',
  ]);
  $GLOBALS['entity_crud_hook_test'] = [];
  $entity = Block::load($entity
    ->id());
  $this
    ->assertHookMessageOrder([
    'entity_crud_hook_test_entity_load called for type block',
    'entity_crud_hook_test_block_load called',
  ]);
  $GLOBALS['entity_crud_hook_test'] = [];
  $entity->label = 'New label';
  $entity
    ->save();
  $this
    ->assertHookMessageOrder([
    'entity_crud_hook_test_block_presave called',
    'entity_crud_hook_test_entity_presave called for type block',
    'entity_crud_hook_test_block_update called',
    'entity_crud_hook_test_entity_update called for type block',
  ]);
  $GLOBALS['entity_crud_hook_test'] = [];
  $entity
    ->delete();
  $this
    ->assertHookMessageOrder([
    'entity_crud_hook_test_block_predelete called',
    'entity_crud_hook_test_entity_predelete called for type block',
    'entity_crud_hook_test_block_delete called',
    'entity_crud_hook_test_entity_delete called for type block',
  ]);
}