SessionTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Model;
  7. use Magento\Catalog\Api\Data\ProductTierPriceInterface;
  8. use Magento\Catalog\Api\ProductRepositoryInterface;
  9. use Magento\Customer\Api\CustomerRepositoryInterface;
  10. use Magento\Customer\Model\Session as CustomerSession;
  11. use Magento\Framework\Api\FilterBuilder;
  12. use Magento\Framework\Api\SearchCriteriaBuilder;
  13. use Magento\Quote\Api\CartRepositoryInterface;
  14. use Magento\Quote\Api\Data\CartInterface;
  15. use Magento\TestFramework\Helper\Bootstrap;
  16. /**
  17. * Class SessionTest
  18. */
  19. class SessionTest extends \PHPUnit\Framework\TestCase
  20. {
  21. /**
  22. * @var \Magento\Framework\ObjectManagerInterface
  23. */
  24. private $objectManager;
  25. /**
  26. * @var CustomerRepositoryInterface
  27. */
  28. private $customerRepository;
  29. /**
  30. * @var CustomerSession
  31. */
  32. private $customerSession;
  33. /**
  34. * @var Session
  35. */
  36. private $checkoutSession;
  37. /**
  38. * @return void
  39. */
  40. protected function setUp()
  41. {
  42. $this->objectManager = Bootstrap::getObjectManager();
  43. $this->customerRepository = $this->objectManager->create(CustomerRepositoryInterface::class);
  44. $this->customerSession = $this->objectManager->get(CustomerSession::class);
  45. $this->checkoutSession = $this->objectManager->create(Session::class);
  46. }
  47. /**
  48. * Test covers case when quote is not yet initialized and customer data is set to checkout session model.
  49. *
  50. * Expected result - quote object should be loaded and customer data should be set to it.
  51. *
  52. * @magentoDataFixture Magento/Sales/_files/quote_with_customer.php
  53. */
  54. public function testGetQuoteNotInitializedCustomerSet()
  55. {
  56. $customer = $this->customerRepository->getById(1);
  57. $this->checkoutSession->setCustomerData($customer);
  58. /** Execute SUT */
  59. $quote = $this->checkoutSession->getQuote();
  60. $this->_validateCustomerDataInQuote($quote);
  61. }
  62. /**
  63. * Test covers case when quote is not yet initialized and customer data is set to customer session model.
  64. *
  65. * Expected result - quote object should be loaded and customer data should be set to it.
  66. *
  67. * @magentoDataFixture Magento/Sales/_files/quote_with_customer.php
  68. * @magentoAppIsolation enabled
  69. */
  70. public function testGetQuoteNotInitializedCustomerLoggedIn()
  71. {
  72. $customer = $this->customerRepository->getById(1);
  73. $this->customerSession->setCustomerDataObject($customer);
  74. /** Execute SUT */
  75. $quote = $this->checkoutSession->getQuote();
  76. $this->_validateCustomerDataInQuote($quote);
  77. }
  78. /**
  79. * Tes merging of customer data into initialized quote object.
  80. *
  81. * Conditions:
  82. * 1. Quote without customer data is set to checkout session
  83. * 2. Customer without associated quote is set to checkout session
  84. *
  85. * Expected result:
  86. * Quote which is set to checkout session should contain customer data
  87. *
  88. * @magentoDataFixture Magento/Customer/_files/customer.php
  89. * @magentoAppIsolation enabled
  90. */
  91. public function testLoadCustomerQuoteCustomerWithoutQuote()
  92. {
  93. $quote = $this->checkoutSession->getQuote();
  94. $this->assertEmpty($quote->getCustomerId(), 'Precondition failed: Customer data must not be set to quote');
  95. $this->assertEmpty($quote->getCustomerEmail(), 'Precondition failed: Customer data must not be set to quote');
  96. $customer = $this->customerRepository->getById(1);
  97. $this->customerSession->setCustomerDataObject($customer);
  98. /** Ensure that customer data is still unavailable before SUT invocation */
  99. $quote = $this->checkoutSession->getQuote();
  100. $this->assertEmpty($quote->getCustomerEmail(), 'Precondition failed: Customer data must not be set to quote');
  101. /** Execute SUT */
  102. $this->checkoutSession->loadCustomerQuote();
  103. $quote = $this->checkoutSession->getQuote();
  104. $this->_validateCustomerDataInQuote($quote);
  105. }
  106. /**
  107. * @magentoDataFixture Magento/Customer/_files/customer.php
  108. * @magentoDataFixture Magento/Sales/_files/quote.php
  109. */
  110. public function testGetQuoteWithProductWithTierPrice()
  111. {
  112. $reservedOrderId = 'test01';
  113. $customerGroupId = 1;
  114. $tierPriceQty = 1;
  115. $tierPriceValue = 9;
  116. $productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
  117. $product = $productRepository->get('simple');
  118. $tierPrice = $this->objectManager->create(ProductTierPriceInterface::class)
  119. ->setCustomerGroupId($customerGroupId)
  120. ->setQty($tierPriceQty)
  121. ->setValue($tierPriceValue);
  122. $product->setTierPrices([$tierPrice]);
  123. $productRepository->save($product);
  124. $quote = $this->getQuote($reservedOrderId);
  125. $this->checkoutSession->setQuoteId($quote->getId());
  126. $quote = $this->checkoutSession->getQuote();
  127. $item = $quote->getItems()[0];
  128. /** @var \Magento\Catalog\Model\Product $quoteProduct */
  129. $quoteProduct = $item->getProduct();
  130. $this->assertEquals(10, $quoteProduct->getTierPrice($tierPriceQty));
  131. $customer = $this->customerRepository->getById(1);
  132. $this->customerSession->setCustomerDataAsLoggedIn($customer);
  133. $quote = $this->checkoutSession->getQuote();
  134. $item = $quote->getItems()[0];
  135. /** @var \Magento\Catalog\Model\Product $quoteProduct */
  136. $quoteProduct = $item->getProduct();
  137. $this->assertEquals($tierPriceValue, $quoteProduct->getTierPrice(1));
  138. }
  139. /**
  140. * Returns quote by reserved order id.
  141. *
  142. * @param string $reservedOrderId
  143. * @return CartInterface
  144. */
  145. private function getQuote(string $reservedOrderId): CartInterface
  146. {
  147. $filterBuilder = $this->objectManager->create(FilterBuilder::class);
  148. $filter = $filterBuilder->setField('reserved_order_id')
  149. ->setConditionType('=')
  150. ->setValue($reservedOrderId)
  151. ->create();
  152. $searchCriteriaBuilder = $this->objectManager->create(SearchCriteriaBuilder::class);
  153. $searchCriteria = $searchCriteriaBuilder->addFilters([$filter])
  154. ->create();
  155. $quoteRepository = $this->objectManager->get(CartRepositoryInterface::class);
  156. $searchResult = $quoteRepository->getList($searchCriteria);
  157. /** @var CartInterface[] $items */
  158. $items = $searchResult->getItems();
  159. return \array_values($items)[0];
  160. }
  161. /**
  162. * Ensure that quote has customer data specified in customer fixture.
  163. *
  164. * @param \Magento\Quote\Model\Quote $quote
  165. */
  166. protected function _validateCustomerDataInQuote($quote)
  167. {
  168. $customerIdFromFixture = 1;
  169. $customerEmailFromFixture = 'customer@example.com';
  170. $customerFirstNameFromFixture = 'John';
  171. $this->assertEquals(
  172. $customerEmailFromFixture,
  173. $quote->getCustomerEmail(),
  174. 'Customer email was not set to Quote correctly.'
  175. );
  176. $this->assertEquals(
  177. $customerIdFromFixture,
  178. $quote->getCustomerId(),
  179. 'Customer ID was not set to Quote correctly.'
  180. );
  181. $this->assertEquals(
  182. $customerFirstNameFromFixture,
  183. $quote->getCustomerFirstname(),
  184. 'Customer first name was not set to Quote correctly.'
  185. );
  186. }
  187. }