You are here

public function EntityCrudHookTest::testCommentHooks in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/EntityCrudHookTest.php \Drupal\system\Tests\Entity\EntityCrudHookTest::testCommentHooks()

Tests hook invocations for CRUD operations on comments.

File

core/modules/system/src/Tests/Entity/EntityCrudHookTest.php, line 143
Contains \Drupal\system\Tests\Entity\EntityCrudHookTest.

Class

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

Namespace

Drupal\system\Tests\Entity

Code

public function testCommentHooks() {
  $account = $this
    ->createUser();
  entity_create('node_type', array(
    'type' => 'article',
    'name' => 'Article',
  ))
    ->save();
  $this
    ->addDefaultCommentField('node', 'article', 'comment', CommentItemInterface::OPEN);
  $node = entity_create('node', array(
    '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'] = array();
  $comment = entity_create('comment', array(
    '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(array(
    'entity_crud_hook_test_comment_create called',
    'entity_crud_hook_test_entity_create called for type comment',
  ));
  $GLOBALS['entity_crud_hook_test'] = array();
  $comment
    ->save();
  $this
    ->assertHookMessageOrder(array(
    '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'] = array();
  $comment = Comment::load($comment
    ->id());
  $this
    ->assertHookMessageOrder(array(
    'entity_crud_hook_test_entity_load called for type comment',
    'entity_crud_hook_test_comment_load called',
  ));
  $GLOBALS['entity_crud_hook_test'] = array();
  $comment
    ->setSubject('New subject');
  $comment
    ->save();
  $this
    ->assertHookMessageOrder(array(
    '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'] = array();
  $comment
    ->delete();
  $this
    ->assertHookMessageOrder(array(
    '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',
  ));
}