Skip to content

Commit 99c7f0f

Browse files
author
Bohdan Korablov
committed
MAGETWO-56317: [GitHub] -[2.1.0] underscore in site url breaks admin redirect - The page isn't redirecting properly #5809
1 parent 0dea6ad commit 99c7f0f

File tree

9 files changed

+215
-123
lines changed

9 files changed

+215
-123
lines changed

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

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

8-
use Magento\Framework\Url\SimpleValidator;
8+
use Magento\Framework\Validator\Url as UrlValidator;
99
use Magento\Framework\App\ObjectManager;
1010

1111
class Baseurl extends \Magento\Framework\App\Config\Value
@@ -16,9 +16,9 @@ class Baseurl extends \Magento\Framework\App\Config\Value
1616
protected $_mergeService;
1717

1818
/**
19-
* @var SimpleValidator
19+
* @var UrlValidator
2020
*/
21-
private $simpleUrlValidator;
21+
private $urlValidator;
2222

2323
/**
2424
* @param \Magento\Framework\Model\Context $context
@@ -201,7 +201,7 @@ private function _validateFullyQualifiedUrl($value)
201201
*/
202202
private function _isFullyQualifiedUrl($value)
203203
{
204-
return preg_match('/\/$/', $value) && $this->getSimpleUrlValidator()->isValid($value);
204+
return preg_match('/\/$/', $value) && $this->getUrlValidator()->isValid($value, ['http', 'https']);
205205
}
206206

