-
Notifications
You must be signed in to change notification settings - Fork 0
/
og.test
2528 lines (2058 loc) · 96.5 KB
/
og.test
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
class OgAccess extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'OG entity access',
'description' => 'Test the access provided by OG API.',
'group' => 'Organic groups',
);
}
function setUp() {
parent::setUp('og', 'entity_feature');
}
/**
* Verify og_user_access_entity() returns correct value.
*/
function testOgAccessEntity() {
$perm = 'administer group';
// Change permissions to authenticated member.
// Add OG group fields.
og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
$roles = array_flip(og_roles('entity_test', 'main'));
og_role_change_permissions($roles[OG_AUTHENTICATED_ROLE], array($perm => 1));
$og_field = og_fields_info(OG_AUDIENCE_FIELD);
$og_field['field']['settings']['target_type'] = 'entity_test';
og_create_field(OG_AUDIENCE_FIELD, 'node', 'article', $og_field);
$user1 = $this->drupalCreateUser();
$user2 = $this->drupalCreateUser();
$user3 = $this->drupalCreateUser();
// Create a group.
$entity1 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
$wrapper = entity_metadata_wrapper('entity_test', $entity1);
$wrapper->{OG_GROUP_FIELD}->set(1);
$wrapper->save();
// User has access to group.
$this->assertTrue(og_user_access_entity($perm, 'entity_test', $entity1, $user1), t('User1 has access to group.'));
$this->assertFalse(og_user_access_entity($perm, 'entity_test', $entity1, $user2), t('User2 does not have access to group.'));
// User has access to a group associated with a group content.
$settings = array();
$settings['type'] = 'article';
$node = $this->drupalCreateNode($settings);
$values = array('entity_type' => 'node', 'entity' => $node);
og_group('entity_test', $entity1->pid, $values);
$this->assertTrue(og_user_access_entity($perm, 'node', $node, $user1), t('User1 has access to group content.'));
$this->assertFalse(og_user_access_entity($perm, 'node', $node, $user2), t('User2 does not have access to group content.'));
// Make group content also a group.
og_create_field(OG_GROUP_FIELD, 'node', 'article');
$og_field = og_fields_info(OG_AUDIENCE_FIELD);
og_create_field('og_group_ref_2', 'user', 'user', $og_field);
$settings['uid'] = $user2->uid;
$settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 1;
$node = $this->drupalCreateNode($settings);
$wrapper = entity_metadata_wrapper('node', $node);
$wrapper->{OG_GROUP_FIELD}->set(1);
$wrapper->save();
$values = array('entity_type' => 'node', 'entity' => $node);
og_group('entity_test', $entity1->pid, $values);
$this->assertTrue(og_user_access_entity($perm, 'node', $node, $user1), t('User1 has access based on access to group.'));
$this->assertTrue(og_user_access_entity($perm, 'node', $node, $user2), t('User2 has access based on access to group content.'));
$this->assertFalse(og_user_access_entity($perm, 'node', $node, $user3), t('User3 has no access to entity.'));
// Entity is a disabled group.
$settings['uid'] = $user2->uid;
$settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 0;
$node = $this->drupalCreateNode($settings);
$this->assertNull(og_user_access_entity($perm, 'node', $node, $user1), t('Entity is a disabled group, so return value is NULL.'));
// Entity is an orphan group content.
$settings = array();
$settings['type'] = 'article';
$settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 0;
$node = $this->drupalCreateNode($settings);
$values = array('entity_type' => 'node', 'entity' => $node);
og_group('entity_test', $entity1->pid, $values);
$entity1->delete();
$this->assertNull(og_user_access_entity($perm, 'node', $node, $user1), t('Entity is an orphan group content, so return value is NULL.'));
// Entity isn't a group or a group content.
$settings = array();
$settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 0;
$settings['type'] = 'article';
$node = $this->drupalCreateNode($settings);
$this->assertNull(og_user_access_entity($perm, 'node', $node, $user1), t('Entity is not a group or a group contentm, so return value is NULL.'));
// Entity is NULL - as might be passed by field_access().
$this->assertNull(og_user_access_entity($perm, 'node', NULL, $user1), t('Entity passed is NULL, so return value is NULL.'));
// Entity is not saved to database yet.
unset($node->nid);
$this->assertNull(og_user_access_entity($perm, 'node', NULL, $user1), t('Entity is not saved to database, so return value is NULL.'));
}
}
/**
* Test Group node access. This will test nodes that are groups and group content.
*/
class OgNodeAccess extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'OG node access',
'description' => 'Test strict node access permissions for group nodes and group content.',
'group' => 'Organic groups',
);
}
function setUp() {
parent::setUp('og');
// Add OG group field to a the node's "page" bundle.
og_create_field(OG_GROUP_FIELD, 'node', 'page');
// Add OG audience field to the node's "article" bundle.
og_create_field(OG_AUDIENCE_FIELD, 'node', 'article');
// Create an editor user and a group manager for these tests.
$this->editor_user = $this->drupalCreateUser(array('access content', 'edit any page content', 'edit any article content', 'create article content'));
$this->group_manager = $this->drupalCreateUser(array('access content', 'create page content', 'edit own article content', 'edit own page content'));
// Create group node.
$settings = array(
'type' => 'page',
OG_GROUP_FIELD . '[und][0][value]' => 1,
'uid' => $this->group_manager->uid
);
$this->group1 = $this->drupalCreateNode($settings);
$this->group2 = $this->drupalCreateNode($settings);
// Create node to add to group.
$settings = array(
'type' => 'article',
'uid' => $this->group_manager->uid,
);
$this->group_content = $this->drupalCreateNode($settings);
// Add node to group.
$values = array(
'entity_type' => 'node',
'entity' => $this->group_content,
);
og_group('node', $this->group1, $values);
}
/**
* Test strict access permissions for updating group node. A non-member of
* a group who has core node access update permission is denied access.
*/
function testStrictAccessNodeUpdate() {
// Set Node access strict variable.
variable_set('og_node_access_strict', TRUE);
// Login as editor and try to change the group node and group content.
$this->drupalLogin($this->editor_user);
$this->drupalGet('node/' . $this->group1->nid . '/edit');
$this->assertResponse('403', t('A non-member with core node access permissions was denied access to edit group node.'));
$this->drupalGet('node/' . $this->group_content->nid . '/edit');
$this->assertResponse('403', t('A non-member with core node access permissions was denied access to edit group content node.'));
// Login as a group manager and try to change group node.
$this->drupalLogin($this->group_manager);
$this->drupalGet('node/' . $this->group1->nid . '/edit');
$this->assertResponse('200', t('Group manager allowed to access to edit group node.'));
$this->drupalGet('node/' . $this->group_content->nid . '/edit');
$this->assertResponse('200', t('Group manager allowed to access to edit group content node.'));
}
/**
* Test access to node create on strict mode.
*/
function testStrictAccessNodeCreate() {
// Set Node access strict variable.
variable_set('og_node_access_strict', TRUE);
$editor_user = $this->editor_user;
$this->drupalLogin($editor_user);
$this->drupalGet('node/add/article');
$this->assertResponse('200', t('User can access node create with non-required field.'));
$instance = field_info_instance('node', OG_AUDIENCE_FIELD, 'article');
$instance['required'] = TRUE;
field_update_instance($instance);
$this->drupalGet('node/add/article');
$this->assertResponse('403', t('User cannot access node create with required field.'));
// Test OG's create permission for a group member.
$editor_user = user_load($editor_user->uid);
og_group('node', $this->group1, array('entity' => $editor_user));
$roles = array_flip(og_roles('node', 'page'));
$permissions = array(
'create article content' => 0,
'update own article content' => 1,
'update any article content' => 1,
);
// Add update permission.
og_role_change_permissions($roles[OG_AUTHENTICATED_ROLE], $permissions);
$this->drupalGet('node/add/article');
$this->assertResponse('403', 'Group member cannot create node.');
// Add create permission.
$permissions = array(
'create article content' => 1,
'update own article content' => 0,
'update any article content' => 0,
);
og_role_change_permissions($roles[OG_AUTHENTICATED_ROLE], $permissions);
$this->drupalGet('node/add/article');
$this->assertResponse('200', 'Group member can create node.');
}
/**
* Test non-strict access permissions for updating group node. A non-member
* of a group who has core node access update permission is allowed access.
*/
function testNoStrictAccessNodeUpdate() {
// Set Node access strict variable.
variable_set('og_node_access_strict', FALSE);
// Login as editor and try to change the group node and group content.
$this->drupalLogin($this->editor_user);
$this->drupalGet('node/' . $this->group1->nid . '/edit');
$this->assertResponse('200', t('A non-member with core node access permissions was not denied access.'));
$this->drupalGet('node/' . $this->group_content->nid . '/edit');
$this->assertResponse('200', t('A non-member with core node access permissions was not denied access to edit group content node.'));
// Login as a group manager and try to change group node.
$this->drupalLogin($this->group_manager);
$this->drupalGet('node/' . $this->group1->nid . '/edit');
$this->assertResponse('200', t('Group manager allowed to access to edit group node.'));
$this->drupalGet('node/' . $this->group_content->nid . '/edit');
$this->assertResponse('200', t('Group manager allowed to access to edit group content node.'));
}
/**
* Test non-strict access permissions for creating group node.
*
* A member of a group who has no core node access create permission is
* allowed access.
*/
function testNoStrictAccessNodeCreate() {
// Set Node access strict variable.
variable_set('og_node_access_strict', FALSE);
$this->group_editor_user = $this->drupalCreateUser(array('access content'));
$this->drupalLogin($this->group_editor_user);
// Test OG's create permission for a group member.
og_group('node', $this->group1, array('entity' => $this->group_editor_user));
$roles = array_flip(og_roles('node', 'page'));
// Add create permission.
$permissions = array(
'create article content' => 1,
'update own article content' => 0,
'update any article content' => 0,
);
og_role_change_permissions($roles[OG_AUTHENTICATED_ROLE], $permissions);
$this->drupalGet('node/add/article');
$this->assertResponse('200', 'Group member can create node.');
}
/**
* Assert a user cannot assign an existing node to a group they don't
* have "create" permissions.
*/
function testNodeUpdateAudienceField() {
// Set Node access strict variable.
variable_set('og_node_access_strict', TRUE);
$editor_user = $this->editor_user;
// Add editor to a single groups.
og_group('node', $this->group1, array('entity' => $editor_user));
og_group('node', $this->group2, array('entity' => $editor_user));
// Add group-content to a single group.
og_group('node', $this->group1, array('entity_type' => 'node', 'entity' => $this->group_content));
// Allow member to update and create.
$og_roles = array_flip(og_roles('node', 'page'));
$permissions = array(
'create article content' => 1,
'update any article content' => 1,
);
og_role_change_permissions($og_roles[OG_AUTHENTICATED_ROLE], $permissions);
// Login and try to edit this node
$this->drupalLogin($this->editor_user);
$this->drupalGet('node/'. $this->group_content->nid .'/edit');
$name = 'og_group_ref[und][0][default][]';
$xpath = $this->buildXPathQuery('//select[@name=:name]', array(':name' => $name));
$fields = $this->xpath($xpath);
$this->assertTrue(!empty($fields[0]->option[2]), 'User can assign group-content to a new group.');
// Allow member to update but not create.
$og_roles = array_flip(og_roles('node', 'page'));
$permissions = array(
'create article content' => 0,
'update any article content' => 1,
);
og_role_change_permissions($og_roles[OG_AUTHENTICATED_ROLE], $permissions);
$this->drupalGet('node/'. $this->group_content->nid .'/edit');
$xpath = $this->buildXPathQuery('//select[@name=:name]', array(':name' => $name));
$fields = $this->xpath($xpath);
$this->assertFalse(!empty($fields[0]->option[2]), 'User cannot assign group-content to a new group.');
// Test for retaining groups on node save.
$this->drupalPost('node/'. $this->group_content->nid .'/edit', array(), t('Save'));
$entity_groups = og_get_entity_groups('node', $this->group_content->nid);
$this->assertFalse(in_array($this->group2->nid, $entity_groups['node']), 'Content retains original groups after saving node form.');
}
}
/**
* Test the Organic groups API and CRUD handling.
*/
class OgMetaData extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'OG metadata',
'description' => 'Test the metadata properties.',
'group' => 'Organic groups',
);
}
function setUp() {
parent::setUp('og', 'entity_feature');
}
/**
* Test the og_get_entity_groups() API function.
*/
function testOgMembershipMetaData() {
// Add OG group field to the entity_test's "main" bundle.
og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
// Add OG audience field to the node's "article" bundle.
$og_field = og_fields_info(OG_AUDIENCE_FIELD);
$og_field['field']['settings']['target_type'] = 'entity_test';
og_create_field(OG_AUDIENCE_FIELD, 'node', 'article', $og_field);
// Add a second audience field.
og_create_field('og_ref_2', 'node', 'article', $og_field);
$user1 = $this->drupalCreateUser();
$entity1 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
$wrapper = entity_metadata_wrapper('entity_test', $entity1);
$wrapper->{OG_GROUP_FIELD}->set(1);
$wrapper->save();
$entity2 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
$wrapper = entity_metadata_wrapper('entity_test', $entity2);
$wrapper->{OG_GROUP_FIELD}->set(1);
$wrapper->save();
$entity3 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
$wrapper = entity_metadata_wrapper('entity_test', $entity3);
$wrapper->{OG_GROUP_FIELD}->set(1);
$wrapper->save();
$settings = array();
$settings['type'] = 'article';
// Create group entities.
foreach (og_group_content_states() as $state => $value) {
$node = $this->drupalCreateNode($settings);
// Assign article to the group.
$values = array('entity_type' => 'node', 'entity' => $node);
og_group('entity_test', $entity1->pid, $values + array('state' => $state));
// Subscribe node to a second group, but with a different state, by
// selecting the state code and incrementing by one (e.g. is the
// state is "active" then the other-state will be "pending").
$other_state = $state == OG_STATE_BLOCKED ? OG_STATE_ACTIVE : $state + 1;
$values += array('state' => $other_state);
og_group('entity_test', $entity2->pid, $values);
// Subscribe node to third group, using a different field.
$values += array('field_name' => 'og_ref_2');
og_group('entity_test', $entity3->pid, $values);
$wrapper = entity_metadata_wrapper('node', $node->nid);
$this->assertEqual($wrapper->og_membership->count(), 3, t('Found all OG memberships.'));
$og_memberships = $wrapper->{'og_membership__' . $state}->value();
$this->assertEqual(count($og_memberships), 1, t('Found 1 OG membership with state @state.', array('@state' => $value)));
$this->assertEqual($og_memberships[0]->state, $state, t('OG membership has correct @state state.', array('@state' => $value)));
$og_memberships = $wrapper->{OG_AUDIENCE_FIELD . '__og_membership__' . $state}->value();
$this->assertEqual(count($og_memberships), 1, t('Found 1 OG membership with state @state in group-audience field.', array('@state' => $value)));
$this->assertEqual($og_memberships[0]->field_name, OG_AUDIENCE_FIELD, t('OG membership with state @state is referencing correct field name in group-audience field.', array('@state' => $value)));
}
$og_memberships = $wrapper->{OG_AUDIENCE_FIELD . '__og_membership'}->value();
$this->assertEqual(count($og_memberships), 2, t('Found 2 OG membership in group-audience field.', array('@state' => $value)));
$this->assertEqual($og_memberships[0]->field_name, OG_AUDIENCE_FIELD, t('OG membership has correct group-audience field.'));
$og_memberships = $wrapper->{'og_ref_2__og_membership'}->value();
$this->assertEqual(count($og_memberships), 1, t('Found 2 OG membership in second group-audience field.', array('@state' => $value)));
$this->assertEqual($og_memberships[0]->field_name, 'og_ref_2', t('OG membership has correct group-audience field.'));
}
}
/**
* Test Group content handeling.
*/
class OgGroupAndUngroup extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'OG group and ungroup',
'description' => 'Test the group and ungrouping of content with a group.',
'group' => 'Organic groups',
);
}
function setUp() {
parent::setUp('og', 'entity_feature');
// Add OG group field to the entity_test's "main" bundle.
og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
// Add OG audience field to the node's "article" bundle.
$og_field = og_fields_info(OG_AUDIENCE_FIELD);
$og_field['field']['settings']['target_type'] = 'entity_test';
og_create_field(OG_AUDIENCE_FIELD, 'node', 'article', $og_field);
}
/**
* Test group and ungroup of content.
*/
function testGroupAndUngroup() {
$user1 = $this->drupalCreateUser();
$user2 = $this->drupalCreateUser();
$entity1 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
$wrapper = entity_metadata_wrapper('entity_test', $entity1);
$wrapper->{OG_GROUP_FIELD}->set(1);
$wrapper->save();
$entity2 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
$wrapper = entity_metadata_wrapper('entity_test', $entity2);
$wrapper->{OG_GROUP_FIELD}->set(1);
$wrapper->save();
$entity3 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
$wrapper = entity_metadata_wrapper('entity_test', $entity3);
$wrapper->{OG_GROUP_FIELD}->set(1);
$wrapper->save();
$settings = array();
$settings['type'] = 'article';
$settings['uid'] = $user2->uid;
$node = $this->drupalCreateNode($settings);
// Exception on OG membership for anonymous user.
try {
og_membership_create('entity_test', $entity1->pid, 'user', 0, OG_AUDIENCE_FIELD)->save();
$this->fail('OG membership can be created for anonymous user.');
}
catch (OgException $e) {
$this->pass('OG membership can not be created for anonymous user.');
}
$this->assertFalse(og_is_member('entity_test', $entity1->pid, 'node', $node), t('Node is not assigned to group1.'));
$values = array('entity_type' => 'node', 'entity' => $node);
og_group('entity_test', $entity1->pid, $values);
$og_membership = og_get_membership('entity_test', $entity1->pid, 'node', $node->nid);
$id = $og_membership->id;
$this->assertTrue(og_is_member('entity_test', $entity1->pid, 'node', $node), t('Node is assigned to group1 with active state.'));
// State changed.
$values += array('state' => OG_STATE_BLOCKED);
og_group('entity_test', $entity1->pid, $values);
$og_membership = og_get_membership('entity_test', $entity1->pid, 'node', $node->nid);
$this->assertEqual($id, $og_membership->id, t('OG membership was updated.'));
$this->assertTrue(og_is_member('entity_test', $entity1->pid, 'node', $node, array(OG_STATE_BLOCKED)), t('Node is assigned to group1 with blocked state.'));
// Exception on existing OG membership.
try {
og_membership_create('entity_test', $entity1->pid, 'node', $node->nid, OG_AUDIENCE_FIELD)->save();
$this->fail('Saving multiple OG membership for same entity and group works.');
}
catch (OgException $e) {
$this->pass('Saving multiple OG membership for same entity and group does not work.');
}
// Add a second audience field.
$og_field = og_fields_info(OG_AUDIENCE_FIELD);
$og_field['field']['settings']['target_type'] = 'entity_test';
$og_field['field']['cardinality'] = 2;
og_create_field('og_ref_2', 'node', 'article', $og_field);
// Re-group to another field.
$values += array('field_name' => 'og_ref_2');
og_group('entity_test', $entity1->pid, $values);
$og_membership = og_get_membership('entity_test', $entity1->pid, 'node', $node->nid);
$this->assertNotEqual($id, $og_membership->id, t('OG membership was re-created.'));
$this->assertEqual('og_ref_2', $og_membership->field_name, t('OG membership is registered under correct field.'));
// Exception on field cardinality.
og_group('entity_test', $entity2->pid, $values);
try {
og_group('entity_test', $entity3->pid, $values);
$this->fail('Grouping beyond field cardinality works.');
}
catch (OgException $e) {
$this->pass('Grouping beyond field cardinality does not work.');
}
// Exception as field-name is incorrect.
$values['field_name'] = 'wrong-field-name';
try {
og_group('entity_test', $entity1->pid, $values);
$this->fail('Grouping with incorrect field name works.');
}
catch (OgException $e) {
$this->pass('Grouping with incorrect field name does not work.');
}
// Exception on audience field, referencing wrong target type.
$og_field = og_fields_info(OG_AUDIENCE_FIELD);
$og_field['field']['settings']['target_type'] = 'node';
og_create_field('og_ref_3', 'node', 'article', $og_field);
$values['field_name'] = 'og_ref_3';
try {
og_group('entity_test', $entity1->pid, $values);
$this->fail('Grouping with wrong target type works.');
}
catch (OgException $e) {
$this->pass('Grouping with wrong target type does not work.');
}
// Exception on audience field, referencing correct target type, but wrong
// bundles.
$og_field = og_fields_info(OG_AUDIENCE_FIELD);
$og_field['field']['settings']['target_type'] = 'entity_test';
$og_field['field']['settings']['handler_settings']['target_bundles'] = array('test');
og_create_field('og_ref_4', 'node', 'article', $og_field);
$values['field_name'] = 'og_ref_4';
try {
og_group('entity_test', $entity1->pid, $values);
$this->fail('Grouping with wrong target bundles works.');
}
catch (OgException $e) {
$this->pass('Grouping with wrong target bundles does not work.');
}
// Exception as user has no group-audience field.
$instance = field_info_instance('user', 'og_user_entity_test', 'user');
field_delete_instance($instance);
try {
$entity2 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
$wrapper = entity_metadata_wrapper('entity_test', $entity2);
$wrapper->{OG_GROUP_FIELD}->set(1);
$wrapper->save();
$this->fail('Grouping with no group-audience field in bundle works.');
}
catch (OgException $e) {
$this->pass('Grouping with no group-audience field in bundle does not work.');
}
// Ungroup node from group.
og_ungroup('entity_test', $entity1->pid, 'node', $node);
$og_membership = og_get_membership('entity_test', $entity1->pid, 'node', $node->nid);
$this->assertFalse($og_membership, t('Node was ungrouped from group.'));
// Delete node and confirm memberships were deleted.
$values = array('entity_type' => 'node', 'entity' => $node);
og_group('entity_test', $entity1->pid, $values);
$nid = $node->nid;
// Re-load node, to make sure we are deleting the most up-to-date one,
// after it was altered by og_group().
$node = node_load($nid, NULL, TRUE);
node_delete($nid);
$this->assertFalse(og_get_entity_groups('node', $nid), t('OG memberships deleted on entity deletion.'));
// Test creating a non-saved OG membership.
$settings = array();
$settings['type'] = 'article';
$settings['uid'] = $user2->uid;
$node = $this->drupalCreateNode($settings);
$values = array('entity_type' => 'node', 'entity' => $node);
$og_membership = og_group('entity_test', $entity1->pid, $values);
$this->assertTrue($og_membership->id, 'New OG membership was created and saved.');
$settings = array();
$settings['type'] = 'article';
$settings['uid'] = $user2->uid;
$node = $this->drupalCreateNode($settings);
$values = array('entity_type' => 'node', 'entity' => $node);
$og_membership = og_group('entity_test', $entity1->pid, $values, FALSE);
$this->assertTrue(empty($og_membership->id), 'New OG membership was created but not saved.');
}
/**
* Test granting deault role to group manager.
*/
function testGroupManagerDefaultRoles() {
// Get only the admin role.
$og_roles = og_roles('entity_test', 'main', 0, FALSE, FALSE);
variable_set('og_group_manager_default_rids_entity_test_main', array_keys($og_roles));
$user1 = $this->drupalCreateUser();
$entity1 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
$wrapper = entity_metadata_wrapper('entity_test', $entity1);
$wrapper->{OG_GROUP_FIELD}->set(1);
$wrapper->save();
$user_roles = og_get_user_roles('entity_test', $entity1->pid, $user1->uid, FALSE);
$this->assertEqual($og_roles, $user_roles, t('Group manager was granted default role.'));
}
}
class OgPermissionsTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'OG permissions',
'description' => 'Verify that permissions can be added and removed via API.',
'group' => 'Organic groups'
);
}
function setUp() {
parent::setUp('og', 'entity_feature');
// Add OG group field to the entity_test's "main" bundle.
og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
// Add OG audience field to the node's "article" bundle.
$og_field = og_fields_info(OG_AUDIENCE_FIELD);
$og_field['field']['settings']['target_type'] = 'entity_test';
og_create_field(OG_AUDIENCE_FIELD, 'node', 'article', $og_field);
}
/**
* Verify proper permission changes by og_role_change_permissions().
*/
function testOgUserRoleChangePermissions() {
// Create user.
$user1 = $this->drupalCreateUser();
// Create an entity.
$entity = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
$wrapper = entity_metadata_wrapper('entity_test', $entity);
$wrapper->{OG_GROUP_FIELD}->set(1);
$wrapper->save();
// Associate user to the group.
$user2 = $this->drupalCreateUser();
$values = array('entity_type' => 'user', 'entity' => $user2);
og_group('entity_test', $entity->pid, $values);
// Assert the user is registered to the new group.
$this->assertTrue(og_is_member('entity_test', $entity->pid, 'user', $user2), t('User is registered to the new group.'));
// Verify current permissions.
$this->assertFalse(og_user_access('entity_test', $entity->pid, 'update own article content', $user2), t('User does not have "update own article content" permission.'));
$this->assertFalse(og_user_access('entity_test', $entity->pid, 'delete own article content', $user2), t('User does not have "delete own article content" permission.'));
// Change permissions to authenticated member.
$og_roles = array_flip(og_roles('entity_test', 'main', $entity->pid));
// Authenticated role ID.
$rid = $og_roles[OG_AUTHENTICATED_ROLE];
$permissions = array(
'delete own article content' => 1,
);
og_role_change_permissions($rid, $permissions);
// Verify proper permission changes.
$this->assertFalse(og_user_access('entity_test', $entity->pid, 'update own article content', $user2), t('User still does not have "update own article content" permission.'));
$this->assertTrue(og_user_access('entity_test', $entity->pid, 'delete own article content', $user2), t('User now has "delete own article content" permission.'));
$permissions = array(
'delete own article content' => 0,
'administer group' => 1,
);
og_role_change_permissions($rid, $permissions);
$this->assertTrue(og_user_access('entity_test', $entity->pid, 'delete own article content', $user2), t('User still has "delete own article content" as they have "administer group" permission.'));
$this->assertTrue(og_user_access('entity_test', $entity->pid, 'administer group', $user2), t('User has "administer group" permission.'));
}
/**
* Assert blocked and pending roles influence the allowed permissions.
*/
function testBlockedAndPendingRoles() {
// Create user.
$user1 = $this->drupalCreateUser();
// Create an entity.
$entity = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
$wrapper = entity_metadata_wrapper('entity_test', $entity);
$wrapper->{OG_GROUP_FIELD}->set(1);
$wrapper->save();
// Associate user to the group, and grant "admin" role.
$user2 = $this->drupalCreateUser();
$values = array('entity_type' => 'user', 'entity' => $user2);
og_group('entity_test', $entity->pid, $values);
$og_roles = og_roles('entity_test', 'main');
$rid = array_search(OG_ADMINISTRATOR_ROLE, $og_roles);
og_role_grant('entity_test', $entity->pid, $user2->uid, $rid);
// Active member.
$roles = og_get_user_roles('entity_test', $entity->pid, $user2->uid);
$expected_result = array(
array_search(OG_AUTHENTICATED_ROLE, $og_roles) => OG_AUTHENTICATED_ROLE,
array_search(OG_ADMINISTRATOR_ROLE, $og_roles) => OG_ADMINISTRATOR_ROLE,
);
$this->assertEqual($roles, $expected_result, 'Active member has also the admin role.');
$this->assertTrue(og_user_access('entity_test', $entity->pid, 'update group', $user2), 'Active member has access.');
// Pending member.
$values['state'] = OG_STATE_PENDING;
og_group('entity_test', $entity->pid, $values);
$roles = og_get_user_roles('entity_test', $entity->pid, $user2->uid);
$rid = array_search(OG_ANONYMOUS_ROLE, $og_roles);
$expected_result = array($rid => OG_ANONYMOUS_ROLE);
$this->assertEqual($roles, $expected_result, 'Pending member has non-member role.');
$this->assertFalse(og_user_access('entity_test', $entity->pid, 'update group', $user2), 'Pending member has no access.');
// Blocked member.
$values['state'] = OG_STATE_BLOCKED;
og_group('entity_test', $entity->pid, $values);
$roles = og_get_user_roles('entity_test', $entity->pid, $user2->uid);
$this->assertEqual($roles, array(), 'Blocked member has no roles.');
$this->assertFalse(og_user_access('entity_test', $entity->pid, 'update group', $user2), 'Blocked member has no access.');
}
function testGrantRolesTwiceForPendingUsers() {
// Create user.
$user1 = $this->drupalCreateUser();
// Create an entity.
$entity = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
$wrapper = entity_metadata_wrapper('entity_test', $entity);
$wrapper->{OG_GROUP_FIELD}->set(1);
$wrapper->save();
$og_roles = og_roles('entity_test', 'main');
//Add the user to the group.
$values = array('entity_type' => 'user', 'entity' => $user1, 'state' => OG_STATE_PENDING);
$rid = array_search(OG_ADMINISTRATOR_ROLE, $og_roles);
og_group('entity_test', $entity->pid, $values);
// Try granting the admin role to a pending user twice.
og_role_grant('entity_test', $entity->pid, $user1->uid, $rid);
og_role_grant('entity_test', $entity->pid, $user1->uid, $rid);
$this->pass('Granting a role twice should not throw an exception.');
}
}
class OgDefaultAccessFieldTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'OG default access field',
'description' => 'Test groups with default access field.',
'group' => 'Organic groups'
);
}
function setUp() {
parent::setUp('og', 'entity_feature');
}
/**
* Test groups with default access field enabled or disabled.
*/
function testOgDefaultAccessField() {
// Create user.
$user1 = $this->drupalCreateUser();
// Add OG group field to the entity_test's "main" bundle.
og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
$og_roles = og_roles('entity_test', 'main');
// Group without default access field.
$entity = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
$wrapper = entity_metadata_wrapper('entity_test', $entity);
$wrapper->{OG_GROUP_FIELD}->set(1);
$wrapper->save();
$this->assertEqual($og_roles, og_roles('entity_test', 'main', $entity->pid), t('Group without default access field is assigned to the global roles and permissions settings.'));
// Add default access field to the entity_test's "main" bundle.
og_create_field(OG_DEFAULT_ACCESS_FIELD, 'entity_test', 'main');
// Group with default access field disabled.
$entity = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
$wrapper = entity_metadata_wrapper('entity_test', $entity);
$wrapper->{OG_GROUP_FIELD}->set(1);
$wrapper->{OG_DEFAULT_ACCESS_FIELD}->set(0);
$wrapper->save();
$this->assertEqual($og_roles, og_roles('entity_test', 'main', $entity->pid), t('Group with default access field disabled is assigned to the global roles and permissions settings.'));
// Add admin role to a user.
$rid = array_search(OG_ADMINISTRATOR_ROLE, $og_roles);
og_role_grant('entity_test', $entity->pid, $user1->uid, $rid);
$user_roles = og_get_user_roles('entity_test', $entity->pid, $user1->uid);
$this->assertTrue(array_search(OG_ADMINISTRATOR_ROLE, $user_roles), t('User has default "admin" role.'));
// Group with default access field enabled.
$wrapper->{OG_DEFAULT_ACCESS_FIELD}->set(1);
$wrapper->save();
$new_og_roles = og_roles('entity_test', 'main', $entity->pid);
$this->assertNotEqual($og_roles, $new_og_roles, t('Group with default access field enabled has own roles and permissions settings.'));
// Assert the newley created admin role was mapped to the default one.
$user_roles = og_get_user_roles('entity_test', $entity->pid, $user1->uid, FALSE);
$this->assertTrue(array_search(OG_ADMINISTRATOR_ROLE, $user_roles), t('User has overriden "admin" role.'));
// Disable existing group's default access field.
variable_set('og_maintain_overridden_roles', TRUE);
$wrapper->{OG_DEFAULT_ACCESS_FIELD}->set(0);
$wrapper->save();
$this->assertEqual($og_roles, og_roles('entity_test', 'main', $entity->pid), t('Group with enabled default access field that was disabled is assigned to the global roles and permissions settings.'));
// Assert admin role was maintained from the overriden group.
$user_roles = og_get_user_roles('entity_test', $entity->pid, $user1->uid, FALSE);
$this->assertTrue(array_search(OG_ADMINISTRATOR_ROLE, $user_roles), t('"admin" role maintained from overriden group.'));
// Override group.
$wrapper->{OG_DEFAULT_ACCESS_FIELD}->set(1);
$wrapper->save();
// Assert admin role was not maintained from the overriden group.
variable_set('og_maintain_overridden_roles', FALSE);
$wrapper->{OG_DEFAULT_ACCESS_FIELD}->set(0);
$wrapper->save();
$user_roles = og_get_user_roles('entity_test', $entity->pid, $user1->uid, FALSE);
$this->assertFalse(array_search(OG_ADMINISTRATOR_ROLE, $user_roles), t('"admin" role not maintained from overriden group.'));
}
}
/**
* Upgrade 7000 test.
*
* Load a filled installation of Drupal 6 and run the upgrade on it.
*
* TODO: We have to use $this->drupalGet('node/' . $nid); to proerly load
* the node data, otherwise. We should understand why this is needed, and
* remove it.
*/
class OgMigrate7000TestCase extends UpgradePathTestCase {
public static function getInfo() {
return array(
'name' => 'OG migrate - 7000',
'description' => 'Tests the upgrade path of OG from Drupal 6.',
'group' => 'Organic groups',
// TODO: Why do we need to enable Views?! - otherwise we get WSOD.
'dependencies' => array('migrate', 'views'),
);
}
public function setUp() {
// Path to the database dump.
$this->databaseDumpFiles = array(
drupal_get_path('module', 'og') . '/tests/drupal-6.og.database.php',
);
parent::setUp();
$this->assertTrue($this->performUpgrade(), t('The upgrade was completed successfully.'));
// spl_autoload_register() wasn't called, so we do it here, to allow
// classes to be auto-loaded.
spl_autoload_register('drupal_autoload_class');
spl_autoload_register('drupal_autoload_interface');
// TODO: Why do we need to enable Views?!
module_enable(array('og', 'views', 'migrate'));
$class_names = array(
'OgMigrateAddFields',
'OgMigrateUser',
'OgMigrateContent',
);
// FIXME: migrate_flush_caches() crashes, so we register manually.
foreach ($class_names as $class_name) {
MigrationBase::registerMigration($class_name);
}
// Register a dynamic migration.
MigrationBase::registerMigration('OgMigrateGroup', 'OgMigrateGroupTest_group', array('bundle' => 'test_group'));
$migration = Migration::getInstance('OgMigrateAddFields');
$result = $migration->processImport();
$this->assertEqual($result, Migration::RESULT_COMPLETED, 'Migration OgMigrateAddFields executed.');
$migration = Migration::getInstance('OgMigrateGroupTest_group', 'OgMigrateGroup', array('bundle' => 'test_group'));
$result = $migration->processImport();
$this->assertEqual($result, Migration::RESULT_COMPLETED, 'Migration OgMigrateGroupTest_group executed.');
$migration = Migration::getInstance('OgMigrateUser');
$result = $migration->processImport();
$this->assertEqual($result, Migration::RESULT_COMPLETED, 'Migration OgMigrateUser executed.');
$migration = Migration::getInstance('OgMigrateContent');
$result = $migration->processImport();
$this->assertEqual($result, Migration::RESULT_COMPLETED, 'Migration OgMigrateContent executed.');
}
/**
* Test a successful group upgrade.
*
* @see og_7000_group()
*/
public function testGroup() {
// Assert according to the scenario Drupal 6's test table dump was created.
foreach (array(1, 2) as $nid) {
$this->drupalGet('node/' . $nid);
$node = node_load($nid);
$this->assertTrue($node->{OG_GROUP_FIELD}[LANGUAGE_NONE][0]['value'], t('Node ID @nid is an active group.', array('@nid' => $nid)));
}
// Test group content with NID 3 - 5 belong to the group with NID 2.
foreach (range(3, 5) as $nid) {
$this->drupalGet('node/' . $nid);
$node = node_load($nid);
$this->assertTrue(og_is_member('node', 2, 'node', $node), t('Node ID @nid is a group content of Node ID 2', array('@nid' => $nid)));
}
// Orphan group content (i.e. not attached to a group).
$node = node_load(6);
$this->assertFalse(og_get_entity_groups('node', $node), t('Node ID 6 is not associated with any group.'));
// Group content that shares the same group.
$node = node_load(9);
foreach (array(7, 8) as $nid) {
$this->assertTrue(og_is_member('node', $nid, 'node', $node), t('Node ID @nid is as group content associated with multiple groups.', array('@nid' => $node->nid)));
}
}
/**
* Test user upgrade.
*
* @see og_7000_user()
*/
public function testUser() {
// Assert users.
$values = array(
// Uid 3 is the group manager, so in OG6 it was marked as admin.
3 => array('admin' => TRUE),
4 => array('active' => FALSE),
5 => array(),
6 => array('active' => FALSE, 'admin' => TRUE),
7 => array('admin' => TRUE),
);
$og_roles = og_roles('node', 'test_group');
foreach ($values as $uid => $value) {
$account = user_load($uid);
// Set default values.
$value += array('active' => TRUE, 'admin' => FALSE);