| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace Tools\Test\TestCase\Utility;
- use Cake\Core\Plugin;
- use Shim\TestSuite\TestCase;
- use TestApp\Http\TestResponse;
- use TestApp\Utility\TestMime;
- class MimeTest extends TestCase {
- /**
- * @var \Tools\Utility\Mime
- */
- protected $Mime;
- /**
- * @return void
- */
- public function setUp(): void {
- parent::setUp();
- $this->Mime = new TestMime();
- }
- /**
- * @return void
- */
- public function testObject() {
- $this->assertTrue(is_object($this->Mime));
- $this->assertInstanceOf('Tools\Utility\Mime', $this->Mime);
- }
- /**
- * @return void
- */
- public function testAll() {
- $res = $this->Mime->mimeTypes();
- $this->assertTrue(is_array($res) && count($res) > 100);
- }
- /**
- * @return void
- */
- public function testSingle() {
- $res = $this->Mime->getMimeTypeByAlias('odxs');
- $this->assertNull($res);
- $res = $this->Mime->getMimeTypeByAlias('ods');
- $this->assertEquals('application/vnd.oasis.opendocument.spreadsheet', $res);
- }
- /**
- * @return void
- */
- public function testOverwrite() {
- $res = $this->Mime->getMimeTypeByAlias('ics');
- $this->assertEquals('application/ics', $res);
- }
- /**
- * @return void
- */
- public function testReverseToSingle() {
- $res = $this->Mime->getMimeTypeByAlias('html');
- $this->assertEquals('text/html', $res);
- $res = $this->Mime->getMimeTypeByAlias('csv');
- $this->assertEquals('text/csv', $res);
- }
- /**
- * @return void
- */
- public function testReverseToMultiple() {
- $res = $this->Mime->getMimeTypeByAlias('html', false);
- $this->assertTrue(is_array($res));
- $this->assertSame(2, count($res));
- $res = $this->Mime->getMimeTypeByAlias('csv', false);
- $this->assertTrue(is_array($res)); // && count($res) > 2
- $this->assertSame(2, count($res));
- }
- /**
- * @return void
- */
- public function testCorrectFileExtension() {
- file_put_contents(TMP . 'sometest.txt', 'xyz');
- $is = $this->Mime->detectMimeType(TMP . 'sometest.txt');
- //pr($is);
- $this->assertEquals($is, 'text/plain');
- }
- /**
- * @return void
- */
- public function testWrongFileExtension() {
- file_put_contents(TMP . 'sometest.zip', 'xyz');
- $is = $this->Mime->detectMimeType(TMP . 'sometest.zip');
- //pr($is);
- $this->assertEquals($is, 'text/plain');
- //Test failes? finfo_open not availaible??
- }
- /**
- * @return void
- */
- public function testgetMimeTypeByAlias() {
- $res = $this->Mime->detectMimeType('https://raw.githubusercontent.com/dereuromark/cakephp-ide-helper/master/docs/img/code_completion.png');
- $this->assertEquals('image/png', $res);
- $res = $this->Mime->detectMimeType('https://raw.githubusercontent.com/dereuromark/cakephp-ide-helper/master/docs/img/code_completion_invalid.png');
- $this->assertEquals('', $res);
- $res = $this->Mime->detectMimeType(Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.jpg');
- $this->assertEquals('image/jpeg', $res);
- }
- /**
- * @return void
- */
- public function testEncoding() {
- file_put_contents(TMP . 'sometest.txt', 'xyz');
- $is = $this->Mime->getEncoding(TMP . 'sometest.txt');
- //pr($is);
- $this->assertEquals($is, 'us-ascii');
- file_put_contents(TMP . 'sometest.zip', mb_convert_encoding('xäääyz', 'UTF-8'));
- $is = $this->Mime->getEncoding(TMP . 'sometest.zip');
- //pr($is);
- $this->assertEquals($is, 'utf-8');
- file_put_contents(TMP . 'sometest.zip', mb_convert_encoding('xyz', 'UTF-8'));
- $is = $this->Mime->getEncoding(TMP . 'sometest.zip');
- //pr($is);
- $this->assertEquals($is, 'us-ascii');
- //Tests fail? finfo_open not availaible??
- }
- /**
- * @return void
- */
- public function testDifferenceBetweenPluginAndCore() {
- $TestCakeResponse = new TestResponse();
- $TestMime = new TestMime();
- $core = $TestCakeResponse->getMimeTypes();
- $plugin = $TestMime->getMimeTypes();
- $diff = [
- 'coreonly' => [],
- 'pluginonly' => [],
- 'modified' => [],
- ];
- foreach ($core as $key => $value) {
- if (!isset($plugin[$key])) {
- $diff['coreonly'][$key] = $value;
- } elseif ($value !== $plugin[$key]) {
- $diff['modified'][$key] = ['was' => $value, 'is' => $plugin[$key]];
- }
- unset($plugin[$key]);
- }
- foreach ($plugin as $key => $value) {
- $diff['pluginonly'][$key] = $value;
- }
- $this->assertNotEmpty($diff['coreonly']);
- $this->assertNotEmpty($diff['pluginonly']);
- $this->assertNotEmpty($diff['modified']);
- }
- }
|