Skip to content

Commit 34416b1

Browse files
committed
MAGETWO-94347: Implement handling of large number of addresses on storefront Address book
1 parent 686a6a5 commit 34416b1

File tree

3 files changed

+70
-49
lines changed
  • app/code/Magento/Customer
  • dev/tests/integration/testsuite/Magento/Customer/Block/Address

3 files changed

+70
-49
lines changed

app/code/Magento/Customer/Block/Address/Book.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public function getAdditionalAddresses()
177177
$primaryAddressIds = [$this->getDefaultBilling(), $this->getDefaultShipping()];
178178
foreach ($addresses as $address) {
179179
if (!in_array($address->getId(), $primaryAddressIds)) {
180-
$additional[] = $address;
180+
$additional[] = $address->getDataModel();
181181
}
182182
}
183183
return empty($additional) ? false : $additional;

app/code/Magento/Customer/view/frontend/templates/address/book.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
<td data-th="<?= $block->escapeHtml(__('Street Address')) ?>" class="col streetaddress"><?= $block->getStreetAddress($address->getStreet()) ?></td>
9393
<td data-th="<?= $block->escapeHtml(__('City')) ?>" class="col city"><?= /* @escapeNotVerified */ $address->getCity() ?></td>
9494
<td data-th="<?= $block->escapeHtml(__('Country')) ?>" class="col country"><?= /* @escapeNotVerified */ $block->getCountryById($address->getCountryId()) ?></td>
95-
<td data-th="<?= $block->escapeHtml(__('State')) ?>" class="col state"><?= /* @escapeNotVerified */ $address->getRegion() ?></td>
95+
<td data-th="<?= $block->escapeHtml(__('State')) ?>" class="col state"><?= /* @escapeNotVerified */ $address->getRegion()->getRegion() ?></td>
9696
<td data-th="<?= $block->escapeHtml(__('Zip/Postal Code')) ?>" class="col zip"><?= /* @escapeNotVerified */ $address->getPostcode() ?></td>
9797
<td data-th="<?= $block->escapeHtml(__('Phone')) ?>" class="col phone"><?= /* @escapeNotVerified */ $address->getTelephone() ?></td>
9898
<td data-th="<?= $block->escapeHtml(__('Actions')) ?>" class="col actions">

dev/tests/integration/testsuite/Magento/Customer/Block/Address/BookTest.php

Lines changed: 68 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
class BookTest extends \PHPUnit\Framework\TestCase
1212
{
1313
/**
14-
* @var \Magento\Customer\Block\Address\Book
14+
* @var \Magento\Framework\View\LayoutInterface
1515
*/
16-
protected $_block;
16+
private $layout;
1717

1818
/**
1919
* @var \Magento\Customer\Helper\Session\CurrentCustomer
@@ -32,15 +32,8 @@ protected function setUp()
3232

3333
$this->currentCustomer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
3434
->get(\Magento\Customer\Helper\Session\CurrentCustomer::class);
35-
/** @var \Magento\Framework\View\LayoutInterface $layout */
36-
$layout = Bootstrap::getObjectManager()->get(\Magento\Framework\View\LayoutInterface::class);
37-
$layout->setBlock('head', $blockMock);
38-
$this->_block = $layout
39-
->createBlock(
40-
\Magento\Customer\Block\Address\Book::class,
41-
'',
42-
['currentCustomer' => $this->currentCustomer]
43-
);
35+
$this->layout = Bootstrap::getObjectManager()->get(\Magento\Framework\View\LayoutInterface::class);
36+
$this->layout->setBlock('head', $blockMock);
4437
}
4538

4639
protected function tearDown()
@@ -52,11 +45,17 @@ protected function tearDown()
5245
$customerRegistry->remove(1);
5346
}
5447

48+
/**
49+
* @magentoDataFixture Magento/Customer/_files/customer.php
50+
* @magentoAppIsolation enabled
51+
*/
5552
public function testGetAddressEditUrl()
5653
{
54+
$bookBlock = $this->createBlockForCustomer(1);
55+
5756
$this->assertEquals(
5857
'http://localhost/index.php/customer/address/edit/id/1/',
59-
$this->_block->getAddressEditUrl(1)
58+
$bookBlock->getAddressEditUrl(1)
6059
);
6160
}
6261

