View source
<?php
namespace Drupal\Tests\filefield_sources\Functional;
use Drupal\Component\Render\PlainTextOutput;
class AttachSourceTest extends FileFieldSourcesTestBase {
public function testMoveFilesFromRelativePath() {
$uri_scheme = $this
->getFieldSetting('uri_scheme');
$path = $uri_scheme . '://' . FILEFIELD_SOURCE_ATTACH_DEFAULT_PATH . '/';
$this
->enableSources([
'attach' => TRUE,
]);
$file = $this
->createTemporaryFile($path);
$dest_uri = $this
->getDestinationUri($file, $uri_scheme);
$this
->drupalGet('node/add/' . $this->typeName);
$this
->fileCanBeUploadAndDeleted($file, $dest_uri);
$file = $this
->createTemporaryFile($path, 'test file.txt');
$dest_uri = $this
->getDestinationUri($file, $uri_scheme);
$this
->drupalGet('node/add/' . $this->typeName);
$this
->fileCanBeUploadAndDeleted($file, $dest_uri);
$original_filename = 'file_áéíóú_ññ.txt';
$transliterated_filename = 'file_aeiou_nn.txt';
$file = $this
->createTemporaryFile($path, $original_filename);
$file->filename = $transliterated_filename;
$dest_uri = $this
->getDestinationUri($file, $uri_scheme);
$this
->drupalGet('node/add/' . $this->typeName);
$this
->fileCanBeUploadAndDeleted($file, $dest_uri);
}
public function fileCanBeUploadAndDeleted($file, $dest_uri) {
$this
->assertCanAttachFile($file);
$this
->uploadFileByAttachSource($file->uri, $file->filename, 0);
$this
->assertNoFieldByXPath('//input[@type="submit"]', t('Attach'), 'After uploading a file, "Attach" button is no longer displayed.');
$this
->assertFalse(is_file($file->uri), 'Source file has been removed.');
$this
->assertTrue(is_file($dest_uri), 'Destination file has been created.');
$this
->removeFile($file->filename, 0);
$this
->assertCanNotAttachFile($file);
}
public function getDestinationUri($file, $uri_scheme) {
$destination = trim($this
->getFieldSetting('file_directory'), '/');
$destination = PlainTextOutput::renderFromHtml(\Drupal::token()
->replace($destination));
return $uri_scheme . '://' . $destination . '/' . $file->filename;
}
public function assertCanAttachFile($file) {
$this
->assertTrue($this
->isOptionPresent($file->uri), 'File option is present.');
$this
->assertNoText('There currently are no files to attach.', "Empty message is not present.");
$this
->assertFieldByXpath('//input[@type="submit"]', t('Attach'), 'Attach button is present.');
}
public function isOptionPresent($uri) {
$options = $this
->xpath('//select[@name=:name]/option[@value=:option]', [
':name' => $this->fieldName . '[0][filefield_attach][filename]',
':option' => $uri,
]);
return isset($options[0]);
}
public function assertCanNotAttachFile($file) {
$this
->assertFalse($this
->isOptionPresent($file->uri), 'File option is not present.');
$this
->assertText('There currently are no files to attach.', "Empty message is present.");
$this
->assertFieldByXpath('//input[@type="submit"]', t('Attach'), 'Attach button is present.');
}
public function testCopyFileFromAbsolutePath() {
$uri_scheme = $this
->getFieldSetting('uri_scheme');
$path = $this
->getCustomAttachPath();
$file = $this
->createTemporaryFile($path);
$dest_uri = $this
->getDestinationUri($file, $uri_scheme);
$this
->updateFilefieldSourcesSettings('source_attach', 'path', $path);
$this
->updateFilefieldSourcesSettings('source_attach', 'absolute', FILEFIELD_SOURCE_ATTACH_ABSOLUTE);
$this
->updateFilefieldSourcesSettings('source_attach', 'attach_mode', FILEFIELD_SOURCE_ATTACH_MODE_COPY);
$this
->enableSources([
'attach' => TRUE,
]);
$this
->assertCanAttachFile($file);
$this
->uploadFileByAttachSource($file->uri, $file->filename, 0);
$this
->assertNoFieldByXPath('//input[@type="submit"]', t('Attach'), 'After uploading a file, "Attach" button is no longer displayed.');
$this
->assertTrue(is_file($file->uri), 'Source file still exists.');
$this
->assertTrue(is_file($dest_uri), 'Destination file has been created.');
$this
->removeFile($file->filename, 0);
$this
->assertCanAttachFile($file);
}
public function getCustomAttachPath() {
$path = \Drupal::service('file_system')
->realpath($this
->getFieldSetting('uri_scheme') . '://');
$path = str_replace(realpath('./'), '', $path);
$path = ltrim($path, '/');
$path = $path . '/custom_file_attach/';
return $path;
}
}