Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SecurityCookieTest.php
Go to the documentation of this file.
1 <?php
8 
10 
14 class SecurityCookieTest extends \PHPUnit\Framework\TestCase
15 {
18 
21 
24 
26  protected $cookieReaderMock;
27 
29  protected $backendDataMock;
30 
32  protected $model;
33 
38  protected function setUp()
39  {
40  $this->phpCookieManagerMock = $this->createPartialMock(
41  \Magento\Framework\Stdlib\Cookie\PhpCookieManager::class,
42  ['setPublicCookie']
43  );
44 
45  $this->cookieMetadataFactoryMock = $this->createPartialMock(
46  \Magento\Framework\Stdlib\Cookie\PublicCookieMetadataFactory::class,
47  ['create']
48  );
49 
50  $this->cookieMetadataMock = $this->createPartialMock(
51  \Magento\Framework\Stdlib\Cookie\PublicCookieMetadata::class,
52  ['setPath', 'setDuration']
53  );
54 
55  $this->cookieReaderMock = $this->createPartialMock(
56  \Magento\Framework\Stdlib\Cookie\CookieReaderInterface::class,
57  ['getCookie']
58  );
59 
60  $this->backendDataMock = $this->createMock(\Magento\Backend\Helper\Data::class);
61 
62  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
63  $this->model = $objectManager->getObject(
64  SecurityCookie::class,
65  [
66  'phpCookieManager' => $this->phpCookieManagerMock,
67  'cookieMetadataFactory' => $this->cookieMetadataFactoryMock,
68  'cookieReader' => $this->cookieReaderMock,
69  'backendData' => $this->backendDataMock
70  ]
71  );
72  }
73 
78  public function testGetLogoutReasonCookie()
79  {
80  $cookie = '123';
81 
82  $this->cookieReaderMock->expects($this->once())
83  ->method('getCookie')
84  ->with(
86  -1
87  )
88  ->willReturn($cookie);
89 
90  $this->assertEquals(intval($cookie), $this->model->getLogoutReasonCookie());
91  }
92 
97  public function testSetLogoutReasonCookie()
98  {
99  $status = '3';
100  $frontName = 'FrontName';
101 
102  $this->createCookieMetaData();
103 
104  $this->backendDataMock->expects($this->once())
105  ->method('getAreaFrontName')
106  ->willReturn($frontName);
107 
108  $this->cookieMetadataMock->expects($this->once())
109  ->method('setPath')
110  ->with('/' . $frontName)
111  ->willReturnSelf();
112 
113  $this->phpCookieManagerMock->expects($this->once())
114  ->method('setPublicCookie')
115  ->with(
117  intval($status),
118  $this->cookieMetadataMock
119  )
120  ->willReturnSelf();
121 
122  $this->assertEquals($this->model, $this->model->setLogoutReasonCookie($status));
123  }
124 
130  {
131  $frontName = 'FrontName';
132 
133  $this->createCookieMetaData();
134 
135  $this->backendDataMock->expects($this->once())
136  ->method('getAreaFrontName')
137  ->willReturn($frontName);
138 
139  $this->cookieMetadataMock->expects($this->once())
140  ->method('setPath')
141  ->with('/' . $frontName)
142  ->willReturnSelf();
143 
144  $this->cookieMetadataMock->expects($this->once())
145  ->method('setDuration')
146  ->with(-1)
147  ->willReturnSelf();
148 
149  $this->phpCookieManagerMock->expects($this->once())
150  ->method('setPublicCookie')
151  ->with(
153  '',
154  $this->cookieMetadataMock
155  )
156  ->willReturnSelf();
157 
158  $this->assertEquals($this->model, $this->model->deleteLogoutReasonCookie());
159  }
160 
164  protected function createCookieMetaData()
165  {
166  $this->cookieMetadataFactoryMock->expects($this->once())
167  ->method('create')
168  ->willReturn($this->cookieMetadataMock);
169  }
170 }
$objectManager
Definition: bootstrap.php:17
$status
Definition: order_status.php:8