MimeTest.php 4.2 KB

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