Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve bug #116 - Setting number of address line lower breaks checkout #117

Open
wants to merge 4 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 5 additions & 0 deletions app/code/Magento/Customer/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -593,4 +593,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>
Loading