Skip to content

Commit 216c337

Browse files
committed
MC-37315: Customer configuration: Persistent shopping cart
1 parent 50404cc commit 216c337

10 files changed

+740
-74
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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\Block\Form;
9+
10+
use Magento\Framework\ObjectManagerInterface;
11+
use Magento\Framework\View\LayoutInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\TestFramework\Helper\Xpath;
14+
use PHPUnit\Framework\TestCase;
15+
16+
/**
17+
* Test for remember me checkbox on create customer account page
18+
*
19+
* @see \Magento\Persistent\Block\Form\Remember
20+
* @magentoAppArea frontend
21+
*/
22+
class RememberTest extends TestCase
23+
{
24+
/** @var ObjectManagerInterface */
25+
private $objectManager;
26+
27+
/** @var Remember */
28+
private $block;
29+
30+
/**
31+
* @inheritdoc
32+
*/
33+
public function setUp(): void
34+
{
35+
parent::setUp();
36+
37+
$this->objectManager = Bootstrap::getObjectManager();
38+
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(Remember::class)
39+
->setTemplate('Magento_Persistent::remember_me.phtml');
40+
}
41+
42+
/**
43+
* @magentoConfigFixture current_store persistent/options/enabled 1
44+
* @magentoConfigFixture current_store persistent/options/remember_enabled 1
45+
* @magentoConfigFixture current_store persistent/options/remember_default 0
46+
*
47+
* @return void
48+
*/
49+
public function testRememberMeEnabled(): void
50+
{
51+
$this->assertFalse($this->block->isRememberMeChecked());
52+
$this->assertEquals(
53+
1,
54+
Xpath::getElementsCountForXpath(
55+
sprintf(
56+
'//input[@name="persistent_remember_me"]/following-sibling::label/span[contains(text(), "%s")]',
57+
__('Remember Me')
58+
),
59+
$this->block->toHtml()
60+
),
61+
'Remember Me checkbox wasn\'t found.'
62+
);
63+
}
64+
65+
/**
66+
* @magentoConfigFixture current_store persistent/options/enabled 1
67+
* @magentoConfigFixture current_store persistent/options/remember_enabled 1
68+
* @magentoConfigFixture current_store persistent/options/remember_default 1
69+
*
70+
* @return void
71+
*/
72+
public function testRememberMeAndRememberDefaultEnabled(): void
73+
{
74+
$this->assertTrue($this->block->isRememberMeChecked());
75+
$this->assertEquals(
76+
1,
77+
Xpath::getElementsCountForXpath(
78+
sprintf(
79+
'//input[@name="persistent_remember_me"]/following-sibling::label/span[contains(text(), "%s")]',
80+
__('Remember Me')
81+
),
82+
$this->block->toHtml()
83+
),
84+
'Remember Me checkbox wasn\'t found or not checked by default.'
85+
);
86+
}
87+
88+
/**
89+
* @magentoConfigFixture current_store persistent/options/enabled 0
90+
*
91+
* @return void
92+
*/
93+
public function testPersistentDisabled(): void
94+
{
95+
$this->assertEmpty($this->block->toHtml());
96+
}
97+
98+
/**
99+
* @magentoConfigFixture current_store persistent/options/enabled 1
100+
* @magentoConfigFixture current_store persistent/options/remember_enabled 0
101+
*
102+
* @return void
103+
*/
104+
public function testRememberMeDisabled(): void
105+
{
106+
$this->assertEmpty($this->block->toHtml());
107+
}
108+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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\Helper;
9+
10+
use Magento\Framework\ObjectManagerInterface;
11+
use Magento\Persistent\Model\SessionFactory;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* Test for persistent session helper
17+
*
18+
* @see \Magento\Persistent\Helper\Session
19+
* @magentoDbIsolation enabled
20+
* @magentoAppArea frontend
21+
*/
22+
class SessionTest extends TestCase
23+
{
24+
/** @var ObjectManagerInterface */
25+
private $objectManager;
26+
27+
/** @var Session */
28+
private $helper;
29+
30+
/** @var SessionFactory */
31+
private $sessionFactory;
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
public function setUp(): void
37+
{
38+
parent::setUp();
39+
40+
$this->objectManager = Bootstrap::getObjectManager();
41+
$this->helper = $this->objectManager->get(Session::class);
42+
$this->sessionFactory = $this->objectManager->get(SessionFactory::class);
43+
}
44+
45+
/**
46+
* @magentoDataFixture Magento/Persistent/_files/persistent.php
47+
* @magentoConfigFixture current_store persistent/options/enabled 1
48+
*
49+
* @return void
50+
*/
51+
public function testPersistentEnabled(): void
52+
{
53+
$this->helper->setSession($this->sessionFactory->create()->loadByCustomerId(1));
54+
$this->assertTrue($this->helper->isPersistent());
55+
}
56+
57+
/**
58+
* @magentoDataFixture Magento/Persistent/_files/persistent.php
59+
* @magentoConfigFixture current_store persistent/options/enabled 0
60+
*
61+
* @return void
62+
*/
63+
public function testPersistentDisabled(): void
64+
{
65+
$this->helper->setSession($this->sessionFactory->create()->loadByCustomerId(1));
66+
$this->assertFalse($this->helper->isPersistent());
67+
}
68+
69+
/**
70+
* @magentoDataFixture Magento/Customer/_files/customer.php
71+
* @magentoConfigFixture current_store persistent/options/enabled 1
72+
*
73+
* @return void
74+
*/
75+
public function testCustomerWithoutPersistent(): void
76+
{
77+
$this->helper->setSession($this->sessionFactory->create()->loadByCustomerId(1));
78+
$this->assertFalse($this->helper->isPersistent());
79+
}
80+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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+
* @magentoDbIsolation enabled
30+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
31+
*/
32+
class ConfigProviderPluginTest extends TestCase
33+
{
34+
/** @var ObjectManagerInterface */
35+
private $objectManager;
36+
37+
/** @var DefaultConfigProvider */
38+
private $configProvider;
39+
40+
/** @var CustomerSession */
41+
private $customerSession;
42+
43+
/** @var CheckoutSession */
44+
private $checkoutSession;
45+
46+
/** @var QuoteIdMask */
47+
private $quoteIdMask;
48+
49+
/** @var PersistentSessionHelper */
50+
private $persistentSessionHelper;
51+
52+
/** @var PersistentSession */
53+
private $persistentSession;
54+
55+
/** @var GetQuoteByReservedOrderId */
56+
private $getQuoteByReservedOrderId;
57+
58+
/**
59+
* @inheritdoc
60+
*/
61+
protected function setUp(): void
62+
{
63+
parent::setUp();
64+
65+
$this->objectManager = Bootstrap::getObjectManager();
66+
$this->configProvider = $this->objectManager->get(DefaultConfigProvider::class);
67+
$this->customerSession = $this->objectManager->get(CustomerSession::class);
68+
$this->checkoutSession = $this->objectManager->get(CheckoutSession::class);
69+
$this->quoteIdMask = $this->objectManager->get(QuoteIdMaskFactory::class)->create();
70+
$this->persistentSessionHelper = $this->objectManager->get(PersistentSessionHelper::class);
71+
$this->persistentSession = $this->objectManager->get(PersistentSessionFactory::class)->create();
72+
$this->getQuoteByReservedOrderId = $this->objectManager->get(GetQuoteByReservedOrderId::class);
73+
}
74+
75+
/**
76+
* @inheritdoc
77+
*/
78+
protected function tearDown(): void
79+
{
80+
$this->customerSession->setCustomerId(null);
81+
$this->checkoutSession->clearQuote();
82+
$this->checkoutSession->setCustomerData(null);
83+
$this->persistentSessionHelper->setSession(null);
84+
85+
parent::tearDown();
86+
}
87+
88+
/**
89+
* @return void
90+
*/
91+
public function testPluginIsRegistered(): void
92+
{
93+
$pluginInfo = $this->objectManager->get(PluginList::class)->get(DefaultConfigProvider::class);
94+
$this->assertSame(ConfigProviderPlugin::class, $pluginInfo['mask_quote_id_substitutor']['instance']);
95+
}
96+
97+
/**
98+
* @magentoDataFixture Magento/Persistent/_files/persistent_with_customer_quote_and_cookie.php
99+
* @magentoConfigFixture current_store persistent/options/enabled 1
100+
*
101+
* @return void
102+
*/
103+
public function testWithNotLoggedCustomer(): void
104+
{
105+
$session = $this->persistentSession->loadByCustomerId(1);
106+
$this->persistentSessionHelper->setSession($session);
107+
$quote = $this->getQuoteByReservedOrderId->execute('test_order_with_customer_without_address');
108+
$this->checkoutSession->setQuoteId($quote->getId());
109+
$result = $this->configProvider->getConfig();
110+
$this->assertEquals(
111+
$this->quoteIdMask->load($quote->getId(), 'quote_id')->getMaskedId(),
112+
$result['quoteData']['entity_id']
113+
);
114+
}
115+
116+
/**
117+
* @magentoDataFixture Magento/Persistent/_files/persistent_with_customer_quote_and_cookie.php
118+
* @magentoConfigFixture current_store persistent/options/enabled 1
119+
*
120+
* @return void
121+
*/
122+
public function testWithLoggedCustomer(): void
123+
{
124+
$this->customerSession->setCustomerId(1);
125+
$session = $this->persistentSession->loadByCustomerId(1);
126+
$this->persistentSessionHelper->setSession($session);
127+
$quote = $this->getQuoteByReservedOrderId->execute('test_order_with_customer_without_address');
128+
$this->checkoutSession->setQuoteId($quote->getId());
129+
$result = $this->configProvider->getConfig();
130+
$this->assertEquals($quote->getId(), $result['quoteData']['entity_id']);
131+
}
132+
133+
/**
134+
* @magentoDataFixture Magento/Checkout/_files/quote_with_customer_without_address.php
135+
* @magentoConfigFixture current_store persistent/options/enabled 0
136+
*
137+
* @return void
138+
*/
139+
public function testPersistentDisabled(): void
140+
{
141+
$quote = $this->getQuoteByReservedOrderId->execute('test_order_with_customer_without_address');
142+
$this->checkoutSession->setQuoteId($quote->getId());
143+
$result = $this->configProvider->getConfig();
144+
$this->assertNull($result['quoteData']['entity_id']);
145+
}
146+
147+
/**
148+
* @magentoDataFixture Magento/Checkout/_files/quote_with_customer_without_address.php
149+
* @magentoConfigFixture current_store persistent/options/enabled 1
150+
*
151+
* @return void
152+
*/
153+
public function testWithoutPersistentSession(): void
154+
{
155+
$quote = $this->getQuoteByReservedOrderId->execute('test_order_with_customer_without_address');
156+
$this->checkoutSession->setQuoteId($quote->getId());
157+
$result = $this->configProvider->getConfig();
158+
$this->assertNull($result['quoteData']['entity_id']);
159+
}
160+
}

0 commit comments

Comments
 (0)