- <?php
- * @file
- * CRUD hook tests for the Entity CRUD API.
- */
-
- * Tests invocation of hooks when performing an action.
- *
- * Tested hooks are:
- * - hook_entity_insert()
- * - hook_entity_load()
- * - hook_entity_update()
- * - hook_entity_predelete()
- * - hook_entity_delete()
- * As well as all type-specific hooks, like hook_node_insert(),
- * hook_comment_update(), etc.
- */
- class EntityCrudHookTestCase extends BackdropWebTestCase {
-
- protected $ids = array();
-
- public function setUp() {
- parent::setUp('entity_crud_hook_test', 'taxonomy', 'comment');
- }
-
-
- * Checks the order of CRUD hook execution messages.
- *
- * entity_crud_hook_test.module implements all core entity CRUD hooks and
- * stores a message for each in $_SESSION['entity_crud_hook_test'].
- *
- * @param $messages
- * An array of plain-text messages in the order they should appear.
- */
- protected function assertHookMessageOrder($messages) {
- $positions = array();
- foreach ($messages as $message) {
-
- $position = array_search($message, $_SESSION['entity_crud_hook_test']);
- if ($this->assertTrue($position !== FALSE, $message)) {
- $positions[] = $position;
- }
- }
-
-
- $sorted = $positions;
- sort($sorted);
- $this->assertTrue($sorted == $positions, 'The hook messages appear in the correct order.');
- }
-
-
- * Tests hook invocations for CRUD operations on comments.
- */
- public function testCommentHooks() {
- $node = entity_create('node', array(
- 'uid' => 1,
- 'type' => 'post',
- 'title' => 'Test node',
- 'status' => 1,
- 'comment' => 2,
- 'promote' => 0,
- 'sticky' => 0,
- 'langcode' => LANGUAGE_NONE,
- '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_NONE,
- ));
-
- $_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',
- ));
- }
-
-
- * Tests hook invocations for CRUD operations on files.
- */
- public function testFileHooks() {
- $url = 'public://entity_crud_hook_test.file';
- file_put_contents($url, 'Test test test');
- $file = entity_create('file', array(
- 'fid' => NULL,
- 'uid' => 1,
- 'filename' => 'entity_crud_hook_test.file',
- 'uri' => $url,
- 'filemime' => 'text/plain',
- 'filesize' => filesize($url),
- 'status' => 1,
- 'timestamp' => REQUEST_TIME,
- ));
- $_SESSION['entity_crud_hook_test'] = array();
- $file->save();
-
- $this->assertHookMessageOrder(array(
- 'entity_crud_hook_test_file_presave called',
- 'entity_crud_hook_test_entity_presave called for type file',
- 'entity_crud_hook_test_file_insert called',
- 'entity_crud_hook_test_entity_insert called for type file',
- ));
-
- $_SESSION['entity_crud_hook_test'] = array();
- $file = file_load($file->fid);
-
- $this->assertHookMessageOrder(array(
- 'entity_crud_hook_test_entity_load called for type file',
- 'entity_crud_hook_test_file_load called',
- ));
-
- $_SESSION['entity_crud_hook_test'] = array();
- $file->filename = 'new.entity_crud_hook_test.file';
- $file->save();
-
- $this->assertHookMessageOrder(array(
- 'entity_crud_hook_test_file_presave called',
- 'entity_crud_hook_test_entity_presave called for type file',
- 'entity_crud_hook_test_file_update called',
- 'entity_crud_hook_test_entity_update called for type file',
- ));
-
- $_SESSION['entity_crud_hook_test'] = array();
- $file->delete();
-
- $this->assertHookMessageOrder(array(
- 'entity_crud_hook_test_file_predelete called',
- 'entity_crud_hook_test_entity_predelete called for type file',
- 'entity_crud_hook_test_file_delete called',
- 'entity_crud_hook_test_entity_delete called for type file',
- ));
- }
-
-
- * Tests hook invocations for CRUD operations on nodes.
- */
- public function testNodeHooks() {
- $node = entity_create('node', array(
- 'uid' => 1,
- 'type' => 'post',
- 'title' => 'Test node',
- 'status' => 1,
- 'comment' => 2,
- 'promote' => 0,
- 'sticky' => 0,
- 'langcode' => LANGUAGE_NONE,
- 'created' => REQUEST_TIME,
- 'changed' => REQUEST_TIME,
- ));
- $_SESSION['entity_crud_hook_test'] = array();
- $node->save();
-
- $this->assertHookMessageOrder(array(
- 'entity_crud_hook_test_node_presave called',
- 'entity_crud_hook_test_entity_presave called for type node',
- 'entity_crud_hook_test_node_insert called',
- 'entity_crud_hook_test_entity_insert called for type node',
- ));
-
- $_SESSION['entity_crud_hook_test'] = array();
- $node = node_load($node->nid);
-
- $this->assertHookMessageOrder(array(
- 'entity_crud_hook_test_entity_load called for type node',
- 'entity_crud_hook_test_node_load called',
- ));
-
- $_SESSION['entity_crud_hook_test'] = array();
- $node->title = 'New title';
- $node->save();
-
- $this->assertHookMessageOrder(array(
- 'entity_crud_hook_test_node_presave called',
- 'entity_crud_hook_test_entity_presave called for type node',
- 'entity_crud_hook_test_node_update called',
- 'entity_crud_hook_test_entity_update called for type node',
- ));
-
- $_SESSION['entity_crud_hook_test'] = array();
- node_delete($node->nid);
-
- $this->assertHookMessageOrder(array(
- 'entity_crud_hook_test_node_predelete called',
- 'entity_crud_hook_test_entity_predelete called for type node',
- 'entity_crud_hook_test_node_delete called',
- 'entity_crud_hook_test_entity_delete called for type node',
- ));
- }
-
-
- * Tests hook invocations for CRUD operations on taxonomy terms.
- */
- public function testTaxonomyTermHooks() {
- $vocabulary = new TaxonomyVocabulary(array(
- 'name' => 'Test vocabulary',
- 'machine_name' => 'test',
- 'description' => NULL,
- ));
- taxonomy_vocabulary_save($vocabulary);
-
- $term = entity_create('taxonomy_term', array(
- 'name' => 'Test term',
- 'vocabulary' => $vocabulary->machine_name,
- 'description' => NULL,
- 'format' => 1,
- ));
- $_SESSION['entity_crud_hook_test'] = array();
- taxonomy_term_save($term);
-
- $this->assertHookMessageOrder(array(
- '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',
- ));
-
- $_SESSION['entity_crud_hook_test'] = array();
- $term = taxonomy_term_load($term->tid);
-
- $this->assertHookMessageOrder(array(
- 'entity_crud_hook_test_entity_load called for type taxonomy_term',
- 'entity_crud_hook_test_taxonomy_term_load called',
- ));
-
- $_SESSION['entity_crud_hook_test'] = array();
- $term->name = 'New name';
- taxonomy_term_save($term);
-
- $this->assertHookMessageOrder(array(
- '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',
- ));
-
- $_SESSION['entity_crud_hook_test'] = array();
- taxonomy_term_delete($term->tid);
-
- $this->assertHookMessageOrder(array(
- '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',
- ));
- }
-
-
- * Tests hook invocations for CRUD operations on users.
- */
- public function testUserHooks() {
- $account = entity_create('user', array(
- 'name' => 'Test user',
- 'mail' => 'test@example.com',
- 'created' => REQUEST_TIME,
- 'status' => 1,
- 'language' => 'en',
- ));
- $_SESSION['entity_crud_hook_test'] = array();
- $account->save();
-
- $this->assertHookMessageOrder(array(
- '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',
- ));
-
- $_SESSION['entity_crud_hook_test'] = array();
- user_load($account->uid);
-
- $this->assertHookMessageOrder(array(
- 'entity_crud_hook_test_entity_load called for type user',
- 'entity_crud_hook_test_user_load called',
- ));
-
- $_SESSION['entity_crud_hook_test'] = array();
- $account->name = 'New name';
- $account->save();
-
- $this->assertHookMessageOrder(array(
- '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',
- ));
-
- $_SESSION['entity_crud_hook_test'] = array();
- user_delete($account->uid);
-
- $this->assertHookMessageOrder(array(
- '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',
- ));
- }
-
- }