MimeTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace Tools\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. * @return void
  51. */
  52. public function testCorrectFileExtension() {
  53. file_put_contents(TMP . 'sometest.txt', 'xyz');
  54. $is = $this->Mime->detectMimeType(TMP . 'sometest.txt');
  55. //pr($is);
  56. $this->assertEquals($is, 'text/plain');
  57. }
  58. /**
  59. * Test fake files
  60. * @return void
  61. */
  62. public function testWrongFileExtension() {
  63. file_put_contents(TMP . 'sometest.zip', 'xyz');
  64. $is = $this->Mime->detectMimeType(TMP . 'sometest.zip');
  65. //pr($is);
  66. $this->assertEquals($is, 'text/plain');
  67. //Test failes? finfo_open not availaible??
  68. }
  69. /**
  70. * testgetMimeTypeByAlias()
  71. *
  72. * @return void
  73. */
  74. public function testgetMimeTypeByAlias() {
  75. $res = $this->Mime->detectMimeType('http://www.spiegel.de/static/sys/v10/icons/home_v2.png');
  76. $this->assertEquals('image/png', $res);
  77. $res = $this->Mime->detectMimeType('http://www.spiegel.de/static/sys/v10/icons/home_v2_inexistent.png');
  78. $this->assertEquals('', $res);
  79. $res = $this->Mime->detectMimeType(Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.jpg');
  80. $this->assertEquals('image/jpeg', $res);
  81. }
  82. /**
  83. * Test fake files
  84. * @return void
  85. */
  86. public function testEncoding() {
  87. file_put_contents(TMP . 'sometest.txt', 'xyz');
  88. $is = $this->Mime->getEncoding(TMP . 'sometest.txt');
  89. //pr($is);
  90. $this->assertEquals($is, 'us-ascii');
  91. file_put_contents(TMP . 'sometest.zip', utf8_encode('xäääyz'));
  92. $is = $this->Mime->getEncoding(TMP . 'sometest.zip');
  93. //pr($is);
  94. $this->assertEquals($is, 'utf-8');
  95. file_put_contents(TMP . 'sometest.zip', utf8_encode('xyz'));
  96. $is = $this->Mime->getEncoding(TMP . 'sometest.zip');
  97. //pr($is);
  98. $this->assertEquals($is, 'us-ascii');
  99. //Tests fail? finfo_open not availaible??
  100. }
  101. /**
  102. * MimeLibTest::testDifferenceBetweenPluginAndCore()
  103. *
  104. * @return void
  105. */
  106. public function testDifferenceBetweenPluginAndCore() {
  107. $this->TestCakeResponse = new TestCakeResponse();
  108. $this->TestMime = new TestMime();
  109. $core = $this->TestCakeResponse->getMimeTypes();
  110. $plugin = $this->TestMime->getMimeTypes();
  111. $diff = [
  112. 'coreonly' => [],
  113. 'pluginonly' => [],
  114. 'modified' => []
  115. ];
  116. foreach ($core as $key => $value) {
  117. if (!isset($plugin[$key])) {
  118. $diff['coreonly'][$key] = $value;
  119. } elseif ($value !== $plugin[$key]) {
  120. $diff['modified'][$key] = ['was' => $value, 'is' => $plugin[$key]];
  121. }
  122. unset($plugin[$key]);
  123. }
  124. foreach ($plugin as $key => $value) {
  125. $diff['pluginonly'][$key] = $value;
  126. }
  127. //$this->debug($diff);
  128. }
  129. }
  130. class TestCakeResponse extends Response {
  131. public function getMimeTypes() {
  132. return $this->_mimeTypes;
  133. }
  134. }
  135. class TestMime extends Mime {
  136. public function getMimeTypes($coreHasPrecedence = false) {
  137. return $this->_mimeTypesExt;
  138. }
  139. }