diff --git a/lib/Component.php b/lib/Component.php index 50064306..df47e829 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 4a247d90..45e85a7a 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();