3 class OgAccess
extends DrupalWebTestCase
{
5 public static
function getInfo() {
7 'name' => 'OG entity access',
8 'description' => 'Test the access provided by OG API.',
9 'group' => 'Organic groups',
14 parent
::setUp('og', 'entity_feature');
18 * Verify og_user_access_entity() returns correct value.
20 function testOgAccessEntity() {
21 $perm = 'administer group';
22 // Change permissions to authenticated member.
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));
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);
34 $user1 = $this->drupalCreateUser();
35 $user2 = $this->drupalCreateUser();
36 $user3 = $this->drupalCreateUser();
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);
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.'));
48 // User has access to a group associated with a group content.
50 $settings['type'] = 'article';
51 $node = $this->drupalCreateNode($settings);
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.'));
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);
63 $settings['uid'] = $user2->uid
;
64 $settings[OG_GROUP_FIELD
][LANGUAGE_NONE
][0]['value'] = 1;
65 $node = $this->drupalCreateNode($settings);
67 $wrapper = entity_metadata_wrapper('node', $node);
68 $wrapper->{OG_GROUP_FIELD
}->set(1);
71 $values = array('entity_type' => 'node', 'entity' => $node);
72 og_group('entity_test', $entity1->pid
, $values);
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.'));
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.'));
84 // Entity is an orphan group content.
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);
92 $this->assertNull(og_user_access_entity($perm, 'node', $node, $user1), t('Entity is an orphan group content, so return value is NULL.'));
94 // Entity isn't a group or a group content.
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.'));
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.'));
104 // Entity is not saved to database yet.
106 $this->assertNull(og_user_access_entity($perm, 'node', NULL
, $user1), t('Entity is not saved to database, so return value is NULL.'));
111 * Test Group node access. This will test nodes that are groups and group content.
113 class OgNodeAccess
extends DrupalWebTestCase
{
115 public static
function getInfo() {
117 'name' => 'OG node access',
118 'description' => 'Test strict node access permissions for group nodes and group content.',
119 'group' => 'Organic groups',
126 // Add OG group field to a the node's "page" bundle.
127 og_create_field(OG_GROUP_FIELD
, 'node', 'page');
129 // Add OG audience field to the node's "article" bundle.
130 og_create_field(OG_AUDIENCE_FIELD
, 'node', 'article');
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'));
136 // Create group node.
139 OG_GROUP_FIELD .
'[und][0][value]' => 1,
140 'uid' => $this->group_manager
->uid
142 $this->group1
= $this->drupalCreateNode($settings);
143 $this->group2
= $this->drupalCreateNode($settings);
145 // Create node to add to group.
148 'uid' => $this->group_manager
->uid
,
150 $this->group_content
= $this->drupalCreateNode($settings);
152 // Add node to group.
154 'entity_type' => 'node',
155 'entity' => $this->group_content
,
157 og_group('node', $this->group1
, $values);
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.
164 function testStrictAccessNodeUpdate() {
165 // Set Node access strict variable.
166 variable_set('og_node_access_strict', TRUE
);
168 // Login as editor and try to change the group node and group content.
169 $this->drupalLogin($this->editor_user
);
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.'));
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.'));
177 // Login as a group manager and try to change group node.
178 $this->drupalLogin($this->group_manager
);
180 $this->drupalGet('node/' .
$this->group1
->nid .
'/edit');
181 $this->assertResponse('200', t('Group manager allowed to access to edit group node.'));
183 $this->drupalGet('node/' .
$this->group_content
->nid .
'/edit');
184 $this->assertResponse('200', t('Group manager allowed to access to edit group content node.'));
188 * Test access to node create on strict mode.
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);
196 $this->drupalGet('node/add/article');
197 $this->assertResponse('200', t('User can access node create with non-required field.'));
199 $instance = field_info_instance('node', OG_AUDIENCE_FIELD
, 'article');
200 $instance['required'] = TRUE
;
201 field_update_instance($instance);
203 $this->drupalGet('node/add/article');
204 $this->assertResponse('403', t('User cannot access node create with required field.'));
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'));
211 $permissions = array(
212 'create article content' => 0,
213 'update own article content' => 1,
214 'update any article content' => 1,
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.');
222 // Add create permission.
223 $permissions = array(
224 'create article content' => 1,
225 'update own article content' => 0,
226 'update any article content' => 0,
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.');
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.
237 function testNoStrictAccessNodeUpdate() {
238 // Set Node access strict variable.
239 variable_set('og_node_access_strict', FALSE
);
241 // Login as editor and try to change the group node and group content.
242 $this->drupalLogin($this->editor_user
);
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.'));
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.'));
250 // Login as a group manager and try to change group node.
251 $this->drupalLogin($this->group_manager
);
253 $this->drupalGet('node/' .
$this->group1
->nid .
'/edit');
254 $this->assertResponse('200', t('Group manager allowed to access to edit group node.'));
256 $this->drupalGet('node/' .
$this->group_content
->nid .
'/edit');
257 $this->assertResponse('200', t('Group manager allowed to access to edit group content node.'));
261 * Assert a user cannot assign an existing node to a group they don't
262 * have "create" permissions.
264 function testNodeUpdateAudienceField() {
265 // Set Node access strict variable.
266 variable_set('og_node_access_strict', TRUE
);
267 $editor_user = $this->editor_user
;
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));
273 // Add group-content to a single group.
274 og_group('node', $this->group1
, array('entity_type' => 'node', 'entity' => $this->group_content
));
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,
282 og_role_change_permissions($og_roles[OG_AUTHENTICATED_ROLE
], $permissions);
284 // Login and try to edit this node
285 $this->drupalLogin($this->editor_user
);
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.');
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,
299 og_role_change_permissions($og_roles[OG_AUTHENTICATED_ROLE
], $permissions);
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.');
306 // Test for retaining groups on node save.
307 $this->drupalPost('node/'.
$this->group_content
->nid .
'/edit', array(), t('Save'));
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.');
316 * Test the Organic groups API and CRUD handling.
318 class OgMetaData
extends DrupalWebTestCase
{
320 public static
function getInfo() {
322 'name' => 'OG metadata',
323 'description' => 'Test the metadata properties.',
324 'group' => 'Organic groups',
329 parent
::setUp('og', 'entity_feature');
333 * Test the og_get_entity_groups() API function.
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');
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);
344 // Add a second audience field.
345 og_create_field('og_ref_2', 'node', 'article', $og_field);
347 $user1 = $this->drupalCreateUser();
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);
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);
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);
365 $settings['type'] = 'article';
367 // Create group enteties.
368 foreach (og_group_content_states() as
$state => $value) {
369 $node = $this->drupalCreateNode($settings);
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);
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);
385 $wrapper = entity_metadata_wrapper('node', $node->nid
);
386 $this->assertEqual($wrapper->og_membership
->count(), 3, t('Found all OG memberships.'));
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)));
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)));
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.'));
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.'));
410 * Test Group content handeling.
412 class OgGroupAndUngroup
extends DrupalWebTestCase
{
414 public static
function getInfo() {
416 'name' => 'OG group and ungroup',
417 'description' => 'Test the group and ungrouping of content with a group.',
418 'group' => 'Organic groups',
423 parent
::setUp('og', 'entity_feature');
425 // Add OG group field to the entity_test's "main" bundle.
426 og_create_field(OG_GROUP_FIELD
, 'entity_test', 'main');
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);
435 * Test group and ungroup of content.
437 function testGroupAndUngroup() {
438 $user1 = $this->drupalCreateUser();
439 $user2 = $this->drupalCreateUser();
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);
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);
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);
457 $settings['type'] = 'article';
458 $settings['uid'] = $user2->uid
;
459 $node = $this->drupalCreateNode($settings);
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.'));
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.'));
475 // Exception on existing OG membership.
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.');
480 catch (OgException
$e) {
481 $this->pass('Saving multiple OG membership for same entity and group does not work.');
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);
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.'));
498 // Exception on field cardinality.
499 og_group('entity_test', $entity2->pid
, $values);
501 og_group('entity_test', $entity3->pid
, $values);
502 $this->fail('Grouping beyond field cardinality works.');
504 catch (OgException
$e) {
505 $this->pass('Grouping beyond field cardinality does not work.');
508 // Exception as field-name is incorrect.
509 $values['field_name'] = 'wrong-field-name';
511 og_group('entity_test', $entity1->pid
, $values);
512 $this->fail('Grouping with incorrect field name works.');
514 catch (OgException
$e) {
515 $this->pass('Grouping with incorrect field name does not work.');
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';
524 og_group('entity_test', $entity1->pid
, $values);
525 $this->fail('Grouping with wrong target type works.');
527 catch (OgException
$e) {
528 $this->pass('Grouping with wrong target type does not work.');
531 // Exception on audience field, referencing correct target type, but wrong
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';
539 og_group('entity_test', $entity1->pid
, $values);
540 $this->fail('Grouping with wrong target bundles works.');
542 catch (OgException
$e) {
543 $this->pass('Grouping with wrong target bundles does not work.');
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);
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);
556 $this->fail('Grouping with no group-audience field in bundle works.');
558 catch (OgException
$e) {
559 $this->pass('Grouping with no group-audience field in bundle does not work.');
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.'));
567 // Delete node and confirm memberships were deleted.
568 $values = array('entity_type' => 'node', 'entity' => $node);
569 og_group('entity_test', $entity1->pid
, $values);
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
);
576 $this->assertFalse(og_get_entity_groups('node', $nid), t('OG memberships deleted on entity deletion.'));
580 * Test granting deault role to group manager.
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();
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);
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.'));
598 class OgPermissionsTestCase
extends DrupalWebTestCase
{
599 public static
function getInfo() {
601 'name' => 'OG permissions',
602 'description' => 'Verify that permissions can be added and removed via API.',
603 'group' => 'Organic groups'
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');
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);
620 * Verify proper permission changes by og_role_change_permissions().
622 function testOgUserRoleChangePermissions() {
624 $user1 = $this->drupalCreateUser();
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);
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);
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.'));
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.'));
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
];
650 $permissions = array(
651 'delete own article content' => 1,
653 og_role_change_permissions($rid, $permissions);
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.'));
659 $permissions = array(
660 'delete own article content' => 0,
661 'administer group' => 1,
663 og_role_change_permissions($rid, $permissions);
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.'));
670 class OgDefaultAccessFieldTestCase
extends DrupalWebTestCase
{
671 public static
function getInfo() {
673 'name' => 'OG default access field',
674 'description' => 'Test groups with default access field.',
675 'group' => 'Organic groups'
680 parent
::setUp('og', 'entity_feature');
685 * Test groups with default access field enabled or disabled.
687 function testOgDefaultAccessField() {
689 $user1 = $this->drupalCreateUser();
691 // Add OG group field to the entity_test's "main" bundle.
692 og_create_field(OG_GROUP_FIELD
, 'entity_test', 'main');
694 $og_roles = og_roles('entity_test', 'main');
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);
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.'));
703 // Add default access field to the entity_test's "main" bundle.
704 og_create_field(OG_DEFAULT_ACCESS_FIELD
, 'entity_test', 'main');
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);
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.'));
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.'));
720 // Group with default access field enabled.
721 $wrapper->{OG_DEFAULT_ACCESS_FIELD
}->set(1);
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.'));
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.'));
730 // Disable existing group's default access field.
731 $wrapper->{OG_DEFAULT_ACCESS_FIELD
}->set(0);
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.'));
740 * Load a filled installation of Drupal 6 and run the upgrade on it.
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
746 class OgMigrate7000TestCase
extends UpgradePathTestCase
{
747 public static
function getInfo() {
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'),
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',
763 $this->assertTrue($this->performUpgrade(), t('The upgrade was completed successfully.'));
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');
770 // TODO: Why do we need to enable Views?!
771 module_enable(array('og', 'views', 'migrate'));
773 $class_names = array(
774 'OgMigrateAddFields',
779 // FIXME: migrate_flush_caches() crashes, so we register manually.
780 foreach ($class_names as
$class_name) {
781 MigrationBase
::registerMigration($class_name);
784 // Register a dynamic migration.
785 MigrationBase
::registerMigration('OgMigrateGroup', 'OgMigrateGroupTest_group', array('bundle' => 'test_group'));
787 $migration = Migration
::getInstance('OgMigrateAddFields');
788 $result = $migration->processImport();
789 $this->assertEqual($result, Migration
::RESULT_COMPLETED
, 'Migration OgMigrateAddFields executed.');
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.');
795 $migration = Migration
::getInstance('OgMigrateUser');
796 $result = $migration->processImport();
797 $this->assertEqual($result, Migration
::RESULT_COMPLETED
, 'Migration OgMigrateUser executed.');
799 $migration = Migration
::getInstance('OgMigrateContent');
800 $result = $migration->processImport();
801 $this->assertEqual($result, Migration
::RESULT_COMPLETED
, 'Migration OgMigrateContent executed.');
805 * Test a successful group upgrade.
807 * @see og_7000_group()
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)));
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)));
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.'));
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
)));
838 * @see og_7000_user()
840 public
function testUser() {
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
),
847 6 => array('active' => FALSE
, 'admin' => TRUE
),
848 7 => array('admin' => TRUE
),
851 $og_roles = og_roles('node', 'test_group');
853 foreach ($values as
$uid => $value) {
854 $account = user_load($uid);
856 // Set default values.
857 $value += array('active' => TRUE
, 'admin' => FALSE
);
860 if ($value['active']) {
862 $states = array(OG_STATE_ACTIVE
);
863 $rid = array_search(OG_AUTHENTICATED_ROLE
, $og_roles);
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);
874 if ($value['admin']) {
875 // OG_ADMINISTRATOR_ROLE
876 $rid = array_search(OG_ADMINISTRATOR_ROLE
, $og_roles);
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
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)));
889 * Test group description upgrade.
891 * @see og_7000_group()
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.'));
905 * Test the complex widget.
907 class OgComplexWidgetTestCase
extends DrupalWebTestCase
{
909 public static
function getInfo() {
911 'name' => 'OG reference widget',
912 'description' => 'Test the OG reference widget behavior.',
913 'group' => 'Organic groups',
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');
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);
932 * Test "field modes" of the OG reference widget.
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'));
938 // Create group nodes.
941 OG_GROUP_FIELD .
'[und][0][value]' => 1,
943 $settings['uid'] = $user1->uid
;
944 $group1 = $this->drupalCreateNode($settings);
946 $settings['uid'] = $user2->uid
;
947 $group2 = $this->drupalCreateNode($settings);
952 $settings['uid'] = $user1->uid
;
953 $post1 = $this->drupalCreateNode($settings);
954 og_group('node', $group1->nid
, array('entity_type' => 'node', 'entity' => $post1));
956 $settings['uid'] = $user2->uid
;
957 $post2 = $this->drupalCreateNode($settings);
958 og_group('node', $group2->nid
, array('entity_type' => 'node', 'entity' => $post2));
960 $this->drupalLogin($user1);
961 $this->drupalGet("node/$post1->nid/edit");
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.');
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."');
969 $this->drupalLogin($user2);
970 $this->drupalGet("node/$post2->nid/edit");
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.');
977 * Test non-accessible group IDs are saved, upon form submit.
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'));
983 // Create group nodes.
986 OG_GROUP_FIELD .
'[und][0][value]' => 1,
988 $settings['uid'] = $user1->uid
;
989 $group1 = $this->drupalCreateNode($settings);
991 $settings['uid'] = $user2->uid
;
992 $group2 = $this->drupalCreateNode($settings);
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));
1002 $this->drupalLogin($user2);
1003 $this->drupalPost("node/$post1->nid/edit", array(), 'Save');
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.');
1012 * Test a non "administer group" user with pending membership, re-saving
1015 function testUserEdit() {
1016 $user1 = $this->drupalCreateUser();
1017 $user2 = $this->drupalCreateUser();
1021 OG_GROUP_FIELD .
'[und][0][value]' => 1,
1023 $settings['uid'] = $user1->uid
;
1024 $group1 = $this->drupalCreateNode($settings);
1026 og_group('node', $group1->nid
, array('entity' => $user2, 'state' => OG_STATE_PENDING
));
1028 $this->drupalLogin($user2);
1029 $this->drupalPost("user/$user2->uid/edit", array(), 'Save');
1031 $this->assertTrue(og_get_entity_groups('user', $user2, array(OG_STATE_PENDING
)), 'User membership was retained after user save.');
1035 * Test multiple group-audience fields.
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);
1042 $user1 = $this->drupalCreateUser();
1047 OG_GROUP_FIELD .
'[und][0][value]' => 1,
1048 'uid' => $user1->uid
,
1050 $group1 = $this->drupalCreateNode($settings);
1051 $group2 = $this->drupalCreateNode($settings);
1053 // Create group content.
1056 'uid' => $user1->uid
,
1058 $post1 = $this->drupalCreateNode($settings);
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'));
1063 $this->drupalLogin($user1);
1064 $this->drupalGet("node/$post1->nid/edit");
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
);
1073 * Test the revocation of group roles.
1075 class OgRoleRevoke
extends DrupalWebTestCase
{
1077 public static
function getInfo() {
1079 'name' => 'OG roles revoke',
1080 'description' => 'Test the revocation of group roles',
1081 'group' => 'Organic groups',
1086 parent
::setUp('og', 'entity_feature');
1089 function testOgRoleRevoke() {
1091 $user1 = $this->drupalCreateUser();
1093 // Add OG group field to the entity_test's "main" bundle.
1094 og_create_field(OG_GROUP_FIELD
, 'entity_test', 'main');
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);
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);
1107 // Create a role named 'role1'.
1108 $role1 = og_role_create('role1', 'entity_test', 0, 'main');
1109 og_role_save($role1);
1111 // Create a role named 'role2'.
1112 $role2 = og_role_create('role2', 'entity_test', 0, 'main');
1113 og_role_save($role2);
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
);
1119 // Unsubscribe user1 from entity1.
1120 og_ungroup('entity_test', $entity1->pid
, 'user', $user1->uid
);
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'));
1127 user_delete($user1->uid
);
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'));
1135 * Test Upgrade from branch 1.x to 2.x.
1137 class OgMigrate7200TestCase
extends UpdatePathTestCase
{
1139 public static
function getInfo() {
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'),
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',
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')
1165 function testUpgrade() {
1166 $this->assertTrue($this->performUpgrade(), t('The upgrade was completed successfully.'));
1168 $this->assertFalse(field_info_field('group_audience'), 'Group audience field was deleted.');
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');
1175 module_enable(array('og', 'migrate'));
1177 // FIXME: migrate_flush_caches() crashes, so we register manually.
1178 MigrationBase
::registerMigration('OgMigrateMembership');
1179 MigrationBase
::registerMigration('OgMigrateRoles');
1180 MigrationBase
::registerMigration('OgMigrateUserRoles');
1182 $migration = Migration
::getInstance('OgMigrateMembership');
1183 $result = $migration->processImport();
1184 $this->assertEqual($result, Migration
::RESULT_COMPLETED
, 'OgMigrateMembership returned RESULT_COMPLETED');
1186 $migration = Migration
::getInstance('OgMigrateRoles');
1187 $result = $migration->processImport();
1188 $this->assertEqual($result, Migration
::RESULT_COMPLETED
, 'OgMigrateRoles returned RESULT_COMPLETED');
1190 $migration = Migration
::getInstance('OgMigrateUserRoles');
1191 $result = $migration->processImport();
1192 $this->assertEqual($result, Migration
::RESULT_COMPLETED
, 'OgMigrateUserRoles returned RESULT_COMPLETED');
1194 // Assert group roles.
1198 'administrator member',
1200 $this->assertEqual(array_values(og_roles('node', 'school', 1)), $roles, 'Returned expected roles group first group.');
1202 $roles[] = 'new role';
1203 $this->assertEqual(array_values(og_roles('node', 'school', 2)), $roles, 'Returned expected roles group second group.');
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.');
1212 * Test queying group-audience fields using entityFieldQuery.
1214 class OgEntityFieldQueryTestCase
extends DrupalWebTestCase
{
1216 public static
function getInfo() {
1218 'name' => 'OG audience fields query',
1219 'description' => 'Test querying group-audience fields using entityFieldQuery.',
1220 'group' => 'Organic groups',
1225 parent
::setUp('og', 'entity_feature');
1227 $user1 = $this->drupalCreateUser();
1228 $user2 = $this->drupalCreateUser();
1229 $type = $this->drupalCreateContentType();
1230 $group_type = $type->type
;
1232 $type = $this->drupalCreateContentType();
1233 $group_content_type = $type->type
;
1235 og_create_field(OG_GROUP_FIELD
, 'node', $group_type);
1236 og_create_field(OG_GROUP_FIELD
, 'entity_test', 'main');
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);
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);
1249 // Create a non-group audience, entity-refence field.
1251 'entity_types' => array('node'),
1252 'settings' => array(
1253 'handler' => 'base',
1254 'target_type' => 'node',
1255 'handler_settings' => array(
1256 'target_bundles' => array(),
1259 'field_name' => 'node_reference',
1260 'type' => 'entityreference',
1263 $field = field_create_field($field);
1265 'field_name' => 'node_reference',
1266 'bundle' => $group_content_type,
1267 'entity_type' => 'node',
1269 field_create_instance($instance);
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);
1278 'type' => $group_type,
1279 'uid' => $user1->uid
,
1281 $settings[OG_GROUP_FIELD
][LANGUAGE_NONE
][0]['value'] = 1;
1282 $group2 = $this->drupalCreateNode($settings);
1284 // Create group-content.
1286 'type' => $group_content_type,
1287 'uid' => $user1->uid
,
1289 $node = $this->drupalCreateNode($settings);
1291 $wrapper = entity_metadata_wrapper('node', $node);
1292 $wrapper->node_reference
->set($group2);
1296 'entity_type' => 'node',
1300 og_group('entity_test', $group1, $values);
1301 og_group('node', $group2, $values);
1303 $entity_test = entity_create('entity_test', array('name' => 'test', 'uid' => $user1->uid
));
1304 $entity_test->save();
1306 'entity_type' => 'entity_test',
1307 'entity' => $entity_test,
1309 og_group('node', $group2, $values);
1312 'entity_type' => 'user',
1315 og_group('node', $group2, $values);
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;
1326 * Test the following query scenarios:
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).
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
;
1345 // Single group audience.
1346 $query = new
EntityFieldQuery();
1348 ->entityCondition('entity_type', 'node')
1349 ->propertyCondition('type', $node->type
)
1350 ->fieldCondition('og_node', 'target_id', $group2->nid
)
1353 $this->assertEqual(array_keys($result['node']), array($node->nid
), 'Single group audience query is correct.');
1355 // Multiple group audience.
1356 $query = new
EntityFieldQuery();
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
)
1364 $this->assertEqual(array_keys($result['node']), array($node->nid
), 'Multiple group audience query is correct.');
1366 // Single group audience first, with another non-audience field.
1367 $query = new
EntityFieldQuery();
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
)
1375 $this->assertEqual(array_keys($result['node']), array($node->nid
), 'Single group audience first, with another non-audience field query is correct.');
1377 // Non-audience field first, with single group audience.
1378 $query = new
EntityFieldQuery();
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
)
1386 $this->assertEqual(array_keys($result['node']), array($node->nid
), 'Non-audience field first, with single group audience query is correct.');
1388 // Multiple entity types in entityCondition().
1389 $query = new
EntityFieldQuery();
1391 ->entityCondition('entity_type', array('node', 'user'), 'IN')
1392 ->fieldCondition('node_reference', 'target_id', $group2->nid
)
1393 ->fieldCondition('og_node', 'target_id', $group2->nid
)
1396 $this->assertEqual(array_keys($result['node']), array($node->nid
), 'Multiple entity types in entityCondition() query is correct.');
1398 // No entity property.
1399 $query = new
EntityFieldQuery();
1401 ->entityCondition('entity_type', 'node')
1402 ->fieldCondition('og_node', 'target_id', $group2->nid
)
1405 $this->assertEqual(array_keys($result['node']), array($node->nid
), 'No entity property query is correct.');
1407 // Non-node entity without revision table.
1408 $query = new
EntityFieldQuery();
1410 ->entityCondition('entity_type', 'entity_test')
1411 ->fieldCondition('og_node', 'target_id', $group2->nid
)
1414 $this->assertEqual(array_keys($result['entity_test']), array($entity_test->pid
), 'Non-node entity without revision table query is correct.');
1416 // Non-node entity without revision table and without bundles.
1417 $query = new
EntityFieldQuery();
1419 ->entityCondition('entity_type', 'user')
1420 ->fieldCondition('og_entity_test', 'target_id', $group2->nid
)
1423 $expected_values = array(
1427 $this->assertEqual(array_keys($result['user']), $expected_values, 'Non-node entity without revision table and without bundles query is correct.');
1430 $query = new
EntityFieldQuery();
1432 ->entityCondition('entity_type', 'node')
1433 ->propertyCondition('type', $node->type
)
1434 ->fieldCondition('og_node', 'target_id', $group2->nid
)
1438 $this->assertEqual($result, 1, 'Count query is correct.');
1443 * Test group-audience field auto-create.
1445 class OgAudienceFieldAutoCreateTestCase
extends DrupalWebTestCase
{
1447 public static
function getInfo() {
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',
1456 parent
::setUp('og');
1460 * Test auto-attaching group-audience fields to the user entity.
1462 function testAutoAttach() {
1463 $type1 = $this->drupalCreateContentType();
1464 $type2 = $this->drupalCreateContentType();
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.');
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);
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.');
1483 class OgBehaviorHandlerTestCase
extends DrupalWebTestCase
{
1485 public static
function getInfo() {
1487 'name' => 'OG widget behavior',
1488 'description' => 'Test the widget behavior of group-audience fields.',
1489 'group' => 'Organic groups',
1494 parent
::setUp('og_test', 'entity_feature');
1496 // Add OG group field to the entity_test's "main" bundle.
1497 og_create_field(OG_GROUP_FIELD
, 'entity_test', 'main');
1499 $type = $this->drupalCreateContentType(array('type' => 'behavior'));
1500 $this->group_content
= $type->type
;
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);
1509 * Test piping group association via the group-audience field.
1511 function testGroupAudienceField() {
1512 $user1 = $this->drupalCreateUser();
1513 $user2 = $this->drupalCreateUser();
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);
1520 $settings = array();
1521 $settings['type'] = $this->group_content
;
1522 $settings['uid'] = $user2->uid
;
1523 $node = $this->drupalCreateNode($settings);
1525 $wrapper = entity_metadata_wrapper('node', $node);
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
;
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.'));
1534 $wrapper->{OG_AUDIENCE_FIELD
}->set(NULL
);
1536 $this->assertFalse(og_get_entity_groups('node', $node), t('Node is not associated with any group.'));
1540 * Test skipping OgBehaviorHandler.
1542 function testGroupAudienceFieldSkipBehavior() {
1543 $user1 = $this->drupalCreateUser();
1544 $user2 = $this->drupalCreateUser();
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);
1551 $settings = array();
1552 $settings['type'] = $this->group_content
;
1553 $settings['uid'] = $user2->uid
;
1554 $node = $this->drupalCreateNode($settings);
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
);
1560 $this->assertFalse(og_get_entity_groups('node', $node), 'Widget behavior removed group association as expected.');
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
;
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.');
1572 * Test settings the OG membership state via field values, when associating
1573 * a new group-content to a group.
1575 function testSetStateOnInsert() {
1576 module_enable(array('og_test'));
1577 $permissions = array(
1579 "create $this->group_content content",
1582 $user1 = $this->drupalCreateUser();
1583 $user2 = $this->drupalCreateUser($permissions);
1584 $user3 = $this->drupalCreateUser($permissions);
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);
1592 og_group('entity_test', $entity1, array('entity_type' => 'user', 'entity' => $user2));
1593 og_group('entity_test', $entity1, array('entity_type' => 'user', 'entity' => $user3));
1595 // Post a node, state should be active.
1596 $type = str_replace('_', '-', $this->group_content
);
1598 'title' => 'state-active',
1599 'og_group_ref[und][0][default][]' => array($entity1->pid
),
1602 $this->drupalLogin($user2);
1603 $this->drupalPost('node/add/' .
$type, $edit, t('Save'));
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');
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');
1623 * Testing for deleting orphans group content.
1625 class OgDeleteOrphansTestCase
extends DrupalWebTestCase
{
1630 public static
function getInfo() {
1632 'name' => 'OG orphan delete',
1633 'description' => 'Verifying for deleting orphan group content.',
1634 'group' => 'Organic groups',
1635 'dependencies' => array('advancedqueue'),
1640 parent
::setUp('og_test', 'advancedqueue');
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
;
1647 // Create group audience content type.
1648 $type = $this->drupalCreateContentType();
1649 $this->node_type
= $type->type
;
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);
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
);
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.
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
));
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));
1676 $second_node = $this->drupalCreateNode(array('type' => $this->node_type
));
1677 og_group('node', $first_group, array('entity_type' => 'node', 'entity' => $second_node));
1679 // Delete the group.
1680 node_delete($first_group->nid
);
1682 // Execute manually the queue worker.
1683 $queue = DrupalQueue
::get('og_membership_orphans');
1684 $item = $queue->claimItem();
1685 og_membership_orphans_worker($item);
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
);
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.");
1698 * Testing the moving of the node to another group when deleting a group.
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
));
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));
1709 // Delete the group.
1710 node_delete($first_group->nid
);
1712 // Execute manually the queue worker.
1713 $queue = DrupalQueue
::get('og_membership_orphans');
1714 $item = $queue->claimItem();
1715 og_membership_orphans_worker($item);
1717 // Load the node into a wrapper and verify we moved him to another group.
1718 $wrapper = entity_metadata_wrapper('node', $first_node->nid
);
1720 $this->assertTrue($wrapper->{OG_AUDIENCE_FIELD
}->get(0)->getIdentifier() == $second_group->nid
, "The node of the group moved to another group.");