View source
<?php
class OgGroupApi extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Organic groups API and CRUD',
'description' => 'Test the create, update and remove of group entities and API functions.',
'group' => 'Organic groups',
);
}
function setUp() {
parent::setUp('og', 'entity_test');
module_enable(array(
'entity_feature',
));
}
function testOgAccess() {
module_enable(array(
'og_test',
));
og_create_field(OG_GROUP_FIELD, 'node', 'article');
$admin_user = $this
->drupalCreateUser(array(
'access content',
'administer content types',
'create article content',
'edit any article content',
'administer group',
));
$this
->drupalLogin($admin_user);
$settings = array();
$settings['type'] = 'article';
$settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 1;
$node = $this
->drupalCreateNode($settings);
$group = og_get_group('node', $node->nid);
$this
->assertTrue($group
->access('update', $admin_user), t('User can update a node that is a group.'));
$settings['title'] = 'deny access';
$node = $this
->drupalCreateNode($settings);
$group = og_get_group('node', $node->nid);
$this
->assertFalse($group
->access('update', $admin_user), t('User can not update the node that is a group.'));
}
function testOgAccessEntity() {
og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
og_create_field(OG_AUDIENCE_FIELD, 'node', 'article');
$perm = 'administer group';
$roles = array_flip(og_get_global_roles());
og_role_change_permissions($roles[OG_AUTHENTICATED_ROLE], array(
$perm => 1,
));
$user1 = $this
->drupalCreateUser();
$user2 = $this
->drupalCreateUser();
$user3 = $this
->drupalCreateUser();
$entity1 = entity_create('entity_test', array(
'name' => 'main',
'uid' => $user1->uid,
));
$wrapper = entity_metadata_wrapper('entity_test', $entity1);
$wrapper->{OG_GROUP_FIELD}
->set(1);
$wrapper
->save();
$this
->assertTrue(og_user_access_entity($perm, 'entity_test', $entity1, $user1), t('User1 has access to group.'));
$this
->assertFalse(og_user_access_entity($perm, 'entity_test', $entity1, $user2), t('User2 does not have access to group.'));
$settings = array();
$settings['type'] = 'article';
$node = $this
->drupalCreateNode($settings);
$group = og_get_group('entity_test', $entity1->pid);
$values = array(
'entity type' => 'node',
'entity' => $node,
);
og_group($group->gid, $values);
$this
->assertTrue(og_user_access_entity($perm, 'node', $node, $user1), t('User1 has access to group content.'));
$this
->assertFalse(og_user_access_entity($perm, 'node', $node, $user2), t('User2 does not have access to group content.'));
og_create_field(OG_GROUP_FIELD, 'node', 'article');
$settings['uid'] = $user2->uid;
$settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 1;
$node = $this
->drupalCreateNode($settings);
$wrapper = entity_metadata_wrapper('node', $node);
$wrapper->{OG_GROUP_FIELD}
->set(1);
$wrapper
->save();
$values = array(
'entity type' => 'node',
'entity' => $node,
);
og_group($group->gid, $values);
$this
->assertTrue(og_user_access_entity($perm, 'node', $node, $user1), t('User1 has access based on access to group.'));
$this
->assertTrue(og_user_access_entity($perm, 'node', $node, $user2), t('User2 has access based on access to group content.'));
$this
->assertFalse(og_user_access_entity($perm, 'node', $node, $user3), t('User3 has no access to entity.'));
$settings['uid'] = $user2->uid;
$settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 0;
$node = $this
->drupalCreateNode($settings);
$this
->assertNull(og_user_access_entity($perm, 'node', $node, $user1), t('Entity is a disabled group, so return value is NULL.'));
$settings = array();
$settings['type'] = 'article';
$settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 0;
$node = $this
->drupalCreateNode($settings);
$values = array(
'entity type' => 'node',
'entity' => $node,
);
og_group($group->gid, $values);
$entity1
->delete();
$this
->assertNull(og_user_access_entity($perm, 'node', $node, $user1), t('Entity is an orphan group content, so return value is NULL.'));
$settings = array();
$settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 0;
$settings['type'] = 'article';
$node = $this
->drupalCreateNode($settings);
$this
->assertNull(og_user_access_entity($perm, 'node', $node, $user1), t('Entity is not a group or a group contentm, so return value is NULL.'));
$this
->assertNull(og_user_access_entity($perm, 'node', NULL, $user1), t('Entity passed is NULL, so return value is NULL.'));
unset($node->nid);
$this
->assertNull(og_user_access_entity($perm, 'node', NULL, $user1), t('Entity is not saved to database, so return value is NULL.'));
}
function testOgCrud() {
$node = entity_create('node', array(
'type' => 'article',
'title' => $this
->randomName(),
'uid' => 1,
));
entity_save('node', $node);
$group = og_get_group('node', $node->nid, TRUE);
$this
->assertTrue($group->is_new, t('New group has "is new" property.'));
$this
->assertTrue($group->state == OG_STATE_ACTIVE, t('Default state property is active.'));
$this
->assertTrue($group->created, t('Group creating time was added to group.'));
$this
->assertTrue(empty($group->gid), t('Group ID not set for unsaved group.'));
$group
->save();
$this
->assertTrue(!empty($group->gid), t('Group ID was set for saved group.'));
$group = og_get_group('node', $node->nid);
$group->state = OG_STATE_PENDING;
$group
->save();
drupal_static_reset('og_get_group_ids');
$group = og_get_group('node', $node->nid);
$this
->assertFalse($group, t('Pending state group was not loaded, as it was not requested.'));
$group = og_get_group('node', $node->nid, FALSE, array(
OG_STATE_PENDING,
), TRUE);
$this
->assertTrue($group->state == OG_STATE_PENDING, t('Group was updated.'));
$group
->delete();
$group = og_get_group('node', $node->nid, FALSE, array(), TRUE);
$this
->assertFalse($group, t('Group was deleted.'));
}
function testOgGetGroupIds() {
$list = array(
1 => array(
'entity_test',
10,
),
2 => array(
'entity_test',
20,
),
3 => array(
'entity_test',
30,
),
4 => array(
'entity_test',
40,
OG_STATE_PENDING,
),
5 => array(
'entity_test',
50,
OG_STATE_PENDING,
),
6 => array(
'node',
10,
),
);
foreach ($list as $value) {
$values = array(
'entity_type' => $value[0],
'etid' => $value[1],
'state' => !empty($value[2]) ? $value[2] : OG_STATE_ACTIVE,
'created' => time(),
'label' => $this
->randomString(),
);
entity_create('group', $values)
->save();
}
$gids = og_get_group_ids('entity_test');
$expected_gids = array(
10 => 1,
20 => 2,
30 => 3,
);
$this
->assertEqual($gids, $expected_gids, t('All active groups of the same entity were returned.'));
drupal_static_reset('og_get_group_ids');
$gids = og_get_group_ids('group', array());
$this
->assertFalse($gids, t('No groups were returned, as no entity ID was specified.'));
drupal_static_reset('og_get_group_ids');
$gids = og_get_group_ids('entity_test', FALSE, array(
OG_STATE_PENDING,
));
$expected_gids = array(
40 => 4,
50 => 5,
);
$this
->assertEqual($gids, $expected_gids, t('All pending groups of the same entity were returned.'));
drupal_static_reset('og_get_group_ids');
$gids = og_get_group_ids('entity_test', array(
10,
20,
40,
));
$expected_gids = array(
10 => 1,
20 => 2,
);
$this
->assertEqual($gids, $expected_gids, t('Specific active groups were returned.'));
drupal_static_reset('og_get_group_ids');
$gids = og_get_group_ids('entity_test', array(
10,
20,
40,
), array(
OG_STATE_PENDING,
));
$expected_gids = array(
40 => 4,
);
$this
->assertEqual($gids, $expected_gids, t('Specific pending groups were returned.'));
drupal_static_reset('og_get_group_ids');
$gids = og_get_group_ids('entity_test_non_exist', array(
1,
));
$this
->assertFalse($gids, t('Non existent entity type was not returned.'));
drupal_static_reset('og_get_group_ids');
$gids = og_get_group_ids('entity_test', array(
100,
));
$this
->assertFalse($gids, t('Non existent entity ID was not returned.'));
drupal_static_reset('og_get_group_ids');
$gids = og_get_group_ids('entity_test', array(
10,
20,
30,
));
$this
->assertEqual(count($gids), 3, t('All active groups loaded.'));
$cache = drupal_static('og_get_group_ids', array());
$cache_gids = array(
10 => 1,
20 => 2,
30 => 3,
);
$this
->assertEqual($cache['entity_test'], $cache_gids, 'All active groups are cached.');
$gids = og_get_group_ids('entity_test', array(
10,
20,
));
$this
->assertEqual(count($gids), 2, t('All active groups re-loaded from cache.'));
$cache = drupal_static('og_get_group_ids', array());
$cache_gids = array(
10 => 1,
20 => 2,
30 => 3,
);
$this
->assertEqual($cache['entity_test'], $cache_gids, 'All active groups are cached.');
$gids = og_get_group_ids('entity_test', array(
10,
20,
), array(
OG_STATE_ACTIVE,
), TRUE);
$this
->assertEqual(count($gids), 2, t('All requested groups loaded after soft-reset.'));
$cache = drupal_static('og_get_group_ids', array());
$cache_gids = array(
10 => 1,
20 => 2,
);
$this
->assertEqual($cache['entity_test'], $cache_gids, 'All requested groups are cached after soft-reset.');
$gids = og_get_group_ids('entity_test', array(
10,
20,
30,
40,
));
$this
->assertEqual(count($gids), 3, t('Only active groups loaded.'));
$cache = drupal_static('og_get_group_ids', array());
$cache_gids = array(
10 => 1,
20 => 2,
30 => 3,
);
$this
->assertEqual($cache['entity_test'], $cache_gids, 'Only active groups cached.');
$cache = drupal_static('og_get_group_ids', array());
$this
->assertEqual($cache['__info']['entity_test']['states'], array(
OG_STATE_ACTIVE,
), 'Cache states is set to active.');
$gids = og_get_group_ids('entity_test', array(
10,
20,
30,
40,
), array(
OG_STATE_PENDING,
));
$this
->assertEqual(count($gids), 1, t('Cache was soft reset as state was changed, and only pending group was loaded.'));
$cache = drupal_static('og_get_group_ids', array());
$cache_gids = array(
40 => 4,
);
$this
->assertEqual($cache['entity_test'], $cache_gids, 'Only requested pending group was cached.');
$this
->assertEqual($cache['__info']['entity_test']['states'], array(
OG_STATE_PENDING,
), 'Cache states was changed to pending.');
$cache = drupal_static('og_get_group_ids', array());
$this
->assertFalse($cache['__info']['entity_test']['query all'], '"query all" is FALSE in cache.');
$gids = og_get_group_ids('entity_test', FALSE);
$this
->assertEqual(count($gids), 3, t('All active groups loaded from cache after query all.'));
$cache = drupal_static('og_get_group_ids', array());
$cache_gids = array(
10 => 1,
20 => 2,
30 => 3,
);
$this
->assertEqual($cache['entity_test'], $cache_gids, 'All active groups are cached.');
$this
->assertTrue($cache['__info']['entity_test']['query all'], '"query all" was set to TRUE in cache.');
$gids = og_get_group_ids('entity_test', array(
10,
20,
40,
));
$this
->assertEqual(count($gids), 2, t('All requested active groups loaded from cache after query all.'));
$cache = drupal_static('og_get_group_ids', array());
$cache_gids = array(
10 => 1,
20 => 2,
30 => 3,
);
$this
->assertEqual($cache['entity_test'], $cache_gids, 'All active groups are still cached from previous call.');
$this
->assertTrue($cache['__info']['entity_test']['query all'], '"query all" is still set to TRUE in cache.');
$cache = drupal_static('og_get_group_ids', array());
$this
->assertTrue(empty($cache['group']), 'Cache of "group" entity is empty.');
$gids = og_get_group_ids('group', FALSE, array(
OG_STATE_ACTIVE,
OG_STATE_PENDING,
));
$this
->assertEqual(count($gids), 6, t('All active and pending groups loaded.'));
$cache = drupal_static('og_get_group_ids', array());
$cache_gids = drupal_map_assoc(array(
1,
2,
3,
4,
5,
6,
));
$this
->assertEqual($cache['group'], $cache_gids, 'All active and pending groups are cached.');
$this
->assertTrue($cache['__info']['group']['query all'], '"query all" is set to TRUE in cache.');
}
function testOgGroupField() {
og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
$web_user = $this
->drupalCreateUser();
$property = OG_GROUP_FIELD;
$entity = entity_create('entity_test', array(
'name' => 'main',
'uid' => $web_user->uid,
));
$entity->{$property}[LANGUAGE_NONE][0]['value'] = 0;
$entity
->save();
$group = og_get_group('entity_test', $entity->pid);
$this
->assertTrue(empty($group->gid), t('Group was not created.'));
$entity->{$property}[LANGUAGE_NONE][0]['value'] = 1;
$entity
->save();
$group = og_get_group('entity_test', $entity->pid);
$this
->assertTrue(!empty($group->gid), t('Group was created.'));
$this
->assertTrue($group->state == OG_STATE_ACTIVE, t('Group state is set to active.'));
$this
->assertTrue(og_is_member($group->gid, 'user', $web_user), t('User is registered to the new group.'));
$entity->{$property}[LANGUAGE_NONE][0]['value'] = 0;
$entity
->save();
$group = og_get_group('entity_test', $entity->pid, FALSE, array(
OG_STATE_ACTIVE,
OG_STATE_PENDING,
), TRUE);
$this
->assertTrue($group->state == OG_STATE_PENDING, t('Group state was set to pending.'));
$gid = $group->gid;
$entity
->delete();
$group = og_get_group('entity_test', $entity->pid, FALSE, array(
OG_STATE_ACTIVE,
OG_STATE_PENDING,
));
$this
->assertFalse($group, t('Group was deleted.'));
$this
->assertFalse(og_is_member($gid), t('User is no longer registered to the new group.'));
}
function testGetEntityGroups() {
og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
og_create_field(OG_AUDIENCE_FIELD, 'node', 'article');
$admin_user = $this
->drupalCreateUser(array(
'access content',
'administer content types',
'create article content',
'edit any article content',
));
$this
->drupalLogin($admin_user);
$settings = array();
$settings['type'] = 'article';
$node = $this
->drupalCreateNode($settings);
$groups = array();
foreach (og_group_content_states() as $state => $value) {
$entity = entity_create('entity_test', array(
'name' => 'main',
'uid' => $admin_user->uid,
));
$entity->{OG_GROUP_FIELD}[LANGUAGE_NONE][0]['value'] = 1;
$entity
->save();
$group = og_get_group('entity_test', $entity->pid);
$groups[$state] = $group->gid;
$values = array(
'entity type' => 'node',
'entity' => $node,
'state' => $state,
);
og_group($group->gid, $values);
}
$node = node_load($node->nid);
foreach ($groups as $state => $gid) {
$this
->assertEqual(og_get_entity_groups('node', $node, array(
$state,
)), drupal_map_assoc(array(
$gid,
)), t('Group content is assigned to group @id with correct state', array(
'@id' => $gid,
)));
}
}
}
class OgNodeAccess extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Organic groups node access',
'description' => 'Test strict node access permissions for group nodes and group content.',
'group' => 'Organic groups',
);
}
function setUp() {
parent::setUp('og');
og_create_field(OG_GROUP_FIELD, 'node', 'page');
og_create_field(OG_AUDIENCE_FIELD, 'node', 'article');
$this->editor_user = $this
->drupalCreateUser(array(
'access content',
'edit any page content',
'edit any article content',
));
$this->group_manager = $this
->drupalCreateUser(array(
'access content',
'create page content',
'edit own article content',
'edit own page content',
));
$settings = array(
'type' => 'page',
OG_GROUP_FIELD . '[und][0][value]' => 1,
'uid' => $this->group_manager->uid,
);
$this->group_node = $this
->drupalCreateNode($settings);
$this->group = og_get_group('node', $this->group_node->nid);
$settings = array(
'type' => 'article',
'uid' => $this->group_manager->uid,
);
$this->group_content = $this
->drupalCreateNode($settings);
$values = array(
'entity type' => 'node',
'entity' => $this->group_content,
);
og_group($this->group->gid, $values);
}
function testStrictAccess() {
variable_set('og_node_access_strict', TRUE);
$this
->drupalLogin($this->editor_user);
$this
->drupalGet('node/' . $this->group_node->nid . '/edit');
$this
->assertResponse('403', t('A non-member with core node access permissions was denied access to edit group node.'));
$this
->drupalGet('node/' . $this->group_content->nid . '/edit');
$this
->assertResponse('403', t('A non-member with core node access permissions was denied access to edit group content node.'));
$this
->drupalLogin($this->group_manager);
$this
->drupalGet('node/' . $this->group_node->nid . '/edit');
$this
->assertResponse('200', t('Group manager allowed to access to edit group node.'));
$this
->drupalGet('node/' . $this->group_content->nid . '/edit');
$this
->assertResponse('200', t('Group manager allowed to access to edit group content node.'));
}
function testNoStrictAccess() {
variable_set('og_node_access_strict', FALSE);
$this
->drupalLogin($this->editor_user);
$this
->drupalGet('node/' . $this->group_node->nid . '/edit');
$this
->assertResponse('200', t('A non-member with core node access permissions was not denied access.'));
$this
->drupalGet('node/' . $this->group_content->nid . '/edit');
$this
->assertResponse('200', t('A non-member with core node access permissions was not denied access to edit group content node.'));
$this
->drupalLogin($this->group_manager);
$this
->drupalGet('node/' . $this->group_node->nid . '/edit');
$this
->assertResponse('200', t('Group manager allowed to access to edit group node.'));
$this
->drupalGet('node/' . $this->group_content->nid . '/edit');
$this
->assertResponse('200', t('Group manager allowed to access to edit group content node.'));
}
}
class OgGroupAndUngroup extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Organic groups group and ungroup',
'description' => 'Test the group and ungrouping of content with a group.',
'group' => 'Organic groups',
);
}
function setUp() {
parent::setUp('og', 'entity_test');
module_enable(array(
'entity_feature',
));
og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
og_create_field(OG_AUDIENCE_FIELD, 'node', 'article');
}
function testGroupAndUngroup() {
$admin_user = $this
->drupalCreateUser();
$web_user = $this
->drupalCreateUser();
$this
->drupalLogin($web_user);
$entity = entity_create('entity_test', array(
'name' => 'main',
'uid' => $admin_user->uid,
));
$entity->{OG_GROUP_FIELD}[LANGUAGE_NONE][0]['value'] = 1;
$entity
->save();
$group1 = og_get_group('entity_test', $entity->pid);
$entity = entity_create('entity_test', array(
'name' => 'main',
'uid' => $admin_user->uid,
));
$entity->{OG_GROUP_FIELD}[LANGUAGE_NONE][0]['value'] = 1;
$entity
->save();
$group2 = og_get_group('entity_test', $entity->pid);
$node = entity_create('node', array(
'type' => 'article',
'title' => $this
->randomName(),
'uid' => $web_user->uid,
));
entity_save('node', $node);
$this
->assertFalse(og_is_member($group1->gid, 'node', $node), t('Node is not assigned to group1.'));
$values = array(
'entity type' => 'node',
'entity' => $node,
);
og_group($group1->gid, $values);
$this
->assertTrue(og_is_member($group1->gid, 'node', $node), t('Node is now assigned to group1.'));
$this
->assertFalse(og_is_member($group2->gid, 'node', $node), t('Node is not assigned to group2.'));
$values = array(
'entity type' => 'node',
'entity' => $node,
);
og_group($group1->gid, $values);
og_group($group2->gid, $values);
$this
->assertTrue(og_is_member($group1->gid, 'node', $node), t('Node is still assigned to group1.'));
$this
->assertTrue(og_is_member($group2->gid, 'node', $node), t('Node is now assigned to group2.'));
og_ungroup($group2->gid, 'node', $node);
$this
->assertFalse(og_is_member($group2->gid, 'node', $node), t('Node is no longer assigned to group2.'));
}
}
class OgGroupMembership extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Organic groups group membership',
'description' => 'Test the group membership entity.',
'group' => 'Organic groups',
);
}
function setUp() {
parent::setUp('og');
}
function testUserEdit() {
$user1 = $this
->drupalCreateUser();
$user2 = $this
->drupalCreateUser();
$type = $this
->drupalCreateContentType()->type;
og_create_field(OG_GROUP_FIELD, 'node', $type);
$settings = array(
'type' => $type,
OG_GROUP_FIELD . '[und][0][value]' => 1,
);
$settings['uid'] = $user1->uid;
$node = $this
->drupalCreateNode($settings);
$group = og_get_group('node', $node->nid);
$this
->assertFalse(og_get_entity_groups('user', $user2, array(
OG_STATE_PENDING,
)), 'User has no pending membership, as expected.');
og_group($group->gid, array(
'entity' => $user2,
'state' => OG_STATE_PENDING,
));
$this
->drupalLogin($user2);
$this
->drupalPost("user/{$user2->uid}/edit", array(), 'Save');
$gids = og_get_entity_groups('user', $user2, array(
OG_STATE_PENDING,
));
$this
->assertEqual(array_values($gids), array(
1,
), 'User membership was retained after user save.');
$user2 = user_load($user2->uid, TRUE);
user_save($user2);
$gids = og_get_entity_groups('user', $user2, array(
OG_STATE_PENDING,
));
$this
->assertEqual(array_values($gids), array(
1,
), 'User membership was retained after programatic save.');
}
function testGroupMembershipCrud() {
module_enable(array(
'entity_feature',
));
og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
og_create_field(OG_AUDIENCE_FIELD, 'node', 'article');
$web_user = $this
->drupalCreateUser();
$entity = entity_create('entity_test', array(
'name' => 'main',
'uid' => $web_user->uid,
));
$entity->{OG_GROUP_FIELD}[LANGUAGE_NONE][0]['value'] = 1;
$entity
->save();
$group = og_get_group('entity_test', $entity->pid);
$group_membership = og_get_group_membership($group->gid, 'user', $web_user->uid);
$this
->assertTrue($group_membership, t('Group manager has group membership in new group.'));
$entity
->save();
$this
->assertTrue($group_membership, t('Group manager still has group membership in updated group.'));
$entity = entity_create('entity_test', array(
'name' => 'main',
'uid' => $web_user->uid,
));
$entity->{OG_GROUP_FIELD}[LANGUAGE_NONE][0]['value'] = 1;
$entity
->save();
$group = og_get_group('entity_test', $entity->pid);
$node = entity_create('node', array(
'type' => 'article',
'title' => $this
->randomName(),
'uid' => $web_user->uid,
));
entity_save('node', $node);
$this
->assertFalse(og_get_entity_groups('node', $node), t('Node is not assigned to any group.'));
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'og_membership')
->propertyCondition('entity_type', 'node', '=')
->propertyCondition('etid', $node->nid, '=')
->execute();
$this
->assertTrue(empty($result['og_membership']), t('Group content not assigned to a group does not have any group membership assigned to it.'));
$values = array(
'entity type' => 'node',
'entity' => $node,
);
og_group($group->gid, $values);
$group_membership = og_get_group_membership($group->gid, 'node', $node->nid);
$this
->assertTrue($group_membership, t('Group content has group membership in group.'));
$this
->assertEqual($group_membership->state, OG_STATE_ACTIVE, t('Group membership state is active.'));
$values = array(
'entity type' => 'node',
'entity' => $node,
'state' => OG_STATE_PENDING,
);
og_group($group->gid, $values);
$group_membership = og_get_group_membership($group->gid, 'node', $node->nid);
$this
->assertEqual($group_membership->state, OG_STATE_PENDING, t('Group membership state is pending.'));
og_ungroup($group->gid, 'node', $node);
$group_membership = og_get_group_membership($group->gid, 'node', $node->nid);
$this
->assertFalse($group_membership, t('Group content no longer has group membership in group.'));
$settings = array();
$settings['type'] = 'article';
$settings[OG_AUDIENCE_FIELD][LANGUAGE_NONE][0]['gid'] = $group->gid;
$node = $this
->drupalCreateNode($settings);
$group_membership = og_get_group_membership($group->gid, 'node', $node->nid);
$this
->assertTrue($group_membership, t('New group content has group membership in group.'));
$nid = $node->nid;
node_delete($nid);
$group_membership = og_get_group_membership($group->gid, 'node', $nid);
$this
->assertFalse($group_membership, t('Group membership was deleted for deleted group content.'));
$gid = $group->gid;
$group
->delete();
$result = $query
->entityCondition('entity_type', 'og_membership')
->propertyCondition('entity_type', 'node', '=')
->propertyCondition('gid', $gid, '=')
->execute();
$this
->assertTrue(empty($result['og_membership']), t('There are no group memberships assocaited with a deleted group.'));
}
}
class OgUserPermissionsTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Organic groups role permissions',
'description' => 'Verify that role permissions can be added and removed via API.',
'group' => 'Organic groups',
);
}
function setUp() {
parent::setUp('og', 'entity_test');
module_enable(array(
'entity_feature',
));
og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
og_create_field(OG_AUDIENCE_FIELD, 'node', 'article');
}
function testOgUserRoleChangePermissions() {
$this
->resetAll();
$user1 = $this
->drupalCreateUser();
$property = OG_GROUP_FIELD;
$entity = entity_create('entity_test', array(
'name' => 'main',
'uid' => $user1->uid,
));
$entity->{$property}[LANGUAGE_NONE][0]['value'] = 1;
$entity
->save();
$group = og_get_group('entity_test', $entity->pid);
$user2 = $this
->drupalCreateUser();
$values = array(
'entity' => $user2,
);
og_group($group->gid, $values);
$this
->assertTrue(og_is_member($group->gid, 'user', $user2), t('User is registered to the new group.'));
$this
->assertFalse(og_user_access($group->gid, 'update own article content', $user2), t('User does not have "update own article content" permission.'));
$this
->assertFalse(og_user_access($group->gid, 'delete own article content', $user2), t('User does not have "delete own article content" permission.'));
$this
->assertFalse(og_user_access($group->gid, 'administer group', $user2), t('User does not have "administer group" permission.'));
$roles = array_flip(og_get_global_roles());
$rid = $roles[OG_AUTHENTICATED_ROLE];
$permissions = array(
'delete own article content' => 1,
'administer group' => 1,
);
og_role_change_permissions($rid, $permissions);
$this
->assertFalse(og_user_access($group->gid, 'update own article content', $user2), t('User still does not have "update own article content" permission.'));
$this
->assertTrue(og_user_access($group->gid, 'delete own article content', $user2), t('User now has "delete own article content" permission.'));
$this
->assertTrue(og_user_access($group->gid, 'administer group', $user2), t('User now has "administer group" permission.'));
$permissions = array(
'delete own article content' => 1,
'administer group' => 0,
);
og_role_change_permissions($rid, $permissions);
$this
->assertTrue(og_user_access($group->gid, 'delete own article content', $user2), t('User still has "delete own article content" permission.'));
$this
->assertFalse(og_user_access($group->gid, 'administer group', $user2), t('User no longer has "administer group" permission.'));
}
}
class OgDefaultAccessFieldTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Organic groups default access field',
'description' => 'Test groups with default access field enabled or disabled.',
'group' => 'Organic groups',
);
}
function setUp() {
parent::setUp('og', 'entity_test');
module_enable(array(
'entity_feature',
));
}
function testOgDefaultAccessField() {
$web_user = $this
->drupalCreateUser();
og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
$entity = entity_create('entity_test', array(
'name' => 'main',
'uid' => $web_user->uid,
));
$entity->{OG_GROUP_FIELD}[LANGUAGE_NONE][0]['value'] = 1;
$entity
->save();
$group = og_get_group('entity_test', $entity->pid);
$this
->assertEqual(og_get_global_roles(), og_roles($group->gid), t('Group without default access field is assigned to the global roles and permissions settings.'));
og_create_field(OG_DEFAULT_ACCESS_FIELD, 'entity_test', 'main');
$entity = entity_create('entity_test', array(
'name' => 'main',
'uid' => $web_user->uid,
));
$entity->{OG_GROUP_FIELD}[LANGUAGE_NONE][0]['value'] = 1;
$entity->{OG_DEFAULT_ACCESS_FIELD}[LANGUAGE_NONE][0]['value'] = 0;
$entity
->save();
$group = og_get_group('entity_test', $entity->pid);
$this
->assertEqual(og_get_global_roles(), og_roles($group->gid), t('Group with default access field disabled is assigned to the global roles and permissions settings.'));
$entity = entity_create('entity_test', array(
'name' => 'main',
'uid' => $web_user->uid,
));
$entity->{OG_GROUP_FIELD}[LANGUAGE_NONE][0]['value'] = 1;
$entity->{OG_DEFAULT_ACCESS_FIELD}[LANGUAGE_NONE][0]['value'] = 1;
$entity
->save();
$group = og_get_group('entity_test', $entity->pid);
$this
->assertNotEqual(og_get_global_roles(), og_roles($group->gid), t('Group with default access field enabled has own roles and permissions settings.'));
$entity->{OG_DEFAULT_ACCESS_FIELD}[LANGUAGE_NONE][0]['value'] = 0;
$entity
->save();
$this
->assertEqual(og_get_global_roles(), og_roles($group->gid), t('Group with enabled default access field that was disabled is assigned to the global roles and permissions settings.'));
}
}
class OgTranslationTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Organic groups multilangual',
'description' => 'Test tranlatable node that is a group, returns the same group ID.',
'group' => 'Organic groups',
);
}
function setUp() {
parent::setUp('translation', 'translation_test', 'og');
}
function testOgNodeLocale() {
$web_user = $this
->drupalCreateUser(array(
'administer languages',
'administer content types',
'access administration pages',
'create page content',
'edit own page content',
'translate content',
));
$this
->drupalLogin($web_user);
$this
->addLanguage('en');
$this
->addLanguage('es');
$this
->drupalGet('admin/structure/types/manage/page');
$edit = array();
$edit['language_content_type'] = 2;
$this
->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
og_create_field(OG_GROUP_FIELD, 'node', 'page');
$node_title = $this
->randomName();
$node_body = $this
->randomName();
$node = $this
->createPage($node_title, $node_body, 'en');
$node_translation_title = $this
->randomName();
$node_translation_body = $this
->randomName();
$node_translation = $this
->createTranslation($node, $node_translation_title, $node_translation_body, 'es');
$group1 = og_get_group('node', $node->nid);
$group2 = og_get_group('node', $node_translation->nid);
$this
->assertEqual($group1, $group2, t('Node and tranlated node are the same group.'));
$this
->drupalGet('node/' . $node_translation->nid . '/edit');
$message = t('You can not change "Group" field from a translated content.');
$this
->assertText($message, t('Group field on tranlated node does not allow editing.'));
$this
->drupalGet('node/' . $node->nid . '/edit');
$this
->assertNoText($message, t('Group field on original node allows editing.'));
}
function addLanguage($language_code) {
$this
->drupalGet('admin/config/regional/language');
if (strpos($this
->drupalGetContent(), 'enabled[' . $language_code . ']') === FALSE) {
$edit = array();
$edit['langcode'] = $language_code;
$this
->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
drupal_static_reset('language_list');
$languages = language_list('language');
}
elseif ($this
->xpath('//input[@type="checkbox" and @name=:name and @checked="checked"]', array(
':name' => 'enabled[' . $language_code . ']',
))) {
}
else {
$this
->drupalPost(NULL, array(
'enabled[' . $language_code . ']' => TRUE,
), t('Save configuration'));
}
}
function createPage($title, $body, $language) {
$edit = array();
$property = OG_GROUP_FIELD;
$langcode = LANGUAGE_NONE;
$edit["title"] = $title;
$edit["body[{$langcode}][0][value]"] = $body;
$edit['language'] = $language;
$this
->drupalPost('node/add/page', $edit, t('Save'));
$node = $this
->drupalGetNodeByTitle($title);
$this
->assertTrue($node, t('Node found in database.'));
return $node;
}
function createTranslation($node, $title, $body, $language) {
$this
->drupalGet('node/add/page', array(
'query' => array(
'translation' => $node->nid,
'target' => $language,
),
));
$body_key = "body[und][0][value]";
$this
->assertFieldByXPath('//input[@id="edit-title"]', $node->title, t('Original title value correctly populated.'));
$this
->assertFieldByXPath("//textarea[@name='{$body_key}']", $node->body[LANGUAGE_NONE][0]['value'], t('Original body value correctly populated.'));
$edit = array();
$edit["title"] = $title;
$edit[$body_key] = $body;
$this
->drupalPost(NULL, $edit, t('Save'));
$this
->assertRaw(t('Basic page %title has been created.', array(
'%title' => $title,
)), t('Translation created.'));
$translation = $this
->drupalGetNodeByTitle($title);
$this
->assertTrue($translation, t('Node found in database.'));
$this
->assertTrue($translation->tnid == $node->nid, t('Translation set id correctly stored.'));
return $translation;
}
}
class OgGroupAudienceField extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Organic groups field group audience',
'description' => 'Test the field group audience functionality.',
'group' => 'Organic groups',
);
}
function setUp() {
parent::setUp('og_test', 'entity_test');
module_enable(array(
'entity_feature',
));
og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
og_create_field(OG_AUDIENCE_FIELD, 'node', 'article');
}
function testOgAudienceFieldBasic() {
$web_user = $this
->drupalCreateUser(array(
'access content',
'administer content types',
'create article content',
'edit any article content',
));
$this
->drupalLogin($web_user);
$this
->drupalGet('node/add/article');
$this
->assertText(t('There are no groups you can select from.'), 'Field group audience shows correct description about no groups.');
$entity = entity_create('entity_test', array(
'name' => 'main',
'uid' => $web_user->uid,
));
$entity->{OG_GROUP_FIELD}[LANGUAGE_NONE][0]['value'] = 1;
$entity
->save();
$group = og_get_group('entity_test', $entity->pid);
$this
->drupalGet('node/add/article');
$this
->assertText(t('Select the groups this content should be associated with.'), 'Field group audience shows correct description about existing groups.');
}
function testPropopulateViaUrl() {
$admin_user = $this
->drupalCreateUser(array(
'access content',
'administer content types',
'create article content',
'edit any article content',
));
$this
->drupalLogin($admin_user);
og_create_field(OG_GROUP_FIELD, 'node', 'article');
$groups = array();
foreach (range(1, 3) as $id) {
$settings = array();
$settings['type'] = 'article';
$settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 1;
$node = $this
->drupalCreateNode($settings);
$group = og_get_group('node', $node->nid);
$groups[$id] = $group;
}
foreach (range(4, 6) as $id) {
$entity = entity_create('entity_test', array(
'name' => 'main',
'uid' => $admin_user->uid,
));
$entity->{OG_GROUP_FIELD}[LANGUAGE_NONE][0]['value'] = 1;
$entity
->save();
$group = og_get_group('entity_test', $entity->pid);
$groups[$id] = $group;
}
$id = 'edit-group-audience-und';
$this
->drupalGet('node/add/article');
foreach ($groups as $group) {
$this
->assertNoOptionSelected($id, $group->gid);
}
$options = array(
'query' => array(
'gids_node[]' => implode(',', array(
$groups[1]->etid,
$groups[2]->etid,
)),
'gids_entity_test[]' => implode(',', array(
$groups[4]->etid,
$groups[5]->etid,
)),
),
);
$this
->drupalGet('node/add/article', $options);
foreach ($groups as $group) {
$op = !in_array($group->gid, array(
3,
6,
)) ? 'assertOptionSelected' : 'assertNoOptionSelected';
$this
->{$op}($id, $group->gid);
}
$group = $groups[1];
$options = array(
'query' => array(
'gids_group[]' => $group->gid,
),
);
$this
->drupalGet('node/add/article', $options);
foreach ($groups as $group) {
$op = $group->gid == 1 ? 'assertOptionSelected' : 'assertNoOptionSelected';
$this
->{$op}($id, $group->gid);
}
$options = array(
'query' => array(
'gids_invalid_entity[]' => $group->gid,
),
);
$this
->drupalGet('node/add/article', $options);
foreach ($groups as $group) {
$this
->assertNoOptionSelected($id, $group->gid);
}
$options = array(
'query' => array(
'gids_node[]' => 200,
),
);
$this
->drupalGet('node/add/article', $options);
foreach ($groups as $group) {
$this
->assertNoOptionSelected($id, $group->gid);
}
}
function testPropopulateViaUrlHidden() {
variable_set('og_test_hide_audience', TRUE);
$web_user = $this
->drupalCreateUser(array(
'access content',
'create article content',
));
$this
->drupalLogin($web_user);
$entity = entity_create('entity_test', array(
'name' => 'main',
'uid' => $web_user->uid,
));
$entity->{OG_GROUP_FIELD}[LANGUAGE_NONE][0]['value'] = 1;
$entity
->save();
$group = og_get_group('entity_test', $entity->pid);
$this
->drupalGet('node/add/article');
$this
->assertNoField('group_audience[und][]', t('OG audience field is hidden.'));
$edit['title'] = $this
->randomName(8);
$this
->drupalPost(NULL, $edit, t('Save'));
$node = node_load(1);
$this
->assertFalse(og_is_member($group->gid, 'node', $node), t('Node does not belong to a group.'));
$this
->drupalGet('node/add/article', array(
'query' => array(
'gids_entity_test' => $entity->pid,
),
));
$edit['title'] = $this
->randomName(8);
$this
->drupalPost(NULL, $edit, t('Save'));
$node = node_load(2);
$this
->assertTrue(og_is_member($group->gid, 'node', $node), t('Node belongs to group.'));
}
function testHiddenSelectedGids() {
$permissions = array(
'access content',
'bypass node access',
);
$user1 = $this
->drupalCreateUser($permissions);
$user2 = $this
->drupalCreateUser($permissions);
$permissions[] = 'administer group';
$admin_user = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($admin_user);
$groups = array();
foreach (range(1, 3) as $id) {
$entity = entity_create('entity_test', array(
'name' => 'main',
'uid' => $id < 3 ? $user1->uid : $user2->uid,
));
$entity->{OG_GROUP_FIELD}[LANGUAGE_NONE][0]['value'] = 1;
$entity
->save();
$group = og_get_group('entity_test', $entity->pid);
$groups[$id] = $group;
}
$settings = array();
$settings['type'] = 'article';
$settings[OG_AUDIENCE_FIELD][LANGUAGE_NONE][0]['gid'] = $groups[2]->gid;
$settings[OG_AUDIENCE_FIELD][LANGUAGE_NONE][1]['gid'] = $groups[3]->gid;
$node = $this
->drupalCreateNode($settings);
$this
->assertEqual(og_get_entity_groups('node', $node), drupal_map_assoc(array(
$groups[2]->gid,
$groups[3]->gid,
)), t('Article node is assigned to two groups.'));
$this
->drupalLogin($user2);
$this
->drupalGet('node/' . $node->nid . '/edit');
$edit = array();
$edit['group_audience[und][]'] = array();
$this
->drupalPost(NULL, $edit, t('Save'));
og_group_membership_invalidate_cache();
$this
->assertEqual(og_get_entity_groups('node', $node), drupal_map_assoc(array(
$groups[2]->gid,
)), t('Article node is still assigned to a group that is hidden from the current user.'));
}
function testOgAudienceFieldDeletedGroup() {
$admin_user = $this
->drupalCreateUser(array(
'access content',
'administer content types',
'create article content',
'edit any article content',
));
$web_user = $this
->drupalCreateUser(array(
'access content',
'administer content types',
'create article content',
'edit any article content',
));
$this
->drupalLogin($web_user);
$groups = array();
for ($i = 0; $i <= 2; $i++) {
$entity = entity_create('entity_test', array(
'name' => 'main',
'uid' => $admin_user->uid,
));
$entity->{OG_GROUP_FIELD}[LANGUAGE_NONE][0]['value'] = 1;
$entity
->save();
$group = og_get_group('entity_test', $entity->pid);
$groups[$i] = $group;
og_group($group->gid, array(
'entity' => $web_user,
));
}
$web_user = user_load($web_user->uid, TRUE);
for ($i = 0; $i <= 2; $i++) {
$gid = $groups[$i]->gid;
$this
->assertEqual($web_user->{OG_AUDIENCE_FIELD}[LANGUAGE_NONE][$i]['gid'], $gid, t("Group ID @id found in user's group-audienec field.", array(
'@id' => $gid,
)));
}
$first_gid = $groups[0]->gid;
entity_delete('entity_test', $groups[0]->etid);
$group = og_load($first_gid);
$this
->assertFalse($group, t('Group was deleted'));
$this
->assertEqual($web_user->{OG_AUDIENCE_FIELD}[LANGUAGE_NONE][0]['gid'], $first_gid, t("Group ID 1 is still found in user's group-audience field."));
$this
->drupalPost('user/' . $web_user->uid . '/edit', array(), t('Save'));
$web_user = user_load($web_user->uid, TRUE);
$this
->assertEqual(count($web_user->{OG_AUDIENCE_FIELD}[LANGUAGE_NONE]), 2, t('After user save, group-audience field has 2 items.'));
for ($i = 0; $i <= 1; $i++) {
$gid = $groups[$i + 1]->gid;
$this
->assertEqual($web_user->{OG_AUDIENCE_FIELD}[LANGUAGE_NONE][$i]['gid'], $gid, t("Group ID @id found in user's group-audienec field.", array(
'@id' => $gid,
)));
}
}
}
class OgUnInstallTestCase extends DrupalWebTestCase {
public static function __getInfo() {
return array(
'name' => 'Organic groups install, uninstall and re-install',
'description' => 'Test the installing, uninstalling and re-installing of the Organic groups module',
'group' => 'Organic groups',
);
}
function setUp() {
parent::setUp('og');
}
function testOgInstall() {
$global_roles = og_get_global_roles();
$this
->assertFalse(array_diff($global_roles, array(
OG_ANONYMOUS_ROLE,
OG_AUTHENTICATED_ROLE,
OG_ADMINISTRATOR_ROLE,
)), t('Global roles were created on module installation.'));
module_disable(array(
'og',
));
drupal_uninstall_modules(array(
'og',
), FALSE);
$og_fields = field_read_fields(array(
'module' => 'og',
), array(
'include_inactive' => TRUE,
));
$this
->assertTrue(empty($og_fields), 'Uninstalling organic groups module removes all fields owned by the module.');
module_enable(array(
'og',
));
$modules = module_list(TRUE);
$this
->assertFalse(empty($modules['og']), 'Enabling organic groups module after uninstall is sucessful.');
}
}
class OgMigrateGroupMembershipTestCase extends OgUpgradePathTestCase {
public static function __getInfo() {
return array(
'name' => 'Organic groups migrate group membership',
'description' => 'Tests the data migrate of Organic groups of creating group memberships.',
'group' => 'Organic groups',
'dependencies' => array(
'og_migrate',
),
);
}
public function setUp() {
$this->databaseDumpFiles = array(
drupal_get_path('module', 'og') . '/tests/drupal-7.og.update_7001.database.php',
);
parent::setUp();
$this
->drupalLogout();
$admin_user = $this
->drupalCreateUser(array(
'access administration pages',
'administer software updates',
));
$this
->drupalLogin($admin_user);
$this
->performUpgrade();
module_enable(array(
'og_migrate',
));
menu_rebuild();
variable_set('og_update_batch_size', 3);
}
public function testOgUpgrade() {
$edit = array(
'migrate[group_membership]' => TRUE,
);
$this
->drupalPost('admin/config/group/group-migrate', $edit, t('Migrate'));
$group_memberships = og_membership_load_multiple(FALSE);
$info = array(
2 => OG_STATE_ACTIVE,
3 => OG_STATE_ACTIVE,
4 => OG_STATE_PENDING,
5 => OG_STATE_BLOCKED,
);
$this
->assertEqual(count($group_memberships), count($info), t('All group membership enteties exist.'));
foreach ($group_memberships as $key => $group_membership) {
$this
->assertEqual($group_membership->state, $info[$group_membership->etid], t('User ID @id has correct state', array(
'@id' => $key + 1,
)));
}
$this
->assertEqual($group_memberships[2]->created, 1000000000, t('Create date was upgraded.'));
}
}