SessionTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Test\Unit\Model\Auth;
  7. use Magento\Backend\Model\Auth\Session;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. /**
  10. * Class SessionTest tests Magento\Backend\Model\Auth\Session
  11. *
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class SessionTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var \Magento\Backend\App\Config | \PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $config;
  20. /**
  21. * @var \Magento\Framework\Session\Config | \PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $sessionConfig;
  24. /**
  25. * @var \Magento\Framework\Stdlib\CookieManagerInterface | \PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $cookieManager;
  28. /**
  29. * @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory | \PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $cookieMetadataFactory;
  32. /**
  33. * @var \Magento\Framework\Session\Storage | \PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $storage;
  36. /**
  37. * @var \Magento\Framework\Acl\Builder | \PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $aclBuilder;
  40. /**
  41. * @var Session
  42. */
  43. protected $session;
  44. protected function setUp()
  45. {
  46. $this->cookieMetadataFactory = $this->createPartialMock(
  47. \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class,
  48. ['createPublicCookieMetadata']
  49. );
  50. $this->config = $this->createPartialMock(\Magento\Backend\App\Config::class, ['getValue']);
  51. $this->cookieManager = $this->createPartialMock(
  52. \Magento\Framework\Stdlib\Cookie\PhpCookieManager::class,
  53. ['getCookie', 'setPublicCookie']
  54. );
  55. $this->storage = $this->createPartialMock(
  56. \Magento\Framework\Session\Storage::class,
  57. ['getUser', 'getAcl', 'setAcl']
  58. );
  59. $this->sessionConfig = $this->createPartialMock(
  60. \Magento\Framework\Session\Config::class,
  61. ['getCookiePath', 'getCookieDomain', 'getCookieSecure', 'getCookieHttpOnly']
  62. );
  63. $this->aclBuilder = $this->getMockBuilder(\Magento\Framework\Acl\Builder::class)
  64. ->disableOriginalConstructor()
  65. ->getMock();
  66. $objectManager = new ObjectManager($this);
  67. $this->session = $objectManager->getObject(
  68. \Magento\Backend\Model\Auth\Session::class,
  69. [
  70. 'config' => $this->config,
  71. 'sessionConfig' => $this->sessionConfig,
  72. 'cookieManager' => $this->cookieManager,
  73. 'cookieMetadataFactory' => $this->cookieMetadataFactory,
  74. 'storage' => $this->storage,
  75. 'aclBuilder' => $this->aclBuilder
  76. ]
  77. );
  78. }
  79. protected function tearDown()
  80. {
  81. $this->config = null;
  82. $this->sessionConfig = null;
  83. $this->session = null;
  84. }
  85. /**
  86. * @dataProvider refreshAclDataProvider
  87. * @param $isUserPassedViaParams
  88. */
  89. public function testRefreshAcl($isUserPassedViaParams)
  90. {
  91. $aclMock = $this->getMockBuilder(\Magento\Framework\Acl::class)->disableOriginalConstructor()->getMock();
  92. $this->aclBuilder->expects($this->any())->method('getAcl')->willReturn($aclMock);
  93. $userMock = $this->getMockBuilder(\Magento\User\Model\User::class)
  94. ->setMethods(['getReloadAclFlag', 'setReloadAclFlag', 'unsetData', 'save'])
  95. ->disableOriginalConstructor()
  96. ->getMock();
  97. $userMock->expects($this->any())->method('getReloadAclFlag')->willReturn(true);
  98. $userMock->expects($this->once())->method('setReloadAclFlag')->with('0')->willReturnSelf();
  99. $userMock->expects($this->once())->method('save');
  100. $this->storage->expects($this->once())->method('setAcl')->with($aclMock);
  101. $this->storage->expects($this->any())->method('getAcl')->willReturn($aclMock);
  102. if ($isUserPassedViaParams) {
  103. $this->session->refreshAcl($userMock);
  104. } else {
  105. $this->storage->expects($this->once())->method('getUser')->willReturn($userMock);
  106. $this->session->refreshAcl();
  107. }
  108. $this->assertSame($aclMock, $this->session->getAcl());
  109. }
  110. /**
  111. * @return array
  112. */
  113. public function refreshAclDataProvider()
  114. {
  115. return [
  116. 'User set via params' => [true],
  117. 'User set to session object' => [false]
  118. ];
  119. }
  120. public function testIsLoggedInPositive()
  121. {
  122. $user = $this->createPartialMock(\Magento\User\Model\User::class, ['getId', '__wakeup']);
  123. $user->expects($this->once())
  124. ->method('getId')
  125. ->will($this->returnValue(1));
  126. $this->storage->expects($this->any())
  127. ->method('getUser')
  128. ->will($this->returnValue($user));
  129. $this->assertTrue($this->session->isLoggedIn());
  130. }
  131. public function testProlong()
  132. {
  133. $name = session_name();
  134. $cookie = 'cookie';
  135. $lifetime = 900;
  136. $path = '/';
  137. $domain = 'magento2';
  138. $secure = true;
  139. $httpOnly = true;
  140. $this->config->expects($this->once())
  141. ->method('getValue')
  142. ->with(\Magento\Backend\Model\Auth\Session::XML_PATH_SESSION_LIFETIME)
  143. ->willReturn($lifetime);
  144. $cookieMetadata = $this->createMock(\Magento\Framework\Stdlib\Cookie\PublicCookieMetadata::class);
  145. $cookieMetadata->expects($this->once())
  146. ->method('setDuration')
  147. ->with($lifetime)
  148. ->will($this->returnSelf());
  149. $cookieMetadata->expects($this->once())
  150. ->method('setPath')
  151. ->with($path)
  152. ->will($this->returnSelf());
  153. $cookieMetadata->expects($this->once())
  154. ->method('setDomain')
  155. ->with($domain)
  156. ->will($this->returnSelf());
  157. $cookieMetadata->expects($this->once())
  158. ->method('setSecure')
  159. ->with($secure)
  160. ->will($this->returnSelf());
  161. $cookieMetadata->expects($this->once())
  162. ->method('setHttpOnly')
  163. ->with($httpOnly)
  164. ->will($this->returnSelf());
  165. $this->cookieMetadataFactory->expects($this->once())
  166. ->method('createPublicCookieMetadata')
  167. ->will($this->returnValue($cookieMetadata));
  168. $this->cookieManager->expects($this->once())
  169. ->method('getCookie')
  170. ->with($name)
  171. ->will($this->returnValue($cookie));
  172. $this->cookieManager->expects($this->once())
  173. ->method('setPublicCookie')
  174. ->with($name, $cookie, $cookieMetadata);
  175. $this->sessionConfig->expects($this->once())
  176. ->method('getCookiePath')
  177. ->will($this->returnValue($path));
  178. $this->sessionConfig->expects($this->once())
  179. ->method('getCookieDomain')
  180. ->will($this->returnValue($domain));
  181. $this->sessionConfig->expects($this->once())
  182. ->method('getCookieSecure')
  183. ->will($this->returnValue($secure));
  184. $this->sessionConfig->expects($this->once())
  185. ->method('getCookieHttpOnly')
  186. ->will($this->returnValue($httpOnly));
  187. $this->session->prolong();
  188. $this->assertLessThanOrEqual(time(), $this->session->getUpdatedAt());
  189. }
  190. /**
  191. * @dataProvider isAllowedDataProvider
  192. * @param bool $isUserDefined
  193. * @param bool $isAclDefined
  194. * @param bool $isAllowed
  195. * @param true $expectedResult
  196. */
  197. public function testIsAllowed($isUserDefined, $isAclDefined, $isAllowed, $expectedResult)
  198. {
  199. $userAclRole = 'userAclRole';
  200. if ($isAclDefined) {
  201. $aclMock = $this->getMockBuilder(\Magento\Framework\Acl::class)->disableOriginalConstructor()->getMock();
  202. $this->storage->expects($this->any())->method('getAcl')->willReturn($aclMock);
  203. }
  204. if ($isUserDefined) {
  205. $userMock = $this->getMockBuilder(\Magento\User\Model\User::class)->disableOriginalConstructor()->getMock();
  206. $this->storage->expects($this->once())->method('getUser')->willReturn($userMock);
  207. }
  208. if ($isAclDefined && $isUserDefined) {
  209. $userMock->expects($this->any())->method('getAclRole')->willReturn($userAclRole);
  210. $aclMock->expects($this->once())->method('isAllowed')->with($userAclRole)->willReturn($isAllowed);
  211. }
  212. $this->assertEquals($expectedResult, $this->session->isAllowed('resource'));
  213. }
  214. /**
  215. * @return array
  216. */
  217. public function isAllowedDataProvider()
  218. {
  219. return [
  220. "Negative: User not defined" => [false, true, true, false],
  221. "Negative: Acl not defined" => [true, false, true, false],
  222. "Negative: Permission denied" => [true, true, false, false],
  223. "Positive: Permission granted" => [true, true, false, false],
  224. ];
  225. }
  226. /**
  227. * @dataProvider firstPageAfterLoginDataProvider
  228. * @param bool $isFirstPageAfterLogin
  229. */
  230. public function testFirstPageAfterLogin($isFirstPageAfterLogin)
  231. {
  232. $this->session->setIsFirstPageAfterLogin($isFirstPageAfterLogin);
  233. $this->assertEquals($isFirstPageAfterLogin, $this->session->isFirstPageAfterLogin());
  234. }
  235. /**
  236. * @return array
  237. */
  238. public function firstPageAfterLoginDataProvider()
  239. {
  240. return [
  241. 'First page after login' => [true],
  242. 'Not first page after login' => [false],
  243. ];
  244. }
  245. }