AddressTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Controller;
  7. use Magento\Customer\Api\AccountManagementInterface;
  8. use Magento\Customer\Model\CustomerRegistry;
  9. use Magento\Framework\Data\Form\FormKey;
  10. use Magento\TestFramework\Helper\Bootstrap;
  11. use Magento\Framework\App\Request\Http as HttpRequest;
  12. class AddressTest extends \Magento\TestFramework\TestCase\AbstractController
  13. {
  14. /** @var AccountManagementInterface */
  15. private $accountManagement;
  16. /** @var FormKey */
  17. private $formKey;
  18. /**
  19. * @inheritDoc
  20. */
  21. protected function setUp()
  22. {
  23. parent::setUp();
  24. $logger = $this->createMock(\Psr\Log\LoggerInterface::class);
  25. $session = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  26. \Magento\Customer\Model\Session::class,
  27. [$logger]
  28. );
  29. $this->accountManagement = Bootstrap::getObjectManager()->create(AccountManagementInterface::class);
  30. $this->formKey = Bootstrap::getObjectManager()->create(FormKey::class);
  31. $customer = $this->accountManagement->authenticate('customer@example.com', 'password');
  32. $session->setCustomerDataAsLoggedIn($customer);
  33. }
  34. /**
  35. * @magentoDataFixture Magento/Customer/_files/customer.php
  36. * @magentoDataFixture Magento/Customer/_files/customer_address.php
  37. */
  38. public function testIndexAction()
  39. {
  40. $this->dispatch('customer/address/index');
  41. $body = $this->getResponse()->getBody();
  42. $this->assertContains('Default Billing Address', $body);
  43. $this->assertContains('Default Shipping Address', $body);
  44. $this->assertContains('Green str, 67', $body);
  45. }
  46. /**
  47. * @magentoDataFixture Magento/Customer/_files/customer.php
  48. * @magentoDataFixture Magento/Customer/_files/customer_address.php
  49. */
  50. public function testFormAction()
  51. {
  52. $this->dispatch('customer/address/edit');
  53. $body = $this->getResponse()->getBody();
  54. $this->assertContains('value="John"', $body);
  55. $this->assertContains('value="Smith"', $body);
  56. }
  57. /**
  58. * @magentoDataFixture Magento/Customer/_files/customer.php
  59. * @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
  60. */
  61. public function testFormPostAction()
  62. {
  63. $this->getRequest()->setParam(
  64. 'id',
  65. 2
  66. )->setMethod(
  67. 'POST'
  68. )->setPostValue(
  69. [
  70. 'form_key' => $this->_objectManager->get(\Magento\Framework\Data\Form\FormKey::class)->getFormKey(),
  71. 'firstname' => 'James',
  72. 'lastname' => 'Bond',
  73. 'company' => 'Magento Commerce Inc.',
  74. 'telephone' => '1112223333',
  75. 'fax' => '2223334444',
  76. 'street' => ['1234 Monterey Rd', 'Apt 13'],
  77. 'city' => 'Kyiv',
  78. 'region' => 'Kiev',
  79. 'postcode' => '55555',
  80. 'country_id' => 'UA',
  81. 'success_url' => '',
  82. 'error_url' => '',
  83. 'default_billing' => true,
  84. 'default_shipping' => true,
  85. ]
  86. );
  87. // we are overwriting the address coming from the fixture
  88. $this->dispatch('customer/address/formPost');
  89. $this->getCustomerRegistry()->remove(1);
  90. $this->assertRedirect($this->stringContains('customer/address/index'));
  91. $this->assertSessionMessages(
  92. $this->equalTo(['You saved the address.']),
  93. \Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
  94. );
  95. $address = $this->accountManagement->getDefaultBillingAddress(1);
  96. $this->assertEquals('UA', $address->getCountryId());
  97. $this->assertEquals('Kyiv', $address->getCity());
  98. $this->assertEquals('Kiev', $address->getRegion()->getRegion());
  99. $this->assertTrue($address->isDefaultBilling());
  100. $this->assertTrue($address->isDefaultShipping());
  101. }
  102. /**
  103. * @return CustomerRegistry
  104. */
  105. private function getCustomerRegistry()
  106. {
  107. return $this->_objectManager->get(CustomerRegistry::class);
  108. }
  109. /**
  110. * @magentoDataFixture Magento/Customer/_files/customer.php
  111. * @magentoDataFixture Magento/Customer/_files/customer_address.php
  112. */
  113. public function testFailedFormPostAction()
  114. {
  115. $this->getRequest()->setParam(
  116. 'id',
  117. 1
  118. )->setMethod(
  119. 'POST'
  120. )->setPostValue(
  121. [
  122. 'form_key' => $this->_objectManager->get(\Magento\Framework\Data\Form\FormKey::class)->getFormKey(),
  123. 'firstname' => 'James',
  124. 'lastname' => 'Bond',
  125. 'company' => 'Magento Commerce Inc.',
  126. 'telephone' => '1112223333',
  127. 'fax' => '2223334444',
  128. // omit street and city to fail validation
  129. 'region_id' => '12',
  130. 'region' => 'California',
  131. 'postcode' => '55555',
  132. 'country_id' => 'US',
  133. 'success_url' => '',
  134. 'error_url' => '',
  135. ]
  136. );
  137. // we are overwriting the address coming from the fixture
  138. $this->dispatch('customer/address/formPost');
  139. $this->getCustomerRegistry()->remove(1);
  140. $this->assertRedirect($this->stringContains('customer/address/edit'));
  141. $this->assertSessionMessages(
  142. $this->equalTo(
  143. [
  144. 'One or more input exceptions have occurred.',
  145. '&quot;street&quot; is required. Enter and try again.',
  146. '&quot;city&quot; is required. Enter and try again.',
  147. ]
  148. ),
  149. \Magento\Framework\Message\MessageInterface::TYPE_ERROR
  150. );
  151. }
  152. /**
  153. * @magentoDataFixture Magento/Customer/_files/customer.php
  154. * @magentoDataFixture Magento/Customer/_files/customer_address.php
  155. */
  156. public function testDeleteAction()
  157. {
  158. $this->getRequest()->setParam('id', 1);
  159. $this->getRequest()->setParam('form_key', $this->formKey->getFormKey())->setMethod(HttpRequest::METHOD_POST);
  160. // we are overwriting the address coming from the fixture
  161. $this->dispatch('customer/address/delete');
  162. $this->assertRedirect($this->stringContains('customer/address/index'));
  163. $this->assertSessionMessages(
  164. $this->equalTo(['You deleted the address.']),
  165. \Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
  166. );
  167. }
  168. /**
  169. * @magentoDataFixture Magento/Customer/_files/customer.php
  170. * @magentoDataFixture Magento/Customer/_files/customer_address.php
  171. */
  172. public function testWrongAddressDeleteAction()
  173. {
  174. $this->getRequest()->setParam('id', 555);
  175. $this->getRequest()->setParam('form_key', $this->formKey->getFormKey())->setMethod(HttpRequest::METHOD_POST);
  176. // we are overwriting the address coming from the fixture
  177. $this->dispatch('customer/address/delete');
  178. $this->assertRedirect($this->stringContains('customer/address/index'));
  179. $this->assertSessionMessages(
  180. $this->equalTo(['We can\'t delete the address right now.']),
  181. \Magento\Framework\Message\MessageInterface::TYPE_ERROR
  182. );
  183. }
  184. }