summaryrefslogtreecommitdiff
path: root/kolab.org/www/drupal-7.26/modules/simpletest/tests/entity_crud_hook_test.test
blob: 178e34dc74ff68f47f0882cbc95f3a932cd2778b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?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');
  }

}