10
10
11
11
use Magento \Customer \Api \CustomerRepositoryInterface ;
12
12
use Magento \Customer \Api \Data \CustomerInterface ;
13
+ use Magento \Customer \Model \Account \Redirect ;
14
+ use Magento \Customer \Model \Session ;
13
15
use Magento \Framework \Api \FilterBuilder ;
14
16
use Magento \Framework \Api \SearchCriteriaBuilder ;
17
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
18
+ use Magento \Framework \App \Config \Value ;
19
+ use Magento \Framework \App \Http ;
15
20
use Magento \Framework \Data \Form \FormKey ;
16
21
use Magento \Framework \Message \MessageInterface ;
17
22
use Magento \Store \Model \ScopeInterface ;
18
23
use Magento \TestFramework \Helper \Bootstrap ;
24
+ use Magento \TestFramework \Request ;
25
+ use Magento \TestFramework \Response ;
26
+ use Zend \Stdlib \Parameters ;
19
27
20
28
/**
21
29
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -30,9 +38,9 @@ class AccountTest extends \Magento\TestFramework\TestCase\AbstractController
30
38
*/
31
39
protected function login ($ customerId )
32
40
{
33
- /** @var \Magento\Customer\Model\ Session $session */
41
+ /** @var Session $session */
34
42
$ session = Bootstrap::getObjectManager ()
35
- ->get (\ Magento \ Customer \ Model \ Session::class);
43
+ ->get (Session::class);
36
44
$ session ->loginById ($ customerId );
37
45
}
38
46
@@ -130,8 +138,8 @@ public function testCreatepasswordActionWithDirectLink()
130
138
$ this ->assertFalse ((bool )preg_match ('/ ' . $ token . '/m ' , $ text ));
131
139
$ this ->assertRedirect ($ this ->stringContains ('customer/account/createpassword ' ));
132
140
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);
135
143
$ this ->assertEquals ($ token , $ session ->getRpToken ());
136
144
$ this ->assertEquals ($ customer ->getId (), $ session ->getRpCustomerId ());
137
145
$ this ->assertNotContains ($ token , $ response ->getHeader ('Location ' )->getFieldValue ());
@@ -151,8 +159,8 @@ public function testCreatepasswordActionWithSession()
151
159
$ customer ->changeResetPasswordLinkToken ($ token );
152
160
$ customer ->save ();
153
161
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);
156
164
$ session ->setRpToken ($ token );
157
165
$ session ->setRpCustomerId ($ customer ->getId ());
158
166
@@ -652,6 +660,50 @@ public function testWrongConfirmationEditPostAction()
652
660
);
653
661
}
654
662
663
+ /**
664
+ * Test redirect customer to account dashboard after logging in.
665
+ *
666
+ * @param bool|null $redirectDashboardValue
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 ($ redirectDashboardValue , string $ redirectUrl )
674
+ {
675
+ if (isset ($ redirectDashboardValue )) {
676
+ $ this ->_objectManager ->get (ScopeConfigInterface::class)->setValue ('customer/startup/redirect_dashboard ' , $ redirectDashboardValue );
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
+
655
707
/**
656
708
* @return void
657
709
*/
@@ -717,4 +769,40 @@ private function getCustomerByEmail($email)
717
769
718
770
return $ customer ;
719
771
}
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
+ }
720
808
}
0 commit comments