Skip to content

Commit 501e370

Browse files
committed
Fix implementation and Unit Tests
1 parent 8bbebf3 commit 501e370

File tree

2 files changed

+24
-57
lines changed

2 files changed

+24
-57
lines changed

app/code/Magento/Customer/Controller/Plugin/Account.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,20 @@ class Account
3535
* @var array
3636
*/
3737
private $allowedActions = [];
38-
/**
39-
* @var ActionFlag
40-
*/
41-
private $actionFlag;
4238

4339
/**
4440
* @param RequestInterface $request
4541
* @param Session $customerSession
46-
* @param ActionFlag $actionFlag
4742
* @param array $allowedActions List of actions that are allowed for not authorized users
4843
*/
4944
public function __construct(
5045
RequestInterface $request,
5146
Session $customerSession,
52-
ActionFlag $actionFlag,
5347
array $allowedActions = []
5448
) {
49+
$this->request = $request;
5550
$this->session = $customerSession;
5651
$this->allowedActions = $allowedActions;
57-
$this->request = $request;
58-
$this->actionFlag = $actionFlag;
5952
}
6053

6154
/**
@@ -68,16 +61,11 @@ public function __construct(
6861
public function aroundExecute(AccountInterface $controllerAction, Closure $proceed)
6962
{
7063
if ($this->isActionAllowed()) {
71-
$this->session->setNoReferer(true);
72-
$response = $proceed();
73-
$this->session->unsNoReferer(false);
74-
75-
return $response;
64+
return $proceed();
7665
}
7766

78-
if (!$this->session->authenticate()) {
79-
$this->actionFlag->set('', ActionInterface::FLAG_NO_DISPATCH, true);
80-
}
67+
/** @FIXME Move Authentication and redirect out of Session model */
68+
$this->session->authenticate();
8169
}
8270

8371
/**

app/code/Magento/Customer/Test/Unit/Controller/Plugin/AccountTest.php

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Magento\Customer\Test\Unit\Controller\Plugin;
88

9+
use Closure;
910
use Magento\Customer\Controller\AccountInterface;
1011
use Magento\Customer\Controller\Plugin\Account;
1112
use Magento\Customer\Model\Session;
@@ -83,40 +84,38 @@ protected function setUp()
8384
/**
8485
* @param string $action
8586
* @param array $allowedActions
86-
* @param boolean $isActionAllowed
87+
* @param boolean $isAllowed
8788
* @param boolean $isAuthenticated
8889
*
8990
* @dataProvider beforeExecuteDataProvider
9091
*/
91-
public function testBeforeExecute($action, $allowedActions, $isActionAllowed, $isAuthenticated)
92+
public function testAroundExecuteInterruptsOriginalCallWhenNotAllowed(string $action, array $allowedActions, bool $isAllowed, bool $isAuthenticated)
9293
{
94+
/** @var callable|MockObject $proceedMock */
95+
$proceedMock = $this->getMockBuilder(\stdClass::class)
96+
->setMethods(['__invoke'])
97+
->getMock();
98+
99+
$closureMock = Closure::fromCallable($proceedMock);
100+
93101
$this->requestMock->expects($this->once())
94102
->method('getActionName')
95103
->willReturn($action);
96104

97-
if ($isActionAllowed) {
98-
$this->sessionMock->expects($this->once())
99-
->method('setNoReferer')
100-
->with(true)
101-
->willReturnSelf();
105+
if ($isAllowed) {
106+
$proceedMock->expects($this->once())->method('__invoke')->willReturn($this->resultMock);
102107
} else {
103-
$this->sessionMock->expects($this->once())
104-
->method('authenticate')
105-
->willReturn($isAuthenticated);
106-
if (!$isAuthenticated) {
107-
$this->actionMock->expects($this->once())
108-
->method('getActionFlag')
109-
->willReturn($this->actionFlagMock);
110-
111-
$this->actionFlagMock->expects($this->once())
112-
->method('set')
113-
->with('', ActionInterface::FLAG_NO_DISPATCH, true)
114-
->willReturnSelf();
115-
}
108+
$proceedMock->expects($this->never())->method('__invoke');
116109
}
117110

118111
$plugin = new Account($this->requestMock, $this->sessionMock, $allowedActions);
119-
$plugin->beforeExecute($this->actionMock);
112+
$result = $plugin->aroundExecute($this->actionMock, $closureMock);
113+
114+
if ($isAllowed) {
115+
$this->assertSame($this->resultMock, $result);
116+
} else {
117+
$this->assertNull($result);
118+
}
120119
}
121120

122121
/**
@@ -157,24 +156,4 @@ public function beforeExecuteDataProvider()
157156
],
158157
];
159158
}
160-
161-
public function testAfterExecute()
162-
{
163-
$this->sessionMock->expects($this->once())
164-
->method('unsNoReferer')
165-
->with(false)
166-
->willReturnSelf();
167-
168-
$plugin = (new ObjectManager($this))->getObject(
169-
Account::class,
170-
[
171-
'session' => $this->sessionMock,
172-
'allowedActions' => ['testaction']
173-
]
174-
);
175-
$this->assertSame(
176-
$this->resultMock,
177-
$plugin->afterExecute($this->actionMock, $this->resultMock, $this->requestMock)
178-
);
179-
}
180159
}

0 commit comments

Comments
 (0)