@@ -18,60 +18,60 @@ |
||
| 18 | 18 | use Symfony\Component\Uid\Uuid; |
| 19 | 19 | |
| 20 | 20 | class DefaultContactService { |
| 21 | - public function __construct( |
|
| 22 | - private CardDavBackend $cardDav, |
|
| 23 | - private IAppManager $appManager, |
|
| 24 | - private IAppDataFactory $appDataFactory, |
|
| 25 | - private IAppConfig $config, |
|
| 26 | - private LoggerInterface $logger, |
|
| 27 | - ) { |
|
| 28 | - } |
|
| 21 | + public function __construct( |
|
| 22 | + private CardDavBackend $cardDav, |
|
| 23 | + private IAppManager $appManager, |
|
| 24 | + private IAppDataFactory $appDataFactory, |
|
| 25 | + private IAppConfig $config, |
|
| 26 | + private LoggerInterface $logger, |
|
| 27 | + ) { |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - public function createDefaultContact(int $addressBookId): void { |
|
| 31 | - $enableDefaultContact = $this->config->getValueString(Application::APP_ID, 'enableDefaultContact', 'no'); |
|
| 32 | - if ($enableDefaultContact !== 'yes') { |
|
| 33 | - return; |
|
| 34 | - } |
|
| 35 | - $appData = $this->appDataFactory->get('dav'); |
|
| 36 | - try { |
|
| 37 | - $folder = $appData->getFolder('defaultContact'); |
|
| 38 | - $defaultContactFile = $folder->getFile('defaultContact.vcf'); |
|
| 39 | - $data = $defaultContactFile->getContent(); |
|
| 40 | - } catch (\Exception $e) { |
|
| 41 | - $this->logger->error('Couldn\'t get default contact file', ['exception' => $e]); |
|
| 42 | - return; |
|
| 43 | - } |
|
| 30 | + public function createDefaultContact(int $addressBookId): void { |
|
| 31 | + $enableDefaultContact = $this->config->getValueString(Application::APP_ID, 'enableDefaultContact', 'no'); |
|
| 32 | + if ($enableDefaultContact !== 'yes') { |
|
| 33 | + return; |
|
| 34 | + } |
|
| 35 | + $appData = $this->appDataFactory->get('dav'); |
|
| 36 | + try { |
|
| 37 | + $folder = $appData->getFolder('defaultContact'); |
|
| 38 | + $defaultContactFile = $folder->getFile('defaultContact.vcf'); |
|
| 39 | + $data = $defaultContactFile->getContent(); |
|
| 40 | + } catch (\Exception $e) { |
|
| 41 | + $this->logger->error('Couldn\'t get default contact file', ['exception' => $e]); |
|
| 42 | + return; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - // Make sure the UID is unique |
|
| 46 | - $newUid = Uuid::v4()->toRfc4122(); |
|
| 47 | - $newRev = date('Ymd\THis\Z'); |
|
| 48 | - $vcard = \Sabre\VObject\Reader::read($data, \Sabre\VObject\Reader::OPTION_FORGIVING); |
|
| 49 | - if ($vcard->UID) { |
|
| 50 | - $vcard->UID->setValue($newUid); |
|
| 51 | - } else { |
|
| 52 | - $vcard->add('UID', $newUid); |
|
| 53 | - } |
|
| 54 | - if ($vcard->REV) { |
|
| 55 | - $vcard->REV->setValue($newRev); |
|
| 56 | - } else { |
|
| 57 | - $vcard->add('REV', $newRev); |
|
| 58 | - } |
|
| 45 | + // Make sure the UID is unique |
|
| 46 | + $newUid = Uuid::v4()->toRfc4122(); |
|
| 47 | + $newRev = date('Ymd\THis\Z'); |
|
| 48 | + $vcard = \Sabre\VObject\Reader::read($data, \Sabre\VObject\Reader::OPTION_FORGIVING); |
|
| 49 | + if ($vcard->UID) { |
|
| 50 | + $vcard->UID->setValue($newUid); |
|
| 51 | + } else { |
|
| 52 | + $vcard->add('UID', $newUid); |
|
| 53 | + } |
|
| 54 | + if ($vcard->REV) { |
|
| 55 | + $vcard->REV->setValue($newRev); |
|
| 56 | + } else { |
|
| 57 | + $vcard->add('REV', $newRev); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - // Level 3 means that the document is invalid |
|
| 61 | - // https://sabre.io/vobject/vcard/#validating-vcard |
|
| 62 | - $level3Warnings = array_filter($vcard->validate(), function ($warning) { |
|
| 63 | - return $warning['level'] === 3; |
|
| 64 | - }); |
|
| 60 | + // Level 3 means that the document is invalid |
|
| 61 | + // https://sabre.io/vobject/vcard/#validating-vcard |
|
| 62 | + $level3Warnings = array_filter($vcard->validate(), function ($warning) { |
|
| 63 | + return $warning['level'] === 3; |
|
| 64 | + }); |
|
| 65 | 65 | |
| 66 | - if (!empty($level3Warnings)) { |
|
| 67 | - $this->logger->error('Default contact is invalid', ['warnings' => $level3Warnings]); |
|
| 68 | - return; |
|
| 69 | - } |
|
| 70 | - try { |
|
| 71 | - $this->cardDav->createCard($addressBookId, 'default', $vcard->serialize(), false); |
|
| 72 | - } catch (\Exception $e) { |
|
| 73 | - $this->logger->error($e->getMessage(), ['exception' => $e]); |
|
| 74 | - } |
|
| 66 | + if (!empty($level3Warnings)) { |
|
| 67 | + $this->logger->error('Default contact is invalid', ['warnings' => $level3Warnings]); |
|
| 68 | + return; |
|
| 69 | + } |
|
| 70 | + try { |
|
| 71 | + $this->cardDav->createCard($addressBookId, 'default', $vcard->serialize(), false); |
|
| 72 | + } catch (\Exception $e) { |
|
| 73 | + $this->logger->error($e->getMessage(), ['exception' => $e]); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - } |
|
| 76 | + } |
|
| 77 | 77 | } |
@@ -24,142 +24,142 @@ |
||
| 24 | 24 | use Test\TestCase; |
| 25 | 25 | |
| 26 | 26 | class DefaultContactServiceTest extends TestCase { |
| 27 | - private DefaultContactService $service; |
|
| 28 | - private MockObject|CardDavBackend $cardDav; |
|
| 29 | - private MockObject|IAppManager $appManager; |
|
| 30 | - private MockObject|IAppDataFactory $appDataFactory; |
|
| 31 | - private MockObject|LoggerInterface $logger; |
|
| 32 | - private MockObject|IAppConfig $config; |
|
| 33 | - |
|
| 34 | - protected function setUp(): void { |
|
| 35 | - parent::setUp(); |
|
| 36 | - |
|
| 37 | - $this->cardDav = $this->createMock(CardDavBackend::class); |
|
| 38 | - $this->appManager = $this->createMock(IAppManager::class); |
|
| 39 | - $this->appDataFactory = $this->createMock(IAppDataFactory::class); |
|
| 40 | - $this->logger = $this->createMock(LoggerInterface::class); |
|
| 41 | - $this->config = $this->createMock(IAppConfig::class); |
|
| 42 | - |
|
| 43 | - $this->service = new DefaultContactService( |
|
| 44 | - $this->cardDav, |
|
| 45 | - $this->appManager, |
|
| 46 | - $this->appDataFactory, |
|
| 47 | - $this->config, |
|
| 48 | - $this->logger, |
|
| 49 | - ); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - public function testCreateDefaultContactWithInvalidCard(): void { |
|
| 53 | - // Invalid vCard missing required FN property |
|
| 54 | - $vcardContent = "BEGIN:VCARD\nVERSION:3.0\nEND:VCARD"; |
|
| 55 | - $this->config->method('getValueString')->willReturn('yes'); |
|
| 56 | - $appData = $this->createMock(IAppData::class); |
|
| 57 | - $folder = $this->createMock(ISimpleFolder::class); |
|
| 58 | - $file = $this->createMock(ISimpleFile::class); |
|
| 59 | - $file->method('getContent')->willReturn($vcardContent); |
|
| 60 | - $folder->method('getFile')->willReturn($file); |
|
| 61 | - $appData->method('getFolder')->willReturn($folder); |
|
| 62 | - $this->appDataFactory->method('get')->willReturn($appData); |
|
| 63 | - |
|
| 64 | - $this->logger->expects($this->once()) |
|
| 65 | - ->method('error') |
|
| 66 | - ->with('Default contact is invalid', $this->anything()); |
|
| 67 | - |
|
| 68 | - $this->cardDav->expects($this->never()) |
|
| 69 | - ->method('createCard'); |
|
| 70 | - |
|
| 71 | - $this->service->createDefaultContact(123); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - public function testUidAndRevAreUpdated(): void { |
|
| 75 | - $originalUid = 'original-uid'; |
|
| 76 | - $originalRev = '20200101T000000Z'; |
|
| 77 | - $vcardContent = "BEGIN:VCARD\nVERSION:3.0\nFN:Test User\nUID:$originalUid\nREV:$originalRev\nEND:VCARD"; |
|
| 78 | - |
|
| 79 | - $this->config->method('getValueString')->willReturn('yes'); |
|
| 80 | - $appData = $this->createMock(IAppData::class); |
|
| 81 | - $folder = $this->createMock(ISimpleFolder::class); |
|
| 82 | - $file = $this->createMock(ISimpleFile::class); |
|
| 83 | - $file->method('getContent')->willReturn($vcardContent); |
|
| 84 | - $folder->method('getFile')->willReturn($file); |
|
| 85 | - $appData->method('getFolder')->willReturn($folder); |
|
| 86 | - $this->appDataFactory->method('get')->willReturn($appData); |
|
| 27 | + private DefaultContactService $service; |
|
| 28 | + private MockObject|CardDavBackend $cardDav; |
|
| 29 | + private MockObject|IAppManager $appManager; |
|
| 30 | + private MockObject|IAppDataFactory $appDataFactory; |
|
| 31 | + private MockObject|LoggerInterface $logger; |
|
| 32 | + private MockObject|IAppConfig $config; |
|
| 33 | + |
|
| 34 | + protected function setUp(): void { |
|
| 35 | + parent::setUp(); |
|
| 36 | + |
|
| 37 | + $this->cardDav = $this->createMock(CardDavBackend::class); |
|
| 38 | + $this->appManager = $this->createMock(IAppManager::class); |
|
| 39 | + $this->appDataFactory = $this->createMock(IAppDataFactory::class); |
|
| 40 | + $this->logger = $this->createMock(LoggerInterface::class); |
|
| 41 | + $this->config = $this->createMock(IAppConfig::class); |
|
| 42 | + |
|
| 43 | + $this->service = new DefaultContactService( |
|
| 44 | + $this->cardDav, |
|
| 45 | + $this->appManager, |
|
| 46 | + $this->appDataFactory, |
|
| 47 | + $this->config, |
|
| 48 | + $this->logger, |
|
| 49 | + ); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + public function testCreateDefaultContactWithInvalidCard(): void { |
|
| 53 | + // Invalid vCard missing required FN property |
|
| 54 | + $vcardContent = "BEGIN:VCARD\nVERSION:3.0\nEND:VCARD"; |
|
| 55 | + $this->config->method('getValueString')->willReturn('yes'); |
|
| 56 | + $appData = $this->createMock(IAppData::class); |
|
| 57 | + $folder = $this->createMock(ISimpleFolder::class); |
|
| 58 | + $file = $this->createMock(ISimpleFile::class); |
|
| 59 | + $file->method('getContent')->willReturn($vcardContent); |
|
| 60 | + $folder->method('getFile')->willReturn($file); |
|
| 61 | + $appData->method('getFolder')->willReturn($folder); |
|
| 62 | + $this->appDataFactory->method('get')->willReturn($appData); |
|
| 63 | + |
|
| 64 | + $this->logger->expects($this->once()) |
|
| 65 | + ->method('error') |
|
| 66 | + ->with('Default contact is invalid', $this->anything()); |
|
| 67 | + |
|
| 68 | + $this->cardDav->expects($this->never()) |
|
| 69 | + ->method('createCard'); |
|
| 70 | + |
|
| 71 | + $this->service->createDefaultContact(123); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + public function testUidAndRevAreUpdated(): void { |
|
| 75 | + $originalUid = 'original-uid'; |
|
| 76 | + $originalRev = '20200101T000000Z'; |
|
| 77 | + $vcardContent = "BEGIN:VCARD\nVERSION:3.0\nFN:Test User\nUID:$originalUid\nREV:$originalRev\nEND:VCARD"; |
|
| 78 | + |
|
| 79 | + $this->config->method('getValueString')->willReturn('yes'); |
|
| 80 | + $appData = $this->createMock(IAppData::class); |
|
| 81 | + $folder = $this->createMock(ISimpleFolder::class); |
|
| 82 | + $file = $this->createMock(ISimpleFile::class); |
|
| 83 | + $file->method('getContent')->willReturn($vcardContent); |
|
| 84 | + $folder->method('getFile')->willReturn($file); |
|
| 85 | + $appData->method('getFolder')->willReturn($folder); |
|
| 86 | + $this->appDataFactory->method('get')->willReturn($appData); |
|
| 87 | 87 | |
| 88 | - $capturedCardData = null; |
|
| 89 | - $this->cardDav->expects($this->once()) |
|
| 90 | - ->method('createCard') |
|
| 91 | - ->with( |
|
| 92 | - $this->anything(), |
|
| 93 | - $this->anything(), |
|
| 94 | - $this->callback(function ($cardData) use (&$capturedCardData) { |
|
| 95 | - $capturedCardData = $cardData; |
|
| 96 | - return true; |
|
| 97 | - }), |
|
| 98 | - $this->anything() |
|
| 99 | - )->willReturn(null); |
|
| 88 | + $capturedCardData = null; |
|
| 89 | + $this->cardDav->expects($this->once()) |
|
| 90 | + ->method('createCard') |
|
| 91 | + ->with( |
|
| 92 | + $this->anything(), |
|
| 93 | + $this->anything(), |
|
| 94 | + $this->callback(function ($cardData) use (&$capturedCardData) { |
|
| 95 | + $capturedCardData = $cardData; |
|
| 96 | + return true; |
|
| 97 | + }), |
|
| 98 | + $this->anything() |
|
| 99 | + )->willReturn(null); |
|
| 100 | 100 | |
| 101 | - $this->service->createDefaultContact(123); |
|
| 101 | + $this->service->createDefaultContact(123); |
|
| 102 | 102 | |
| 103 | - $vcard = \Sabre\VObject\Reader::read($capturedCardData); |
|
| 104 | - $this->assertNotEquals($originalUid, $vcard->UID->getValue()); |
|
| 105 | - $this->assertTrue(Uuid::isValid($vcard->UID->getValue())); |
|
| 106 | - $this->assertNotEquals($originalRev, $vcard->REV->getValue()); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - public function testDefaultContactFileDoesNotExist(): void { |
|
| 110 | - $appData = $this->createMock(IAppData::class); |
|
| 111 | - $this->config->method('getValueString')->willReturn('yes'); |
|
| 112 | - $appData->method('getFolder')->willThrowException(new NotFoundException()); |
|
| 113 | - $this->appDataFactory->method('get')->willReturn($appData); |
|
| 114 | - |
|
| 115 | - $this->cardDav->expects($this->never()) |
|
| 116 | - ->method('createCard'); |
|
| 117 | - |
|
| 118 | - $this->service->createDefaultContact(123); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - public function testUidAndRevAreAddedIfMissing(): void { |
|
| 122 | - $vcardContent = "BEGIN:VCARD\nVERSION:3.0\nFN:Test User\nEND:VCARD"; |
|
| 123 | - |
|
| 124 | - $this->config->method('getValueString')->willReturn('yes'); |
|
| 125 | - $appData = $this->createMock(IAppData::class); |
|
| 126 | - $folder = $this->createMock(ISimpleFolder::class); |
|
| 127 | - $file = $this->createMock(ISimpleFile::class); |
|
| 128 | - $file->method('getContent')->willReturn($vcardContent); |
|
| 129 | - $folder->method('getFile')->willReturn($file); |
|
| 130 | - $appData->method('getFolder')->willReturn($folder); |
|
| 131 | - $this->appDataFactory->method('get')->willReturn($appData); |
|
| 132 | - |
|
| 133 | - $capturedCardData = 'new-card-data'; |
|
| 134 | - |
|
| 135 | - $this->cardDav |
|
| 136 | - ->expects($this->once()) |
|
| 137 | - ->method('createCard') |
|
| 138 | - ->with( |
|
| 139 | - $this->anything(), |
|
| 140 | - $this->anything(), |
|
| 141 | - $this->callback(function ($cardData) use (&$capturedCardData) { |
|
| 142 | - $capturedCardData = $cardData; |
|
| 143 | - return true; |
|
| 144 | - }), |
|
| 145 | - $this->anything() |
|
| 146 | - ); |
|
| 147 | - |
|
| 148 | - $this->service->createDefaultContact(123); |
|
| 149 | - $vcard = \Sabre\VObject\Reader::read($capturedCardData); |
|
| 150 | - |
|
| 151 | - $this->assertNotNull($vcard->REV); |
|
| 152 | - $this->assertNotNull($vcard->UID); |
|
| 153 | - $this->assertTrue(Uuid::isValid($vcard->UID->getValue())); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - public function testDefaultContactIsNotCreatedIfEnabled(): void { |
|
| 157 | - $this->config->method('getValueString')->willReturn('no'); |
|
| 158 | - $this->logger->expects($this->never()) |
|
| 159 | - ->method('error'); |
|
| 160 | - $this->cardDav->expects($this->never()) |
|
| 161 | - ->method('createCard'); |
|
| 162 | - |
|
| 163 | - $this->service->createDefaultContact(123); |
|
| 164 | - } |
|
| 103 | + $vcard = \Sabre\VObject\Reader::read($capturedCardData); |
|
| 104 | + $this->assertNotEquals($originalUid, $vcard->UID->getValue()); |
|
| 105 | + $this->assertTrue(Uuid::isValid($vcard->UID->getValue())); |
|
| 106 | + $this->assertNotEquals($originalRev, $vcard->REV->getValue()); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + public function testDefaultContactFileDoesNotExist(): void { |
|
| 110 | + $appData = $this->createMock(IAppData::class); |
|
| 111 | + $this->config->method('getValueString')->willReturn('yes'); |
|
| 112 | + $appData->method('getFolder')->willThrowException(new NotFoundException()); |
|
| 113 | + $this->appDataFactory->method('get')->willReturn($appData); |
|
| 114 | + |
|
| 115 | + $this->cardDav->expects($this->never()) |
|
| 116 | + ->method('createCard'); |
|
| 117 | + |
|
| 118 | + $this->service->createDefaultContact(123); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + public function testUidAndRevAreAddedIfMissing(): void { |
|
| 122 | + $vcardContent = "BEGIN:VCARD\nVERSION:3.0\nFN:Test User\nEND:VCARD"; |
|
| 123 | + |
|
| 124 | + $this->config->method('getValueString')->willReturn('yes'); |
|
| 125 | + $appData = $this->createMock(IAppData::class); |
|
| 126 | + $folder = $this->createMock(ISimpleFolder::class); |
|
| 127 | + $file = $this->createMock(ISimpleFile::class); |
|
| 128 | + $file->method('getContent')->willReturn($vcardContent); |
|
| 129 | + $folder->method('getFile')->willReturn($file); |
|
| 130 | + $appData->method('getFolder')->willReturn($folder); |
|
| 131 | + $this->appDataFactory->method('get')->willReturn($appData); |
|
| 132 | + |
|
| 133 | + $capturedCardData = 'new-card-data'; |
|
| 134 | + |
|
| 135 | + $this->cardDav |
|
| 136 | + ->expects($this->once()) |
|
| 137 | + ->method('createCard') |
|
| 138 | + ->with( |
|
| 139 | + $this->anything(), |
|
| 140 | + $this->anything(), |
|
| 141 | + $this->callback(function ($cardData) use (&$capturedCardData) { |
|
| 142 | + $capturedCardData = $cardData; |
|
| 143 | + return true; |
|
| 144 | + }), |
|
| 145 | + $this->anything() |
|
| 146 | + ); |
|
| 147 | + |
|
| 148 | + $this->service->createDefaultContact(123); |
|
| 149 | + $vcard = \Sabre\VObject\Reader::read($capturedCardData); |
|
| 150 | + |
|
| 151 | + $this->assertNotNull($vcard->REV); |
|
| 152 | + $this->assertNotNull($vcard->UID); |
|
| 153 | + $this->assertTrue(Uuid::isValid($vcard->UID->getValue())); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + public function testDefaultContactIsNotCreatedIfEnabled(): void { |
|
| 157 | + $this->config->method('getValueString')->willReturn('no'); |
|
| 158 | + $this->logger->expects($this->never()) |
|
| 159 | + ->method('error'); |
|
| 160 | + $this->cardDav->expects($this->never()) |
|
| 161 | + ->method('createCard'); |
|
| 162 | + |
|
| 163 | + $this->service->createDefaultContact(123); |
|
| 164 | + } |
|
| 165 | 165 | } |
@@ -25,11 +25,11 @@ discard block |
||
| 25 | 25 | |
| 26 | 26 | class DefaultContactServiceTest extends TestCase { |
| 27 | 27 | private DefaultContactService $service; |
| 28 | - private MockObject|CardDavBackend $cardDav; |
|
| 29 | - private MockObject|IAppManager $appManager; |
|
| 30 | - private MockObject|IAppDataFactory $appDataFactory; |
|
| 31 | - private MockObject|LoggerInterface $logger; |
|
| 32 | - private MockObject|IAppConfig $config; |
|
| 28 | + private MockObject | CardDavBackend $cardDav; |
|
| 29 | + private MockObject | IAppManager $appManager; |
|
| 30 | + private MockObject | IAppDataFactory $appDataFactory; |
|
| 31 | + private MockObject | LoggerInterface $logger; |
|
| 32 | + private MockObject | IAppConfig $config; |
|
| 33 | 33 | |
| 34 | 34 | protected function setUp(): void { |
| 35 | 35 | parent::setUp(); |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | ->with( |
| 92 | 92 | $this->anything(), |
| 93 | 93 | $this->anything(), |
| 94 | - $this->callback(function ($cardData) use (&$capturedCardData) { |
|
| 94 | + $this->callback(function($cardData) use (&$capturedCardData) { |
|
| 95 | 95 | $capturedCardData = $cardData; |
| 96 | 96 | return true; |
| 97 | 97 | }), |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | ->with( |
| 139 | 139 | $this->anything(), |
| 140 | 140 | $this->anything(), |
| 141 | - $this->callback(function ($cardData) use (&$capturedCardData) { |
|
| 141 | + $this->callback(function($cardData) use (&$capturedCardData) { |
|
| 142 | 142 | $capturedCardData = $cardData; |
| 143 | 143 | return true; |
| 144 | 144 | }), |