Add og-test stuff.
[project/og.git] / og.test
1 <?php
2
3 class OgAccess extends DrupalWebTestCase {
4
5 public static function getInfo() {
6 return array(
7 'name' => 'OG entity access',
8 'description' => 'Test the access provided by OG API.',
9 'group' => 'Organic groups',
10 );
11 }
12
13 function setUp() {
14 parent::setUp('og', 'entity_feature');
15 }
16
17 /**
18 * Verify og_user_access_entity() returns correct value.
19 */
20 function testOgAccessEntity() {
21 $perm = 'administer group';
22 // Change permissions to authenticated member.
23
24 // Add OG group fields.
25 og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
26 $roles = array_flip(og_roles('entity_test', 'main'));
27 og_role_change_permissions($roles[OG_AUTHENTICATED_ROLE], array($perm => 1));
28
29
30 $og_field = og_fields_info(OG_AUDIENCE_FIELD);
31 $og_field['field']['settings']['target_type'] = 'entity_test';
32 og_create_field(OG_AUDIENCE_FIELD, 'node', 'article', $og_field);
33
34 $user1 = $this->drupalCreateUser();
35 $user2 = $this->drupalCreateUser();
36 $user3 = $this->drupalCreateUser();
37
38 // Create a group.
39 $entity1 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
40 $wrapper = entity_metadata_wrapper('entity_test', $entity1);
41 $wrapper->{OG_GROUP_FIELD}->set(1);
42 $wrapper->save();
43
44 // User has access to group.
45 $this->assertTrue(og_user_access_entity($perm, 'entity_test', $entity1, $user1), t('User1 has access to group.'));
46 $this->assertFalse(og_user_access_entity($perm, 'entity_test', $entity1, $user2), t('User2 does not have access to group.'));
47
48 // User has access to a group associated with a group content.
49 $settings = array();
50 $settings['type'] = 'article';
51 $node = $this->drupalCreateNode($settings);
52
53 $values = array('entity_type' => 'node', 'entity' => $node);
54 og_group('entity_test', $entity1->pid, $values);
55 $this->assertTrue(og_user_access_entity($perm, 'node', $node, $user1), t('User1 has access to group content.'));
56 $this->assertFalse(og_user_access_entity($perm, 'node', $node, $user2), t('User2 does not have access to group content.'));
57
58 // Make group content also a group.
59 og_create_field(OG_GROUP_FIELD, 'node', 'article');
60 $og_field = og_fields_info(OG_AUDIENCE_FIELD);
61 og_create_field('og_group_ref_2', 'user', 'user', $og_field);
62
63 $settings['uid'] = $user2->uid;
64 $settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 1;
65 $node = $this->drupalCreateNode($settings);
66
67 $wrapper = entity_metadata_wrapper('node', $node);
68 $wrapper->{OG_GROUP_FIELD}->set(1);
69 $wrapper->save();
70
71 $values = array('entity_type' => 'node', 'entity' => $node);
72 og_group('entity_test', $entity1->pid, $values);
73
74 $this->assertTrue(og_user_access_entity($perm, 'node', $node, $user1), t('User1 has access based on access to group.'));
75 $this->assertTrue(og_user_access_entity($perm, 'node', $node, $user2), t('User2 has access based on access to group content.'));
76 $this->assertFalse(og_user_access_entity($perm, 'node', $node, $user3), t('User3 has no access to entity.'));
77
78 // Entity is a disabled group.
79 $settings['uid'] = $user2->uid;
80 $settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 0;
81 $node = $this->drupalCreateNode($settings);
82 $this->assertNull(og_user_access_entity($perm, 'node', $node, $user1), t('Entity is a disabled group, so return value is NULL.'));
83
84 // Entity is an orphan group content.
85 $settings = array();
86 $settings['type'] = 'article';
87 $settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 0;
88 $node = $this->drupalCreateNode($settings);
89 $values = array('entity_type' => 'node', 'entity' => $node);
90 og_group('entity_test', $entity1->pid, $values);
91 $entity1->delete();
92 $this->assertNull(og_user_access_entity($perm, 'node', $node, $user1), t('Entity is an orphan group content, so return value is NULL.'));
93
94 // Entity isn't a group or a group content.
95 $settings = array();
96 $settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 0;
97 $settings['type'] = 'article';
98 $node = $this->drupalCreateNode($settings);
99 $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.'));
100
101 // Entity is NULL - as might be passed by field_access().
102 $this->assertNull(og_user_access_entity($perm, 'node', NULL, $user1), t('Entity passed is NULL, so return value is NULL.'));
103
104 // Entity is not saved to database yet.
105 unset($node->nid);
106 $this->assertNull(og_user_access_entity($perm, 'node', NULL, $user1), t('Entity is not saved to database, so return value is NULL.'));
107 }
108 }
109
110 /**
111 * Test Group node access. This will test nodes that are groups and group content.
112 */
113 class OgNodeAccess extends DrupalWebTestCase {
114
115 public static function getInfo() {
116 return array(
117 'name' => 'OG node access',
118 'description' => 'Test strict node access permissions for group nodes and group content.',
119 'group' => 'Organic groups',
120 );
121 }
122
123 function setUp() {
124 parent::setUp('og');
125
126 // Add OG group field to a the node's "page" bundle.
127 og_create_field(OG_GROUP_FIELD, 'node', 'page');
128
129 // Add OG audience field to the node's "article" bundle.
130 og_create_field(OG_AUDIENCE_FIELD, 'node', 'article');
131
132 // Create an editor user and a group manager for these tests.
133 $this->editor_user = $this->drupalCreateUser(array('access content', 'edit any page content', 'edit any article content', 'create article content'));
134 $this->group_manager = $this->drupalCreateUser(array('access content', 'create page content', 'edit own article content', 'edit own page content'));
135
136 // Create group node.
137 $settings = array(
138 'type' => 'page',
139 OG_GROUP_FIELD . '[und][0][value]' => 1,
140 'uid' => $this->group_manager->uid
141 );
142 $this->group1 = $this->drupalCreateNode($settings);
143 $this->group2 = $this->drupalCreateNode($settings);
144
145 // Create node to add to group.
146 $settings = array(
147 'type' => 'article',
148 'uid' => $this->group_manager->uid,
149 );
150 $this->group_content = $this->drupalCreateNode($settings);
151
152 // Add node to group.
153 $values = array(
154 'entity_type' => 'node',
155 'entity' => $this->group_content,
156 );
157 og_group('node', $this->group1, $values);
158 }
159
160 /**
161 * Test strict access permissions for updating group node. A non-member of
162 * a group who has core node access update permission is denied access.
163 */
164 function testStrictAccessNodeUpdate() {
165 // Set Node access strict variable.
166 variable_set('og_node_access_strict', TRUE);
167
168 // Login as editor and try to change the group node and group content.
169 $this->drupalLogin($this->editor_user);
170
171 $this->drupalGet('node/' . $this->group1->nid . '/edit');
172 $this->assertResponse('403', t('A non-member with core node access permissions was denied access to edit group node.'));
173
174 $this->drupalGet('node/' . $this->group_content->nid . '/edit');
175 $this->assertResponse('403', t('A non-member with core node access permissions was denied access to edit group content node.'));
176
177 // Login as a group manager and try to change group node.
178 $this->drupalLogin($this->group_manager);
179
180 $this->drupalGet('node/' . $this->group1->nid . '/edit');
181 $this->assertResponse('200', t('Group manager allowed to access to edit group node.'));
182
183 $this->drupalGet('node/' . $this->group_content->nid . '/edit');
184 $this->assertResponse('200', t('Group manager allowed to access to edit group content node.'));
185 }
186
187 /**
188 * Test access to node create on strict mode.
189 */
190 function testStrictAccessNodeCreate() {
191 // Set Node access strict variable.
192 variable_set('og_node_access_strict', TRUE);
193 $editor_user = $this->editor_user;
194 $this->drupalLogin($editor_user);
195
196 $this->drupalGet('node/add/article');
197 $this->assertResponse('200', t('User can access node create with non-required field.'));
198
199 $instance = field_info_instance('node', OG_AUDIENCE_FIELD, 'article');
200 $instance['required'] = TRUE;
201 field_update_instance($instance);
202
203 $this->drupalGet('node/add/article');
204 $this->assertResponse('403', t('User cannot access node create with required field.'));
205
206 // Test OG's create permission for a group member.
207 $editor_user = user_load($editor_user->uid);
208 og_group('node', $this->group1, array('entity' => $editor_user));
209 $roles = array_flip(og_roles('node', 'page'));
210
211 $permissions = array(
212 'create article content' => 0,
213 'update own article content' => 1,
214 'update any article content' => 1,
215 );
216
217 // Add update permission.
218 og_role_change_permissions($roles[OG_AUTHENTICATED_ROLE], $permissions);
219 $this->drupalGet('node/add/article');
220 $this->assertResponse('403', 'Group member cannot create node.');
221
222 // Add create permission.
223 $permissions = array(
224 'create article content' => 1,
225 'update own article content' => 0,
226 'update any article content' => 0,
227 );
228 og_role_change_permissions($roles[OG_AUTHENTICATED_ROLE], $permissions);
229 $this->drupalGet('node/add/article');
230 $this->assertResponse('200', 'Group member can create node.');
231 }
232
233 /**
234 * Test non-strict access permissions for updating group node. A non-member
235 * of a group who has core node access update permission is allowed access.
236 */
237 function testNoStrictAccessNodeUpdate() {
238 // Set Node access strict variable.
239 variable_set('og_node_access_strict', FALSE);
240
241 // Login as editor and try to change the group node and group content.
242 $this->drupalLogin($this->editor_user);
243
244 $this->drupalGet('node/' . $this->group1->nid . '/edit');
245 $this->assertResponse('200', t('A non-member with core node access permissions was not denied access.'));
246
247 $this->drupalGet('node/' . $this->group_content->nid . '/edit');
248 $this->assertResponse('200', t('A non-member with core node access permissions was not denied access to edit group content node.'));
249
250 // Login as a group manager and try to change group node.
251 $this->drupalLogin($this->group_manager);
252
253 $this->drupalGet('node/' . $this->group1->nid . '/edit');
254 $this->assertResponse('200', t('Group manager allowed to access to edit group node.'));
255
256 $this->drupalGet('node/' . $this->group_content->nid . '/edit');
257 $this->assertResponse('200', t('Group manager allowed to access to edit group content node.'));
258 }
259
260 /**
261 * Assert a user cannot assign an existing node to a group they don't
262 * have "create" permissions.
263 */
264 function testNodeUpdateAudienceField() {
265 // Set Node access strict variable.
266 variable_set('og_node_access_strict', TRUE);
267 $editor_user = $this->editor_user;
268
269 // Add editor to a single groups.
270 og_group('node', $this->group1, array('entity' => $editor_user));
271 og_group('node', $this->group2, array('entity' => $editor_user));
272
273 // Add group-content to a single group.
274 og_group('node', $this->group1, array('entity_type' => 'node', 'entity' => $this->group_content));
275
276 // Allow member to update and create.
277 $og_roles = array_flip(og_roles('node', 'page'));
278 $permissions = array(
279 'create article content' => 1,
280 'update any article content' => 1,
281 );
282 og_role_change_permissions($og_roles[OG_AUTHENTICATED_ROLE], $permissions);
283
284 // Login and try to edit this node
285 $this->drupalLogin($this->editor_user);
286
287 $this->drupalGet('node/'. $this->group_content->nid .'/edit');
288 $name = 'og_group_ref[und][0][default][]';
289 $xpath = $this->buildXPathQuery('//select[@name=:name]', array(':name' => $name));
290 $fields = $this->xpath($xpath);
291 $this->assertTrue(!empty($fields[0]->option[2]), 'User can assign group-content to a new group.');
292
293 // Allow member to update but not create.
294 $og_roles = array_flip(og_roles('node', 'page'));
295 $permissions = array(
296 'create article content' => 0,
297 'update any article content' => 1,
298 );
299 og_role_change_permissions($og_roles[OG_AUTHENTICATED_ROLE], $permissions);
300
301 $this->drupalGet('node/'. $this->group_content->nid .'/edit');
302 $xpath = $this->buildXPathQuery('//select[@name=:name]', array(':name' => $name));
303 $fields = $this->xpath($xpath);
304 $this->assertFalse(!empty($fields[0]->option[2]), 'User cannot assign group-content to a new group.');
305
306 // Test for retaining groups on node save.
307 $this->drupalPost('node/'. $this->group_content->nid .'/edit', array(), t('Save'));
308
309 $entity_groups = og_get_entity_groups('node', $this->group_content->nid);
310 $this->assertFalse(in_array($this->group2->nid, $entity_groups['node']), 'Content retains original groups after saving node form.');
311 }
312
313 }
314
315 /**
316 * Test the Organic groups API and CRUD handling.
317 */
318 class OgMetaData extends DrupalWebTestCase {
319
320 public static function getInfo() {
321 return array(
322 'name' => 'OG metadata',
323 'description' => 'Test the metadata properties.',
324 'group' => 'Organic groups',
325 );
326 }
327
328 function setUp() {
329 parent::setUp('og', 'entity_feature');
330 }
331
332 /**
333 * Test the og_get_entity_groups() API function.
334 */
335 function testOgMembershipMetaData() {
336 // Add OG group field to the entity_test's "main" bundle.
337 og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
338
339 // Add OG audience field to the node's "article" bundle.
340 $og_field = og_fields_info(OG_AUDIENCE_FIELD);
341 $og_field['field']['settings']['target_type'] = 'entity_test';
342 og_create_field(OG_AUDIENCE_FIELD, 'node', 'article', $og_field);
343
344 // Add a second audience field.
345 og_create_field('og_ref_2', 'node', 'article', $og_field);
346
347 $user1 = $this->drupalCreateUser();
348
349 $entity1 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
350 $wrapper = entity_metadata_wrapper('entity_test', $entity1);
351 $wrapper->{OG_GROUP_FIELD}->set(1);
352 $wrapper->save();
353
354 $entity2 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
355 $wrapper = entity_metadata_wrapper('entity_test', $entity2);
356 $wrapper->{OG_GROUP_FIELD}->set(1);
357 $wrapper->save();
358
359 $entity3 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
360 $wrapper = entity_metadata_wrapper('entity_test', $entity3);
361 $wrapper->{OG_GROUP_FIELD}->set(1);
362 $wrapper->save();
363
364 $settings = array();
365 $settings['type'] = 'article';
366
367 // Create group enteties.
368 foreach (og_group_content_states() as $state => $value) {
369 $node = $this->drupalCreateNode($settings);
370
371 // Assign article to the group.
372 $values = array('entity_type' => 'node', 'entity' => $node);
373 og_group('entity_test', $entity1->pid, $values + array('state' => $state));
374 // Subscribe node to a second group, but with a different state, by
375 // selecting the state code and incrementing by one (e.g. is the
376 // state is "active" then the other-state will be "pending").
377 $other_state = $state == OG_STATE_BLOCKED ? OG_STATE_ACTIVE : $state + 1;
378 $values += array('state' => $other_state);
379 og_group('entity_test', $entity2->pid, $values);
380
381 // Subscribe node to third group, using a diffferent field.
382 $values += array('field_name' => 'og_ref_2');
383 og_group('entity_test', $entity3->pid, $values);
384
385 $wrapper = entity_metadata_wrapper('node', $node->nid);
386 $this->assertEqual($wrapper->og_membership->count(), 3, t('Found all OG memberships.'));
387
388 $og_memberships = $wrapper->{'og_membership__' . $state}->value();
389 $this->assertEqual(count($og_memberships), 1, t('Found 1 OG membership with state @state.', array('@state' => $value)));
390 $this->assertEqual($og_memberships[0]->state, $state, t('OG membership has correct @state state.', array('@state' => $value)));
391
392 $og_memberships = $wrapper->{OG_AUDIENCE_FIELD . '__og_membership__' . $state}->value();
393 $this->assertEqual(count($og_memberships), 1, t('Found 1 OG membership with state @state in group-audience field.', array('@state' => $value)));
394 $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)));
395 }
396
397 $og_memberships = $wrapper->{OG_AUDIENCE_FIELD . '__og_membership'}->value();
398 $this->assertEqual(count($og_memberships), 2, t('Found 2 OG membership in group-audience field.', array('@state' => $value)));
399 $this->assertEqual($og_memberships[0]->field_name, OG_AUDIENCE_FIELD, t('OG membership has correct group-audience field.'));
400
401 $og_memberships = $wrapper->{'og_ref_2__og_membership'}->value();
402 $this->assertEqual(count($og_memberships), 1, t('Found 2 OG membership in second group-audience field.', array('@state' => $value)));
403 $this->assertEqual($og_memberships[0]->field_name, 'og_ref_2', t('OG membership has correct group-audience field.'));
404 }
405 }
406
407
408
409 /**
410 * Test Group content handeling.
411 */
412 class OgGroupAndUngroup extends DrupalWebTestCase {
413
414 public static function getInfo() {
415 return array(
416 'name' => 'OG group and ungroup',
417 'description' => 'Test the group and ungrouping of content with a group.',
418 'group' => 'Organic groups',
419 );
420 }
421
422 function setUp() {
423 parent::setUp('og', 'entity_feature');
424
425 // Add OG group field to the entity_test's "main" bundle.
426 og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
427
428 // Add OG audience field to the node's "article" bundle.
429 $og_field = og_fields_info(OG_AUDIENCE_FIELD);
430 $og_field['field']['settings']['target_type'] = 'entity_test';
431 og_create_field(OG_AUDIENCE_FIELD, 'node', 'article', $og_field);
432 }
433
434 /**
435 * Test group and ungroup of content.
436 */
437 function testGroupAndUngroup() {
438 $user1 = $this->drupalCreateUser();
439 $user2 = $this->drupalCreateUser();
440
441 $entity1 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
442 $wrapper = entity_metadata_wrapper('entity_test', $entity1);
443 $wrapper->{OG_GROUP_FIELD}->set(1);
444 $wrapper->save();
445
446 $entity2 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
447 $wrapper = entity_metadata_wrapper('entity_test', $entity2);
448 $wrapper->{OG_GROUP_FIELD}->set(1);
449 $wrapper->save();
450
451 $entity3 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
452 $wrapper = entity_metadata_wrapper('entity_test', $entity3);
453 $wrapper->{OG_GROUP_FIELD}->set(1);
454 $wrapper->save();
455
456 $settings = array();
457 $settings['type'] = 'article';
458 $settings['uid'] = $user2->uid;
459 $node = $this->drupalCreateNode($settings);
460
461 $this->assertFalse(og_is_member('entity_test', $entity1->pid, 'node', $node), t('Node is not assigned to group1.'));
462 $values = array('entity_type' => 'node', 'entity' => $node);
463 og_group('entity_test', $entity1->pid, $values);
464 $og_membership = og_get_membership('entity_test', $entity1->pid, 'node', $node->nid);
465 $id = $og_membership->id;
466 $this->assertTrue(og_is_member('entity_test', $entity1->pid, 'node', $node), t('Node is assigned to group1 with active state.'));
467
468 // State changed.
469 $values += array('state' => OG_STATE_BLOCKED);
470 og_group('entity_test', $entity1->pid, $values);
471 $og_membership = og_get_membership('entity_test', $entity1->pid, 'node', $node->nid);
472 $this->assertEqual($id, $og_membership->id, t('OG membership was updated.'));
473 $this->assertTrue(og_is_member('entity_test', $entity1->pid, 'node', $node, array(OG_STATE_BLOCKED)), t('Node is assigned to group1 with blocked state.'));
474
475 // Exception on existing OG membership.
476 try {
477 og_membership_create('entity_test', $entity1->pid, 'node', $node->nid, OG_AUDIENCE_FIELD)->save();
478 $this->fail('Saving multiple OG membership for same entity and group works.');
479 }
480 catch (OgException $e) {
481 $this->pass('Saving multiple OG membership for same entity and group does not work.');
482 }
483
484 // Add a second audience field.
485 $og_field = og_fields_info(OG_AUDIENCE_FIELD);
486 $og_field['field']['settings']['target_type'] = 'entity_test';
487 $og_field['field']['cardinality'] = 2;
488 og_create_field('og_ref_2', 'node', 'article', $og_field);
489
490 // Re-group to another field.
491 $values += array('field_name' => 'og_ref_2');
492 og_group('entity_test', $entity1->pid, $values);
493 $og_membership = og_get_membership('entity_test', $entity1->pid, 'node', $node->nid);
494 $this->assertNotEqual($id, $og_membership->id, t('OG membership was re-created.'));
495 $this->assertEqual('og_ref_2', $og_membership->field_name, t('OG membership is registered under correct field.'));
496
497
498 // Exception on field cardinality.
499 og_group('entity_test', $entity2->pid, $values);
500 try {
501 og_group('entity_test', $entity3->pid, $values);
502 $this->fail('Grouping beyond field cardinality works.');
503 }
504 catch (OgException $e) {
505 $this->pass('Grouping beyond field cardinality does not work.');
506 }
507
508 // Exception as field-name is incorrect.
509 $values['field_name'] = 'wrong-field-name';
510 try {
511 og_group('entity_test', $entity1->pid, $values);
512 $this->fail('Grouping with incorrect field name works.');
513 }
514 catch (OgException $e) {
515 $this->pass('Grouping with incorrect field name does not work.');
516 }
517
518 // Exception on audience field, referencing wrong target type.
519 $og_field = og_fields_info(OG_AUDIENCE_FIELD);
520 $og_field['field']['settings']['target_type'] = 'node';
521 og_create_field('og_ref_3', 'node', 'article', $og_field);
522 $values['field_name'] = 'og_ref_3';
523 try {
524 og_group('entity_test', $entity1->pid, $values);
525 $this->fail('Grouping with wrong target type works.');
526 }
527 catch (OgException $e) {
528 $this->pass('Grouping with wrong target type does not work.');
529 }
530
531 // Exception on audience field, referencing correct target type, but wrong
532 // bundles.
533 $og_field = og_fields_info(OG_AUDIENCE_FIELD);
534 $og_field['field']['settings']['target_type'] = 'entity_test';
535 $og_field['field']['settings']['handler_settings']['target_bundles'] = array('test');
536 og_create_field('og_ref_4', 'node', 'article', $og_field);
537 $values['field_name'] = 'og_ref_4';
538 try {
539 og_group('entity_test', $entity1->pid, $values);
540 $this->fail('Grouping with wrong target bundles works.');
541 }
542 catch (OgException $e) {
543 $this->pass('Grouping with wrong target bundles does not work.');
544 }
545
546
547 // Exception as user has no group-audience field.
548 $instance = field_info_instance('user', 'og_user_entity_test', 'user');
549 field_delete_instance($instance);
550
551 try {
552 $entity2 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
553 $wrapper = entity_metadata_wrapper('entity_test', $entity2);
554 $wrapper->{OG_GROUP_FIELD}->set(1);
555 $wrapper->save();
556 $this->fail('Grouping with no group-audience field in bundle works.');
557 }
558 catch (OgException $e) {
559 $this->pass('Grouping with no group-audience field in bundle does not work.');
560 }
561
562 // Ungroup node from group.
563 og_ungroup('entity_test', $entity1->pid, 'node', $node);
564 $og_membership = og_get_membership('entity_test', $entity1->pid, 'node', $node->nid);
565 $this->assertFalse($og_membership, t('Node was ungrouped from group.'));
566
567 // Delete node and confirm memberships were deleted.
568 $values = array('entity_type' => 'node', 'entity' => $node);
569 og_group('entity_test', $entity1->pid, $values);
570 $nid = $node->nid;
571
572 // Re-load node, to make sure we are deleting the most up-to-date one,
573 // after it was altered by og_group().
574 $node = node_load($nid, NULL, TRUE);
575 node_delete($nid);
576 $this->assertFalse(og_get_entity_groups('node', $nid), t('OG memberships deleted on entity deletion.'));
577 }
578
579 /**
580 * Test granting deault role to group manager.
581 */
582 function testGroupManagerDefaultRoles() {
583 // Get only the admin role.
584 $og_roles = og_roles('entity_test', 'main', 0, FALSE, FALSE);
585 variable_set('og_group_manager_default_rids_entity_test_main', array_keys($og_roles));
586 $user1 = $this->drupalCreateUser();
587
588 $entity1 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
589 $wrapper = entity_metadata_wrapper('entity_test', $entity1);
590 $wrapper->{OG_GROUP_FIELD}->set(1);
591 $wrapper->save();
592
593 $user_roles = og_get_user_roles('entity_test', $entity1->pid, $user1->uid, FALSE);
594 $this->assertEqual($og_roles, $user_roles, t('Group manager was granted default role.'));
595 }
596 }
597
598 class OgPermissionsTestCase extends DrupalWebTestCase {
599 public static function getInfo() {
600 return array(
601 'name' => 'OG permissions',
602 'description' => 'Verify that permissions can be added and removed via API.',
603 'group' => 'Organic groups'
604 );
605 }
606
607 function setUp() {
608 parent::setUp('og', 'entity_feature');
609 // Add OG group field to the entity_test's "main" bundle.
610 og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
611
612 // Add OG audience field to the node's "article" bundle.
613 $og_field = og_fields_info(OG_AUDIENCE_FIELD);
614 $og_field['field']['settings']['target_type'] = 'entity_test';
615 og_create_field(OG_AUDIENCE_FIELD, 'node', 'article', $og_field);
616 }
617
618
619 /**
620 * Verify proper permission changes by og_role_change_permissions().
621 */
622 function testOgUserRoleChangePermissions() {
623 // Create user.
624 $user1 = $this->drupalCreateUser();
625
626 // Create an entity.
627 $property = OG_GROUP_FIELD;
628 $entity = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
629 $wrapper = entity_metadata_wrapper('entity_test', $entity);
630 $wrapper->{OG_GROUP_FIELD}->set(1);
631 $wrapper->save();
632
633 // Associate user to the group.
634 $user2 = $this->drupalCreateUser();
635 $values = array('entity_type' => 'user', 'entity' => $user2);
636 og_group('entity_test', $entity->pid, $values);
637
638 // Assert the user is registered to the new group.
639 $this->assertTrue(og_is_member('entity_test', $entity->pid, 'user', $user2), t('User is registered to the new group.'));
640
641 // Verify current permissions.
642 $this->assertFalse(og_user_access('entity_test', $entity->pid, 'update own article content', $user2), t('User does not have "update own article content" permission.'));
643 $this->assertFalse(og_user_access('entity_test', $entity->pid, 'delete own article content', $user2), t('User does not have "delete own article content" permission.'));
644
645 // Change permissions to authenticated member.
646 $og_roles = array_flip(og_roles('entity_test', 'main', $entity->pid));
647 // Authenticated role ID.
648 $rid = $og_roles[OG_AUTHENTICATED_ROLE];
649
650 $permissions = array(
651 'delete own article content' => 1,
652 );
653 og_role_change_permissions($rid, $permissions);
654
655 // Verify proper permission changes.
656 $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.'));
657 $this->assertTrue(og_user_access('entity_test', $entity->pid, 'delete own article content', $user2), t('User now has "delete own article content" permission.'));
658
659 $permissions = array(
660 'delete own article content' => 0,
661 'administer group' => 1,
662 );
663 og_role_change_permissions($rid, $permissions);
664
665 $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.'));
666 $this->assertTrue(og_user_access('entity_test', $entity->pid, 'administer group', $user2), t('User has "administer group" permission.'));
667 }
668 }
669
670 class OgDefaultAccessFieldTestCase extends DrupalWebTestCase {
671 public static function getInfo() {
672 return array(
673 'name' => 'OG default access field',
674 'description' => 'Test groups with default access field.',
675 'group' => 'Organic groups'
676 );
677 }
678
679 function setUp() {
680 parent::setUp('og', 'entity_feature');
681 }
682
683
684 /**
685 * Test groups with default access field enabled or disabled.
686 */
687 function testOgDefaultAccessField() {
688 // Create user.
689 $user1 = $this->drupalCreateUser();
690
691 // Add OG group field to the entity_test's "main" bundle.
692 og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
693
694 $og_roles = og_roles('entity_test', 'main');
695
696 // Group without default access field.
697 $entity = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
698 $wrapper = entity_metadata_wrapper('entity_test', $entity);
699 $wrapper->{OG_GROUP_FIELD}->set(1);
700 $wrapper->save();
701 $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.'));
702
703 // Add default access field to the entity_test's "main" bundle.
704 og_create_field(OG_DEFAULT_ACCESS_FIELD, 'entity_test', 'main');
705
706 // Group with default access field disabled.
707 $entity = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
708 $wrapper = entity_metadata_wrapper('entity_test', $entity);
709 $wrapper->{OG_GROUP_FIELD}->set(1);
710 $wrapper->{OG_DEFAULT_ACCESS_FIELD}->set(0);
711 $wrapper->save();
712 $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.'));
713
714 // Add admin role to a user.
715 $rid = array_search(OG_ADMINISTRATOR_ROLE, $og_roles);
716 og_role_grant('entity_test', $entity->pid, $user1->uid, $rid);
717 $user_roles = og_get_user_roles('entity_test', $entity->pid, $user1->uid);
718 $this->assertTrue(array_search(OG_ADMINISTRATOR_ROLE, $user_roles), t('User has default "admin" role.'));
719
720 // Group with default access field enabled.
721 $wrapper->{OG_DEFAULT_ACCESS_FIELD}->set(1);
722 $wrapper->save();
723 $new_og_roles = og_roles('entity_test', 'main', $entity->pid);
724 $this->assertNotEqual($og_roles, $new_og_roles, t('Group with default access field enabled has own roles and permissions settings.'));
725
726 // Assert the newley created admin role was mapped to the default one.
727 $user_roles = og_get_user_roles('entity_test', $entity->pid, $user1->uid);
728 $this->assertTrue(array_search(OG_ADMINISTRATOR_ROLE, $user_roles), t('User has overriden "admin" role.'));
729
730 // Disable existing group's default access field.
731 $wrapper->{OG_DEFAULT_ACCESS_FIELD}->set(0);
732 $wrapper->save();
733 $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.'));
734 }
735 }
736
737 /**
738 * Upgrade 7000 test.
739 *
740 * Load a filled installation of Drupal 6 and run the upgrade on it.
741 *
742 * TODO: We have to use $this->drupalGet('node/' . $nid); to proerly load
743 * the node data, otherwise. We should understand why this is needed, and
744 * remove it.
745 */
746 class OgMigrate7000TestCase extends UpgradePathTestCase {
747 public static function getInfo() {
748 return array(
749 'name' => 'OG migrate - 7000',
750 'description' => 'Tests the upgrade path of OG from Drupal 6.',
751 'group' => 'Organic groups',
752 // TODO: Why do we need to enable Views?! - otherwise we get WSOD.
753 'dependencies' => array('migrate', 'views'),
754 );
755 }
756
757 public function setUp() {
758 // Path to the database dump.
759 $this->databaseDumpFiles = array(
760 drupal_get_path('module', 'og') . '/tests/drupal-6.og.database.php',
761 );
762 parent::setUp();
763 $this->assertTrue($this->performUpgrade(), t('The upgrade was completed successfully.'));
764
765 // spl_autoload_register() wasn't called, so we do it here, to allow
766 // classes to be auto-loaded.
767 spl_autoload_register('drupal_autoload_class');
768 spl_autoload_register('drupal_autoload_interface');
769
770 // TODO: Why do we need to enable Views?!
771 module_enable(array('og', 'views', 'migrate'));
772
773 $class_names = array(
774 'OgMigrateAddFields',
775 'OgMigrateUser',
776 'OgMigrateContent',
777 );
778
779 // FIXME: migrate_flush_caches() crashes, so we register manually.
780 foreach ($class_names as $class_name) {
781 MigrationBase::registerMigration($class_name);
782 }
783
784 // Register a dynamic migration.
785 MigrationBase::registerMigration('OgMigrateGroup', 'OgMigrateGroupTest_group', array('bundle' => 'test_group'));
786
787 $migration = Migration::getInstance('OgMigrateAddFields');
788 $result = $migration->processImport();
789 $this->assertEqual($result, Migration::RESULT_COMPLETED, 'Migration OgMigrateAddFields executed.');
790
791 $migration = Migration::getInstance('OgMigrateGroupTest_group', 'OgMigrateGroup', array('bundle' => 'test_group'));
792 $result = $migration->processImport();
793 $this->assertEqual($result, Migration::RESULT_COMPLETED, 'Migration OgMigrateGroupTest_group executed.');
794
795 $migration = Migration::getInstance('OgMigrateUser');
796 $result = $migration->processImport();
797 $this->assertEqual($result, Migration::RESULT_COMPLETED, 'Migration OgMigrateUser executed.');
798
799 $migration = Migration::getInstance('OgMigrateContent');
800 $result = $migration->processImport();
801 $this->assertEqual($result, Migration::RESULT_COMPLETED, 'Migration OgMigrateContent executed.');
802 }
803
804 /**
805 * Test a successful group upgrade.
806 *
807 * @see og_7000_group()
808 */
809 public function testGroup() {
810 // Assert according to the scenario Drupal 6's test table dump was created.
811 foreach (array(1, 2) as $nid) {
812 $this->drupalGet('node/' . $nid);
813 $node = node_load($nid);
814 $this->assertTrue($node->{OG_GROUP_FIELD}[LANGUAGE_NONE][0]['value'], t('Node ID @nid is an active group.', array('@nid' => $nid)));
815 }
816
817 // Test group content with NID 3 - 5 belong to the group with NID 2.
818 foreach (range(3, 5) as $nid) {
819 $this->drupalGet('node/' . $nid);
820 $node = node_load($nid);
821 $this->assertTrue(og_is_member('node', 2, 'node', $node), t('Node ID @nid is a group content of Node ID 2', array('@nid' => $nid)));
822 }
823
824 // Orphan group content (i.e. not attached to a group).
825 $node = node_load(6);
826 $this->assertFalse(og_get_entity_groups('node', $node), t('Node ID 6 is not associated with any group.'));
827
828 // Group content that shares the same group.
829 $node = node_load(9);
830 foreach (array(7, 8) as $nid) {
831 $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)));
832 }
833 }
834
835 /**
836 * Test user upgrade.
837 *
838 * @see og_7000_user()
839 */
840 public function testUser() {
841 // Assert users.
842 $values = array(
843 // Uid 3 is the group manager, so in OG6 it was marked as admin.
844 3 => array('admin' => TRUE),
845 4 => array('active' => FALSE),
846 5 => array(),
847 6 => array('active' => FALSE, 'admin' => TRUE),
848 7 => array('admin' => TRUE),
849 );
850
851 $og_roles = og_roles('node', 'test_group');
852
853 foreach ($values as $uid => $value) {
854 $account = user_load($uid);
855
856 // Set default values.
857 $value += array('active' => TRUE, 'admin' => FALSE);
858 $roles = array();
859
860 if ($value['active']) {
861 $op = 'active';
862 $states = array(OG_STATE_ACTIVE);
863 $rid = array_search(OG_AUTHENTICATED_ROLE, $og_roles);
864 }
865 else {
866 $op = 'pending';
867 $states = array(OG_STATE_PENDING);
868 // If the member is pending then they have the anonymous role.
869 $rid = array_search(OG_ANONYMOUS_ROLE, $og_roles);
870 }
871
872 $roles[$rid] = TRUE;
873
874 if ($value['admin']) {
875 // OG_ADMINISTRATOR_ROLE
876 $rid = array_search(OG_ADMINISTRATOR_ROLE, $og_roles);
877 $roles[$rid] = TRUE;
878 }
879
880 $this->assertTrue(og_is_member('node', 10, 'user', $account, $states), format_string('User @uid is @op member in group.', array('@uid' => $uid, '@op' => $op)));
881 // Pass also pending state, so we make sure that even if the user
882 // isn't active they are considered members, to check they get the
883 // correct role.
884 $this->assertEqual(array_keys(og_get_user_roles('node', 10, $uid)), array_keys($roles), format_string('User @uid has the correct roles in group.', array('@uid' => $uid)));
885 }
886 }
887
888 /**
889 * Test group description upgrade.
890 *
891 * @see og_7000_group()
892 */
893 public function testGroupDescription() {
894 // Assert description was converted to a field.
895 foreach (array(1, 2, 7, 8, 10) as $nid) {
896 $this->drupalGet('node/' . $nid);
897 $node = node_load($nid);
898 $this->assertTrue($node->og_description[LANGUAGE_NONE][0]['value'], t('Description fields has correct data.'));
899 }
900 }
901 }
902
903
904 /**
905 * Test the complex widget.
906 */
907 class OgComplexWidgetTestCase extends DrupalWebTestCase {
908
909 public static function getInfo() {
910 return array(
911 'name' => 'OG reference widget',
912 'description' => 'Test the OG reference widget behavior.',
913 'group' => 'Organic groups',
914 );
915 }
916
917 function setUp() {
918 parent::setUp('og');
919
920 // Add OG group field to a the node's "group" bundle.
921 $this->drupalCreateContentType(array('type' => 'group'));
922 og_create_field(OG_GROUP_FIELD, 'node', 'group');
923
924 // Add OG audience field to the node's "post" bundle.
925 $this->drupalCreateContentType(array('type' => 'post'));
926 $og_field = og_fields_info(OG_AUDIENCE_FIELD);
927 $og_field['instance']['required'] = TRUE;
928 og_create_field(OG_AUDIENCE_FIELD, 'node', 'post', $og_field);
929 }
930
931 /**
932 * Test "field modes" of the OG reference widget.
933 */
934 function testFieldModes() {
935 $user1 = $this->drupalCreateUser(array('administer group', 'access content', 'create post content'));
936 $user2 = $this->drupalCreateUser(array('access content', 'create post content'));
937
938 // Create group nodes.
939 $settings = array(
940 'type' => 'group',
941 OG_GROUP_FIELD . '[und][0][value]' => 1,
942 );
943 $settings['uid'] = $user1->uid;
944 $group1 = $this->drupalCreateNode($settings);
945
946 $settings['uid'] = $user2->uid;
947 $group2 = $this->drupalCreateNode($settings);
948
949 $settings = array(
950 'type' => 'post',
951 );
952 $settings['uid'] = $user1->uid;
953 $post1 = $this->drupalCreateNode($settings);
954 og_group('node', $group1->nid, array('entity_type' => 'node', 'entity' => $post1));
955
956 $settings['uid'] = $user2->uid;
957 $post2 = $this->drupalCreateNode($settings);
958 og_group('node', $group2->nid, array('entity_type' => 'node', 'entity' => $post2));
959
960 $this->drupalLogin($user1);
961 $this->drupalGet("node/$post1->nid/edit");
962
963 $fields = $this->xpath('//*[@id="edit-og-group-ref-und-0-default"]');
964 $this->assertEqual($fields[0]->option['value'], '_none', '"Default" field mode is not required for administrator.');
965
966 $fields = $this->xpath('//*[@id="edit-og-group-ref-und-0-admin-0-target-id"]');
967 $this->assertTrue(strpos($fields[0]->attributes()->class[0], 'form-autocomplete'), '"Administrator field more is an autocomplete widget type."');
968
969 $this->drupalLogin($user2);
970 $this->drupalGet("node/$post2->nid/edit");
971
972 $fields = $this->xpath('//*[@id="edit-og-group-ref-und-0-default"]');
973 $this->assertEqual($fields[0]->option['value'], $group2->nid, '"Default" field mode is required.');
974 }
975
976 /**
977 * Test non-accessible group IDs are saved, upon form submit.
978 */
979 function testHiddenGroupIds() {
980 $user1 = $this->drupalCreateUser(array('administer group', 'access content', 'create post content'));
981 $user2 = $this->drupalCreateUser(array('access content', 'create post content'));
982
983 // Create group nodes.
984 $settings = array(
985 'type' => 'group',
986 OG_GROUP_FIELD . '[und][0][value]' => 1,
987 );
988 $settings['uid'] = $user1->uid;
989 $group1 = $this->drupalCreateNode($settings);
990
991 $settings['uid'] = $user2->uid;
992 $group2 = $this->drupalCreateNode($settings);
993
994 $settings = array(
995 'type' => 'post',
996 );
997 $settings['uid'] = $user1->uid;
998 $post1 = $this->drupalCreateNode($settings);
999 og_group('node', $group1->nid, array('entity_type' => 'node', 'entity' => $post1));
1000 og_group('node', $group2->nid, array('entity_type' => 'node', 'entity' => $post1));
1001
1002 $this->drupalLogin($user2);
1003 $this->drupalPost("node/$post1->nid/edit", array(), 'Save');
1004
1005 // Assert post still belongs to both groups, although user was able
1006 // to select only one.
1007 $gids = og_get_entity_groups('node', $post1);
1008 $this->assertEqual(count($gids['node']), 2, 'Hidden groups remained.');
1009 }
1010
1011 /**
1012 * Test a non "administer group" user with pending membership, re-saving
1013 * user edit.
1014 */
1015 function testUserEdit() {
1016 $user1 = $this->drupalCreateUser();
1017 $user2 = $this->drupalCreateUser();
1018
1019 $settings = array(
1020 'type' => 'group',
1021 OG_GROUP_FIELD . '[und][0][value]' => 1,
1022 );
1023 $settings['uid'] = $user1->uid;
1024 $group1 = $this->drupalCreateNode($settings);
1025
1026 og_group('node', $group1->nid, array('entity' => $user2, 'state' => OG_STATE_PENDING));
1027
1028 $this->drupalLogin($user2);
1029 $this->drupalPost("user/$user2->uid/edit", array(), 'Save');
1030
1031 $this->assertTrue(og_get_entity_groups('user', $user2, array(OG_STATE_PENDING)), 'User membership was retained after user save.');
1032 }
1033
1034 /**
1035 * Test multiple group-audience fields.
1036 */
1037 function testMultipleFields() {
1038 // Add another group-audience field.
1039 $og_field = og_fields_info(OG_AUDIENCE_FIELD);
1040 og_create_field('another_field', 'node', 'post', $og_field);
1041
1042 $user1 = $this->drupalCreateUser();
1043
1044 // Create a group.
1045 $settings = array(
1046 'type' => 'group',
1047 OG_GROUP_FIELD . '[und][0][value]' => 1,
1048 'uid' => $user1->uid,
1049 );
1050 $group1 = $this->drupalCreateNode($settings);
1051 $group2 = $this->drupalCreateNode($settings);
1052
1053 // Create group content.
1054 $settings = array(
1055 'type' => 'post',
1056 'uid' => $user1->uid,
1057 );
1058 $post1 = $this->drupalCreateNode($settings);
1059
1060 og_group('node', $group1->nid, array('entity_type' => 'node', 'entity' => $post1, 'field_name' => OG_AUDIENCE_FIELD));
1061 og_group('node', $group2->nid, array('entity_type' => 'node', 'entity' => $post1, 'field_name' => 'another_field'));
1062
1063 $this->drupalLogin($user1);
1064 $this->drupalGet("node/$post1->nid/edit");
1065
1066 // Assert correct selection in both fields.
1067 $this->assertOptionSelected('edit-og-group-ref-und-0-default', $group1->nid);
1068 $this->assertOptionSelected('edit-another-field-und-0-default', $group2->nid);
1069 }
1070 }
1071
1072 /**
1073 * Test the revocation of group roles.
1074 */
1075 class OgRoleRevoke extends DrupalWebTestCase {
1076
1077 public static function getInfo() {
1078 return array(
1079 'name' => 'OG roles revoke',
1080 'description' => 'Test the revocation of group roles',
1081 'group' => 'Organic groups',
1082 );
1083 }
1084
1085 function setUp() {
1086 parent::setUp('og', 'entity_feature');
1087 }
1088
1089 function testOgRoleRevoke() {
1090 // Create a user.
1091 $user1 = $this->drupalCreateUser();
1092
1093 // Add OG group field to the entity_test's "main" bundle.
1094 og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
1095
1096 // Create two groups entity1 and entity2.
1097 $entity1 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
1098 $wrapper = entity_metadata_wrapper('entity_test', $entity1);
1099 $wrapper->{OG_GROUP_FIELD}->set(1);
1100 $wrapper->save();
1101
1102 $entity2 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
1103 $wrapper = entity_metadata_wrapper('entity_test', $entity2);
1104 $wrapper->{OG_GROUP_FIELD}->set(1);
1105 $wrapper->save();
1106
1107 // Create a role named 'role1'.
1108 $role1 = og_role_create('role1', 'entity_test', 0, 'main');
1109 og_role_save($role1);
1110
1111 // Create a role named 'role2'.
1112 $role2 = og_role_create('role2', 'entity_test', 0, 'main');
1113 og_role_save($role2);
1114
1115 // Grant 'role1' to user1 at entity1 and 'role2' to user1 at entity2
1116 og_role_grant('entity_test', $entity1->pid, $user1->uid, $role1->rid);
1117 og_role_grant('entity_test', $entity2->pid, $user1->uid, $role2->rid);
1118
1119 // Unsubscribe user1 from entity1.
1120 og_ungroup('entity_test', $entity1->pid, 'user', $user1->uid);
1121
1122 $this->assertFalse(og_get_user_roles('entity_test', $entity1->pid, $user1->uid, FALSE), t('User is unsubscribed from group, so role was revoked'));
1123 $this->assertTrue(og_get_user_roles('entity_test', $entity2->pid, $user1->uid, FALSE), t('User is still subscribed to group, so return value is not empty'));
1124
1125 $uid = $user1->uid;
1126 // Delete user1.
1127 user_delete($user1->uid);
1128
1129 $result = db_query('SELECT * FROM {og_users_roles} WHERE uid = :uid', array(':uid' => $uid));
1130 $this->assertFalse($result->rowCount(), t('User is removed, so all roles of this user were revoked'));
1131 }
1132 }
1133
1134 /**
1135 * Test Upgrade from branch 1.x to 2.x.
1136 */
1137 class OgMigrate7200TestCase extends UpdatePathTestCase {
1138
1139 public static function getInfo() {
1140 return array(
1141 'name' => 'OG migrate 7.x-2.x',
1142 'description' => 'Test the upgrade from 7.x-1.x to 7.x-2.x branch.',
1143 'group' => 'Organic groups',
1144 'dependencies' => array('migrate'),
1145 );
1146 }
1147
1148 function setUp() {
1149 // Path to the database dump files.
1150 $this->databaseDumpFiles = array(
1151 drupal_get_path('module', 'og') . '/tests/og-7.x-1.x.database.php',
1152 );
1153
1154 parent::setUp();
1155
1156 // FIXME: Since the DB dump holds group-audience field instances,
1157 // we need to delete them, otherwise this will create notices in
1158 // resulting from OG no longer defining hook_field_info().
1159 // Deleting using field_delete_instance() didn't work.
1160 db_delete('field_config_instance')
1161 ->condition('field_name', 'group_audience')
1162 ->execute();
1163 }
1164
1165 function testUpgrade() {
1166 $this->assertTrue($this->performUpgrade(), t('The upgrade was completed successfully.'));
1167
1168 $this->assertFalse(field_info_field('group_audience'), 'Group audience field was deleted.');
1169
1170 // spl_autoload_register() wasn't called, so we do it here, to allow
1171 // classes to be auto-loaded.
1172 spl_autoload_register('drupal_autoload_class');
1173 spl_autoload_register('drupal_autoload_interface');
1174
1175 module_enable(array('og', 'migrate'));
1176
1177 // FIXME: migrate_flush_caches() crashes, so we register manually.
1178 MigrationBase::registerMigration('OgMigrateMembership');
1179 MigrationBase::registerMigration('OgMigrateRoles');
1180 MigrationBase::registerMigration('OgMigrateUserRoles');
1181
1182 $migration = Migration::getInstance('OgMigrateMembership');
1183 $result = $migration->processImport();
1184 $this->assertEqual($result, Migration::RESULT_COMPLETED, 'OgMigrateMembership returned RESULT_COMPLETED');
1185
1186 $migration = Migration::getInstance('OgMigrateRoles');
1187 $result = $migration->processImport();
1188 $this->assertEqual($result, Migration::RESULT_COMPLETED, 'OgMigrateRoles returned RESULT_COMPLETED');
1189
1190 $migration = Migration::getInstance('OgMigrateUserRoles');
1191 $result = $migration->processImport();
1192 $this->assertEqual($result, Migration::RESULT_COMPLETED, 'OgMigrateUserRoles returned RESULT_COMPLETED');
1193
1194 // Assert group roles.
1195 $roles = array(
1196 'non-member',
1197 'member',
1198 'administrator member',
1199 );
1200 $this->assertEqual(array_values(og_roles('node', 'school', 1)), $roles, 'Returned expected roles group first group.');
1201
1202 $roles[] = 'new role';
1203 $this->assertEqual(array_values(og_roles('node', 'school', 2)), $roles, 'Returned expected roles group second group.');
1204
1205 // Assert field name was registered in the OG membership.
1206 $og_membership = entity_load_single('og_membership', 1);
1207 $this->assertEqual($og_membership->field_name, 'og_node', 'Field name was registered in the OG membership.');
1208 }
1209 }
1210
1211 /**
1212 * Test queying group-audience fields using entityFieldQuery.
1213 */
1214 class OgEntityFieldQueryTestCase extends DrupalWebTestCase {
1215
1216 public static function getInfo() {
1217 return array(
1218 'name' => 'OG audience fields query',
1219 'description' => 'Test querying group-audience fields using entityFieldQuery.',
1220 'group' => 'Organic groups',
1221 );
1222 }
1223
1224 function setUp() {
1225 parent::setUp('og', 'entity_feature');
1226
1227 $user1 = $this->drupalCreateUser();
1228 $user2 = $this->drupalCreateUser();
1229 $type = $this->drupalCreateContentType();
1230 $group_type = $type->type;
1231
1232 $type = $this->drupalCreateContentType();
1233 $group_content_type = $type->type;
1234
1235 og_create_field(OG_GROUP_FIELD, 'node', $group_type);
1236 og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
1237
1238 // Add audience field to reference node.
1239 $og_field = og_fields_info(OG_AUDIENCE_FIELD);
1240 og_create_field('og_node', 'node', $group_content_type, $og_field);
1241 og_create_field('og_node', 'entity_test', 'test', $og_field);
1242
1243 // Add audience field to reference entity-test.
1244 $og_field['field']['settings']['target_type'] = 'entity_test';
1245 og_create_field('og_entity_test', 'node', $group_content_type, $og_field);
1246 og_create_field('og_entity_test', 'user', 'user', $og_field);
1247
1248
1249 // Create a non-group audience, entity-refence field.
1250 $field = array(
1251 'entity_types' => array('node'),
1252 'settings' => array(
1253 'handler' => 'base',
1254 'target_type' => 'node',
1255 'handler_settings' => array(
1256 'target_bundles' => array(),
1257 ),
1258 ),
1259 'field_name' => 'node_reference',
1260 'type' => 'entityreference',
1261 'cardinality' => 1,
1262 );
1263 $field = field_create_field($field);
1264 $instance = array(
1265 'field_name' => 'node_reference',
1266 'bundle' => $group_content_type,
1267 'entity_type' => 'node',
1268 );
1269 field_create_instance($instance);
1270
1271 // Create two groups.
1272 $group1 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
1273 $wrapper = entity_metadata_wrapper('entity_test', $group1);
1274 $wrapper->{OG_GROUP_FIELD}->set(1);
1275 $wrapper->save();
1276
1277 $settings = array(
1278 'type' => $group_type,
1279 'uid' => $user1->uid,
1280 );
1281 $settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 1;
1282 $group2 = $this->drupalCreateNode($settings);
1283
1284 // Create group-content.
1285 $settings = array(
1286 'type' => $group_content_type,
1287 'uid' => $user1->uid,
1288 );
1289 $node = $this->drupalCreateNode($settings);
1290
1291 $wrapper = entity_metadata_wrapper('node', $node);
1292 $wrapper->node_reference->set($group2);
1293 $wrapper->save();
1294
1295 $values = array(
1296 'entity_type' => 'node',
1297 'entity' => $node,
1298 );
1299
1300 og_group('entity_test', $group1, $values);
1301 og_group('node', $group2, $values);
1302
1303 $entity_test = entity_create('entity_test', array('name' => 'test', 'uid' => $user1->uid));
1304 $entity_test->save();
1305 $values = array(
1306 'entity_type' => 'entity_test',
1307 'entity' => $entity_test,
1308 );
1309 og_group('node', $group2, $values);
1310
1311 $values = array(
1312 'entity_type' => 'user',
1313 'entity' => $user2,
1314 );
1315 og_group('node', $group2, $values);
1316
1317 $this->group1 = $group1;
1318 $this->group2 = $group2;
1319 $this->node = $node;
1320 $this->user1 = $user1;
1321 $this->user2 = $user2;
1322 $this->entity_test = $entity_test;
1323 }
1324
1325 /**
1326 * Test the following query scenarios:
1327 *
1328 * - Single group audience.
1329 * - Multiple group audience.
1330 * - Single group audience first, with another non-audience field.
1331 * - Non-audience field first, with single group audience.
1332 * - Multiple entity types in entityCondition().
1333 * - No entity property.
1334 * - Non-node entity without revision table (e.g. entity_test).
1335 * - Non-node entity without revision table and without bundles (e.g. user).
1336 * - Count query.
1337 */
1338 function testEntityFieldQuery() {
1339 $group1 = $this->group1;
1340 $group2 = $this->group2;
1341 $node = $this->node;
1342 $user1 = $this->user1;
1343 $entity_test = $this->entity_test;
1344
1345 // Single group audience.
1346 $query = new EntityFieldQuery();
1347 $result = $query
1348 ->entityCondition('entity_type', 'node')
1349 ->propertyCondition('type', $node->type)
1350 ->fieldCondition('og_node', 'target_id', $group2->nid)
1351 ->execute();
1352
1353 $this->assertEqual(array_keys($result['node']), array($node->nid), 'Single group audience query is correct.');
1354
1355 // Multiple group audience.
1356 $query = new EntityFieldQuery();
1357 $result = $query
1358 ->entityCondition('entity_type', 'node')
1359 ->propertyCondition('type', $node->type)
1360 ->fieldCondition('og_node', 'target_id', $group2->nid)
1361 ->fieldCondition('og_entity_test', 'target_id', $group1->pid)
1362 ->execute();
1363
1364 $this->assertEqual(array_keys($result['node']), array($node->nid), 'Multiple group audience query is correct.');
1365
1366 // Single group audience first, with another non-audience field.
1367 $query = new EntityFieldQuery();
1368 $result = $query
1369 ->entityCondition('entity_type', 'node')
1370 ->propertyCondition('type', $node->type)
1371 ->fieldCondition('og_node', 'target_id', $group2->nid)
1372 ->fieldCondition('node_reference', 'target_id', $group2->nid)
1373 ->execute();
1374
1375 $this->assertEqual(array_keys($result['node']), array($node->nid), 'Single group audience first, with another non-audience field query is correct.');
1376
1377 // Non-audience field first, with single group audience.
1378 $query = new EntityFieldQuery();
1379 $result = $query
1380 ->entityCondition('entity_type', 'node')
1381 ->propertyCondition('type', $node->type)
1382 ->fieldCondition('node_reference', 'target_id', $group2->nid)
1383 ->fieldCondition('og_node', 'target_id', $group2->nid)
1384 ->execute();
1385
1386 $this->assertEqual(array_keys($result['node']), array($node->nid), 'Non-audience field first, with single group audience query is correct.');
1387
1388 // Multiple entity types in entityCondition().
1389 $query = new EntityFieldQuery();
1390 $result = $query
1391 ->entityCondition('entity_type', array('node', 'user'), 'IN')
1392 ->fieldCondition('node_reference', 'target_id', $group2->nid)
1393 ->fieldCondition('og_node', 'target_id', $group2->nid)
1394 ->execute();
1395
1396 $this->assertEqual(array_keys($result['node']), array($node->nid), 'Multiple entity types in entityCondition() query is correct.');
1397
1398 // No entity property.
1399 $query = new EntityFieldQuery();
1400 $result = $query
1401 ->entityCondition('entity_type', 'node')
1402 ->fieldCondition('og_node', 'target_id', $group2->nid)
1403 ->execute();
1404
1405 $this->assertEqual(array_keys($result['node']), array($node->nid), 'No entity property query is correct.');
1406
1407 // Non-node entity without revision table.
1408 $query = new EntityFieldQuery();
1409 $result = $query
1410 ->entityCondition('entity_type', 'entity_test')
1411 ->fieldCondition('og_node', 'target_id', $group2->nid)
1412 ->execute();
1413
1414 $this->assertEqual(array_keys($result['entity_test']), array($entity_test->pid), 'Non-node entity without revision table query is correct.');
1415
1416 // Non-node entity without revision table and without bundles.
1417 $query = new EntityFieldQuery();
1418 $result = $query
1419 ->entityCondition('entity_type', 'user')
1420 ->fieldCondition('og_entity_test', 'target_id', $group2->nid)
1421 ->execute();
1422
1423 $expected_values = array(
1424 $this->user1->uid,
1425 $this->user2->uid,
1426 );
1427 $this->assertEqual(array_keys($result['user']), $expected_values, 'Non-node entity without revision table and without bundles query is correct.');
1428
1429 // Count query.
1430 $query = new EntityFieldQuery();
1431 $result = $query
1432 ->entityCondition('entity_type', 'node')
1433 ->propertyCondition('type', $node->type)
1434 ->fieldCondition('og_node', 'target_id', $group2->nid)
1435 ->count()
1436 ->execute();
1437
1438 $this->assertEqual($result, 1, 'Count query is correct.');
1439 }
1440 }
1441
1442 /**
1443 * Test group-audience field auto-create.
1444 */
1445 class OgAudienceFieldAutoCreateTestCase extends DrupalWebTestCase {
1446
1447 public static function getInfo() {
1448 return array(
1449 'name' => 'OG audience fields auto-create',
1450 'description' => 'Test auto attaching a group-audience field to the user entity, if needed, upon adding a new group field.',
1451 'group' => 'Organic groups',
1452 );
1453 }
1454
1455 function setUp() {
1456 parent::setUp('og');
1457 }
1458
1459 /**
1460 * Test auto-attaching group-audience fields to the user entity.
1461 */
1462 function testAutoAttach() {
1463 $type1 = $this->drupalCreateContentType();
1464 $type2 = $this->drupalCreateContentType();
1465
1466 $this->assertFalse(field_info_instance('user', 'og_user_node', 'user'), 'Field does not exist in user entity yet.');
1467 og_create_field(OG_GROUP_FIELD, 'node', $type1->type);
1468 $this->assertTrue(field_info_instance('user', 'og_user_node', 'user'), 'Field was added to the user entity.');
1469
1470 // Change field to reference only type1.
1471 $field = field_info_field('og_user_node');
1472 $field['settings']['handler_settings']['target_bundles'] = array($type1->type);
1473 field_update_field($field);
1474
1475 // Assert an alternative field name was found.
1476 $this->assertFalse(field_info_instance('user', 'og_user_node1', 'user'), 'Alternative field does not exist in user entity yet.');
1477 og_create_field(OG_GROUP_FIELD, 'node', $type2->type);
1478 $this->assertTrue(field_info_instance('user', 'og_user_node1', 'user'), 'Alternative field was added to the user entity.');
1479 }
1480 }
1481
1482
1483 class OgBehaviorHandlerTestCase extends DrupalWebTestCase {
1484
1485 public static function getInfo() {
1486 return array(
1487 'name' => 'OG widget behavior',
1488 'description' => 'Test the widget behavior of group-audience fields.',
1489 'group' => 'Organic groups',
1490 );
1491 }
1492
1493 function setUp() {
1494 parent::setUp('og_test', 'entity_feature');
1495
1496 // Add OG group field to the entity_test's "main" bundle.
1497 og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
1498
1499 $type = $this->drupalCreateContentType(array('type' => 'behavior'));
1500 $this->group_content = $type->type;
1501
1502 // Add OG audience field to the new bundle.
1503 $og_field = og_fields_info(OG_AUDIENCE_FIELD);
1504 $og_field['field']['settings']['target_type'] = 'entity_test';
1505 og_create_field(OG_AUDIENCE_FIELD, 'node', $type->type, $og_field);
1506 }
1507
1508 /**
1509 * Test piping group association via the group-audience field.
1510 */
1511 function testGroupAudienceField() {
1512 $user1 = $this->drupalCreateUser();
1513 $user2 = $this->drupalCreateUser();
1514
1515 $entity1 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
1516 $wrapper = entity_metadata_wrapper('entity_test', $entity1);
1517 $wrapper->{OG_GROUP_FIELD}->set(1);
1518 $wrapper->save();
1519
1520 $settings = array();
1521 $settings['type'] = $this->group_content;
1522 $settings['uid'] = $user2->uid;
1523 $node = $this->drupalCreateNode($settings);
1524
1525 $wrapper = entity_metadata_wrapper('node', $node);
1526
1527 $this->assertFalse(og_is_member('entity_test', $entity1->pid, 'node', $node), t('Node is not assigned to group1.'));
1528 $wrapper->{OG_AUDIENCE_FIELD}[] = $entity1->pid;
1529 $wrapper->save();
1530 $og_membership = og_get_membership('entity_test', $entity1->pid, 'node', $node->nid);
1531 $id = $og_membership->id;
1532 $this->assertTrue(og_is_member('entity_test', $entity1->pid, 'node', $node), t('Node is assigned to group1 with active state.'));
1533
1534 $wrapper->{OG_AUDIENCE_FIELD}->set(NULL);
1535 $wrapper->save();
1536 $this->assertFalse(og_get_entity_groups('node', $node), t('Node is not associated with any group.'));
1537 }
1538
1539 /**
1540 * Test skipping OgBehaviorHandler.
1541 */
1542 function testGroupAudienceFieldSkipBehavior() {
1543 $user1 = $this->drupalCreateUser();
1544 $user2 = $this->drupalCreateUser();
1545
1546 $entity1 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
1547 $wrapper = entity_metadata_wrapper('entity_test', $entity1);
1548 $wrapper->{OG_GROUP_FIELD}->set(1);
1549 $wrapper->save();
1550
1551 $settings = array();
1552 $settings['type'] = $this->group_content;
1553 $settings['uid'] = $user2->uid;
1554 $node = $this->drupalCreateNode($settings);
1555
1556 og_group('entity_test', $entity1, array('entity_type' => 'node', 'entity' => $node));
1557 $node->og_group_on_save = array('group_type' => 'entity_test', 'gid' => $entity1->pid);
1558 node_save($node);
1559
1560 $this->assertFalse(og_get_entity_groups('node', $node), 'Widget behavior removed group association as expected.');
1561
1562 $node = node_load($node->nid);
1563 $node->og_group_on_save = array('group_type' => 'entity_test', 'gid' => $entity1->pid);
1564 $node->skip_og_membership = TRUE;
1565 node_save($node);
1566
1567 $gids = og_get_entity_groups('node', $node);
1568 $this->assertEqual(array_values($gids['entity_test']), array($entity1->pid), 'Widget behavior was skipped and removed group association as expected.');
1569 }
1570
1571 /**
1572 * Test settings the OG membership state via field values, when associating
1573 * a new group-content to a group.
1574 */
1575 function testSetStateOnInsert() {
1576 module_enable(array('og_test'));
1577 $permissions = array(
1578 'access content',
1579 "create $this->group_content content",
1580 'administer group',
1581 );
1582 $user1 = $this->drupalCreateUser();
1583 $user2 = $this->drupalCreateUser($permissions);
1584 $user3 = $this->drupalCreateUser($permissions);
1585
1586 // Create a group.
1587 $entity1 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
1588 $wrapper = entity_metadata_wrapper('entity_test', $entity1);
1589 $wrapper->{OG_GROUP_FIELD}->set(1);
1590 $wrapper->save();
1591
1592 og_group('entity_test', $entity1, array('entity_type' => 'user', 'entity' => $user2));
1593 og_group('entity_test', $entity1, array('entity_type' => 'user', 'entity' => $user3));
1594
1595 // Post a node, state should be active.
1596 $type = str_replace('_', '-', $this->group_content);
1597 $edit = array(
1598 'title' => 'state-active',
1599 'og_group_ref[und][0][default][]' => array($entity1->pid),
1600 );
1601
1602 $this->drupalLogin($user2);
1603 $this->drupalPost('node/add/' . $type, $edit, t('Save'));
1604
1605 $gids = og_get_entity_groups('node', 1);
1606 $id = key($gids['entity_test']);
1607 $og_membership = og_membership_load($id);
1608 $this->assertEqual($og_membership->state, OG_STATE_ACTIVE, 'Memebership status is Active');
1609
1610
1611 // Post a node, state should be pending.
1612 $this->drupalLogin($user3);
1613 $edit['title'] = 'state-pending';
1614 $this->drupalPost('node/add/' . $type, $edit, t('Save'));
1615 $gids = og_get_entity_groups('node', 2, array(OG_STATE_PENDING));
1616 $id = key($gids['entity_test']);
1617 $og_membership = og_membership_load($id);
1618 $this->assertEqual($og_membership->state, OG_STATE_PENDING, 'Memebership status is Active');
1619 }
1620 }
1621
1622 /**
1623 * Testing for deleting orphans group content.
1624 */
1625 class OgDeleteOrphansTestCase extends DrupalWebTestCase {
1626
1627 public $group_type;
1628 public $node_type;
1629
1630 public static function getInfo() {
1631 return array(
1632 'name' => 'OG orphan delete',
1633 'description' => 'Verifying for deleting orphan group content.',
1634 'group' => 'Organic groups',
1635 'dependencies' => array('advancedqueue'),
1636 );
1637 }
1638
1639 function setUp() {
1640 parent::setUp('og_test', 'advancedqueue');
1641
1642 // Create a group content type.
1643 $group = $this->drupalCreateContentType();
1644 og_create_field(OG_GROUP_FIELD, 'node', $group->type);
1645 $this->group_type = $group->type;
1646
1647 // Create group audience content type.
1648 $type = $this->drupalCreateContentType();
1649 $this->node_type = $type->type;
1650
1651 // Add OG audience field to the audience content type.
1652 $og_field = og_fields_info(OG_AUDIENCE_FIELD);
1653 $og_field['field']['settings']['target_type'] = 'node';
1654 og_create_field(OG_AUDIENCE_FIELD, 'node', $type->type, $og_field);
1655
1656 // Set the setting for delete a group content when deleting group.
1657 variable_set('og_orphans_delete', TRUE);
1658 variable_set('og_use_queue', TRUE);
1659 }
1660
1661 /**
1662 * Testing two things:
1663 * When deleting a group, the node of the group will be deleted.
1664 * Associated node with the deleted group and another group won't be deleted.
1665 */
1666 function testDeleteGroup() {
1667 // Creating two groups.
1668 $first_group = $this->drupalCreateNode(array('type' => $this->group_type));
1669 $second_group = $this->drupalCreateNode(array('type' => $this->group_type));
1670
1671 // Create two nodes.
1672 $first_node = $this->drupalCreateNode(array('type' => $this->node_type));
1673 og_group('node', $first_group, array('entity_type' => 'node', 'entity' => $first_node));
1674 og_group('node', $second_group, array('entity_type' => 'node', 'entity' => $first_node));
1675
1676 $second_node = $this->drupalCreateNode(array('type' => $this->node_type));
1677 og_group('node', $first_group, array('entity_type' => 'node', 'entity' => $second_node));
1678
1679 // Delete the group.
1680 node_delete($first_group->nid);
1681
1682 // Execute manually the queue worker.
1683 $queue = DrupalQueue::get('og_membership_orphans');
1684 $item = $queue->claimItem();
1685 og_membership_orphans_worker($item);
1686
1687 // Load the nodes we used during the test.
1688 $first_node = node_load($first_node->nid);
1689 $second_node = node_load($second_node->nid);
1690
1691 // Verify the none orphan node wasn't deleted.
1692 $this->assertTrue($first_node, "The second node is realted to another group and deleted.");
1693 // Verify the orphan node deleted.
1694 $this->assertFalse($second_node, "The orphan node deleted.");
1695 }
1696
1697 /**
1698 * Testing the moving of the node to another group when deleting a group.
1699 */
1700 function testMoveOrphans() {
1701 // Creating two groups.
1702 $first_group = $this->drupalCreateNode(array('type' => $this->group_type, 'title' => 'move'));
1703 $second_group = $this->drupalCreateNode(array('type' => $this->group_type));
1704
1705 // Create a group and relate it to the first group.
1706 $first_node = $this->drupalCreateNode(array('type' => $this->node_type));
1707 og_group('node', $first_group, array('entity_type' => 'node', 'entity' => $first_node));
1708
1709 // Delete the group.
1710 node_delete($first_group->nid);
1711
1712 // Execute manually the queue worker.
1713 $queue = DrupalQueue::get('og_membership_orphans');
1714 $item = $queue->claimItem();
1715 og_membership_orphans_worker($item);
1716
1717 // Load the node into a wrapper and verify we moved him to another group.
1718 $wrapper = entity_metadata_wrapper('node', $first_node->nid);
1719
1720 $this->assertTrue($wrapper->{OG_AUDIENCE_FIELD}->get(0)->getIdentifier() == $second_group->nid, "The node of the group moved to another group.");
1721 }
1722 }