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