AccountTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Controller\Plugin;
  7. use Magento\Customer\Controller\Plugin\Account;
  8. use Magento\Customer\Model\Session;
  9. use Magento\Framework\App\ActionFlag;
  10. use Magento\Framework\App\ActionInterface;
  11. use Magento\Framework\App\Action\AbstractAction;
  12. use Magento\Framework\App\Request\Http;
  13. use Magento\Framework\Controller\ResultInterface;
  14. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  15. class AccountTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var string
  19. */
  20. const EXPECTED_VALUE = 'expected_value';
  21. /**
  22. * @var Account
  23. */
  24. protected $plugin;
  25. /**
  26. * @var Session | \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $session;
  29. /**
  30. * @var AbstractAction | \PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $subject;
  33. /**
  34. * @var Http | \PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $request;
  37. /**
  38. * @var ActionFlag | \PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $actionFlag;
  41. /**
  42. * @var ResultInterface|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. private $resultInterface;
  45. protected function setUp()
  46. {
  47. $this->session = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
  48. ->disableOriginalConstructor()
  49. ->setMethods([
  50. 'setNoReferer',
  51. 'unsNoReferer',
  52. 'authenticate',
  53. ])
  54. ->getMock();
  55. $this->subject = $this->getMockBuilder(AbstractAction::class)
  56. ->setMethods([
  57. 'getActionFlag',
  58. ])
  59. ->disableOriginalConstructor()
  60. ->getMockForAbstractClass();
  61. $this->request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  62. ->disableOriginalConstructor()
  63. ->setMethods([
  64. 'getActionName',
  65. ])
  66. ->getMock();
  67. $this->resultInterface = $this->getMockBuilder(ResultInterface::class)
  68. ->getMockForAbstractClass();
  69. $this->actionFlag = $this->getMockBuilder(\Magento\Framework\App\ActionFlag::class)
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. }
  73. /**
  74. * @param string $action
  75. * @param array $allowedActions
  76. * @param boolean $isActionAllowed
  77. * @param boolean $isAuthenticated
  78. *
  79. * @dataProvider beforeDispatchDataProvider
  80. */
  81. public function testBeforeDispatch(
  82. $action,
  83. $allowedActions,
  84. $isActionAllowed,
  85. $isAuthenticated
  86. ) {
  87. $this->request->expects($this->once())
  88. ->method('getActionName')
  89. ->willReturn($action);
  90. if ($isActionAllowed) {
  91. $this->session->expects($this->once())
  92. ->method('setNoReferer')
  93. ->with(true)
  94. ->willReturnSelf();
  95. } else {
  96. $this->session->expects($this->once())
  97. ->method('authenticate')
  98. ->willReturn($isAuthenticated);
  99. if (!$isAuthenticated) {
  100. $this->subject->expects($this->once())
  101. ->method('getActionFlag')
  102. ->willReturn($this->actionFlag);
  103. $this->actionFlag->expects($this->once())
  104. ->method('set')
  105. ->with('', ActionInterface::FLAG_NO_DISPATCH, true)
  106. ->willReturnSelf();
  107. }
  108. }
  109. $plugin = new Account($this->session, $allowedActions);
  110. $plugin->beforeDispatch($this->subject, $this->request);
  111. }
  112. /**
  113. * @return array
  114. */
  115. public function beforeDispatchDataProvider()
  116. {
  117. return [
  118. [
  119. 'action' => 'TestAction',
  120. 'allowed_actions' => ['TestAction'],
  121. 'is_action_allowed' => 1,
  122. 'is_authenticated' => 0,
  123. ],
  124. [
  125. 'action' => 'testaction',
  126. 'allowed_actions' => ['testaction'],
  127. 'is_action_allowed' => 1,
  128. 'is_authenticated' => 0,
  129. ],
  130. [
  131. 'action' => 'wrongaction',
  132. 'allowed_actions' => ['testaction'],
  133. 'is_action_allowed' => 0,
  134. 'is_authenticated' => 0,
  135. ],
  136. [
  137. 'action' => 'wrongaction',
  138. 'allowed_actions' => ['testaction'],
  139. 'is_action_allowed' => 0,
  140. 'is_authenticated' => 1,
  141. ],
  142. [
  143. 'action' => 'wrongaction',
  144. 'allowed_actions' => [],
  145. 'is_action_allowed' => 0,
  146. 'is_authenticated' => 1,
  147. ],
  148. ];
  149. }
  150. public function testAfterDispatch()
  151. {
  152. $this->session->expects($this->once())
  153. ->method('unsNoReferer')
  154. ->with(false)
  155. ->willReturnSelf();
  156. $plugin = (new ObjectManager($this))->getObject(
  157. Account::class,
  158. [
  159. 'session' => $this->session,
  160. 'allowedActions' => ['testaction']
  161. ]
  162. );
  163. $this->assertSame(
  164. $this->resultInterface,
  165. $plugin->afterDispatch($this->subject, $this->resultInterface, $this->request)
  166. );
  167. }
  168. }