From ec8d37bba15d197ff4b09d045a9ec9fd82bd42d2 Mon Sep 17 00:00:00 2001 From: Michiel Gerritsen Date: Mon, 6 Jan 2025 11:10:31 +0100 Subject: [PATCH] Bugfix: Check if the shipping address isset #841 --- .../TransactionPart/LimitStreetLength.php | 24 ++-- .../TransactionPart/LimitStreetLengthTest.php | 106 ++++++++++++++++++ 2 files changed, 118 insertions(+), 12 deletions(-) create mode 100644 Test/Integration/Service/Order/TransactionPart/LimitStreetLengthTest.php diff --git a/Service/Order/TransactionPart/LimitStreetLength.php b/Service/Order/TransactionPart/LimitStreetLength.php index 51be9c975d1..e3845afccb2 100644 --- a/Service/Order/TransactionPart/LimitStreetLength.php +++ b/Service/Order/TransactionPart/LimitStreetLength.php @@ -19,18 +19,8 @@ class LimitStreetLength implements TransactionPartInterface public function process(OrderInterface $order, $apiMethod, array $transaction): array { - if ($apiMethod == Orders::CHECKOUT_TYPE) { - $transaction['billingAddress']['streetAndNumber'] = $this->limitStreetLength($transaction['billingAddress']['streetAndNumber']); - $transaction['shippingAddress']['streetAndNumber'] = $this->limitStreetLength($transaction['shippingAddress']['streetAndNumber']); - } - - if ($apiMethod == Payments::CHECKOUT_TYPE) { - $transaction['billingAddress']['streetAndNumber'] = $this->limitStreetLength($transaction['billingAddress']['streetAndNumber']); - } - - if ($apiMethod == Payments::CHECKOUT_TYPE && array_key_exists('shippingAddress', $transaction)) { - $transaction['shippingAddress']['streetAndNumber'] = $this->limitStreetLength($transaction['shippingAddress']['streetAndNumber']); - } + $transaction = $this->limitAddress('billingAddress', $transaction); + $transaction = $this->limitAddress('shippingAddress', $transaction); if ($this->streetTruncated) { $transaction['metadata']['street_truncated'] = true; @@ -48,4 +38,14 @@ private function limitStreetLength(string $street): string $this->streetTruncated = true; return mb_substr($street, 0, 100); } + + private function limitAddress(string $type, array $transaction): array + { + if (array_key_exists($type, $transaction)) { + $limited = $this->limitStreetLength($transaction[$type]['streetAndNumber']); + $transaction[$type]['streetAndNumber'] = $limited; + } + + return $transaction; + } } diff --git a/Test/Integration/Service/Order/TransactionPart/LimitStreetLengthTest.php b/Test/Integration/Service/Order/TransactionPart/LimitStreetLengthTest.php new file mode 100644 index 00000000000..1c1478d46b9 --- /dev/null +++ b/Test/Integration/Service/Order/TransactionPart/LimitStreetLengthTest.php @@ -0,0 +1,106 @@ +objectManager->create(LimitStreetLength::class); + + $transaction = [ + 'billingAddress' => [ + 'streetAndNumber' => 'a super long steet name that exceeds the maximum of 100 characters and should be truncated when its too long', + ], + 'shippingAddress' => [ + 'streetAndNumber' => 'a super long steet name that exceeds the maximum of 100 characters and should be truncated when its too long', + ], + ]; + + $result = $instance->process( + $this->objectManager->create(OrderInterface::class), + Orders::CHECKOUT_TYPE, + $transaction + ); + + $this->assertEquals( + 'a super long steet name that exceeds the maximum of 100 characters and should be truncated when its ', + $result['billingAddress']['streetAndNumber'] + ); + + $this->assertEquals( + 'a super long steet name that exceeds the maximum of 100 characters and should be truncated when its ', + $result['shippingAddress']['streetAndNumber'] + ); + + $this->assertArrayHasKey('metadata', $result); + $this->assertArrayHasKey('street_truncated', $result['metadata']); + $this->assertTrue($result['metadata']['street_truncated']); + } + + public function testHandlesVirtualProducts(): void + { + /** @var LimitStreetLength $instance */ + $instance = $this->objectManager->create(LimitStreetLength::class); + + $transaction = [ + 'billingAddress' => [ + 'streetAndNumber' => 'a super long steet name that exceeds the maximum of 100 characters and should be truncated when its too long', + ], + // Omit the shipping address as that's not relevant for virtual products + ]; + + $result = $instance->process( + $this->objectManager->create(OrderInterface::class), + Orders::CHECKOUT_TYPE, + $transaction + ); + + $this->assertEquals( + 'a super long steet name that exceeds the maximum of 100 characters and should be truncated when its ', + $result['billingAddress']['streetAndNumber'] + ); + + $this->assertArrayHasKey('metadata', $result); + $this->assertArrayHasKey('street_truncated', $result['metadata']); + $this->assertTrue($result['metadata']['street_truncated']); + } + + public function testDoesNotMarkAsTruncatedWhenNotTruncated(): void + { + /** @var LimitStreetLength $instance */ + $instance = $this->objectManager->create(LimitStreetLength::class); + + $transaction = [ + 'metadata' => [], + 'billingAddress' => [ + 'streetAndNumber' => 'a short street name', + ], + ]; + + $result = $instance->process( + $this->objectManager->create(OrderInterface::class), + Orders::CHECKOUT_TYPE, + $transaction + ); + + $this->assertEquals( + 'a short street name', + $result['billingAddress']['streetAndNumber'] + ); + + $this->assertArrayNotHasKey('street_truncated', $result['metadata']); + } +}