From 96af6b595b38e10d702e260b5dcf8aa9c65c0635 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ce=CC=81dric=20Aguettaz?= Date: Mon, 3 Apr 2023 09:23:26 +0200 Subject: [PATCH] validate repair component should not deduplicate properties when properties must appear exactly once --- lib/Component.php | 3 +++ tests/VObject/ComponentTest.php | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/Component.php b/lib/Component.php index 500643062..df47e8293 100644 --- a/lib/Component.php +++ b/lib/Component.php @@ -569,6 +569,9 @@ public function validate(int $options = 0): array if (!isset($propertyCounters[$propName]) || 1 !== $propertyCounters[$propName]) { $repaired = false; if ($options & self::REPAIR && isset($defaults[$propName])) { + if (isset($propertyCounters[$propName]) && 1 < $propertyCounters[$propName]) { + $this->remove($propName); + } $this->add($propName, $defaults[$propName]); $repaired = true; } diff --git a/tests/VObject/ComponentTest.php b/tests/VObject/ComponentTest.php index 4a247d906..45e85a7af 100644 --- a/tests/VObject/ComponentTest.php +++ b/tests/VObject/ComponentTest.php @@ -482,6 +482,22 @@ public function testValidateRepair(): void self::assertEquals('yow', $component->BAR->getValue()); } + public function testValidateRepairShouldNotDeduplicatePropertiesWhenPropertiesMustAppearExactlyOnce(): void + { + $vcard = new Component\VCard(); + + $component = new FakeComponent($vcard, 'Hi', []); + $component->add('BAZ', 'BAZ'); + $component->add('BAR', 'BAR'); + $component->add('BAR', 'BAR'); + + $messages = $component->validate(Component::REPAIR); + + self::assertCount(1, $messages); + self::assertCount(1, $component->BAR); + self::assertEquals('yow', $component->BAR->getValue()); + } + public function testValidateRepairShouldNotDeduplicatePropertiesWhenValuesDiffer(): void { $vcard = new Component\VCard();