You are here

public function FeatureContext::openAndCheckGroupContentAccess in Open Social 8

Same name and namespace in other branches
  1. 8.2 tests/behat/features/bootstrap/FeatureContext.php \FeatureContext::openAndCheckGroupContentAccess()

Opens the content from a group and check for access.

@Then /I open and check the access of content in group "(?P<groupname>[^"]+)" and I expect access "(?P<access>[^"]+)"$/

File

tests/behat/features/bootstrap/FeatureContext.php, line 710

Class

FeatureContext
Defines application features from the specific context.

Code

public function openAndCheckGroupContentAccess($groupname, $access) {
  $allowed_access = array(
    '0' => 'denied',
    '1' => 'allowed',
  );
  if (!in_array($access, $allowed_access)) {
    throw new \InvalidArgumentException(sprintf('This access option is not allowed: "%s"', $access));
  }
  $expected_access = 0;
  if ($access == 'allowed') {
    $expected_access = 1;
  }
  $query = \Drupal::entityQuery('group')
    ->condition('label', $groupname);
  $gid = $query
    ->execute();
  if (!empty($gid) && count($gid) === 1) {
    $gid = reset($gid);
    if ($gid) {
      $group = Group::load($gid);
      $group_content_types = \Drupal\group\Entity\GroupContentType::loadByEntityTypeId('node');
      $group_content_types = array_keys($group_content_types);

      // Get all the node's related to the current group
      $query = \Drupal::database()
        ->select('group_content_field_data', 'gcfd');
      $query
        ->addField('gcfd', 'entity_id');
      $query
        ->condition('gcfd.gid', $group
        ->id());
      $query
        ->condition('gcfd.type', $group_content_types, 'IN');
      $query
        ->execute()
        ->fetchAll();
      $nodes = $query
        ->execute()
        ->fetchAllAssoc('entity_id');
      foreach (array_keys($nodes) as $key => $entity_id) {
        $this
          ->openEntityAndExpectAccess('node', $entity_id, $expected_access);
      }

      // Get all the posts from this group
      $query = \Drupal::database()
        ->select('post__field_recipient_group', 'pfrg');
      $query
        ->addField('pfrg', 'entity_id');
      $query
        ->condition('pfrg.field_recipient_group_target_id', $group
        ->id());
      $query
        ->execute()
        ->fetchAll();
      $post_ids = $query
        ->execute()
        ->fetchAllAssoc('entity_id');
      foreach (array_keys($post_ids) as $key => $entity_id) {
        $this
          ->openEntityAndExpectAccess('post', $entity_id, $expected_access);
      }
    }
  }
  else {
    if (empty($gid)) {
      throw new \Exception(sprintf("Group '%s' does not exist.", $groupname));
    }
    if (count($gid) > 1) {
      throw new \Exception(sprintf("Multiple groups with label '%s' found.", $groupname));
    }
  }
}