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
12 changes: 9 additions & 3 deletions apps/dav/lib/Service/ExampleContactService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
use OCA\DAV\CardDAV\CardDavBackend;
use OCP\AppFramework\Services\IAppConfig;
use OCP\Files\AppData\IAppDataFactory;
use OCP\Files\GenericFileException;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IL10N;
use OCP\Lock\LockedException;
use Psr\Log\LoggerInterface;
use Symfony\Component\Uid\Uuid;

Expand Down Expand Up @@ -47,11 +50,14 @@ public function getCard(): ?string {
return null;
}

if (!$folder->fileExists('defaultContact.vcf')) {
try {
return $folder->getFile('defaultContact.vcf')->getContent();
} catch (NotFoundException $e) {
return null;
} catch (GenericFileException|NotPermittedException|LockedException $e) {
$this->logger->error('Could not read default contact file', ['exception' => $e]);
return null;
}

return $folder->getFile('defaultContact.vcf')->getContent();
}

private function createInitialDefaultContact(): void {
Expand Down
3 changes: 2 additions & 1 deletion apps/dav/lib/Service/ExampleEventService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OCA\DAV\Exception\ExampleEventException;
use OCA\DAV\Model\ExampleEvent;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\GenericFileException;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
Expand Down Expand Up @@ -105,7 +106,7 @@ private function getCustomExampleEvent(): ?string {

try {
return $icsFile->getContent();
} catch (NotFoundException|NotPermittedException $e) {
} catch (NotFoundException|NotPermittedException|GenericFileException $e) {
throw new ExampleEventException(
'Failed to read custom example event',
0,
Expand Down
39 changes: 37 additions & 2 deletions apps/dav/tests/unit/Service/ExampleContactServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

use OCA\DAV\CardDAV\CardDavBackend;
use OCA\DAV\Service\ExampleContactService;
use OCP\App\IAppManager;
use OCP\AppFramework\Services\IAppConfig;
use OCP\Files\AppData\IAppDataFactory;
use OCP\Files\GenericFileException;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile;
Expand All @@ -27,7 +27,6 @@
class ExampleContactServiceTest extends TestCase {
protected ExampleContactService $service;
protected CardDavBackend&MockObject $cardDav;
protected IAppManager&MockObject $appManager;
protected IAppDataFactory&MockObject $appDataFactory;
protected LoggerInterface&MockObject $logger;
protected IAppConfig&MockObject $appConfig;
Expand Down Expand Up @@ -161,6 +160,42 @@ public function testUidAndRevAreAddedIfMissing(): void {
$this->assertTrue(Uuid::isValid($vcard->UID->getValue()));
}

public function testGetCardReturnsNullWhenFolderNotFound(): void {
$this->appData->method('getFolder')->willThrowException(new NotFoundException());
$this->assertNull($this->service->getCard());
}

public function testGetCardReturnsNullWhenFileNotFound(): void {
$folder = $this->createMock(ISimpleFolder::class);
$this->appData->method('getFolder')->willReturn($folder);
$folder->method('getFile')->willThrowException(new NotFoundException());
$this->assertNull($this->service->getCard());
}

public function testGetCardReturnsNullOnReadError(): void {
$folder = $this->createMock(ISimpleFolder::class);
$file = $this->createMock(ISimpleFile::class);
$this->appData->method('getFolder')->willReturn($folder);
$folder->method('getFile')->willReturn($file);
$file->method('getContent')->willThrowException(new GenericFileException());

$this->logger->expects($this->once())
->method('error')
->with('Could not read default contact file', $this->anything());

$this->assertNull($this->service->getCard());
}

public function testGetCardReturnsContent(): void {
$folder = $this->createMock(ISimpleFolder::class);
$file = $this->createMock(ISimpleFile::class);
$this->appData->method('getFolder')->willReturn($folder);
$folder->method('getFile')->willReturn($file);
$file->method('getContent')->willReturn('vcarddata');

$this->assertEquals('vcarddata', $this->service->getCard());
}

public function testDefaultContactIsNotCreatedIfEnabled(): void {
$this->appConfig->method('getAppValueBool')
->with('enableDefaultContact', true)
Expand Down
25 changes: 25 additions & 0 deletions apps/dav/tests/unit/Service/ExampleEventServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
namespace OCA\DAV\Tests\unit\Service;

use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\Exception\ExampleEventException;
use OCA\DAV\Service\ExampleEventService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\GenericFileException;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile;
Expand Down Expand Up @@ -173,6 +175,29 @@ public function testGetExampleEventWithCustomEvent($customEventIcs): void {
$this->assertEquals($expectedIcs, $actualIcs);
}

public function testGetExampleEventThrowsOnReadError(): void {
$exampleEventFolder = $this->createMock(ISimpleFolder::class);
$this->appData->expects(self::once())
->method('getFolder')
->with('example_event')
->willReturn($exampleEventFolder);
$exampleEventFile = $this->createMock(ISimpleFile::class);
$exampleEventFolder->expects(self::once())
->method('getFile')
->with('example_event.ics')
->willReturn($exampleEventFile);
$exampleEventFile->expects(self::once())
->method('getContent')
->willThrowException(new GenericFileException());

$this->random->expects(self::once())
->method('generate')
->willReturn('RANDOM-UID');

$this->expectException(ExampleEventException::class);
$this->service->getExampleEvent();
}

public function testGetExampleEventWithDefault(): void {
$this->appData->expects(self::once())
->method('getFolder')
Expand Down
Loading