6
6
namespace Magento \Framework ;
7
7
8
8
use Zend \Stdlib \Parameters ;
9
+ use Magento \TestFramework \Helper \Bootstrap ;
9
10
10
11
class UrlTest extends \PHPUnit \Framework \TestCase
11
12
{
12
13
/**
13
14
* @var \Magento\Framework\UrlInterface
14
15
*/
15
- protected $ _model ;
16
+ protected $ model ;
16
17
17
18
protected function setUp ()
18
19
{
19
- $ this ->_model = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
20
- \Magento \Framework \Url::class
21
- );
20
+ $ this ->model = Bootstrap::getObjectManager ()->create (\Magento \Framework \Url::class);
22
21
}
23
22
24
23
public function testSetGetUseSession ()
25
24
{
26
- $ this ->assertTrue ((bool )$ this ->_model ->getUseSession ());
27
- $ this ->_model ->setUseSession (false );
28
- $ this ->assertFalse ($ this ->_model ->getUseSession ());
25
+ $ this ->assertTrue ((bool )$ this ->model ->getUseSession ());
26
+ $ this ->model ->setUseSession (false );
27
+ $ this ->assertFalse ($ this ->model ->getUseSession ());
29
28
}
30
29
31
30
public function testSetRouteFrontName ()
32
31
{
33
32
$ value = 'route ' ;
34
- $ this ->_model ->setRouteFrontName ($ value );
35
- $ this ->assertEquals ($ value , $ this ->_model ->getData ('route_front_name ' ));
33
+ $ this ->model ->setRouteFrontName ($ value );
34
+ $ this ->assertEquals ($ value , $ this ->model ->getData ('route_front_name ' ));
36
35
}
37
36
38
37
public function testGetConfigData ()
39
38
{
40
- $ this ->assertEquals ('http://localhost/ ' , $ this ->_model ->getConfigData ('base_url ' ));
39
+ $ this ->assertEquals ('http://localhost/ ' , $ this ->model ->getConfigData ('base_url ' ));
41
40
}
42
41
43
42
/**
@@ -46,7 +45,7 @@ public function testGetConfigData()
46
45
*/
47
46
public function testGetBaseUrlDefaults ()
48
47
{
49
- $ this ->assertEquals ('http://localhost/index.php/ ' , $ this ->_model ->getBaseUrl ());
48
+ $ this ->assertEquals ('http://localhost/index.php/ ' , $ this ->model ->getBaseUrl ());
50
49
}
51
50
52
51
/**
@@ -56,7 +55,7 @@ public function testGetBaseUrlDefaults()
56
55
*/
57
56
public function testGetBaseUrlSeoRewrites ()
58
57
{
59
- $ this ->assertEquals ('http://localhost/ ' , $ this ->_model ->getBaseUrl ());
58
+ $ this ->assertEquals ('http://localhost/ ' , $ this ->model ->getBaseUrl ());
60
59
}
61
60
62
61
/**
@@ -75,10 +74,96 @@ public function testGetBaseUrlSeoRewrites()
75
74
*/
76
75
public function testGetBaseUrlConfigured ($ params , $ expectedUrl )
77
76
{
78
- $ actualUrl = $ this ->_model ->getBaseUrl ($ params );
77
+ $ actualUrl = $ this ->model ->getBaseUrl ($ params );
79
78
$ this ->assertEquals ($ expectedUrl , $ actualUrl );
80
79
}
81
80
81
+ /**
82
+ * Note: isolation flushes the URL memory cache
83
+ * @magentoAppIsolation enabled
84
+ *
85
+ * @magentoConfigFixture current_store web/secure/base_url http://sample.com/
86
+ * @magentoConfigFixture current_store web/unsecure/base_link_url http://sample.com/
87
+ * @magentoConfigFixture current_store web/secure/base_link_url https://sample.com/
88
+ * @magentoConfigFixture current_store web/secure/use_in_frontend 1
89
+ *
90
+ * @magentoAppArea frontend
91
+ */
92
+ public function testGetUnsecureUrlInSecureArea ()
93
+ {
94
+ /** @var \Magento\Framework\App\Request\Http $request */
95
+ $ request = Bootstrap::getObjectManager ()->create (\Magento \Framework \App \Request \Http::class);
96
+ //Emulate HTTPS request
97
+ $ request ->getServer ()->set ('HTTPS ' , 'on ' );
98
+ $ request ->getServer ()->set ('SERVER_PORT ' , 443 );
99
+
100
+ $ model = Bootstrap::getObjectManager ()->create (\Magento \Framework \Url::class, ['request ' => $ request ]);
101
+
102
+ $ secureUrl = $ model ->getUrl ('some/index/controller ' , ['_nosid ' => 1 ]);
103
+ $ this ->assertEquals (
104
+ 'https://sample.com/index.php/some/index/controller/ ' ,
105
+ $ secureUrl ,
106
+ 'Default URL in secure area is incorrect '
107
+ );
108
+
109
+ $ secureUrl = $ model ->getUrl ('some/index/controller ' , ['_secure ' => true , '_nosid ' => 1 ]);
110
+ $ this ->assertEquals (
111
+ 'https://sample.com/index.php/some/index/controller/ ' ,
112
+ $ secureUrl ,
113
+ 'Secure URL in secure area is incorrect '
114
+ );
115
+
116
+ $ unsecureUrl = $ model ->getUrl ('some/index/controller ' , ['_secure ' => false , '_nosid ' => 1 ]);
117
+ $ this ->assertEquals (
118
+ 'http://sample.com/index.php/some/index/controller/ ' ,
119
+ $ unsecureUrl ,
120
+ 'Unsecure URL in secure area is incorrect '
121
+ );
122
+ }
123
+
124
+ /**
125
+ * Note: isolation flushes the URL memory cache
126
+ * @magentoAppIsolation enabled
127
+ *
128
+ * @magentoConfigFixture current_store web/secure/base_url http://sample.com/
129
+ * @magentoConfigFixture current_store web/unsecure/base_link_url http://sample.com/
130
+ * @magentoConfigFixture current_store web/secure/base_link_url https://sample.com/
131
+ * @magentoConfigFixture current_store web/secure/use_in_frontend 1
132
+ *
133
+ * @magentoAppArea frontend
134
+ */
135
+ public function testGetSecureUrlInUnsecureArea ()
136
+ {
137
+ /** @var \Magento\Framework\App\Request\Http $request */
138
+ $ request = Bootstrap::getObjectManager ()->create (\Magento \Framework \App \Request \Http::class);
139
+ //Emulate HTTPS request
140
+ $ request ->getServer ()->set ('HTTPS ' , 'off ' );
141
+ $ request ->getServer ()->set ('SERVER_PORT ' , 80 );
142
+
143
+ $ model = Bootstrap::getObjectManager ()->create (\Magento \Framework \Url::class, ['request ' => $ request ]);
144
+
145
+ $ secureUrl = $ model ->getUrl ('some/index/controller ' , ['_nosid ' => 1 ]);
146
+ $ this ->assertEquals (
147
+ 'http://sample.com/index.php/some/index/controller/ ' ,
148
+ $ secureUrl ,
149
+ 'Default URL in unsecure area is incorrect '
150
+ );
151
+
152
+ $ secureUrl = $ model ->getUrl ('some/index/controller ' , ['_secure ' => true , '_nosid ' => 1 ]);
153
+ $ this ->assertEquals (
154
+ 'https://sample.com/index.php/some/index/controller/ ' ,
155
+ $ secureUrl ,
156
+ 'Secure URL in unsecure area is incorrect '
157
+ );
158
+
159
+ $ unsecureUrl = $ model ->getUrl ('some/index/controller ' , ['_secure ' => false , '_nosid ' => 1 ]);
160
+ $ this ->assertEquals (
161
+ 'http://sample.com/index.php/some/index/controller/ ' ,
162
+ $ unsecureUrl ,
163
+ 'Unsecure URL in unsecure area is incorrect '
164
+ );
165
+ }
166
+
82
167
/**
83
168
* Check that url type is restored to default after call getBaseUrl with type specified in params
84
169
*/
@@ -87,21 +172,21 @@ public function testGetBaseUrlWithTypeRestoring()
87
172
/**
88
173
* Get base URL with default type
89
174
*/
90
- $ this ->assertEquals ('http://localhost/index.php/ ' , $ this ->_model ->getBaseUrl (), 'Incorrect link url ' );
175
+ $ this ->assertEquals ('http://localhost/index.php/ ' , $ this ->model ->getBaseUrl (), 'Incorrect link url ' );
91
176
92
177
/**
93
178
* Set specified type
94
179
*/
95
- $ webUrl = $ this ->_model ->getBaseUrl (['_type ' => \Magento \Framework \UrlInterface::URL_TYPE_WEB ]);
180
+ $ webUrl = $ this ->model ->getBaseUrl (['_type ' => \Magento \Framework \UrlInterface::URL_TYPE_WEB ]);
96
181
$ this ->assertEquals ('http://localhost/ ' , $ webUrl , 'Incorrect web url ' );
97
- $ this ->assertEquals ('http://localhost/index.php/ ' , $ this ->_model ->getBaseUrl (), 'Incorrect link url ' );
182
+ $ this ->assertEquals ('http://localhost/index.php/ ' , $ this ->model ->getBaseUrl (), 'Incorrect link url ' );
98
183
99
184
/**
100
185
* Get url with type specified in params
101
186
*/
102
- $ mediaUrl = $ this ->_model ->getBaseUrl (['_type ' => \Magento \Framework \UrlInterface::URL_TYPE_MEDIA ]);
187
+ $ mediaUrl = $ this ->model ->getBaseUrl (['_type ' => \Magento \Framework \UrlInterface::URL_TYPE_MEDIA ]);
103
188
$ this ->assertEquals ('http://localhost/pub/media/ ' , $ mediaUrl , 'Incorrect media url ' );
104
- $ this ->assertEquals ('http://localhost/index.php/ ' , $ this ->_model ->getBaseUrl (), 'Incorrect link url ' );
189
+ $ this ->assertEquals ('http://localhost/index.php/ ' , $ this ->model ->getBaseUrl (), 'Incorrect link url ' );
105
190
}
106
191
107
192
public function getBaseUrlConfiguredDataProvider ()
@@ -121,24 +206,24 @@ public function getBaseUrlConfiguredDataProvider()
121
206
122
207
public function testSetGetRouteName ()
123
208
{
124
- $ this ->_model ->setRouteName ('catalog ' );
125
- $ this ->assertEquals ('catalog ' , $ this ->_model ->getRouteName ());
209
+ $ this ->model ->setRouteName ('catalog ' );
210
+ $ this ->assertEquals ('catalog ' , $ this ->model ->getRouteName ());
126
211
127
212
$ this ->markTestIncomplete ('setRouteName() logic is unclear. ' );
128
213
}
129
214
130
215
public function testSetGetControllerName ()
131
216
{
132
- $ this ->_model ->setControllerName ('product ' );
133
- $ this ->assertEquals ('product ' , $ this ->_model ->getControllerName ());
217
+ $ this ->model ->setControllerName ('product ' );
218
+ $ this ->assertEquals ('product ' , $ this ->model ->getControllerName ());
134
219
135
220
$ this ->markTestIncomplete ('setControllerName() logic is unclear. ' );
136
221
}
137
222
138
223
public function testSetGetActionName ()
139
224
{
140
- $ this ->_model ->setActionName ('view ' );
141
- $ this ->assertEquals ('view ' , $ this ->_model ->getActionName ());
225
+ $ this ->model ->setActionName ('view ' );
226
+ $ this ->assertEquals ('view ' , $ this ->model ->getActionName ());
142
227
143
228
$ this ->markTestIncomplete ('setActionName() logic is unclear. ' );
144
229
}
@@ -149,21 +234,21 @@ public function testSetGetActionName()
149
234
*/
150
235
public function testGetRouteUrl ()
151
236
{
152
- $ this ->assertEquals ('http://localhost/index.php/ ' , $ this ->_model ->getRouteUrl ());
237
+ $ this ->assertEquals ('http://localhost/index.php/ ' , $ this ->model ->getRouteUrl ());
153
238
$ this ->assertEquals (
154
239
'http://localhost/index.php/catalog/product/view/id/50/ ' ,
155
- $ this ->_model ->getRouteUrl ('catalog/product/view ' , ['id ' => 50 ])
240
+ $ this ->model ->getRouteUrl ('catalog/product/view ' , ['id ' => 50 ])
156
241
);
157
242
$ this ->assertEquals (
158
243
'http://localhost/index.php/fancy_uri ' ,
159
- $ this ->_model ->getRouteUrl ('core/index/index ' , ['_direct ' => 'fancy_uri ' ])
244
+ $ this ->model ->getRouteUrl ('core/index/index ' , ['_direct ' => 'fancy_uri ' ])
160
245
);
161
246
}
162
247
163
248
public function testSetGetFragment ()
164
249
{
165
- $ this ->_model ->setFragment ('value ' );
166
- $ this ->assertEquals ('value ' , $ this ->_model ->getFragment ());
250
+ $ this ->model ->setFragment ('value ' );
251
+ $ this ->assertEquals ('value ' , $ this ->model ->getFragment ());
167
252
}
168
253
169
254
/**
@@ -172,7 +257,7 @@ public function testSetGetFragment()
172
257
*/
173
258
public function testGetUrl ()
174
259
{
175
- $ result = $ this ->_model ->getUrl (
260
+ $ result = $ this ->model ->getUrl (
176
261
'catalog/product/view ' ,
177
262
['_fragment ' => 'anchor ' , '_escape ' => 1 , '_query ' => 'foo=bar ' , '_nosid ' => 1 , 'id ' => 100 ]
178
263
);
@@ -185,9 +270,9 @@ public function testGetUrl()
185
270
*/
186
271
public function testGetUrlDoesntAddQueryParamsOnConsequentCalls ()
187
272
{
188
- $ result = $ this ->_model ->getUrl ('catalog/product/view ' , ['_query ' => 'foo=bar ' , '_nosid ' => 1 ]);
273
+ $ result = $ this ->model ->getUrl ('catalog/product/view ' , ['_query ' => 'foo=bar ' , '_nosid ' => 1 ]);
189
274
$ this ->assertEquals ('http://localhost/index.php/catalog/product/view/?foo=bar ' , $ result );
190
- $ result = $ this ->_model ->getUrl ('catalog/product/view ' , ['_nosid ' => 1 ]);
275
+ $ result = $ this ->model ->getUrl ('catalog/product/view ' , ['_nosid ' => 1 ]);
191
276
$ this ->assertEquals ('http://localhost/index.php/catalog/product/view/ ' , $ result );
192
277
}
193
278
@@ -198,9 +283,9 @@ public function testGetUrlDoesntAddQueryParamsOnConsequentCalls()
198
283
*/
199
284
public function testGetUrlDoesntAddFragmentOnConsequentCalls ()
200
285
{
201
- $ result = $ this ->_model ->getUrl ('catalog/product/view ' , ['_nosid ' => 1 , '_fragment ' => 'section ' ]);
286
+ $ result = $ this ->model ->getUrl ('catalog/product/view ' , ['_nosid ' => 1 , '_fragment ' => 'section ' ]);
202
287
$ this ->assertEquals ('http://localhost/index.php/catalog/product/view/#section ' , $ result );
203
- $ result = $ this ->_model ->getUrl ('catalog/product/view ' , ['_nosid ' => 1 ]);
288
+ $ result = $ this ->model ->getUrl ('catalog/product/view ' , ['_nosid ' => 1 ]);
204
289
$ this ->assertEquals ('http://localhost/index.php/catalog/product/view/ ' , $ result );
205
290
}
206
291
@@ -226,10 +311,10 @@ public function testGetUrlOnConsequentCalls(
226
311
$ firstExpectedUrl ,
227
312
$ secondExpectedUrl
228
313
) {
229
- $ result = $ this ->_model ->getUrl ($ firstCallUrl , $ firstRouteParams );
314
+ $ result = $ this ->model ->getUrl ($ firstCallUrl , $ firstRouteParams );
230
315
$ this ->assertEquals ($ firstExpectedUrl , $ result );
231
316
232
- $ result = $ this ->_model ->getUrl ($ secondCallUrl , $ secondRouteParams );
317
+ $ result = $ this ->model ->getUrl ($ secondCallUrl , $ secondRouteParams );
233
318
$ this ->assertEquals ($ secondExpectedUrl , $ result );
234
319
}
235
320
@@ -375,7 +460,7 @@ public function consequentCallsDataProvider()
375
460
376
461
public function testEscape ()
377
462
{
378
- $ this ->assertEquals ('%22%27%3E%3C ' , $ this ->_model ->escape ('" \'>< ' ));
463
+ $ this ->assertEquals ('%22%27%3E%3C ' , $ this ->model ->escape ('" \'>< ' ));
379
464
}
380
465
381
466
/**
@@ -384,7 +469,7 @@ public function testEscape()
384
469
*/
385
470
public function testGetDirectUrl ()
386
471
{
387
- $ directUrl = $ this ->_model ->getDirectUrl ('fancy_uri ' , ['_query ' => ['foo ' => 'bar ' ]]);
472
+ $ directUrl = $ this ->model ->getDirectUrl ('fancy_uri ' , ['_query ' => ['foo ' => 'bar ' ]]);
388
473
$ this ->assertEquals ('http://localhost/index.php/fancy_uri?foo=bar ' , $ directUrl );
389
474
}
390
475
@@ -400,15 +485,15 @@ public function testSessionUrlVar()
400
485
$ sessionId = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->get (
401
486
\Magento \Framework \Session \Generic::class
402
487
)->getSessionId ();
403
- $ sessionUrl = $ this ->_model ->sessionUrlVar ('<a href="http://example.com/?___SID=U">www.example.com</a> ' );
488
+ $ sessionUrl = $ this ->model ->sessionUrlVar ('<a href="http://example.com/?___SID=U">www.example.com</a> ' );
404
489
$ this ->assertEquals ('<a href="http://example.com/?SID= ' . $ sessionId . '">www.example.com</a> ' , $ sessionUrl );
405
490
}
406
491
407
492
public function testUseSessionIdForUrl ()
408
493
{
409
494
$ _SERVER ['HTTP_HOST ' ] = 'localhost ' ;
410
- $ this ->assertFalse ($ this ->_model ->useSessionIdForUrl (true ));
411
- $ this ->assertFalse ($ this ->_model ->useSessionIdForUrl (false ));
495
+ $ this ->assertFalse ($ this ->model ->useSessionIdForUrl (true ));
496
+ $ this ->assertFalse ($ this ->model ->useSessionIdForUrl (false ));
412
497
}
413
498
414
499
/**
@@ -421,9 +506,9 @@ public function testIsOwnOriginUrl()
421
506
/** @var $request \Magento\TestFramework\Request */
422
507
$ request = $ objectManager ->get (\Magento \Framework \App \RequestInterface::class);
423
508
$ request ->setServer (new Parameters (['HTTP_REFERER ' => 'http://localhost/ ' ]));
424
- $ this ->assertTrue ($ this ->_model ->isOwnOriginUrl ());
509
+ $ this ->assertTrue ($ this ->model ->isOwnOriginUrl ());
425
510
426
511
$ request ->setServer (new Parameters (['HTTP_REFERER ' => 'http://example.com/ ' ]));
427
- $ this ->assertFalse ($ this ->_model ->isOwnOriginUrl ());
512
+ $ this ->assertFalse ($ this ->model ->isOwnOriginUrl ());
428
513
}
429
514
}
0 commit comments