Return empty template for default creators

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2019-12-16 14:58:56 +01:00
parent 29f6f15cf3
commit de5384466c
No known key found for this signature in database
GPG Key ID: 4C614C6ED2CDE6DF
2 changed files with 34 additions and 4 deletions

View File

@ -40,7 +40,9 @@ use OCP\Files\IRootFolder;
use OCP\Files\Node; use OCP\Files\Node;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\IDBConnection; use OCP\IDBConnection;
use OCP\IL10N;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\L10N\IFactory;
use OCP\Security\ISecureRandom; use OCP\Security\ISecureRandom;
use OCP\Share\IShare; use OCP\Share\IShare;
@ -61,17 +63,21 @@ class Manager implements IManager {
private $random; private $random;
private $userId; private $userId;
private $rootFolder; private $rootFolder;
/** @var IL10N */
private $l10n;
public function __construct( public function __construct(
ISecureRandom $random, ISecureRandom $random,
IDBConnection $connection, IDBConnection $connection,
IUserSession $userSession, IUserSession $userSession,
IRootFolder $rootFolder IRootFolder $rootFolder,
IFactory $l10nFactory
) { ) {
$this->random = $random; $this->random = $random;
$this->connection = $connection; $this->connection = $connection;
$this->userId = $userSession->getUser() ? $userSession->getUser()->getUID() : null; $this->userId = $userSession->getUser() ? $userSession->getUser()->getUID() : null;
$this->rootFolder = $rootFolder; $this->rootFolder = $rootFolder;
$this->l10n = $l10nFactory->get('core');
} }
public function registerDirectEditor(IEditor $directEditor): void { public function registerDirectEditor(IEditor $directEditor): void {
@ -88,9 +94,25 @@ class Manager implements IManager {
} }
$templates = []; $templates = [];
foreach ($this->editors[$editor]->getCreators() as $creator) { foreach ($this->editors[$editor]->getCreators() as $creator) {
if ($creator instanceof ACreateFromTemplate && $creator->getId() === $type) { if ($creator->getId() === $type) {
$templates = [
'empty' => [
'id' => 'empty',
'title' => $this->l10n->t('Empty file'),
'preview' => null
]
];
if ($creator instanceof ACreateFromTemplate) {
$templates = $creator->getTemplates(); $templates = $creator->getTemplates();
} }
$templates = array_map(function ($template) use ($creator) {
$template['extension'] = $creator->getExtension();
$template['mimetype'] = $creator->getMimetype();
return $template;
}, $templates);
}
} }
$return = []; $return = [];
$return['templates'] = $templates; $return['templates'] = $templates;

View File

@ -13,7 +13,9 @@ use OCP\DirectEditing\IToken;
use OCP\Files\Folder; use OCP\Files\Folder;
use OCP\Files\IRootFolder; use OCP\Files\IRootFolder;
use OCP\IDBConnection; use OCP\IDBConnection;
use OCP\IL10N;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\L10N\IFactory;
use OCP\Security\ISecureRandom; use OCP\Security\ISecureRandom;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase; use Test\TestCase;
@ -116,6 +118,12 @@ class ManagerTest extends TestCase {
$this->userSession = $this->createMock(IUserSession::class); $this->userSession = $this->createMock(IUserSession::class);
$this->rootFolder = $this->createMock(IRootFolder::class); $this->rootFolder = $this->createMock(IRootFolder::class);
$this->userFolder = $this->createMock(Folder::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()) $this->rootFolder->expects($this->any())
@ -123,7 +131,7 @@ class ManagerTest extends TestCase {
->willReturn($this->userFolder); ->willReturn($this->userFolder);
$this->manager = new Manager( $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); $this->manager->registerDirectEditor($this->editor);