|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Persistent\Model\Checkout; |
| 9 | + |
| 10 | +use Magento\Customer\Model\Session as CustomerSession; |
| 11 | +use Magento\Checkout\Model\DefaultConfigProvider; |
| 12 | +use Magento\Checkout\Model\Session as CheckoutSession; |
| 13 | +use Magento\Framework\ObjectManagerInterface; |
| 14 | +use Magento\Persistent\Helper\Session as PersistentSessionHelper; |
| 15 | +use Magento\Persistent\Model\Session as PersistentSession; |
| 16 | +use Magento\Persistent\Model\SessionFactory as PersistentSessionFactory; |
| 17 | +use Magento\Quote\Model\QuoteIdMask; |
| 18 | +use Magento\Quote\Model\QuoteIdMaskFactory; |
| 19 | +use Magento\TestFramework\Helper\Bootstrap; |
| 20 | +use Magento\TestFramework\Interception\PluginList; |
| 21 | +use Magento\TestFramework\Quote\Model\GetQuoteByReservedOrderId; |
| 22 | +use PHPUnit\Framework\TestCase; |
| 23 | + |
| 24 | +/** |
| 25 | + * Test for checkout config provider plugin |
| 26 | + * |
| 27 | + * @see \Magento\Persistent\Model\Checkout\ConfigProviderPlugin |
| 28 | + * @magentoAppArea frontend |
| 29 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 30 | + */ |
| 31 | +class ConfigProviderPluginTest extends TestCase |
| 32 | +{ |
| 33 | + /** @var ObjectManagerInterface */ |
| 34 | + private $objectManager; |
| 35 | + |
| 36 | + /** @var DefaultConfigProvider */ |
| 37 | + private $configProvider; |
| 38 | + |
| 39 | + /** @var CustomerSession */ |
| 40 | + private $customerSession; |
| 41 | + |
| 42 | + /** @var CheckoutSession */ |
| 43 | + private $checkoutSession; |
| 44 | + |
| 45 | + /** @var QuoteIdMask */ |
| 46 | + private $quoteIdMask; |
| 47 | + |
| 48 | + /** @var PersistentSessionHelper */ |
| 49 | + private $persistentSessionHelper; |
| 50 | + |
| 51 | + /** @var PersistentSession */ |
| 52 | + private $persistentSession; |
| 53 | + |
| 54 | + /** @var GetQuoteByReservedOrderId */ |
| 55 | + private $getQuoteByReservedOrderId; |
| 56 | + |
| 57 | + /** |
| 58 | + * @inheritdoc |
| 59 | + */ |
| 60 | + protected function setUp() |
| 61 | + { |
| 62 | + parent::setUp(); |
| 63 | + |
| 64 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 65 | + $this->configProvider = $this->objectManager->get(DefaultConfigProvider::class); |
| 66 | + $this->customerSession = $this->objectManager->get(CustomerSession::class); |
| 67 | + $this->checkoutSession = $this->objectManager->get(CheckoutSession::class); |
| 68 | + $this->quoteIdMask = $this->objectManager->get(QuoteIdMaskFactory::class)->create(); |
| 69 | + $this->persistentSessionHelper = $this->objectManager->get(PersistentSessionHelper::class); |
| 70 | + $this->persistentSession = $this->objectManager->get(PersistentSessionFactory::class)->create(); |
| 71 | + $this->getQuoteByReservedOrderId = $this->objectManager->get(GetQuoteByReservedOrderId::class); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @inheritdoc |
| 76 | + */ |
| 77 | + protected function tearDown() |
| 78 | + { |
| 79 | + $this->customerSession->setCustomerId(null); |
| 80 | + $this->checkoutSession->clearQuote(); |
| 81 | + $this->checkoutSession->setCustomerData(null); |
| 82 | + |
| 83 | + parent::tearDown(); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @return void |
| 88 | + */ |
| 89 | + public function testPluginIsRegistered(): void |
| 90 | + { |
| 91 | + $pluginInfo = $this->objectManager->get(PluginList::class)->get(DefaultConfigProvider::class); |
| 92 | + $this->assertSame(ConfigProviderPlugin::class, $pluginInfo['mask_quote_id_substitutor']['instance']); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @magentoDataFixture Magento/Persistent/_files/persistent_with_customer_quote_and_cookie.php |
| 97 | + * @magentoConfigFixture current_store persistent/options/enabled 1 |
| 98 | + * |
| 99 | + * @return void |
| 100 | + */ |
| 101 | + public function testWithNotLoggedCustomer(): void |
| 102 | + { |
| 103 | + $session = $this->persistentSession->loadByCustomerId(1); |
| 104 | + $this->persistentSessionHelper->setSession($session); |
| 105 | + $quote = $this->getQuoteByReservedOrderId->execute('test_order_with_customer_without_address'); |
| 106 | + $this->checkoutSession->setQuoteId($quote->getId()); |
| 107 | + $result = $this->configProvider->getConfig(); |
| 108 | + $this->assertEquals( |
| 109 | + $this->quoteIdMask->load($quote->getId(), 'quote_id')->getMaskedId(), |
| 110 | + $result['quoteData']['entity_id'] |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @magentoDataFixture Magento/Persistent/_files/persistent_with_customer_quote_and_cookie.php |
| 116 | + * @magentoConfigFixture current_store persistent/options/enabled 1 |
| 117 | + * |
| 118 | + * @return void |
| 119 | + */ |
| 120 | + public function testWithLoggedCustomer(): void |
| 121 | + { |
| 122 | + $this->customerSession->setCustomerId(1); |
| 123 | + $session = $this->persistentSession->loadByCustomerId(1); |
| 124 | + $this->persistentSessionHelper->setSession($session); |
| 125 | + $quote = $this->getQuoteByReservedOrderId->execute('test_order_with_customer_without_address'); |
| 126 | + $this->checkoutSession->setQuoteId($quote->getId()); |
| 127 | + $result = $this->configProvider->getConfig(); |
| 128 | + $this->assertEquals($quote->getId(), $result['quoteData']['entity_id']); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @magentoDataFixture Magento/Persistent/_files/persistent_with_customer_quote_and_cookie.php |
| 133 | + * @magentoConfigFixture current_store persistent/options/enabled 0 |
| 134 | + * |
| 135 | + * @return void |
| 136 | + */ |
| 137 | + public function testPersistentDisabled(): void |
| 138 | + { |
| 139 | + $quote = $this->getQuoteByReservedOrderId->execute('test_order_with_customer_without_address'); |
| 140 | + $this->checkoutSession->setQuoteId($quote->getId()); |
| 141 | + $result = $this->configProvider->getConfig(); |
| 142 | + $this->assertNull($result['quoteData']['entity_id']); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @magentoDataFixture Magento/Persistent/_files/persistent_with_customer_quote_and_cookie.php |
| 147 | + * @magentoConfigFixture current_store persistent/options/enabled 1 |
| 148 | + * |
| 149 | + * @return void |
| 150 | + */ |
| 151 | + public function testWithoutPersistentSession(): void |
| 152 | + { |
| 153 | + $quote = $this->getQuoteByReservedOrderId->execute('test_order_with_customer_without_address'); |
| 154 | + $this->checkoutSession->setQuoteId($quote->getId()); |
| 155 | + $result = $this->configProvider->getConfig(); |
| 156 | + $this->assertNull($result['quoteData']['entity_id']); |
| 157 | + } |
| 158 | +} |
0 commit comments