Skip to content

Commit 44520e8

Browse files
fix: default extension in direct editing
Signed-off-by: Luka Trovic <luka@nextcloud.com>
1 parent c527cb0 commit 44520e8

3 files changed

Lines changed: 96 additions & 5 deletions

File tree

lib/DirectEditing/TextDirectEditor.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use OCP\Files\InvalidPathException;
1818
use OCP\Files\NotFoundException;
1919
use OCP\Files\NotPermittedException;
20+
use OCP\IAppConfig;
2021
use OCP\IL10N;
2122
use OCP\Util;
2223

@@ -31,10 +32,16 @@ class TextDirectEditor implements IEditor {
3132
/** @var ApiService */
3233
private $apiService;
3334

34-
public function __construct(IL10N $l10n, InitialStateProvider $initialStateProvider, ApiService $apiService) {
35+
/**
36+
* @var IAppConfig
37+
*/
38+
private $appConfig;
39+
40+
public function __construct(IL10N $l10n, InitialStateProvider $initialStateProvider, ApiService $apiService, IAppConfig $appConfig) {
3541
$this->l10n = $l10n;
3642
$this->initialStateProvider = $initialStateProvider;
3743
$this->apiService = $apiService;
44+
$this->appConfig = $appConfig;
3845
}
3946

4047
/**
@@ -109,7 +116,7 @@ public function getMimetypesOptional(): array {
109116
*/
110117
public function getCreators(): array {
111118
return [
112-
new TextDocumentCreator($this->l10n),
119+
new TextDocumentCreator($this->l10n, $this->appConfig),
113120
];
114121
}
115122

lib/DirectEditing/TextDocumentCreator.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace OCA\Text\DirectEditing;
88

99
use OCP\DirectEditing\ACreateEmpty;
10+
use OCP\IAppConfig;
1011
use OCP\IL10N;
1112

1213
class TextDocumentCreator extends ACreateEmpty {
@@ -17,8 +18,14 @@ class TextDocumentCreator extends ACreateEmpty {
1718
*/
1819
private $l10n;
1920

20-
public function __construct(IL10N $l10n) {
21+
/**
22+
* @var IAppConfig
23+
*/
24+
private $appConfig;
25+
26+
public function __construct(IL10N $l10n, IAppConfig $appConfig) {
2127
$this->l10n = $l10n;
28+
$this->appConfig = $appConfig;
2229
}
2330

2431
public function getId(): string {
@@ -30,10 +37,16 @@ public function getName(): string {
3037
}
3138

3239
public function getExtension(): string {
33-
return 'md';
40+
return $this->appConfig->getValueString('text', 'default_file_extension', 'md');
3441
}
3542

3643
public function getMimetype(): string {
37-
return 'text/markdown';
44+
switch ($this->getExtension()) {
45+
case 'txt':
46+
return 'text/plain';
47+
case 'md':
48+
default:
49+
return 'text/markdown';
50+
}
3851
}
3952
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace OCA\Text\Tests;
4+
5+
use OCA\Text\DirectEditing\TextDocumentCreator;
6+
use OCP\IAppConfig;
7+
use OCP\IL10N;
8+
9+
class TextDocumentCreatorTest extends \PHPUnit\Framework\TestCase
10+
{
11+
private TextDocumentCreator $textDocumentCreator;
12+
13+
private IL10N $l10n;
14+
15+
private IAppConfig $appConfig;
16+
17+
protected function setUp(): void
18+
{
19+
$this->l10n = $this->createMock(IL10N::class);
20+
$this->appConfig = $this->createMock(IAppConfig::class);
21+
$this->textDocumentCreator = new TextDocumentCreator($this->l10n, $this->appConfig);
22+
}
23+
24+
public function testGetId(): void
25+
{
26+
$this->assertEquals('textdocument', $this->textDocumentCreator->getId());
27+
}
28+
29+
public function testGetName(): void
30+
{
31+
$this->l10n->expects($this->once())
32+
->method('t')
33+
->with('text document')
34+
->willReturn('text document');
35+
$this->assertEquals('text document', $this->textDocumentCreator->getName());
36+
}
37+
38+
public function testGetDefaultExtension(): void
39+
{
40+
$this->appConfig->expects($this->once())
41+
->method('getValueString')
42+
->with('text', 'default_file_extension', 'md')
43+
->willReturn('md');
44+
$this->assertEquals('md', $this->textDocumentCreator->getExtension());
45+
}
46+
47+
public function testGetExtensionFromConfig(): void
48+
{
49+
$this->appConfig->expects($this->once())
50+
->method('getValueString')
51+
->with('text', 'default_file_extension', 'md')
52+
->willReturn('txt');
53+
54+
$this->assertEquals('txt', $this->textDocumentCreator->getExtension());
55+
}
56+
57+
public function testGetDefaultMimetype(): void
58+
{
59+
$this->assertEquals('text/markdown', $this->textDocumentCreator->getMimetype());
60+
}
61+
62+
public function testGetMimetypeFromConfig(): void
63+
{
64+
$this->appConfig->expects($this->once())
65+
->method('getValueString')
66+
->with('text', 'default_file_extension', 'md')
67+
->willReturn('txt');
68+
69+
$this->assertEquals('text/plain', $this->textDocumentCreator->getMimetype());
70+
}
71+
}

0 commit comments

Comments
 (0)