You are here

public function EntityCrudHookTest::testUserHooks 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::testUserHooks()

Tests hook invocations for CRUD operations on users.

File

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

Class

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

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testUserHooks() {
  $account = User::create([
    'name' => 'Test user',
    'mail' => 'test@example.com',
    'created' => REQUEST_TIME,
    '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',
  ]);
}