View source
<?php
namespace Drupal\Tests\filefield_paths\Functional;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\node\Entity\Node;
class FileFieldPathsGeneralTest extends FileFieldPathsTestBase {
public function testAddField() {
$session = $this
->assertSession();
$field_name = mb_strtolower($this
->randomMachineName());
$field_settings = [
'file_directory' => "fields/{$field_name}",
];
$this
->createFileField($field_name, 'node', $this->contentType, [], $field_settings);
$this
->drupalGet("admin/structure/types/manage/{$this->contentType}/fields/node.{$this->contentType}.{$field_name}");
$session
->responseContains('Enable File (Field) Paths?');
$element = $this
->xpath('//div[contains(@class, :class)]/following-sibling::*[1][@id=\'edit-third-party-settings-filefield-paths--2\']', [
':class' => 'form-item-third-party-settings-filefield-paths-enabled',
]);
$this
->assertNotEmpty($element, 'Enable checkbox is next to settings fieldset.');
$session
->fieldValueEquals('third_party_settings[filefield_paths][file_path][value]', "fields/{$field_name}");
}
public function testNoFile() {
$field_name = mb_strtolower($this
->randomMachineName());
$third_party_settings['filefield_paths']['file_path']['value'] = 'node/[node:nid]';
$third_party_settings['filefield_paths']['file_name']['value'] = '[node:nid].[file:ffp-extension-original]';
$this
->createFileField($field_name, 'node', $this->contentType, [], [], $third_party_settings);
$this
->drupalGet('node/add/' . $this->contentType);
$this
->submitForm([
'title[0][value]' => $this
->randomMachineName(8),
], 'Save');
}
public function testUploadFile() {
$session = $this
->assertSession();
$file_system = \Drupal::service('file_system');
$field_name = mb_strtolower($this
->randomMachineName());
$third_party_settings['filefield_paths']['file_path']['value'] = 'node/[node:nid]';
$third_party_settings['filefield_paths']['file_name']['value'] = '[node:nid].[file:ffp-extension-original]';
$this
->createFileField($field_name, 'node', $this->contentType, [], [], $third_party_settings);
$test_file = $this
->getTestFile('text');
$this
->drupalGet("node/add/{$this->contentType}");
$edit['title[0][value]'] = $this
->randomMachineName();
$edit["files[{$field_name}_0]"] = $file_system
->realpath($test_file
->getFileUri());
$this
->submitForm($edit, 'Upload');
$config = $this
->config('filefield_paths.settings');
$session
->responseContains(file_create_url("{$config->get('temp_location')}/{$test_file->getFilename()}"), 'File has been uploaded to the temporary file location.');
$this
->submitForm([], 'Save');
$matches = [];
preg_match('/node\\/([0-9]+)/', $this
->getUrl(), $matches);
$nid = $matches[1];
$session
->responseContains("{$this->publicFilesDirectory}/node/{$nid}/{$nid}.txt", 'The File path has been processed correctly.');
}
public function testUploadFileMultivalue() {
$file_system = \Drupal::service('file_system');
$field_name = mb_strtolower($this
->randomMachineName());
$storage_settings['cardinality'] = FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED;
$third_party_settings['filefield_paths']['file_path']['value'] = 'node/[node:nid]';
$third_party_settings['filefield_paths']['file_name']['value'] = '[file:fid].txt';
$this
->createFileField($field_name, 'node', $this->contentType, $storage_settings, [], $third_party_settings);
$text_files = $this
->drupalGetTestFiles('text');
$this
->drupalGet("node/add/{$this->contentType}");
$this
->submitForm([
"files[{$field_name}_0][]" => $file_system
->realpath($text_files[0]->uri),
], 'Upload');
$this
->submitForm([
"files[{$field_name}_1][]" => $file_system
->realpath($text_files[1]->uri),
], 'Upload');
$edit = [
'title[0][value]' => $this
->randomMachineName(),
"files[{$field_name}_2][]" => $file_system
->realpath($text_files[1]->uri),
];
$this
->submitForm($edit, 'Save');
$matches = [];
preg_match('/node\\/([0-9]+)/', $this
->getUrl(), $matches);
$nid = $matches[1];
$session = $this
->assertSession();
$session
->responseContains("{$this->publicFilesDirectory}/node/{$nid}/1.txt", 'The first File path has been processed correctly.');
$session
->responseContains("{$this->publicFilesDirectory}/node/{$nid}/2.txt", 'The second File path has been processed correctly.');
$session
->responseContains("{$this->publicFilesDirectory}/node/{$nid}/3.txt", 'The third File path has been processed correctly.');
}
public function testLongPath() {
$field_name = mb_strtolower($this
->randomMachineName());
$third_party_settings['filefield_paths']['file_path']['value'] = 'node/[random:hash:sha512]/[random:hash:sha512]';
$this
->createFileField($field_name, 'node', $this->contentType, [], [], $third_party_settings);
$test_file = $this
->getTestFile('text');
$nid = $this
->uploadNodeFile($test_file, $field_name, $this->contentType);
$node = Node::load($nid);
$this
->assertLessThanOrEqual(255, mb_strlen($node->{$field_name}->uri), 'File path is no more than 255 characters');
}
public function testProgrammaticAttach() {
$field_name = mb_strtolower($this
->randomMachineName());
$third_party_settings['filefield_paths']['file_path']['value'] = 'node/[node:nid]';
$third_party_settings['filefield_paths']['file_name']['value'] = '[node:nid].[file:ffp-extension-original]';
$this
->createFileField($field_name, 'node', $this->contentType, [], [], $third_party_settings);
$node = $this
->drupalCreateNode([
'type' => $this->contentType,
]);
$test_file = $this
->getTestFile('text');
$test_file
->setPermanent();
$test_file
->save();
$node->{$field_name}
->setValue([
'target_id' => $test_file
->id(),
]);
$node
->save();
$node = Node::load($node
->id());
$this
->assertSame("public://node/{$node->id()}/{$node->id()}.txt", $node->{$field_name}[0]->entity
->getFileUri(), 'The File path has been processed correctly.');
}
public function testSlashes() {
$file_system = \Drupal::service('file_system');
$etm = \Drupal::entityTypeManager();
$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);
$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');
$matches = [];
preg_match('/node\\/([0-9]+)/', $this
->getUrl(), $matches);
$nid = $matches[1];
$node = $etm
->getStorage('node')
->load($nid);
$this
->assertSame("public://node/{$title}/{$title}.txt", $node
->get($field_name)
->referencedEntities()[0]
->getFileUri());
$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,
]);
$node = $node_storage
->load($nid);
$title = str_replace('/', '', $title);
$this
->assertSame("public://node/{$title}/{$title}.txt", $node->{$field_name}[0]->entity
->getFileUri());
}
public function testFileUsage() {
$node_storage = $this->container
->get('entity_type.manager')
->getStorage('node');
$file_usage = $this->container
->get('file.usage');
$field_name = mb_strtolower($this
->randomMachineName());
$third_party_settings['filefield_paths']['file_path']['value'] = 'node/[node:nid]';
$this
->createFileField($field_name, 'node', $this->contentType, [], [], $third_party_settings);
$test_file = $this
->getTestFile('text');
$nid = $this
->uploadNodeFile($test_file, $field_name, $this->contentType);
$node_storage
->resetCache([
$nid,
]);
$node = $node_storage
->load($nid);
$file = $node->{$field_name}->entity;
$usage = $file_usage
->listUsage($file);
$this
->assertNotEmpty($usage['file']['node'][$nid]);
$this
->assertSame(1, (int) $usage['file']['node'][$nid], 'File usage count for new node is correct.');
$this
->drupalGet("node/{$nid}/edit");
$this
->submitForm([
'revision' => FALSE,
], 'Save');
$usage = $file_usage
->listUsage($file);
$this
->assertNotEmpty($usage['file']['node'][$nid]);
$this
->assertSame(1, (int) $usage['file']['node'][$nid], 'File usage count for updated node is correct.');
$this
->drupalGet("node/{$nid}/edit");
$this
->submitForm([
'revision' => TRUE,
], 'Save');
$usage = $file_usage
->listUsage($file);
$this
->assertNotEmpty($usage['file']['node'][$nid]);
$this
->assertSame(2, (int) $usage['file']['node'][$nid], 'File usage count for updated node with revision is correct.');
}
public function testReadOnly() {
$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);
$file = $this
->getTestFile('image');
$file
->setFileUri(str_replace('public', 'ffp-dummy-readonly', $file
->getFileUri()));
$file
->save();
$node['type'] = $this->contentType;
$node[$field_name][0]['target_id'] = $file
->id();
$node = $this
->drupalCreateNode($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');
$this
->drupalGet("node/{$node->id()}");
$this
->assertSession()
->responseContains("{$this->publicFilesDirectory}/{$file->getFilename()}");
}
}