You are here

function OgAccessTestCase::testOgStrictPrivate in Organic groups 7.2

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

Test a group content that belongs to a private and non-private group results in a private group content.

File

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

Class

OgAccessTestCase
Test OG access.

Code

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

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

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

  // Create a group content node and set default access.
  $settings = array();
  $settings['type'] = $this->group_content_type;
  $settings[OG_AUDIENCE_FIELD][LANGUAGE_NONE][0]['target_id'] = $group_node1->nid;
  $settings[OG_AUDIENCE_FIELD][LANGUAGE_NONE][1]['target_id'] = $group_node2->nid;
  $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('node', $group_node1->nid, 'user', $this->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($this->user2);
  $this
    ->assertFalse(og_is_member('node', $group_node1->nid, 'user', $this->user2), t('User is not a group member.'));
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertResponse('403', t('Non group member can not view group content node.'));

  // 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, 'og_access:node', t('Grant realm is correct for public group content.'));
  $this
    ->assertEqual($records[0]->gid, $group_node1->nid, t('First gid is the first group ID.'));
  $this
    ->assertEqual($records[1]->gid, $group_node2->nid, t('Second gid is the second group ID.'));
}