SaveTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Customer\Controller\Adminhtml\Address;
  8. use Magento\Customer\Api\AccountManagementInterface;
  9. use Magento\Customer\Api\CustomerRepositoryInterface;
  10. use Magento\TestFramework\Helper\Bootstrap;
  11. use Magento\Framework\App\Request\Http as HttpRequest;
  12. /**
  13. * @magentoAppArea adminhtml
  14. */
  15. class SaveTest extends \Magento\TestFramework\TestCase\AbstractBackendController
  16. {
  17. /** @var CustomerRepositoryInterface */
  18. private $customerRepository;
  19. /** @var AccountManagementInterface */
  20. private $accountManagement;
  21. /** @var \Magento\TestFramework\ObjectManager */
  22. private $objectManager;
  23. /** @var \Magento\Customer\Controller\Adminhtml\Address\Save */
  24. private $customerAddress;
  25. /**
  26. * @inheritDoc
  27. */
  28. protected function setUp()
  29. {
  30. parent::setUp();
  31. $this->customerRepository = Bootstrap::getObjectManager()->get(
  32. \Magento\Customer\Api\CustomerRepositoryInterface::class
  33. );
  34. $this->accountManagement = Bootstrap::getObjectManager()->get(
  35. \Magento\Customer\Api\AccountManagementInterface::class
  36. );
  37. $this->objectManager = Bootstrap::getObjectManager();
  38. $this->customerAddress = $this->objectManager->get(\Magento\Customer\Controller\Adminhtml\Address\Save::class);
  39. }
  40. /**
  41. * @inheritDoc
  42. */
  43. protected function tearDown()
  44. {
  45. /**
  46. * Unset customer data
  47. */
  48. Bootstrap::getObjectManager()->get(\Magento\Backend\Model\Session::class)->setCustomerData(null);
  49. /**
  50. * Unset messages
  51. */
  52. Bootstrap::getObjectManager()->get(\Magento\Backend\Model\Session::class)->getMessages(true);
  53. }
  54. /**
  55. * @magentoDataFixture Magento/Customer/_files/customer_no_address.php
  56. *
  57. * Check that customer id set and addresses saved
  58. */
  59. public function testSaveActionWithValidAddressData()
  60. {
  61. $customer = $this->customerRepository->get('customer5@example.com');
  62. $customerId = $customer->getId();
  63. $post = [
  64. 'parent_id' => $customerId,
  65. 'firstname' => 'test firstname',
  66. 'lastname' => 'test lastname',
  67. 'street' => ['test street'],
  68. 'city' => 'test city',
  69. 'region_id' => 10,
  70. 'country_id' => 'US',
  71. 'postcode' => '01001',
  72. 'telephone' => '+7000000001',
  73. ];
  74. $this->getRequest()->setPostValue($post)->setMethod(HttpRequest::METHOD_POST);
  75. $this->objectManager->get(\Magento\Backend\Model\Session::class)->setCustomerFormData($post);
  76. $this->customerAddress->execute();
  77. $this->assertSessionMessages($this->isEmpty(), \Magento\Framework\Message\MessageInterface::TYPE_ERROR);
  78. /** Check that customer data were cleaned after it was saved successfully*/
  79. $this->assertEmpty($this->objectManager->get(\Magento\Backend\Model\Session::class)->getCustomerData());
  80. $customer = $this->customerRepository->getById($customerId);
  81. $this->assertEquals('Firstname', $customer->getFirstname());
  82. $addresses = $customer->getAddresses();
  83. $this->assertCount(1, $addresses);
  84. $this->assertNull($this->accountManagement->getDefaultBillingAddress($customerId));
  85. $this->assertNull($this->accountManagement->getDefaultShippingAddress($customerId));
  86. }
  87. /**
  88. * @magentoDataFixture Magento/Customer/_files/customer_no_address.php
  89. *
  90. * Check that customer id set and addresses saved
  91. */
  92. public function testSaveActionWithDefaultShippingAndBilling()
  93. {
  94. $customer = $this->customerRepository->get('customer5@example.com');
  95. $customerId = $customer->getId();
  96. $post = [
  97. 'parent_id' => $customerId,
  98. 'firstname' => 'test firstname',
  99. 'lastname' => 'test lastname',
  100. 'street' => ['test street'],
  101. 'city' => 'test city',
  102. 'region_id' => 10,
  103. 'country_id' => 'US',
  104. 'postcode' => '01001',
  105. 'telephone' => '+7000000001',
  106. 'default_billing' => true,
  107. 'default_shipping' => true
  108. ];
  109. $this->getRequest()->setPostValue($post)->setMethod(HttpRequest::METHOD_POST);
  110. $this->objectManager->get(\Magento\Backend\Model\Session::class)->setCustomerFormData($post);
  111. $this->customerAddress->execute();
  112. /**
  113. * Check that errors was generated and set to session
  114. */
  115. $this->assertSessionMessages($this->isEmpty(), \Magento\Framework\Message\MessageInterface::TYPE_ERROR);
  116. /**
  117. * Check that customer data were cleaned after it was saved successfully
  118. */
  119. $this->assertEmpty($this->objectManager->get(\Magento\Backend\Model\Session::class)->getCustomerData());
  120. /**
  121. * Remove stored customer from registry
  122. */
  123. $this->objectManager->get(\Magento\Customer\Model\CustomerRegistry::class)->remove($customerId);
  124. $customer = $this->customerRepository->get('customer5@example.com');
  125. $this->assertEquals('Firstname', $customer->getFirstname());
  126. $addresses = $customer->getAddresses();
  127. $this->assertCount(1, $addresses);
  128. $this->assertNotNull($this->accountManagement->getDefaultBillingAddress($customerId));
  129. $this->assertNotNull($this->accountManagement->getDefaultShippingAddress($customerId));
  130. }
  131. /**
  132. * @magentoDataFixture Magento/Customer/_files/customer_sample.php
  133. *
  134. * Check that customer id set and addresses saved
  135. */
  136. public function testSaveActionWithExistingAdresses()
  137. {
  138. $customer = $this->customerRepository->get('customer@example.com');
  139. $customerId = $customer->getId();
  140. $post = [
  141. 'parent_id' => $customerId,
  142. 'firstname' => 'test firstname',
  143. 'lastname' => 'test lastname',
  144. 'street' => ['test street'],
  145. 'city' => 'test city',
  146. 'region_id' => 10,
  147. 'country_id' => 'US',
  148. 'postcode' => '01001',
  149. 'telephone' => '+7000000001',
  150. ];
  151. $this->getRequest()->setPostValue($post)->setMethod(HttpRequest::METHOD_POST);
  152. $this->objectManager->get(\Magento\Backend\Model\Session::class)->setCustomerFormData($post);
  153. $this->customerAddress->execute();
  154. /**
  155. * Check that errors was generated and set to session
  156. */
  157. $this->assertSessionMessages($this->isEmpty(), \Magento\Framework\Message\MessageInterface::TYPE_ERROR);
  158. /**
  159. * Check that customer data were cleaned after it was saved successfully
  160. */
  161. $this->assertEmpty($this->objectManager->get(\Magento\Backend\Model\Session::class)->getCustomerData());
  162. $customer = $this->customerRepository->getById($customerId);
  163. $this->assertEquals('test firstname', $customer->getFirstname());
  164. $addresses = $customer->getAddresses();
  165. $this->assertCount(4, $addresses);
  166. }
  167. }