5
5
*/
6
6
namespace Magento \Catalog \Test \Unit \Block \Widget ;
7
7
8
+ use Exception ;
9
+ use Magento \Catalog \Block \Widget \Link ;
10
+ use Magento \Catalog \Model \ResourceModel \AbstractResource ;
8
11
use Magento \CatalogUrlRewrite \Model \ProductUrlRewriteGenerator ;
12
+ use Magento \Framework \App \Config \ReinitableConfigInterface ;
9
13
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
14
+ use Magento \Framework \Url ;
15
+ use Magento \Framework \Url \ModifierInterface ;
16
+ use Magento \Framework \View \Element \Template \Context ;
17
+ use Magento \Store \Model \Store ;
18
+ use Magento \Store \Model \StoreManagerInterface ;
19
+ use Magento \UrlRewrite \Model \UrlFinderInterface ;
10
20
use Magento \UrlRewrite \Service \V1 \Data \UrlRewrite ;
21
+ use PHPUnit \Framework \TestCase ;
22
+ use PHPUnit_Framework_MockObject_MockObject ;
23
+ use ReflectionClass ;
24
+ use RuntimeException ;
11
25
12
- class LinkTest extends \PHPUnit \Framework \TestCase
26
+ /**
27
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
28
+ * @SuppressWarnings(PHPMD.UnusedLocalVariable)
29
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
30
+ */
31
+ class LinkTest extends TestCase
13
32
{
14
33
/**
15
- * @var \ PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\ StoreManagerInterface
34
+ * @var PHPUnit_Framework_MockObject_MockObject|StoreManagerInterface
16
35
*/
17
36
protected $ storeManager ;
18
37
19
38
/**
20
- * @var \ PHPUnit_Framework_MockObject_MockObject|\Magento\UrlRewrite\Model\ UrlFinderInterface
39
+ * @var PHPUnit_Framework_MockObject_MockObject|UrlFinderInterface
21
40
*/
22
41
protected $ urlFinder ;
23
42
24
43
/**
25
- * @var \Magento\Catalog\Block\Widget\ Link
44
+ * @var Link
26
45
*/
27
46
protected $ block ;
28
47
29
48
/**
30
- * @var \Magento\Catalog\Model\ResourceModel\ AbstractResource|\ PHPUnit_Framework_MockObject_MockObject
49
+ * @var AbstractResource|PHPUnit_Framework_MockObject_MockObject
31
50
*/
32
51
protected $ entityResource ;
33
52
53
+ /**
54
+ * @inheritDoc
55
+ */
34
56
protected function setUp ()
35
57
{
36
- $ this ->storeManager = $ this ->createMock (\ Magento \ Store \ Model \ StoreManagerInterface::class);
37
- $ this ->urlFinder = $ this ->createMock (\ Magento \ UrlRewrite \ Model \ UrlFinderInterface::class);
58
+ $ this ->storeManager = $ this ->createMock (StoreManagerInterface::class);
59
+ $ this ->urlFinder = $ this ->createMock (UrlFinderInterface::class);
38
60
39
- $ context = $ this ->createMock (\ Magento \ Framework \ View \ Element \ Template \ Context::class);
61
+ $ context = $ this ->createMock (Context::class);
40
62
$ context ->expects ($ this ->any ())
41
63
->method ('getStoreManager ' )
42
64
->will ($ this ->returnValue ($ this ->storeManager ));
43
65
44
66
$ this ->entityResource =
45
- $ this ->createMock (\Magento \Catalog \Model \ResourceModel \AbstractResource::class);
46
-
47
- $ this ->block = (new ObjectManager ($ this ))->getObject (\Magento \Catalog \Block \Widget \Link::class, [
48
- 'context ' => $ context ,
49
- 'urlFinder ' => $ this ->urlFinder ,
50
- 'entityResource ' => $ this ->entityResource
51
- ]);
67
+ $ this ->createMock (AbstractResource::class);
68
+
69
+ $ this ->block = (new ObjectManager ($ this ))->getObject (
70
+ Link::class,
71
+ [
72
+ 'context ' => $ context ,
73
+ 'urlFinder ' => $ this ->urlFinder ,
74
+ 'entityResource ' => $ this ->entityResource
75
+ ]
76
+ );
52
77
}
53
78
54
79
/**
55
- * @expectedException \RuntimeException
80
+ * Tests getHref with wrong id_path
81
+ *
82
+ * @expectedException RuntimeException
56
83
* @expectedExceptionMessage Parameter id_path is not set.
57
84
*/
58
85
public function testGetHrefWithoutSetIdPath ()
@@ -61,7 +88,9 @@ public function testGetHrefWithoutSetIdPath()
61
88
}
62
89
63
90
/**
64
- * @expectedException \RuntimeException
91
+ * Tests getHref with wrong id_path
92
+ *
93
+ * @expectedException RuntimeException
65
94
* @expectedExceptionMessage Wrong id_path structure.
66
95
*/
67
96
public function testGetHrefIfSetWrongIdPath ()
@@ -70,27 +99,30 @@ public function testGetHrefIfSetWrongIdPath()
70
99
$ this ->block ->getHref ();
71
100
}
72
101
102
+ /**
103
+ * Tests getHref with wrong store ID
104
+ *
105
+ * @expectedException Exception
106
+ */
73
107
public function testGetHrefWithSetStoreId ()
74
108
{
75
109
$ this ->block ->setData ('id_path ' , 'type/id ' );
76
110
$ this ->block ->setData ('store_id ' , 'store_id ' );
77
-
78
111
$ this ->storeManager ->expects ($ this ->once ())
79
- ->method ('getStore ' )->with ('store_id ' )
80
- // interrupt test execution
81
- ->will ($ this ->throwException (new \Exception ()));
82
-
83
- try {
84
- $ this ->block ->getHref ();
85
- } catch (\Exception $ e ) {
86
- }
112
+ ->method ('getStore ' )
113
+ ->with ('store_id ' )
114
+ ->will ($ this ->throwException (new Exception ()));
115
+ $ this ->block ->getHref ();
87
116
}
88
117
118
+ /**
119
+ * Tests getHref with not found URL
120
+ */
89
121
public function testGetHrefIfRewriteIsNotFound ()
90
122
{
91
123
$ this ->block ->setData ('id_path ' , 'entity_type/entity_id ' );
92
124
93
- $ store = $ this ->createPartialMock (\ Magento \ Store \ Model \ Store::class, ['getId ' , '__wakeUp ' ]);
125
+ $ store = $ this ->createPartialMock (Store::class, ['getId ' , '__wakeUp ' ]);
94
126
$ store ->expects ($ this ->any ())
95
127
->method ('getId ' );
96
128
@@ -105,59 +137,104 @@ public function testGetHrefIfRewriteIsNotFound()
105
137
}
106
138
107
139
/**
108
- * @param string $url
109
- * @param string $separator
140
+ * Tests getHref whether it should include the store code or not
141
+ *
110
142
* @dataProvider dataProviderForTestGetHrefWithoutUrlStoreSuffix
143
+ * @param string $path
144
+ * @param bool $includeStoreCode
145
+ * @param string $expected
146
+ * @throws \ReflectionException
111
147
*/
112
- public function testGetHrefWithoutUrlStoreSuffix ( $ url , $ separator )
113
- {
114
- $ storeId = 15 ;
115
- $ storeCode = ' store-code ' ;
116
- $ requestPath = ' request-path ' ;
148
+ public function testStoreCodeShouldBeIncludedInURLOnlyIfItIsConfiguredSo (
149
+ string $ path ,
150
+ bool $ includeStoreCode ,
151
+ string $ expected
152
+ ) {
117
153
$ this ->block ->setData ('id_path ' , 'entity_type/entity_id ' );
118
-
119
- $ rewrite = $ this ->createMock (\Magento \UrlRewrite \Service \V1 \Data \UrlRewrite::class);
120
- $ rewrite ->expects ($ this ->once ())
121
- ->method ('getRequestPath ' )
122
- ->will ($ this ->returnValue ($ requestPath ));
123
-
124
- $ store = $ this ->createPartialMock (
125
- \Magento \Store \Model \Store::class,
126
- ['getId ' , 'getUrl ' , 'getCode ' , '__wakeUp ' ]
154
+ $ objectManager = new ObjectManager ($ this );
155
+
156
+ $ rewrite = $ this ->createPartialMock (UrlRewrite::class, ['getRequestPath ' ]);
157
+ $ url = $ this ->createPartialMock (Url::class, ['setScope ' , 'getUrl ' ]);
158
+ $ urlModifier = $ this ->getMockForAbstractClass (ModifierInterface::class);
159
+ $ config = $ this ->getMockForAbstractClass (ReinitableConfigInterface::class);
160
+ $ store = $ objectManager ->getObject (
161
+ Store::class,
162
+ [
163
+ 'storeManager ' => $ this ->storeManager ,
164
+ 'url ' => $ url ,
165
+ 'config ' => $ config
166
+ ]
127
167
);
128
- $ store ->expects ($ this ->once ())
129
- ->method ('getId ' )
130
- ->will ($ this ->returnValue ($ storeId ));
131
- $ store ->expects ($ this ->once ())
168
+ $ property = (new ReflectionClass (get_class ($ store )))->getProperty ('urlModifier ' );
169
+ $ property ->setAccessible (true );
170
+ $ property ->setValue ($ store , $ urlModifier );
171
+
172
+ $ urlModifier ->expects ($ this ->any ())
173
+ ->method ('execute ' )
174
+ ->willReturnArgument (0 );
175
+ $ config ->expects ($ this ->any ())
176
+ ->method ('getValue ' )
177
+ ->willReturnMap (
178
+ [
179
+ [Store::XML_PATH_USE_REWRITES , ReinitableConfigInterface::SCOPE_TYPE_DEFAULT , null , true ],
180
+ [
181
+ Store::XML_PATH_STORE_IN_URL ,
182
+ ReinitableConfigInterface::SCOPE_TYPE_DEFAULT ,
183
+ null , $ includeStoreCode
184
+ ]
185
+ ]
186
+ );
187
+
188
+ $ url ->expects ($ this ->any ())
189
+ ->method ('setScope ' )
190
+ ->willReturnSelf ();
191
+
192
+ $ url ->expects ($ this ->any ())
132
193
->method ('getUrl ' )
133
- ->with ( '' , [ ' _direct ' => $ requestPath ])
134
- -> will ( $ this -> returnValue ( $ url ));
135
- $ store ->expects ( $ this -> once ())
136
- -> method ( ' getCode ' )
137
- -> will ( $ this -> returnValue ( $ storeCode ) );
194
+ ->willReturnCallback (
195
+ function ( $ route , $ params ) use ( $ store ) {
196
+ return rtrim ( $ store ->getBaseUrl (), ' / ' ) . ' / ' . ltrim ( $ params [ ' _direct ' ], ' / ' );
197
+ }
198
+ );
138
199
139
- $ this ->storeManager ->expects ($ this ->once ())
200
+ $ store ->addData (['store_id ' => 1 , 'code ' => 'french ' ]);
201
+
202
+ $ this ->storeManager
203
+ ->expects ($ this ->any ())
140
204
->method ('getStore ' )
141
- ->will ( $ this -> returnValue ( $ store) );
205
+ ->willReturn ( $ store );
142
206
143
- $ this ->urlFinder ->expects ($ this ->once ())->method ('findOneByData ' )
144
- ->with ([
207
+ $ this ->urlFinder ->expects ($ this ->once ())
208
+ ->method ('findOneByData ' )
209
+ ->with (
210
+ [
145
211
UrlRewrite::ENTITY_ID => 'entity_id ' ,
146
212
UrlRewrite::ENTITY_TYPE => 'entity_type ' ,
147
- UrlRewrite::STORE_ID => $ storeId ,
148
- ])
213
+ UrlRewrite::STORE_ID => $ store ->getStoreId (),
214
+ ]
215
+ )
149
216
->will ($ this ->returnValue ($ rewrite ));
150
217
151
- $ this ->assertEquals ($ url . $ separator . '___store= ' . $ storeCode , $ this ->block ->getHref ());
218
+ $ rewrite ->expects ($ this ->once ())
219
+ ->method ('getRequestPath ' )
220
+ ->will ($ this ->returnValue ($ path ));
221
+
222
+ $ this ->assertContains ($ expected , $ this ->block ->getHref ());
152
223
}
153
224
225
+ /**
226
+ * Tests getLabel with custom text
227
+ */
154
228
public function testGetLabelWithCustomText ()
155
229
{
156
230
$ customText = 'Some text ' ;
157
231
$ this ->block ->setData ('anchor_text ' , $ customText );
158
232
$ this ->assertEquals ($ customText , $ this ->block ->getLabel ());
159
233
}
160
234
235
+ /**
236
+ * Tests getLabel without custom text
237
+ */
161
238
public function testGetLabelWithoutCustomText ()
162
239
{
163
240
$ category = 'Some text ' ;
@@ -178,17 +255,20 @@ public function testGetLabelWithoutCustomText()
178
255
public function dataProviderForTestGetHrefWithoutUrlStoreSuffix ()
179
256
{
180
257
return [
181
- ['url ' , ' ? ' ],
182
- ['url?some_parameter ' , ' & ' ],
258
+ ['/accessories.html ' , true , ' french/accessories.html ' ],
259
+ ['/accessories.html ' , false , ' /accessories.html ' ],
183
260
];
184
261
}
185
262
263
+ /**
264
+ * Tests getHref with product entity and additional category id in the id_path
265
+ */
186
266
public function testGetHrefWithForProductWithCategoryIdParameter ()
187
267
{
188
268
$ storeId = 15 ;
189
269
$ this ->block ->setData ('id_path ' , ProductUrlRewriteGenerator::ENTITY_TYPE . '/entity_id/category_id ' );
190
270
191
- $ store = $ this ->createPartialMock (\ Magento \ Store \ Model \ Store::class, ['getId ' , '__wakeUp ' ]);
271
+ $ store = $ this ->createPartialMock (Store::class, ['getId ' , '__wakeUp ' ]);
192
272
$ store ->expects ($ this ->any ())
193
273
->method ('getId ' )
194
274
->will ($ this ->returnValue ($ storeId ));
@@ -197,13 +277,16 @@ public function testGetHrefWithForProductWithCategoryIdParameter()
197
277
->method ('getStore ' )
198
278
->will ($ this ->returnValue ($ store ));
199
279
200
- $ this ->urlFinder ->expects ($ this ->once ())->method ('findOneByData ' )
201
- ->with ([
202
- UrlRewrite::ENTITY_ID => 'entity_id ' ,
203
- UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE ,
204
- UrlRewrite::STORE_ID => $ storeId ,
205
- UrlRewrite::METADATA => ['category_id ' => 'category_id ' ],
206
- ])
280
+ $ this ->urlFinder ->expects ($ this ->once ())
281
+ ->method ('findOneByData ' )
282
+ ->with (
283
+ [
284
+ UrlRewrite::ENTITY_ID => 'entity_id ' ,
285
+ UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE ,
286
+ UrlRewrite::STORE_ID => $ storeId ,
287
+ UrlRewrite::METADATA => ['category_id ' => 'category_id ' ],
288
+ ]
289
+ )
207
290
->will ($ this ->returnValue (false ));
208
291
209
292
$ this ->block ->getHref ();
0 commit comments