filefield_paths.general.test 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. /**
  3. * @file
  4. * Tests for the File (Field) Paths module.
  5. */
  6. /**
  7. * Class FileFieldPathsGeneralTestCase
  8. */
  9. class FileFieldPathsGeneralTestCase extends FileFieldPathsTestCase {
  10. /**
  11. * @inheritdoc
  12. */
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'General functionality',
  16. 'description' => 'Test general functionality.',
  17. 'group' => 'File (Field) Paths',
  18. );
  19. }
  20. /**
  21. * Test that the File (Field) Paths UI works as expected.
  22. */
  23. public function testAddField() {
  24. // Create a File field.
  25. $field_name = drupal_strtolower($this->randomName());
  26. $instance_settings = array('file_directory' => "fields/{$field_name}");
  27. $this->createFileField($field_name, $this->content_type, array(), $instance_settings);
  28. // Ensure File (Field) Paths settings are present.
  29. $this->drupalGet("admin/structure/types/manage/{$this->content_type}/fields/{$field_name}");
  30. $this->assertText('Enable File (Field) Paths?', t('File (Field) Path settings are present.'));
  31. // Ensure that 'Enable File (Field) Paths?' is a direct sibling of
  32. // 'File (Field) Path settings'.
  33. $element = $this->xpath('//div[contains(@class, :class)]/following-sibling::*[1]/@id', array(':class' => 'form-item-instance-settings-filefield-paths-enabled'));
  34. $this->assert(isset($element[0]) && 'edit-instance-settings-filefield-paths' == (string) $element[0], t('Enable checkbox is next to settings fieldset.'));
  35. // Ensure that the File path used the File directory as it's default value.
  36. $this->assertFieldByName('instance[settings][filefield_paths][file_path][value]', "fields/{$field_name}");
  37. }
  38. /**
  39. * Test File (Field) Paths works as normal when no file uploaded.
  40. */
  41. public function testNoFile() {
  42. // Create a File field.
  43. $field_name = drupal_strtolower($this->randomName());
  44. $instance_settings['filefield_paths']['file_path']['value'] = 'node/[node:nid]';
  45. $instance_settings['filefield_paths']['file_name']['value'] = '[node:nid].[file:ffp-extension-original]';
  46. $this->createFileField($field_name, $this->content_type, array(), $instance_settings);
  47. // Create a node without a file attached.
  48. $this->drupalCreateNode(array('type' => $this->content_type));
  49. }
  50. /**
  51. * Test a basic file upload with File (Field) Paths.
  52. */
  53. public function testUploadFile() {
  54. $langcode = LANGUAGE_NONE;
  55. // Create a File field with 'node/[node:nid]' as the File path and
  56. // '[node:nid].[file:ffp-extension-original]' as the File name.
  57. $field_name = drupal_strtolower($this->randomName());
  58. $instance_settings['filefield_paths']['file_path']['value'] = 'node/[node:nid]';
  59. $instance_settings['filefield_paths']['file_name']['value'] = '[node:nid].[file:ffp-extension-original]';
  60. $this->createFileField($field_name, $this->content_type, array(), $instance_settings);
  61. $schemes = array('public', 'private');
  62. foreach ($schemes as $scheme) {
  63. // Set the field URI scheme.
  64. $this->drupalPost("admin/structure/types/manage/{$this->content_type}/fields/{$field_name}", array('field[settings][uri_scheme]' => $scheme), t('Save settings'));
  65. // Upload a file to a node.
  66. $test_file = $this->getTestFile('text');
  67. $this->drupalGet("node/add/{$this->content_type}");
  68. $edit['title'] = $this->randomName();
  69. $edit["files[{$field_name}_{$langcode}_0]"] = $test_file->uri;
  70. $this->drupalPost(NULL, $edit, t('Upload'));
  71. // Ensure that the file was put into the Temporary file location.
  72. $temp_location = variable_get('filefield_paths_temp_location', 'public://filefield_paths');
  73. $this->assertRaw(file_create_url("{$temp_location}/{$test_file->filename}"), t('File has been uploaded to the temporary file location.'));
  74. // Save the node.
  75. $this->drupalPost(NULL, array(), t('Save'));
  76. // Get created Node ID.
  77. $matches = array();
  78. preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches);
  79. $nid = $matches[1];
  80. // Ensure that the File path has been processed correctly.
  81. $uri = file_create_url("{$scheme}://node/{$nid}/{$nid}.txt");
  82. $this->assertRaw($uri, t('The File path has been processed correctly.'));
  83. // Delete the node so we can change the URI scheme.
  84. node_delete($nid);
  85. }
  86. }
  87. /**
  88. * Tests a multivalue file upload with File (Field) Paths.
  89. */
  90. public function testUploadFileMultivalue() {
  91. $langcode = LANGUAGE_NONE;
  92. // Create a multivalue File field with 'node/[node:nid]' as the File path
  93. // and '[file:fid].txt' as the File name.
  94. $field_name = drupal_strtolower($this->randomName());
  95. $field_settings['cardinality'] = FIELD_CARDINALITY_UNLIMITED;
  96. $instance_settings['filefield_paths']['file_path']['value'] = 'node/[node:nid]';
  97. $instance_settings['filefield_paths']['file_name']['value'] = '[file:fid].txt';
  98. $this->createFileField($field_name, $this->content_type, $field_settings, $instance_settings);
  99. // Create a node with three (3) test files.
  100. $text_files = $this->drupalGetTestFiles('text');
  101. $this->drupalGet("node/add/{$this->content_type}");
  102. $this->drupalPost(NULL, array("files[{$field_name}_{$langcode}_0]" => drupal_realpath($text_files[0]->uri)), t('Upload'));
  103. $this->drupalPost(NULL, array("files[{$field_name}_{$langcode}_1]" => drupal_realpath($text_files[1]->uri)), t('Upload'));
  104. $edit = array(
  105. 'title' => $this->randomName(),
  106. "files[{$field_name}_{$langcode}_2]" => drupal_realpath($text_files[1]->uri),
  107. );
  108. $this->drupalPost(NULL, $edit, t('Save'));
  109. // Get created Node ID.
  110. $matches = array();
  111. preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches);
  112. $nid = $matches[1];
  113. // Ensure that the File path has been processed correctly.
  114. $this->assertRaw("{$this->public_files_directory}/node/{$nid}/1.txt", t('The first File path has been processed correctly.'));
  115. $this->assertRaw("{$this->public_files_directory}/node/{$nid}/2.txt", t('The second File path has been processed correctly.'));
  116. $this->assertRaw("{$this->public_files_directory}/node/{$nid}/3.txt", t('The third File path has been processed correctly.'));
  117. }
  118. /**
  119. * Test File (Field) Paths with a very long path.
  120. */
  121. public function testLongPath() {
  122. // Create a File field with 'node/[random:hash:sha256]' as the File path.
  123. $field_name = drupal_strtolower($this->randomName());
  124. $instance_settings['filefield_paths']['file_path']['value'] = 'node/[random:hash:sha512]/[random:hash:sha512]';
  125. $this->createFileField($field_name, $this->content_type, array(), $instance_settings);
  126. // Create a node with a test file.
  127. $test_file = $this->getTestFile('text');
  128. $nid = $this->uploadNodeFile($test_file, $field_name, $this->content_type);
  129. // Ensure file path is no more than 255 characters.
  130. $node = node_load($nid, NULL, TRUE);
  131. $this->assert(drupal_strlen($node->{$field_name}[LANGUAGE_NONE][0]['uri']) <= 255, t('File path is no more than 255 characters'));
  132. }
  133. /**
  134. * Test File (Field) Paths on a programmatically added file.
  135. */
  136. public function testProgrammaticAttach() {
  137. // Create a File field with 'node/[node:nid]' as the File path and
  138. // '[node:nid].[file:ffp-extension-original]' as the File name.
  139. $field_name = drupal_strtolower($this->randomName());
  140. $instance_settings['filefield_paths']['file_path']['value'] = 'node/[node:nid]';
  141. $instance_settings['filefield_paths']['file_name']['value'] = '[node:nid].[file:ffp-extension-original]';
  142. $this->createFileField($field_name, $this->content_type, array(), $instance_settings);
  143. // Create a node without an attached file.
  144. $node = $this->drupalCreateNode(array('type' => $this->content_type));
  145. // Create a file object.
  146. $test_file = $this->getTestFile('text');
  147. $file = new stdClass();
  148. $file->fid = NULL;
  149. $file->uri = $test_file->uri;
  150. $file->filename = basename($file->uri);
  151. $file->filemime = file_get_mimetype($file->uri);
  152. $file->uid = $GLOBALS['user']->uid;
  153. $file->status = FILE_STATUS_PERMANENT;
  154. $file->display = TRUE;
  155. file_save($file);
  156. // Adjust timestamp to simulate real-world experience.
  157. $file->timestamp = REQUEST_TIME - 60;
  158. // Attach the file to the node.
  159. $node->{$field_name}[LANGUAGE_NONE][0] = (array) $file;
  160. node_save($node);
  161. // Ensure that the File path has been processed correctly.
  162. $node = node_load($node->nid, NULL, TRUE);
  163. $this->assertEqual("public://node/{$node->nid}/{$node->nid}.txt", $node->{$field_name}[LANGUAGE_NONE][0]['uri'], t('The File path has been processed correctly.'));
  164. }
  165. /**
  166. * Test File (Field) Paths slashes cleanup functionality.
  167. */
  168. public function testSlashes() {
  169. $langcode = LANGUAGE_NONE;
  170. // Create a File field with 'node/[node:title]' as the File path and
  171. // '[node:title].[file:ffp-extension-original]' as the File name.
  172. $field_name = drupal_strtolower($this->randomName());
  173. $instance_settings['filefield_paths']['file_path']['value'] = 'node/[node:title]';
  174. $instance_settings['filefield_paths']['file_name']['value'] = '[node:title].[file:ffp-extension-original]';
  175. $this->createFileField($field_name, $this->content_type, array(), $instance_settings);
  176. // Create a node with a test file.
  177. $test_file = $this->getTestFile('text');
  178. $title = "{$this->randomName()}/{$this->randomName()}";
  179. $edit['title'] = $title;
  180. $edit["body[{$langcode}][0][value]"] = '';
  181. $edit["files[{$field_name}_{$langcode}_0]"] = drupal_realpath($test_file->uri);
  182. $this->drupalPost("node/add/{$this->content_type}", $edit, t('Save'));
  183. // Get created Node ID.
  184. $matches = array();
  185. preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches);
  186. $nid = $matches[1];
  187. // Ensure slashes are present in file path and name.
  188. $node = node_load($nid);
  189. $this->assertEqual("public://node/{$title}/{$title}.txt", $node->{$field_name}[$langcode][0]['uri']);
  190. // Remove slashes.
  191. $edit = array(
  192. 'instance[settings][filefield_paths][file_path][options][slashes]' => TRUE,
  193. 'instance[settings][filefield_paths][file_name][options][slashes]' => TRUE,
  194. 'instance[settings][filefield_paths][retroactive_update]' => TRUE,
  195. );
  196. $this->drupalPost("admin/structure/types/manage/{$this->content_type}/fields/{$field_name}", $edit, t('Save settings'));
  197. // Ensure slashes are not present in file path and name.
  198. $node = node_load($nid, NULL, TRUE);
  199. $title = str_replace('/', '', $title);
  200. $this->assertEqual("public://node/{$title}/{$title}.txt", $node->{$field_name}[$langcode][0]['uri']);
  201. }
  202. /**
  203. * Test a file usage of a basic file upload with File (Field) Paths.
  204. */
  205. public function testFileUsage() {
  206. // Create a File field with 'node/[node:nid]' as the File path.
  207. $field_name = drupal_strtolower($this->randomName());
  208. $instance_settings['filefield_paths']['file_path']['value'] = 'node/[node:nid]';
  209. $this->createFileField($field_name, $this->content_type, array(), $instance_settings);
  210. // Create a node with a test file.
  211. $test_file = $this->getTestFile('text');
  212. $nid = $this->uploadNodeFile($test_file, $field_name, $this->content_type);
  213. // Get file usage for uploaded file.
  214. $node = node_load($nid, NULL, TRUE);
  215. $items = field_get_items('node', $node, $field_name);
  216. $file = file_load($items[0]['fid']);
  217. $usage = file_usage_list($file);
  218. // Ensure file usage count for new node is correct.
  219. $this->assert(isset($usage['file']['node'][$nid]) && $usage['file']['node'][$nid] == 1, t('File usage count for new node is correct.'));
  220. // Update node.
  221. $this->drupalPost("node/{$nid}/edit", array(), t('Save'));
  222. $usage = file_usage_list($file);
  223. // Ensure file usage count for updated node is correct.
  224. $this->assert(isset($usage['file']['node'][$nid]) && $usage['file']['node'][$nid] == 1, t('File usage count for updated node is correct.'));
  225. // Update node with revision.
  226. $this->drupalPost("node/{$nid}/edit", array('revision' => TRUE), t('Save'));
  227. $usage = file_usage_list($file);
  228. // Ensure file usage count for updated node with revision is correct.
  229. $this->assert(isset($usage['file']['node'][$nid]) && $usage['file']['node'][$nid] == 2, t('File usage count for updated node with revision is correct.'));
  230. }
  231. /**
  232. * Test File (Field) Paths works with read-only stream wrappers.
  233. */
  234. public function testReadOnly() {
  235. // Create a File field.
  236. $field_name = drupal_strtolower($this->randomName());
  237. $field_settings = array('uri_scheme' => 'ffp');
  238. $instance_settings = array('file_directory' => "fields/{$field_name}");
  239. $this->createFileField($field_name, $this->content_type, $field_settings, $instance_settings);
  240. // Get a test file.
  241. $file = $this->getTestFile('image');
  242. // Prepare the file for the test 'ffp://' read-only stream wrapper.
  243. $file->uri = str_replace('public', 'ffp', $file->uri);
  244. $uri = file_stream_wrapper_uri_normalize($file->uri);
  245. // Create a file object.
  246. $file = new stdClass();
  247. $file->fid = NULL;
  248. $file->uri = $uri;
  249. $file->filename = basename($file->uri);
  250. $file->filemime = file_get_mimetype($file->uri);
  251. $file->uid = $GLOBALS['user']->uid;
  252. $file->status = FILE_STATUS_PERMANENT;
  253. $file->display = TRUE;
  254. file_save($file);
  255. // Attach the file to a node.
  256. $node = array();
  257. $node['type'] = $this->content_type;
  258. $node[$field_name][LANGUAGE_NONE][0] = (array) $file;
  259. $node = $this->drupalCreateNode($node);
  260. // Ensure file has been attached to a node.
  261. $this->assert(isset($node->{$field_name}[LANGUAGE_NONE][0]) && !empty($node->{$field_name}[LANGUAGE_NONE][0]), t('Read-only file is correctly attached to a node.'));
  262. $edit = array();
  263. $edit['instance[settings][filefield_paths][retroactive_update]'] = TRUE;
  264. $edit['instance[settings][filefield_paths][file_path][value]'] = 'node/[node:nid]';
  265. $this->drupalPost("admin/structure/types/manage/{$this->content_type}/fields/{$field_name}", $edit, t('Save settings'));
  266. // Ensure file is still in original location.
  267. $this->drupalGet("node/{$node->nid}");
  268. $this->assertRaw("{$this->public_files_directory}/{$file->filename}", t('Read-only file not affected by Retroactive updates.'));
  269. }
  270. }