MimeTest.php 4.1 KB

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