public function FileFieldPathsGeneralTest::testSlashes in File (Field) Paths 8
Test File (Field) Paths slashes cleanup functionality.
File
- tests/
src/ Functional/ FileFieldPathsGeneralTest.php, line 190
Class
- FileFieldPathsGeneralTest
- Test general functionality.
Namespace
Drupal\Tests\filefield_paths\FunctionalCode
public function testSlashes() {
$file_system = \Drupal::service('file_system');
$etm = \Drupal::entityTypeManager();
// Create a File field with 'node/[node:title]' as the File path and
// '[node:title].[file:ffp-extension-original]' as the File name.
$field_name = mb_strtolower($this
->randomMachineName());
$third_party_settings['filefield_paths']['file_path']['value'] = 'node/[node:title]';
$third_party_settings['filefield_paths']['file_name']['value'] = '[node:title].[file:ffp-extension-original]';
$this
->createFileField($field_name, 'node', $this->contentType, [], [], $third_party_settings);
// Create a node with a test file.
/** @var \Drupal\file\Entity\File $test_file */
$test_file = $this
->getTestFile('text');
$title = "{$this->randomMachineName()}/{$this->randomMachineName()}";
$edit['title[0][value]'] = $title;
$edit["body[0][value]"] = '';
$edit["files[{$field_name}_0]"] = $file_system
->realpath($test_file
->getFileUri());
$this
->drupalGet('node/add/' . $this->contentType);
$this
->submitForm($edit, 'Save');
// Get created Node ID.
$matches = [];
preg_match('/node\\/([0-9]+)/', $this
->getUrl(), $matches);
$nid = $matches[1];
// Ensure slashes are present in file path and name.
$node = $etm
->getStorage('node')
->load($nid);
$this
->assertSame("public://node/{$title}/{$title}.txt", $node
->get($field_name)
->referencedEntities()[0]
->getFileUri());
// Remove slashes.
$edit = [
'third_party_settings[filefield_paths][file_path][options][slashes]' => TRUE,
'third_party_settings[filefield_paths][file_name][options][slashes]' => TRUE,
'third_party_settings[filefield_paths][retroactive_update]' => TRUE,
];
$this
->drupalGet("admin/structure/types/manage/{$this->contentType}/fields/node.{$this->contentType}.{$field_name}");
$this
->submitForm($edit, 'Save settings');
$etm
->getStorage('file')
->resetCache([
$node->{$field_name}->target_id,
]);
$node_storage = $etm
->getStorage('node');
$node_storage
->resetCache([
$nid,
]);
// Ensure slashes are not present in file path and name.
$node = $node_storage
->load($nid);
$title = str_replace('/', '', $title);
$this
->assertSame("public://node/{$title}/{$title}.txt", $node->{$field_name}[0]->entity
->getFileUri());
}