|
3 | 3 | * Copyright © Magento, Inc. All rights reserved.
|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
| 6 | +declare(strict_types=1); |
| 7 | + |
6 | 8 | namespace Magento\Customer\Block\Address;
|
7 | 9 |
|
| 10 | +use Magento\Customer\Model\AddressRegistry; |
| 11 | +use Magento\Customer\Model\CustomerRegistry; |
| 12 | +use Magento\Customer\Model\Session; |
| 13 | +use Magento\Framework\App\RequestInterface; |
| 14 | +use Magento\Framework\ObjectManagerInterface; |
| 15 | +use Magento\Framework\View\Result\Page; |
| 16 | +use Magento\Framework\View\Result\PageFactory; |
| 17 | +use Magento\TestFramework\Helper\Bootstrap; |
| 18 | +use Magento\TestFramework\Helper\Xpath; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
8 | 21 | /**
|
9 | 22 | * Tests Address Edit Block
|
| 23 | + * |
| 24 | + * @magentoAppArea frontend |
| 25 | + * @magentoAppIsolation enabled |
10 | 26 | */
|
11 |
| -class EditTest extends \PHPUnit\Framework\TestCase |
| 27 | +class EditTest extends TestCase |
12 | 28 | {
|
| 29 | + /** @var ObjectManagerInterface */ |
| 30 | + private $objectManager; |
| 31 | + |
13 | 32 | /** @var Edit */
|
14 |
| - protected $_block; |
| 33 | + private $block; |
15 | 34 |
|
16 |
| - /** @var \Magento\Customer\Model\Session */ |
17 |
| - protected $_customerSession; |
| 35 | + /** @var Session */ |
| 36 | + private $customerSession; |
18 | 37 |
|
19 |
| - /** @var \Magento\Backend\Block\Template\Context */ |
20 |
| - protected $_context; |
| 38 | + /** @var AddressRegistry */ |
| 39 | + private $addressRegistry; |
21 | 40 |
|
22 |
| - /** @var string */ |
23 |
| - protected $_requestId; |
| 41 | + /** @var CustomerRegistry */ |
| 42 | + private $customerRegistry; |
24 | 43 |
|
| 44 | + /** @var RequestInterface */ |
| 45 | + private $request; |
| 46 | + |
| 47 | + /** |
| 48 | + * @inheritdoc |
| 49 | + */ |
25 | 50 | protected function setUp(): void
|
26 | 51 | {
|
27 |
| - $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
28 |
| - |
29 |
| - $this->_customerSession = $objectManager->get(\Magento\Customer\Model\Session::class); |
30 |
| - $this->_customerSession->setCustomerId(1); |
31 |
| - |
32 |
| - $this->_context = $objectManager->get(\Magento\Backend\Block\Template\Context::class); |
33 |
| - $this->_requestId = $this->_context->getRequest()->getParam('id'); |
34 |
| - $this->_context->getRequest()->setParam('id', '1'); |
35 |
| - |
36 |
| - $objectManager->get(\Magento\Framework\App\State::class)->setAreaCode('frontend'); |
37 |
| - |
38 |
| - /** @var $layout \Magento\Framework\View\Layout */ |
39 |
| - $layout = $objectManager->get(\Magento\Framework\View\LayoutInterface::class); |
40 |
| - $currentCustomer = $objectManager->create( |
41 |
| - \Magento\Customer\Helper\Session\CurrentCustomer::class, |
42 |
| - ['customerSession' => $this->_customerSession] |
43 |
| - ); |
44 |
| - $this->_block = $layout->createBlock( |
45 |
| - \Magento\Customer\Block\Address\Edit::class, |
46 |
| - '', |
47 |
| - ['customerSession' => $this->_customerSession, 'currentCustomer' => $currentCustomer] |
48 |
| - ); |
| 52 | + parent::setUp(); |
| 53 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 54 | + $this->customerSession = $this->objectManager->get(Session::class); |
| 55 | + $this->customerSession->setCustomerId(1); |
| 56 | + $this->request = $this->objectManager->get(RequestInterface::class); |
| 57 | + $this->request->setParam('id', '1'); |
| 58 | + /** @var Page $page */ |
| 59 | + $page = $this->objectManager->get(PageFactory::class)->create(); |
| 60 | + $page->addHandle(['default', 'customer_address_form']); |
| 61 | + $page->getLayout()->generateXml(); |
| 62 | + $this->block = $page->getLayout()->getBlock('customer_address_edit'); |
| 63 | + $this->addressRegistry = $this->objectManager->get(AddressRegistry::class); |
| 64 | + $this->customerRegistry = $this->objectManager->get(CustomerRegistry::class); |
49 | 65 | }
|
50 | 66 |
|
| 67 | + /** |
| 68 | + * @inheritdoc |
| 69 | + */ |
51 | 70 | protected function tearDown(): void
|
52 | 71 | {
|
53 |
| - $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
54 |
| - $this->_customerSession->setCustomerId(null); |
55 |
| - $this->_context->getRequest()->setParam('id', $this->_requestId); |
56 |
| - /** @var \Magento\Customer\Model\AddressRegistry $addressRegistry */ |
57 |
| - $addressRegistry = $objectManager->get(\Magento\Customer\Model\AddressRegistry::class); |
| 72 | + parent::tearDown(); |
| 73 | + $this->customerSession->setCustomerId(null); |
| 74 | + $this->request->setParam('id', null); |
58 | 75 | //Cleanup address from registry
|
59 |
| - $addressRegistry->remove(1); |
60 |
| - $addressRegistry->remove(2); |
61 |
| - |
62 |
| - /** @var \Magento\Customer\Model\CustomerRegistry $customerRegistry */ |
63 |
| - $customerRegistry = $objectManager->get(\Magento\Customer\Model\CustomerRegistry::class); |
| 76 | + $this->addressRegistry->remove(1); |
| 77 | + $this->addressRegistry->remove(2); |
64 | 78 | //Cleanup customer from registry
|
65 |
| - $customerRegistry->remove(1); |
| 79 | + $this->customerRegistry->remove(1); |
66 | 80 | }
|
67 | 81 |
|
68 | 82 | /**
|
69 | 83 | * @magentoDataFixture Magento/Customer/_files/customer.php
|
| 84 | + * @return void |
70 | 85 | */
|
71 |
| - public function testGetSaveUrl() |
| 86 | + public function testGetSaveUrl(): void |
72 | 87 | {
|
73 |
| - $this->assertEquals('http://localhost/index.php/customer/address/formPost/', $this->_block->getSaveUrl()); |
| 88 | + $this->assertEquals('http://localhost/index.php/customer/address/formPost/', $this->block->getSaveUrl()); |
74 | 89 | }
|
75 | 90 |
|
76 | 91 | /**
|
77 | 92 | * @magentoDataFixture Magento/Customer/_files/customer.php
|
78 | 93 | * @magentoDataFixture Magento/Customer/_files/customer_address.php
|
| 94 | + * @return void |
79 | 95 | */
|
80 |
| - public function testGetRegionId() |
| 96 | + public function testGetRegionId(): void |
81 | 97 | {
|
82 |
| - $this->assertEquals(1, $this->_block->getRegionId()); |
| 98 | + $this->assertEquals(1, $this->block->getRegionId()); |
83 | 99 | }
|
84 | 100 |
|
85 | 101 | /**
|
86 | 102 | * @magentoDataFixture Magento/Customer/_files/customer.php
|
87 | 103 | * @magentoDataFixture Magento/Customer/_files/customer_address.php
|
| 104 | + * @return void |
88 | 105 | */
|
89 |
| - public function testGetCountryId() |
| 106 | + public function testGetCountryId(): void |
90 | 107 | {
|
91 |
| - $this->assertEquals('US', $this->_block->getCountryId()); |
| 108 | + $this->assertEquals('US', $this->block->getCountryId()); |
92 | 109 | }
|
93 | 110 |
|
94 | 111 | /**
|
95 | 112 | * @magentoDataFixture Magento/Customer/_files/customer.php
|
96 | 113 | * @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
|
| 114 | + * @return void |
97 | 115 | */
|
98 |
| - public function testGetCustomerAddressCount() |
| 116 | + public function testGetCustomerAddressCount(): void |
99 | 117 | {
|
100 |
| - $this->assertEquals(2, $this->_block->getCustomerAddressCount()); |
| 118 | + $this->assertEquals(2, $this->block->getCustomerAddressCount()); |
101 | 119 | }
|
102 | 120 |
|
103 | 121 | /**
|
104 | 122 | * @magentoDataFixture Magento/Customer/_files/customer.php
|
| 123 | + * @return void |
105 | 124 | */
|
106 |
| - public function testCanSetAsDefaultShipping() |
| 125 | + public function testCanSetAsDefaultShipping(): void |
107 | 126 | {
|
108 |
| - $this->assertEquals(0, $this->_block->canSetAsDefaultShipping()); |
| 127 | + $this->assertEquals(0, $this->block->canSetAsDefaultShipping()); |
109 | 128 | }
|
110 | 129 |
|
111 | 130 | /**
|
112 | 131 | * @magentoDataFixture Magento/Customer/_files/customer.php
|
| 132 | + * @return void |
113 | 133 | */
|
114 |
| - public function testIsDefaultBilling() |
| 134 | + public function testIsDefaultBilling(): void |
115 | 135 | {
|
116 |
| - $this->assertFalse($this->_block->isDefaultBilling()); |
| 136 | + $this->assertFalse($this->block->isDefaultBilling()); |
117 | 137 | }
|
118 | 138 |
|
119 | 139 | /**
|
120 | 140 | * @magentoDataFixture Magento/Customer/_files/customer.php
|
121 | 141 | * @magentoDataFixture Magento/Customer/_files/customer_address.php
|
| 142 | + * @return void |
| 143 | + */ |
| 144 | + public function testGetStreetLine(): void |
| 145 | + { |
| 146 | + $this->assertEquals('Green str, 67', $this->block->getStreetLine(1)); |
| 147 | + $this->assertEquals('', $this->block->getStreetLine(2)); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * @magentoDataFixture Magento/Customer/_files/customer.php |
| 152 | + * @magentoConfigFixture current_store customer/create_account/vat_frontend_visibility 1 |
| 153 | + * @return void |
| 154 | + */ |
| 155 | + public function testVatIdFieldVisible(): void |
| 156 | + { |
| 157 | + $html = $this->block->toHtml(); |
| 158 | + $labelXpath = "//div[contains(@class, 'taxvat')]//label/span[normalize-space(text()) = '%s']"; |
| 159 | + $this->assertEquals(1, Xpath::getElementsCountForXpath(sprintf($labelXpath, __('VAT Number')), $html)); |
| 160 | + $inputXpath = "//div[contains(@class, 'taxvat')]//div/input[contains(@id,'vat_id') and @type='text']"; |
| 161 | + $this->assertEquals(1, Xpath::getElementsCountForXpath($inputXpath, $html)); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * @magentoDataFixture Magento/Customer/_files/customer.php |
| 166 | + * @magentoConfigFixture current_store customer/create_account/vat_frontend_visibility 0 |
| 167 | + * @return void |
122 | 168 | */
|
123 |
| - public function testGetStreetLine() |
| 169 | + public function testVatIdFieldNotVisible(): void |
124 | 170 | {
|
125 |
| - $this->assertEquals('Green str, 67', $this->_block->getStreetLine(1)); |
126 |
| - $this->assertEquals('', $this->_block->getStreetLine(2)); |
| 171 | + $html = $this->block->toHtml(); |
| 172 | + $labelXpath = "//div[contains(@class, 'taxvat')]//label/span[normalize-space(text()) = '%s']"; |
| 173 | + $this->assertEquals(0, Xpath::getElementsCountForXpath(sprintf($labelXpath, __('VAT Number')), $html)); |
| 174 | + $inputXpath = "//div[contains(@class, 'taxvat')]//div/input[contains(@id,'vat_id') and @type='text']"; |
| 175 | + $this->assertEquals(0, Xpath::getElementsCountForXpath($inputXpath, $html)); |
127 | 176 | }
|
128 | 177 | }
|
0 commit comments