public function EntityCrudHookTest::testCommentHooks

Tests hook invocations for CRUD operations on comments.

File

drupal/core/modules/system/lib/Drupal/system/Tests/Entity/EntityCrudHookTest.php, line 71
Definition of Drupal\system\Tests\Entity\EntityCrudHookTest.

Class

EntityCrudHookTest
Tests invocation of hooks when performing an action.

Namespace

Drupal\system\Tests\Entity

Code

public function testCommentHooks() {
  $node = entity_create('node', array(
    'uid' => 1,
    'type' => 'article',
    'title' => 'Test node',
    'status' => 1,
    'comment' => 2,
    'promote' => 0,
    'sticky' => 0,
    'langcode' => LANGUAGE_NOT_SPECIFIED,
    'created' => REQUEST_TIME,
    'changed' => REQUEST_TIME,
  ));
  $node
    ->save();
  $nid = $node->nid;
  $comment = entity_create('comment', array(
    'cid' => NULL,
    'pid' => 0,
    'nid' => $nid,
    'uid' => 1,
    'subject' => 'Test comment',
    'created' => REQUEST_TIME,
    'changed' => REQUEST_TIME,
    'status' => 1,
    'langcode' => LANGUAGE_NOT_SPECIFIED,
  ));
  $_SESSION['entity_crud_hook_test'] = array();
  comment_save($comment);
  $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',
  ));
  $_SESSION['entity_crud_hook_test'] = array();
  $comment = comment_load($comment->cid);
  $this
    ->assertHookMessageOrder(array(
    'entity_crud_hook_test_entity_load called for type comment',
    'entity_crud_hook_test_comment_load called',
  ));
  $_SESSION['entity_crud_hook_test'] = array();
  $comment->subject = 'New subject';
  comment_save($comment);
  $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',
  ));
  $_SESSION['entity_crud_hook_test'] = array();
  comment_delete($comment->cid);
  $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',
  ));
}