You are here

public function FeatureContext::openAndCheckFilesPrivateForUser in Open Social 8

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

Opens the files uploaded by a given user.

@Then /I open and check the access of the files uploaded by "(?P<username>[^"]+)" and I expect access "(?P<access>[^"]+)"$/

File

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

Class

FeatureContext
Defines application features from the specific context.

Code

public function openAndCheckFilesPrivateForUser($username, $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('user')
    ->condition('name', $username);
  $uid = $query
    ->execute();
  if (!empty($uid) && count($uid) === 1) {
    $uid = reset($uid);
    if ($uid) {
      $private_query = \Drupal::database()
        ->select('file_managed', 'fm');
      $private_query
        ->addField('fm', 'fid');
      $private_query
        ->condition('fm.uid', $uid, '=');
      $private_query
        ->condition('fm.uri', 'private://%', 'LIKE');
      $private_files = $private_query
        ->execute()
        ->fetchAllAssoc('fid');
      foreach ($private_files as $fid => $file) {
        $this
          ->openFileAndExpectAccess($fid, $expected_access);
      }
    }
  }
  else {
    throw new \Exception(sprintf("User '%s' does not exist.", $username));
  }
}