You are here

public function PrivateFileTest::testPrivateFile in Feeds 8.3

Tests private files work with the Feeds module.

See also

feeds_file_download()

File

tests/src/Functional/PrivateFileTest.php, line 48

Class

PrivateFileTest
Tests private files work with the Feeds module.

Namespace

Drupal\Tests\feeds\Functional

Code

public function testPrivateFile() {
  $node_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('node');
  $type_name = 'article';
  $field_name = strtolower($this
    ->randomMachineName());
  $this
    ->createFileField($field_name, 'node', $type_name, [
    'uri_scheme' => 'private',
  ]);
  $test_file = $this
    ->getTestFile('text');
  $nid = $this
    ->uploadNodeFile($test_file, $field_name, $type_name, TRUE, [
    'private' => TRUE,
  ]);
  $this->container
    ->get('entity_type.manager')
    ->getStorage('node')
    ->resetCache([
    $nid,
  ]);

  /* @var \Drupal\node\NodeInterface $node */
  $node = $node_storage
    ->load($nid);
  $node_file = File::load($node->{$field_name}->target_id);

  // Ensure the file can be viewed.
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertRaw($node_file
    ->getFilename(), 'File reference is displayed after attaching it');

  // Ensure the file can be downloaded.
  $this
    ->drupalGet(file_create_url($node_file
    ->getFileUri()));
  $this
    ->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the shipped file.');
}