|
8 | 8 |
|
9 | 9 | use Magento\Customer\Api\CustomerRepositoryInterface;
|
10 | 10 | use Magento\Customer\Api\Data\CustomerInterface;
|
| 11 | +use Magento\Customer\Model\Account\Redirect; |
| 12 | +use Magento\Customer\Model\Session; |
11 | 13 | use Magento\Framework\Api\FilterBuilder;
|
12 | 14 | use Magento\Framework\Api\SearchCriteriaBuilder;
|
| 15 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 16 | +use Magento\Framework\App\Config\Value; |
| 17 | +use Magento\Framework\App\Http; |
13 | 18 | use Magento\Framework\Data\Form\FormKey;
|
14 | 19 | use Magento\Framework\Message\MessageInterface;
|
15 | 20 | use Magento\Store\Model\ScopeInterface;
|
16 | 21 | use Magento\TestFramework\Helper\Bootstrap;
|
| 22 | +use Magento\TestFramework\Request; |
| 23 | +use Magento\TestFramework\Response; |
| 24 | +use Zend\Stdlib\Parameters; |
17 | 25 |
|
18 | 26 | /**
|
19 | 27 | * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
@@ -660,6 +668,47 @@ public function testWrongConfirmationEditPostAction()
|
660 | 668 | );
|
661 | 669 | }
|
662 | 670 |
|
| 671 | + /** |
| 672 | + * Test redirect customer to account dashboard after logging in. |
| 673 | + * |
| 674 | + * @param bool|null $redirectDashboard |
| 675 | + * @param string $redirectUrl |
| 676 | + * @magentoDbIsolation enabled |
| 677 | + * @magentoAppIsolation enabled |
| 678 | + * @magentoDataFixture Magento/Customer/_files/customer.php |
| 679 | + * @dataProvider loginPostRedirectDataProvider |
| 680 | + */ |
| 681 | + public function testLoginPostRedirect($redirectDashboard, string $redirectUrl) |
| 682 | + { |
| 683 | + if (isset($redirectDashboard)) { |
| 684 | + $this->_objectManager->get(ScopeConfigInterface::class)->setValue('customer/startup/redirect_dashboard', |
| 685 | + $redirectDashboard); |
| 686 | + } |
| 687 | + $this->_objectManager->get(Redirect::class)->setRedirectCookie('test'); |
| 688 | + $configValue = $this->_objectManager->create(Value::class); |
| 689 | + $configValue->load('web/unsecure/base_url', 'path'); |
| 690 | + $baseUrl = $configValue->getValue() ?: 'http://localhost/'; |
| 691 | + $request = $this->prepareRequest(); |
| 692 | + $app = $this->_objectManager->create(Http::class, ['_request' => $request]); |
| 693 | + $response = $app->launch(); |
| 694 | + $this->assertResponseRedirect($response, $baseUrl . $redirectUrl); |
| 695 | + $this->assertTrue($this->_objectManager->get(Session::class)->isLoggedIn()); |
| 696 | + } |
| 697 | + |
| 698 | + /** |
| 699 | + * Data provider for testLoginPostRedirect. |
| 700 | + * |
| 701 | + * @return array |
| 702 | + */ |
| 703 | + public function loginPostRedirectDataProvider() |
| 704 | + { |
| 705 | + return [ |
| 706 | + [null, 'index.php/'], |
| 707 | + [0, 'index.php/'], |
| 708 | + [1, 'index.php/customer/account/'], |
| 709 | + ]; |
| 710 | + } |
| 711 | + |
663 | 712 | /**
|
664 | 713 | * @param string $email
|
665 | 714 | * @return void
|
@@ -727,4 +776,40 @@ private function getCustomerByEmail($email)
|
727 | 776 |
|
728 | 777 | return $customer;
|
729 | 778 | }
|
| 779 | + |
| 780 | + /** |
| 781 | + * Prepare request for customer login. |
| 782 | + * |
| 783 | + * @return Request |
| 784 | + */ |
| 785 | + private function prepareRequest() |
| 786 | + { |
| 787 | + $post = new Parameters([ |
| 788 | + 'form_key' => $this->_objectManager->get(FormKey::class)->getFormKey(), |
| 789 | + 'login' => [ |
| 790 | + 'username' => '[email protected]', |
| 791 | + 'password' => 'password' |
| 792 | + ] |
| 793 | + ]); |
| 794 | + $request = $this->getRequest(); |
| 795 | + $formKey = $this->_objectManager->get(FormKey::class); |
| 796 | + $request->setParam('form_key', $formKey->getFormKey()); |
| 797 | + $request->setMethod(Request::METHOD_POST); |
| 798 | + $request->setRequestUri('customer/account/loginPost/'); |
| 799 | + $request->setPost($post); |
| 800 | + return $request; |
| 801 | + } |
| 802 | + |
| 803 | + /** |
| 804 | + * Assert response is redirect. |
| 805 | + * |
| 806 | + * @param Response $response |
| 807 | + * @param string $redirectUrl |
| 808 | + * @return void |
| 809 | + */ |
| 810 | + private function assertResponseRedirect(Response $response, string $redirectUrl) |
| 811 | + { |
| 812 | + $this->assertTrue($response->isRedirect()); |
| 813 | + $this->assertSame($redirectUrl, $response->getHeader('Location')->getUri()); |
| 814 | + } |
730 | 815 | }
|
0 commit comments