|
| 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