5
5
*/
6
6
namespace Magento \Store \Controller \Store ;
7
7
8
+ use Magento \Authorization \Model \UserContextInterface ;
8
9
use Magento \Framework \App \ActionInterface ;
9
10
use Magento \Framework \Encryption \UrlCoder ;
11
+ use Magento \Framework \Interception \InterceptorInterface ;
10
12
use Magento \Store \Api \StoreResolverInterface ;
11
13
use Magento \Store \Model \Store ;
12
14
use Magento \Store \Model \StoreManagerInterface ;
20
22
21
23
/**
22
24
* Test for store switch controller.
25
+ *
26
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27
+ * @magentoAppArea frontend
23
28
*/
24
29
class SwitchActionTest extends AbstractController
25
30
{
@@ -39,6 +44,14 @@ class SwitchActionTest extends AbstractController
39
44
* @var MockObject
40
45
*/
41
46
private $ postprocessorMock ;
47
+ /**
48
+ * @var UserContextInterface
49
+ */
50
+ private $ userContext ;
51
+ /**
52
+ * @var MockObject
53
+ */
54
+ private $ userContextMock ;
42
55
43
56
/**
44
57
* @inheritDoc
@@ -48,11 +61,15 @@ protected function setUp(): void
48
61
parent ::setUp ();
49
62
$ this ->preprocessor = $ this ->_objectManager ->get (RedirectDataPreprocessorInterface::class);
50
63
$ this ->preprocessorMock = $ this ->createMock (RedirectDataPreprocessorInterface::class);
51
- $ this ->_objectManager ->addSharedInstance ($ this ->preprocessorMock , get_class ($ this ->preprocessor ));
64
+ $ this ->_objectManager ->addSharedInstance ($ this ->preprocessorMock , $ this -> getClassName ($ this ->preprocessor ));
52
65
53
66
$ this ->postprocessor = $ this ->_objectManager ->get (RedirectDataPostprocessorInterface::class);
54
67
$ this ->postprocessorMock = $ this ->createMock (RedirectDataPostprocessorInterface::class);
55
- $ this ->_objectManager ->addSharedInstance ($ this ->postprocessorMock , get_class ($ this ->postprocessor ));
68
+ $ this ->_objectManager ->addSharedInstance ($ this ->postprocessorMock , $ this ->getClassName ($ this ->postprocessor ));
69
+
70
+ $ this ->userContext = $ this ->_objectManager ->get (UserContextInterface::class);
71
+ $ this ->userContextMock = $ this ->createMock (UserContextInterface::class);
72
+ $ this ->_objectManager ->addSharedInstance ($ this ->userContextMock , $ this ->getClassName ($ this ->userContext ));
56
73
}
57
74
58
75
/**
@@ -61,15 +78,19 @@ protected function setUp(): void
61
78
protected function tearDown (): void
62
79
{
63
80
if ($ this ->preprocessor ) {
64
- $ this ->_objectManager ->addSharedInstance ($ this ->preprocessor , get_class ($ this ->preprocessor ));
81
+ $ this ->_objectManager ->addSharedInstance ($ this ->preprocessor , $ this -> getClassName ($ this ->preprocessor ));
65
82
}
66
83
if ($ this ->postprocessor ) {
67
- $ this ->_objectManager ->addSharedInstance ($ this ->postprocessor , get_class ($ this ->postprocessor ));
84
+ $ this ->_objectManager ->addSharedInstance ($ this ->postprocessor , $ this ->getClassName ($ this ->postprocessor ));
85
+ }
86
+ if ($ this ->userContext ) {
87
+ $ this ->_objectManager ->addSharedInstance ($ this ->userContext , $ this ->getClassName ($ this ->userContext ));
68
88
}
69
89
parent ::tearDown ();
70
90
}
71
91
72
92
/**
93
+ * @magentoDataFixture Magento/Customer/_files/customer.php
73
94
* @magentoDataFixture Magento/Store/_files/second_store.php
74
95
* @magentoConfigFixture web/url/use_store 0
75
96
* @magentoConfigFixture fixture_second_store_store web/unsecure/base_url http://second_store.test/
@@ -82,10 +103,23 @@ public function testSwitch()
82
103
$ data = ['key1 ' => 'value1 ' , 'key2 ' => 1 ];
83
104
$ this ->preprocessorMock ->method ('process ' )
84
105
->willReturn ($ data );
106
+ $ this ->userContextMock ->method ('getUserType ' )
107
+ ->willReturn (UserContextInterface::USER_TYPE_CUSTOMER );
108
+ $ this ->userContextMock ->method ('getUserId ' )
109
+ ->willReturn (1 );
85
110
$ this ->postprocessorMock ->expects ($ this ->once ())
86
111
->method ('process ' )
87
- ->with ($ this ->isInstanceOf (ContextInterface::class), $ data );
88
-
112
+ ->with (
113
+ $ this ->callback (
114
+ function (ContextInterface $ context ) {
115
+ return $ context ->getFromStore ()->getCode () === 'fixture_second_store '
116
+ && $ context ->getTargetStore ()->getCode () === 'default '
117
+ && $ context ->getRedirectUrl () === 'http://localhost/index.php/ '
118
+ && $ context ->getCustomerId () === 1 ;
119
+ }
120
+ ),
121
+ $ data
122
+ );
89
123
$ redirectDataGenerator = $ this ->_objectManager ->get (RedirectDataGenerator::class);
90
124
$ contextFactory = $ this ->_objectManager ->get (ContextInterfaceFactory::class);
91
125
$ storeManager = $ this ->_objectManager ->get (StoreManagerInterface::class);
@@ -115,6 +149,21 @@ public function testSwitch()
115
149
$ this ->assertRedirect ($ this ->equalTo ('http://localhost/index.php/ ' ));
116
150
}
117
151
152
+ /**
153
+ * Return class name of the given object
154
+ *
155
+ * @param mixed $instance
156
+ */
157
+ private function getClassName ($ instance ): string
158
+ {
159
+ if ($ instance instanceof InterceptorInterface) {
160
+ $ actionClass = get_parent_class ($ instance );
161
+ } else {
162
+ $ actionClass = get_class ($ instance );
163
+ }
164
+ return $ actionClass ;
165
+ }
166
+
118
167
/**
119
168
* Ensure that proper default store code is calculated.
120
169
*
0 commit comments