function EntityCrudHookTest::testUserHooks

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

Tests hook invocations for CRUD operations on users.

File

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

Class

EntityCrudHookTest
Tests entity CRUD via hooks.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testUserHooks() : void {
    $account = User::create([
        'name' => 'Test user',
        'mail' => 'test@example.com',
        'created' => \Drupal::time()->getRequestTime(),
        'status' => 1,
        'language' => 'en',
    ]);
    $this->assertHookMessageOrder([
        'entity_crud_hook_test_user_create called',
        'entity_crud_hook_test_entity_create called for type user',
    ]);
    $GLOBALS['entity_crud_hook_test'] = [];
    $account->save();
    $this->assertHookMessageOrder([
        'entity_crud_hook_test_user_presave called',
        'entity_crud_hook_test_entity_presave called for type user',
        'entity_crud_hook_test_user_insert called',
        'entity_crud_hook_test_entity_insert called for type user',
    ]);
    $GLOBALS['entity_crud_hook_test'] = [];
    User::load($account->id());
    $this->assertHookMessageOrder([
        'entity_crud_hook_test_entity_load called for type user',
        'entity_crud_hook_test_user_load called',
    ]);
    $GLOBALS['entity_crud_hook_test'] = [];
    $account->name = 'New name';
    $account->save();
    $this->assertHookMessageOrder([
        'entity_crud_hook_test_user_presave called',
        'entity_crud_hook_test_entity_presave called for type user',
        'entity_crud_hook_test_user_update called',
        'entity_crud_hook_test_entity_update called for type user',
    ]);
    $GLOBALS['entity_crud_hook_test'] = [];
    $account->delete();
    $this->assertHookMessageOrder([
        'entity_crud_hook_test_user_predelete called',
        'entity_crud_hook_test_entity_predelete called for type user',
        'entity_crud_hook_test_user_delete called',
        'entity_crud_hook_test_entity_delete called for type user',
    ]);
}

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