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
fix: check if config is enabled before creating a default contact
Signed-off-by: Hamza Mahjoubi <[email protected]>
  • Loading branch information
hamza221 committed Apr 28, 2025
commit 58ffd9d06e39d1e40508b6fd177e983304e34b4e
7 changes: 7 additions & 0 deletions apps/dav/lib/Service/DefaultContactService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

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;

Expand All @@ -20,11 +22,16 @@ public function __construct(
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');
Expand Down
21 changes: 19 additions & 2 deletions apps/dav/tests/unit/Service/DefaultContactServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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;
Expand All @@ -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();
Expand All @@ -36,19 +38,21 @@ protected function setUp(): void {
$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);
Expand All @@ -72,6 +76,7 @@ public function testUidAndRevAreUpdated(): void {
$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);
Expand Down Expand Up @@ -103,6 +108,7 @@ public function testUidAndRevAreUpdated(): void {

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);

Expand All @@ -115,6 +121,7 @@ public function testDefaultContactFileDoesNotExist(): void {
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);
Expand Down Expand Up @@ -145,4 +152,14 @@ public function testUidAndRevAreAddedIfMissing(): void {
$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