ltsecurityinc安防产品在线商城代码
This commit is contained in:
+90
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Persistent\Observer;
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Persistent/_files/persistent.php
|
||||
*/
|
||||
class EmulateCustomerObserverTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Customer\Api\CustomerRepositoryInterface
|
||||
*/
|
||||
protected $customerRepository;
|
||||
|
||||
/**
|
||||
* @var \Magento\Persistent\Helper\Session
|
||||
*/
|
||||
protected $_persistentSessionHelper;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\ObjectManagerInterface
|
||||
*/
|
||||
protected $_objectManager;
|
||||
|
||||
/**
|
||||
* @var \Magento\Persistent\Observer\EmulateCustomerObserver
|
||||
*/
|
||||
protected $_observer;
|
||||
|
||||
/**
|
||||
* @var \Magento\Customer\Model\Session
|
||||
*/
|
||||
protected $_customerSession;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->_objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
|
||||
$this->_customerSession = $this->_objectManager->get(\Magento\Customer\Model\Session::class);
|
||||
|
||||
$this->customerRepository = $this->_objectManager->create(
|
||||
\Magento\Customer\Api\CustomerRepositoryInterface::class
|
||||
);
|
||||
$this->_persistentSessionHelper = $this->_objectManager->create(\Magento\Persistent\Helper\Session::class);
|
||||
|
||||
$this->_observer = $this->_objectManager->create(
|
||||
\Magento\Persistent\Observer\EmulateCustomerObserver::class,
|
||||
[
|
||||
'customerRepository' => $this->customerRepository,
|
||||
'persistentSession' => $this->_persistentSessionHelper
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppArea frontend
|
||||
* @magentoConfigFixture current_store persistent/options/shopping_cart 1
|
||||
* @magentoConfigFixture current_store persistent/options/logout_clear 0
|
||||
* @magentoConfigFixture current_store persistent/options/enabled 1
|
||||
*/
|
||||
public function testEmulateCustomer()
|
||||
{
|
||||
$observer = new \Magento\Framework\Event\Observer();
|
||||
|
||||
$this->_customerSession->loginById(1);
|
||||
$this->_customerSession->logout();
|
||||
$this->assertNull($this->_customerSession->getCustomerId());
|
||||
$this->assertEquals(
|
||||
\Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID,
|
||||
$this->_customerSession->getCustomerGroupId()
|
||||
);
|
||||
|
||||
$this->_observer->execute($observer);
|
||||
$customer = $this->customerRepository->getById(
|
||||
$this->_persistentSessionHelper->getSession()->getCustomerId()
|
||||
);
|
||||
$this->assertEquals(
|
||||
$customer->getId(),
|
||||
$this->_customerSession->getCustomerId()
|
||||
);
|
||||
$this->assertEquals(
|
||||
$customer->getGroupId(),
|
||||
$this->_customerSession->getCustomerGroupId()
|
||||
);
|
||||
}
|
||||
}
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Persistent\Observer;
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Persistent/_files/persistent.php
|
||||
*/
|
||||
class EmulateQuoteObserverTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Customer\Api\CustomerRepositoryInterface
|
||||
*/
|
||||
protected $customerRepository;
|
||||
|
||||
/**
|
||||
* @var \Magento\Persistent\Helper\Session
|
||||
*/
|
||||
protected $_persistentSessionHelper;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\ObjectManagerInterface
|
||||
*/
|
||||
protected $_objectManager;
|
||||
|
||||
/**
|
||||
* @var \Magento\Persistent\Observer\EmulateQuoteObserver
|
||||
*/
|
||||
protected $_observer;
|
||||
|
||||
/**
|
||||
* @var \Magento\Customer\Model\Session
|
||||
*/
|
||||
protected $_customerSession;
|
||||
|
||||
/**
|
||||
* @var \Magento\Checkout\Model\Session | \PHPUnit\Framework\MockObject\MockObject
|
||||
*/
|
||||
protected $_checkoutSession;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->_objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
|
||||
$this->_customerSession = $this->_objectManager->get(\Magento\Customer\Model\Session::class);
|
||||
|
||||
$this->customerRepository = $this->_objectManager->create(
|
||||
\Magento\Customer\Api\CustomerRepositoryInterface::class
|
||||
);
|
||||
|
||||
$this->_checkoutSession = $this->getMockBuilder(
|
||||
\Magento\Checkout\Model\Session::class
|
||||
)->disableOriginalConstructor()->setMethods([])->getMock();
|
||||
|
||||
$this->_persistentSessionHelper = $this->_objectManager->create(\Magento\Persistent\Helper\Session::class);
|
||||
|
||||
$this->_observer = $this->_objectManager->create(
|
||||
\Magento\Persistent\Observer\EmulateQuoteObserver::class,
|
||||
[
|
||||
'customerRepository' => $this->customerRepository,
|
||||
'checkoutSession' => $this->_checkoutSession,
|
||||
'persistentSession' => $this->_persistentSessionHelper
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store persistent/options/enabled 1
|
||||
* @magentoConfigFixture current_store persistent/options/remember_enabled 1
|
||||
* @magentoConfigFixture current_store persistent/options/remember_default 1
|
||||
* @magentoAppArea frontend
|
||||
* @magentoConfigFixture current_store persistent/options/shopping_cart 1
|
||||
* @magentoConfigFixture current_store persistent/options/logout_clear 0
|
||||
*/
|
||||
public function testEmulateQuote()
|
||||
{
|
||||
$requestMock = $this->getMockBuilder(
|
||||
\Magento\Framework\App\Request\Http::class
|
||||
)->disableOriginalConstructor()->setMethods(
|
||||
[]
|
||||
)->getMock();
|
||||
$requestMock->expects($this->once())->method('getFullActionName')->willReturn('valid_action');
|
||||
$event = new \Magento\Framework\Event(['request' => $requestMock]);
|
||||
$observer = new \Magento\Framework\Event\Observer();
|
||||
$observer->setEvent($event);
|
||||
|
||||
$this->_customerSession->loginById(1);
|
||||
|
||||
$customer = $this->customerRepository->getById(
|
||||
$this->_persistentSessionHelper->getSession()->getCustomerId()
|
||||
);
|
||||
$this->_checkoutSession->expects($this->once())->method('setCustomerData')->with($customer);
|
||||
$this->_customerSession->logout();
|
||||
|
||||
$this->_observer->execute($observer);
|
||||
}
|
||||
}
|
||||
+161
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Persistent\Observer;
|
||||
|
||||
use DateTime;
|
||||
use DateTimeZone;
|
||||
use Magento\Customer\Api\CustomerRepositoryInterface;
|
||||
use Magento\Customer\Model\Session as CustomerSession;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Magento\Framework\Stdlib\CookieManagerInterface;
|
||||
use Magento\Persistent\Helper\Session as PersistentSessionHelper;
|
||||
use Magento\Persistent\Model\Session;
|
||||
use Magento\Persistent\Model\SessionFactory;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Test for synchronize persistent session on login observer
|
||||
*
|
||||
* @see \Magento\Persistent\Observer\SynchronizePersistentOnLoginObserver
|
||||
* @magentoAppArea frontend
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoDataFixture Magento/Customer/_files/customer.php
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class SynchronizePersistentOnLoginObserverTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var SynchronizePersistentOnLoginObserver
|
||||
*/
|
||||
private $model;
|
||||
|
||||
/**
|
||||
* @var ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @var PersistentSessionHelper
|
||||
*/
|
||||
private $persistentSessionHelper;
|
||||
|
||||
/**
|
||||
* @var CustomerRepositoryInterface
|
||||
*/
|
||||
private $customerRepository;
|
||||
|
||||
/**
|
||||
* @var SessionFactory
|
||||
*/
|
||||
private $persistentSessionFactory;
|
||||
|
||||
/**
|
||||
* @var CookieManagerInterface
|
||||
*/
|
||||
private $cookieManager;
|
||||
|
||||
/**
|
||||
* @var CustomerSession
|
||||
*/
|
||||
private $customerSession;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->persistentSessionHelper = $this->objectManager->get(PersistentSessionHelper::class);
|
||||
$this->model = $this->objectManager->get(SynchronizePersistentOnLoginObserver::class);
|
||||
$this->customerRepository = $this->objectManager->get(CustomerRepositoryInterface::class);
|
||||
$this->persistentSessionFactory = $this->objectManager->get(SessionFactory::class);
|
||||
$this->cookieManager = $this->objectManager->get(CookieManagerInterface::class);
|
||||
$this->customerSession = $this->objectManager->get(CustomerSession::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$this->persistentSessionHelper->setRememberMeChecked(null);
|
||||
$this->customerSession->logout();
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that persistent session is created on customer login
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSynchronizePersistentOnLogin(): void
|
||||
{
|
||||
$customer = $this->customerRepository->get('customer@example.com');
|
||||
$sessionModel = $this->persistentSessionFactory->create();
|
||||
$sessionModel->loadByCustomerId($customer->getId());
|
||||
$this->assertNull($sessionModel->getCustomerId());
|
||||
$this->persistentSessionHelper->setRememberMeChecked(true);
|
||||
$this->customerSession->loginById($customer->getId());
|
||||
$sessionModel = $this->persistentSessionFactory->create();
|
||||
$sessionModel->loadByCustomerId($customer->getId());
|
||||
$this->assertEquals($customer->getId(), $sessionModel->getCustomerId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that expired persistent session is renewed on customer login
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testExpiredPersistentSessionShouldBeRenewedOnLogin(): void
|
||||
{
|
||||
$customer = $this->customerRepository->get('customer@example.com');
|
||||
$lastUpdatedAt = (new DateTime('-1day'))->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d H:i:s');
|
||||
$sessionModel = $this->persistentSessionFactory->create();
|
||||
$sessionModel->setCustomerId($customer->getId());
|
||||
$sessionModel->setUpdatedAt($lastUpdatedAt);
|
||||
$sessionModel->save();
|
||||
$this->persistentSessionHelper->setRememberMeChecked(true);
|
||||
$this->customerSession->loginById($customer->getId());
|
||||
$sessionModel = $this->persistentSessionFactory->create();
|
||||
$sessionModel->loadByCustomerId($customer->getId());
|
||||
$this->assertGreaterThan($lastUpdatedAt, $sessionModel->getUpdatedAt());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Persistent/_files/persistent_with_customer_quote_and_cookie.php
|
||||
* @magentoConfigFixture current_store persistent/options/enabled 0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDisabledPersistentSession(): void
|
||||
{
|
||||
$customer = $this->customerRepository->get('customer@example.com');
|
||||
$this->customerSession->loginById($customer->getId());
|
||||
$this->assertNull($this->cookieManager->getCookie(Session::COOKIE_NAME));
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Persistent/_files/persistent_with_customer_quote_and_cookie.php
|
||||
* @magentoConfigFixture current_store persistent/options/enabled 1
|
||||
* @magentoConfigFixture current_store persistent/options/lifetime 0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDisabledPersistentSessionLifetime(): void
|
||||
{
|
||||
$customer = $this->customerRepository->get('customer@example.com');
|
||||
$this->customerSession->loginById($customer->getId());
|
||||
$session = $this->persistentSessionFactory->create()->setLoadExpired()->loadByCustomerId($customer->getId());
|
||||
$this->assertNull($session->getId());
|
||||
$this->assertNull($this->cookieManager->getCookie(Session::COOKIE_NAME));
|
||||
}
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Persistent\Observer;
|
||||
|
||||
use Magento\Customer\Model\Session as CustomerSession;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Magento\Persistent\Model\SessionFactory;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Test for synchronize persistent on logout observer
|
||||
*
|
||||
* @see \Magento\Persistent\Observer\SynchronizePersistentOnLogoutObserver
|
||||
* @magentoDataFixture Magento/Customer/_files/customer.php
|
||||
* @magentoAppArea frontend
|
||||
* @magentoDbIsolation enabled
|
||||
*/
|
||||
class SynchronizePersistentOnLogoutObserverTest extends TestCase
|
||||
{
|
||||
/** @var ObjectManagerInterface */
|
||||
private $objectManager;
|
||||
|
||||
/** @var CustomerSession */
|
||||
private $customerSession;
|
||||
|
||||
/** @var SessionFactory */
|
||||
private $sessionFactory;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->customerSession = $this->objectManager->get(CustomerSession::class);
|
||||
$this->sessionFactory = $this->objectManager->get(SessionFactory::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store persistent/options/enabled 1
|
||||
* @magentoConfigFixture current_store persistent/options/logout_clear 1
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSynchronizePersistentOnLogout(): void
|
||||
{
|
||||
$this->customerSession->loginById(1);
|
||||
$sessionModel = $this->sessionFactory->create();
|
||||
$sessionModel->loadByCookieKey();
|
||||
$this->assertEquals(1, $sessionModel->getCustomerId());
|
||||
$this->customerSession->logout();
|
||||
$sessionModel = $this->sessionFactory->create();
|
||||
$sessionModel->loadByCookieKey();
|
||||
$this->assertNull($sessionModel->getCustomerId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store persistent/options/enabled 1
|
||||
* @magentoConfigFixture current_store persistent/options/logout_clear 0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSynchronizePersistentOnLogoutDisabled(): void
|
||||
{
|
||||
$this->customerSession->loginById(1);
|
||||
$this->customerSession->logout();
|
||||
$sessionModel = $this->sessionFactory->create();
|
||||
$sessionModel->loadByCookieKey();
|
||||
$this->assertEquals(1, $sessionModel->getCustomerId());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user