Skip to content

Commit c0bbd6f

Browse files
rhoerrfballiano
authored andcommitted
Cherry-pick of pull request #117 from ProxiBlue/2.4-develop
1 parent 4eb2b41 commit c0bbd6f

File tree

6 files changed

+120
-1
lines changed

6 files changed

+120
-1
lines changed

app/code/Magento/Customer/Model/Address.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Customer\Model\Address\AbstractAddress\RegionModelsCache;
1414
use Magento\Customer\Model\Address\CompositeValidator;
1515
use Magento\Framework\Indexer\StateInterface;
16+
use Magento\Customer\Helper\Address as AddressHelper;
1617

1718
/**
1819
* Customer address model
@@ -80,6 +81,7 @@ class Address extends \Magento\Customer\Model\Address\AbstractAddress
8081
* @param CompositeValidator|null $compositeValidator
8182
* @param CountryModelsCache|null $countryModelsCache
8283
* @param RegionModelsCache|null $regionModelsCache
84+
* @param AddressHelper|null $addressHelper
8385
*
8486
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
8587
*/
@@ -106,6 +108,7 @@ public function __construct(
106108
?CompositeValidator $compositeValidator = null,
107109
?CountryModelsCache $countryModelsCache = null,
108110
?RegionModelsCache $regionModelsCache = null,
111+
?AddressHelper $addressHelper = null
109112
) {
110113
$this->dataProcessor = $dataProcessor;
111114
$this->_customerFactory = $customerFactory;
@@ -130,6 +133,7 @@ public function __construct(
130133
$compositeValidator,
131134
$countryModelsCache,
132135
$regionModelsCache,
136+
$addressHelper
133137
);
134138
}
135139

app/code/Magento/Customer/Model/Address/AbstractAddress.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Framework\App\ObjectManager;
1818
use Magento\Framework\Model\AbstractExtensibleModel;
1919
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
20+
use Magento\Customer\Helper\Address as AddressHelper;
2021

2122
/**
2223
* Address abstract model
@@ -146,6 +147,11 @@ class AbstractAddress extends AbstractExtensibleModel implements AddressModelInt
146147
*/
147148
private array $regionIdCountry = [];
148149

