1010
1111use Magento \Customer \Api \CustomerRepositoryInterface ;
1212use Magento \Customer \Api \Data \CustomerInterface ;
13+ use Magento \Customer \Model \Account \Redirect ;
14+ use Magento \Customer \Model \Session ;
1315use Magento \Framework \Api \FilterBuilder ;
1416use Magento \Framework \Api \SearchCriteriaBuilder ;
17+ use Magento \Framework \App \Config \ScopeConfigInterface ;
18+ use Magento \Framework \App \Config \Value ;
19+ use Magento \Framework \App \Http ;
1520use Magento \Framework \Data \Form \FormKey ;
1621use Magento \Framework \Message \MessageInterface ;
1722use Magento \Store \Model \ScopeInterface ;
1823use Magento \TestFramework \Helper \Bootstrap ;
24+ use Magento \TestFramework \Request ;
25+ use Magento \TestFramework \Response ;
26+ use Zend \Stdlib \Parameters ;
1927
2028/**
2129 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -30,9 +38,9 @@ class AccountTest extends \Magento\TestFramework\TestCase\AbstractController
3038 */
3139 protected function login ($ customerId )
3240 {
33- /** @var \Magento\Customer\Model\ Session $session */
41+ /** @var Session $session */
3442 $ session = Bootstrap::getObjectManager ()
35- ->get (\ Magento \ Customer \ Model \ Session::class);
43+ ->get (Session::class);
3644 $ session ->loginById ($ customerId );
3745 }
3846
@@ -130,8 +138,8 @@ public function testCreatepasswordActionWithDirectLink()
130138 $ this ->assertFalse ((bool )preg_match ('/ ' . $ token . '/m ' , $ text ));
131139 $ this ->assertRedirect ($ this ->stringContains ('customer/account/createpassword ' ));
132140
133- /** @var \Magento\Customer\Model\ Session $customer */
134- $ session = Bootstrap::getObjectManager ()->get (\ Magento \ Customer \ Model \ Session::class);
141+ /** @var Session $customer */
142+ $ session = Bootstrap::getObjectManager ()->get (Session::class);
135143 $ this ->assertEquals ($ token , $ session ->getRpToken ());
136144 $ this ->assertEquals ($ customer ->getId (), $ session ->getRpCustomerId ());
137145 $ this ->assertNotContains ($ token , $ response ->getHeader ('Location ' )->getFieldValue ());
@@ -151,8 +159,8 @@ public function testCreatepasswordActionWithSession()
151159 $ customer ->changeResetPasswordLinkToken ($ token );
152160 $ customer ->save ();
153161
154- /** @var \Magento\Customer\Model\ Session $customer */
155- $ session = Bootstrap::getObjectManager ()->get (\ Magento \ Customer \ Model \ Session::class);
162+ /** @var Session $customer */
163+ $ session = Bootstrap::getObjectManager ()->get (Session::class);
156164 $ session ->setRpToken ($ token );
157165 $ session ->setRpCustomerId ($ customer ->getId ());
158166
@@ -652,6 +660,50 @@ public function testWrongConfirmationEditPostAction()
652660 );
653661 }
654662
663+ /**
664+ * Test redirect customer to account dashboard after logging in.
665+ *
666+ * @param bool|null $redirectDashboard
667+ * @param string $redirectUrl
668+ * @magentoDbIsolation enabled
669+ * @magentoAppIsolation enabled
670+ * @magentoDataFixture Magento/Customer/_files/customer.php
671+ * @dataProvider loginPostRedirectDataProvider
672+ */
673+ public function testLoginPostRedirect ($ redirectDashboard , string $ redirectUrl )
674+ {
675+ if (isset ($ redirectDashboard )) {
676+ $ this ->_objectManager ->get (ScopeConfigInterface::class)->setValue ('customer/startup/redirect_dashboard ' , $ redirectDashboard );
677+ }
678+
679+ $ this ->_objectManager ->get (Redirect::class)->setRedirectCookie ('test ' );
680+
681+ $ configValue = $ this ->_objectManager ->create (Value::class);
682+ $ configValue ->load ('web/unsecure/base_url ' , 'path ' );
683+ $ baseUrl = $ configValue ->getValue () ?: 'http://localhost/ ' ;
684+
685+ $ request = $ this ->prepareRequest ();
686+ $ app = $ this ->_objectManager ->create (Http::class, ['_request ' => $ request ]);
687+ $ response = $ app ->launch ();
688+
689+ $ this ->assertResponseRedirect ($ response , $ baseUrl . $ redirectUrl );
690+ $ this ->assertTrue ($ this ->_objectManager ->get (Session::class)->isLoggedIn ());
691+ }
692+
693+ /**
694+ * Data provider for testLoginPostRedirect.
695+ *
696+ * @return array
697+ */
698+ public function loginPostRedirectDataProvider ()
699+ {
700+ return [
701+ [null , 'index.php/ ' ],
702+ [0 , 'index.php/ ' ],
703+ [1 , 'index.php/customer/account/ ' ],
704+ ];
705+ }
706+
655707 /**
656708 * @return void
657709 */
@@ -717,4 +769,40 @@ private function getCustomerByEmail($email)
717769
718770 return $ customer ;
719771 }
772+
773+ /**
774+ * Prepare request for customer login.
775+ *
776+ * @return Request
777+ */
778+ private function prepareRequest ()
779+ {
780+ $ post = new Parameters ([
781+ 'form_key ' => $ this ->_objectManager ->get (FormKey::class)->getFormKey (),
782+ 'login ' => [
783+ 'username ' =>
'[email protected] ' ,
784+ 'password ' => 'password '
785+ ]
786+ ]);
787+ $ request = $ this ->getRequest ();
788+ $ formKey = $ this ->_objectManager ->get (FormKey::class);
789+ $ request ->setParam ('form_key ' , $ formKey ->getFormKey ());
790+ $ request ->setMethod (Request::METHOD_POST );
791+ $ request ->setRequestUri ('customer/account/loginPost/ ' );
792+ $ request ->setPost ($ post );
793+ return $ request ;
794+ }
795+
796+ /**
797+ * Assert response is redirect.
798+ *
799+ * @param Response $response
800+ * @param string $redirectUrl
801+ * @return void
802+ */
803+ private function assertResponseRedirect (Response $ response , string $ redirectUrl )
804+ {
805+ $ this ->assertTrue ($ response ->isRedirect ());
806+ $ this ->assertSame ($ redirectUrl , $ response ->getHeader ('Location ' )->getUri ());
807+ }
720808}
0 commit comments