entity_crud_hook_test.test

CRUD hook tests for the Entity CRUD API.

File

drupal-7.x/modules/simpletest/tests/entity_crud_hook_test.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * CRUD hook tests for the Entity CRUD API.
  5. */
  6. /**
  7. * Tests invocation of hooks when performing an action.
  8. *
  9. * Tested hooks are:
  10. * - hook_entity_insert()
  11. * - hook_entity_load()
  12. * - hook_entity_update()
  13. * - hook_entity_delete()
  14. * As well as all type-specific hooks, like hook_node_insert(),
  15. * hook_comment_update(), etc.
  16. */
  17. class EntityCrudHookTestCase extends DrupalWebTestCase {
  18. protected $ids = array();
  19. public static function getInfo() {
  20. return array(
  21. 'name' => 'Entity CRUD hooks',
  22. 'description' => 'Tests the invocation of hooks when inserting, loading, updating or deleting an entity.',
  23. 'group' => 'Entity API',
  24. );
  25. }
  26. public function setUp() {
  27. parent::setUp('entity_crud_hook_test', 'taxonomy', 'comment');
  28. }
  29. /**
  30. * Pass if the message $text was set by one of the CRUD hooks in
  31. * entity_crud_hook_test.module, i.e., if the $text is an element of
  32. * $_SESSION['entity_crud_hook_test'].
  33. *
  34. * @param $text
  35. * Plain text to look for.
  36. * @param $message
  37. * Message to display.
  38. * @param $group
  39. * The group this message belongs to, defaults to 'Other'.
  40. * @return
  41. * TRUE on pass, FALSE on fail.
  42. */
  43. protected function assertHookMessage($text, $message = NULL, $group = 'Other') {
  44. if (!isset($message)) {
  45. $message = $text;
  46. }
  47. return $this->assertTrue(array_search($text, $_SESSION['entity_crud_hook_test']) !== FALSE, $message, $group);
  48. }
  49. /**
  50. * Tests hook invocations for CRUD operations on comments.
  51. */
  52. public function testCommentHooks() {
  53. $node = (object) array(
  54. 'uid' => 1,
  55. 'type' => 'article',
  56. 'title' => 'Test node',
  57. 'status' => 1,
  58. 'comment' => 2,
  59. 'promote' => 0,
  60. 'sticky' => 0,
  61. 'language' => LANGUAGE_NONE,
  62. 'created' => REQUEST_TIME,
  63. 'changed' => REQUEST_TIME,
  64. );
  65. node_save($node);
  66. $nid = $node->nid;
  67. $comment = (object) array(
  68. 'cid' => NULL,
  69. 'pid' => 0,
  70. 'nid' => $nid,
  71. 'uid' => 1,
  72. 'subject' => 'Test comment',
  73. 'created' => REQUEST_TIME,
  74. 'changed' => REQUEST_TIME,
  75. 'status' => 1,
  76. 'language' => LANGUAGE_NONE,
  77. );
  78. $_SESSION['entity_crud_hook_test'] = array();
  79. comment_save($comment);
  80. $this->assertHookMessage('entity_crud_hook_test_entity_presave called for type comment');
  81. $this->assertHookMessage('entity_crud_hook_test_comment_presave called');
  82. $this->assertHookMessage('entity_crud_hook_test_entity_insert called for type comment');
  83. $this->assertHookMessage('entity_crud_hook_test_comment_insert called');
  84. $_SESSION['entity_crud_hook_test'] = array();
  85. $comment = comment_load($comment->cid);
  86. $this->assertHookMessage('entity_crud_hook_test_entity_load called for type comment');
  87. $this->assertHookMessage('entity_crud_hook_test_comment_load called');
  88. $_SESSION['entity_crud_hook_test'] = array();
  89. $comment->subject = 'New subject';
  90. comment_save($comment);
  91. $this->assertHookMessage('entity_crud_hook_test_entity_presave called for type comment');
  92. $this->assertHookMessage('entity_crud_hook_test_comment_presave called');
  93. $this->assertHookMessage('entity_crud_hook_test_entity_update called for type comment');
  94. $this->assertHookMessage('entity_crud_hook_test_comment_update called');
  95. $_SESSION['entity_crud_hook_test'] = array();
  96. comment_delete($comment->cid);
  97. $this->assertHookMessage('entity_crud_hook_test_entity_delete called for type comment');
  98. $this->assertHookMessage('entity_crud_hook_test_comment_delete called');
  99. }
  100. /**
  101. * Tests hook invocations for CRUD operations on files.
  102. */
  103. public function testFileHooks() {
  104. $url = 'public://entity_crud_hook_test.file';
  105. file_put_contents($url, 'Test test test');
  106. $file = (object) array(
  107. 'fid' => NULL,
  108. 'uid' => 1,
  109. 'filename' => 'entity_crud_hook_test.file',
  110. 'uri' => $url,
  111. 'filemime' => 'text/plain',
  112. 'filesize' => filesize($url),
  113. 'status' => 1,
  114. 'timestamp' => REQUEST_TIME,
  115. );
  116. $_SESSION['entity_crud_hook_test'] = array();
  117. file_save($file);
  118. $this->assertHookMessage('entity_crud_hook_test_entity_presave called for type file');
  119. $this->assertHookMessage('entity_crud_hook_test_file_presave called');
  120. $this->assertHookMessage('entity_crud_hook_test_entity_insert called for type file');
  121. $this->assertHookMessage('entity_crud_hook_test_file_insert called');
  122. $_SESSION['entity_crud_hook_test'] = array();
  123. $file = file_load($file->fid);
  124. $this->assertHookMessage('entity_crud_hook_test_entity_load called for type file');
  125. $this->assertHookMessage('entity_crud_hook_test_file_load called');
  126. $_SESSION['entity_crud_hook_test'] = array();
  127. $file->filename = 'new.entity_crud_hook_test.file';
  128. file_save($file);
  129. $this->assertHookMessage('entity_crud_hook_test_entity_presave called for type file');
  130. $this->assertHookMessage('entity_crud_hook_test_file_presave called');
  131. $this->assertHookMessage('entity_crud_hook_test_entity_update called for type file');
  132. $this->assertHookMessage('entity_crud_hook_test_file_update called');
  133. $_SESSION['entity_crud_hook_test'] = array();
  134. file_delete($file);
  135. $this->assertHookMessage('entity_crud_hook_test_entity_delete called for type file');
  136. $this->assertHookMessage('entity_crud_hook_test_file_delete called');
  137. }
  138. /**
  139. * Tests hook invocations for CRUD operations on nodes.
  140. */
  141. public function testNodeHooks() {
  142. $node = (object) array(
  143. 'uid' => 1,
  144. 'type' => 'article',
  145. 'title' => 'Test node',
  146. 'status' => 1,
  147. 'comment' => 2,
  148. 'promote' => 0,
  149. 'sticky' => 0,
  150. 'language' => LANGUAGE_NONE,
  151. 'created' => REQUEST_TIME,
  152. 'changed' => REQUEST_TIME,
  153. );
  154. $_SESSION['entity_crud_hook_test'] = array();
  155. node_save($node);
  156. $this->assertHookMessage('entity_crud_hook_test_entity_presave called for type node');
  157. $this->assertHookMessage('entity_crud_hook_test_node_presave called');
  158. $this->assertHookMessage('entity_crud_hook_test_entity_insert called for type node');
  159. $this->assertHookMessage('entity_crud_hook_test_node_insert called');
  160. $_SESSION['entity_crud_hook_test'] = array();
  161. $node = node_load($node->nid);
  162. $this->assertHookMessage('entity_crud_hook_test_entity_load called for type node');
  163. $this->assertHookMessage('entity_crud_hook_test_node_load called');
  164. $_SESSION['entity_crud_hook_test'] = array();
  165. $node->title = 'New title';
  166. node_save($node);
  167. $this->assertHookMessage('entity_crud_hook_test_entity_presave called for type node');
  168. $this->assertHookMessage('entity_crud_hook_test_node_presave called');
  169. $this->assertHookMessage('entity_crud_hook_test_entity_update called for type node');
  170. $this->assertHookMessage('entity_crud_hook_test_node_update called');
  171. $_SESSION['entity_crud_hook_test'] = array();
  172. node_delete($node->nid);
  173. $this->assertHookMessage('entity_crud_hook_test_entity_delete called for type node');
  174. $this->assertHookMessage('entity_crud_hook_test_node_delete called');
  175. }
  176. /**
  177. * Tests hook invocations for CRUD operations on taxonomy terms.
  178. */
  179. public function testTaxonomyTermHooks() {
  180. $vocabulary = (object) array(
  181. 'name' => 'Test vocabulary',
  182. 'machine_name' => 'test',
  183. 'description' => NULL,
  184. 'module' => 'entity_crud_hook_test',
  185. );
  186. taxonomy_vocabulary_save($vocabulary);
  187. $term = (object) array(
  188. 'vid' => $vocabulary->vid,
  189. 'name' => 'Test term',
  190. 'description' => NULL,
  191. 'format' => 1,
  192. );
  193. $_SESSION['entity_crud_hook_test'] = array();
  194. taxonomy_term_save($term);
  195. $this->assertHookMessage('entity_crud_hook_test_entity_presave called for type taxonomy_term');
  196. $this->assertHookMessage('entity_crud_hook_test_taxonomy_term_presave called');
  197. $this->assertHookMessage('entity_crud_hook_test_entity_insert called for type taxonomy_term');
  198. $this->assertHookMessage('entity_crud_hook_test_taxonomy_term_insert called');
  199. $_SESSION['entity_crud_hook_test'] = array();
  200. $term = taxonomy_term_load($term->tid);
  201. $this->assertHookMessage('entity_crud_hook_test_entity_load called for type taxonomy_term');
  202. $this->assertHookMessage('entity_crud_hook_test_taxonomy_term_load called');
  203. $_SESSION['entity_crud_hook_test'] = array();
  204. $term->name = 'New name';
  205. taxonomy_term_save($term);
  206. $this->assertHookMessage('entity_crud_hook_test_entity_presave called for type taxonomy_term');
  207. $this->assertHookMessage('entity_crud_hook_test_taxonomy_term_presave called');
  208. $this->assertHookMessage('entity_crud_hook_test_entity_update called for type taxonomy_term');
  209. $this->assertHookMessage('entity_crud_hook_test_taxonomy_term_update called');
  210. $_SESSION['entity_crud_hook_test'] = array();
  211. taxonomy_term_delete($term->tid);
  212. $this->assertHookMessage('entity_crud_hook_test_entity_delete called for type taxonomy_term');
  213. $this->assertHookMessage('entity_crud_hook_test_taxonomy_term_delete called');
  214. }
  215. /**
  216. * Tests hook invocations for CRUD operations on taxonomy vocabularies.
  217. */
  218. public function testTaxonomyVocabularyHooks() {
  219. $vocabulary = (object) array(
  220. 'name' => 'Test vocabulary',
  221. 'machine_name' => 'test',
  222. 'description' => NULL,
  223. 'module' => 'entity_crud_hook_test',
  224. );
  225. $_SESSION['entity_crud_hook_test'] = array();
  226. taxonomy_vocabulary_save($vocabulary);
  227. $this->assertHookMessage('entity_crud_hook_test_entity_presave called for type taxonomy_vocabulary');
  228. $this->assertHookMessage('entity_crud_hook_test_taxonomy_vocabulary_presave called');
  229. $this->assertHookMessage('entity_crud_hook_test_entity_insert called for type taxonomy_vocabulary');
  230. $this->assertHookMessage('entity_crud_hook_test_taxonomy_vocabulary_insert called');
  231. $_SESSION['entity_crud_hook_test'] = array();
  232. $vocabulary = taxonomy_vocabulary_load($vocabulary->vid);
  233. $this->assertHookMessage('entity_crud_hook_test_entity_load called for type taxonomy_vocabulary');
  234. $this->assertHookMessage('entity_crud_hook_test_taxonomy_vocabulary_load called');
  235. $_SESSION['entity_crud_hook_test'] = array();
  236. $vocabulary->name = 'New name';
  237. taxonomy_vocabulary_save($vocabulary);
  238. $this->assertHookMessage('entity_crud_hook_test_entity_presave called for type taxonomy_vocabulary');
  239. $this->assertHookMessage('entity_crud_hook_test_taxonomy_vocabulary_presave called');
  240. $this->assertHookMessage('entity_crud_hook_test_entity_update called for type taxonomy_vocabulary');
  241. $this->assertHookMessage('entity_crud_hook_test_taxonomy_vocabulary_update called');
  242. $_SESSION['entity_crud_hook_test'] = array();
  243. taxonomy_vocabulary_delete($vocabulary->vid);
  244. $this->assertHookMessage('entity_crud_hook_test_entity_delete called for type taxonomy_vocabulary');
  245. $this->assertHookMessage('entity_crud_hook_test_taxonomy_vocabulary_delete called');
  246. }
  247. /**
  248. * Tests hook invocations for CRUD operations on users.
  249. */
  250. public function testUserHooks() {
  251. $edit = array(
  252. 'name' => 'Test user',
  253. 'mail' => 'test@example.com',
  254. 'created' => REQUEST_TIME,
  255. 'status' => 1,
  256. 'language' => 'en',
  257. );
  258. $account = (object) $edit;
  259. $_SESSION['entity_crud_hook_test'] = array();
  260. $account = user_save($account, $edit);
  261. $this->assertHookMessage('entity_crud_hook_test_entity_presave called for type user');
  262. $this->assertHookMessage('entity_crud_hook_test_user_presave called');
  263. $this->assertHookMessage('entity_crud_hook_test_entity_insert called for type user');
  264. $this->assertHookMessage('entity_crud_hook_test_user_insert called');
  265. $_SESSION['entity_crud_hook_test'] = array();
  266. $account = user_load($account->uid);
  267. $this->assertHookMessage('entity_crud_hook_test_entity_load called for type user');
  268. $this->assertHookMessage('entity_crud_hook_test_user_load called');
  269. $_SESSION['entity_crud_hook_test'] = array();
  270. $edit['name'] = 'New name';
  271. $account = user_save($account, $edit);
  272. $this->assertHookMessage('entity_crud_hook_test_entity_presave called for type user');
  273. $this->assertHookMessage('entity_crud_hook_test_user_presave called');
  274. $this->assertHookMessage('entity_crud_hook_test_entity_update called for type user');
  275. $this->assertHookMessage('entity_crud_hook_test_user_update called');
  276. $_SESSION['entity_crud_hook_test'] = array();
  277. user_delete($account->uid);
  278. $this->assertHookMessage('entity_crud_hook_test_entity_delete called for type user');
  279. $this->assertHookMessage('entity_crud_hook_test_user_delete called');
  280. }
  281. }