Further modules included.
[yaffs-website] / web / modules / contrib / filefield_sources / src / Tests / AttachSourceTest.php
1 <?php
2
3 /**
4  * @file
5  * Definition of Drupal\filefield_sources\Tests\AttachSourceTest.
6  */
7
8 namespace Drupal\filefield_sources\Tests;
9
10 use Drupal\Component\Render\PlainTextOutput;
11
12 /**
13  * Tests the attach source.
14  *
15  * @group filefield_sources
16  */
17 class AttachSourceTest extends FileFieldSourcesTestBase {
18
19   /**
20    * Check to see if a option is present.
21    *
22    * @param string $uri
23    *   The option to check.
24    *
25    * @return bool
26    *   TRUE if the option is present, FALSE otherwise.
27    */
28   public function isOptionPresent($uri) {
29     $options = $this->xpath('//select[@name=:name]/option[@value=:option]', array(
30       ':name' => $this->fieldName . '[0][filefield_attach][filename]',
31       ':option' => $uri,
32     ));
33     return isset($options[0]);
34   }
35
36   /**
37    * Check to see if can attach file.
38    *
39    * @param object $file
40    *   File to attach.
41    */
42   public function assertCanAttachFile($file) {
43     // Ensure option is present.
44     $this->assertTrue($this->isOptionPresent($file->uri), 'File option is present.');
45
46     // Ensure empty message is not present.
47     $this->assertNoText('There currently are no files to attach.', "Empty message is not present.");
48
49     // Attach button is always present.
50     $this->assertFieldByXpath('//input[@type="submit"]', t('Attach'), 'Attach button is present.');
51   }
52
53   /**
54    * Check to see if can attach file.
55    *
56    * @param object $file
57    *   File to attach.
58    */
59   public function assertCanNotAttachFile($file) {
60     // Ensure option is not present.
61     $this->assertFalse($this->isOptionPresent($file->uri), 'File option is not present.');
62
63     // Ensure empty message is present.
64     $this->assertText('There currently are no files to attach.', "Empty message is present.");
65
66     // Attach button is always present.
67     $this->assertFieldByXpath('//input[@type="submit"]', t('Attach'), 'Attach button is present.');
68   }
69
70   /**
71    * Tests move file from relative path.
72    *
73    * Default settings: Move file from 'public://file_attach' to 'public://'.
74    */
75   public function testMoveFileFromRelativePath() {
76     $uri_scheme = $this->getFieldSetting('uri_scheme');
77     $path = $uri_scheme . '://' . FILEFIELD_SOURCE_ATTACH_DEFAULT_PATH . '/';
78
79     // Create test file.
80     $file = $this->createTemporaryFile($path);
81     $dest_uri = $this->getDestinationUri($file, $uri_scheme);
82
83     $this->enableSources(array(
84       'attach' => TRUE,
85     ));
86
87     $this->assertCanAttachFile($file);
88
89     // Upload a file.
90     $this->uploadFileByAttachSource($file->uri, $file->filename, 0);
91
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.');
94
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.');
98
99     $this->removeFile($file->filename, 0);
100
101     $this->assertCanNotAttachFile($file);
102   }
103
104   /**
105    * Calculate custom absolute path.
106    */
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/';
112     return $path;
113   }
114
115   /**
116    * Tests copy file from absolute path.
117    *
118    * Copy file from 'sites/default/files/custom_file_attach' to 'public://'.
119    */
120   public function testCopyFileFromAbsolutePath() {
121     $uri_scheme = $this->getFieldSetting('uri_scheme');
122     $path = $this->getCustomAttachPath();
123
124     // Create test file.
125     $file = $this->createTemporaryFile($path);
126     $dest_uri = $this->getDestinationUri($file, $uri_scheme);
127
128     // Change settings.
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);
132
133     $this->enableSources(array(
134       'attach' => TRUE,
135     ));
136
137     $this->assertCanAttachFile($file);
138
139     // Upload a file.
140     $this->uploadFileByAttachSource($file->uri, $file->filename, 0);
141
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.');
144
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.');
148
149     $this->removeFile($file->filename, 0);
150
151     $this->assertCanAttachFile($file);
152   }
153
154   /**
155    * Get destination uri of a .
156    *
157    * @param object $file
158    *   File.
159    * @param string $uri_scheme
160    *   Uri scheme.
161    */
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;
166   }
167
168 }