AddressTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Block\Account\Dashboard;
  7. use Magento\Customer\Api\CustomerRepositoryInterface;
  8. use Magento\TestFramework\Helper\Bootstrap;
  9. class AddressTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var \Magento\Customer\Block\Account\Dashboard\Address
  13. */
  14. protected $_block;
  15. /** @var \Magento\Customer\Model\Session */
  16. protected $_customerSession;
  17. /**
  18. * @var \Magento\Framework\ObjectManagerInterface
  19. */
  20. protected $objectManager;
  21. protected function setUp()
  22. {
  23. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  24. $this->_customerSession = $this->objectManager->get(\Magento\Customer\Model\Session::class);
  25. $this->_block = $this->objectManager->get(\Magento\Framework\View\LayoutInterface::class)
  26. ->createBlock(
  27. \Magento\Customer\Block\Account\Dashboard\Address::class,
  28. '',
  29. ['customerSession' => $this->_customerSession]
  30. );
  31. $this->objectManager->get(\Magento\Framework\App\ViewInterface::class)->setIsLayoutLoaded(true);
  32. }
  33. protected function tearDown()
  34. {
  35. $this->_customerSession->unsCustomerId();
  36. /** @var \Magento\Customer\Model\CustomerRegistry $customerRegistry */
  37. $customerRegistry = $this->objectManager->get(\Magento\Customer\Model\CustomerRegistry::class);
  38. //Cleanup customer from registry
  39. $customerRegistry->remove(1);
  40. }
  41. /**
  42. * @magentoDataFixture Magento/Customer/_files/customer.php
  43. */
  44. public function testGetCustomer()
  45. {
  46. $objectManager = Bootstrap::getObjectManager();
  47. $layout = $objectManager->get(\Magento\Framework\View\LayoutInterface::class);
  48. $layout->setIsCacheable(false);
  49. /** @var CustomerRepositoryInterface $customerRepository */
  50. $customerRepository = $objectManager
  51. ->get(\Magento\Customer\Api\CustomerRepositoryInterface::class);
  52. $customer = $customerRepository->getById(1);
  53. $this->_customerSession->setCustomerId(1);
  54. $object = $this->_block->getCustomer();
  55. $this->assertEquals($customer, $object);
  56. $layout->setIsCacheable(true);
  57. }
  58. public function testGetCustomerMissingCustomer()
  59. {
  60. $moduleManager = $this->objectManager->get(\Magento\Framework\Module\Manager::class);
  61. if ($moduleManager->isEnabled('Magento_PageCache')) {
  62. $customerDataFactory = $this->objectManager->create(
  63. \Magento\Customer\Api\Data\CustomerInterfaceFactory::class
  64. );
  65. $customerData = $customerDataFactory->create()->setGroupId($this->_customerSession->getCustomerGroupId());
  66. $this->assertEquals($customerData, $this->_block->getCustomer());
  67. } else {
  68. $this->assertNull($this->_block->getCustomer());
  69. }
  70. }
  71. /**
  72. * @magentoDataFixture Magento/Customer/_files/customer.php
  73. * @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
  74. * @magentoDataFixture Magento/Customer/_files/customer_no_address.php
  75. * @dataProvider getPrimaryShippingAddressHtmlDataProvider
  76. */
  77. public function testGetPrimaryShippingAddressHtml($customerId, $expected)
  78. {
  79. // todo: this test is sensitive to caching impact
  80. if (!empty($customerId)) {
  81. $this->_customerSession->setCustomerId($customerId);
  82. }
  83. $html = $this->_block->getPrimaryShippingAddressHtml();
  84. $this->assertEquals($expected, $html);
  85. }
  86. public function getPrimaryShippingAddressHtmlDataProvider()
  87. {
  88. $expected = "John Smith<br />\nCompanyName<br />\nGreen str, 67<br />\n\n\n\nCityM, Alabama, 75477<br />"
  89. . "\nUnited States<br />\nT: <a href=\"tel:3468676\">3468676</a>\n\n";
  90. return [
  91. '0' => [0, 'You have not set a default shipping address.'],
  92. '1' => [1, $expected],
  93. '5' => [5, 'You have not set a default shipping address.']
  94. ];
  95. }
  96. /**
  97. * @magentoDataFixture Magento/Customer/_files/customer.php
  98. * @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
  99. * @magentoDataFixture Magento/Customer/_files/customer_no_address.php
  100. * @dataProvider getPrimaryBillingAddressHtmlDataProvider
  101. */
  102. public function testGetPrimaryBillingAddressHtml($customerId, $expected)
  103. {
  104. if (!empty($customerId)) {
  105. $this->_customerSession->setCustomerId($customerId);
  106. }
  107. $html = $this->_block->getPrimaryBillingAddressHtml();
  108. $this->assertEquals($expected, $html);
  109. }
  110. public function getPrimaryBillingAddressHtmlDataProvider()
  111. {
  112. $expected = "John Smith<br />\nCompanyName<br />\nGreen str, 67<br />\n\n\n\nCityM, Alabama, 75477<br />"
  113. . "\nUnited States<br />\nT: <a href=\"tel:3468676\">3468676</a>\n\n";
  114. return [
  115. '0' => [0, 'You have not set a default billing address.'],
  116. '1' => [1, $expected],
  117. '5' => [5, 'You have not set a default billing address.'],
  118. ];
  119. }
  120. /**
  121. * @magentoDataFixture Magento/Customer/_files/customer.php
  122. * @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
  123. * @magentoDataFixture Magento/Customer/_files/customer_no_address.php
  124. * @dataProvider getPrimaryAddressEditUrlDataProvider
  125. */
  126. public function testGetPrimaryShippingAddressEditUrl($customerId, $expected)
  127. {
  128. if (!empty($customerId)) {
  129. $this->_customerSession->setCustomerId($customerId);
  130. }
  131. $url = $this->_block->getPrimaryShippingAddressEditUrl();
  132. $this->assertEquals($expected, $url);
  133. }
  134. /**
  135. * @magentoDataFixture Magento/Customer/_files/customer.php
  136. * @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
  137. * @magentoDataFixture Magento/Customer/_files/customer_no_address.php
  138. * @dataProvider getPrimaryAddressEditUrlDataProvider
  139. */
  140. public function testGetPrimaryBillingAddressEditUrl($customerId, $expected)
  141. {
  142. if (!empty($customerId)) {
  143. $this->_customerSession->setCustomerId($customerId);
  144. }
  145. $url = $this->_block->getPrimaryBillingAddressEditUrl();
  146. $this->assertEquals($expected, $url);
  147. }
  148. public function getPrimaryAddressEditUrlDataProvider()
  149. {
  150. return [
  151. '1' => [1, 'http://localhost/index.php/customer/address/edit/id/1/'],
  152. ];
  153. }
  154. }