Newer
Older
* Test the Organic groups API and CRUD handling.
class OgGroupApi extends DrupalWebTestCase {
'name' => 'Organic groups API and CRUD',
'description' => 'Test the create, update and remove of group entities and API functions.',
parent::setUp('og', 'entity_test');
Amitaibu
committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
* Test access control of groups.
*/
function testOgAccess() {
module_enable(array('og_test'));
og_create_field(OG_GROUP_FIELD, 'node', 'article');
// Create users.
$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);
// Assert user can update entity that is the group.
$this->assertTrue($group->access('update', $admin_user), t('User can update a node that is a group.'));
// Add node a with "deny access" in title, so it will return no access. See
// og_test_node_access().
$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.'));
}
/**
* Test CRUD of group entities.
$node = entity_create('node', array(
'type' => 'article',
'title' => $this->randomName(),
'uid' => 1
));
entity_save('node', $node);
$group = og_get_group('node', $node->nid, TRUE);
// Assert is new property.
$this->assertTrue($group->is_new, t('New group has "is new" property.'));
// Assert default state.
$this->assertTrue($group->state == OG_STATE_ACTIVE, t('Default state property is active.'));
// Assert default creation time.
$this->assertTrue($group->created, t('Group creating time was added to group.'));
// Assert group ID not set.
$this->assertTrue(empty($group->gid), t('Group ID not set for unsaved group.'));
// Save the group.
// Assert group ID was set.
$this->assertTrue(!empty($group->gid), t('Group ID was set for saved group.'));
// Set a new state for the group.
// Assert group isn't loaded, when state is pending and state isn't
// specifically stated.
$this->assertFalse($group, t('Pending state group was not loaded, as it was not requested.'));
// Reload group to make sure state was updated.
$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 = og_get_group('node', $node->nid, FALSE, array(), TRUE);
$this->assertFalse($group, t('Group was deleted.'));
/**
* Test og_get_group_ids().
*
* Create a few groups of different entities. and check we get their IDs.
*/
function testOgGetGroupIds() {
// List keyed by the group ID and their entity type, ID as value.
$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.'));
// Ask for a group that is pending, but result should include only active.
$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.'));
// Check the state conditions filters results.
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.'));
// Entity type that doesn't exist.
$gids = og_get_group_ids('entity_test_non_exist', array(1));
$this->assertFalse($gids, t('Non existent entity type was not returned.'));
// Entity Id that doesn't exist.
$gids = og_get_group_ids('entity_test', array(100));
$this->assertFalse($gids, t('Non existent entity ID was not returned.'));
// Test caching is correct and resets properly when the state changes.
// We can check the cache itself, by getting it (not be reference) and
// making sure it has the correct values.
$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.');
// Get all groups (i.e. get by "group" entity type).
$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.');
}
// Add OG group field to the entity_test's "main" bundle.
og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
Amitaibu
committed
$web_user = $this->drupalCreateUser();
// Create an entity.
$property = OG_GROUP_FIELD;
Amitaibu
committed
$entity = entity_create('entity_test', array('name' => 'main', 'uid' => $web_user->uid));
$entity->{$property}[LANGUAGE_NONE][0]['value'] = 0;
// Assert no group was created.
$group = og_get_group('entity_test', $entity->pid);
$this->assertTrue(empty($group->gid), t('Group was not created.'));
// Assert group was created, and was already saved, and its state is active
$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.'));
// Assert the user is registered to the new group.
Amitaibu
committed
$this->assertTrue(og_is_member($group->gid, 'user', $web_user), t('User is registered to the new group.'));
// Assert group's state was set to pending.
$entity->{$property}[LANGUAGE_NONE][0]['value'] = 0;
$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.'));
// Delete the entity, and assert the group was deleted.
$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.'));
// Assert user no longer belongs to group.
$this->assertFalse(og_is_member($gid), t('User is no longer registered to the new group.'));
}
/**
* Test the og_get_entity_groups() API function.
*/
function testGetEntityGroups() {
// Add OG group field to the entity_test's "main" bundle.
og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
// Add OG audience field to the node's "article" bundle.
og_create_field(OG_AUDIENCE_FIELD, 'node', 'article');
// Create users.
$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();
// Create group enteties.
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;
// Assign article to the group.
og_group($group->gid, 'node', $node, $state);
}
$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)));
}
* TODO: Test Group content handeling.
class OgGroupAndUngroup extends DrupalWebTestCase {
public static function getInfo() {
'name' => 'Organic groups group and ungroup',
'description' => 'Test the group and ungrouping of content with a group.',
parent::setUp('og', 'entity_test');
module_enable(array('entity_feature'));
// Add OG group field to the entity_test's "main" bundle.
og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
// Add OG audience field to the node's "article" bundle.
og_create_field(OG_AUDIENCE_FIELD, 'node', 'article');
/**
* Test group and ungroup of content.
*/
function testGroupAndUngroup() {
$admin_user = $this->drupalCreateUser();
$web_user = $this->drupalCreateUser();
$this->drupalLogin($web_user);
$this->drupalGet('node/add/article');
// Create a group.
$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.'));
og_group($group1->gid, 'node', $node);
$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.'));
og_group($group1->gid, 'node', $node);
og_group($group2->gid, 'node', $node);
$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 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.',
parent::setUp('og', 'entity_test');
module_enable(array('entity_feature'));
// Add OG group field to the entity_test's "main" bundle.
og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
// Add OG audience field to the node's "article" bundle.
og_create_field(OG_AUDIENCE_FIELD, 'node', 'article');
/**
* Verify proper permission changes by og_role_change_permissions().
*/
function testOgUserRoleChangePermissions() {
// TODO: We need to invalidate cache as the og permissions are fired before
// the field was added to the article content type. But this is a hack, and
// should be removed.
$this->resetAll();
// Create user.
$user1 = $this->drupalCreateUser();
// Create an entity.
$property = OG_GROUP_FIELD;
Amitaibu
committed
$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);
// Associate user to the group.
$user2 = $this->drupalCreateUser();
og_group($group->gid, 'user', $user2);
// Assert the user is registered to the new group.
$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.'));
// Change permissions to authenticated member.
$roles = array_flip(og_get_global_roles());
// Authenticated role ID.
'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');
}
/**
* Test groups with default access field enabled or disabled.
*
* - Group without default access field.
* - Group with default access field enabled.
* - Previous group with field disabled.
*/
function testOgDefaultAccessField() {
// Create user.
Amitaibu
committed
$web_user = $this->drupalCreateUser();
// Add OG group field to the entity_test's "main" bundle.
og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
// Group without default access field.
Amitaibu
committed
$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.'));
// Add default access field to the entity_test's "main" bundle.
og_create_field(OG_DEFAULT_ACCESS_FIELD, 'entity_test', 'main');
// Group with default access field disabled.
Amitaibu
committed
$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.'));
// Group with default access field enabled.
Amitaibu
committed
$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.'));
// Disable existing group's default access field.
$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.'));
}
}
/**
* Test the multilangual groups.
*/
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');
/**
* Test disabling OG fields on translated content.
*/
function testOgNodeLocale() {
Amitaibu
committed
$web_user = $this->drupalCreateUser(array(
'administer languages',
'administer content types',
'access administration pages',
'create page content',
'edit own page content',
'translate content',
));
Amitaibu
committed
$this->drupalLogin($web_user);
// Add languages.
$this->addLanguage('en');
$this->addLanguage('es');
// Set "Basic page" content type to use multilingual support with translation.
$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'));
// Add OG group field to the node's "page" bundle.
og_create_field(OG_GROUP_FIELD, 'node', 'page');
// Create Basic page in English.
$node_title = $this->randomName();
$node_body = $this->randomName();
$node = $this->createPage($node_title, $node_body, 'en');
// Submit translation in Spanish.
$node_translation_title = $this->randomName();
$node_translation_body = $this->randomName();
$node_translation = $this->createTranslation($node, $node_translation_title, $node_translation_body, 'es');
// Assert both nodes return the same group.
$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 type" 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.'));
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/**
* Install a the specified language if it has not been already. Otherwise make sure that
* the language is enabled.
*
* @param string $language_code The language code the check.
*/
function addLanguage($language_code) {
// Check to make sure that language has not already been installed.
$this->drupalGet('admin/config/regional/language');
if (strpos($this->drupalGetContent(), 'enabled[' . $language_code . ']') === FALSE) {
// Doesn't have language installed so add it.
$edit = array();
$edit['langcode'] = $language_code;
$this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
// Make sure we're not using a stale list.
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 . ']'))) {
// It's installed and enabled. No need to do anything.
}
else {
// It's installed but not enabled. Enable it.
$this->drupalPost(NULL, array('enabled[' . $language_code . ']' => TRUE), t('Save configuration'));
}
}
/**
* Create a "Basic page" in the specified language, and set it to be a group.
*
* @param $title
* Title of basic page in specified language.
* @param $body
* Body of basic page in specified language.
* @param $language
* Language code.
* @param group
* TRUE to set node as group. FALSE by default.
*/
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
$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'));
// Check to make sure the node was created.
$node = $this->drupalGetNodeByTitle($title);
$this->assertTrue($node, t('Node found in database.'));
return $node;
}
/**
* Create a translation for the specified basic page in the specified language.
*
* @param object $node The basic page to create translation for.
* @param string $title Title of basic page in specified language.
* @param string $body Body of basic page in specified language.
* @param string $language Language code.
*/
function createTranslation($node, $title, $body, $language) {
$this->drupalGet('node/add/page', array('query' => array('translation' => $node->nid, 'target' => $language)));
$body_key = "body[$language][0][value]";
$this->assertFieldByXPath('//input[@id="edit-title"]', $node->title, "Original title value correctly populated.");
$this->assertFieldByXPath("//textarea[@name='$body_key']", $node->body[$node->language][0]['value'], "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.'));
// Check to make sure that translation was successful.
$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;
}
}
/**
* Test group audience field.
*
* TODO:
* - User with no permissions.
* - User with and without permissions for opt-group.
* - Opt group enabled or disabled.
* - Editing existing field.
* - Prepopulate via URL.
* - Change input from checkboxes to select.
*/
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', 'entity_test');
module_enable(array('entity_feature'));
// Add OG group field to the entity_test's "main" bundle.
og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
// Add OG audience field to the node's "article" bundle.
og_create_field(OG_AUDIENCE_FIELD, 'node', 'article');
/**
* No groups and a group is added.
*/
function testOgAudienceFieldBasic() {
Amitaibu
committed
$web_user = $this->drupalCreateUser(array(
'access content',
'administer content types',
'create article content',
'edit any article content',
));
Amitaibu
committed
$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.');
// Create an entity.
Amitaibu
committed
$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.');
// TODO: Why is the extra space needed in the result?
$this->assertFieldByXPath('//label[@for="edit-group-audience-und-' . $group->gid . '"]', $group->label . ' ', 'Field group audience shows the existing group.');
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
function testOptGroup() {
// Create users.
$admin_user = $this->drupalCreateUser(array(
'access content',
'administer content types',
'create article content',
'edit any article content',
'administer group',
));
$web_user = $this->drupalCreateUser(array(
'access content',
'administer content types',
'create article content',
'edit any article content',
));
// Assert no group options are presented to any of the users.
foreach (array($admin_user, $web_user) as $account) {
$this->drupalLogin($account);
$this->drupalGet('node/add/article');
$this->assertText(t('There are no groups you can select from.'), t('no group options are presented to any of the users.'));
}
// Create one group that belongs to each user.
$groups = array();
foreach (array($admin_user, $web_user) as $account) {
$settings = array();
$settings['type'] = 'article';
$settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 1;
$settings['uid'] = $account->uid;
$node = $this->drupalCreateNode($settings);
$group = og_get_group('node', $node->nid);
$groups[] = $group;
}
list($admin_group, $web_group) = $groups;
// Assert admin user sees opt group with both groups.
// and web user sees only their own group.
$this->drupalLogin($admin_user);
$this->drupalGet('node/add/article');
// TODO: Get the group label in the xpath instead of just group ID.
$this->assertFieldByXPath('//optgroup[@label="My groups"]/option', $admin_group->gid, t('Field group audience shows the group to user may see.'));
$this->assertFieldByXPath('//optgroup[@label="Other groups"]/option', $web_group->gid, t('Field group audience shows the group to user may see.'));
$this->drupalLogin($web_user);
$this->drupalGet('node/add/article');
$this->assertFieldByXPath('//label[@for="edit-group-audience-und-' . $web_group->gid . '"]', $web_group->label . ' ', t('Field group audience shows the group to user may see.'));
$this->assertNoFieldByXPath('//label[@for="edit-group-audience-und-' . $admin_group->gid . '"]', $admin_group->label . ' ', t('Field group audience is shown without opt group to non-privileged user.'));
}
/**
* Test prepopulating the audience field widget via URL.
*/
function testPropopulateViaUrl() {
// Create users.
$admin_user = $this->drupalCreateUser(array(
'access content',
'administer content types',
'create article content',
'edit any article content',
));
$this->drupalLogin($admin_user);
// Add OG group field to the "article" bundle.
og_create_field(OG_GROUP_FIELD, 'node', 'article');
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
$groups = array();
// Create two node groups.
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;
}
// Create two entity_test 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;
}
// Assert all 6 groups are unselected in the audience field.
$this->drupalGet('node/add/article');
foreach ($groups as $group) {
$this->assertNoFieldChecked('edit-group-audience-und-' . $group->gid);
}
// Assert all entity types passed in the URL as selected.
$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)) ? 'assertFieldChecked' : 'assertNoFieldChecked';
$this->$op('edit-group-audience-und-' . $group->gid);
}
// Assert all groups passed in the URL are selected.
$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 ? 'assertFieldChecked' : 'assertNoFieldChecked';
$this->$op('edit-group-audience-und-' . $group->gid);
}
// Pass invalid entity types, check nothing is selected.
$options = array('query' => array('gids_invalid_entity[]' => $group->gid));
$this->drupalGet('node/add/article', $options);
foreach ($groups as $group) {
$this->assertNoFieldChecked('edit-group-audience-und-' . $group->gid);
}
// Pass invalid group IDs, check nothing is selected.
$options = array('query' => array('gids_node[]' => 200));
$this->drupalGet('node/add/article', $options);
foreach ($groups as $group) {
$this->assertNoFieldChecked('edit-group-audience-und-' . $group->gid);
}
}
}
/**
* Test re-installation of module.
*/
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', 'entity_test');
}
/**
* Instaill, uninstall and re-install.
*/
function testOgInstall() {
// Assert default roles and permissions were created.
$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.'));
// Uninstall module. Assert the deletion of fields on uninstall.
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.');
// Re-enable module.
module_enable(array('og'));
$modules = module_list(TRUE);
$this->assertFalse(empty($modules['og']), 'Enabling organic groups module after uninstall is sucessful.');
}
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
}
/**
* Upgrade test.
*
* Load a filled installation of Drupal 6 and run the upgrade process on it.
*/
class OgUpgradePathTestCase extends UpgradePathTestCase {
public static function getInfo() {
return array(
'name' => 'Organic groups upgrade path',
'description' => 'Tests the upgrade path of Organic groups.',
'group' => 'Organic groups',
);
}
public function setUp() {
// Path to the database dump.
$this->databaseDumpFiles = array(
drupal_get_path('module', 'og') . '/tests/drupal-6.og.database.php',
);
parent::setUp();
}
/**
* Test a successful upgrade.
*/
public function testOgUpgrade() {
// The upgrade should "fail" since OG won't upgrade until it's enabled.
$this->performUpgrade(FALSE);
$this->assertText('You must enable Organic groups module to perform an upgrade from Drupal 6', t('Organic groups did not upgrade when module is disabled.'));
// TODO: Understand why this workround is needed to load all entity classes.
module_enable(array('entity'));
module_load_include('inc', 'entity' ,'includes/entity');
module_load_include('inc', 'entity' ,'includes/entity.controller');
module_enable(array('og'));
$this->performUpgrade(FALSE);
// Assert according to the scenario Drupal 6's test table dump was created.
// TODO: Assert OG fields.
$group = og_get_group('node', 1);
$this->assertEqual($group->state, OG_STATE_ACTIVE, t('Node ID 1 is an active group.'));
// Assert description was converted to a field.
$node = node_load(1);
$this->assertTrue($node->og_description[LANGUAGE_NONE][0]['value'] == 'description group without posts.', t('Description fields has correct data.'));
$group = og_get_group('node', 2);
$this->assertEqual($group->state, OG_STATE_ACTIVE, t('Node ID 2 is an active group.'));
// Test group content with NID 3 - 5 belong to the group with NID 2.
foreach (range(3, 5) as $nid) {
$node = node_load($nid);
$this->assertTrue(og_is_member($group->gid, 'node', $node), t('Node ID @nid is a group content of Node ID 2', array('@nid' => $nid)));
}
// Orphan group content (i.e. not attached to a group).
$group = og_get_group('node', 6);
$this->assertFalse($group, t('Node ID 6 is not a group.'));
$node = node_load(6);
$this->assertFalse($node->{OG_AUDIENCE_FIELD}, t('Node ID 6 is not associated with a group.'));
// Group content that shares the same group.
$group_alpha = og_get_group('node', 7);
$group_beta = og_get_group('node', 8);
$node = node_load(9);
foreach (array($group_alpha, $group_beta) as $group) {
$this->assertTrue(og_is_member($group->gid, 'node', $node), t('Node ID @nid is as group content associated with multiple groups.'));
}
// Check user membership.