@@ -65,137 +64,159 @@ public function testGetAddressEditUrl()
6564
* @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
6665
* @magentoDataFixture Magento/Customer/_files/customer_no_address.php
6766
* @dataProvider hasPrimaryAddressDataProvider
67+
* @param int $customerId
68+
* @param bool $expected
69+
* @magentoAppIsolation enabled
6870
*/
6971
public function testHasPrimaryAddress($customerId, $expected)
7072
{
71-
if (!empty($customerId)) {
72-
$this->currentCustomer->setCustomerId($customerId);
73-
}
74-
$this->assertEquals($expected, $this->_block->hasPrimaryAddress());
73+
$bookBlock = $this->createBlockForCustomer($customerId);
74+
$this->assertEquals($expected, $bookBlock->hasPrimaryAddress());
7575
}
7676

7777
public function hasPrimaryAddressDataProvider()
7878
{
79-
return ['0' => [0, false], '1' => [1, true], '5' => [5, false]];
79+
return ['1' => [1, true], '5' => [5, false]];
8080
}
8181

8282
/**
8383
* @magentoDataFixture Magento/Customer/_files/customer.php
8484
* @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
85+
* @magentoAppIsolation enabled
8586
*/
8687
public function testGetAdditionalAddresses()
8788
{
88-
$this->currentCustomer->setCustomerId(1);
89-
$this->assertNotNull($this->_block->getAdditionalAddresses());
90-
$this->assertCount(1, $this->_block->getAdditionalAddresses());
89+
$bookBlock = $this->createBlockForCustomer(1);
90+
$this->assertNotNull($bookBlock->getAdditionalAddresses());
91+
$this->assertCount(1, $bookBlock->getAdditionalAddresses());
9192
$this->assertInstanceOf(
9293
\Magento\Customer\Api\Data\AddressInterface::class,
93-
$this->_block->getAdditionalAddresses()[0]
94+
$bookBlock->getAdditionalAddresses()[0]
9495
);
95-
$this->assertEquals(2, $this->_block->getAdditionalAddresses()[0]->getId());
96+
$this->assertEquals(2, $bookBlock->getAdditionalAddresses()[0]->getId());
9697
}
9798

9899
/**
99100
* @magentoDataFixture Magento/Customer/_files/customer_no_address.php
100101
* @dataProvider getAdditionalAddressesDataProvider
102+
* @magentoAppIsolation enabled
101103
*/
102104
public function testGetAdditionalAddressesNegative($customerId, $expected)
103105
{
104-
if (!empty($customerId)) {
105-
$this->currentCustomer->setCustomerId($customerId);
106-
}
107-
$this->assertEquals($expected, $this->_block->getAdditionalAddresses());
106+
$bookBlock = $this->createBlockForCustomer($customerId);
107+
$this->currentCustomer->setCustomerId($customerId);
108+
$this->assertEquals($expected, $bookBlock->getAdditionalAddresses());
108109
}
109110

110111
public function getAdditionalAddressesDataProvider()
111112
{
112-
return ['0' => [0, false], '5' => [5, false]];
113+
return ['5' => [5, false]];
113114
}
114115

115116
/**
116117
* @magentoDataFixture Magento/Customer/_files/customer.php
117118
* @magentoDataFixture Magento/Customer/_files/customer_address.php
119+
* @magentoAppIsolation enabled
118120
*/
119121
public function testGetAddressHtml()
120122
{
123+
$bookBlock = $this->createBlockForCustomer(1);
121124
$expected = "John Smith<br />\nCompanyName<br />\nGreen str, 67<br />\n\n\n\nCityM, Alabama, 75477<br />" .
122125
"\nUnited States<br />\nT: <a href=\"tel:3468676\">3468676</a>\n\n";
123126
$address = Bootstrap::getObjectManager()->get(
124127
\Magento\Customer\Api\AddressRepositoryInterface::class
125128
)->getById(1);
126-
$html = $this->_block->getAddressHtml($address);
129+
$html = $bookBlock->getAddressHtml($address);
127130
$this->assertEquals($expected, $html);
128131
}
129132

133+
/**
134+
* @magentoDataFixture Magento/Customer/_files/customer_no_address.php
135+
* @magentoAppIsolation enabled
136+
*/
130137
public function testGetAddressHtmlWithoutAddress()
131138
{
132-
$this->assertEquals('', $this->_block->getAddressHtml(null));
139+
$bookBlock = $this->createBlockForCustomer(5);
140+
$this->assertEquals('', $bookBlock->getAddressHtml(null));
133141
}
134142

