You are here

public function FeatureContext::openAndCheckFilesPrivateForUser in Open Social 8.4

Same name and namespace in other branches
  1. 8.9 tests/behat/features/bootstrap/FeatureContext.php \Drupal\social\Behat\FeatureContext::openAndCheckFilesPrivateForUser()
  2. 8.3 tests/behat/features/bootstrap/FeatureContext.php \Drupal\social\Behat\FeatureContext::openAndCheckFilesPrivateForUser()
  3. 8.5 tests/behat/features/bootstrap/FeatureContext.php \Drupal\social\Behat\FeatureContext::openAndCheckFilesPrivateForUser()
  4. 8.6 tests/behat/features/bootstrap/FeatureContext.php \Drupal\social\Behat\FeatureContext::openAndCheckFilesPrivateForUser()
  5. 8.7 tests/behat/features/bootstrap/FeatureContext.php \Drupal\social\Behat\FeatureContext::openAndCheckFilesPrivateForUser()
  6. 8.8 tests/behat/features/bootstrap/FeatureContext.php \Drupal\social\Behat\FeatureContext::openAndCheckFilesPrivateForUser()
  7. 10.3.x tests/behat/features/bootstrap/FeatureContext.php \Drupal\social\Behat\FeatureContext::openAndCheckFilesPrivateForUser()
  8. 10.0.x tests/behat/features/bootstrap/FeatureContext.php \Drupal\social\Behat\FeatureContext::openAndCheckFilesPrivateForUser()
  9. 10.1.x tests/behat/features/bootstrap/FeatureContext.php \Drupal\social\Behat\FeatureContext::openAndCheckFilesPrivateForUser()
  10. 10.2.x tests/behat/features/bootstrap/FeatureContext.php \Drupal\social\Behat\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 688

Class

FeatureContext
Defines application features from the specific context.

Namespace

Drupal\social\Behat

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));
  }
}