Skip to content

Commit

Permalink
Cherry-pick of pull request #117 from ProxiBlue/2.4-develop
Browse files Browse the repository at this point in the history
  • Loading branch information
rhoerr authored and fballiano committed Feb 5, 2025
1 parent 4eb2b41 commit c0bbd6f
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app/code/Magento/Customer/Model/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\Customer\Model\Address\AbstractAddress\RegionModelsCache;
use Magento\Customer\Model\Address\CompositeValidator;
use Magento\Framework\Indexer\StateInterface;
use Magento\Customer\Helper\Address as AddressHelper;

/**
* Customer address model
Expand Down Expand Up @@ -80,6 +81,7 @@ class Address extends \Magento\Customer\Model\Address\AbstractAddress
* @param CompositeValidator|null $compositeValidator
* @param CountryModelsCache|null $countryModelsCache
* @param RegionModelsCache|null $regionModelsCache
* @param AddressHelper|null $addressHelper
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
Expand All @@ -106,6 +108,7 @@ public function __construct(
?CompositeValidator $compositeValidator = null,
?CountryModelsCache $countryModelsCache = null,
?RegionModelsCache $regionModelsCache = null,
?AddressHelper $addressHelper = null
) {
$this->dataProcessor = $dataProcessor;
$this->_customerFactory = $customerFactory;
Expand All @@ -130,6 +133,7 @@ public function __construct(
$compositeValidator,
$countryModelsCache,
$regionModelsCache,
$addressHelper
);
}

Expand Down
18 changes: 17 additions & 1 deletion app/code/Magento/Customer/Model/Address/AbstractAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Model\AbstractExtensibleModel;
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
use Magento\Customer\Helper\Address as AddressHelper;

/**
* Address abstract model
Expand Down Expand Up @@ -146,6 +147,11 @@ class AbstractAddress extends AbstractExtensibleModel implements AddressModelInt
*/
private array $regionIdCountry = [];

