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
18 changes: 7 additions & 11 deletions lib/Service/WorkspaceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@

namespace OCA\Text\Service;

use OCP\Files\Cache\ICacheEntry;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\NotFoundException;
use OCP\Files\StorageInvalidException;
use OCP\IL10N;

Expand All @@ -39,17 +37,15 @@ public function getFile(Folder $folder): ?File {
return null;
}

$content = $cache->getFolderContents($internalPath . '/', 'text/markdown');
$namesFound = array_flip(array_map(static fn ($entry) => $entry->getName(), $content));

foreach ($this->getSupportedFilenames() as $filename) {
try {
$cacheEntry = $cache->get($internalPath . '/' . $filename);
if ($cacheEntry !== false && $cacheEntry->getMimeType() !== ICacheEntry::DIRECTORY_MIMETYPE) {
$file = $folder->get($filename);
if ($file instanceof File) {
return $file;
}
if (isset($namesFound[$filename])) {
$file = $folder->get($filename);
if ($file instanceof File) {
return $file;
}
} catch (NotFoundException) {
continue;
}
}
return null;
Expand Down
57 changes: 30 additions & 27 deletions tests/unit/Service/WorkspaceServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,35 @@ protected function setUp(): void {
$this->workspaceService = new WorkspaceService($this->l10n);
}

public function testGetFileReturnsFirstMatchingFileInPriorityOrder(): void {
private function mockFolder(string $internalPath, array $entryNames): Folder&MockObject {
$folder = $this->createMock(Folder::class);
$storage = $this->createMock(IStorage::class);
$cache = $this->createMock(ICache::class);
$readmeFile = $this->createMock(File::class);

$folder->method('getStorage')->willReturn($storage);
$storage->method('getCache')->willReturn($cache);
$folder->method('getInternalPath')->willReturn('docs');

$readmeEntry = $this->createMock(ICacheEntry::class);
$readmeEntry->method('getMimeType')->willReturn('text/markdown');
$entries = array_map(function (string $name) {
$entry = $this->createMock(ICacheEntry::class);
$entry->method('getName')->willReturn($name);
return $entry;
}, $entryNames);

$uppercaseEntry = $this->createMock(ICacheEntry::class);
$uppercaseEntry->method('getMimeType')->willReturn('text/markdown');
$cache->expects($this->once())
->method('getFolderContents')
->with($internalPath . '/', 'text/markdown')
->willReturn($entries);

$cache->method('get')->willReturnMap([
['docs/Readme.md', $readmeEntry],
['docs/README.md', $uppercaseEntry],
['docs/readme.md', false],
['docs/.Readme.md', false],
]);
return $folder;
}

public function testGetFileReturnsFirstMatchingFileInPriorityOrder(): void {
$readmeFile = $this->createMock(File::class);

// Cache order deliberately does not match priority order :README.md
// comes back before Readme.md, but Readme.md must still win.
$folder = $this->mockFolder('docs', ['README.md', 'Readme.md']);

$folder->expects($this->once())
->method('get')
Expand All @@ -66,24 +73,20 @@ public function testGetFileReturnsFirstMatchingFileInPriorityOrder(): void {
$this->assertSame($readmeFile, $result);
}

public function testGetFileSkipsDirectoryCacheEntries(): void {
$folder = $this->createMock(Folder::class);
$storage = $this->createMock(IStorage::class);
$cache = $this->createMock(ICache::class);
public function testGetFileIgnoresMarkdownFilesWithUnsupportedNames(): void {
// text/markdown files that aren't one of the supported readme names
// (e.g. picked up by the mimetype filter but not a readme) must be skipped.
$folder = $this->mockFolder('docs', ['notes.md', 'CHANGELOG.md']);

$folder->method('getStorage')->willReturn($storage);
$storage->method('getCache')->willReturn($cache);
$folder->method('getInternalPath')->willReturn('docs');
$folder->expects($this->never())->method('get');

$directoryEntry = $this->createMock(ICacheEntry::class);
$directoryEntry->method('getMimeType')->willReturn(ICacheEntry::DIRECTORY_MIMETYPE);
$result = $this->workspaceService->getFile($folder);

$this->assertNull($result);
}

$cache->method('get')->willReturnMap([
['docs/Readme.md', $directoryEntry],
['docs/README.md', false],
['docs/readme.md', false],
['docs/.Readme.md', false],
]);
public function testGetFileReturnsNullWhenNoSupportedFileExists(): void {
$folder = $this->mockFolder('docs', []);

$folder->expects($this->never())->method('get');

Expand Down
Loading