filefield_paths.text_replace.test 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * @file
  4. * Tests for the File (Field) Paths module.
  5. */
  6. /**
  7. * Class FileFieldPathsTextReplaceTestCase
  8. */
  9. class FileFieldPathsTextReplaceTestCase extends FileFieldPathsTestCase {
  10. /**
  11. * @inheritdoc
  12. */
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Text replace functionality',
  16. 'description' => 'Tests text replace functionality.',
  17. 'group' => 'File (Field) Paths',
  18. );
  19. }
  20. /**
  21. * Generates all variations of the URI for text replacement.
  22. *
  23. * @param $uri
  24. * @param string $type
  25. *
  26. * @return mixed
  27. */
  28. protected function getPathVariations($uri, $type = 'image') {
  29. // Force clean urls on.
  30. $GLOBALS['conf']['clean_url'] = TRUE;
  31. $variations['uri'] = $uri;
  32. $variations['absolute'] = urldecode(file_create_url($uri));
  33. $variations['relative'] = parse_url($variations['absolute'], PHP_URL_PATH);
  34. if ($type == 'image') {
  35. $variations['image_style'] = urldecode(image_style_url('thumbnail', $uri));
  36. $variations['image_style_relative'] = parse_url($variations['image_style'], PHP_URL_PATH) . '?' . parse_url($variations['image_style'], PHP_URL_QUERY);
  37. }
  38. foreach ($variations as $key => $value) {
  39. $variations["{$key}_urlencode"] = urlencode($value);
  40. $variations["{$key}_drupal_encode_path"] = drupal_encode_path($value);
  41. }
  42. return $variations;
  43. }
  44. /**
  45. * Test text replace with multiple file uploads.
  46. */
  47. public function testTextReplace() {
  48. $langcode = LANGUAGE_NONE;
  49. // Create a File field with 'node/[node:nid]' as the File path and
  50. // '[node:nid].png’ as the File name,
  51. $field_name = drupal_strtolower($this->randomName());
  52. $instance_settings['filefield_paths']['file_path']['value'] = 'node/[node:nid]';
  53. $instance_settings['filefield_paths']['file_name']['value'] = '[node:nid].png';
  54. $this->createImageField($field_name, $this->content_type, array(), $instance_settings);
  55. // Prepare test files.
  56. $test_files['basic_image'] = $this->getTestFile('image');
  57. $test_files['complex_image'] = $this->getTestFile('image');
  58. file_unmanaged_copy($test_files['complex_image']->uri, 'public://test image.png');
  59. $files = file_scan_directory('public://', '/test image\.png/');
  60. $test_files['complex_image'] = current($files);
  61. // Iterate over each test file.
  62. foreach ($test_files as $type => $test_file) {
  63. // Get the available file paths for the test file.
  64. $uri = str_replace('public://', variable_get('filefield_paths_temp_location', 'public://filefield_paths') . '/', $test_file->uri);
  65. $source_paths = $this->getPathVariations($uri);
  66. // Upload a file and reference the original path(s) to the file in the body
  67. // field.
  68. $edit['title'] = $this->randomName();
  69. $edit["body[{$langcode}][0][value]"] = '';
  70. $edit["files[{$field_name}_{$langcode}_0]"] = drupal_realpath($test_file->uri);
  71. foreach ($source_paths as $key => $value) {
  72. $edit["body[{$langcode}][0][value]"] .= "{$key}: {$value}\n";
  73. }
  74. $this->drupalPost("node/add/{$this->content_type}", $edit, t('Save'));
  75. // Get created Node ID.
  76. $matches = array();
  77. preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches);
  78. $nid = $matches[1];
  79. // Ensure body field has updated file path.
  80. $node = node_load($nid);
  81. $destination_paths = $this->getPathVariations($node->{$field_name}[$langcode][0]['uri']);
  82. foreach ($destination_paths as $key => $value) {
  83. $this->assert($source_paths[$key] !== $destination_paths[$key] && strpos($node->body[$langcode][0]['value'], "{$key}: {$value}") !== FALSE, t('@type %key file path replaced successfully.', array(
  84. '@type' => str_replace('_', ' ', drupal_ucfirst($type)),
  85. '%key' => $key
  86. )));
  87. }
  88. }
  89. }
  90. }