Skip to content

Commit 1b2386d

Browse files
committed
handle nested properties, fixes #20544
Signed-off-by: call-me-matt <nextcloud@matthiasheinisch.de>
1 parent 0dbb99a commit 1b2386d

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

apps/dav/lib/CardDAV/AddressBookImpl.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,22 @@ public function createOrUpdate($properties) {
146146
}
147147

148148
foreach ($properties as $key => $value) {
149-
$vCard->$key = $vCard->createProperty($key, $value);
149+
if (is_array($value)) {
150+
$vCard->remove($key);
151+
foreach ($value as $entry) {
152+
if (($key === "ADR" || $key === "PHOTO") && is_string($entry["value"])) {
153+
$entry["value"] = stripslashes($entry["value"]);
154+
$entry["value"] = explode(';', $entry["value"]);
155+
}
156+
$property = $vCard->createProperty($key, $entry["value"]);
157+
if (isset($entry["type"])) {
158+
$property->add('TYPE', $entry["type"]);
159+
}
160+
$vCard->add($property);
161+
}
162+
} elseif ($key !== 'URI') {
163+
$vCard->$key = $vCard->createProperty($key, $value);
164+
}
150165
}
151166

152167
if ($update) {

apps/dav/tests/unit/CardDAV/AddressBookImplTest.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* @author Morris Jobke <hey@morrisjobke.de>
1111
* @author Roeland Jago Douma <roeland@famdouma.nl>
1212
* @author Thomas Müller <thomas.mueller@tmit.eu>
13+
* @author Matthias Heinisch <nextcloud@matthiasheinisch.de>
1314
*
1415
* @license AGPL-3.0
1516
*
@@ -35,6 +36,7 @@
3536
use OCP\IURLGenerator;
3637
use Sabre\VObject\Component\VCard;
3738
use Sabre\VObject\Property\Text;
39+
//use Sabre\VObject\Property\;
3840
use Test\TestCase;
3941

4042
class AddressBookImplTest extends TestCase {
@@ -199,7 +201,7 @@ public function testUpdate() {
199201
->willReturn(['carddata' => 'data']);
200202
$addressBookImpl->expects($this->once())->method('readCard')
201203
->with('data')->willReturn($this->vCard);
202-
$this->vCard->expects($this->exactly(count($properties)))
204+
$this->vCard->expects($this->exactly(count($properties)-1))
203205
->method('createProperty');
204206
$this->backend->expects($this->never())->method('createCard');
205207
$this->backend->expects($this->once())->method('updateCard');
@@ -209,6 +211,41 @@ public function testUpdate() {
209211
$this->assertTrue($addressBookImpl->createOrUpdate($properties));
210212
}
211213

214+
public function testUpdateWithTypes() {
215+
$uid = 'uid';
216+
$uri = 'bla.vcf';
217+
$properties = ['URI' => $uri, 'UID' => $uid, 'FN' => 'John Doe', 'ADR' => [['type' => 'HOME', 'value' => ';;street;city;;;country']]];
218+
$vCard = new vCard;
219+
$textProperty = $vCard->createProperty('KEY','value');
220+
221+
/** @var \PHPUnit\Framework\MockObject\MockObject | AddressBookImpl $addressBookImpl */
222+
$addressBookImpl = $this->getMockBuilder(AddressBookImpl::class)
223+
->setConstructorArgs(
224+
[
225+
$this->addressBook,
226+
$this->addressBookInfo,
227+
$this->backend,
228+
$this->urlGenerator,
229+
]
230+
)
231+
->setMethods(['vCard2Array', 'createUid', 'createEmptyVCard', 'readCard'])
232+
->getMock();
233+
234+
$this->backend->expects($this->once())->method('getCard')
235+
->with($this->addressBookInfo['id'], $uri)
236+
->willReturn(['carddata' => 'data']);
237+
$addressBookImpl->expects($this->once())->method('readCard')
238+
->with('data')->willReturn($this->vCard);
239+
$this->vCard->method('createProperty')->willReturn($textProperty);
240+
$this->vCard->expects($this->exactly(count($properties)-1))
241+
->method('createProperty');
242+
$this->vCard->expects($this->once())->method('remove')
243+
->with('ADR');
244+
$this->vCard->expects($this->once())->method('add');
245+
246+
$addressBookImpl->createOrUpdate($properties);
247+
}
248+
212249
/**
213250
* @dataProvider dataTestGetPermissions
214251
*

0 commit comments

Comments
 (0)