This commit is contained in:
2026-09-15 10:26:34 +08:00
commit ecf36afa56
10493 changed files with 1404888 additions and 0 deletions
@@ -0,0 +1,160 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Persistent\Model\Checkout;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Checkout\Model\DefaultConfigProvider;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Framework\ObjectManagerInterface;
use Magento\Persistent\Helper\Session as PersistentSessionHelper;
use Magento\Persistent\Model\Session as PersistentSession;
use Magento\Persistent\Model\SessionFactory as PersistentSessionFactory;
use Magento\Quote\Model\QuoteIdMask;
use Magento\Quote\Model\QuoteIdMaskFactory;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Interception\PluginList;
use Magento\TestFramework\Quote\Model\GetQuoteByReservedOrderId;
use PHPUnit\Framework\TestCase;
/**
* Test for checkout config provider plugin
*
* @see \Magento\Persistent\Model\Checkout\ConfigProviderPlugin
* @magentoAppArea frontend
* @magentoDbIsolation enabled
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ConfigProviderPluginTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var DefaultConfigProvider */
private $configProvider;
/** @var CustomerSession */
private $customerSession;
/** @var CheckoutSession */
private $checkoutSession;
/** @var QuoteIdMask */
private $quoteIdMask;
/** @var PersistentSessionHelper */
private $persistentSessionHelper;
/** @var PersistentSession */
private $persistentSession;
/** @var GetQuoteByReservedOrderId */
private $getQuoteByReservedOrderId;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->configProvider = $this->objectManager->get(DefaultConfigProvider::class);
$this->customerSession = $this->objectManager->get(CustomerSession::class);
$this->checkoutSession = $this->objectManager->get(CheckoutSession::class);
$this->quoteIdMask = $this->objectManager->get(QuoteIdMaskFactory::class)->create();
$this->persistentSessionHelper = $this->objectManager->get(PersistentSessionHelper::class);
$this->persistentSession = $this->objectManager->get(PersistentSessionFactory::class)->create();
$this->getQuoteByReservedOrderId = $this->objectManager->get(GetQuoteByReservedOrderId::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->customerSession->setCustomerId(null);
$this->checkoutSession->clearQuote();
$this->checkoutSession->setCustomerData(null);
$this->persistentSessionHelper->setSession(null);
parent::tearDown();
}
/**
* @return void
*/
public function testPluginIsRegistered(): void
{
$pluginInfo = $this->objectManager->get(PluginList::class)->get(DefaultConfigProvider::class);
$this->assertSame(ConfigProviderPlugin::class, $pluginInfo['mask_quote_id_substitutor']['instance']);
}
/**
* @magentoDataFixture Magento/Persistent/_files/persistent_with_customer_quote_and_cookie.php
* @magentoConfigFixture current_store persistent/options/enabled 1
*
* @return void
*/
public function testWithNotLoggedCustomer(): void
{
$session = $this->persistentSession->loadByCustomerId(1);
$this->persistentSessionHelper->setSession($session);
$quote = $this->getQuoteByReservedOrderId->execute('test_order_with_customer_without_address');
$this->checkoutSession->setQuoteId($quote->getId());
$result = $this->configProvider->getConfig();
$this->assertEquals(
$this->quoteIdMask->load($quote->getId(), 'quote_id')->getMaskedId(),
$result['quoteData']['entity_id']
);
}
/**
* @magentoDataFixture Magento/Persistent/_files/persistent_with_customer_quote_and_cookie.php
* @magentoConfigFixture current_store persistent/options/enabled 1
*
* @return void
*/
public function testWithLoggedCustomer(): void
{
$this->customerSession->setCustomerId(1);
$session = $this->persistentSession->loadByCustomerId(1);
$this->persistentSessionHelper->setSession($session);
$quote = $this->getQuoteByReservedOrderId->execute('test_order_with_customer_without_address');
$this->checkoutSession->setQuoteId($quote->getId());
$result = $this->configProvider->getConfig();
$this->assertEquals($quote->getId(), $result['quoteData']['entity_id']);
}
/**
* @magentoDataFixture Magento/Checkout/_files/quote_with_customer_without_address.php
* @magentoConfigFixture current_store persistent/options/enabled 0
*
* @return void
*/
public function testPersistentDisabled(): void
{
$quote = $this->getQuoteByReservedOrderId->execute('test_order_with_customer_without_address');
$this->checkoutSession->setQuoteId($quote->getId());
$result = $this->configProvider->getConfig();
$this->assertNull($result['quoteData']['entity_id']);
}
/**
* @magentoDataFixture Magento/Checkout/_files/quote_with_customer_without_address.php
* @magentoConfigFixture current_store persistent/options/enabled 1
*
* @return void
*/
public function testWithoutPersistentSession(): void
{
$quote = $this->getQuoteByReservedOrderId->execute('test_order_with_customer_without_address');
$this->checkoutSession->setQuoteId($quote->getId());
$result = $this->configProvider->getConfig();
$this->assertNull($result['quoteData']['entity_id']);
}
}
@@ -0,0 +1,239 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Persistent\Model\Checkout;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class GuestPaymentInformationManagementPluginTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Persistent\Helper\Session
*/
protected $persistentSessionHelper;
/**
* @var \Magento\Customer\Model\Session
*/
protected $customerSession;
/**
* @var \Magento\Checkout\Model\Session
*/
protected $checkoutSession;
/**
* @var \Magento\Quote\Api\CartRepositoryInterface
*/
protected $cartRepository;
/**
* @var \Magento\Quote\Api\CartManagementInterface
*/
protected $cartManagement;
/**
* @var \Magento\Customer\Api\CustomerRepositoryInterface
*/
protected $customerFactory;
/**
* @var \Magento\Quote\Api\CartItemRepositoryInterface
*/
protected $cartItemRepository;
/**
* @var \Magento\Quote\Model\QuoteIdMask
*/
protected $quoteIdMaskFactory;
/**
* @var \Magento\Quote\Api\PaymentMethodManagementInterface
*/
protected $paymentMethodManagement;
/**
* @var \Magento\Quote\Api\BillingAddressManagementInterface
*/
protected $billingAddressManagement;
/**
* @var \Magento\Quote\Model\ShippingAddressManagementInterface
*/
protected $shippingAddressManagement;
/**
* @var \Magento\Quote\Api\ShippingMethodManagementInterface
*/
protected $shippingEstimateManagement;
/**
* @var \Magento\Checkout\Api\TotalsInformationManagementInterface
*/
protected $totalsInformationManagement;
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
protected $objectManager;
protected function setUp(): void
{
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$this->customerSession = $this->objectManager->get(\Magento\Customer\Model\Session::class);
$this->persistentSessionHelper = $this->objectManager->create(\Magento\Persistent\Helper\Session::class);
$this->customerFactory = $this->objectManager->create(
\Magento\Customer\Model\CustomerFactory::class
);
$this->checkoutSession = $this->objectManager->create(\Magento\Checkout\Model\Session::class);
$this->cartRepository = $this->objectManager->create(\Magento\Quote\Api\CartRepositoryInterface::class);
$this->cartManagement = $this->objectManager->create(\Magento\Quote\Api\CartManagementInterface::class);
$this->cartItemRepository = $this->objectManager->create(\Magento\Quote\Api\CartItemRepositoryInterface::class);
$this->quoteIdMaskFactory = $this->objectManager->create(\Magento\Quote\Model\QuoteIdMaskFactory::class);
$this->paymentMethodManagement = $this->objectManager->create(
\Magento\Quote\Api\PaymentMethodManagementInterface::class
);
$this->billingAddressManagement = $this->objectManager->create(
\Magento\Quote\Api\BillingAddressManagementInterface::class
);
$this->shippingEstimateManagement = $this->objectManager->create(
\Magento\Quote\Api\ShippingMethodManagementInterface::class
);
$this->totalsInformationManagement = $this->objectManager->create(
\Magento\Checkout\Api\TotalsInformationManagementInterface::class
);
$this->shippingAddressManagement = $this->objectManager->create(
\Magento\Quote\Model\ShippingAddressManagementInterface::class
);
}
/**
* Test builds out a persistent customer shopping cart, emulates a
* session expiring, and checks out with the persisted cart as a guest.
*
* Expected - Order contains guest email, not customer email.
*
* @magentoConfigFixture current_store persistent/options/customer 1
* @magentoConfigFixture current_store persistent/options/enabled 1
* @magentoConfigFixture current_store persistent/options/remember_enabled 1
* @magentoConfigFixture current_store persistent/options/remember_default 1
* @magentoConfigFixture current_store payment/substitution/active 1
* @magentoAppArea frontend
* @magentoAppIsolation enabled
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Customer/_files/customer_address.php
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
* @magentoDbIsolation disabled
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function testBeforeSavePaymentInformationAndPlaceOrder()
{
$guestEmail = 'guest@example.com';
//Retrieve customer from repository
/** @var \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository */
$customerRepository = $this->objectManager->create(\Magento\Customer\Api\CustomerRepositoryInterface::class);
$customer = $customerRepository->getById(1);
$this->customerSession->loginById($customer->getId());
//Retrieve product from repository
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
$productRepository = $this->objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
$product = $productRepository->getById(1);
$product->setOptions(null);
$productRepository->save($product);
//Add item to newly created customer cart
$cartId = $this->cartManagement->createEmptyCartForCustomer($customer->getId());
/** @var \Magento\Quote\Api\Data\CartItemInterface $quoteItem */
$quoteItem = $this->objectManager->create(\Magento\Quote\Api\Data\CartItemInterface::class);
$quoteItem->setQuoteId($cartId);
$quoteItem->setProduct($product);
$quoteItem->setQty(2);
$this->cartItemRepository->save($quoteItem);
//Fill out address data
/** @var \Magento\Quote\Api\Data\AddressInterface $billingAddress */
$billingAddress = $this->objectManager->create(\Magento\Quote\Api\Data\AddressInterface::class);
$billingAddress->setFirstname('guestFirst');
$billingAddress->setLastname('guestLast');
$billingAddress->setEmail($guestEmail);
$billingAddress->setStreet('guestStreet');
$billingAddress->setCity('Austin');
$billingAddress->setTelephone('1342587690');
$billingAddress->setPostcode('14325');
$billingAddress->setRegionId(12);
$billingAddress->setCountryId('US');
/** @var \Magento\Quote\Api\Data\AddressInterface $shippingAddress */
$shippingAddress = $this->objectManager->create(\Magento\Quote\Api\Data\AddressInterface::class);
$shippingAddress->setFirstname('guestFirst');
$shippingAddress->setLastname('guestLast');
$shippingAddress->setEmail(null);
$shippingAddress->setStreet('guestStreet');
$shippingAddress->setCity('Austin');
$shippingAddress->setTelephone('1342587690');
$shippingAddress->setPostcode('14325');
$shippingAddress->setRegionId(12);
$shippingAddress->setCountryId('US');
$shippingAddress->setSameAsBilling(true);
$this->shippingAddressManagement->assign($cartId, $shippingAddress);
$shippingAddress = $this->shippingAddressManagement->get($cartId);
//Determine shipping options and collect totals
/** @var \Magento\Checkout\Api\Data\TotalsInformationInterface $totals */
$totals = $this->objectManager->create(\Magento\Checkout\Api\Data\TotalsInformationInterface::class);
$totals->setAddress($shippingAddress);
$totals->setShippingCarrierCode('flatrate');
$totals->setShippingMethodCode('flatrate');
$this->totalsInformationManagement->calculate($cartId, $totals);
//Select payment method
/** @var \Magento\Quote\Api\Data\PaymentInterface $payment */
$payment = $this->objectManager->create(\Magento\Quote\Api\Data\PaymentInterface::class);
$payment->setMethod('checkmo');
$this->paymentMethodManagement->set($cartId, $payment);
$quote = $this->cartRepository->get($cartId);
//Verify checkout session contains correct quote data
$this->checkoutSession->clearQuote();
$this->checkoutSession->setQuoteId($quote->getId());
//Set up persistent session data and expire customer session
$this->persistentSessionHelper->getSession()->setCustomerId($customer->getId())
->setPersistentCookie(10000, '');
$this->persistentSessionHelper->getSession()->removePersistentCookie()->setPersistentCookie(100000000, '');
$this->customerSession->setIsCustomerEmulated(true)->expireSessionCookie();
//Grab masked quote Id to pass to payment manager
/** @var $quoteIdMask \Magento\Quote\Model\QuoteIdMask */
$quoteIdMask = $this->quoteIdMaskFactory->create()->load(
$this->checkoutSession->getQuote()->getId(),
'quote_id'
);
$maskedCartId = $quoteIdMask->getMaskedId();
//Submit order as expired/emulated customer
/** @var \Magento\Checkout\Model\GuestPaymentInformationManagement $paymentManagement */
$paymentManagement = $this->objectManager->create(
\Magento\Checkout\Model\GuestPaymentInformationManagement::class
);
//Grab created order data
$orderId = $paymentManagement->savePaymentInformationAndPlaceOrder(
$maskedCartId,
$guestEmail,
$quote->getPayment(),
$billingAddress
);
/** @var \Magento\Sales\Api\OrderRepositoryInterface $orderRepo */
$orderRepo = $this->objectManager->create(\Magento\Sales\Api\OrderRepositoryInterface::class);
$order = $orderRepo->get($orderId);
//Assert order tied to guest email
$this->assertEquals($guestEmail, $order->getCustomerEmail());
}
}
@@ -0,0 +1,84 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Persistent\Model;
use Magento\Framework\ObjectManagerInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Test for remember me checkbox on create customer account page.
*
* @see \Magento\Persistent\Model\CheckoutConfigProvider
*/
class CheckoutConfigProviderTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var CheckoutConfigProvider */
private $model;
/**
* @inheritdoc
*/
public function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->model = $this->objectManager->get(CheckoutConfigProvider::class);
}
/**
* @magentoConfigFixture current_store persistent/options/enabled 1
* @magentoConfigFixture current_store persistent/options/remember_enabled 1
* @magentoConfigFixture current_store persistent/options/remember_default 1
*
* @return void
*/
public function testRememberMeEnabled(): void
{
$expectedConfig = [
'persistenceConfig' => ['isRememberMeCheckboxVisible' => true, 'isRememberMeCheckboxChecked' => true],
];
$config = $this->model->getConfig();
$this->assertEquals($expectedConfig, $config);
}
/**
* @magentoConfigFixture current_store persistent/options/enabled 1
* @magentoConfigFixture current_store persistent/options/remember_enabled 0
* @magentoConfigFixture current_store persistent/options/remember_default 0
*
* @return void
*/
public function testRememberMeDisabled(): void
{
$expectedConfig = [
'persistenceConfig' => ['isRememberMeCheckboxVisible' => false, 'isRememberMeCheckboxChecked' => false],
];
$config = $this->model->getConfig();
$this->assertEquals($expectedConfig, $config);
}
/**
* @magentoConfigFixture current_store persistent/options/enabled 0
* @magentoConfigFixture current_store persistent/options/remember_default 0
*
* @return void
*/
public function testPersistentDisabled(): void
{
$expectedConfig = [
'persistenceConfig' => ['isRememberMeCheckboxVisible' => false, 'isRememberMeCheckboxChecked' => false],
];
$config = $this->model->getConfig();
$this->assertEquals($expectedConfig, $config);
}
}
@@ -0,0 +1,94 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Persistent\Model;
use Magento\Customer\Model\Context;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ObserverTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Customer\Helper\View
*/
protected $_customerViewHelper;
/**
* @var \Magento\Framework\Escaper
*/
protected $_escaper;
/**
* @var \Magento\Customer\Api\CustomerRepositoryInterface
*/
protected $customerRepository;
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
protected $_objectManager;
/**
* @var \Magento\Persistent\Model\Observer
*/
protected $_observer;
/**
* @var \Magento\Checkout\Model\Session | \PHPUnit\Framework\MockObject\MockObject
*/
protected $_checkoutSession;
protected function setUp(): void
{
$this->_objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$this->_customerViewHelper = $this->_objectManager->create(
\Magento\Customer\Helper\View::class
);
$this->_escaper = $this->_objectManager->create(
\Magento\Framework\Escaper::class
);
$this->customerRepository = $this->_objectManager->create(
\Magento\Customer\Api\CustomerRepositoryInterface::class
);
$this->_checkoutSession = $this->getMockBuilder(
\Magento\Checkout\Model\Session::class
)->disableOriginalConstructor()->setMethods([])->getMock();
$this->_observer = $this->_objectManager->create(
\Magento\Persistent\Model\Observer::class,
[
'escaper' => $this->_escaper,
'customerViewHelper' => $this->_customerViewHelper,
'customerRepository' => $this->customerRepository,
'checkoutSession' => $this->_checkoutSession
]
);
}
/**
* @magentoAppArea frontend
* @magentoAppIsolation enabled
*/
public function testEmulateWelcomeBlock()
{
$httpContext = new \Magento\Framework\App\Http\Context();
$httpContext->setValue(Context::CONTEXT_AUTH, 1, 1);
$block = $this->_objectManager->create(
\Magento\Sales\Block\Reorder\Sidebar::class,
[
'httpContext' => $httpContext
]
);
$this->_observer->emulateWelcomeBlock($block);
$this->assertEquals('&nbsp;', $block->getWelcome());
}
}
@@ -0,0 +1,62 @@
<?php
/**
* \Magento\Persistent\Model\Persistent\Config
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Persistent\Model\Persistent;
use Magento\Framework\App\Filesystem\DirectoryList;
class ConfigTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Persistent\Model\Persistent\Config
*/
protected $_model;
/** @var \Magento\Framework\ObjectManagerInterface */
protected $_objectManager;
protected function setUp(): void
{
$directoryList = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Framework\App\Filesystem\DirectoryList::class,
['root' => DirectoryList::ROOT]
);
$filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Framework\Filesystem::class,
['directoryList' => $directoryList]
);
$this->_objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$this->_model = $this->_objectManager->create(
\Magento\Persistent\Model\Persistent\Config::class,
['filesystem' => $filesystem]
);
}
public function testCollectInstancesToEmulate()
{
$this->_model->setConfigFilePath(__DIR__ . '/_files/persistent.xml');
$result = $this->_model->collectInstancesToEmulate();
$expected = include '_files/expectedArray.php';
$this->assertEquals($expected, $result);
}
public function testGetBlockConfigInfo()
{
$this->_model->setConfigFilePath(__DIR__ . '/_files/persistent.xml');
$blocks = $this->_model->getBlockConfigInfo(\Magento\Sales\Block\Reorder\Sidebar::class);
$expected = include '_files/expectedBlocksArray.php';
$this->assertEquals($expected, $blocks);
}
public function testGetBlockConfigInfoNotConfigured()
{
$this->_model->setConfigFilePath(__DIR__ . '/_files/persistent.xml');
$blocks = $this->_model->getBlockConfigInfo(\Magento\Catalog\Block\Product\Compare\ListCompare::class);
$this->assertEquals([], $blocks);
}
}
@@ -0,0 +1,27 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
return [
'blocks' => [
'reorder_sidebar' => [
'name_in_layout' => 'sale.reorder.sidebar',
'class' => \Magento\PersistentHistory\Model\Observer::class,
'method' => 'initReorderSidebar',
'block_type' => \Magento\Sales\Block\Reorder\Sidebar::class,
],
'viewed_products' => [
'name_in_layout' => 'left.reports.product.viewed',
'class' => \Magento\PersistentHistory\Model\Observer::class,
'method' => 'emulateViewedProductsBlock',
'block_type' => \Magento\Sales\Block\Reorder\Sidebar::class,
],
'compared_products' => [
'name_in_layout' => 'right.reports.product.compared',
'class' => \Magento\PersistentHistory\Model\Observer::class,
'method' => 'emulateComparedProductsBlock',
'block_type' => \Magento\Reports\Block\Product\Compared::class,
],
]
];
@@ -0,0 +1,19 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
return [
'reorder_sidebar' => [
'name_in_layout' => 'sale.reorder.sidebar',
'class' => \Magento\PersistentHistory\Model\Observer::class,
'method' => 'initReorderSidebar',
'block_type' => \Magento\Sales\Block\Reorder\Sidebar::class,
],
'viewed_products' => [
'name_in_layout' => 'left.reports.product.viewed',
'class' => \Magento\PersistentHistory\Model\Observer::class,
'method' => 'emulateViewedProductsBlock',
'block_type' => \Magento\Sales\Block\Reorder\Sidebar::class,
]
];
@@ -0,0 +1,33 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Persistent/etc/persistent.xsd">
<instances>
<blocks>
<reference id="reorder_sidebar">
<name_in_layout>sale.reorder.sidebar</name_in_layout>
<class>Magento\PersistentHistory\Model\Observer</class>
<method>initReorderSidebar</method>
<block_type>Magento\Sales\Block\Reorder\Sidebar</block_type>
</reference>
<reference id="viewed_products">
<name_in_layout>left.reports.product.viewed</name_in_layout>
<class>Magento\PersistentHistory\Model\Observer</class>
<method>emulateViewedProductsBlock</method>
<block_type>Magento\Sales\Block\Reorder\Sidebar</block_type>
</reference>
<reference id="compared_products">
<name_in_layout>right.reports.product.compared</name_in_layout>
<class>Magento\PersistentHistory\Model\Observer</class>
<method>emulateComparedProductsBlock</method>
<block_type>Magento\Reports\Block\Product\Compared</block_type>
</reference>
</blocks>
</instances>
</config>
@@ -0,0 +1,116 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Persistent\Model;
use Magento\Customer\Api\Data\GroupInterface;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Framework\ObjectManagerInterface;
use Magento\Persistent\Helper\Session as PersistentSessionHelper;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Quote\Model\GetQuoteByReservedOrderId;
use PHPUnit\Framework\TestCase;
/**
* Test for persistent quote manager model
*
* @see \Magento\Persistent\Model\QuoteManager
* @magentoDbIsolation enabled
*/
class QuoteManagerTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var QuoteManager */
private $model;
/** @var CheckoutSession */
private $checkoutSession;
/** @var GetQuoteByReservedOrderId */
private $getQuoteByReservedOrderId;
/** @var PersistentSessionHelper */
private $persistentSessionHelper;
/** @var CartInterface */
private $quote;
/** @var CartRepositoryInterface */
private $quoteRepository;
/**
* @inheritdoc
*/
public function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->model = $this->objectManager->get(QuoteManager::class);
$this->checkoutSession = $this->objectManager->get(CheckoutSession::class);
$this->getQuoteByReservedOrderId = $this->objectManager->get(GetQuoteByReservedOrderId::class);
$this->persistentSessionHelper = $this->objectManager->get(PersistentSessionHelper::class);
$this->quoteRepository = $this->objectManager->get(CartRepositoryInterface::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->checkoutSession->clearQuote();
$this->checkoutSession->setCustomerData(null);
if ($this->quote instanceof CartInterface) {
$this->quoteRepository->delete($this->quote);
}
parent::tearDown();
}
/**
* @magentoDataFixture Magento/Persistent/_files/persistent_with_customer_quote_and_cookie.php
* @magentoConfigFixture current_store persistent/options/enabled 1
* @magentoConfigFixture current_store persistent/options/shopping_cart 1
*
* @return void
*/
public function testPersistentShoppingCartEnabled(): void
{
$customerQuote = $this->getQuoteByReservedOrderId->execute('test_order_with_customer_without_address');
$this->checkoutSession->setQuoteId($customerQuote->getId());
$this->model->setGuest(true);
$this->quote = $this->checkoutSession->getQuote();
$this->assertNotEquals($customerQuote->getId(), $this->quote->getId());
$this->assertFalse($this->model->isPersistent());
$this->assertNull($this->quote->getCustomerId());
$this->assertNull($this->quote->getCustomerEmail());
$this->assertNull($this->quote->getCustomerFirstname());
$this->assertNull($this->quote->getCustomerLastname());
$this->assertEquals(GroupInterface::NOT_LOGGED_IN_ID, $this->quote->getCustomerGroupId());
$this->assertEmpty($this->quote->getIsPersistent());
$this->assertNull($this->persistentSessionHelper->getSession()->getId());
}
/**
* @magentoDataFixture Magento/Persistent/_files/persistent_with_customer_quote_and_cookie.php
* @magentoConfigFixture current_store persistent/options/enabled 1
* @magentoConfigFixture current_store persistent/options/shopping_cart 0
*
* @return void
*/
public function testPersistentShoppingCartDisabled(): void
{
$quote = $this->getQuoteByReservedOrderId->execute('test_order_with_customer_without_address');
$this->checkoutSession->setQuoteId($quote->getId());
$this->model->setGuest(true);
$this->assertNull($this->checkoutSession->getQuote()->getId());
}
}
@@ -0,0 +1,102 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Persistent\Model;
class SessionTest extends \PHPUnit\Framework\TestCase
{
/**
* Session model
*
* @var \Magento\Persistent\Model\Session
*/
protected $session;
/**
* Object manager
*
* @var \Magento\Framework\ObjectManagerInterface
*/
protected $objectManager;
/**
* The existing cookies
*
* @var array
*/
protected $existingCookies;
protected function setUp(): void
{
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$this->session = $this->objectManager->create(
\Magento\Persistent\Model\Session::class
);
$this->existingCookies = $_COOKIE;
}
protected function tearDown(): void
{
$_COOKIE = $this->existingCookies;
}
public function testSetPersistentCookie()
{
$this->assertArrayNotHasKey(Session::COOKIE_NAME, $_COOKIE);
$key = 'sessionKey';
$this->session->setKey($key);
$this->session->setPersistentCookie(1000, '/');
$this->assertEquals($key, $_COOKIE[Session::COOKIE_NAME]);
}
public function testRemovePersistendCookie()
{
$_COOKIE[Session::COOKIE_NAME] = 'cookieValue';
$this->session->removePersistentCookie();
$this->assertArrayNotHasKey(Session::COOKIE_NAME, $_COOKIE);
}
/**
* @param int $duration
* @param string $cookieValue
* @dataProvider renewPersistentCookieDataProvider
*/
public function testRenewPersistentCookie($duration, $cookieValue = 'cookieValue')
{
$_COOKIE[Session::COOKIE_NAME] = $cookieValue;
$this->session->renewPersistentCookie($duration, '/');
$this->assertEquals($cookieValue, $_COOKIE[Session::COOKIE_NAME]);
}
public function renewPersistentCookieDataProvider()
{
return [
'no duration' => [null],
'no cookie' => [1000, null],
'all' => [1000],
];
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
*/
public function testLoadByCookieKey()
{
/** @var \Magento\Persistent\Model\Session $preSession */
$preSession = $this->objectManager->get(\Magento\Persistent\Model\SessionFactory::class)
->create()
->loadByCookieKey();
$this->assertNull($preSession->getCustomerId());
$this->session->setCustomerId(1)->save();
$this->session->setPersistentCookie(1000, '/');
/** @var \Magento\Persistent\Model\Session $postSession */
$postSession = $this->objectManager->get(\Magento\Persistent\Model\SessionFactory::class)
->create()
->loadByCookieKey();
$this->assertEquals(1, $postSession->getCustomerId());
}
}