207207
/**
@@ -225,16 +225,16 @@ public function afterSave()
225225
}
226226

227227
/**
228-
* Get Simple URL Validator
228+
* Get URL Validator
229229
*
230230
* @deprecated
231-
* @return SimpleValidator
231+
* @return UrlValidator
232232
*/
233-
private function getSimpleUrlValidator()
233+
private function getUrlValidator()
234234
{
235-
if (!$this->simpleUrlValidator) {
236-
$this->simpleUrlValidator = ObjectManager::getInstance()->get(SimpleValidator::class);
235+
if (!$this->urlValidator) {
236+
$this->urlValidator = ObjectManager::getInstance()->get(UrlValidator::class);
237237
}
238-
return $this->simpleUrlValidator;
238+
return $this->urlValidator;
239239
}
240240
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Setup\Controller;
7+
8+
use Magento\TestFramework\Helper\Bootstrap;
9+
use Zend\Stdlib\RequestInterface as Request;
10+
use Zend\View\Model\JsonModel;
11+
12+
class UrlCheckTest extends \PHPUnit_Framework_TestCase
13+
{
14+
/**
15+
* @var UrlCheck
16+
*/
17+
private $controller;
18+
19+
protected function setUp()
20+
{
21+
$this->controller = Bootstrap::getObjectManager()->create(UrlCheck::class);
22+
}
23+
24+
/**
25+
* @param array $requestContent
26+
* @param bool $successUrl
27+
* @param bool $successSecureUrl
28+
* @return void
29+
* @dataProvider indexActionDataProvider
30+
*/
31+
public function testIndexAction($requestContent, $successUrl, $successSecureUrl)
32+
{
33+
$requestMock = $this->getMockBuilder(Request::class)
34+
->getMockForAbstractClass();
35+
$requestMock->expects($this->once())
36+
->method('getContent')
37+
->willReturn(json_encode($requestContent));
38+
39+
$requestProperty = new \ReflectionProperty(get_class($this->controller), 'request');
40+
$requestProperty->setAccessible(true);
41+
$requestProperty->setValue($this->controller, $requestMock);
42+
43+
$resultModel = new JsonModel(['successUrl' => $successUrl, 'successSecureUrl' => $successSecureUrl]);
44+
45+
$this->assertEquals($resultModel, $this->controller->indexAction());
46+
}
47+
48+
/**
49+
* @return array
50+
*/
51+
public function indexActionDataProvider()
52+
{
53+
return [
54+
[
55+
'requestContent' => [
56+
'address' => [
57+
'actual_base_url' => 'http://example.com/'
58+
],
59+
'https' => [
60+
'text' => 'https://example.com/',
61+
'admin' => true,
62+
'front' => false
63+
],
64+
],
65+
'successUrl' => true,
66+
'successSecureUrl' => true
67+
],
68+
[
69+
'requestContent' => [
70+
'address' => [
71+
'actual_base_url' => 'http://example.com/folder/'
72+
],
73+
'https' => [
74+
'text' => 'https://example.com/folder_name/',
75+
'admin' => false,
76+
'front' => true
77+
],
78+
],
79+
'successUrl' => true,
80+
'successSecureUrl' => true
81+
],
82+
[
83+
'requestContent' => [
84+
'address' => [
85+
'actual_base_url' => 'ftp://example.com/'
86+
],
87+
'https' => [
88+
'text' => 'https://example.com_test/',
89+
'admin' => true,
90+
'front' => true
91+
],
92+
],
93+
'successUrl' => false,
94+
'successSecureUrl' => false
95+
],
96+
];
97+
}
98+
}

lib/internal/Magento/Framework/Url/SimpleValidator.php

Lines changed: 0 additions & 63 deletions
This file was deleted.

lib/internal/Magento/Framework/Url/Test/Unit/SimpleValidatorTest.php renamed to lib/internal/Magento/Framework/Validator/Test/Unit/UrlTest.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
* Copyright © 2016 Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
namespace Magento\Framework\Url\Test\Unit;
6+
namespace Magento\Framework\Validator\Test\Unit;
77

8-
use Magento\Framework\Url\SimpleValidator;
8+
use Magento\Framework\Validator\Url as UrlValidator;
99

10-
class SimpleValidatorTest extends \PHPUnit_Framework_TestCase
10+
class UrlTest extends \PHPUnit_Framework_TestCase
1111
{
1212
/**
13-
* @var SimpleValidator
13+
* @var UrlValidator
1414
*/
1515
private $validator;
1616

1717
protected function setUp()
1818
{
19-
$this->validator = new SimpleValidator();
19+
$this->validator = new UrlValidator();
2020
}
2121

2222
/**
@@ -27,8 +27,7 @@ protected function setUp()
2727
*/
2828
public function testIsValid(array $allowedSchemes, $url, $expectedResult)
2929
{
30-
$this->validator->setAllowedSchemes($allowedSchemes);
31-
$this->assertSame($expectedResult, $this->validator->isValid($url));
30+
$this->assertSame($expectedResult, $this->validator->isValid($url, $allowedSchemes));
3231
}
3332

3433
/**
@@ -42,29 +41,34 @@ public function isValidDataProvider()
4241
'url' => 'http://example.com',
4342
'expectedResult' => true,
4443
],
44+
[
45+
'allowedSchemes' => ['http'],
46+
'url' => 'http://example.com',
47+
'expectedResult' => true,
48+
],
4549
[
4650
'allowedSchemes' => [],
4751
'url' => 'https://example.com',
4852
'expectedResult' => true,
4953
],
5054
[
51-
'allowedSchemes' => [],
52-
'url' => 'http://example.com_test',
53-
'expectedResult' => false,
55+
'allowedSchemes' => ['https'],
56+
'url' => 'https://example.com',
57+
'expectedResult' => true,
5458
],
5559
[
5660
'allowedSchemes' => [],
57-
'url' => 'ftp://example.com',
61+
'url' => 'http://example.com_test',
5862
'expectedResult' => false,
5963
],
6064
[
61-
'allowedSchemes' => ['ftp'],
65+
'allowedSchemes' => [],
6266
'url' => 'ftp://example.com',
6367
'expectedResult' => true,
6468
],
6569
[
6670
'allowedSchemes' => ['ftp'],
67-
'url' => 'ftp://example.com_test',
71+
'url' => 'ftp://example.com',
6872
'expectedResult' => true,
6973
],
7074
];
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Validator;
7+
8+
/**
9+
* Class Url validates URL and checks that it has allowed scheme
10+
*/
11+
class Url
12+
{
13+
/**
14+
* Validate URL and check that it has allowed scheme
15+
*
16+
* @param string $value
17+
* @param array $allowedSchemes
18+
* @return bool
19+
*/
20+
public function isValid($value, array $allowedSchemes = [])
21+
{
22+
$isValid = true;
23+
24+
if (!filter_var($value, FILTER_VALIDATE_URL)) {
25+
$isValid = false;
26+
}
27+
28+
if (!empty($allowedSchemes)) {
29+
$url = parse_url($value);
30+
if (empty($url['scheme']) || !in_array($url['scheme'], $allowedSchemes)) {
31+
$isValid = false;
32+
}
33+
}
34+
35+
return $isValid;
36+
}
37+
}

0 commit comments

Comments
 (0)