ElggCoreFilestoreTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Elgg Test Skeleton
  4. *
  5. * @package Elgg
  6. * @subpackage Test
  7. */
  8. class ElggCoreFilestoreTest extends \ElggCoreUnitTest {
  9. /**
  10. * Called before each test method.
  11. */
  12. public function setUp() {
  13. $this->filestore = new \ElggDiskFilestore();
  14. }
  15. /**
  16. * Called after each test method.
  17. */
  18. public function tearDown() {
  19. unset($this->filestore);
  20. }
  21. public function testFilenameOnFilestore() {
  22. global $CONFIG;
  23. // create a user to own the file
  24. $user = $this->createTestUser();
  25. $dir = new \Elgg\EntityDirLocator($user->guid);
  26. // setup a test file
  27. $file = new \ElggFile();
  28. $file->owner_guid = $user->guid;
  29. $file->setFilename('testing/filestore.txt');
  30. $file->open('write');
  31. $file->write('Testing!');
  32. $this->assertTrue($file->close());
  33. // ensure filename and path is expected
  34. $filename = $file->getFilenameOnFilestore($file);
  35. $filepath = $CONFIG->dataroot . $dir . 'testing/filestore.txt';
  36. $this->assertIdentical($filename, $filepath);
  37. $this->assertTrue(file_exists($filepath));
  38. // ensure file removed on user delete
  39. // deleting the user calls _elgg_clear_entity_files()
  40. $user->delete();
  41. $this->assertFalse(file_exists($filepath));
  42. }
  43. function testElggFileDelete() {
  44. global $CONFIG;
  45. $user = $this->createTestUser();
  46. $filestore = $this->filestore;
  47. $dir = new \Elgg\EntityDirLocator($user->guid);
  48. $file = new \ElggFile();
  49. $file->owner_guid = $user->guid;
  50. $file->setFilename('testing/ElggFileDelete');
  51. $this->assertTrue($file->open('write'));
  52. $this->assertTrue($file->write('Test'));
  53. $this->assertTrue($file->close());
  54. $file->save();
  55. $filename = $file->getFilenameOnFilestore($file);
  56. $filepath = $CONFIG->dataroot . $dir . "testing/ElggFileDelete";
  57. $this->assertIdentical($filename, $filepath);
  58. $this->assertTrue(file_exists($filepath));
  59. $this->assertTrue($file->delete());
  60. $this->assertFalse(file_exists($filepath));
  61. $user->delete();
  62. }
  63. function testElggGetFileSimpletype() {
  64. $tests = array(
  65. 'x-world/x-svr' => 'general',
  66. 'application/msword' => 'document',
  67. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'document',
  68. 'application/vnd.oasis.opendocument.text' => 'document',
  69. 'application/pdf' => 'document',
  70. 'application/ogg' => 'audio',
  71. 'text/css' => 'document',
  72. 'text/plain' => 'document',
  73. 'audio/midi' => 'audio',
  74. 'audio/mpeg' => 'audio',
  75. 'image/jpeg' => 'image',
  76. 'image/bmp' => 'image',
  77. 'video/mpeg' => 'video',
  78. 'video/quicktime' => 'video',
  79. );
  80. foreach ($tests as $mime_type => $simple_type) {
  81. $this->assertEqual($simple_type, elgg_get_file_simple_type($mime_type));
  82. }
  83. }
  84. function testDetectMimeType() {
  85. $user = $this->createTestUser();
  86. $file = new \ElggFile();
  87. $file->owner_guid = $user->guid;
  88. $file->setFilename('testing/filestore.txt');
  89. $file->open('write');
  90. $file->write('Testing!');
  91. $file->close();
  92. $mime = $file->detectMimeType(null, 'text/plain');
  93. // mime should not be null if default is set
  94. $this->assertTrue(isset($mime));
  95. // mime of a file object should match mime of a file path that represents this file on filestore
  96. $resource_mime = $file->detectMimeType($file->getFilenameOnFilestore(), 'text/plain');
  97. $this->assertIdentical($mime, $resource_mime);
  98. // calling detectMimeType statically raises strict policy warning
  99. // @todo: remove this once a new static method has been implemented
  100. error_reporting(E_ALL & ~E_STRICT & ~E_DEPRECATED);
  101. // method output should not differ between a static and a concrete call if the file path is set
  102. $resource_mime_static = \ElggFile::detectMimeType($file->getFilenameOnFilestore(), 'text/plain');
  103. $this->assertIdentical($resource_mime, $resource_mime_static);
  104. error_reporting(E_ALL);
  105. $user->delete();
  106. }
  107. protected function createTestUser($username = 'fileTest') {
  108. $user = new \ElggUser();
  109. $user->username = $username;
  110. $guid = $user->save();
  111. // load user to have access to creation time
  112. return get_entity($guid);
  113. }
  114. }