AuthenticationTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Test\Unit\App\Action\Plugin;
  7. use Magento\Backend\App\Action\Plugin\Authentication;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. /**
  10. * Class AuthenticationTest
  11. */
  12. class AuthenticationTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Backend\Model\Auth | \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $auth;
  18. /**
  19. * @var Authentication
  20. */
  21. protected $plugin;
  22. protected function setUp()
  23. {
  24. $this->auth = $this->createPartialMock(
  25. \Magento\Backend\Model\Auth::class,
  26. ['getUser', 'isLoggedIn', 'getAuthStorage']
  27. );
  28. $objectManager = new ObjectManager($this);
  29. $this->plugin = $objectManager->getObject(
  30. \Magento\Backend\App\Action\Plugin\Authentication::class,
  31. ['auth' => $this->auth]
  32. );
  33. }
  34. protected function tearDown()
  35. {
  36. $this->auth = null;
  37. $this->plugin = null;
  38. }
  39. public function testAroundDispatchProlongStorage()
  40. {
  41. $subject = $this->createMock(\Magento\Backend\Controller\Adminhtml\Index::class);
  42. $request = $this->createPartialMock(\Magento\Framework\App\Request\Http::class, ['getActionName']);
  43. $user = $this->createPartialMock(\Magento\User\Model\User::class, ['reload', '__wakeup']);
  44. $storage = $this->createPartialMock(\Magento\Backend\Model\Auth\Session::class, ['prolong', 'refreshAcl']);
  45. $expectedResult = 'expectedResult';
  46. $action = 'index';
  47. $loggedIn = true;
  48. $this->auth->expects($this->any())
  49. ->method('getUser')
  50. ->will($this->returnValue($user));
  51. $this->auth->expects($this->once())
  52. ->method('isLoggedIn')
  53. ->will($this->returnValue($loggedIn));
  54. $this->auth->expects($this->any())
  55. ->method('getAuthStorage')
  56. ->will($this->returnValue($storage));
  57. $request->expects($this->once())
  58. ->method('getActionName')
  59. ->will($this->returnValue($action));
  60. $user->expects($this->once())
  61. ->method('reload');
  62. $storage->expects($this->at(0))
  63. ->method('prolong');
  64. $storage->expects($this->at(1))
  65. ->method('refreshAcl');
  66. $proceed = function ($request) use ($expectedResult) {
  67. return $expectedResult;
  68. };
  69. $this->assertEquals($expectedResult, $this->plugin->aroundDispatch($subject, $proceed, $request));
  70. }
  71. /**
  72. * Calls aroundDispatch to access protected method _processNotLoggedInUser
  73. *
  74. * Data provider supplies different possibilities of request parameters and properties
  75. * @dataProvider processNotLoggedInUserDataProvider
  76. */
  77. public function testProcessNotLoggedInUser($isIFrameParam, $isAjaxParam, $isForwardedFlag)
  78. {
  79. $subject = $this->getMockBuilder(\Magento\Backend\Controller\Adminhtml\Index::class)
  80. ->disableOriginalConstructor()
  81. ->getMock();
  82. $request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  83. ->disableOriginalConstructor()
  84. ->getMock();
  85. $storage = $this->getMockBuilder(\Magento\Backend\Model\Auth\Session::class)
  86. ->disableOriginalConstructor()
  87. ->getMock();
  88. // Stubs to control the flow of execution in aroundDispatch
  89. $this->auth->expects($this->any())->method('getAuthStorage')->will($this->returnValue($storage));
  90. $request->expects($this->once())->method('getActionName')->will($this->returnValue('non/open/action/name'));
  91. $this->auth->expects($this->any())->method('getUser')->willReturn(false);
  92. $this->auth->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
  93. $request->expects($this->any())->method('getPost')->willReturn(false);
  94. // Test cases and expectations based on provided data
  95. $request->expects($this->once())->method('isForwarded')->willReturn($isForwardedFlag);
  96. $getParamCalls = 0;
  97. $actionName = '';
  98. // If forwarded flag is set, getParam never gets called
  99. if (!$isForwardedFlag) {
  100. if ($isIFrameParam) {
  101. $getParamCalls = 1;
  102. $actionName = 'deniedIframe';
  103. } elseif ($isAjaxParam) {
  104. $getParamCalls = 2;
  105. $actionName = 'deniedJson';
  106. } else {
  107. $getParamCalls = 2;
  108. $actionName = 'login';
  109. }
  110. }
  111. $requestParams = [
  112. ['isIframe', null, $isIFrameParam],
  113. ['isAjax', null, $isAjaxParam]
  114. ];
  115. $setterCalls = $isForwardedFlag ? 0 : 1;
  116. $request->expects($this->exactly($getParamCalls))->method('getParam')->willReturnMap($requestParams);
  117. $request->expects($this->exactly($setterCalls))->method('setForwarded')->with(true)->willReturnSelf();
  118. $request->expects($this->exactly($setterCalls))->method('setRouteName')->with('adminhtml')->willReturnSelf();
  119. $request->expects($this->exactly($setterCalls))->method('setControllerName')->with('auth')->willReturnSelf();
  120. $request->expects($this->exactly($setterCalls))->method('setActionName')->with($actionName)->willReturnSelf();
  121. $request->expects($this->exactly($setterCalls))->method('setDispatched')->with(false)->willReturnSelf();
  122. $expectedResult = 'expectedResult';
  123. $proceed = function ($request) use ($expectedResult) {
  124. return $expectedResult;
  125. };
  126. $this->assertEquals($expectedResult, $this->plugin->aroundDispatch($subject, $proceed, $request));
  127. }
  128. /**
  129. * @return array
  130. */
  131. public function processNotLoggedInUserDataProvider()
  132. {
  133. return [
  134. 'iFrame' => [true, false, false],
  135. 'Ajax' => [false, true, false],
  136. 'Neither iFrame nor Ajax' => [false, false, false],
  137. 'Forwarded request' => [true, true, true]
  138. ];
  139. }
  140. }