X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Ffilefield_sources%2Fsrc%2FTests%2FAttachSourceTest.php;fp=web%2Fmodules%2Fcontrib%2Ffilefield_sources%2Fsrc%2FTests%2FAttachSourceTest.php;h=71ff7b174c997400190c534a36b54dda1f57dde0;hp=0000000000000000000000000000000000000000;hb=8acec36f19c470dfcda1ae2336826a782f41874c;hpb=e0411c4e83ba0d079034db83c3f7f55be24a0e35 diff --git a/web/modules/contrib/filefield_sources/src/Tests/AttachSourceTest.php b/web/modules/contrib/filefield_sources/src/Tests/AttachSourceTest.php new file mode 100644 index 000000000..71ff7b174 --- /dev/null +++ b/web/modules/contrib/filefield_sources/src/Tests/AttachSourceTest.php @@ -0,0 +1,168 @@ +xpath('//select[@name=:name]/option[@value=:option]', array( + ':name' => $this->fieldName . '[0][filefield_attach][filename]', + ':option' => $uri, + )); + return isset($options[0]); + } + + /** + * Check to see if can attach file. + * + * @param object $file + * File to attach. + */ + public function assertCanAttachFile($file) { + // Ensure option is present. + $this->assertTrue($this->isOptionPresent($file->uri), 'File option is present.'); + + // Ensure empty message is not present. + $this->assertNoText('There currently are no files to attach.', "Empty message is not present."); + + // Attach button is always present. + $this->assertFieldByXpath('//input[@type="submit"]', t('Attach'), 'Attach button is present.'); + } + + /** + * Check to see if can attach file. + * + * @param object $file + * File to attach. + */ + public function assertCanNotAttachFile($file) { + // Ensure option is not present. + $this->assertFalse($this->isOptionPresent($file->uri), 'File option is not present.'); + + // Ensure empty message is present. + $this->assertText('There currently are no files to attach.', "Empty message is present."); + + // Attach button is always present. + $this->assertFieldByXpath('//input[@type="submit"]', t('Attach'), 'Attach button is present.'); + } + + /** + * Tests move file from relative path. + * + * Default settings: Move file from 'public://file_attach' to 'public://'. + */ + public function testMoveFileFromRelativePath() { + $uri_scheme = $this->getFieldSetting('uri_scheme'); + $path = $uri_scheme . '://' . FILEFIELD_SOURCE_ATTACH_DEFAULT_PATH . '/'; + + // Create test file. + $file = $this->createTemporaryFile($path); + $dest_uri = $this->getDestinationUri($file, $uri_scheme); + + $this->enableSources(array( + 'attach' => TRUE, + )); + + $this->assertCanAttachFile($file); + + // Upload a file. + $this->uploadFileByAttachSource($file->uri, $file->filename, 0); + + // We can only attach one file on single value field. + $this->assertNoFieldByXPath('//input[@type="submit"]', t('Attach'), 'After uploading a file, "Attach" button is no longer displayed.'); + + // Ensure file is moved. + $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); + } + + /** + * Calculate custom absolute path. + */ + public function getCustomAttachPath() { + $path = drupal_realpath($this->getFieldSetting('uri_scheme') . '://'); + $path = str_replace(realpath('./'), '', $path); + $path = ltrim($path, '/'); + $path = $path . '/custom_file_attach/'; + return $path; + } + + /** + * Tests copy file from absolute path. + * + * Copy file from 'sites/default/files/custom_file_attach' to 'public://'. + */ + public function testCopyFileFromAbsolutePath() { + $uri_scheme = $this->getFieldSetting('uri_scheme'); + $path = $this->getCustomAttachPath(); + + // Create test file. + $file = $this->createTemporaryFile($path); + $dest_uri = $this->getDestinationUri($file, $uri_scheme); + + // Change settings. + $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(array( + 'attach' => TRUE, + )); + + $this->assertCanAttachFile($file); + + // Upload a file. + $this->uploadFileByAttachSource($file->uri, $file->filename, 0); + + // We can only attach one file on single value field. + $this->assertNoFieldByXPath('//input[@type="submit"]', t('Attach'), 'After uploading a file, "Attach" button is no longer displayed.'); + + // Ensure file is copied. + $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); + } + + /** + * Get destination uri of a . + * + * @param object $file + * File. + * @param string $uri_scheme + * Uri scheme. + */ + 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; + } + +}