forked from Gizra/message_subscribe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_subscribe.test
More file actions
567 lines (468 loc) · 17.7 KB
/
message_subscribe.test
File metadata and controls
567 lines (468 loc) · 17.7 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
<?php
/**
* @file
* Test for the Message subscribe module.
*/
/**
* Test getting context from entity.
*/
class MessageSubscribeContextTest extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Get context',
'description' => 'Get context from an entity.',
'group' => 'Message subscribe',
'dependencies' => array('og'),
);
}
function setUp() {
parent::setUp('message_subscribe', 'taxonomy', 'og');
$user1 = $this->drupalCreateUser();
$user2 = $this->drupalCreateUser();
$user3 = $this->drupalCreateUser();
// Create group node-type.
$type = $this->drupalCreateContentType();
$group_type = $type->type;
og_create_field(OG_GROUP_FIELD, 'node', $group_type);
// Create node-type.
$type = $this->drupalCreateContentType();
$node_type = $type->type;
og_create_field(OG_AUDIENCE_FIELD, 'node', $node_type);
// Create vocabulary and terms.
$vocabulary = new stdClass();
$vocabulary->name = 'Terms';
$vocabulary->machine_name = 'terms';
taxonomy_vocabulary_save($vocabulary);
// Create terms.
$tids = array();
for ($i = 1; $i <= 3; $i++) {
$term = new stdClass();
$term->name = "term $i";
$term->vid = $vocabulary->vid;
taxonomy_term_save($term);
$tids[] = $term->tid;
}
// Create a multiple terms-reference field.
$field = array(
'translatable' => FALSE,
'entity_types' => array('node'),
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => 'terms',
'parent' => 0,
),
),
),
'field_name' => 'field_terms_ref',
'type' => 'taxonomy_term_reference',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
);
$field = field_create_field($field);
$instance = array(
'field_name' => 'field_terms_ref',
'bundle' => $node_type,
'entity_type' => 'node',
);
field_create_instance($instance);
// Create OG group.
$settings = array();
$settings['type'] = $group_type;
$settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 1;
$settings['uid'] = $user3->uid;
$group = $this->drupalCreateNode($settings);
// Create node.
$settings = array();
$settings['type'] = $node_type;
$settings['uid'] = $user1->uid;
$node = $this->drupalCreateNode($settings);
// Assign node to terms.
$wrapper = entity_metadata_wrapper('node', $node);
$wrapper->field_terms_ref->set($tids);
$wrapper->save();
// Assign node to group.
og_group('node', $group->nid, array('entity_type' => 'node', 'entity' => $node));
// Add comment.
$comment = (object) array(
'subject' => 'topic',
'nid' => $node->nid,
'uid' => $user2->uid,
'cid' => FALSE,
'pid' => 0,
'homepage' => '',
'language' => LANGUAGE_NONE,
);
comment_save($comment);
$this->node = $node;
$this->group = $group;
$this->comment = $comment;
$this->tids = $tids;
}
function testGetBasicContext() {
$node = $this->node;
$group = $this->group;
$comment = $this->comment;
// Get context from comment.
$context = message_subscribe_get_basic_context('comment', $comment);
$expected_context = array();
$expected_context['comment'] = drupal_map_assoc(array($comment->cid));
$expected_context['node'] = drupal_map_assoc(array(
$node->nid,
$group->nid,
));
$expected_context['user'] = drupal_map_assoc(array(
$comment->uid,
$node->uid,
$group->uid,
));
$expected_context['taxonomy_term'] = drupal_map_assoc($this->tids);
$this->assertEqual($context, $expected_context, 'Correct context from comment.');
// Pass existing context.
$subscribe_options = array('skip context' => TRUE);
$original_context = array('node' => array(1 => 1), 'user' => array(1 => 1));
$context = message_subscribe_get_basic_context('comment', $comment, $subscribe_options, $original_context);
$this->assertEqual($original_context, $context, 'Correct context when skiping context.');
}
}
/**
* Test getting subscribes from context.
*/
class MessageSubscribeSubscribersTest extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Get subscribers',
'description' => 'Get subscribers from content.',
'group' => 'Message subscribe',
);
}
function setUp() {
parent::setUp('message_subscribe', 'flag', 'taxonomy');
// Create node-type.
$node_type = 'article';
// Enable flags.
$flags = flag_get_default_flags(TRUE);
$flag = $flags['subscribe_node'];
$flag->types[] = $node_type;
$flag->save();
$flag->enable();
$flag = $flags['subscribe_user'];
$flag->save();
$flag->enable();
// Reset our cache so our permissions show up.
drupal_static_reset('flag_get_flags');
// Reset permissions so that permissions for this flag are available.
$this->checkPermissions(array(), TRUE);
$user1 = $this->drupalCreateUser(array(
'flag subscribe_node',
'unflag subscribe_node',
'flag subscribe_user',
'unflag subscribe_user',
));
$user2 = $this->drupalCreateUser(array(
'flag subscribe_node',
'unflag subscribe_node',
'flag subscribe_user',
'unflag subscribe_user',
));
$user_blocked = $this->drupalCreateUser(array(
'flag subscribe_node',
'unflag subscribe_node',
'flag subscribe_user',
'unflag subscribe_user',
));
// Create node.
$settings = array();
$settings['type'] = $node_type;
$settings['uid'] = $user1->uid;
$node = $this->drupalCreateNode($settings);
$settings['uid'] = $user2->uid;
$node1 = $this->drupalCreateNode($settings);
// User1, User2 and user_blocked flag node1.
flag('flag', 'subscribe_node', $node->nid, $user1);
flag('flag', 'subscribe_node', $node->nid, $user2);
flag('flag', 'subscribe_node', $node->nid, $user_blocked);
flag('flag', 'subscribe_node', $node1->nid, $user_blocked);
// User2 flags User1.
flag('flag', 'subscribe_user', $user1->uid, $user2);
// Create a dummy message-type.
$message_type = message_type_create('foo', array('message_text' => array(LANGUAGE_NONE => array(array('value' => 'Example text.')))));
$message_type->save();
$this->node = $node;
$this->node1 = $node1;
$this->user1 = $user1;
$this->user2 = $user2;
// $user_blocked is blocked in order to test
// $subscribe_options['notify blocked users'].
$user_blocked->status = 0;
user_save($user_blocked);
$this->user_blocked = $user_blocked;
// Override default notifiers.
variable_set('message_subscribe_default_notifiers', array());
}
/**
* Test getting the subscribers list.
*/
function testGetSubscribers() {
$message = message_create('foo', array('uid' => $this->user1->uid));
$node = $this->node;
$user2 = $this->user2;
$user_blocked = $this->user_blocked;
$uids = message_subscribe_get_subscribers('node', $node, $message);
// Assert subscribers data.
$expected_uids = array(
$user2->uid => array(
'notifiers' => array(),
'flags' => array(
'subscribe_node',
'subscribe_user',
),
),
);
$this->assertEqual($uids, $expected_uids, 'All expected subscribers were fetched.');
// Test none of users will get message if only blocked user is subscribed.
$message = message_create('foo', array('uid' => $this->user1->uid));
$node1 = $this->node1;
$uids = message_subscribe_get_subscribers('node', $node1, $message);
// Assert subscribers data.
$expected_uids = array();
$this->assertEqual($uids, $expected_uids, 'All expected subscribers were fetched.');
// Test notifying all users, including those who are blocked.
$subscribe_options['notify blocked users'] = TRUE;
$uids = message_subscribe_get_subscribers('node', $node, $message, $subscribe_options);
$expected_uids = array(
$user2->uid => array(
'notifiers' => array(),
'flags' => array(
'subscribe_node',
'subscribe_user',
),
),
$user_blocked->uid => array(
'notifiers' => array(),
'flags' => array(
'subscribe_node',
),
),
);
$this->assertEqual($uids, $expected_uids, 'All expected subscribers were fetched, including blocked users.');
$user3 = $this->drupalCreateUser(array(
'flag subscribe_node',
'unflag subscribe_node',
'flag subscribe_user',
'unflag subscribe_user',
));
$user4 = $this->drupalCreateUser(array(
'flag subscribe_node',
'unflag subscribe_node',
'flag subscribe_user',
'unflag subscribe_user',
));
flag('flag', 'subscribe_node', $node->nid, $user3);
flag('flag', 'subscribe_node', $node->nid, $user4);
// Get subscribers from a given "last uid".
$subscribe_options = array('last uid' => $user2->uid);
$uids = message_subscribe_get_subscribers('node', $node, $message, $subscribe_options);
$this->assertEqual(array_keys($uids), array($user3->uid, $user4->uid), 'All subscribers from "last uid" were fetched.');
// Get a range of subscribers.
$subscribe_options['range'] = 1;
$uids = message_subscribe_get_subscribers('node', $node, $message, $subscribe_options);
$this->assertEqual(array_keys($uids), array($user3->uid), 'All subscribers from "last uid" and "range" were fetched.');
}
/**
* Testing the exclusion of the entity author from the subscribers lists.
*/
function testGetSubscribersExcludeSelf() {
// Test the affect of the variable when set to FALSE (do not notify self).
variable_set('message_subscribe_notify_own_actions', FALSE);
$message = message_create('foo', array('uid' => $this->user1->uid));
$node = $this->node;
$user1 = $this->user1;
$user2 = $this->user2;
$uids = message_subscribe_get_subscribers('node', $node, $message);
// Assert subscribers data.
$expected_uids = array(
$user2->uid => array(
'notifiers' => array(),
'flags' => array(
'subscribe_node',
'subscribe_user',
),
),
);
$this->assertEqual($uids, $expected_uids, 'All subscribers except for the triggering user were fetched.');
// Test the affect of the variable when set to TRUE (Notify self).
variable_set('message_subscribe_notify_own_actions', TRUE);
$uids = message_subscribe_get_subscribers('node', $node, $message);
// Assert subscribers data.
$expected_uids = array(
$user1->uid => array(
'notifiers' => array(),
'flags' => array(
'subscribe_node',
),
),
$user2->uid => array(
'notifiers' => array(),
'flags' => array(
'subscribe_node',
'subscribe_user',
),
),
);
$this->assertEqual($uids, $expected_uids, 'All subscribers including the triggering user were fetched.');
}
/**
* Assert subscribers list is entity-access aware.
*/
function testEntityAccess() {
// Make sure we are notifying ourselves for this test.
variable_set('message_subscribe_notify_own_actions', TRUE);
$message = message_create('foo', array());
$node = $this->node;
$node->status = NODE_NOT_PUBLISHED;
node_save($node);
// Add permission to view own unpublished content.
user_role_change_permissions(DRUPAL_AUTHENTICATED_RID, array('view own unpublished content' => TRUE));
// Set the node to be unpublished.
$user1 = $this->user1;
$user2 = $this->user2;
$subscribe_options['entity access'] = TRUE;
$uids = message_subscribe_get_subscribers('node', $node, $message, $subscribe_options);
$this->assertEqual(array_keys($uids), array($user1->uid), 'Only user with access to node returned for subscribers list.');
$subscribe_options['entity access'] = FALSE;
$uids = message_subscribe_get_subscribers('node', $node, $message, $subscribe_options);
$this->assertEqual(array_keys($uids), array($user1->uid, $user2->uid), 'All users (even without access) returned for subscribers list.');
}
}
/**
* Test queue integration.
*/
class MessageSubscribeQueueTest extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Queue API',
'description' => 'Test integration with queue API.',
'group' => 'Message subscribe',
);
}
function setUp() {
parent::setUp('message_subscribe');
// Override default notifiers.
variable_set('message_subscribe_default_notifiers', array());
// Enable using queue.
variable_set('message_subscribe_use_queue', TRUE);
// Create a dummy message-type.
$message_type = message_type_create('foo', array('message_text' => array(LANGUAGE_NONE => array(array('value' => 'Example text.')))));
$message_type->save();
// Create node-type.
$type = $this->drupalCreateContentType();
$node_type = $type->type;
// Create node.
$user1 = $this->drupalCreateUser();
$settings = array();
$settings['type'] = $node_type;
$settings['uid'] = $user1->uid;
$this->node = $this->drupalCreateNode($settings);
}
/**
* Test base queue processing logic.
*/
function testQueue() {
$node = $this->node;
$message = message_create('foo', array());
$subscribe_options = array();
$subscribe_options['save message'] = FALSE;
try {
$message = message_subscribe_send_message('node', $node, $message, array(), $subscribe_options);
$this->fail('Can add a non-saved message to the queue.');
}
catch (Exception $e) {
$this->pass('Cannot add a non-saved message to the queue.');
}
// Assert message was saved and added to queue.
$uids = array_fill(1, 10, array());
$subscribe_options = array(
'uids' => $uids,
'skip context' => TRUE,
'range' => 3,
);
$queue = DrupalQueue::get('message_subscribe');
$this->assertEqual($queue->numberOfItems(), 0, 'Queue is empty');
message_subscribe_send_message('node', $node, $message, array(), $subscribe_options);
$this->assertTrue($message->mid, 'Message was saved');
$this->assertEqual($queue->numberOfItems(), 1, 'Message added to queue.');
// Assert queue-item is processed and updated. We mock subscription
// of users to the message. It will not be sent, as the default
// notifier is disabled.
$item = $queue->claimItem();
$item_id = $item->item_id;
// Add the queue information, and the user IDs to process.
$subscribe_options['queue'] = array(
'uids' => $uids,
'item' => $item,
'end time' => FALSE,
);
message_subscribe_send_message('node', $node, $message, array(), $subscribe_options);
// Reclaim the new item, and assert the "last UID" was updated.
$item = $queue->claimItem();
$this->assertNotEqual($item_id, $item->item_id, 'Queue item was updated.');
$this->assertEqual($item->data['subscribe_options']['last uid'], 3, 'Last processed user ID was updated.');
}
/**
* Test cron-based queue handling. These are very basic checks that ensure
* the cron worker callback functions as expected. No formal subscription
* processing is triggered here.
*/
function testQueueCron() {
$node = $this->node;
$message = message_create('foo', array());
$queue = DrupalQueue::get('message_subscribe');
// Start with a control case.
message_subscribe_send_message('node', $node, $message, array(), array());
$this->assertEqual($queue->numberOfItems(), 1, 'Message item 1 added to queue.');
$this->cronRun();
$this->assertEqual($queue->numberOfItems(), 0, 'Message item 1 processed by cron.');
// Now try a case where the message entity is deleted before any related
// queue items can be processed.
message_subscribe_send_message('node', $node, $message, array(), array());
$this->assertEqual($queue->numberOfItems(), 1, 'Message item 2 added to queue.');
$message->delete();
// Assert message was deleted.
$this->assertFalse(message_load($message->mid), 'Message entity deleted.');
$this->cronRun();
$this->assertEqual($queue->numberOfItems(), 0, 'Message item 2 processed by cron.');
}
/**
* Test that if we get a list of user IDs directly from
* the implementing module, the messages are sent respecting
* the range value.
*/
function testProvidedUserIdsAreSplitAccordingToRangeValue() {
$user1 = $this->drupalCreateUser();
$user2 = $this->drupalCreateUser();
$user3 = $this->drupalCreateUser();
$message = message_create('foo', array());
$subscribe_options = array(
'uids' => array(
$user1->uid => array('notifiers' => array('email')),
$user2->uid => array('notifiers' => array('email')),
$user3->uid => array('notifiers' => array('email')),
),
'skip context' => TRUE,
'range' => 1,
'last uid' => $user1->uid,
);
message_subscribe_send_message('node', $this->node, $message, array(), $subscribe_options);
// Run cron.
$this->cronRun();
$captured_emails = variable_get('drupal_test_email_collector', array());
$this->assertEqual(2, count($captured_emails), 'The amount of recipients is the same as amount of emails captured according to "subscribe_options".');
$recipients = array();
foreach ($captured_emails as $captured_email) {
$recipients[] = $captured_email['to'];
}
$this->assertFalse(in_array($user1->mail, $recipients), 'User 1 mail is not in the recipient list as it was defined as "last uid".');
$this->assertTrue(in_array($user2->mail, $recipients), 'User 2 mail is in the recipient list.');
$this->assertTrue(in_array($user3->mail, $recipients), 'User 3 mail is in the recipient list.');
}
}