1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* @copyright 2019 Mautic Inc. All rights reserved |
5
|
|
|
* @author Mautic, Inc. |
6
|
|
|
* |
7
|
|
|
* @link https://www.mautic.com |
8
|
|
|
* |
9
|
|
|
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Mautic\CoreBundle\Tests\Helper; |
13
|
|
|
|
14
|
|
|
use Mautic\CoreBundle\Helper\CoreParametersHelper; |
15
|
|
|
use Mautic\CoreBundle\Helper\TrailingSlashHelper; |
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
17
|
|
|
|
18
|
|
|
class TrailingSlashHelperTest extends \PHPUnit_Framework_TestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var CoreParametersHelper|\PHPUnit_Framework_MockObject_MockObject |
22
|
|
|
*/ |
23
|
|
|
private $coreParametersHelper; |
24
|
|
|
|
25
|
|
|
protected function setUp() |
26
|
|
|
{ |
27
|
|
|
$this->coreParametersHelper = $this->createMock(CoreParametersHelper::class); |
28
|
|
|
$this->coreParametersHelper->method('getParameter') |
29
|
|
|
->with('site_url') |
30
|
|
|
->willReturn('https://test.com'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testOpenRedirectIsNotPossible() |
34
|
|
|
{ |
35
|
|
|
$server = [ |
36
|
|
|
'HTTP_HOST' => 'test.com', |
37
|
|
|
'HTTP_USER_AGENT' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.56 Safari/537.36', |
38
|
|
|
'SERVER_NAME' => 'test.com', |
39
|
|
|
'SERVER_ADDR' => '::1', |
40
|
|
|
'SERVER_PORT' => '80', |
41
|
|
|
'REMOTE_ADDR' => '::1', |
42
|
|
|
'DOCUMENT_ROOT' => null, |
43
|
|
|
'REQUEST_SCHEME' => 'http', |
44
|
|
|
'REMOTE_PORT' => '80', |
45
|
|
|
'REDIRECT_URL' => '/google.com/', |
46
|
|
|
'SERVER_PROTOCOL' => 'HTTP/1.1', |
47
|
|
|
'REQUEST_METHOD' => 'GET', |
48
|
|
|
'QUERY_STRING' => '', |
49
|
|
|
'REQUEST_URI' => '//google.com/', |
50
|
|
|
'SCRIPT_NAME' => '/index.php', |
51
|
|
|
'PHP_SELF' => '/index.php', |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
$request = new Request([], [], [], [], [], $server); |
55
|
|
|
|
56
|
|
|
// google.com should not be returned as the URL |
57
|
|
|
$this->assertEquals('https://test.com//google.com', $this->getHelper()->getSafeRedirectUrl($request)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testMauticUrlWithTrailingSlashIsGeneratedCorrectly() |
61
|
|
|
{ |
62
|
|
|
$server = [ |
63
|
|
|
'HTTP_HOST' => 'test.com', |
64
|
|
|
'HTTP_USER_AGENT' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.56 Safari/537.36', |
65
|
|
|
'SERVER_NAME' => 'test.com', |
66
|
|
|
'SERVER_ADDR' => '::1', |
67
|
|
|
'SERVER_PORT' => '80', |
68
|
|
|
'REMOTE_ADDR' => '::1', |
69
|
|
|
'DOCUMENT_ROOT' => null, |
70
|
|
|
'REQUEST_SCHEME' => 'http', |
71
|
|
|
'REMOTE_PORT' => '80', |
72
|
|
|
'REDIRECT_URL' => '/s/dashboard/', |
73
|
|
|
'SERVER_PROTOCOL' => 'HTTP/1.1', |
74
|
|
|
'REQUEST_METHOD' => 'GET', |
75
|
|
|
'QUERY_STRING' => '', |
76
|
|
|
'REQUEST_URI' => '/s/dashboard/', |
77
|
|
|
'SCRIPT_NAME' => '/index.php', |
78
|
|
|
'PHP_SELF' => '/index.php', |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
$request = new Request([], [], [], [], [], $server); |
82
|
|
|
|
83
|
|
|
// google.com should not be returned as the URL |
84
|
|
|
$this->assertEquals('https://test.com/s/dashboard', $this->getHelper()->getSafeRedirectUrl($request)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return TrailingSlashHelper |
89
|
|
|
*/ |
90
|
|
|
private function getHelper() |
91
|
|
|
{ |
92
|
|
|
return new TrailingSlashHelper($this->coreParametersHelper); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|