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
5 changes: 1 addition & 4 deletions composer/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
echo $err;
}
}
trigger_error(
$err,
E_USER_ERROR
);
throw new RuntimeException($err);
}

require_once __DIR__ . '/composer/autoload_real.php';
Expand Down
20 changes: 19 additions & 1 deletion composer/composer/InstalledVersions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}|array{}|null
Expand Down Expand Up @@ -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<array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}>
Expand All @@ -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])) {
Expand Down
11 changes: 9 additions & 2 deletions lib/DirectEditing/TextDirectEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -109,7 +116,7 @@ public function getMimetypesOptional(): array {
*/
public function getCreators(): array {
return [
new TextDocumentCreator($this->l10n),
new TextDocumentCreator($this->l10n, $this->appConfig),
];
}

Expand Down
19 changes: 16 additions & 3 deletions lib/DirectEditing/TextDocumentCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace OCA\Text\DirectEditing;

use OCP\DirectEditing\ACreateEmpty;
use OCP\IAppConfig;
use OCP\IL10N;

class TextDocumentCreator extends ACreateEmpty {
Expand All @@ -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 {
Expand All @@ -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';
}
}
}
68 changes: 68 additions & 0 deletions tests/unit/DirectEditing/TextDocumentCreatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/**
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Text\Tests;

use OCA\Text\DirectEditing\TextDocumentCreator;
use OCP\IAppConfig;
use OCP\IL10N;

class TextDocumentCreatorTest extends \PHPUnit\Framework\TestCase {
private TextDocumentCreator $textDocumentCreator;

private IL10N $l10n;

private IAppConfig $appConfig;

protected function setUp(): void {
$this->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());
}
}
Loading