Skip to content
Closed
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: 3 additions & 2 deletions apps/dav/lib/Service/DefaultContactService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
namespace OCA\DAV\Service;

use OCA\DAV\CardDAV\CardDavBackend;
use OCP\App\IAppManager;
use OCP\Files\AppData\IAppDataFactory;
use OCP\Files\NotFoundException;
use Psr\Log\LoggerInterface;
use Symfony\Component\Uid\Uuid;

class DefaultContactService {
public function __construct(
private CardDavBackend $cardDav,
private IAppManager $appManager,
private IAppDataFactory $appDataFactory,
private LoggerInterface $logger,
) {
Expand All @@ -30,6 +29,8 @@ public function createDefaultContact(int $addressBookId): void {
$folder = $appData->getFolder('defaultContact');
$defaultContactFile = $folder->getFile('defaultContact.vcf');
$data = $defaultContactFile->getContent();
} catch (NotFoundException $e) {
return;
} catch (\Exception $e) {
$this->logger->error('Couldn\'t get default contact file', ['exception' => $e]);
return;
Expand Down
25 changes: 18 additions & 7 deletions apps/dav/tests/unit/Service/DefaultContactServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use OCA\DAV\CardDAV\CardDavBackend;
use OCA\DAV\Service\DefaultContactService;
use OCP\App\IAppManager;
use OCP\Files\AppData\IAppDataFactory;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
Expand All @@ -25,21 +24,18 @@
class DefaultContactServiceTest extends TestCase {
private DefaultContactService $service;
private MockObject|CardDavBackend $cardDav;
private MockObject|IAppManager $appManager;
private MockObject|IAppDataFactory $appDataFactory;
private MockObject|LoggerInterface $logger;

protected function setUp(): void {
parent::setUp();

$this->cardDav = $this->createMock(CardDavBackend::class);
$this->appManager = $this->createMock(IAppManager::class);
$this->appDataFactory = $this->createMock(IAppDataFactory::class);
$this->logger = $this->createMock(LoggerInterface::class);

$this->service = new DefaultContactService(
$this->cardDav,
$this->appManager,
$this->appDataFactory,
$this->logger
);
Expand Down Expand Up @@ -79,7 +75,7 @@ public function testUidAndRevAreUpdated(): void {
$folder->method('getFile')->willReturn($file);
$appData->method('getFolder')->willReturn($folder);
$this->appDataFactory->method('get')->willReturn($appData);

$capturedCardData = null;
$this->cardDav->expects($this->once())
->method('createCard')
Expand All @@ -92,9 +88,9 @@ public function testUidAndRevAreUpdated(): void {
}),
$this->anything()
)->willReturn(null);

$this->service->createDefaultContact(123);

$vcard = \Sabre\VObject\Reader::read($capturedCardData);
$this->assertNotEquals($originalUid, $vcard->UID->getValue());
$this->assertTrue(Uuid::isValid($vcard->UID->getValue()));
Expand All @@ -106,6 +102,21 @@ public function testDefaultContactFileDoesNotExist(): void {
$appData->method('getFolder')->willThrowException(new NotFoundException());
$this->appDataFactory->method('get')->willReturn($appData);

$this->logger->expects($this->never())
->method('error');
$this->cardDav->expects($this->never())
->method('createCard');

$this->service->createDefaultContact(123);
}

public function testDefaultContactFileException(): void {
$appData = $this->createMock(IAppData::class);
$appData->method('getFolder')->willThrowException(new \Exception());
$this->appDataFactory->method('get')->willReturn($appData);

$this->logger->expects($this->once())
->method('error');
$this->cardDav->expects($this->never())
->method('createCard');

Expand Down
Loading