You are here

public function FileFieldPathsGeneralTest::testReadOnly in File (Field) Paths 8

Test File (Field) Paths works with read-only stream wrappers.

File

tests/src/Functional/FileFieldPathsGeneralTest.php, line 292

Class

FileFieldPathsGeneralTest
Test general functionality.

Namespace

Drupal\Tests\filefield_paths\Functional

Code

public function testReadOnly() {

  // Create a File field.
  $field_name = mb_strtolower($this
    ->randomMachineName());
  $field_settings = [
    'uri_scheme' => 'ffp-dummy-readonly',
  ];
  $instance_settings = [
    'file_directory' => "fields/{$field_name}",
  ];
  $this
    ->createFileField($field_name, 'node', $this->contentType, $field_settings, $instance_settings);

  // Get a test file.

  /** @var \Drupal\file\Entity\File $file */
  $file = $this
    ->getTestFile('image');

  // Prepare the file for the test 'ffp-dummy-readonly://' read-only stream
  // wrapper.
  $file
    ->setFileUri(str_replace('public', 'ffp-dummy-readonly', $file
    ->getFileUri()));
  $file
    ->save();

  // Attach the file to a node.
  $node['type'] = $this->contentType;
  $node[$field_name][0]['target_id'] = $file
    ->id();
  $node = $this
    ->drupalCreateNode($node);

  // Ensure file has been attached to a node.
  $this
    ->assertNotEmpty($node->{$field_name}[0], 'Read-only file is correctly attached to a node.');
  $edit['third_party_settings[filefield_paths][retroactive_update]'] = TRUE;
  $edit['third_party_settings[filefield_paths][file_path][value]'] = 'node/[node:nid]';
  $this
    ->drupalGet("admin/structure/types/manage/{$this->contentType}/fields/node.{$this->contentType}.{$field_name}");
  $this
    ->submitForm($edit, 'Save settings');

  // Ensure file is still in original location.
  $this
    ->drupalGet("node/{$node->id()}");

  // Read-only file not affected by Retroactive updates.
  $this
    ->assertSession()
    ->responseContains("{$this->publicFilesDirectory}/{$file->getFilename()}");
}