You are here

function OgAccessTestCase::testOgStrictPrivate in Organic groups 7

Same name and namespace in other branches
  1. 7.2 og_access/og_access.test \OgAccessTestCase::testOgStrictPrivate()

Test "Strict private" variable enabled or disabled.

File

og_access/og_access.test, line 180
Test organic groups access module.

Class

OgAccessTestCase
Test OG access.

Code

function testOgStrictPrivate() {
  $user1 = $this
    ->drupalCreateUser();
  $user2 = $this
    ->drupalCreateUser();
  $this
    ->drupalLogin($user1);

  // Create group and group content node types.
  $group_type = $this
    ->drupalCreateContentType();
  og_create_field(OG_GROUP_FIELD, 'node', $group_type->type);
  og_create_field(OG_ACCESS_FIELD, 'node', $group_type->type);
  $group_content_type = $this
    ->drupalCreateContentType();
  og_create_field(OG_AUDIENCE_FIELD, 'node', $group_content_type->type);
  og_create_field(OG_CONTENT_ACCESS_FIELD, 'node', $group_content_type->type);

  // Create a group node and set as private.
  $settings = array();
  $settings['type'] = $group_type->type;
  $settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 1;
  $settings[OG_ACCESS_FIELD][LANGUAGE_NONE][0]['value'] = 1;
  $group_node1 = $this
    ->drupalCreateNode($settings);
  $group1 = og_get_group('node', $group_node1->nid);

  // Create a group node and set as public.
  $settings[OG_ACCESS_FIELD][LANGUAGE_NONE][0]['value'] = 0;
  $group_node2 = $this
    ->drupalCreateNode($settings);
  $group2 = og_get_group('node', $group_node2->nid);

  // Create a group content node and set default access.
  $settings = array();
  $settings['type'] = $group_content_type->type;
  $settings[OG_AUDIENCE_FIELD][LANGUAGE_NONE][0]['gid'] = $group1->gid;
  $settings[OG_AUDIENCE_FIELD][LANGUAGE_NONE][1]['gid'] = $group2->gid;
  $settings[OG_CONTENT_ACCESS_FIELD][LANGUAGE_NONE][0]['value'] = OG_CONTENT_ACCESS_DEFAULT;
  $node = $this
    ->drupalCreateNode($settings);

  // Assert the user can view the group.
  $this
    ->assertTrue(og_is_member($group1->gid, 'user', $user1), t('User is a group member.'));
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertResponse('200', t('Group member can view public group node.'));

  // Assert another user is not a group member.
  $this
    ->drupalLogin($user2);
  $this
    ->assertFalse(og_is_member($group1->gid, 'user', $user2), t('User is not a group member.'));

  // Strict private enabled.
  variable_set('group_access_strict_private', 1);
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertResponse('403', t('Non group member can not view group node when "strict private" is enabled.'));

  // Assert all groups were registered in {node_access}.
  $records = db_query('SELECT realm, gid FROM {node_access} WHERE nid = :nid', array(
    ':nid' => $node->nid,
  ))
    ->fetchAll();
  $this
    ->assertEqual(count($records), 2, t('Returned the correct number of rows.'));
  $this
    ->assertEqual($records[0]->realm, 'group_access_authenticated', t('Grant realm is correct for public group content.'));
  $this
    ->assertEqual($records[0]->gid, $group1->gid, t('First gid is the first group ID.'));
  $this
    ->assertEqual($records[1]->gid, $group2->gid, t('Second gid is the second group ID.'));

  // Strict private enabled.
  variable_set('group_access_strict_private', 0);
  node_access_rebuild();
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertResponse('200', t('Non group member can view public group node.'));

  // Assert "all" realm was registered in {node_access}.
  $records = db_query('SELECT realm, gid FROM {node_access} WHERE nid = :nid', array(
    ':nid' => $node->nid,
  ))
    ->fetchAll();
  $this
    ->assertEqual(count($records), 1, t('Returned the correct number of rows.'));
  $this
    ->assertEqual($records[0]->realm, 'all', t('Grant realm is correct for public group content.'));
}