diff options
author | Hamza Mahjoubi <hamzamahjoubi221@gmail.com> | 2025-04-28 10:44:47 +0200 |
---|---|---|
committer | Hamza Mahjoubi <hamzamahjoubi221@gmail.com> | 2025-04-28 11:46:32 +0200 |
commit | 58ffd9d06e39d1e40508b6fd177e983304e34b4e (patch) | |
tree | 996884f8bf8ecad078b27372c04ad121fd477f9d | |
parent | 6a2c0a254ea71f3b94d49d4f0e3f5c4506d558ed (diff) | |
download | nextcloud-server-fix/default-contact.tar.gz nextcloud-server-fix/default-contact.zip |
fix: check if config is enabled before creating a default contactfix/default-contact
Signed-off-by: Hamza Mahjoubi <hamzamahjoubi221@gmail.com>
-rw-r--r-- | apps/dav/lib/Service/DefaultContactService.php | 7 | ||||
-rw-r--r-- | apps/dav/tests/unit/Service/DefaultContactServiceTest.php | 21 |
2 files changed, 26 insertions, 2 deletions
diff --git a/apps/dav/lib/Service/DefaultContactService.php b/apps/dav/lib/Service/DefaultContactService.php index 193f3b285c6..ad7a1179195 100644 --- a/apps/dav/lib/Service/DefaultContactService.php +++ b/apps/dav/lib/Service/DefaultContactService.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'); diff --git a/apps/dav/tests/unit/Service/DefaultContactServiceTest.php b/apps/dav/tests/unit/Service/DefaultContactServiceTest.php index 9540e77ce6c..68bbc02ed08 100644 --- a/apps/dav/tests/unit/Service/DefaultContactServiceTest.php +++ b/apps/dav/tests/unit/Service/DefaultContactServiceTest.php @@ -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); + } } |