function EntityCrudHookTest::testNodeHooks

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityCrudHookTest.php \Drupal\KernelTests\Core\Entity\EntityCrudHookTest::testNodeHooks()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityCrudHookTest.php \Drupal\KernelTests\Core\Entity\EntityCrudHookTest::testNodeHooks()
  3. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityCrudHookTest.php \Drupal\KernelTests\Core\Entity\EntityCrudHookTest::testNodeHooks()

Tests hook invocations for CRUD operations on nodes.

File

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

Class

EntityCrudHookTest
Tests entity CRUD via hooks.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testNodeHooks() : void {
    $account = $this->createUser();
    $node = Node::create([
        'uid' => $account->id(),
        'type' => 'article',
        'title' => 'Test node',
        'status' => 1,
        'promote' => 0,
        'sticky' => 0,
        'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
        'created' => \Drupal::time()->getRequestTime(),
        'changed' => \Drupal::time()->getRequestTime(),
    ]);
    $this->assertHookMessageOrder([
        'entity_crud_hook_test_node_create called',
        'entity_crud_hook_test_entity_create called for type node',
    ]);
    $GLOBALS['entity_crud_hook_test'] = [];
    $node->save();
    $this->assertHookMessageOrder([
        'entity_crud_hook_test_node_presave called',
        'entity_crud_hook_test_entity_presave called for type node',
        'entity_crud_hook_test_node_insert called',
        'entity_crud_hook_test_entity_insert called for type node',
    ]);
    $GLOBALS['entity_crud_hook_test'] = [];
    $node = Node::load($node->id());
    $this->assertHookMessageOrder([
        'entity_crud_hook_test_entity_preload called for type node',
        'entity_crud_hook_test_entity_load called for type node',
        'entity_crud_hook_test_node_load called',
    ]);
    $GLOBALS['entity_crud_hook_test'] = [];
    $node->title = 'New title';
    $node->save();
    $this->assertHookMessageOrder([
        'entity_crud_hook_test_node_presave called',
        'entity_crud_hook_test_entity_presave called for type node',
        'entity_crud_hook_test_node_update called',
        'entity_crud_hook_test_entity_update called for type node',
    ]);
    $GLOBALS['entity_crud_hook_test'] = [];
    $node->delete();
    $this->assertHookMessageOrder([
        'entity_crud_hook_test_node_predelete called',
        'entity_crud_hook_test_entity_predelete called for type node',
        'entity_crud_hook_test_node_delete called',
        'entity_crud_hook_test_entity_delete called for type node',
    ]);
}

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