Skip to content

Commit ac269e0

Browse files
author
Dale Sikkema
committed
MAGETWO-33609: pull request for stories in this sprint
- unit test for SecureUrl
1 parent 661b542 commit ac269e0

File tree

3 files changed

+143
-8
lines changed

3 files changed

+143
-8
lines changed

app/code/Magento/Backend/Model/Config/Backend/Baseurl.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
namespace Magento\Backend\Model\Config\Backend;
77

88
use Magento\Framework\Store\StoreManagerInterface;
9-
use Magento\Store\Model\Store;
109

1110
class Baseurl extends \Magento\Framework\App\Config\Value
1211
{
@@ -69,7 +68,7 @@ private function _validateUnsecure($value)
6968
{
7069
$placeholders = ['{{unsecure_base_url}}'];
7170
switch ($this->getPath()) {
72-
case Store::XML_PATH_UNSECURE_BASE_URL:
71+
case \Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_URL:
7372
$this->_assertValuesOrUrl(['{{base_url}}'], $value);
7473
break;
7574
case \Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_LINK_URL:
@@ -95,7 +94,7 @@ private function _validateSecure($value)
9594
{
9695
$placeholders = ['{{unsecure_base_url}}', '{{secure_base_url}}'];
9796
switch ($this->getPath()) {
98-
case Store::XML_PATH_SECURE_BASE_URL:
97+
case \Magento\Store\Model\Store::XML_PATH_SECURE_BASE_URL:
9998
$this->_assertValuesOrUrl(['{{base_url}}', '{{unsecure_base_url}}'], $value);
10099
break;
101100
case \Magento\Store\Model\Store::XML_PATH_SECURE_BASE_LINK_URL:
@@ -207,9 +206,9 @@ public function afterSave()
207206
{
208207
if ($this->isValueChanged()) {
209208
switch ($this->getPath()) {
210-
case Store::XML_PATH_UNSECURE_BASE_URL:
209+
case \Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_URL:
211210
case \Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_MEDIA_URL:
212-
case Store::XML_PATH_SECURE_BASE_URL:
211+
case \Magento\Store\Model\Store::XML_PATH_SECURE_BASE_URL:
213212
case \Magento\Store\Model\Store::XML_PATH_SECURE_BASE_MEDIA_URL:
214213
$this->_mergeService->cleanMergedJsCss();
215214
break;

app/code/Magento/Store/Model/SecureUrl.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ class SecureUrl implements \Magento\Framework\App\Router\SecureUrlInterface
2626
* @param StoreManagerInterface $storeManager
2727
*/
2828
public function __construct(
29-
\Magento\Framework\App\ResponseFactory $responseFactory,
30-
\Magento\Framework\UrlInterface $url,
3129
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
3230
\Magento\Framework\Url\SecurityInfoInterface $urlSecurityInfo,
3331
StoreManagerInterface $storeManager
@@ -46,7 +44,7 @@ public function __construct(
4644
*/
4745
public function getCurrentSecureUrl(\Magento\Framework\App\RequestInterface $request)
4846
{
49-
$alias = $request->getAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS) || $request->getPathInfo();
47+
$alias = $request->getAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS) ?: $request->getPathInfo();
5048
return $this->storeManager->getStore()->getBaseUrl('link', true) . ltrim($alias, '/');
5149
}
5250

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
// @codingStandardsIgnoreFile
8+
9+
namespace Magento\Store\Model;
10+
11+
use Magento\Framework\Store\StoreManagerInterface;
12+
use Magento\Framework\Store\ScopeInterface;
13+
use Magento\Store\Model\Store;
14+
use Magento\TestFramework\Helper\ObjectManager;
15+
16+
class SecureUrlTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/** @var \Magento\Framework\App\Config\ScopeConfigInterface | \PHPUnit_Framework_MockObject_MockObject*/
19+
private $scopeConfigMock;
20+
/** @var \Magento\Framework\Url\SecurityInfoInterface | \PHPUnit_Framework_MockObject_MockObject*/
21+
private $urlSecurityInfoMock;
22+
/** @var StoreManagerInterface | \PHPUnit_Framework_MockObject_MockObject*/
23+
private $storeManagerMock;
24+
/** @var Store | \PHPUnit_Framework_MockObject_MockObject*/
25+
private $storeMock;
26+
/** @var \Magento\Store\Model\SecureUrl */
27+
protected $model;
28+
29+
public function setUp()
30+
{
31+
$this->scopeConfigMock = $this->getMockBuilder('Magento\Framework\App\Config\ScopeConfigInterface')
32+
->disableOriginalConstructor()
33+
->getMock();
34+
$this->urlSecurityInfoMock = $this->getMockBuilder('Magento\Framework\Url\SecurityInfoInterface')
35+
->disableOriginalConstructor()
36+
->getMock();
37+
$this->storeManagerMock = $this->getMockBuilder('Magento\Framework\Store\StoreManagerInterface')
38+
->disableOriginalConstructor()
39+
->getMock();
40+
$this->storeMock = $this->getMockBuilder('Magento\Store\Model\Store')
41+
->disableOriginalConstructor()
42+
->getMock();
43+
$mockArgs = [
44+
'scopeConfig' => $this->scopeConfigMock,
45+
'urlSecurityInfo' => $this->urlSecurityInfoMock,
46+
'storeManager' => $this->storeManagerMock,
47+
];
48+
$this->model = (new ObjectManager($this))->getObject('\Magento\Store\Model\SecureUrl', $mockArgs);
49+
}
50+
51+
public function testGetCurrentSecureUrlNoAlias()
52+
{
53+
$baseUrl = 'base-store.url/';
54+
$pathInfo = 'path/to/action';
55+
56+
$this->storeMock->expects($this->once())->method('getBaseUrl')->with('link', true)->willReturn($baseUrl);
57+
$this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
58+
59+
$request = $this->getMockBuilder('Magento\Framework\App\Request\Http')
60+
->disableOriginalConstructor()
61+
->getMock();
62+
63+
$request->expects($this->once())->method('getAlias')->willReturn(null);
64+
$request->expects($this->once())->method('getPathInfo')->willReturn($pathInfo);
65+
$this->assertSame($baseUrl . $pathInfo, $this->model->getCurrentSecureUrl($request));
66+
}
67+
68+
public function testGetCurrentSecureUrlWithAlias()
69+
{
70+
$baseUrl = 'base-store.url/';
71+
$alias = 'action-alias';
72+
73+
$this->storeMock->expects($this->once())->method('getBaseUrl')->with('link', true)->willReturn($baseUrl);
74+
$this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
75+
76+
$request = $this->getMockBuilder('Magento\Framework\App\Request\Http')
77+
->disableOriginalConstructor()
78+
->getMock();
79+
80+
$request->expects($this->once())->method('getAlias')->willReturn($alias);
81+
$request->expects($this->never())->method('getPathInfo');
82+
$this->assertSame($baseUrl . $alias, $this->model->getCurrentSecureUrl($request));
83+
}
84+
85+
/**
86+
* @dataProvider urlSchemeProvider
87+
* @param string $base Base Url
88+
* @param bool $secure Expected return value
89+
*/
90+
public function testShouldBeSecureUnsecureBaseUrl($base, $secure)
91+
{
92+
$this->scopeConfigMock->expects($this->once())
93+
->method('getValue')
94+
->with(Store::XML_PATH_UNSECURE_BASE_URL, ScopeInterface::SCOPE_STORE)
95+
->willReturn($base);
96+
$this->assertSame($secure, $this->model->shouldBeSecure('path/to/action'));
97+
}
98+
99+
/**
100+
* @dataProvider urlSchemeProvider
101+
* @param string $base Base Url
102+
* @param bool $secure Expected return value
103+
*/
104+
public function testShouldBeSecureSecureBaseUrl($base, $secure)
105+
{
106+
$path = 'path/to/action';
107+
108+
$this->scopeConfigMock->expects($this->once())->method('isSetFlag')
109+
->with(Store::XML_PATH_SECURE_IN_FRONTEND, ScopeInterface::SCOPE_STORE)
110+
->willReturn($secure);
111+
112+
$getValueReturnMap = [
113+
[Store::XML_PATH_SECURE_BASE_URL, ScopeInterface::SCOPE_STORE, null, $base],
114+
[Store::XML_PATH_UNSECURE_BASE_URL, ScopeInterface::SCOPE_STORE, null, 'http://unsecure.url'],
115+
];
116+
117+
$this->scopeConfigMock->expects($this->any())
118+
->method('getValue')
119+
->will($this->returnValueMap($getValueReturnMap));
120+
121+
if ($secure) {
122+
$this->urlSecurityInfoMock->expects($this->once())->method('isSecure')->with($path)->willReturn($secure);
123+
}
124+
125+
$this->assertSame($secure, $this->model->shouldBeSecure($path));
126+
}
127+
128+
/**
129+
* @return array
130+
*/
131+
public function urlSchemeProvider()
132+
{
133+
return [
134+
['https://base.url', true],
135+
['http://base.url', false]
136+
];
137+
}
138+
}

0 commit comments

Comments
 (0)