150+
/**
151+
* @var AddressHelper|null
152+
*/
153+
protected ?AddressHelper $addressHelper;
154+
149155
/**
150156
* @param \Magento\Framework\Model\Context $context
151157
* @param \Magento\Framework\Registry $registry
@@ -166,6 +172,7 @@ class AbstractAddress extends AbstractExtensibleModel implements AddressModelInt
166172
* @param CompositeValidator $compositeValidator
167173
* @param CountryModelsCache|null $countryModelsCache
168174
* @param RegionModelsCache|null $regionModelsCache
175+
* @param AddressHelper|null $addressHelper
169176
*
170177
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
171178
*/
@@ -189,6 +196,7 @@ public function __construct(
189196
CompositeValidator $compositeValidator = null,
190197
?CountryModelsCache $countryModelsCache = null,
191198
?RegionModelsCache $regionModelsCache = null,
199+
?AddressHelper $addressHelper = null
192200
) {
193201
$this->_directoryData = $directoryData;
194202
$data = $this->_implodeArrayField($data);
@@ -206,6 +214,8 @@ public function __construct(
206214
->get(CountryModelsCache::class);
207215
$this->regionModelsCache = $regionModelsCache ?: ObjectManager::getInstance()
208216
->get(RegionModelsCache::class);
217+
$this->addressHelper = $addressHelper ?: ObjectManager::getInstance()
218+
->get(AddressHelper::class);
209219
parent::__construct(
210220
$context,
211221
$registry,
@@ -242,6 +252,8 @@ public function getName()
242252

243253
/**
244254
* Retrieve street field of an address
255+
* Honour current configured street lines, and convert
256+
* legacy data to match
245257
*
246258
* @return string[]
247259
*/
@@ -250,7 +262,11 @@ public function getStreet()
250262
if (is_array($this->getStreetFull())) {
251263
return $this->getStreetFull();
252264
}
253-
return explode("\n", $this->getStreetFull());
265+
$maxAllowedLineCount = $this->addressHelper->getStreetLines() ?? 2;
266+
$lines = explode("\n", $this->getStreetFull());
267+
$lines = $this->addressHelper->convertStreetLines($lines, $maxAllowedLineCount);
268+
$this->setStreetFull(implode("\n", $lines));
269+
return $lines;
254270
}
255271

256272
/**

app/code/Magento/Customer/Test/Unit/Model/Address/AbstractAddressTest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ class AbstractAddressTest extends TestCase
7070
/** @var CompositeValidator|MockObject */
7171
private $compositeValidatorMock;
7272

73+
/**
74+
* @var \Magento\Customer\Helper\Address|MockObject
75+
*/
76+
private $addressHelperMock;
77+
7378
protected function setUp(): void
7479
{
7580
$this->contextMock = $this->createMock(Context::class);
@@ -491,6 +496,89 @@ function ($data) {
491496
);
492497
}
493498

499+
public function testGetStreetWithTwoLines()
500+
{
501+
// Create a partial mock for AddressHelper
502+
$this->addressHelperMock = $this->getMockBuilder(\Magento\Customer\Helper\Address::class)
503+
->disableOriginalConstructor()
504+
->onlyMethods(['getStreetLines']) // Mock only getStreetLines, keep the real convertStreetLines
505+
->getMock();
506+
507+
// Mock getStreetLines to return 2 by default
508+
$this->addressHelperMock->method('getStreetLines')->willReturn(2);
509+
510+
// Use reflection to inject the partial mock into the model
511+
$reflection = new \ReflectionClass($this->model);
512+
$property = $reflection->getProperty('addressHelper');
513+
$property->setAccessible(true);
514+
$property->setValue($this->model, $this->addressHelperMock);
515+
516+
$this->addressHelperMock->method('getStreetLines')->willReturn(2);
517+
$streetData = ["Street Line 1", "Street Line 2", "Street Line 3", "Street Line 4"];
518+
$this->model->setData('street', $streetData);
519+
520+
// Call getStreet() which should internally call convertStreetLines()
521+
$result = $this->model->getStreet();
522+
523+
// Assert that empty and whitespace-only lines are removed by convertStreetLines
524+
$this->assertEquals(["Street Line 1 Street Line 2", "Street Line 3 Street Line 4"], $result);
525+
}
526+
527+
public function testGetStreetWithThreeLines()
528+
{
529+
// Create a partial mock for AddressHelper
530+
$this->addressHelperMock = $this->getMockBuilder(\Magento\Customer\Helper\Address::class)
531+
->disableOriginalConstructor()
532+
->onlyMethods(['getStreetLines']) // Mock only getStreetLines, keep the real convertStreetLines
533+
->getMock();
534+
535+
// Mock getStreetLines to return 2 by default
536+
$this->addressHelperMock->method('getStreetLines')->willReturn(3);
537+
538+
// Use reflection to inject the partial mock into the model
539+
$reflection = new \ReflectionClass($this->model);
540+
$property = $reflection->getProperty('addressHelper');
541+
$property->setAccessible(true);
542+
$property->setValue($this->model, $this->addressHelperMock);
543+
544+
$this->addressHelperMock->method('getStreetLines')->willReturn(3);
545+
$streetData = ["Street Line 1", "Street Line 2", "Street Line 3", "Street Line 4"];
546+
$this->model->setData('street', $streetData);
547+
548+
// Call getStreet() which should internally call convertStreetLines()
549+
$result = $this->model->getStreet();
550+
551+
// Assert that empty and whitespace-only lines are removed by convertStreetLines
552+
$this->assertEquals(["Street Line 1 Street Line 2","Street Line 3","Street Line 4"], $result);
553+
}
554+
555+
public function testGetStreetWithOneLine()
556+
{
557+
// Create a partial mock for AddressHelper
558+
$this->addressHelperMock = $this->getMockBuilder(\Magento\Customer\Helper\Address::class)
559+
->disableOriginalConstructor()
560+
->onlyMethods(['getStreetLines']) // Mock only getStreetLines, keep the real convertStreetLines
561+
->getMock();
562+
563+
// Mock getStreetLines to return 2 by default
564+
$this->addressHelperMock->method('getStreetLines')->willReturn(1);
565+
566+
// Use reflection to inject the partial mock into the model
567+
$reflection = new \ReflectionClass($this->model);
568+
$property = $reflection->getProperty('addressHelper');
569+
$property->setAccessible(true);
570+
$property->setValue($this->model, $this->addressHelperMock);
571+
572+
$streetData = ["Street Line 1", "Street Line 2", "Street Line 3", "Street Line 4"];
573+
$this->model->setData('street', $streetData);
574+
575+
// Call getStreet() which should internally call convertStreetLines()
576+
$result = $this->model->getStreet();
577+
578+
// Assert that empty and whitespace-only lines are removed by convertStreetLines
579+
$this->assertEquals(["Street Line 1 Street Line 2 Street Line 3 Street Line 4"], $result);
580+
}
581+
494582
protected function tearDown(): void
495583
{
496584
$this->objectManager->setBackwardCompatibleProperty(

app/code/Magento/Customer/etc/di.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<type name="Magento\Customer\Helper\Address">
8989
<arguments>
9090
<argument name="addressConfig" xsi:type="object">Magento\Customer\Model\Address\Config\Proxy</argument>
91+
<argument name="addressHelper" xsi:type="object">Magento\Customer\Helper\Address</argument>
9192
</arguments>
9293
</type>
9394
<type name="Magento\Framework\App\Http\Context">
@@ -593,4 +594,9 @@
593594
type="Magento\Customer\Plugin\AsyncRequestCustomerGroupAuthorization"
594595
/>
595596
</type>
597+
<type name="Magento\Customer\Model\Address\AbstractAddress">
598+
<arguments>
599+
<argument name="addressHelper" xsi:type="object">Magento\Customer\Helper\Address</argument>
600+
</arguments>
601+
</type>
596602
</config>

app/code/Magento/Quote/Model/Quote/Address.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
use Magento\Store\Api\Data\StoreInterface;
4747
use Magento\Store\Model\ScopeInterface;
4848
use Magento\Store\Model\StoreManagerInterface;
49+
use Magento\Customer\Helper\Address as AddressHelper;
4950

5051
/**
5152
* Sales Quote address model
@@ -341,6 +342,7 @@ class Address extends AbstractAddress implements
341342
* @param CompositeValidator|null $compositeValidator
342343
* @param CountryModelsCache|null $countryModelsCache
343344
* @param RegionModelsCache|null $regionModelsCache
345+
* @param AddressHelper|null $addressHelper
344346
*
345347
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
346348
*/
@@ -382,6 +384,7 @@ public function __construct(
382384
?CompositeValidator $compositeValidator = null,
383385
?CountryModelsCache $countryModelsCache = null,
384386
?RegionModelsCache $regionModelsCache = null,
387+
?AddressHelper $addressHelper = null
385388
) {
386389
$this->_scopeConfig = $scopeConfig;
387390
$this->_addressItemFactory = $addressItemFactory;
@@ -422,6 +425,7 @@ public function __construct(
422425
$compositeValidator,
423426
$countryModelsCache,
424427
$regionModelsCache,
428+
$addressHelper
425429
);
426430
}
427431

app/code/Magento/Quote/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<type name="Magento\Quote\Model\Quote\Address">
6363
<arguments>
6464
<argument name="addressConfig" xsi:type="object">Magento\Customer\Model\Address\Config\Proxy</argument>
65+
<argument name="addressHelper" xsi:type="object">Magento\Customer\Helper\Address</argument>
6566
</arguments>
6667
</type>
6768
<virtualType name="QuoteAddressRelationsComposite" type="Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite">

0 commit comments

Comments
 (0)