Skip to content
Snippets Groups Projects
og.test 33 KiB
Newer Older
Amitaibu's avatar
Amitaibu committed
<?php
Amitaibu's avatar
Amitaibu committed

class OgAccess extends DrupalWebTestCase {
Amitaibu's avatar
Amitaibu committed

  public static function getInfo() {
    return array(
      'name' => 'OG entity access',
      'description' => 'Test the access provided by OG API.',
Gizra Gizra's avatar
Gizra Gizra committed
      'group' => 'Organic groups',
Amitaibu's avatar
Amitaibu committed
    );
  }

  function setUp() {
    parent::setUp('og', 'entity_feature');
  /**
   * Verify og_user_access_entity() returns correct value.
   */
  function testOgAccessEntity() {
    $perm = 'administer group';
    // Change permissions to authenticated member.
    $roles = array_flip(og_get_global_roles());
    og_role_change_permissions($roles[OG_AUTHENTICATED_ROLE], array($perm => 1));

    // Add OG group fields.
    og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');
    $og_field = og_fields_info(OG_AUDIENCE_FIELD);
    $og_field['field']['settings']['target_type'] = 'entity_test';
    og_create_field(OG_AUDIENCE_FIELD, 'node', 'article', $og_field);

    $user1 = $this->drupalCreateUser();
    $user2 = $this->drupalCreateUser();
    $user3 = $this->drupalCreateUser();

    $entity1 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
    $wrapper = entity_metadata_wrapper('entity_test', $entity1);
    $wrapper->{OG_GROUP_FIELD}->set(1);
    $wrapper->save();

    // User has access to group.
    $this->assertTrue(og_user_access_entity($perm, 'entity_test', $entity1, $user1), t('User1 has access to group.'));
    $this->assertFalse(og_user_access_entity($perm, 'entity_test', $entity1, $user2), t('User2 does not have access to group.'));

    // User has access to a group associated with a group content.
    $settings = array();
    $settings['type'] = 'article';
    $node = $this->drupalCreateNode($settings);

    $values = array('entity_type' => 'node', 'entity' => $node);
    og_group('entity_test', $entity1->pid, $values);
    $this->assertTrue(og_user_access_entity($perm, 'node', $node, $user1), t('User1 has access to group content.'));
    $this->assertFalse(og_user_access_entity($perm, 'node', $node, $user2), t('User2 does not have access to group content.'));

    // Make group content also a group.
    og_create_field(OG_GROUP_FIELD, 'node', 'article');
    $og_field = og_fields_info(OG_AUDIENCE_FIELD);
    og_create_field('og_group_ref_2', 'user', 'user', $og_field);

    $settings['uid'] = $user2->uid;
    $settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 1;
    $node = $this->drupalCreateNode($settings);

    $wrapper = entity_metadata_wrapper('node', $node);
    $wrapper->{OG_GROUP_FIELD}->set(1);
    $wrapper->save();

    $values = array('entity_type' => 'node', 'entity' => $node);
    og_group('entity_test', $entity1->pid, $values);

    $this->assertTrue(og_user_access_entity($perm, 'node', $node, $user1), t('User1 has access based on access to group.'));
    $this->assertTrue(og_user_access_entity($perm, 'node', $node, $user2), t('User2 has access based on access to group content.'));
    $this->assertFalse(og_user_access_entity($perm, 'node', $node, $user3), t('User3 has no access to entity.'));

    // Entity is a disabled group.
    $settings['uid'] = $user2->uid;
    $settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 0;
    $node = $this->drupalCreateNode($settings);
    $this->assertNull(og_user_access_entity($perm, 'node', $node, $user1), t('Entity is a disabled group, so return value is NULL.'));

    // Entity is an orphan group content.
    $settings = array();
    $settings['type'] = 'article';
    $settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 0;
    $node = $this->drupalCreateNode($settings);
    $values = array('entity_type' => 'node', 'entity' => $node);
    og_group('entity_test', $entity1->pid, $values);
    $entity1->delete();
    $this->assertNull(og_user_access_entity($perm, 'node', $node, $user1), t('Entity is an orphan group content, so return value is NULL.'));

    // Entity isn't a group or a group content.
    $settings = array();
    $settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 0;
    $settings['type'] = 'article';
    $node = $this->drupalCreateNode($settings);
    $this->assertNull(og_user_access_entity($perm, 'node', $node, $user1), t('Entity is not a group or a group contentm, so return value is NULL.'));

    // Entity is NULL - as might be passed by field_access().
    $this->assertNull(og_user_access_entity($perm, 'node', NULL, $user1), t('Entity passed is NULL, so return value is NULL.'));

    // Entity is not saved to database yet.
    unset($node->nid);
    $this->assertNull(og_user_access_entity($perm, 'node', NULL, $user1), t('Entity is not saved to database, so return value is NULL.'));
  }
/**
 * Test Group node access. This will test nodes that are groups and group content.
 */
class OgNodeAccess extends DrupalWebTestCase {

  public static function getInfo() {
    return array(
      'name' => 'OG node access',
      'description' => 'Test strict node access permissions for group nodes and group content.',
      'group' => 'Organic groups',
    );
  }

  function setUp() {
    parent::setUp('og');

    // Add OG group field to a the node's "page" bundle.
    og_create_field(OG_GROUP_FIELD, 'node', 'page');

    // Add OG audience field to the node's "article" bundle.
    og_create_field(OG_AUDIENCE_FIELD, 'node', 'article');

    // Create an editor user and a group manager for these tests.
    $this->editor_user = $this->drupalCreateUser(array('access content', 'edit any page content', 'edit any article content'));
    $this->group_manager = $this->drupalCreateUser(array('access content', 'create page content', 'edit own article content', 'edit own page content'));

    // Create group node.
    $settings = array(
      'type' => 'page',
      OG_GROUP_FIELD . '[und][0][value]' => 1,
      'uid' => $this->group_manager->uid
    );
    $this->group = $this->drupalCreateNode($settings);

    // Create node to add to group.
    $settings = array(
      'type' => 'article',
      'uid' => $this->group_manager->uid,
    );
    $this->group_content = $this->drupalCreateNode($settings);

    // Add node to group.
    $values = array(
      'entity_type' => 'node',
    og_group('node', $this->group->nid, $values);
  }

  /**
   * Test strict access permissions for updating group node. A non-member of
   * a group who has core node access update permission is denied access.
   */
  function testStrictAccess() {
    // Set Node access strict variable.
    variable_set('og_node_access_strict', TRUE);

    // Login as editor and try to change the group node and group content.
    $this->drupalLogin($this->editor_user);

    $this->drupalGet('node/' . $this->group->nid . '/edit');
    $this->assertResponse('403', t('A non-member with core node access permissions was denied access to edit group node.'));

    $this->drupalGet('node/' . $this->group_content->nid . '/edit');
    $this->assertResponse('403', t('A non-member with core node access permissions was denied access to edit group content node.'));

    // Login as a group manager and try to change group node.
    $this->drupalLogin($this->group_manager);

    $this->drupalGet('node/' . $this->group->nid . '/edit');
    $this->assertResponse('200', t('Group manager allowed to access to edit group node.'));

    $this->drupalGet('node/' . $this->group_content->nid . '/edit');
    $this->assertResponse('200', t('Group manager allowed to access to edit group content node.'));
  }

  /**
   * Test non-strict access permissions for updating group node. A non-member
   * of a group who has core node access update permission is allowed access.
   */
  function testNoStrictAccess() {
    // Set Node access strict variable.
    variable_set('og_node_access_strict', FALSE);

    // Login as editor and try to change the group node and group content.
    $this->drupalLogin($this->editor_user);

    $this->drupalGet('node/' . $this->group->nid . '/edit');
    $this->assertResponse('200', t('A non-member with core node access permissions was not denied access.'));

    $this->drupalGet('node/' . $this->group_content->nid . '/edit');
    $this->assertResponse('200', t('A non-member with core node access permissions was not denied access to edit group content node.'));

    // Login as a group manager and try to change group node.
    $this->drupalLogin($this->group_manager);

    $this->drupalGet('node/' . $this->group->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.'));
  }
}

Amitaibu's avatar
Amitaibu committed
/**
 * Test the Organic groups API and CRUD handling.
Amitaibu's avatar
Amitaibu committed
 */
class OgMetaData extends DrupalWebTestCase {
Amitaibu's avatar
Amitaibu committed

  public static function getInfo() {
Amitaibu's avatar
Amitaibu committed
    return array(
      'name' => 'OG metadata',
      'description' => 'Test the metadata properties.',
      'group' => 'Organic groups',
Amitaibu's avatar
Amitaibu committed
    );
  }

  function setUp() {
    parent::setUp('og', 'entity_feature');
  }
  /**
   * Test the og_get_entity_groups() API function.
   */
  function testOgMembershipMetaData() {
    // Add OG group field to the entity_test's "main" bundle.
    og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');

    // Add OG audience field to the node's "article" bundle.
    $og_field = og_fields_info(OG_AUDIENCE_FIELD);
    $og_field['field']['settings']['target_type'] = 'entity_test';
    og_create_field(OG_AUDIENCE_FIELD, 'node', 'article', $og_field);
    // Add a second audience field.
    og_create_field('og_ref_2', 'node', 'article', $og_field);
    $user1 = $this->drupalCreateUser();
    $entity1 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
    $wrapper = entity_metadata_wrapper('entity_test', $entity1);
    $wrapper->{OG_GROUP_FIELD}->set(1);
    $wrapper->save();

    $entity2 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
    $wrapper = entity_metadata_wrapper('entity_test', $entity2);
    $wrapper->{OG_GROUP_FIELD}->set(1);
    $wrapper->save();

    $entity3 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
    $wrapper = entity_metadata_wrapper('entity_test', $entity3);
    $wrapper->{OG_GROUP_FIELD}->set(1);
    $wrapper->save();

    $settings = array();
    $settings['type'] = 'article';

    // Create group enteties.
    foreach (og_group_content_states() as $state => $value) {
      $node = $this->drupalCreateNode($settings);
      // Assign article to the group.
      $values = array('entity_type' => 'node', 'entity' => $node);
      og_group('entity_test', $entity1->pid, $values + array('state' => $state));
      // Subscribe node to a second group, but with a different state, by
      // selecting the state code and incrementing by one (e.g. is the
      // state is "active" then the other-state will be "pending").
      $other_state = $state == OG_STATE_BLOCKED ? OG_STATE_ACTIVE : $state + 1;
      $values += array('state' => $other_state);
      og_group('entity_test', $entity2->pid, $values);

      // Subscribe node to third group, using a diffferent field.
      $values += array('field_name' => 'og_ref_2');
      og_group('entity_test', $entity3->pid, $values);

      $wrapper = entity_metadata_wrapper('node', $node->nid);
      $this->assertEqual($wrapper->og_membership->count(), 3, t('Found all OG memberships.'));

      $og_memberships = $wrapper->{'og_membership__' . $state}->value();
      $this->assertEqual(count($og_memberships), 1, t('Found 1 OG membership with state @state.', array('@state' => $value)));
      $this->assertEqual($og_memberships[0]->state, $state, t('OG membership has correct @state state.', array('@state' => $value)));

      $og_memberships = $wrapper->{OG_AUDIENCE_FIELD . '__og_membership__' . $state}->value();
      $this->assertEqual(count($og_memberships), 1, t('Found 1 OG membership with state @state in group-audience field.', array('@state' => $value)));
      $this->assertEqual($og_memberships[0]->field_name, OG_AUDIENCE_FIELD, t('OG membership with state @state is referencing correct field name in group-audience field.', array('@state' => $value)));
    }
    $og_memberships = $wrapper->{OG_AUDIENCE_FIELD . '__og_membership'}->value();
    $this->assertEqual(count($og_memberships), 2, t('Found 2 OG membership in group-audience field.', array('@state' => $value)));
    $this->assertEqual($og_memberships[0]->field_name, OG_AUDIENCE_FIELD, t('OG membership has correct group-audience field.'));
    $og_memberships = $wrapper->{'og_ref_2__og_membership'}->value();
    $this->assertEqual(count($og_memberships), 1, t('Found 2 OG membership in second group-audience field.', array('@state' => $value)));
    $this->assertEqual($og_memberships[0]->field_name, 'og_ref_2', t('OG membership has correct group-audience field.'));
 * Test Group content handeling.
class OgGroupAndUngroup extends DrupalWebTestCase {

  public static function getInfo() {
    return array(
      'name' => 'OG group and ungroup',
      'description' => 'Test the group and ungrouping of content with a group.',
      'group' => 'Organic groups',
    );
  }

  function setUp() {
    parent::setUp('og', 'entity_feature');

    // Add OG group field to the entity_test's "main" bundle.
    og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');

    // Add OG audience field to the node's "article" bundle.
    $og_field = og_fields_info(OG_AUDIENCE_FIELD);
    $og_field['field']['settings']['target_type'] = 'entity_test';
    og_create_field(OG_AUDIENCE_FIELD, 'node', 'article', $og_field);
   * Test group and ungroup of content.
  function testGroupAndUngroup() {
    $user1 = $this->drupalCreateUser();
    $user2 = $this->drupalCreateUser();
    $entity1 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
    $wrapper = entity_metadata_wrapper('entity_test', $entity1);
    $wrapper->{OG_GROUP_FIELD}->set(1);
    $wrapper->save();
    $entity2 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
    $wrapper = entity_metadata_wrapper('entity_test', $entity2);
    $wrapper->{OG_GROUP_FIELD}->set(1);
    $wrapper->save();
    $entity3 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
    $wrapper = entity_metadata_wrapper('entity_test', $entity3);
    $wrapper->{OG_GROUP_FIELD}->set(1);
    $wrapper->save();
    $settings = array();
    $settings['type'] = 'article';
    $settings['uid'] = $user2->uid;
    $node = $this->drupalCreateNode($settings);

    $this->assertFalse(og_is_member('entity_test', $entity1->pid, 'node', $node), t('Node is not assigned to group1.'));
    $values = array('entity_type' => 'node', 'entity' => $node);
    og_group('entity_test', $entity1->pid, $values);
    $og_membership = og_get_membership('entity_test', $entity1->pid, 'node', $node->nid);
    $id = $og_membership->id;
    $this->assertTrue(og_is_member('entity_test', $entity1->pid, 'node', $node), t('Node is assigned to group1 with active state.'));

    // State changed.
    $values += array('state' => OG_STATE_BLOCKED);
    og_group('entity_test', $entity1->pid, $values);
    $og_membership = og_get_membership('entity_test', $entity1->pid, 'node', $node->nid);
    $this->assertEqual($id, $og_membership->id, t('OG membership was updated.'));
    $this->assertTrue(og_is_member('entity_test', $entity1->pid, 'node', $node, array(OG_STATE_BLOCKED)), t('Node is assigned to group1 with blocked state.'));

    // Exception on existing OG membership.
    try {
      og_membership_create('entity_test', $entity1->pid, 'node', $node->nid, OG_AUDIENCE_FIELD)->save();
      $this->fail('Saving multiple OG membership for same entity and group works.');
    }
    catch (OgException $e) {
      $this->pass('Saving multiple OG membership for same entity and group does not work.');
    }

    // Add a second audience field.
    $og_field = og_fields_info(OG_AUDIENCE_FIELD);
    $og_field['field']['settings']['target_type'] = 'entity_test';
    $og_field['field']['cardinality'] = 2;
    og_create_field('og_ref_2', 'node', 'article', $og_field);

    // Re-group to another field.
    $values += array('field_name' => 'og_ref_2');
    og_group('entity_test', $entity1->pid, $values);
    $og_membership = og_get_membership('entity_test', $entity1->pid, 'node', $node->nid);
    $this->assertNotEqual($id, $og_membership->id, t('OG membership was re-created.'));
    $this->assertEqual('og_ref_2', $og_membership->field_name, t('OG membership is registered under correct field.'));


    // Exception on field cardinality.
    og_group('entity_test', $entity2->pid, $values);
    try {
      og_group('entity_test', $entity3->pid, $values);
      $this->fail('Grouping beyond field cardinality works.');
    }
    catch (OgException $e) {
      $this->pass('Grouping beyond field cardinality does not work.');
    }

    // Exception as field-name is incorrect.
    $values['field_name'] = 'wrong-field-name';
    try {
      og_group('entity_test', $entity1->pid, $values);
      $this->fail('Grouping with incorrect field name works.');
    }
    catch (OgException $e) {
      $this->pass('Grouping with incorrect field name does not work.');
    }

    // Exception grouping using a secondary field.
    $og_field = og_fields_info(OG_AUDIENCE_FIELD);
    $og_field['field']['settings']['target_type'] = 'entity_test';
    $og_field['field']['settings']['handler_settings']['primary_field'] = OG_AUDIENCE_FIELD;
    og_create_field('og_ref_secondary', 'node', 'article', $og_field);
    $values['field_name'] = 'og_ref_secondary';
    try {
      og_group('entity_test', $entity1->pid, $values);
      $this->fail('Grouping with secondary field name works.');
    }
    catch (OgException $e) {
      $this->pass('Grouping with secondary field name does not work.');
    }

    // Exception on audience field, referencing wrong target type.
    $og_field = og_fields_info(OG_AUDIENCE_FIELD);
    $og_field['field']['settings']['target_type'] = 'node';
    og_create_field('og_ref_3', 'node', 'article', $og_field);
    $values['field_name'] = 'og_ref_3';
    try {
      og_group('entity_test', $entity1->pid, $values);
      $this->fail('Grouping with wrong target type works.');
    }
    catch (OgException $e) {
      $this->pass('Grouping with wrong target type does not work.');
    }

    // Exception on audience field, referencing correct target type, but wrong
    // bundles.
    $og_field = og_fields_info(OG_AUDIENCE_FIELD);
    $og_field['field']['settings']['target_type'] = 'entity_test';
    $og_field['field']['settings']['handler_settings']['target_bundles'] = array('test');
    og_create_field('og_ref_4', 'node', 'article', $og_field);
    $values['field_name'] = 'og_ref_4';
    try {
      og_group('entity_test', $entity1->pid, $values);
      $this->fail('Grouping with wrong target bundles works.');
    }
    catch (OgException $e) {
      $this->pass('Grouping with wrong target bundles does not work.');
    }


    // Exception as user has no group-audience field.
Amitaibu's avatar
Amitaibu committed
    $instance = field_info_instance('user', 'og_user_group_ref', 'user');
    field_delete_instance($instance);

    try {
      $entity2 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
      $wrapper = entity_metadata_wrapper('entity_test', $entity2);
      $wrapper->{OG_GROUP_FIELD}->set(1);
      $wrapper->save();
      $this->fail('Grouping with no group-audience field in bundle works.');
    }
    catch (OgException $e) {
      $this->pass('Grouping with no group-audience field in bundle does not work.');
    }

    // Ungroup node from group.
    og_ungroup('entity_test', $entity1->pid, 'node', $node);
    $og_membership = og_get_membership('entity_test', $entity1->pid, 'node', $node->nid);
    $this->assertFalse($og_membership, t('Node was ungrouped from group.'));
  }

  /**
   * Test piping group association via the group-audience field.
   */
  function testGroupAudienceField() {
    $user1 = $this->drupalCreateUser();
    $user2 = $this->drupalCreateUser();

    $entity1 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
    $wrapper = entity_metadata_wrapper('entity_test', $entity1);
    $wrapper->{OG_GROUP_FIELD}->set(1);
    $wrapper->save();

    $settings = array();
    $settings['type'] = 'article';
    $settings['uid'] = $user2->uid;
    $node = $this->drupalCreateNode($settings);

    $wrapper = entity_metadata_wrapper('node', $node);

    $this->assertFalse(og_is_member('entity_test', $entity1->pid, 'node', $node), t('Node is not assigned to group1.'));
    $wrapper->{OG_AUDIENCE_FIELD}[] = $entity1->pid;
    $wrapper->save();
    $og_membership = og_get_membership('entity_test', $entity1->pid, 'node', $node->nid);
    $id = $og_membership->id;
    $this->assertTrue(og_is_member('entity_test', $entity1->pid, 'node', $node), t('Node is assigned to group1 with active state.'));

    $wrapper->{OG_AUDIENCE_FIELD}->set(NULL);
    $wrapper->save();
    $this->assertFalse(og_get_entity_groups('node', $node), t('Node is not associated with any group.'));
  /**
   * Test granting deault role to group manager.
   */
  function testGroupManagerDefaultRoles() {
    // Get only the admin role.
    $og_roles = og_roles('entity_test', 0, 'main', FALSE, FALSE);
    variable_set('og_group_manager_default_rids_entity_test_main', array_keys($og_roles));
    $user1 = $this->drupalCreateUser();

    $entity1 = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
    $wrapper = entity_metadata_wrapper('entity_test', $entity1);
    $wrapper->{OG_GROUP_FIELD}->set(1);
    $wrapper->save();

    $user_roles = og_get_user_roles('entity_test', $entity1->pid, $user1->uid, FALSE);
    $this->assertEqual($og_roles, $user_roles, t('Group manager was granted default role.'));
  }
class OgPermissionsTestCase extends DrupalWebTestCase {
Amitaibu's avatar
Amitaibu committed
  public static function getInfo() {
    return array(
      'name' => 'OG permissions',
      'description' => 'Verify that permissions can be added and removed via API.',
      'group' => 'Organic groups'
Amitaibu's avatar
Amitaibu committed
    );
  }

  function setUp() {
    parent::setUp('og', 'entity_feature');
    // Add OG group field to the entity_test's "main" bundle.
    og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');

    // Add OG audience field to the node's "article" bundle.
    $og_field = og_fields_info(OG_AUDIENCE_FIELD);
    $og_field['field']['settings']['target_type'] = 'entity_test';
    og_create_field(OG_AUDIENCE_FIELD, 'node', 'article', $og_field);

  /**
   * Verify proper permission changes by og_role_change_permissions().
   */
  function testOgUserRoleChangePermissions() {
    // Create user.
    $user1 = $this->drupalCreateUser();

    // Create an entity.
    $property = OG_GROUP_FIELD;
    $entity = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
    $wrapper = entity_metadata_wrapper('entity_test', $entity);
    $wrapper->{OG_GROUP_FIELD}->set(1);
    $wrapper->save();
Amitaibu's avatar
Amitaibu committed

    // Associate user to the group.
    $user2 = $this->drupalCreateUser();
    $values = array('entity_type' => 'user', 'entity' => $user2);
    og_group('entity_test', $entity->pid, $values);

    // Assert the user is registered to the new group.
    $this->assertTrue(og_is_member('entity_test', $entity->pid, 'user', $user2), t('User is registered to the new group.'));
Amitaibu's avatar
Amitaibu committed
    // Verify current permissions.
    $this->assertFalse(og_user_access('entity_test', $entity->pid, 'update own article content', $user2), t('User does not have "update own article content" permission.'));
    $this->assertFalse(og_user_access('entity_test', $entity->pid, 'delete own article content', $user2), t('User does not have "delete own article content" permission.'));
Amitaibu's avatar
Amitaibu committed

    // Change permissions to authenticated member.
    $og_roles = array_flip(og_roles('entity_test', $entity->pid, 'main'));
    // Authenticated role ID.
    $rid = $og_roles[OG_AUTHENTICATED_ROLE];
Amitaibu's avatar
Amitaibu committed

    $permissions = array(
      'delete own article content' => 1,
Amitaibu's avatar
Amitaibu committed
    );
    og_role_change_permissions($rid, $permissions);
Amitaibu's avatar
Amitaibu committed

    // Verify proper permission changes.
    $this->assertFalse(og_user_access('entity_test', $entity->pid, 'update own article content', $user2), t('User still does not have "update own article content" permission.'));
    $this->assertTrue(og_user_access('entity_test', $entity->pid, 'delete own article content', $user2), t('User now has "delete own article content" permission.'));
      'delete own article content' => 0,
      'administer group' => 1,
    og_role_change_permissions($rid, $permissions);
    $this->assertTrue(og_user_access('entity_test', $entity->pid, 'delete own article content', $user2), t('User still has "delete own article content" as they have "administer group" permission.'));
    $this->assertTrue(og_user_access('entity_test', $entity->pid, 'administer group', $user2), t('User has "administer group" permission.'));
Amitaibu's avatar
Amitaibu committed
}
class OgDefaultAccessFieldTestCase extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'OG default access field',
      'description' => 'Test groups with default access field.',
      'group' => 'Organic groups'
    );
  }

  function setUp() {
    parent::setUp('og', 'entity_feature');
  }


  /**
   * Test groups with default access field enabled or disabled.
   */
  function testOgDefaultAccessField() {
    // Create user.
    $user1 = $this->drupalCreateUser();

    // Add OG group field to the entity_test's "main" bundle.
    og_create_field(OG_GROUP_FIELD, 'entity_test', 'main');

    $og_roles = og_roles('entity_test', 0, 'main');

    // Group without default access field.
    $entity = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
    $wrapper = entity_metadata_wrapper('entity_test', $entity);
    $wrapper->{OG_GROUP_FIELD}->set(1);
    $wrapper->save();
    $this->assertEqual($og_roles, og_roles('entity_test', $entity->pid, 'main'), t('Group without default access field is assigned to the global roles and permissions settings.'));

    // Add default access field to the entity_test's "main" bundle.
    og_create_field(OG_DEFAULT_ACCESS_FIELD, 'entity_test', 'main');

    // Group with default access field disabled.
    $entity = entity_create('entity_test', array('name' => 'main', 'uid' => $user1->uid));
    $wrapper = entity_metadata_wrapper('entity_test', $entity);
    $wrapper->{OG_GROUP_FIELD}->set(1);
    $wrapper->{OG_DEFAULT_ACCESS_FIELD}->set(0);
    $wrapper->save();
    $this->assertEqual($og_roles, og_roles('entity_test', $entity->pid, 'main'), t('Group with default access field disabled is assigned to the global roles and permissions settings.'));
    // Add admin role to a user.
    $rid = array_search(OG_ADMINISTRATOR_ROLE, $og_roles);
    og_role_grant('entity_test', $entity->pid, $user1->uid, $rid);
    $user_roles = og_get_user_roles('entity_test', $entity->pid, $user1->uid);
    $this->assertTrue(array_search(OG_ADMINISTRATOR_ROLE, $user_roles), t('User has default "admin" role.'));

    // Group with default access field enabled.
    $wrapper->{OG_DEFAULT_ACCESS_FIELD}->set(1);
    $wrapper->save();
    $new_og_roles = og_roles('entity_test', $entity->pid, 'main');
    $this->assertNotEqual($og_roles, $new_og_roles, t('Group with default access field enabled has own roles and permissions settings.'));

    // Assert the newley created admin role was mapped to the default one.
    $user_roles = og_get_user_roles('entity_test', $entity->pid, $user1->uid);
    $this->assertTrue(array_search(OG_ADMINISTRATOR_ROLE, $user_roles), t('User has overriden "admin" role.'));

    // Disable existing group's default access field.
    $wrapper->{OG_DEFAULT_ACCESS_FIELD}->set(0);
    $wrapper->save();
    $this->assertEqual($og_roles, og_roles('entity_test', $entity->pid, 'main'), t('Group with enabled default access field that was disabled is assigned to the global roles and permissions settings.'));
 * Upgrade 7000 test.
 * Load a filled installation of Drupal 6 and run the upgrade on it.
 *
 * TODO: We have to use $this->drupalGet('node/' . $nid); to proerly load
 * the node data, otherwise. We should understand why this is needed, and
 * remove it.
class OgMigrate7000TestCase extends UpgradePathTestCase {
  public static function getInfo() {
    return array(
      'name'  => 'OG migrate - 7000',
      'description'  => 'Tests the upgrade path of OG from Drupal 6.',
      'group' => 'Organic groups',
      'dependencies' => array('ctools'),
  public function setUp() {
    // Path to the database dump.
    $this->databaseDumpFiles = array(
      drupal_get_path('module', 'og_migrate') . '/tests/drupal-6.og.database.php',
    );
    parent::setUp();
    $this->performUpgrade();
    // Drupal doesn't rebuild the registery properly, so we have to
    // manually include all the classes.
    module_load_include('inc', 'entity' ,'entity.info');
    module_load_include('inc', 'entity' ,'includes/entity');
    module_load_include('inc', 'entity' ,'includes/entity.controller');
    module_load_include('inc', 'entity' ,'includes/entity.wrapper');
    module_load_include('inc', 'og' ,'includes/og.info');
    module_enable(array('og_migrate'));
    // Set small batch size, so we can simulate upgrade with many records.
    variable_set('og_update_batch_size', 3);
   * Test a successful group upgrade.
  public function testOg7000Group() {
    // Run the required migration.
    $edit = array(
      'migrate[og_7000_group]' => TRUE,
      'migrate[og_7000_group_content]' => TRUE,
    $this->drupalPost('admin/config/group/group-migrate', $edit, t('Migrate'));
    // Assert according to the scenario Drupal 6's test table dump was created.
    foreach (array(1, 2) as $nid) {
      $this->drupalGet('node/' . $nid);
      $node = node_load($nid);
      $this->assertTrue($node->{OG_GROUP_FIELD}[LANGUAGE_NONE][0]['value'], t('Node ID @nid is an active group.', array('@nid' => $nid)));
    // Test group content with NID 3 - 5 belong to the group with NID 2.
    foreach (range(3, 5) as $nid) {
      $this->drupalGet('node/' . $nid);
      $node = node_load($nid);
      $this->assertTrue(og_is_member('node', 2, 'node', $node), t('Node ID @nid is a group content of Node ID 2', array('@nid' => $nid)));
    // Orphan group content (i.e. not attached to a group).
    $node = node_load(6);
    $this->assertFalse(og_get_entity_groups('node', $node), t('Node ID 6 is not associated with any group.'));
    // Group content that shares the same group.
    $node = node_load(9);
    foreach (array(7, 8) as $nid) {
      $this->assertTrue(og_is_member('node', $nid, 'node', $node), t('Node ID @nid is as group content associated with multiple groups.', array('@nid' => $node->nid)));
   * Test user upgrade.
   *
   * @see og_7000_user()
  public function testOg7000User() {
    // Run the required migration.
    $edit = array(
      'migrate[og_7000_group]' => TRUE,
      'migrate[og_7000_user]' => TRUE,
    $this->drupalPost('admin/config/group/group-migrate', $edit, t('Migrate'));
    // Assert users.
    $values = array(
      // Uid 3 is the group manager, so in OG6 it was marked as admin.
      3 => array('admin' => TRUE),
      4 => array('active' => FALSE),
      5 => array(),
      6 => array('active' => FALSE, 'admin' => TRUE),
      7 => array('admin' => TRUE),
    $og_roles = og_roles('node', 0, 'test_group');

    foreach ($values as $uid => $value) {
      $account = user_load($uid);

      // Set default values.
      $value += array('active' => TRUE, 'admin' => FALSE);
      $roles = array();

      if ($value['active']) {
        $op = t('active');
        $states = array(OG_STATE_ACTIVE);
        $rid = array_search(OG_AUTHENTICATED_ROLE, $og_roles);
      }
      else {
        $op = t('pending');
        $states = array(OG_STATE_PENDING);
        // If the member is pending then they have the anonymous role.
        $rid = array_search(OG_ANONYMOUS_ROLE, $og_roles);
      }

      $roles[$rid] = TRUE;

      if ($value['admin']) {
        // OG_ADMINISTRATOR_ROLE
        $rid = array_search(OG_ADMINISTRATOR_ROLE, $og_roles);
        $roles[$rid] = TRUE;
      }

      $this->assertTrue(og_is_member('node', 10, 'user', $account, $states), t('User @uid is @op member in group.', array('@uid' => $uid, '@op' => $op)));
      // Pass also pending state, so we make sure that even if the user
      // isn't active they are considered members, to check they get the
      // correct role.
      $this->assertEqual(array_keys(og_get_user_roles('node', 10, $uid)), array_keys($roles), t('User @uid has the correct roles in group.', array('@uid' => $uid)));
    }
   * Test group description upgrade.
   *
   * @see og_7000_group_description()
  public function testOg7000GroupDescription() {
    // Run the required migration.
    $edit = array(
      'migrate[og_7000_group]' => TRUE,
      'migrate[og_7000_group_description]' => TRUE,
    $this->drupalPost('admin/config/group/group-migrate', $edit, t('Migrate'));
    // Assert description was converted to a field.
    foreach (array(1, 2, 7, 8, 10) as $nid) {
      $this->drupalGet('node/' . $nid);
      $node = node_load($nid);
      $this->assertTrue($node->og_description[LANGUAGE_NONE][0]['value'], t('Description fields has correct data.'));