123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- <?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_delete()
- * As well as all type-specific hooks, like hook_node_insert(),
- * hook_comment_update(), etc.
- */
- class EntityCrudHookTestCase extends DrupalWebTestCase {
- protected $ids = array();
- public static function getInfo() {
- return array(
- 'name' => 'Entity CRUD hooks',
- 'description' => 'Tests the invocation of hooks when inserting, loading, updating or deleting an entity.',
- 'group' => 'Entity API',
- );
- }
- public function setUp() {
- parent::setUp('entity_crud_hook_test', 'taxonomy', 'comment');
- }
- /**
- * Pass if the message $text was set by one of the CRUD hooks in
- * entity_crud_hook_test.module, i.e., if the $text is an element of
- * $_SESSION['entity_crud_hook_test'].
- *
- * @param $text
- * Plain text to look for.
- * @param $message
- * Message to display.
- * @param $group
- * The group this message belongs to, defaults to 'Other'.
- * @return
- * TRUE on pass, FALSE on fail.
- */
- protected function assertHookMessage($text, $message = NULL, $group = 'Other') {
- if (!isset($message)) {
- $message = $text;
- }
- return $this->assertTrue(array_search($text, $_SESSION['entity_crud_hook_test']) !== FALSE, $message, $group);
- }
- /**
- * Tests hook invocations for CRUD operations on comments.
- */
- public function testCommentHooks() {
- $node = (object) array(
- 'uid' => 1,
- 'type' => 'article',
- 'title' => 'Test node',
- 'status' => 1,
- 'comment' => 2,
- 'promote' => 0,
- 'sticky' => 0,
- 'language' => LANGUAGE_NONE,
- 'created' => REQUEST_TIME,
- 'changed' => REQUEST_TIME,
- );
- node_save($node);
- $nid = $node->nid;
- $comment = (object) array(
- 'cid' => NULL,
- 'pid' => 0,
- 'nid' => $nid,
- 'uid' => 1,
- 'subject' => 'Test comment',
- 'created' => REQUEST_TIME,
- 'changed' => REQUEST_TIME,
- 'status' => 1,
- 'language' => LANGUAGE_NONE,
- );
- $_SESSION['entity_crud_hook_test'] = array();
- comment_save($comment);
- $this->assertHookMessage('entity_crud_hook_test_entity_presave called for type comment');
- $this->assertHookMessage('entity_crud_hook_test_comment_presave called');
- $this->assertHookMessage('entity_crud_hook_test_entity_insert called for type comment');
- $this->assertHookMessage('entity_crud_hook_test_comment_insert called');
- $_SESSION['entity_crud_hook_test'] = array();
- $comment = comment_load($comment->cid);
- $this->assertHookMessage('entity_crud_hook_test_entity_load called for type comment');
- $this->assertHookMessage('entity_crud_hook_test_comment_load called');
- $_SESSION['entity_crud_hook_test'] = array();
- $comment->subject = 'New subject';
- comment_save($comment);
- $this->assertHookMessage('entity_crud_hook_test_entity_presave called for type comment');
- $this->assertHookMessage('entity_crud_hook_test_comment_presave called');
- $this->assertHookMessage('entity_crud_hook_test_entity_update called for type comment');
- $this->assertHookMessage('entity_crud_hook_test_comment_update called');
- $_SESSION['entity_crud_hook_test'] = array();
- comment_delete($comment->cid);
- $this->assertHookMessage('entity_crud_hook_test_entity_delete called for type comment');
- $this->assertHookMessage('entity_crud_hook_test_comment_delete called');
- }
- /**
- * 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 = (object) 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($file);
- $this->assertHookMessage('entity_crud_hook_test_entity_presave called for type file');
- $this->assertHookMessage('entity_crud_hook_test_file_presave called');
- $this->assertHookMessage('entity_crud_hook_test_entity_insert called for type file');
- $this->assertHookMessage('entity_crud_hook_test_file_insert called');
- $_SESSION['entity_crud_hook_test'] = array();
- $file = file_load($file->fid);
- $this->assertHookMessage('entity_crud_hook_test_entity_load called for type file');
- $this->assertHookMessage('entity_crud_hook_test_file_load called');
- $_SESSION['entity_crud_hook_test'] = array();
- $file->filename = 'new.entity_crud_hook_test.file';
- file_save($file);
- $this->assertHookMessage('entity_crud_hook_test_entity_presave called for type file');
- $this->assertHookMessage('entity_crud_hook_test_file_presave called');
- $this->assertHookMessage('entity_crud_hook_test_entity_update called for type file');
- $this->assertHookMessage('entity_crud_hook_test_file_update called');
- $_SESSION['entity_crud_hook_test'] = array();
- file_delete($file);
- $this->assertHookMessage('entity_crud_hook_test_entity_delete called for type file');
- $this->assertHookMessage('entity_crud_hook_test_file_delete called');
- }
- /**
- * Tests hook invocations for CRUD operations on nodes.
- */
- public function testNodeHooks() {
- $node = (object) array(
- 'uid' => 1,
- 'type' => 'article',
- 'title' => 'Test node',
- 'status' => 1,
- 'comment' => 2,
- 'promote' => 0,
- 'sticky' => 0,
- 'language' => LANGUAGE_NONE,
- 'created' => REQUEST_TIME,
- 'changed' => REQUEST_TIME,
- );
- $_SESSION['entity_crud_hook_test'] = array();
- node_save($node);
- $this->assertHookMessage('entity_crud_hook_test_entity_presave called for type node');
- $this->assertHookMessage('entity_crud_hook_test_node_presave called');
- $this->assertHookMessage('entity_crud_hook_test_entity_insert called for type node');
- $this->assertHookMessage('entity_crud_hook_test_node_insert called');
- $_SESSION['entity_crud_hook_test'] = array();
- $node = node_load($node->nid);
- $this->assertHookMessage('entity_crud_hook_test_entity_load called for type node');
- $this->assertHookMessage('entity_crud_hook_test_node_load called');
- $_SESSION['entity_crud_hook_test'] = array();
- $node->title = 'New title';
- node_save($node);
- $this->assertHookMessage('entity_crud_hook_test_entity_presave called for type node');
- $this->assertHookMessage('entity_crud_hook_test_node_presave called');
- $this->assertHookMessage('entity_crud_hook_test_entity_update called for type node');
- $this->assertHookMessage('entity_crud_hook_test_node_update called');
- $_SESSION['entity_crud_hook_test'] = array();
- node_delete($node->nid);
- $this->assertHookMessage('entity_crud_hook_test_entity_delete called for type node');
- $this->assertHookMessage('entity_crud_hook_test_node_delete called');
- }
- /**
- * Tests hook invocations for CRUD operations on taxonomy terms.
- */
- public function testTaxonomyTermHooks() {
- $vocabulary = (object) array(
- 'name' => 'Test vocabulary',
- 'machine_name' => 'test',
- 'description' => NULL,
- 'module' => 'entity_crud_hook_test',
- );
- taxonomy_vocabulary_save($vocabulary);
- $term = (object) array(
- 'vid' => $vocabulary->vid,
- 'name' => 'Test term',
- 'description' => NULL,
- 'format' => 1,
- );
- $_SESSION['entity_crud_hook_test'] = array();
- taxonomy_term_save($term);
- $this->assertHookMessage('entity_crud_hook_test_entity_presave called for type taxonomy_term');
- $this->assertHookMessage('entity_crud_hook_test_taxonomy_term_presave called');
- $this->assertHookMessage('entity_crud_hook_test_entity_insert called for type taxonomy_term');
- $this->assertHookMessage('entity_crud_hook_test_taxonomy_term_insert called');
- $_SESSION['entity_crud_hook_test'] = array();
- $term = taxonomy_term_load($term->tid);
- $this->assertHookMessage('entity_crud_hook_test_entity_load called for type taxonomy_term');
- $this->assertHookMessage('entity_crud_hook_test_taxonomy_term_load called');
- $_SESSION['entity_crud_hook_test'] = array();
- $term->name = 'New name';
- taxonomy_term_save($term);
- $this->assertHookMessage('entity_crud_hook_test_entity_presave called for type taxonomy_term');
- $this->assertHookMessage('entity_crud_hook_test_taxonomy_term_presave called');
- $this->assertHookMessage('entity_crud_hook_test_entity_update called for type taxonomy_term');
- $this->assertHookMessage('entity_crud_hook_test_taxonomy_term_update called');
- $_SESSION['entity_crud_hook_test'] = array();
- taxonomy_term_delete($term->tid);
- $this->assertHookMessage('entity_crud_hook_test_entity_delete called for type taxonomy_term');
- $this->assertHookMessage('entity_crud_hook_test_taxonomy_term_delete called');
- }
- /**
- * Tests hook invocations for CRUD operations on taxonomy vocabularies.
- */
- public function testTaxonomyVocabularyHooks() {
- $vocabulary = (object) array(
- 'name' => 'Test vocabulary',
- 'machine_name' => 'test',
- 'description' => NULL,
- 'module' => 'entity_crud_hook_test',
- );
- $_SESSION['entity_crud_hook_test'] = array();
- taxonomy_vocabulary_save($vocabulary);
- $this->assertHookMessage('entity_crud_hook_test_entity_presave called for type taxonomy_vocabulary');
- $this->assertHookMessage('entity_crud_hook_test_taxonomy_vocabulary_presave called');
- $this->assertHookMessage('entity_crud_hook_test_entity_insert called for type taxonomy_vocabulary');
- $this->assertHookMessage('entity_crud_hook_test_taxonomy_vocabulary_insert called');
- $_SESSION['entity_crud_hook_test'] = array();
- $vocabulary = taxonomy_vocabulary_load($vocabulary->vid);
- $this->assertHookMessage('entity_crud_hook_test_entity_load called for type taxonomy_vocabulary');
- $this->assertHookMessage('entity_crud_hook_test_taxonomy_vocabulary_load called');
- $_SESSION['entity_crud_hook_test'] = array();
- $vocabulary->name = 'New name';
- taxonomy_vocabulary_save($vocabulary);
- $this->assertHookMessage('entity_crud_hook_test_entity_presave called for type taxonomy_vocabulary');
- $this->assertHookMessage('entity_crud_hook_test_taxonomy_vocabulary_presave called');
- $this->assertHookMessage('entity_crud_hook_test_entity_update called for type taxonomy_vocabulary');
- $this->assertHookMessage('entity_crud_hook_test_taxonomy_vocabulary_update called');
- $_SESSION['entity_crud_hook_test'] = array();
- taxonomy_vocabulary_delete($vocabulary->vid);
- $this->assertHookMessage('entity_crud_hook_test_entity_delete called for type taxonomy_vocabulary');
- $this->assertHookMessage('entity_crud_hook_test_taxonomy_vocabulary_delete called');
- }
- /**
- * Tests hook invocations for CRUD operations on users.
- */
- public function testUserHooks() {
- $edit = array(
- 'name' => 'Test user',
- 'mail' => 'test@example.com',
- 'created' => REQUEST_TIME,
- 'status' => 1,
- 'language' => 'en',
- );
- $account = (object) $edit;
- $_SESSION['entity_crud_hook_test'] = array();
- $account = user_save($account, $edit);
- $this->assertHookMessage('entity_crud_hook_test_entity_presave called for type user');
- $this->assertHookMessage('entity_crud_hook_test_user_presave called');
- $this->assertHookMessage('entity_crud_hook_test_entity_insert called for type user');
- $this->assertHookMessage('entity_crud_hook_test_user_insert called');
- $_SESSION['entity_crud_hook_test'] = array();
- $account = user_load($account->uid);
- $this->assertHookMessage('entity_crud_hook_test_entity_load called for type user');
- $this->assertHookMessage('entity_crud_hook_test_user_load called');
- $_SESSION['entity_crud_hook_test'] = array();
- $edit['name'] = 'New name';
- $account = user_save($account, $edit);
- $this->assertHookMessage('entity_crud_hook_test_entity_presave called for type user');
- $this->assertHookMessage('entity_crud_hook_test_user_presave called');
- $this->assertHookMessage('entity_crud_hook_test_entity_update called for type user');
- $this->assertHookMessage('entity_crud_hook_test_user_update called');
- $_SESSION['entity_crud_hook_test'] = array();
- user_delete($account->uid);
- $this->assertHookMessage('entity_crud_hook_test_entity_delete called for type user');
- $this->assertHookMessage('entity_crud_hook_test_user_delete called');
- }
- }
|