You are here

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

Tests hook invocations for CRUD operations on taxonomy terms.

File

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

Class

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

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testTaxonomyTermHooks() {
  $this
    ->installEntitySchema('taxonomy_term');
  $vocabulary = Vocabulary::create([
    'name' => 'Test vocabulary',
    'vid' => 'test',
    'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    'description' => NULL,
    'module' => 'entity_crud_hook_test',
  ]);
  $vocabulary
    ->save();
  $GLOBALS['entity_crud_hook_test'] = [];
  $term = Term::create([
    'vid' => $vocabulary
      ->id(),
    'name' => 'Test term',
    'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    'description' => NULL,
    'format' => 1,
  ]);
  $this
    ->assertHookMessageOrder([
    'entity_crud_hook_test_taxonomy_term_create called',
    'entity_crud_hook_test_entity_create called for type taxonomy_term',
  ]);
  $GLOBALS['entity_crud_hook_test'] = [];
  $term
    ->save();
  $this
    ->assertHookMessageOrder([
    'entity_crud_hook_test_taxonomy_term_presave called',
    'entity_crud_hook_test_entity_presave called for type taxonomy_term',
    'entity_crud_hook_test_taxonomy_term_insert called',
    'entity_crud_hook_test_entity_insert called for type taxonomy_term',
  ]);
  $GLOBALS['entity_crud_hook_test'] = [];
  $term = Term::load($term
    ->id());
  $this
    ->assertHookMessageOrder([
    'entity_crud_hook_test_entity_load called for type taxonomy_term',
    'entity_crud_hook_test_taxonomy_term_load called',
  ]);
  $GLOBALS['entity_crud_hook_test'] = [];
  $term
    ->setName('New name');
  $term
    ->save();
  $this
    ->assertHookMessageOrder([
    'entity_crud_hook_test_taxonomy_term_presave called',
    'entity_crud_hook_test_entity_presave called for type taxonomy_term',
    'entity_crud_hook_test_taxonomy_term_update called',
    'entity_crud_hook_test_entity_update called for type taxonomy_term',
  ]);
  $GLOBALS['entity_crud_hook_test'] = [];
  $term
    ->delete();
  $this
    ->assertHookMessageOrder([
    'entity_crud_hook_test_taxonomy_term_predelete called',
    'entity_crud_hook_test_entity_predelete called for type taxonomy_term',
    'entity_crud_hook_test_taxonomy_term_delete called',
    'entity_crud_hook_test_entity_delete called for type taxonomy_term',
  ]);
}