/**
* @var AddressHelper|null
*/
protected ?AddressHelper $addressHelper;

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
Expand All @@ -166,6 +172,7 @@ class AbstractAddress extends AbstractExtensibleModel implements AddressModelInt
* @param CompositeValidator $compositeValidator
* @param CountryModelsCache|null $countryModelsCache
* @param RegionModelsCache|null $regionModelsCache
* @param AddressHelper|null $addressHelper
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
Expand All @@ -189,6 +196,7 @@ public function __construct(
CompositeValidator $compositeValidator = null,
?CountryModelsCache $countryModelsCache = null,
?RegionModelsCache $regionModelsCache = null,
?AddressHelper $addressHelper = null
) {
$this->_directoryData = $directoryData;
$data = $this->_implodeArrayField($data);
Expand All @@ -206,6 +214,8 @@ public function __construct(
->get(CountryModelsCache::class);
$this->regionModelsCache = $regionModelsCache ?: ObjectManager::getInstance()
->get(RegionModelsCache::class);
$this->addressHelper = $addressHelper ?: ObjectManager::getInstance()
->get(AddressHelper::class);
parent::__construct(
$context,
$registry,
Expand Down Expand Up @@ -242,6 +252,8 @@ public function getName()

/**
* Retrieve street field of an address
* Honour current configured street lines, and convert
* legacy data to match
*
* @return string[]
*/
Expand All @@ -250,7 +262,11 @@ public function getStreet()
if (is_array($this->getStreetFull())) {
return $this->getStreetFull();
}
return explode("\n", $this->getStreetFull());
$maxAllowedLineCount = $this->addressHelper->getStreetLines() ?? 2;
$lines = explode("\n", $this->getStreetFull());
$lines = $this->addressHelper->convertStreetLines($lines, $maxAllowedLineCount);
$this->setStreetFull(implode("\n", $lines));
return $lines;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ class AbstractAddressTest extends TestCase
/** @var CompositeValidator|MockObject */
private $compositeValidatorMock;

/**
* @var \Magento\Customer\Helper\Address|MockObject
*/
private $addressHelperMock;

protected function setUp(): void
{
$this->contextMock = $this->createMock(Context::class);
Expand Down Expand Up @@ -491,6 +496,89 @@ function ($data) {
);
}

public function testGetStreetWithTwoLines()
{
// Create a partial mock for AddressHelper
$this->addressHelperMock = $this->getMockBuilder(\Magento\Customer\Helper\Address::class)
->disableOriginalConstructor()
->onlyMethods(['getStreetLines']) // Mock only getStreetLines, keep the real convertStreetLines
->getMock();

// Mock getStreetLines to return 2 by default
$this->addressHelperMock->method('getStreetLines')->willReturn(2);

// Use reflection to inject the partial mock into the model
$reflection = new \ReflectionClass($this->model);
$property = $reflection->getProperty('addressHelper');
$property->setAccessible(true);
$property->setValue($this->model, $this->addressHelperMock);

$this->addressHelperMock->method('getStreetLines')->willReturn(2);
$streetData = ["Street Line 1", "Street Line 2", "Street Line 3", "Street Line 4"];
$this->model->setData('street', $streetData);

// Call getStreet() which should internally call convertStreetLines()
$result = $this->model->getStreet();

// Assert that empty and whitespace-only lines are removed by convertStreetLines
$this->assertEquals(["Street Line 1 Street Line 2", "Street Line 3 Street Line 4"], $result);
}

public function testGetStreetWithThreeLines()
{
// Create a partial mock for AddressHelper
$this->addressHelperMock = $this->getMockBuilder(\Magento\Customer\Helper\Address::class)
->disableOriginalConstructor()
->onlyMethods(['getStreetLines']) // Mock only getStreetLines, keep the real convertStreetLines
->getMock();

// Mock getStreetLines to return 2 by default
$this->addressHelperMock->method('getStreetLines')->willReturn(3);

// Use reflection to inject the partial mock into the model
$reflection = new \ReflectionClass($this->model);
$property = $reflection->getProperty('addressHelper');
$property->setAccessible(true);
$property->setValue($this->model, $this->addressHelperMock);

$this->addressHelperMock->method('getStreetLines')->willReturn(3);
$streetData = ["Street Line 1", "Street Line 2", "Street Line 3", "Street Line 4"];
$this->model->setData('street', $streetData);

// Call getStreet() which should internally call convertStreetLines()
$result = $this->model->getStreet();

// Assert that empty and whitespace-only lines are removed by convertStreetLines
$this->assertEquals(["Street Line 1 Street Line 2","Street Line 3","Street Line 4"], $result);
}

public function testGetStreetWithOneLine()
{
// Create a partial mock for AddressHelper
$this->addressHelperMock = $this->getMockBuilder(\Magento\Customer\Helper\Address::class)
->disableOriginalConstructor()
->onlyMethods(['getStreetLines']) // Mock only getStreetLines, keep the real convertStreetLines
->getMock();

// Mock getStreetLines to return 2 by default
$this->addressHelperMock->method('getStreetLines')->willReturn(1);

// Use reflection to inject the partial mock into the model
$reflection = new \ReflectionClass($this->model);
$property = $reflection->getProperty('addressHelper');
$property->setAccessible(true);
$property->setValue($this->model, $this->addressHelperMock);

$streetData = ["Street Line 1", "Street Line 2", "Street Line 3", "Street Line 4"];
$this->model->setData('street', $streetData);

// Call getStreet() which should internally call convertStreetLines()
$result = $this->model->getStreet();

// Assert that empty and whitespace-only lines are removed by convertStreetLines
$this->assertEquals(["Street Line 1 Street Line 2 Street Line 3 Street Line 4"], $result);
}

protected function tearDown(): void
{
$this->objectManager->setBackwardCompatibleProperty(
Expand Down
6 changes: 6 additions & 0 deletions app/code/Magento/Customer/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<type name="Magento\Customer\Helper\Address">
<arguments>
<argument name="addressConfig" xsi:type="object">Magento\Customer\Model\Address\Config\Proxy</argument>
<argument name="addressHelper" xsi:type="object">Magento\Customer\Helper\Address</argument>
</arguments>
</type>
<type name="Magento\Framework\App\Http\Context">
Expand Down Expand Up @@ -593,4 +594,9 @@
type="Magento\Customer\Plugin\AsyncRequestCustomerGroupAuthorization"
/>
</type>
<type name="Magento\Customer\Model\Address\AbstractAddress">
<arguments>
<argument name="addressHelper" xsi:type="object">Magento\Customer\Helper\Address</argument>
</arguments>
</type>
</config>
4 changes: 4 additions & 0 deletions app/code/Magento/Quote/Model/Quote/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Customer\Helper\Address as AddressHelper;

/**
* Sales Quote address model
Expand Down Expand Up @@ -341,6 +342,7 @@ class Address extends AbstractAddress implements
* @param CompositeValidator|null $compositeValidator
* @param CountryModelsCache|null $countryModelsCache
* @param RegionModelsCache|null $regionModelsCache
* @param AddressHelper|null $addressHelper
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
Expand Down Expand Up @@ -382,6 +384,7 @@ public function __construct(
?CompositeValidator $compositeValidator = null,
?CountryModelsCache $countryModelsCache = null,
?RegionModelsCache $regionModelsCache = null,
?AddressHelper $addressHelper = null
) {
$this->_scopeConfig = $scopeConfig;
$this->_addressItemFactory = $addressItemFactory;
Expand Down Expand Up @@ -422,6 +425,7 @@ public function __construct(
$compositeValidator,
$countryModelsCache,
$regionModelsCache,
$addressHelper
);
}

Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Quote/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<type name="Magento\Quote\Model\Quote\Address">
<arguments>
<argument name="addressConfig" xsi:type="object">Magento\Customer\Model\Address\Config\Proxy</argument>
<argument name="addressHelper" xsi:type="object">Magento\Customer\Helper\Address</argument>
</arguments>
</type>
<virtualType name="QuoteAddressRelationsComposite" type="Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite">
Expand Down

0 comments on commit c0bbd6f

Please sign in to comment.