MimeTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace Tools\Test\TestCase\Utility;
  3. use Cake\Core\Plugin;
  4. use Cake\Network\Response;
  5. use Tools\TestSuite\TestCase;
  6. use Tools\Utility\Mime;
  7. class MimeTest extends TestCase {
  8. /**
  9. * @var \Tools\Utility\Mime
  10. */
  11. public $Mime;
  12. public function setUp() {
  13. parent::setUp();
  14. $this->Mime = new Mime();
  15. }
  16. public function testObject() {
  17. $this->assertTrue(is_object($this->Mime));
  18. $this->assertInstanceOf('Tools\Utility\Mime', $this->Mime);
  19. }
  20. public function testAll() {
  21. $res = $this->Mime->mimeTypes();
  22. $this->assertTrue(is_array($res) && count($res) > 100);
  23. }
  24. public function testSingle() {
  25. $res = $this->Mime->getMimeTypeByAlias('odxs');
  26. $this->assertFalse($res);
  27. $res = $this->Mime->getMimeTypeByAlias('ods');
  28. $this->assertEquals('application/vnd.oasis.opendocument.spreadsheet', $res);
  29. }
  30. public function testOverwrite() {
  31. $res = $this->Mime->getMimeTypeByAlias('ics');
  32. $this->assertEquals('application/ics', $res);
  33. }
  34. public function testReverseToSingle() {
  35. $res = $this->Mime->getMimeTypeByAlias('html');
  36. $this->assertEquals('text/html', $res);
  37. $res = $this->Mime->getMimeTypeByAlias('csv');
  38. $this->assertEquals('text/csv', $res);
  39. }
  40. public function testReverseToMultiple() {
  41. $res = $this->Mime->getMimeTypeByAlias('html', false);
  42. $this->assertTrue(is_array($res));
  43. $this->assertSame(2, count($res));
  44. $res = $this->Mime->getMimeTypeByAlias('csv', false);
  45. $this->assertTrue(is_array($res)); // && count($res) > 2
  46. $this->assertSame(2, count($res));
  47. }
  48. /**
  49. * Test fake files
  50. *
  51. * @return void
  52. */
  53. public function testCorrectFileExtension() {
  54. file_put_contents(TMP . 'sometest.txt', 'xyz');
  55. $is = $this->Mime->detectMimeType(TMP . 'sometest.txt');
  56. //pr($is);
  57. $this->assertEquals($is, 'text/plain');
  58. }
  59. /**
  60. * Test fake files
  61. *
  62. * @return void
  63. */
  64. public function testWrongFileExtension() {
  65. file_put_contents(TMP . 'sometest.zip', 'xyz');
  66. $is = $this->Mime->detectMimeType(TMP . 'sometest.zip');
  67. //pr($is);
  68. $this->assertEquals($is, 'text/plain');
  69. //Test failes? finfo_open not availaible??
  70. }
  71. /**
  72. * testgetMimeTypeByAlias()
  73. *
  74. * @return void
  75. */
  76. public function testgetMimeTypeByAlias() {
  77. $res = $this->Mime->detectMimeType('http://www.spiegel.de/static/sys/v10/icons/home_v2.png');
  78. $this->assertEquals('image/png', $res);
  79. $res = $this->Mime->detectMimeType('http://www.spiegel.de/static/sys/v10/icons/home_v2_inexistent.png');
  80. $this->assertEquals('', $res);
  81. $res = $this->Mime->detectMimeType(Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.jpg');
  82. $this->assertEquals('image/jpeg', $res);
  83. }
  84. /**
  85. * Test fake files
  86. *
  87. * @return void
  88. */
  89. public function testEncoding() {
  90. file_put_contents(TMP . 'sometest.txt', 'xyz');
  91. $is = $this->Mime->getEncoding(TMP . 'sometest.txt');
  92. //pr($is);
  93. $this->assertEquals($is, 'us-ascii');
  94. file_put_contents(TMP . 'sometest.zip', utf8_encode('xäääyz'));
  95. $is = $this->Mime->getEncoding(TMP . 'sometest.zip');
  96. //pr($is);
  97. $this->assertEquals($is, 'utf-8');
  98. file_put_contents(TMP . 'sometest.zip', utf8_encode('xyz'));
  99. $is = $this->Mime->getEncoding(TMP . 'sometest.zip');
  100. //pr($is);
  101. $this->assertEquals($is, 'us-ascii');
  102. //Tests fail? finfo_open not availaible??
  103. }
  104. /**
  105. * MimeLibTest::testDifferenceBetweenPluginAndCore()
  106. *
  107. * @return void
  108. */
  109. public function testDifferenceBetweenPluginAndCore() {
  110. $this->TestCakeResponse = new TestCakeResponse();
  111. $this->TestMime = new TestMime();
  112. $core = $this->TestCakeResponse->getMimeTypes();
  113. $plugin = $this->TestMime->getMimeTypes();
  114. $diff = [
  115. 'coreonly' => [],
  116. 'pluginonly' => [],
  117. 'modified' => []
  118. ];
  119. foreach ($core as $key => $value) {
  120. if (!isset($plugin[$key])) {
  121. $diff['coreonly'][$key] = $value;
  122. } elseif ($value !== $plugin[$key]) {
  123. $diff['modified'][$key] = ['was' => $value, 'is' => $plugin[$key]];
  124. }
  125. unset($plugin[$key]);
  126. }
  127. foreach ($plugin as $key => $value) {
  128. $diff['pluginonly'][$key] = $value;
  129. }
  130. //$this->debug($diff);
  131. }
  132. }
  133. class TestCakeResponse extends Response {
  134. public function getMimeTypes() {
  135. return $this->_mimeTypes;
  136. }
  137. }
  138. class TestMime extends Mime {
  139. public function getMimeTypes($coreHasPrecedence = false) {
  140. return $this->_mimeTypesExt;
  141. }
  142. }