UpdateQuoteStoreTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Quote\Model\Plugin;
  8. use Magento\Checkout\Model\Session;
  9. use Magento\Framework\Api\SearchCriteriaBuilder;
  10. use Magento\FunctionalTestingFramework\ObjectManagerInterface;
  11. use Magento\Quote\Api\CartRepositoryInterface;
  12. use Magento\Quote\Model\Quote;
  13. use Magento\Store\Api\Data\StoreInterface;
  14. use Magento\Store\Api\StoreCookieManagerInterface;
  15. use Magento\Store\Model\StoreManagerInterface;
  16. use Magento\Store\Model\StoreRepository;
  17. use Magento\TestFramework\Helper\Bootstrap as BootstrapHelper;
  18. /**
  19. * @magentoAppArea frontend
  20. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  21. */
  22. class UpdateQuoteStoreTest extends \PHPUnit\Framework\TestCase
  23. {
  24. /**
  25. * @var ObjectManagerInterface
  26. */
  27. private $objectManager;
  28. /**
  29. * @var CartRepositoryInterface
  30. */
  31. private $quoteRepository;
  32. protected function setUp()
  33. {
  34. $this->objectManager = BootstrapHelper::getObjectManager();
  35. $this->quoteRepository = $this->objectManager->create(CartRepositoryInterface::class);
  36. }
  37. /**
  38. * Tests that active quote store id updates after store cookie change.
  39. *
  40. * @magentoDataFixture Magento/Quote/_files/empty_quote.php
  41. * @magentoDataFixture Magento/Store/_files/second_store.php
  42. * @throws \ReflectionException
  43. * @throws \Magento\Framework\Exception\NoSuchEntityException
  44. */
  45. public function testUpdateQuoteStoreAfterChangeStoreCookie()
  46. {
  47. $secondStoreCode = 'fixture_second_store';
  48. $reservedOrderId = 'reserved_order_id';
  49. /** @var StoreManagerInterface $storeManager */
  50. $storeManager = $this->objectManager->get(StoreManagerInterface::class);
  51. $currentStore = $storeManager->getStore();
  52. $quote = $this->getQuote($reservedOrderId);
  53. $this->assertEquals(
  54. $currentStore->getId(),
  55. $quote->getStoreId(),
  56. 'Current store id and quote store id are not match'
  57. );
  58. /** @var Session $checkoutSession */
  59. $checkoutSession = $this->objectManager->get(Session::class);
  60. $checkoutSession->setQuoteId($quote->getId());
  61. $storeRepository = $this->objectManager->create(StoreRepository::class);
  62. $secondStore = $storeRepository->get($secondStoreCode);
  63. $storeCookieManager = $this->getStoreCookieManager($currentStore);
  64. $storeCookieManager->setStoreCookie($secondStore);
  65. $updatedQuote = $this->getQuote($reservedOrderId);
  66. $this->assertEquals(
  67. $secondStore->getId(),
  68. $updatedQuote->getStoreId(),
  69. 'Active quote store id should be equal second store id'
  70. );
  71. }
  72. /**
  73. * Retrieves quote by reserved order id.
  74. *
  75. * @param string $reservedOrderId
  76. * @return Quote
  77. */
  78. private function getQuote(string $reservedOrderId): Quote
  79. {
  80. /** @var SearchCriteriaBuilder $searchCriteriaBuilder */
  81. $searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
  82. $searchCriteria = $searchCriteriaBuilder->addFilter('reserved_order_id', $reservedOrderId)
  83. ->create();
  84. $items = $this->quoteRepository->getList($searchCriteria)->getItems();
  85. return array_pop($items);
  86. }
  87. /**
  88. * Returns instance of StoreCookieManagerInterface with mocked cookieManager dependency.
  89. *
  90. * Mock is needed since integration test framework use own cookie manager with
  91. * behavior that differs from real environment.
  92. *
  93. * @param $currentStore
  94. * @return StoreCookieManagerInterface
  95. * @throws \ReflectionException
  96. */
  97. private function getStoreCookieManager(StoreInterface $currentStore): StoreCookieManagerInterface
  98. {
  99. /** @var StoreCookieManagerInterface $storeCookieManager */
  100. $storeCookieManager = $this->objectManager->get(StoreCookieManagerInterface::class);
  101. $cookieManagerMock = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\PhpCookieManager::class)
  102. ->disableOriginalConstructor()
  103. ->getMock();
  104. $cookieManagerMock->method('getCookie')
  105. ->willReturn($currentStore->getCode());
  106. $reflection = new \ReflectionClass($storeCookieManager);
  107. $cookieManager = $reflection->getProperty('cookieManager');
  108. $cookieManager->setAccessible(true);
  109. $cookieManager->setValue($storeCookieManager, $cookieManagerMock);
  110. return $storeCookieManager;
  111. }
  112. }