diff --git a/composer/autoload.php b/composer/autoload.php index 252d6688664..00a30bcd1ae 100644 --- a/composer/autoload.php +++ b/composer/autoload.php @@ -14,10 +14,7 @@ echo $err; } } - trigger_error( - $err, - E_USER_ERROR - ); + throw new RuntimeException($err); } require_once __DIR__ . '/composer/autoload_real.php'; diff --git a/composer/composer/InstalledVersions.php b/composer/composer/InstalledVersions.php index 6d29bff66aa..2052022fd8e 100644 --- a/composer/composer/InstalledVersions.php +++ b/composer/composer/InstalledVersions.php @@ -26,6 +26,12 @@ */ class InstalledVersions { + /** + * @var string|null if set (by reflection by Composer), this should be set to the path where this class is being copied to + * @internal + */ + private static $selfDir = null; + /** * @var mixed[]|null * @psalm-var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array}|array{}|null @@ -322,6 +328,18 @@ public static function reload($data) self::$installedIsLocalDir = false; } + /** + * @return string + */ + private static function getSelfDir() + { + if (self::$selfDir === null) { + self::$selfDir = strtr(__DIR__, '\\', '/'); + } + + return self::$selfDir; + } + /** * @return array[] * @psalm-return list}> @@ -336,7 +354,7 @@ private static function getInstalled() $copiedLocalDir = false; if (self::$canGetVendors) { - $selfDir = strtr(__DIR__, '\\', '/'); + $selfDir = self::getSelfDir(); foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) { $vendorDir = strtr($vendorDir, '\\', '/'); if (isset(self::$installedByVendor[$vendorDir])) { diff --git a/lib/DirectEditing/TextDirectEditor.php b/lib/DirectEditing/TextDirectEditor.php index 01276d14bec..ac1a7b9c578 100644 --- a/lib/DirectEditing/TextDirectEditor.php +++ b/lib/DirectEditing/TextDirectEditor.php @@ -17,6 +17,7 @@ use OCP\Files\InvalidPathException; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; +use OCP\IAppConfig; use OCP\IL10N; use OCP\Util; @@ -31,10 +32,16 @@ class TextDirectEditor implements IEditor { /** @var ApiService */ private $apiService; - public function __construct(IL10N $l10n, InitialStateProvider $initialStateProvider, ApiService $apiService) { + /** + * @var IAppConfig + */ + private $appConfig; + + public function __construct(IL10N $l10n, InitialStateProvider $initialStateProvider, ApiService $apiService, IAppConfig $appConfig) { $this->l10n = $l10n; $this->initialStateProvider = $initialStateProvider; $this->apiService = $apiService; + $this->appConfig = $appConfig; } /** @@ -109,7 +116,7 @@ public function getMimetypesOptional(): array { */ public function getCreators(): array { return [ - new TextDocumentCreator($this->l10n), + new TextDocumentCreator($this->l10n, $this->appConfig), ]; } diff --git a/lib/DirectEditing/TextDocumentCreator.php b/lib/DirectEditing/TextDocumentCreator.php index 56cc5019d92..c35eb428a6a 100644 --- a/lib/DirectEditing/TextDocumentCreator.php +++ b/lib/DirectEditing/TextDocumentCreator.php @@ -7,6 +7,7 @@ namespace OCA\Text\DirectEditing; use OCP\DirectEditing\ACreateEmpty; +use OCP\IAppConfig; use OCP\IL10N; class TextDocumentCreator extends ACreateEmpty { @@ -17,8 +18,14 @@ class TextDocumentCreator extends ACreateEmpty { */ private $l10n; - public function __construct(IL10N $l10n) { + /** + * @var IAppConfig + */ + private $appConfig; + + public function __construct(IL10N $l10n, IAppConfig $appConfig) { $this->l10n = $l10n; + $this->appConfig = $appConfig; } public function getId(): string { @@ -30,10 +37,16 @@ public function getName(): string { } public function getExtension(): string { - return 'md'; + return $this->appConfig->getValueString('text', 'default_file_extension', 'md'); } public function getMimetype(): string { - return 'text/markdown'; + switch ($this->getExtension()) { + case 'txt': + return 'text/plain'; + case 'md': + default: + return 'text/markdown'; + } } } diff --git a/tests/unit/DirectEditing/TextDocumentCreatorTest.php b/tests/unit/DirectEditing/TextDocumentCreatorTest.php new file mode 100644 index 00000000000..a19acf96aa5 --- /dev/null +++ b/tests/unit/DirectEditing/TextDocumentCreatorTest.php @@ -0,0 +1,68 @@ +l10n = $this->createMock(IL10N::class); + $this->appConfig = $this->createMock(IAppConfig::class); + $this->textDocumentCreator = new TextDocumentCreator($this->l10n, $this->appConfig); + } + + public function testGetId(): void { + $this->assertEquals('textdocument', $this->textDocumentCreator->getId()); + } + + public function testGetName(): void { + $this->l10n->expects($this->once()) + ->method('t') + ->with('text document') + ->willReturn('text document'); + $this->assertEquals('text document', $this->textDocumentCreator->getName()); + } + + public function testGetDefaultExtension(): void { + $this->appConfig->expects($this->once()) + ->method('getValueString') + ->with('text', 'default_file_extension', 'md') + ->willReturn('md'); + $this->assertEquals('md', $this->textDocumentCreator->getExtension()); + } + + public function testGetExtensionFromConfig(): void { + $this->appConfig->expects($this->once()) + ->method('getValueString') + ->with('text', 'default_file_extension', 'md') + ->willReturn('txt'); + + $this->assertEquals('txt', $this->textDocumentCreator->getExtension()); + } + + public function testGetDefaultMimetype(): void { + $this->assertEquals('text/markdown', $this->textDocumentCreator->getMimetype()); + } + + public function testGetMimetypeFromConfig(): void { + $this->appConfig->expects($this->once()) + ->method('getValueString') + ->with('text', 'default_file_extension', 'md') + ->willReturn('txt'); + + $this->assertEquals('text/plain', $this->textDocumentCreator->getMimetype()); + } +}