file_entity.test 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * @file
  4. * Test integration for the file_entity module.
  5. */
  6. class FileEntityTestHelper extends DrupalWebTestCase {
  7. protected $files = array();
  8. function setUp($modules = array()) {
  9. $modules[] = 'file_entity';
  10. parent::setUp($modules);
  11. $this->setUpFiles();
  12. }
  13. function setUpFiles() {
  14. $types = array('text', 'image');
  15. foreach ($types as $type) {
  16. foreach ($this->drupalGetTestFiles($type) as $file) {
  17. $this->files[$type][] = file_save($file);
  18. }
  19. }
  20. }
  21. }
  22. class FileEntityUnitTestCase extends FileEntityTestHelper {
  23. public static function getInfo() {
  24. return array(
  25. 'name' => 'File entity unit tests',
  26. 'description' => 'Test basic file entity funcitonality.',
  27. 'group' => 'File entity',
  28. );
  29. }
  30. /**
  31. * Regression tests for core issue http://drupal.org/node/1239376.
  32. */
  33. function testMimeTypeMappings() {
  34. $tests = array(
  35. 'public://test.ogg' => 'audio/ogg',
  36. 'public://test.mkv' => 'video/x-m4v',
  37. 'public://test.mka' => 'audio/x-matroska',
  38. 'public://test.mkv' => 'video/x-matroska',
  39. 'public://test.webp' => 'image/webp',
  40. );
  41. foreach ($tests as $input => $expected) {
  42. $this->assertEqual(file_get_mimetype($input), $expected);
  43. }
  44. }
  45. function testFileEntity() {
  46. $file = reset($this->files['text']);
  47. // Test entity ID, revision ID, and bundle.
  48. $ids = entity_extract_ids('file', $file);
  49. $this->assertIdentical($ids, array($file->fid, NULL, 'text'));
  50. // Test the entity URI callback.
  51. $uri = entity_uri('file', $file);
  52. $this->assertEqual($uri['path'], "file/{$file->fid}");
  53. }
  54. }
  55. class FileEntityTokenTestCase extends FileEntityTestHelper {
  56. public static function getInfo() {
  57. return array(
  58. 'name' => 'File entity tokens',
  59. 'description' => 'Test the file entity tokens.',
  60. 'group' => 'File entity',
  61. );
  62. }
  63. function testFileEntityTokens() {
  64. $tokens = array(
  65. 'type' => 'Text',
  66. 'type:name' => 'Text',
  67. 'type:machine-name' => 'text',
  68. 'type:count' => count($this->files['text']),
  69. );
  70. $this->assertTokens('file', array('file' => $this->files['text'][0]), $tokens);
  71. $tokens = array(
  72. 'type' => 'Image',
  73. 'type:name' => 'Image',
  74. 'type:machine-name' => 'image',
  75. 'type:count' => count($this->files['image']),
  76. );
  77. $this->assertTokens('file', array('file' => $this->files['image'][0]), $tokens);
  78. }
  79. function assertTokens($type, array $data, array $tokens, array $options = array()) {
  80. $token_input = drupal_map_assoc(array_keys($tokens));
  81. $values = token_generate($type, $token_input, $data, $options);
  82. foreach ($tokens as $token => $expected) {
  83. if (!isset($expected)) {
  84. $this->assertTrue(!isset($values[$token]), t("Token value for [@type:@token] was not generated.", array('@type' => $type, '@token' => $token)));
  85. }
  86. elseif (!isset($values[$token])) {
  87. $this->fail(t("Token value for [@type:@token] was not generated.", array('@type' => $type, '@token' => $token)));
  88. }
  89. elseif (!empty($options['regex'])) {
  90. $this->assertTrue(preg_match('/^' . $expected . '$/', $values[$token]), t("Token value for [@type:@token] was '@actual', matching regular expression pattern '@expected'.", array('@type' => $type, '@token' => $token, '@actual' => $values[$token], '@expected' => $expected)));
  91. }
  92. else {
  93. $this->assertIdentical($values[$token], $expected, t("Token value for [@type:@token] was '@actual', expected value '@expected'.", array('@type' => $type, '@token' => $token, '@actual' => $values[$token], '@expected' => $expected)));
  94. }
  95. }
  96. return $values;
  97. }
  98. }