diff --git a/cypress/integration/viewer.spec.js b/cypress/integration/viewer.spec.js index 9dc820b9ebe..e1d8172825b 100644 --- a/cypress/integration/viewer.spec.js +++ b/cypress/integration/viewer.spec.js @@ -32,6 +32,7 @@ describe('Open test.md in viewer', function() { // Upload test files cy.uploadFile('test.md', 'text/markdown') + cy.uploadFile('empty.md', 'text/markdown') }) beforeEach(function() { @@ -68,6 +69,30 @@ describe('Open test.md in viewer', function() { cy.screenshot() }) + it('Open an empty file', function() { + cy.openFile('empty.md') + + cy.log('Inspect viewer') + const viewer = cy.get('#viewer') + viewer.should('be.visible') + .and('have.class', 'modal-mask') + .and('not.have.class', 'icon-loading') + viewer.get('.modal-title').should('contain', 'empty.md') + viewer.get('.modal-header button.action-item__menutoggle') + .should('be.visible') + + cy.log('Inspect editor') + const editor = viewer.get('#editor .ProseMirror') + editor.should('contain', '') + + cy.log('Inspect menubar') + const menubar = editor.get('.menubar .menubar-icons') + menubar.get('.icon-undo').should('be.visible') + menubar.get('.icon-bold').should('be.visible') + + cy.screenshot() + }) + it('Closes the editor', function() { cy.openFile('test.md') cy.get('#viewer .modal-header button.header-close').click() diff --git a/lib/Service/ApiService.php b/lib/Service/ApiService.php index 5ed1b3e7924..651b1eefc09 100644 --- a/lib/Service/ApiService.php +++ b/lib/Service/ApiService.php @@ -111,7 +111,7 @@ public function create($fileId = null, $filePath = null, $token = null, $guestNa $content = $baseFile->getContent(); $content = $this->encodingService->encodeToUtf8($content); - if (!$content) { + if ($content === null) { $this->logger->log(ILogger::WARN, 'Failed to encode file to UTF8. File ID: ' . $file->getId()); } } catch (NotFoundException $e) { diff --git a/lib/Service/EncodingService.php b/lib/Service/EncodingService.php index 3f5bada0625..5026cd9a33d 100644 --- a/lib/Service/EncodingService.php +++ b/lib/Service/EncodingService.php @@ -42,7 +42,8 @@ public function encodeToUtf8(string $string): ?string { return null; } - return mb_convert_encoding($string, 'UTF-8', $encoding); + $encoded = mb_convert_encoding($string, 'UTF-8', $encoding); + return is_string($encoded) ? $encoded : null; } public function detectEncoding(string $string): ?string { diff --git a/lib/TextFile.php b/lib/TextFile.php index 30b58c84c80..9d632dcfef3 100644 --- a/lib/TextFile.php +++ b/lib/TextFile.php @@ -63,7 +63,7 @@ public function getMTime() { public function getContent() { $content = $this->encodingService->encodeToUtf8($this->file->getContent()); - if (!$content) { + if ($content === null) { throw new NotFoundException('File not compatible with text because it could not be encoded to UTF-8.'); }