Skip to content

Commit 682097c

Browse files
committed
MC-32243: [Magento Cloud] - "Invalid Form Key. Please refresh the page." not being translated
1 parent f3df323 commit 682097c

File tree

4 files changed

+97
-7
lines changed

4 files changed

+97
-7
lines changed

dev/tests/integration/testsuite/Magento/Customer/Controller/Account/LoginPostTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Customer\Model\Url;
1212
use Magento\Framework\App\Request\Http as HttpRequest;
1313
use Magento\Framework\Message\MessageInterface;
14+
use Magento\Framework\Phrase;
1415
use Magento\Framework\Url\EncoderInterface;
1516
use Magento\TestFramework\TestCase\AbstractController;
1617

@@ -151,6 +152,28 @@ public function testLoginWithRedirectToDashboard(): void
151152
$this->assertRedirect($this->stringContains('customer/account/'));
152153
}
153154

155+
/**
156+
* @magentoConfigFixture current_store customer/startup/redirect_dashboard 1
157+
* @magentoConfigFixture current_store customer/captcha/enable 0
158+
*
159+
* @magentoDataFixture Magento/Customer/_files/customer.php
160+
*
161+
* @return void
162+
*/
163+
public function testNoFormKeyLoginPostAction(): void
164+
{
165+
$this->prepareRequest('customer@example.com', 'password');
166+
$this->getRequest()->setPostValue('form_key', null);
167+
$this->getRequest()->setParam(Url::REFERER_QUERY_PARAM_NAME, $this->urlEncoder->encode('test_redirect'));
168+
$this->dispatch('customer/account/loginPost');
169+
$this->assertFalse($this->session->isLoggedIn());
170+
$this->assertRedirect($this->stringContains('customer/account/'));
171+
$this->assertSessionMessages(
172+
$this->equalTo([new Phrase('Invalid Form Key. Please refresh the page.')]),
173+
MessageInterface::TYPE_ERROR
174+
);
175+
}
176+
154177
/**
155178
* Prepare request
156179
*

dev/tests/integration/testsuite/Magento/Customer/Controller/AccountTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Magento\Framework\App\Request\Http as HttpRequest;
1717
use Magento\Framework\Data\Form\FormKey;
1818
use Magento\Framework\Message\MessageInterface;
19+
use Magento\Framework\Phrase;
1920
use Magento\Framework\Serialize\Serializer\Json;
2021
use Magento\Framework\Stdlib\CookieManagerInterface;
2122
use Magento\Store\Model\StoreManager;
@@ -227,6 +228,10 @@ public function testNoFormKeyCreatePostAction()
227228

228229
$this->assertNull($this->getCustomerByEmail('test1@email.com'));
229230
$this->assertRedirect($this->stringEndsWith('customer/account/create/'));
231+
$this->assertSessionMessages(
232+
$this->equalTo([new Phrase('Invalid Form Key. Please refresh the page.')]),
233+
MessageInterface::TYPE_ERROR
234+
);
230235
}
231236

232237
/**
@@ -612,6 +617,10 @@ public function testWrongConfirmationEditPostAction()
612617
*
613618
* @magentoDataFixture Magento/Customer/_files/customer_confirmation_config_enable.php
614619
* @return void
620+
* @throws \Magento\Framework\Exception\InputException
621+
* @throws \Magento\Framework\Exception\LocalizedException
622+
* @throws \Magento\Framework\Exception\NoSuchEntityException
623+
* @throws \Magento\Framework\Stdlib\Cookie\FailureToSendException
615624
*/
616625
public function testRegisterCustomerWithEmailConfirmation(): void
617626
{
@@ -725,6 +734,10 @@ public function testConfirmationEmailWithSpecialCharacters(): void
725734
* @magentoConfigFixture current_store customer/captcha/enable 0
726735
*
727736
* @return void
737+
* @throws \Magento\Framework\Exception\InputException
738+
* @throws \Magento\Framework\Exception\LocalizedException
739+
* @throws \Magento\Framework\Exception\NoSuchEntityException
740+
* @throws \Magento\Framework\Exception\State\InputMismatchException
728741
*/
729742
public function testResetPasswordWhenEmailChanged(): void
730743
{

lib/internal/Magento/Framework/App/FrontController.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
*/
66
namespace Magento\Framework\App;
77

8+
use Magento\Framework\App\Action\AbstractAction;
9+
use Magento\Framework\App\Request\Http as HttpRequest;
810
use Magento\Framework\App\Request\InvalidRequestException;
9-
use Magento\Framework\Controller\ResultInterface;
1011
use Magento\Framework\App\Request\ValidatorInterface as RequestValidator;
12+
use Magento\Framework\Controller\ResultInterface;
13+
use Magento\Framework\Exception\LocalizedException;
1114
use Magento\Framework\Exception\NotFoundException;
1215
use Magento\Framework\Message\ManagerInterface as MessageManager;
13-
use Magento\Framework\App\Action\AbstractAction;
1416
use Psr\Log\LoggerInterface;
15-
use Magento\Framework\App\Request\Http as HttpRequest;
1617

1718
/**
1819
* Front controller responsible for dispatching application requests
@@ -51,19 +52,33 @@ class FrontController implements FrontControllerInterface
5152
*/
5253
private $validatedRequest = false;
5354

55+
/**
56+
* @var State
57+
*/
58+
private $appState;
59+
60+
/**
61+
* @var AreaList
62+
*/
63+
private $areaList;
64+
5465
/**
5566
* @param RouterListInterface $routerList
5667
* @param ResponseInterface $response
5768
* @param RequestValidator|null $requestValidator
5869
* @param MessageManager|null $messageManager
5970
* @param LoggerInterface|null $logger
71+
* @param State $appState
72+
* @param AreaList $areaList
6073
*/
6174
public function __construct(
6275
RouterListInterface $routerList,
6376
ResponseInterface $response,
6477
?RequestValidator $requestValidator = null,
6578
?MessageManager $messageManager = null,
66-
?LoggerInterface $logger = null
79+
?LoggerInterface $logger = null,
80+
?State $appState = null,
81+
?AreaList $areaList = null
6782
) {
6883
$this->_routerList = $routerList;
6984
$this->response = $response;
@@ -73,6 +88,10 @@ public function __construct(
7388
?? ObjectManager::getInstance()->get(MessageManager::class);
7489
$this->logger = $logger
7590
?? ObjectManager::getInstance()->get(LoggerInterface::class);
91+
$this->appState = $appState
92+
?? ObjectManager::getInstance()->get(State::class);
93+
$this->areaList = $areaList
94+
?? ObjectManager::getInstance()->get(AreaList::class);
7695
}
7796

7897
/**
@@ -81,6 +100,7 @@ public function __construct(
81100
* @param RequestInterface|HttpRequest $request
82101
* @return ResponseInterface|ResultInterface
83102
* @throws \LogicException
103+
* @throws LocalizedException
84104
*/
85105
public function dispatch(RequestInterface $request)
86106
{
@@ -120,9 +140,10 @@ public function dispatch(RequestInterface $request)
120140
*
121141
* @param HttpRequest $request
122142
* @param ActionInterface $actionInstance
123-
* @throws NotFoundException
124-
*
125143
* @return ResponseInterface|ResultInterface
144+
* @throws LocalizedException
145+
*
146+
* @throws NotFoundException
126147
*/
127148
private function processRequest(
128149
HttpRequest $request,
@@ -147,6 +168,9 @@ private function processRequest(
147168
["exception" => $exception]
148169
);
149170
$result = $exception->getReplaceResult();
171+
$area = $this->areaList->getArea($this->appState->getAreaCode());
172+
$area->load(Area::PART_DESIGN);
173+
$area->load(Area::PART_TRANSLATE);
150174
if ($messages = $exception->getMessages()) {
151175
foreach ($messages as $message) {
152176
$this->messages->addErrorMessage($message);

lib/internal/Magento/Framework/App/Test/Unit/FrontControllerTest.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66

77
namespace Magento\Framework\App\Test\Unit;
88

9+
use Magento\Framework\App\Area;
10+
use Magento\Framework\App\AreaInterface;
11+
use Magento\Framework\App\AreaList;
912
use Magento\Framework\App\Request\InvalidRequestException;
1013
use Magento\Framework\App\Request\ValidatorInterface;
14+
use Magento\Framework\App\State;
1115
use Magento\Framework\Exception\NotFoundException;
1216
use Magento\Framework\Message\ManagerInterface as MessageManager;
1317
use Psr\Log\LoggerInterface;
@@ -57,6 +61,21 @@ class FrontControllerTest extends \PHPUnit\Framework\TestCase
5761
*/
5862
private $logger;
5963

64+
/**
65+
* @var \PHPUnit\Framework\MockObject\MockObject|AreaList
66+
*/
67+
private $areaListMock;
68+
69+
/**
70+
* @var \PHPUnit\Framework\MockObject\MockObject|State
71+
*/
72+
private $appStateMock;
73+
74+
/**
75+
* @var \PHPUnit\Framework\MockObject\MockObject|AreaInterface
76+
*/
77+
private $areaMock;
78+
6079
protected function setUp()
6180
{
6281
$this->request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
@@ -70,12 +89,19 @@ protected function setUp()
7089
$this->requestValidator = $this->createMock(ValidatorInterface::class);
7190
$this->messages = $this->createMock(MessageManager::class);
7291
$this->logger = $this->createMock(LoggerInterface::class);
92+
$this->appStateMock = $this->getMockBuilder(State::class)
93+
->disableOriginalConstructor()
94+
->getMock();
95+
$this->areaListMock = $this->createMock(AreaList::class);
96+
$this->areaMock = $this->createMock(AreaInterface::class);
7397
$this->model = new \Magento\Framework\App\FrontController(
7498
$this->routerList,
7599
$this->response,
76100
$this->requestValidator,
77101
$this->messages,
78-
$this->logger
102+
$this->logger,
103+
$this->appStateMock,
104+
$this->areaListMock
79105
);
80106
}
81107

@@ -114,6 +140,10 @@ public function testAddingValidationFailureMessageToDebugLog()
114140
$exceptionMessage = 'exception_message';
115141
$exception = new InvalidRequestException($exceptionMessage);
116142

143+
$this->appStateMock->expects($this->any())->method('getAreaCode')->willReturn('frontend');
144+
$this->areaMock->expects($this->at(0))->method('load')->with(Area::PART_DESIGN)->willReturnSelf();
145+
$this->areaMock->expects($this->at(1))->method('load')->with(Area::PART_TRANSLATE)->willReturnSelf();
146+
$this->areaListMock->expects($this->any())->method('getArea')->will($this->returnValue($this->areaMock));
117147
$this->routerList->expects($this->any())
118148
->method('valid')
119149
->will($this->returnValue(true));

0 commit comments

Comments
 (0)