123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Security\Test\Unit\Model;
- use Magento\Security\Model\SecurityCookie;
- /**
- * Test class for \Magento\Security\Model\SecurityCookie testing
- */
- class SecurityCookieTest extends \PHPUnit\Framework\TestCase
- {
- /** @var \Magento\Framework\Stdlib\Cookie\PhpCookieManager */
- protected $phpCookieManagerMock;
- /** @var \Magento\Framework\Stdlib\Cookie\PublicCookieMetadataFactory */
- protected $cookieMetadataFactoryMock;
- /** @var \Magento\Framework\Stdlib\Cookie\PublicCookieMetadata */
- protected $cookieMetadataMock;
- /** @var \Magento\Framework\Stdlib\Cookie\CookieReaderInterface */
- protected $cookieReaderMock;
- /** @var \Magento\Framework\Stdlib\Cookie\PublicCookieMetadata */
- protected $backendDataMock;
- /** @var SecurityCookie */
- protected $model;
- /**
- * Init mocks for tests
- * @return void
- */
- protected function setUp()
- {
- $this->phpCookieManagerMock = $this->createPartialMock(
- \Magento\Framework\Stdlib\Cookie\PhpCookieManager::class,
- ['setPublicCookie']
- );
- $this->cookieMetadataFactoryMock = $this->createPartialMock(
- \Magento\Framework\Stdlib\Cookie\PublicCookieMetadataFactory::class,
- ['create']
- );
- $this->cookieMetadataMock = $this->createPartialMock(
- \Magento\Framework\Stdlib\Cookie\PublicCookieMetadata::class,
- ['setPath', 'setDuration']
- );
- $this->cookieReaderMock = $this->createPartialMock(
- \Magento\Framework\Stdlib\Cookie\CookieReaderInterface::class,
- ['getCookie']
- );
- $this->backendDataMock = $this->createMock(\Magento\Backend\Helper\Data::class);
- $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->model = $objectManager->getObject(
- SecurityCookie::class,
- [
- 'phpCookieManager' => $this->phpCookieManagerMock,
- 'cookieMetadataFactory' => $this->cookieMetadataFactoryMock,
- 'cookieReader' => $this->cookieReaderMock,
- 'backendData' => $this->backendDataMock
- ]
- );
- }
- /**
- * Test get logout reason cookie
- * @return void
- */
- public function testGetLogoutReasonCookie()
- {
- $cookie = '123';
- $this->cookieReaderMock->expects($this->once())
- ->method('getCookie')
- ->with(
- SecurityCookie::LOGOUT_REASON_CODE_COOKIE_NAME,
- -1
- )
- ->willReturn($cookie);
- $this->assertEquals((int)$cookie, $this->model->getLogoutReasonCookie());
- }
- /**
- * Test set logout reason cookie
- * @return void
- */
- public function testSetLogoutReasonCookie()
- {
- $status = '3';
- $frontName = 'FrontName';
- $this->createCookieMetaData();
- $this->backendDataMock->expects($this->once())
- ->method('getAreaFrontName')
- ->willReturn($frontName);
- $this->cookieMetadataMock->expects($this->once())
- ->method('setPath')
- ->with('/' . $frontName)
- ->willReturnSelf();
- $this->phpCookieManagerMock->expects($this->once())
- ->method('setPublicCookie')
- ->with(
- SecurityCookie::LOGOUT_REASON_CODE_COOKIE_NAME,
- (int)$status,
- $this->cookieMetadataMock
- )
- ->willReturnSelf();
- $this->assertEquals($this->model, $this->model->setLogoutReasonCookie($status));
- }
- /**
- * Test delete logout reason cookie
- * @return void
- */
- public function testDeleteLogoutReasonCookie()
- {
- $frontName = 'FrontName';
- $this->createCookieMetaData();
- $this->backendDataMock->expects($this->once())
- ->method('getAreaFrontName')
- ->willReturn($frontName);
- $this->cookieMetadataMock->expects($this->once())
- ->method('setPath')
- ->with('/' . $frontName)
- ->willReturnSelf();
- $this->cookieMetadataMock->expects($this->once())
- ->method('setDuration')
- ->with(-1)
- ->willReturnSelf();
- $this->phpCookieManagerMock->expects($this->once())
- ->method('setPublicCookie')
- ->with(
- SecurityCookie::LOGOUT_REASON_CODE_COOKIE_NAME,
- '',
- $this->cookieMetadataMock
- )
- ->willReturnSelf();
- $this->assertEquals($this->model, $this->model->deleteLogoutReasonCookie());
- }
- /**
- * @return void
- */
- protected function createCookieMetaData()
- {
- $this->cookieMetadataFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($this->cookieMetadataMock);
- }
- }
|