MimeTest.php 3.5 KB

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