5 * Definition of Drupal\filefield_sources\Tests\AttachSourceTest.
8 namespace Drupal\filefield_sources\Tests;
10 use Drupal\Component\Render\PlainTextOutput;
13 * Tests the attach source.
15 * @group filefield_sources
17 class AttachSourceTest extends FileFieldSourcesTestBase {
20 * Check to see if a option is present.
23 * The option to check.
26 * TRUE if the option is present, FALSE otherwise.
28 public function isOptionPresent($uri) {
29 $options = $this->xpath('//select[@name=:name]/option[@value=:option]', array(
30 ':name' => $this->fieldName . '[0][filefield_attach][filename]',
33 return isset($options[0]);
37 * Check to see if can attach file.
42 public function assertCanAttachFile($file) {
43 // Ensure option is present.
44 $this->assertTrue($this->isOptionPresent($file->uri), 'File option is present.');
46 // Ensure empty message is not present.
47 $this->assertNoText('There currently are no files to attach.', "Empty message is not present.");
49 // Attach button is always present.
50 $this->assertFieldByXpath('//input[@type="submit"]', t('Attach'), 'Attach button is present.');
54 * Check to see if can attach file.
59 public function assertCanNotAttachFile($file) {
60 // Ensure option is not present.
61 $this->assertFalse($this->isOptionPresent($file->uri), 'File option is not present.');
63 // Ensure empty message is present.
64 $this->assertText('There currently are no files to attach.', "Empty message is present.");
66 // Attach button is always present.
67 $this->assertFieldByXpath('//input[@type="submit"]', t('Attach'), 'Attach button is present.');
71 * Tests move file from relative path.
73 * Default settings: Move file from 'public://file_attach' to 'public://'.
75 public function testMoveFileFromRelativePath() {
76 $uri_scheme = $this->getFieldSetting('uri_scheme');
77 $path = $uri_scheme . '://' . FILEFIELD_SOURCE_ATTACH_DEFAULT_PATH . '/';
80 $file = $this->createTemporaryFile($path);
81 $dest_uri = $this->getDestinationUri($file, $uri_scheme);
83 $this->enableSources(array(
87 $this->assertCanAttachFile($file);
90 $this->uploadFileByAttachSource($file->uri, $file->filename, 0);
92 // We can only attach one file on single value field.
93 $this->assertNoFieldByXPath('//input[@type="submit"]', t('Attach'), 'After uploading a file, "Attach" button is no longer displayed.');
95 // Ensure file is moved.
96 $this->assertFalse(is_file($file->uri), 'Source file has been removed.');
97 $this->assertTrue(is_file($dest_uri), 'Destination file has been created.');
99 $this->removeFile($file->filename, 0);
101 $this->assertCanNotAttachFile($file);
105 * Calculate custom absolute path.
107 public function getCustomAttachPath() {
108 $path = drupal_realpath($this->getFieldSetting('uri_scheme') . '://');
109 $path = str_replace(realpath('./'), '', $path);
110 $path = ltrim($path, '/');
111 $path = $path . '/custom_file_attach/';
116 * Tests copy file from absolute path.
118 * Copy file from 'sites/default/files/custom_file_attach' to 'public://'.
120 public function testCopyFileFromAbsolutePath() {
121 $uri_scheme = $this->getFieldSetting('uri_scheme');
122 $path = $this->getCustomAttachPath();
125 $file = $this->createTemporaryFile($path);
126 $dest_uri = $this->getDestinationUri($file, $uri_scheme);
129 $this->updateFilefieldSourcesSettings('source_attach', 'path', $path);
130 $this->updateFilefieldSourcesSettings('source_attach', 'absolute', FILEFIELD_SOURCE_ATTACH_ABSOLUTE);
131 $this->updateFilefieldSourcesSettings('source_attach', 'attach_mode', FILEFIELD_SOURCE_ATTACH_MODE_COPY);
133 $this->enableSources(array(
137 $this->assertCanAttachFile($file);
140 $this->uploadFileByAttachSource($file->uri, $file->filename, 0);
142 // We can only attach one file on single value field.
143 $this->assertNoFieldByXPath('//input[@type="submit"]', t('Attach'), 'After uploading a file, "Attach" button is no longer displayed.');
145 // Ensure file is copied.
146 $this->assertTrue(is_file($file->uri), 'Source file still exists.');
147 $this->assertTrue(is_file($dest_uri), 'Destination file has been created.');
149 $this->removeFile($file->filename, 0);
151 $this->assertCanAttachFile($file);
155 * Get destination uri of a .
157 * @param object $file
159 * @param string $uri_scheme
162 public function getDestinationUri($file, $uri_scheme) {
163 $destination = trim($this->getFieldSetting('file_directory'), '/');
164 $destination = PlainTextOutput::renderFromHtml(\Drupal::token()->replace($destination));
165 return $uri_scheme . '://' . $destination . '/' . $file->filename;