From de5384466c4236181d21810142e3f2773bdeefe7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Julius=20H=C3=A4rtl?= Date: Mon, 16 Dec 2019 14:58:56 +0100 Subject: [PATCH] Return empty template for default creators MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/private/DirectEditing/Manager.php | 28 ++++++++++++++++++++++--- tests/lib/DirectEditing/ManagerTest.php | 10 ++++++++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/lib/private/DirectEditing/Manager.php b/lib/private/DirectEditing/Manager.php index fcaaf9e0303..a739402d629 100644 --- a/lib/private/DirectEditing/Manager.php +++ b/lib/private/DirectEditing/Manager.php @@ -40,7 +40,9 @@ use OCP\Files\IRootFolder; use OCP\Files\Node; use OCP\Files\NotFoundException; use OCP\IDBConnection; +use OCP\IL10N; use OCP\IUserSession; +use OCP\L10N\IFactory; use OCP\Security\ISecureRandom; use OCP\Share\IShare; @@ -61,17 +63,21 @@ class Manager implements IManager { private $random; private $userId; private $rootFolder; + /** @var IL10N */ + private $l10n; public function __construct( ISecureRandom $random, IDBConnection $connection, IUserSession $userSession, - IRootFolder $rootFolder + IRootFolder $rootFolder, + IFactory $l10nFactory ) { $this->random = $random; $this->connection = $connection; $this->userId = $userSession->getUser() ? $userSession->getUser()->getUID() : null; $this->rootFolder = $rootFolder; + $this->l10n = $l10nFactory->get('core'); } public function registerDirectEditor(IEditor $directEditor): void { @@ -88,8 +94,24 @@ class Manager implements IManager { } $templates = []; foreach ($this->editors[$editor]->getCreators() as $creator) { - if ($creator instanceof ACreateFromTemplate && $creator->getId() === $type) { - $templates = $creator->getTemplates(); + if ($creator->getId() === $type) { + $templates = [ + 'empty' => [ + 'id' => 'empty', + 'title' => $this->l10n->t('Empty file'), + 'preview' => null + ] + ]; + + if ($creator instanceof ACreateFromTemplate) { + $templates = $creator->getTemplates(); + } + + $templates = array_map(function ($template) use ($creator) { + $template['extension'] = $creator->getExtension(); + $template['mimetype'] = $creator->getMimetype(); + return $template; + }, $templates); } } $return = []; diff --git a/tests/lib/DirectEditing/ManagerTest.php b/tests/lib/DirectEditing/ManagerTest.php index 52a18584d6e..a97c02f19d2 100644 --- a/tests/lib/DirectEditing/ManagerTest.php +++ b/tests/lib/DirectEditing/ManagerTest.php @@ -13,7 +13,9 @@ use OCP\DirectEditing\IToken; use OCP\Files\Folder; use OCP\Files\IRootFolder; use OCP\IDBConnection; +use OCP\IL10N; use OCP\IUserSession; +use OCP\L10N\IFactory; use OCP\Security\ISecureRandom; use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; @@ -116,6 +118,12 @@ class ManagerTest extends TestCase { $this->userSession = $this->createMock(IUserSession::class); $this->rootFolder = $this->createMock(IRootFolder::class); $this->userFolder = $this->createMock(Folder::class); + $this->l10n = $this->createMock(IL10N::class); + + $l10nFactory = $this->createMock(IFactory::class); + $l10nFactory->expects($this->once()) + ->method('get') + ->willReturn($this->l10n); $this->rootFolder->expects($this->any()) @@ -123,7 +131,7 @@ class ManagerTest extends TestCase { ->willReturn($this->userFolder); $this->manager = new Manager( - $this->random, $this->connection, $this->userSession, $this->rootFolder + $this->random, $this->connection, $this->userSession, $this->rootFolder, $l10nFactory ); $this->manager->registerDirectEditor($this->editor); -- 2.39.5