You are here

public function YamlFormElementManagedFileTest::testPrivateFiles in YAML Form 8

Test private files.

File

src/Tests/YamlFormElementManagedFileTest.php, line 60

Class

YamlFormElementManagedFileTest
Test for form element managed file handling.

Namespace

Drupal\yamlform\Tests

Code

public function testPrivateFiles() {
  $elements = $this->yamlform
    ->getElementsDecoded();
  $elements['managed_file_single']['#uri_scheme'] = 'private';
  $this->yamlform
    ->setElements($elements);
  $this->yamlform
    ->save();
  $this
    ->drupalLogin($this->adminSubmissionUser);

  // Upload private file.
  $edit = [
    'files[managed_file_single]' => \Drupal::service('file_system')
      ->realpath($this->files[0]->uri),
  ];
  $sid = $this
    ->postSubmission($this->yamlform, $edit);

  /** @var \Drupal\yamlform\YamlFormSubmissionInterface $submission */
  $submission = YamlFormSubmission::load($sid);

  /** @var \Drupal\file\Entity\File $file */
  $fid = $this
    ->getLastFileId();
  $file = File::load($fid);

  // Check that test file 3 was uploaded to the current submission.
  $this
    ->assertEqual($submission
    ->getData('managed_file_single'), $fid, 'Test file 3 was upload to the current submission');

  // Check test file 3 file usage.
  $this
    ->assertIdentical([
    'yamlform' => [
      'yamlform_submission' => [
        $sid => '1',
      ],
    ],
  ], $this->fileUsage
    ->listUsage($file), 'The file has 3 usage.');

  // Check test file 3 uploaded file path.
  $this
    ->assertEqual($file
    ->getFileUri(), 'private://yamlform/test_element_managed_file/' . $sid . '/' . $this->files[0]->filename);

  // Check private file access allowed.
  $this
    ->drupalGet(file_create_url($file
    ->getFileUri()));
  $this
    ->assertResponse(200);
  $this
    ->drupalLogout();

  // Check private file access denied.
  $this
    ->drupalGet(file_create_url($file
    ->getFileUri()));
  $this
    ->assertResponse(403);
}