filefield_paths.tokens.test 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * @file
  4. * Tests for the File (Field) Paths module.
  5. */
  6. /**
  7. * Class FileFieldPathsTokensTestCase
  8. */
  9. class FileFieldPathsTokensTestCase extends FileFieldPathsTestCase {
  10. /**
  11. * @inheritdoc
  12. */
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Token functionality',
  16. 'description' => 'Tests File (Field) Paths tokens.',
  17. 'group' => 'File (Field) Paths',
  18. );
  19. }
  20. /**
  21. * @param $token
  22. * @param $value
  23. * @param $data
  24. */
  25. public function assertToken($token, $value, $data) {
  26. $result = token_replace($token, $data);
  27. $this->assertEqual($result, $value, t('Token @token equals @value', array(
  28. '@token' => $token,
  29. '@value' => $value
  30. )));
  31. }
  32. /**
  33. * Test token values with a text file.
  34. */
  35. public function testTokensBasic() {
  36. // Prepare a test text file.
  37. $text_file = $this->getTestFile('text');
  38. file_save($text_file);
  39. // Ensure tokens are processed correctly.
  40. $data = array('file' => $text_file);
  41. $this->assertToken('[file:ffp-name-only]', 'text-0', $data);
  42. $this->assertToken('[file:ffp-name-only-original]', 'text-0', $data);
  43. $this->assertToken('[file:ffp-extension-original]', 'txt', $data);
  44. }
  45. /**
  46. * Test token values with a moved text file.
  47. */
  48. public function testTokensMoved() {
  49. // Prepare a test text file.
  50. $text_file = $this->getTestFile('text');
  51. file_save($text_file);
  52. // Move the text file.
  53. $moved_file = file_move($text_file, 'public://moved.diff');
  54. // Ensure tokens are processed correctly.
  55. $data = array('file' => $moved_file);
  56. $this->assertToken('[file:ffp-name-only]', 'moved', $data);
  57. $this->assertToken('[file:ffp-name-only-original]', 'text-0', $data);
  58. $this->assertToken('[file:ffp-extension-original]', 'txt', $data);
  59. }
  60. /**
  61. * Test token values with a multi-extension text file.
  62. */
  63. public function testTokensMultiExtension() {
  64. // Prepare a test text file.
  65. $text_file = $this->getTestFile('text');
  66. file_unmanaged_copy($text_file->uri, 'public://text.multiext.txt');
  67. $files = file_scan_directory('public://', '/text\.multiext\.txt/');
  68. $multiext_file = current($files);
  69. file_save($multiext_file);
  70. // Ensure tokens are processed correctly.
  71. $data = array('file' => $multiext_file);
  72. $this->assertToken('[file:ffp-name-only]', 'text.multiext', $data);
  73. $this->assertToken('[file:ffp-name-only-original]', 'text.multiext', $data);
  74. $this->assertToken('[file:ffp-extension-original]', 'txt', $data);
  75. }
  76. /**
  77. * Test token value with a UTF file.
  78. * @see https://www.drupal.org/node/1292436
  79. */
  80. public function testTokensUTF() {
  81. // Prepare a test text file.
  82. $text_file = $this->getTestFile('text');
  83. file_unmanaged_copy($text_file->uri, 'public://тест.txt');
  84. $files = file_scan_directory('public://', '/тест\.txt/');
  85. $utf_file = current($files);
  86. file_save($utf_file);
  87. // Ensure tokens are processed correctly.
  88. $data = array('file' => $utf_file);
  89. $this->assertToken('[file:ffp-name-only]', 'тест', $data);
  90. }
  91. }