135143
/**
136144
* @magentoDataFixture Magento/Customer/_files/customer.php
145+
* @magentoAppIsolation enabled
137146
*/
138147
public function testGetCustomer()
139148
{
149+
$bookBlock = $this->createBlockForCustomer(1);
140150
/** @var CustomerRepositoryInterface $customerRepository */
141151
$customerRepository = Bootstrap::getObjectManager()->get(
142152
\Magento\Customer\Api\CustomerRepositoryInterface::class
143153
);
144154
$customer = $customerRepository->getById(1);
145-
146-
$this->currentCustomer->setCustomerId(1);
147-
$object = $this->_block->getCustomer();
155+
$object = $bookBlock->getCustomer();
148156
$this->assertEquals($customer, $object);
149157
}
150158

151-
public function testGetCustomerMissingCustomer()
152-
{
153-
$this->assertNull($this->_block->getCustomer());
154-
}
155-
156159
/**
157160
* @magentoDataFixture Magento/Customer/_files/customer.php
158161
* @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
159162
* @magentoDataFixture Magento/Customer/_files/customer_no_address.php
160163
* @dataProvider getDefaultBillingDataProvider
164+
* @magentoAppIsolation enabled
161165
*/
162166
public function testGetDefaultBilling($customerId, $expected)
163167
{
164-
$this->currentCustomer->setCustomerId($customerId);
165-
$this->assertEquals($expected, $this->_block->getDefaultBilling());
168+
$bookBlock = $this->createBlockForCustomer($customerId);
169+
$this->assertEquals($expected, $bookBlock->getDefaultBilling());
166170
}
167171

168172
public function getDefaultBillingDataProvider()
169173
{
170-
return ['0' => [0, null], '1' => [1, 1], '5' => [5, null]];
174+
return ['1' => [1, 1], '5' => [5, null]];
171175
}
172176

173177
/**
174178
* @magentoDataFixture Magento/Customer/_files/customer.php
175179
* @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
176180
* @magentoDataFixture Magento/Customer/_files/customer_no_address.php
177181
* @dataProvider getDefaultShippingDataProvider
182+
* @magentoAppIsolation enabled
178183
*/
179184
public function testGetDefaultShipping($customerId, $expected)
180185
{
181-
if (!empty($customerId)) {
182-
$this->currentCustomer->setCustomerId($customerId);
183-
}
184-
$this->assertEquals($expected, $this->_block->getDefaultShipping());
186+
$bookBlock = $this->createBlockForCustomer($customerId);
187+
$this->currentCustomer->setCustomerId($customerId);
188+
$this->assertEquals($expected, $bookBlock->getDefaultShipping());
185189
}
186190

187191
public function getDefaultShippingDataProvider()
188192
{
189-
return ['0' => [0, null], '1' => [1, 1], '5' => [5, null]];
193+
return ['1' => [1, 1], '5' => [5, null]];
190194
}
191195

192196
/**
193197
* @magentoDataFixture Magento/Customer/_files/customer.php
194198
* @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
199+
* @magentoAppIsolation enabled
195200
*/
196201
public function testGetAddressById()
197202
{
198-
$this->assertInstanceOf(\Magento\Customer\Api\Data\AddressInterface::class, $this->_block->getAddressById(1));
199-
$this->assertNull($this->_block->getAddressById(5));
203+
$bookBlock = $this->createBlockForCustomer(1);
204+
$this->assertInstanceOf(\Magento\Customer\Api\Data\AddressInterface::class, $bookBlock->getAddressById(1));
205+
}
206+
207+
/**
208+
* Create address book block for customer
209+
*
210+
* @param int $customerId
211+
* @return \Magento\Framework\View\Element\BlockInterface
212+
*/
213+
private function createBlockForCustomer($customerId)
214+
{
215+
$this->currentCustomer->setCustomerId($customerId);
216+
return $this->layout->createBlock(
217+
\Magento\Customer\Block\Address\Book::class,
218+
'',
219+
['currentCustomer' => $this->currentCustomer]
220+
);
200221
}
201222
}

0 commit comments

Comments
 (0)