SecurityCookieTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Security\Test\Unit\Model;
  7. use Magento\Security\Model\SecurityCookie;
  8. /**
  9. * Test class for \Magento\Security\Model\SecurityCookie testing
  10. */
  11. class SecurityCookieTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /** @var \Magento\Framework\Stdlib\Cookie\PhpCookieManager */
  14. protected $phpCookieManagerMock;
  15. /** @var \Magento\Framework\Stdlib\Cookie\PublicCookieMetadataFactory */
  16. protected $cookieMetadataFactoryMock;
  17. /** @var \Magento\Framework\Stdlib\Cookie\PublicCookieMetadata */
  18. protected $cookieMetadataMock;
  19. /** @var \Magento\Framework\Stdlib\Cookie\CookieReaderInterface */
  20. protected $cookieReaderMock;
  21. /** @var \Magento\Framework\Stdlib\Cookie\PublicCookieMetadata */
  22. protected $backendDataMock;
  23. /** @var SecurityCookie */
  24. protected $model;
  25. /**
  26. * Init mocks for tests
  27. * @return void
  28. */
  29. protected function setUp()
  30. {
  31. $this->phpCookieManagerMock = $this->createPartialMock(
  32. \Magento\Framework\Stdlib\Cookie\PhpCookieManager::class,
  33. ['setPublicCookie']
  34. );
  35. $this->cookieMetadataFactoryMock = $this->createPartialMock(
  36. \Magento\Framework\Stdlib\Cookie\PublicCookieMetadataFactory::class,
  37. ['create']
  38. );
  39. $this->cookieMetadataMock = $this->createPartialMock(
  40. \Magento\Framework\Stdlib\Cookie\PublicCookieMetadata::class,
  41. ['setPath', 'setDuration']
  42. );
  43. $this->cookieReaderMock = $this->createPartialMock(
  44. \Magento\Framework\Stdlib\Cookie\CookieReaderInterface::class,
  45. ['getCookie']
  46. );
  47. $this->backendDataMock = $this->createMock(\Magento\Backend\Helper\Data::class);
  48. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  49. $this->model = $objectManager->getObject(
  50. SecurityCookie::class,
  51. [
  52. 'phpCookieManager' => $this->phpCookieManagerMock,
  53. 'cookieMetadataFactory' => $this->cookieMetadataFactoryMock,
  54. 'cookieReader' => $this->cookieReaderMock,
  55. 'backendData' => $this->backendDataMock
  56. ]
  57. );
  58. }
  59. /**
  60. * Test get logout reason cookie
  61. * @return void
  62. */
  63. public function testGetLogoutReasonCookie()
  64. {
  65. $cookie = '123';
  66. $this->cookieReaderMock->expects($this->once())
  67. ->method('getCookie')
  68. ->with(
  69. SecurityCookie::LOGOUT_REASON_CODE_COOKIE_NAME,
  70. -1
  71. )
  72. ->willReturn($cookie);
  73. $this->assertEquals((int)$cookie, $this->model->getLogoutReasonCookie());
  74. }
  75. /**
  76. * Test set logout reason cookie
  77. * @return void
  78. */
  79. public function testSetLogoutReasonCookie()
  80. {
  81. $status = '3';
  82. $frontName = 'FrontName';
  83. $this->createCookieMetaData();
  84. $this->backendDataMock->expects($this->once())
  85. ->method('getAreaFrontName')
  86. ->willReturn($frontName);
  87. $this->cookieMetadataMock->expects($this->once())
  88. ->method('setPath')
  89. ->with('/' . $frontName)
  90. ->willReturnSelf();
  91. $this->phpCookieManagerMock->expects($this->once())
  92. ->method('setPublicCookie')
  93. ->with(
  94. SecurityCookie::LOGOUT_REASON_CODE_COOKIE_NAME,
  95. (int)$status,
  96. $this->cookieMetadataMock
  97. )
  98. ->willReturnSelf();
  99. $this->assertEquals($this->model, $this->model->setLogoutReasonCookie($status));
  100. }
  101. /**
  102. * Test delete logout reason cookie
  103. * @return void
  104. */
  105. public function testDeleteLogoutReasonCookie()
  106. {
  107. $frontName = 'FrontName';
  108. $this->createCookieMetaData();
  109. $this->backendDataMock->expects($this->once())
  110. ->method('getAreaFrontName')
  111. ->willReturn($frontName);
  112. $this->cookieMetadataMock->expects($this->once())
  113. ->method('setPath')
  114. ->with('/' . $frontName)
  115. ->willReturnSelf();
  116. $this->cookieMetadataMock->expects($this->once())
  117. ->method('setDuration')
  118. ->with(-1)
  119. ->willReturnSelf();
  120. $this->phpCookieManagerMock->expects($this->once())
  121. ->method('setPublicCookie')
  122. ->with(
  123. SecurityCookie::LOGOUT_REASON_CODE_COOKIE_NAME,
  124. '',
  125. $this->cookieMetadataMock
  126. )
  127. ->willReturnSelf();
  128. $this->assertEquals($this->model, $this->model->deleteLogoutReasonCookie());
  129. }
  130. /**
  131. * @return void
  132. */
  133. protected function createCookieMetaData()
  134. {
  135. $this->cookieMetadataFactoryMock->expects($this->once())
  136. ->method('create')
  137. ->willReturn($this->cookieMetadataMock);
  138. }
  139. }