Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Return empty template for default creators
Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr committed Dec 16, 2019
commit de5384466c4236181d21810142e3f2773bdeefe7
28 changes: 25 additions & 3 deletions lib/private/DirectEditing/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
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;

Expand All @@ -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 {
Expand All @@ -88,8 +94,24 @@ public function getTemplates(string $editor, string $type): array {
}
$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 = [];
Expand Down
10 changes: 9 additions & 1 deletion tests/lib/DirectEditing/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
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;
Expand Down Expand Up @@ -116,14 +118,20 @@ protected function setUp(): void {
$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())
->method('getUserFolder')
->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);
Expand Down