1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\DAV\Tests\Unit\Service;
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;
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;
use Test\TestCase;
class DefaultContactServiceTest extends TestCase {
private DefaultContactService $service;
private MockObject|CardDavBackend $cardDav;
private MockObject|IAppManager $appManager;
private MockObject|IAppDataFactory $appDataFactory;
private MockObject|LoggerInterface $logger;
private MockObject|IAppConfig $config;
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->config = $this->createMock(IAppConfig::class);
$this->service = new DefaultContactService(
$this->cardDav,
$this->appManager,
$this->appDataFactory,
$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);
$file->method('getContent')->willReturn($vcardContent);
$folder->method('getFile')->willReturn($file);
$appData->method('getFolder')->willReturn($folder);
$this->appDataFactory->method('get')->willReturn($appData);
$this->logger->expects($this->once())
->method('error')
->with('Default contact is invalid', $this->anything());
$this->cardDav->expects($this->never())
->method('createCard');
$this->service->createDefaultContact(123);
}
public function testUidAndRevAreUpdated(): void {
$originalUid = 'original-uid';
$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);
$file->method('getContent')->willReturn($vcardContent);
$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')
->with(
$this->anything(),
$this->anything(),
$this->callback(function ($cardData) use (&$capturedCardData) {
$capturedCardData = $cardData;
return true;
}),
$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()));
$this->assertNotEquals($originalRev, $vcard->REV->getValue());
}
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);
$this->cardDav->expects($this->never())
->method('createCard');
$this->service->createDefaultContact(123);
}
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);
$file->method('getContent')->willReturn($vcardContent);
$folder->method('getFile')->willReturn($file);
$appData->method('getFolder')->willReturn($folder);
$this->appDataFactory->method('get')->willReturn($appData);
$capturedCardData = 'new-card-data';
$this->cardDav
->expects($this->once())
->method('createCard')
->with(
$this->anything(),
$this->anything(),
$this->callback(function ($cardData) use (&$capturedCardData) {
$capturedCardData = $cardData;
return true;
}),
$this->anything()
);
$this->service->createDefaultContact(123);
$vcard = \Sabre\VObject\Reader::read($capturedCardData);
$this->assertNotNull($vcard->REV);
$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);
}
}
|