You are here

public function EntityCrudHookTest::testCommentHooks in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Entity/EntityCrudHookTest.php \Drupal\KernelTests\Core\Entity\EntityCrudHookTest::testCommentHooks()
  2. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityCrudHookTest.php \Drupal\KernelTests\Core\Entity\EntityCrudHookTest::testCommentHooks()

Tests hook invocations for CRUD operations on comments.

File

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

Class

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

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testCommentHooks() {
  $account = $this
    ->createUser();
  NodeType::create([
    'type' => 'article',
    'name' => 'Article',
  ])
    ->save();
  $this
    ->addDefaultCommentField('node', 'article', 'comment', CommentItemInterface::OPEN);
  $node = Node::create([
    'uid' => $account
      ->id(),
    'type' => 'article',
    'title' => 'Test node',
    'status' => 1,
    'promote' => 0,
    'sticky' => 0,
    'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    'created' => REQUEST_TIME,
    'changed' => REQUEST_TIME,
  ]);
  $node
    ->save();
  $nid = $node
    ->id();
  $GLOBALS['entity_crud_hook_test'] = [];
  $comment = Comment::create([
    'cid' => NULL,
    'pid' => 0,
    'entity_id' => $nid,
    'entity_type' => 'node',
    'field_name' => 'comment',
    'uid' => $account
      ->id(),
    'subject' => 'Test comment',
    'created' => REQUEST_TIME,
    'changed' => REQUEST_TIME,
    'status' => 1,
    'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $this
    ->assertHookMessageOrder([
    'entity_crud_hook_test_comment_create called',
    'entity_crud_hook_test_entity_create called for type comment',
  ]);
  $GLOBALS['entity_crud_hook_test'] = [];
  $comment
    ->save();
  $this
    ->assertHookMessageOrder([
    'entity_crud_hook_test_comment_presave called',
    'entity_crud_hook_test_entity_presave called for type comment',
    'entity_crud_hook_test_comment_insert called',
    'entity_crud_hook_test_entity_insert called for type comment',
  ]);
  $GLOBALS['entity_crud_hook_test'] = [];
  $comment = Comment::load($comment
    ->id());
  $this
    ->assertHookMessageOrder([
    'entity_crud_hook_test_entity_load called for type comment',
    'entity_crud_hook_test_comment_load called',
  ]);
  $GLOBALS['entity_crud_hook_test'] = [];
  $comment
    ->setSubject('New subject');
  $comment
    ->save();
  $this
    ->assertHookMessageOrder([
    'entity_crud_hook_test_comment_presave called',
    'entity_crud_hook_test_entity_presave called for type comment',
    'entity_crud_hook_test_comment_update called',
    'entity_crud_hook_test_entity_update called for type comment',
  ]);
  $GLOBALS['entity_crud_hook_test'] = [];
  $comment
    ->delete();
  $this
    ->assertHookMessageOrder([
    'entity_crud_hook_test_comment_predelete called',
    'entity_crud_hook_test_entity_predelete called for type comment',
    'entity_crud_hook_test_comment_delete called',
    'entity_crud_hook_test_entity_delete called for type comment',
  ]);
}