|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Sales\Model; |
| 9 | + |
| 10 | +use Magento\Config\Model\Config\Source\Nooptreq; |
| 11 | +use Magento\Framework\App\Config\MutableScopeConfigInterface; |
| 12 | +use Magento\Framework\ObjectManagerInterface; |
| 13 | +use Magento\Store\Model\ScopeInterface; |
| 14 | +use Magento\TestFramework\Helper\Bootstrap; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +/** |
| 18 | + * Tests visibility of customer prefix/suffix via Order::getCustomerName(), |
| 19 | + * covering null (unset), empty string, optional and required config values. |
| 20 | + * |
| 21 | + * @magentoDbIsolation enabled |
| 22 | + */ |
| 23 | +class OrderPrefixSuffixVisibilityTest extends TestCase |
| 24 | +{ |
| 25 | + private const XML_PATH_PREFIX_SHOW = 'customer/address/prefix_show'; |
| 26 | + private const XML_PATH_SUFFIX_SHOW = 'customer/address/suffix_show'; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var ObjectManagerInterface |
| 30 | + */ |
| 31 | + private ObjectManagerInterface $om; |
| 32 | + |
| 33 | + protected function setUp(): void |
| 34 | + { |
| 35 | + $this->om = Bootstrap::getObjectManager(); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @dataProvider visibilityCases |
| 40 | + */ |
| 41 | + public function testPrefixVisibility(?string $value, bool $setValue, bool $expectedVisible): void |
| 42 | + { |
| 43 | + // Ensure suffix never interferes in this test |
| 44 | + $this->setConfig(self::XML_PATH_SUFFIX_SHOW, Nooptreq::VALUE_NO); |
| 45 | + |
| 46 | + if ($setValue) { |
| 47 | + $this->setConfig(self::XML_PATH_PREFIX_SHOW, $value); |
| 48 | + } |
| 49 | + // If not set, path remains unset (null), simulating the reported case. |
| 50 | + |
| 51 | + $order = $this->makeOrder('Dr', 'John', 'Doe', 'Jr'); // suffix ignored here |
| 52 | + $expected = $expectedVisible ? 'Dr John Doe' : 'John Doe'; |
| 53 | + |
| 54 | + $this->assertSame($expected, $order->getCustomerName()); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @dataProvider visibilityCases |
| 59 | + */ |
| 60 | + public function testSuffixVisibility(?string $value, bool $setValue, bool $expectedVisible): void |
| 61 | + { |
| 62 | + // Ensure prefix never interferes in this test |
| 63 | + $this->setConfig(self::XML_PATH_PREFIX_SHOW, Nooptreq::VALUE_NO); |
| 64 | + |
| 65 | + if ($setValue) { |
| 66 | + $this->setConfig(self::XML_PATH_SUFFIX_SHOW, $value); |
| 67 | + } |
| 68 | + // If not set, path remains unset (null), simulating the reported case. |
| 69 | + |
| 70 | + $order = $this->makeOrder('Dr', 'John', 'Doe', 'Jr'); // prefix ignored here |
| 71 | + $expected = $expectedVisible ? 'John Doe Jr' : 'John Doe'; |
| 72 | + |
| 73 | + $this->assertSame($expected, $order->getCustomerName()); |
| 74 | + } |
| 75 | + |
| 76 | + public static function visibilityCases(): array |
| 77 | + { |
| 78 | + return [ |
| 79 | + // value, setValue, expectedVisible |
| 80 | + 'unset_null_defaults_to_no' => [null, false, false], // path not set → null |
| 81 | + 'explicit_empty_string_no' => [Nooptreq::VALUE_NO, true, false], // '' |
| 82 | + 'optional_visible' => [Nooptreq::VALUE_OPTIONAL, true, true], // 'opt' |
| 83 | + 'required_visible' => [Nooptreq::VALUE_REQUIRED, true, true], // 'req' |
| 84 | + ]; |
| 85 | + } |
| 86 | + |
| 87 | + private function makeOrder(string $prefix, string $first, string $last, string $suffix): Order |
| 88 | + { |
| 89 | + /** @var Order $order */ |
| 90 | + $order = $this->om->create(Order::class); |
| 91 | + $order->setCustomerPrefix($prefix); |
| 92 | + $order->setCustomerFirstname($first); |
| 93 | + $order->setCustomerLastname($last); |
| 94 | + $order->setCustomerSuffix($suffix); |
| 95 | + // Middlename left null to avoid affecting formatting. |
| 96 | + return $order; |
| 97 | + } |
| 98 | + |
| 99 | + private function setConfig( |
| 100 | + string $path, |
| 101 | + ?string $value, |
| 102 | + string $scope = ScopeInterface::SCOPE_STORE, |
| 103 | + string $scopeCode = 'default' |
| 104 | + ): void { |
| 105 | + $this->om->get(MutableScopeConfigInterface::class)->setValue($path, $value, $scope, $scopeCode); |
| 106 | + } |
| 107 | +} |
0 commit comments