Skip to content

Commit 6084381

Browse files
MAGETWO-75455: [Backport] - Added ability generate unsecure URL if current URL is secure #10244 - for 2.2
1 parent 29bc9ad commit 6084381

File tree

2 files changed

+131
-43
lines changed
  • dev/tests/integration/testsuite/Magento/Framework
  • lib/internal/Magento/Framework

2 files changed

+131
-43
lines changed

dev/tests/integration/testsuite/Magento/Framework/UrlTest.php

Lines changed: 128 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,37 @@
66
namespace Magento\Framework;
77

88
use Zend\Stdlib\Parameters;
9+
use Magento\TestFramework\Helper\Bootstrap;
910

1011
class UrlTest extends \PHPUnit\Framework\TestCase
1112
{
1213
/**
1314
* @var \Magento\Framework\UrlInterface
1415
*/
15-
protected $_model;
16+
protected $model;
1617

1718
protected function setUp()
1819
{
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);
2221
}
2322

2423
public function testSetGetUseSession()
2524
{
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());
2928
}
3029

3130
public function testSetRouteFrontName()
3231
{
3332
$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'));
3635
}
3736

3837
public function testGetConfigData()
3938
{
40-
$this->assertEquals('http://localhost/', $this->_model->getConfigData('base_url'));
39+
$this->assertEquals('http://localhost/', $this->model->getConfigData('base_url'));
4140
}
4241

4342
/**
@@ -46,7 +45,7 @@ public function testGetConfigData()
4645
*/
4746
public function testGetBaseUrlDefaults()
4847
{
49-
$this->assertEquals('http://localhost/index.php/', $this->_model->getBaseUrl());
48+
$this->assertEquals('http://localhost/index.php/', $this->model->getBaseUrl());
5049
}
5150

5251
/**
@@ -56,7 +55,7 @@ public function testGetBaseUrlDefaults()
5655
*/
5756
public function testGetBaseUrlSeoRewrites()
5857
{
59-
$this->assertEquals('http://localhost/', $this->_model->getBaseUrl());
58+
$this->assertEquals('http://localhost/', $this->model->getBaseUrl());
6059
}
6160

6261
/**
@@ -75,10 +74,96 @@ public function testGetBaseUrlSeoRewrites()
7574
*/
7675
public function testGetBaseUrlConfigured($params, $expectedUrl)
7776
{
78-
$actualUrl = $this->_model->getBaseUrl($params);
77+
$actualUrl = $this->model->getBaseUrl($params);
7978
$this->assertEquals($expectedUrl, $actualUrl);
8079
}
8180

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+
82167
/**
83168
* Check that url type is restored to default after call getBaseUrl with type specified in params
84169
*/
@@ -87,21 +172,21 @@ public function testGetBaseUrlWithTypeRestoring()
87172
/**
88173
* Get base URL with default type
89174
*/
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');
91176

92177
/**
93178
* Set specified type
94179
*/
95-
$webUrl = $this->_model->getBaseUrl(['_type' => \Magento\Framework\UrlInterface::URL_TYPE_WEB]);
180+
$webUrl = $this->model->getBaseUrl(['_type' => \Magento\Framework\UrlInterface::URL_TYPE_WEB]);
96181
$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');
98183

99184
/**
100185
* Get url with type specified in params
101186
*/
102-
$mediaUrl = $this->_model->getBaseUrl(['_type' => \Magento\Framework\UrlInterface::URL_TYPE_MEDIA]);
187+
$mediaUrl = $this->model->getBaseUrl(['_type' => \Magento\Framework\UrlInterface::URL_TYPE_MEDIA]);
103188
$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');
105190
}
106191

107192
public function getBaseUrlConfiguredDataProvider()
@@ -121,24 +206,24 @@ public function getBaseUrlConfiguredDataProvider()
121206

122207
public function testSetGetRouteName()
123208
{
124-
$this->_model->setRouteName('catalog');
125-
$this->assertEquals('catalog', $this->_model->getRouteName());
209+
$this->model->setRouteName('catalog');
210+
$this->assertEquals('catalog', $this->model->getRouteName());
126211

127212
$this->markTestIncomplete('setRouteName() logic is unclear.');
128213
}
129214

130215
public function testSetGetControllerName()
131216
{
132-
$this->_model->setControllerName('product');
133-
$this->assertEquals('product', $this->_model->getControllerName());
217+
$this->model->setControllerName('product');
218+
$this->assertEquals('product', $this->model->getControllerName());
134219

135220
$this->markTestIncomplete('setControllerName() logic is unclear.');
136221
}
137222

138223
public function testSetGetActionName()
139224
{
140-
$this->_model->setActionName('view');
141-
$this->assertEquals('view', $this->_model->getActionName());
225+
$this->model->setActionName('view');
226+
$this->assertEquals('view', $this->model->getActionName());
142227

143228
$this->markTestIncomplete('setActionName() logic is unclear.');
144229
}
@@ -149,21 +234,21 @@ public function testSetGetActionName()
149234
*/
150235
public function testGetRouteUrl()
151236
{
152-
$this->assertEquals('http://localhost/index.php/', $this->_model->getRouteUrl());
237+
$this->assertEquals('http://localhost/index.php/', $this->model->getRouteUrl());
153238
$this->assertEquals(
154239
'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])
156241
);
157242
$this->assertEquals(
158243
'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'])
160245
);
161246
}
162247

