Skip to content

Commit d0f3da8

Browse files
committed
MC-31521: Customer login on storefront
1 parent a25c107 commit d0f3da8

File tree

5 files changed

+343
-115
lines changed

5 files changed

+343
-115
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Block\Form;
9+
10+
use Magento\Framework\ObjectManagerInterface;
11+
use Magento\Framework\View\LayoutInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\TestFramework\Helper\Xpath;
14+
use PHPUnit\Framework\TestCase;
15+
16+
/**
17+
* Class checks login form view
18+
*
19+
* @magentoAppArea frontend
20+
*/
21+
class LoginTest extends TestCase
22+
{
23+
private const EMAIL_LABEL_XPATH = "//label[@for='email']/span[contains(text(), 'Email')]";
24+
private const PASSWORD_LABEL_XPATH = "//label[@for='pass' ]/span[contains(text(), 'Password')]";
25+
private const EMAIL_INPUT_XPATH = "//input[@name ='login[username]' and contains(@data-validate,'required:true')"
26+
. "and contains(@data-validate, \"'validate-email':true\")]";
27+
private const PASSWORD_INPUT_XPATH = "//input[@name='login[password]'"
28+
. "and contains(@data-validate,'required:true')]";
29+
private const SIGN_IN_BUTTON_XPATH = "//button[@type='submit']/span[contains(text(), 'Sign In')]";
30+
private const FORGOT_PASSWORD_LINK_PATH = "//a[contains(@href, 'customer/account/forgotpassword')]"
31+
. "/span[contains(text(), 'Forgot Your Password?')] ";
32+
33+
/** @var ObjectManagerInterface */
34+
private $objectManager;
35+
36+
/** @var LayoutInterface */
37+
private $layout;
38+
39+
/** @var Login */
40+
private $block;
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
protected function setUp()
46+
{
47+
$this->objectManager = Bootstrap::getObjectManager();
48+
$this->layout = $this->objectManager->get(LayoutInterface::class);
49+
$this->block = $this->layout->createBlock(Login::class);
50+
$this->block->setTemplate('Magento_Customer::form/login.phtml');
51+
52+
parent::setUp();
53+
}
54+
55+
/**
56+
* @return void
57+
*/
58+
public function testLoginForm(): void
59+
{
60+
$result = $this->block->toHtml();
61+
$this->assertEquals(
62+
1,
63+
Xpath::getElementsCountForXpath(self::EMAIL_LABEL_XPATH, $result),
64+
'Email label does not exist on the page'
65+
);
66+
$this->assertEquals(
67+
1,
68+
Xpath::getElementsCountForXpath(self::PASSWORD_LABEL_XPATH, $result),
69+
'Password label does not exist on the page'
70+
);
71+
$this->assertEquals(
72+
1,
73+
Xpath::getElementsCountForXpath(self::EMAIL_INPUT_XPATH, $result),
74+
'Email input does not exist on the page'
75+
);
76+
$this->assertEquals(
77+
1,
78+
Xpath::getElementsCountForXpath(self::PASSWORD_INPUT_XPATH, $result),
79+
'Password input does not exist on the page'
80+
);
81+
$this->assertEquals(
82+
1,
83+
Xpath::getElementsCountForXpath(self::SIGN_IN_BUTTON_XPATH, $result),
84+
'Sign in button does not exist on the page'
85+
);
86+
$this->assertEquals(
87+
1,
88+
Xpath::getElementsCountForXpath(self::FORGOT_PASSWORD_LINK_PATH, $result),
89+
'Forgot password link does not exist on the page'
90+
);
91+
}
92+
}
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Controller\Account;
9+
10+
use Magento\Customer\Model\Session;
11+
use Magento\Customer\Model\Url;
12+
use Magento\Framework\App\Request\Http as HttpRequest;
13+
use Magento\Framework\Message\MessageInterface;
14+
use Magento\Framework\Url\EncoderInterface;
15+
use Magento\TestFramework\TestCase\AbstractController;
16+
17+
/**
18+
* Class checks customer login action
19+
*
20+
* @see \Magento\Customer\Controller\Account\LoginPost
21+
*/
22+
class LoginPostTest extends AbstractController
23+
{
24+
/** @var Session */
25+
private $session;
26+
27+
/** @var EncoderInterface */
28+
private $urlEncoder;
29+
30+
/**
31+
* @inheritdoc
32+
*/
33+
protected function setUp()
34+
{
35+
parent::setUp();
36+
37+
$this->session = $this->_objectManager->get(Session::class);
38+
$this->urlEncoder = $this->_objectManager->get(EncoderInterface::class);
39+
}
40+
41+
/**
42+
* @magentoConfigFixture current_store customer/captcha/enable 0
43+
*
44+
* @magentoDataFixture Magento/Customer/_files/customer.php
45+
*
46+
* @dataProvider missingParametersDataProvider
47+
*
48+
* @param string|null $email
49+
* @param string|null $password
50+
* @param string $expectedErrorMessage
51+
* @return void
52+
*/
53+
public function testLoginIncorrectParameters(?string $email, ?string $password, string $expectedErrorMessage): void
54+
{
55+
$this->prepareRequest($email, $password);
56+
$this->dispatch('customer/account/loginPost');
57+
$this->assertSessionMessages(
58+
$this->equalTo([(string)__($expectedErrorMessage)]),
59+
MessageInterface::TYPE_ERROR
60+
);
61+
}
62+
63+
/**
64+
* @return array
65+
*/
66+
public function missingParametersDataProvider(): array
67+
{
68+
return [
69+
'missing_email' => [
70+
'email' => null,
71+
'password' => 'password',
72+
'expected_error_message' => 'A login and a password are required.',
73+
],
74+
'missing_password' => [
75+
'email' => 'customer@example.com',
76+
'password' => null,
77+
'expected_error_message' => 'A login and a password are required.',
78+
],
79+
'missing_both_parameters' => [
80+
'email' => null,
81+
'password' => null,
82+
'expected_error_message' => 'A login and a password are required.',
83+
],
84+
'wrong_email' => [
85+
'email' => 'wrongemail@example.com',
86+
'password' => 'password',
87+
'expected_error_message' => 'The account sign-in was incorrect or your account is disabled temporarily.'
88+
. ' Please wait and try again later.',
89+
],
90+
'wrong_password' => [
91+
'email' => 'customer@example.com',
92+
'password' => 'wrongpassword',
93+
'expected_error_message' => 'The account sign-in was incorrect or your account is disabled temporarily.'
94+
. ' Please wait and try again later.',
95+
],
96+
];
97+
}
98+
99+
/**
100+
* @magentoDataFixture Magento/Customer/_files/customer_confirmation_config_enable.php
101+
* @magentoDataFixture Magento/Customer/_files/unconfirmed_customer.php
102+
*
103+
* @magentoConfigFixture current_store customer/captcha/enable 0
104+
*
105+
* @return void
106+
*/
107+
public function testLoginWithUnconfirmedPassword(): void
108+
{
109+
$this->markTestSkipped('Blocked by MC-31370.');
110+
$email = 'unconfirmedcustomer@example.com';
111+
$this->prepareRequest($email, 'Qwert12345');
112+
$this->dispatch('customer/account/loginPost');
113+
$this->assertEquals($email, $this->session->getUsername());
114+
$this->assertSessionMessages(
115+
$this->equalTo([(string)__('This account is not confirmed. Click here to resend confirmation email.')]),
116+
MessageInterface::TYPE_ERROR
117+
);
118+
}
119+
120+
/**
121+
* @magentoConfigFixture current_store customer/startup/redirect_dashboard 0
122+
* @magentoConfigFixture current_store customer/captcha/enable 0
123+
*
124+
* @magentoDataFixture Magento/Customer/_files/customer.php
125+
*
126+
* @return void
127+
*/
128+
public function testLoginWithRedirectToDashboardDisabled(): void
129+
{
130+
$this->prepareRequest('customer@example.com', 'password');
131+
$this->getRequest()->setParam(Url::REFERER_QUERY_PARAM_NAME, $this->urlEncoder->encode('test_redirect'));
132+
$this->dispatch('customer/account/loginPost');
133+
$this->assertTrue($this->session->isLoggedIn());
134+
$this->assertRedirect($this->stringContains('test_redirect'));
135+
}
136+
137+
/**
138+
* @magentoConfigFixture current_store customer/startup/redirect_dashboard 1
139+
* @magentoConfigFixture current_store customer/captcha/enable 0
140+
*
141+
* @magentoDataFixture Magento/Customer/_files/customer.php
142+
*
143+
* @return void
144+
*/
145+
public function testLoginWithRedirectToDashboard(): void
146+
{
147+
$this->prepareRequest('customer@example.com', 'password');
148+
$this->getRequest()->setParam(Url::REFERER_QUERY_PARAM_NAME, $this->urlEncoder->encode('test_redirect'));
149+
$this->dispatch('customer/account/loginPost');
150+
$this->assertTrue($this->session->isLoggedIn());
151+
$this->assertRedirect($this->stringContains('customer/account/'));
152+
}
153+
154+
/**
155+
* Prepare request
156+
*
157+
* @param string|null $email
158+
* @param string|null $password
159+
* @return void
160+
*/
161+
private function prepareRequest(?string $email, ?string $password): void
162+
{
163+
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
164+
$this->getRequest()->setPostValue([
165+
'login' => [
166+
'username' => $email,
167+
'password' => $password,
168+
],
169+
]);
170+
}
171+
}

0 commit comments

Comments
 (0)