fix: check if config is enabled before creating a default contact

Signed-off-by: Hamza Mahjoubi <hamzamahjoubi221@gmail.com>
pull/52494/head
Hamza Mahjoubi 8 months ago
parent 6a2c0a254e
commit 58ffd9d06e
  1. 7
      apps/dav/lib/Service/DefaultContactService.php
  2. 21
      apps/dav/tests/unit/Service/DefaultContactServiceTest.php

@ -9,9 +9,11 @@ declare(strict_types=1);
namespace OCA\DAV\Service;
use OCA\DAV\AppInfo\Application;
use OCA\DAV\CardDAV\CardDavBackend;
use OCP\App\IAppManager;
use OCP\Files\AppData\IAppDataFactory;
use OCP\IAppConfig;
use Psr\Log\LoggerInterface;
use Symfony\Component\Uid\Uuid;
@ -20,11 +22,16 @@ class DefaultContactService {
private CardDavBackend $cardDav,
private IAppManager $appManager,
private IAppDataFactory $appDataFactory,
private IAppConfig $config,
private LoggerInterface $logger,
) {
}
public function createDefaultContact(int $addressBookId): void {
$enableDefaultContact = $this->config->getValueString(Application::APP_ID, 'enableDefaultContact', 'no');
if ($enableDefaultContact !== 'yes') {
return;
}
$appData = $this->appDataFactory->get('dav');
try {
$folder = $appData->getFolder('defaultContact');

@ -17,6 +17,7 @@ use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\IAppConfig;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Symfony\Component\Uid\Uuid;
@ -28,6 +29,7 @@ class DefaultContactServiceTest extends TestCase {
private MockObject|IAppManager $appManager;
private MockObject|IAppDataFactory $appDataFactory;
private MockObject|LoggerInterface $logger;
private MockObject|IAppConfig $config;
protected function setUp(): void {
parent::setUp();
@ -36,19 +38,21 @@ class DefaultContactServiceTest extends TestCase {
$this->appManager = $this->createMock(IAppManager::class);
$this->appDataFactory = $this->createMock(IAppDataFactory::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->config = $this->createMock(IAppConfig::class);
$this->service = new DefaultContactService(
$this->cardDav,
$this->appManager,
$this->appDataFactory,
$this->logger
$this->config,
$this->logger,
);
}
public function testCreateDefaultContactWithInvalidCard(): void {
// Invalid vCard missing required FN property
$vcardContent = "BEGIN:VCARD\nVERSION:3.0\nEND:VCARD";
$this->config->method('getValueString')->willReturn('yes');
$appData = $this->createMock(IAppData::class);
$folder = $this->createMock(ISimpleFolder::class);
$file = $this->createMock(ISimpleFile::class);
@ -72,6 +76,7 @@ class DefaultContactServiceTest extends TestCase {
$originalRev = '20200101T000000Z';
$vcardContent = "BEGIN:VCARD\nVERSION:3.0\nFN:Test User\nUID:$originalUid\nREV:$originalRev\nEND:VCARD";
$this->config->method('getValueString')->willReturn('yes');
$appData = $this->createMock(IAppData::class);
$folder = $this->createMock(ISimpleFolder::class);
$file = $this->createMock(ISimpleFile::class);
@ -103,6 +108,7 @@ class DefaultContactServiceTest extends TestCase {
public function testDefaultContactFileDoesNotExist(): void {
$appData = $this->createMock(IAppData::class);
$this->config->method('getValueString')->willReturn('yes');
$appData->method('getFolder')->willThrowException(new NotFoundException());
$this->appDataFactory->method('get')->willReturn($appData);
@ -115,6 +121,7 @@ class DefaultContactServiceTest extends TestCase {
public function testUidAndRevAreAddedIfMissing(): void {
$vcardContent = "BEGIN:VCARD\nVERSION:3.0\nFN:Test User\nEND:VCARD";
$this->config->method('getValueString')->willReturn('yes');
$appData = $this->createMock(IAppData::class);
$folder = $this->createMock(ISimpleFolder::class);
$file = $this->createMock(ISimpleFile::class);
@ -145,4 +152,14 @@ class DefaultContactServiceTest extends TestCase {
$this->assertNotNull($vcard->UID);
$this->assertTrue(Uuid::isValid($vcard->UID->getValue()));
}
public function testDefaultContactIsNotCreatedIfEnabled(): void {
$this->config->method('getValueString')->willReturn('no');
$this->logger->expects($this->never())
->method('error');
$this->cardDav->expects($this->never())
->method('createCard');
$this->service->createDefaultContact(123);
}
}

Loading…
Cancel
Save