MimeTest.php 4.1 KB

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