MimeTest.php 4.2 KB

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