function EntityCrudHookTest::testBlockHooks

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityCrudHookTest.php \Drupal\KernelTests\Core\Entity\EntityCrudHookTest::testBlockHooks()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityCrudHookTest.php \Drupal\KernelTests\Core\Entity\EntityCrudHookTest::testBlockHooks()
  3. 10 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 102

Class

EntityCrudHookTest
Tests entity CRUD via hooks.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testBlockHooks() : void {
    $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',
    ]);
}

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