AdminSessionUserContextTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\User\Test\Unit\Model\Authorization;
  7. use Magento\Authorization\Model\UserContextInterface;
  8. /**
  9. * Tests Magento\User\Model\Authorization\AdminSessionUserContext
  10. */
  11. class AdminSessionUserContextTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  15. */
  16. protected $objectManager;
  17. /**
  18. * @var \Magento\User\Model\Authorization\AdminSessionUserContext
  19. */
  20. protected $adminSessionUserContext;
  21. /**
  22. * @var \Magento\Backend\Model\Auth\Session
  23. */
  24. protected $adminSession;
  25. protected function setUp()
  26. {
  27. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  28. $this->adminSession = $this->getMockBuilder(\Magento\Backend\Model\Auth\Session::class)
  29. ->disableOriginalConstructor()
  30. ->setMethods(['hasUser', 'getUser', 'getId'])
  31. ->getMock();
  32. $this->adminSessionUserContext = $this->objectManager->getObject(
  33. \Magento\User\Model\Authorization\AdminSessionUserContext::class,
  34. ['adminSession' => $this->adminSession]
  35. );
  36. }
  37. public function testGetUserIdExist()
  38. {
  39. $userId = 1;
  40. $this->setupUserId($userId);
  41. $this->assertEquals($userId, $this->adminSessionUserContext->getUserId());
  42. }
  43. public function testGetUserIdDoesNotExist()
  44. {
  45. $userId = null;
  46. $this->setupUserId($userId);
  47. $this->assertEquals($userId, $this->adminSessionUserContext->getUserId());
  48. }
  49. public function testGetUserType()
  50. {
  51. $this->assertEquals(UserContextInterface::USER_TYPE_ADMIN, $this->adminSessionUserContext->getUserType());
  52. }
  53. /**
  54. * @param int|null $userId
  55. * @return void
  56. */
  57. public function setupUserId($userId)
  58. {
  59. $this->adminSession->expects($this->once())
  60. ->method('hasUser')
  61. ->will($this->returnValue($userId));
  62. if ($userId) {
  63. $this->adminSession->expects($this->once())
  64. ->method('getUser')
  65. ->will($this->returnSelf());
  66. $this->adminSession->expects($this->once())
  67. ->method('getId')
  68. ->will($this->returnValue($userId));
  69. }
  70. }
  71. }