Skip to content

Commit 63c8224

Browse files
committed
MC-31747: Storefront: Create/update/delete customer addresses
1 parent 04b376d commit 63c8224

File tree

3 files changed

+1011
-0
lines changed

3 files changed

+1011
-0
lines changed
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Controller\Address\Delete;
9+
10+
use Magento\Customer\Api\AddressRepositoryInterface;
11+
use Magento\Customer\Api\CustomerRepositoryInterface;
12+
use Magento\Customer\Api\Data\AddressInterface;
13+
use Magento\Customer\Model\Session;
14+
use Magento\Framework\App\Request\Http;
15+
use Magento\Framework\Escaper;
16+
use Magento\Framework\Exception\LocalizedException;
17+
use Magento\Framework\Message\MessageInterface;
18+
use Magento\TestFramework\TestCase\AbstractController;
19+
20+
/**
21+
* Test cases related to check that customer address correctly deleted on frontend
22+
* or wasn't deleted and proper error message appears.
23+
*
24+
* @magentoAppArea frontend
25+
* @magentoDbIsolation enabled
26+
*
27+
* @see \Magento\Customer\Controller\Address\Delete::execute
28+
*/
29+
class DeleteAddressTest extends AbstractController
30+
{
31+
/**
32+
* @var Escaper
33+
*/
34+
private $escaper;
35+
36+
/**
37+
* @var Session
38+
*/
39+
private $customerSession;
40+
41+
/**
42+
* @var AddressRepositoryInterface
43+
*/
44+
private $addressRepository;
45+
46+
/**
47+
* @var CustomerRepositoryInterface
48+
*/
49+
private $customerRepository;
50+
51+
/**
52+
* @inheritdoc
53+
*/
54+
protected function setUp()
55+
{
56+
parent::setUp();
57+
$this->escaper = $this->_objectManager->get(Escaper::class);
58+
$this->customerSession = $this->_objectManager->get(Session::class);
59+
$this->addressRepository = $this->_objectManager->get(AddressRepositoryInterface::class);
60+
$this->customerRepository = $this->_objectManager->get(CustomerRepositoryInterface::class);
61+
}
62+
63+
/**
64+
* Assert that customer address deleted successfully.
65+
*
66+
* @magentoDataFixture Magento/Customer/_files/customer.php
67+
* @magentoDataFixture Magento/Customer/_files/customer_address.php
68+
*
69+
* @return void
70+
*/
71+
public function testSuccessDeleteExistCustomerAddress(): void
72+
{
73+
$customer = $this->customerRepository->get('customer@example.com');
74+
$customerAddresses = $customer->getAddresses() ?? [];
75+
$this->assertCount(1, $customerAddresses);
76+
/** @var AddressInterface $currentCustomerAddress */
77+
$currentCustomerAddress = reset($customerAddresses);
78+
$this->customerSession->setCustomerId($customer->getId());
79+
$this->performAddressDeleteRequest((int)$currentCustomerAddress->getId());
80+
$this->checkRequestPerformedSuccessfully();
81+
$customer = $this->customerRepository->get('customer@example.com');
82+
$this->assertCount(0, $customer->getAddresses() ?? []);
83+
try {
84+
$this->addressRepository->getById((int)$currentCustomerAddress->getId());
85+
$this->fail('Customer address is not deleted.');
86+
} catch (LocalizedException $e) {
87+
//Do nothing, this block mean that address deleted successfully from DB.
88+
}
89+
}
90+
91+
/**
92+
* Check that customer address will not be deleted if we don't pass address ID parameter.
93+
*
94+
* @magentoDataFixture Magento/Customer/_files/customer.php
95+
* @magentoDataFixture Magento/Customer/_files/customer_address.php
96+
*
97+
* @return void
98+
*/
99+
public function testDeleteWithoutParam(): void
100+
{
101+
$customer = $this->customerRepository->get('customer@example.com');
102+
$customerAddresses = $customer->getAddresses() ?? [];
103+
$this->assertCount(1, $customerAddresses);
104+
/** @var AddressInterface $currentCustomerAddress */
105+
$currentCustomerAddress = reset($customerAddresses);
106+
$this->customerSession->setCustomerId($customer->getId());
107+
$this->performAddressDeleteRequest();
108+
$this->assertRedirect($this->stringContains('customer/address/index'));
109+
$customer = $this->customerRepository->get('customer@example.com');
110+
$this->assertCount(1, $customer->getAddresses() ?? []);
111+
$this->checkAddressWasntDeleted((int)$currentCustomerAddress->getId());
112+
}
113+
114+
/**
115+
* Check that customer address will not be deleted if customer id in address and in session are not equals.
116+
*
117+
* @magentoDataFixture Magento/Customer/_files/customer.php
118+
* @magentoDataFixture Magento/Customer/_files/customer_address.php
119+
* @magentoDataFixture Magento/Customer/_files/customer_with_uk_address.php
120+
*
121+
* @return void
122+
*/
123+
public function testDeleteDifferentCustomerAddress(): void
124+
{
125+
$firstCustomer = $this->customerRepository->get('customer@example.com');
126+
$customerAddresses = $firstCustomer->getAddresses() ?? [];
127+
$this->assertCount(1, $customerAddresses);
128+
/** @var AddressInterface $currentCustomerAddress */
129+
$currentCustomerAddress = reset($customerAddresses);
130+
$this->customerSession->setCustomerId('1');
131+
$secondCustomer = $this->customerRepository->get('customer_uk_address@test.com');
132+
$secondCustomerAddresses = $secondCustomer->getAddresses() ?? [];
133+
/** @var AddressInterface $secondCustomerAddress */
134+
$secondCustomerAddress = reset($secondCustomerAddresses);
135+
$this->performAddressDeleteRequest((int)$secondCustomerAddress->getId());
136+
$this->checkRequestPerformedWithError(true);
137+
$firstCustomer = $this->customerRepository->get('customer@example.com');
138+
$this->assertCount(1, $firstCustomer->getAddresses() ?? []);
139+
$this->checkAddressWasntDeleted((int)$currentCustomerAddress->getId());
140+
}
141+
142+
/**
143+
* Check that error message appear if we try to delete non-exits address.
144+
*
145+
* @magentoDataFixture Magento/Customer/_files/customer.php
146+
*
147+
* @return void
148+
*/
149+
public function testDeleteNonExistAddress(): void
150+
{
151+
$customer = $this->customerRepository->get('customer@example.com');
152+
$this->customerSession->setCustomerId($customer->getId());
153+
$this->performAddressDeleteRequest(999);
154+
$this->checkRequestPerformedWithError();
155+
}
156+
157+
/**
158+
* Perform delete request by provided address id.
159+
*
160+
* @param int|null $processAddressId
161+
* @return void
162+
*/
163+
private function performAddressDeleteRequest(?int $processAddressId = null): void
164+
{
165+
$this->getRequest()->setMethod(Http::METHOD_POST);
166+
if (null !== $processAddressId) {
167+
$this->getRequest()->setPostValue(['id' => $processAddressId]);
168+
}
169+
$this->dispatch('customer/address/delete');
170+
}
171+
172+
/**
173+
* Check that delete address request performed successfully
174+
* (proper success message and redirect to customer/address/index are appear).
175+
*
176+
* @return void
177+
*/
178+
private function checkRequestPerformedSuccessfully(): void
179+
{
180+
$this->assertRedirect($this->stringContains('customer/address/index'));
181+
$this->assertSessionMessages(
182+
$this->equalTo([(string)__('You deleted the address.')]),
183+
MessageInterface::TYPE_SUCCESS
184+
);
185+
}
186+
187+
/**
188+
* Check that delete address request performed with error.
189+
* (proper error messages and redirect to customer/address/edit are appear).
190+
*
191+
* @param bool $isNeedEscapeMessage
192+
* @return void
193+
*/
194+
private function checkRequestPerformedWithError(bool $isNeedEscapeMessage = false): void
195+
{
196+
$message = (string)__("We can't delete the address right now.");
197+
if ($isNeedEscapeMessage) {
198+
$message = $this->escaper->escapeHtml($message);
199+
}
200+
$this->assertSessionMessages($this->contains($message), MessageInterface::TYPE_ERROR);
201+
}
202+
203+
/**
204+
* Assert that customer address wasn't deleted.
205+
*
206+
* @param int $addressId
207+
* @return void
208+
*/
209+
private function checkAddressWasntDeleted(int $addressId): void
210+
{
211+
try {
212+
$this->addressRepository->getById($addressId);
213+
} catch (LocalizedException $e) {
214+
$this->fail('Expects that customer address will not be deleted.');
215+
}
216+
}
217+
}

0 commit comments

Comments
 (0)