163248
public function testSetGetFragment()
164249
{
165-
$this->_model->setFragment('value');
166-
$this->assertEquals('value', $this->_model->getFragment());
250+
$this->model->setFragment('value');
251+
$this->assertEquals('value', $this->model->getFragment());
167252
}
168253

169254
/**
@@ -172,7 +257,7 @@ public function testSetGetFragment()
172257
*/
173258
public function testGetUrl()
174259
{
175-
$result = $this->_model->getUrl(
260+
$result = $this->model->getUrl(
176261
'catalog/product/view',
177262
['_fragment' => 'anchor', '_escape' => 1, '_query' => 'foo=bar', '_nosid' => 1, 'id' => 100]
178263
);
@@ -185,9 +270,9 @@ public function testGetUrl()
185270
*/
186271
public function testGetUrlDoesntAddQueryParamsOnConsequentCalls()
187272
{
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]);
189274
$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]);
191276
$this->assertEquals('http://localhost/index.php/catalog/product/view/', $result);
192277
}
193278

@@ -198,9 +283,9 @@ public function testGetUrlDoesntAddQueryParamsOnConsequentCalls()
198283
*/
199284
public function testGetUrlDoesntAddFragmentOnConsequentCalls()
200285
{
201-
$result = $this->_model->getUrl('catalog/product/view', ['_nosid' => 1, '_fragment' => 'section']);
286+
$result = $this->model->getUrl('catalog/product/view', ['_nosid' => 1, '_fragment' => 'section']);
202287
$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]);
204289
$this->assertEquals('http://localhost/index.php/catalog/product/view/', $result);
205290
}
206291

@@ -226,10 +311,10 @@ public function testGetUrlOnConsequentCalls(
226311
$firstExpectedUrl,
227312
$secondExpectedUrl
228313
) {
229-
$result = $this->_model->getUrl($firstCallUrl, $firstRouteParams);
314+
$result = $this->model->getUrl($firstCallUrl, $firstRouteParams);
230315
$this->assertEquals($firstExpectedUrl, $result);
231316

232-
$result = $this->_model->getUrl($secondCallUrl, $secondRouteParams);
317+
$result = $this->model->getUrl($secondCallUrl, $secondRouteParams);
233318
$this->assertEquals($secondExpectedUrl, $result);
234319
}
235320

@@ -375,7 +460,7 @@ public function consequentCallsDataProvider()
375460

376461
public function testEscape()
377462
{
378-
$this->assertEquals('%22%27%3E%3C', $this->_model->escape('"\'><'));
463+
$this->assertEquals('%22%27%3E%3C', $this->model->escape('"\'><'));
379464
}
380465

381466
/**
@@ -384,7 +469,7 @@ public function testEscape()
384469
*/
385470
public function testGetDirectUrl()
386471
{
387-
$directUrl = $this->_model->getDirectUrl('fancy_uri', ['_query' => ['foo' => 'bar']]);
472+
$directUrl = $this->model->getDirectUrl('fancy_uri', ['_query' => ['foo' => 'bar']]);
388473
$this->assertEquals('http://localhost/index.php/fancy_uri?foo=bar', $directUrl);
389474
}
390475

@@ -400,15 +485,15 @@ public function testSessionUrlVar()
400485
$sessionId = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
401486
\Magento\Framework\Session\Generic::class
402487
)->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>');
404489
$this->assertEquals('<a href="http://example.com/?SID=' . $sessionId . '">www.example.com</a>', $sessionUrl);
405490
}
406491

407492
public function testUseSessionIdForUrl()
408493
{
409494
$_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));
412497
}
413498

414499
/**
@@ -421,9 +506,9 @@ public function testIsOwnOriginUrl()
421506
/** @var $request \Magento\TestFramework\Request */
422507
$request = $objectManager->get(\Magento\Framework\App\RequestInterface::class);
423508
$request->setServer(new Parameters(['HTTP_REFERER' => 'http://localhost/']));
424-
$this->assertTrue($this->_model->isOwnOriginUrl());
509+
$this->assertTrue($this->model->isOwnOriginUrl());
425510

426511
$request->setServer(new Parameters(['HTTP_REFERER' => 'http://example.com/']));
427-
$this->assertFalse($this->_model->isOwnOriginUrl());
512+
$this->assertFalse($this->model->isOwnOriginUrl());
428513
}
429514
}

lib/internal/Magento/Framework/Url.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,9 @@ protected function _getType()
389389
protected function _isSecure()
390390
{
391391
if ($this->_request->isSecure()) {
392+
if ($this->getRouteParamsResolver()->hasData('secure')) {
393+
return (bool) $this->getRouteParamsResolver()->getData('secure');
394+
}
392395
return true;
393396
}
394397

0 commit comments

Comments
 (0)