1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\StaticPublishQueue\Test; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Config\Config; |
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
7
|
|
|
use SilverStripe\StaticPublishQueue\Service\URLSanitisationService; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class URLSanitisationServiceTest |
11
|
|
|
* |
12
|
|
|
* @package SilverStripe\StaticPublishQueue\Test |
13
|
|
|
*/ |
14
|
|
|
class URLSanitisationServiceTest extends SapphireTest |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param bool $formatted |
18
|
|
|
* @param array $urls |
19
|
|
|
* @param array $expected |
20
|
|
|
* @dataProvider formattedUrlsProvider |
21
|
|
|
*/ |
22
|
|
|
public function testFormatting($formatted, array $urls, array $expected) |
23
|
|
|
{ |
24
|
|
|
$urlsService = URLSanitisationService::create(); |
25
|
|
|
$urlsService->addURLs($urls); |
26
|
|
|
$urls = $urlsService->getURLs($formatted); |
27
|
|
|
|
28
|
|
|
$this->assertEquals($expected, $urls); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param bool $ssl |
33
|
|
|
* @param array $urls |
34
|
|
|
* @param array $expected |
35
|
|
|
* @dataProvider transformationUrlsProvider |
36
|
|
|
*/ |
37
|
|
|
public function testTransformation($ssl, array $urls, array $expected) |
38
|
|
|
{ |
39
|
|
|
if ($ssl) { |
40
|
|
|
Config::modify()->set(URLSanitisationService::class, 'force_ssl', true); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// normal transformation (protocol change) |
44
|
|
|
$urlsService = URLSanitisationService::create(); |
45
|
|
|
$urlsService->addURLs($urls); |
46
|
|
|
$urls = $urlsService->getURLs(); |
47
|
|
|
|
48
|
|
|
$this->assertEquals($expected, $urls); |
49
|
|
|
|
50
|
|
|
$urlsService = URLSanitisationService::create(); |
51
|
|
|
$urlsService->addURLs($urls); |
52
|
|
|
$this->assertEquals($expected, $urlsService->getURLs()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function transformationUrlsProvider() |
56
|
|
|
{ |
57
|
|
|
return [ |
58
|
|
|
[ |
59
|
|
|
// protocol change |
60
|
|
|
false, |
61
|
|
|
[ |
62
|
|
|
'http://some-locale/some-page/', |
63
|
|
|
], |
64
|
|
|
[ |
65
|
|
|
'http://some-locale/some-page/', |
66
|
|
|
], |
67
|
|
|
], |
68
|
|
|
[ |
69
|
|
|
// protocol change |
70
|
|
|
true, |
71
|
|
|
[ |
72
|
|
|
'http://some-locale/some-page/', |
73
|
|
|
], |
74
|
|
|
[ |
75
|
|
|
'https://some-locale/some-page/', |
76
|
|
|
], |
77
|
|
|
], |
78
|
|
|
[ |
79
|
|
|
// removal of stage params |
80
|
|
|
false, |
81
|
|
|
[ |
82
|
|
|
'http://some-locale/some-page/?stage=Stage', |
83
|
|
|
'http://some-locale/some-page/?stage=Live', |
84
|
|
|
'https://some-locale/some-page/?stage=Live&deviceContext=mobile', |
85
|
|
|
], |
86
|
|
|
[ |
87
|
|
|
'http://some-locale/some-page/', |
88
|
|
|
'https://some-locale/some-page/?deviceContext=mobile', |
89
|
|
|
], |
90
|
|
|
], |
91
|
|
|
[ |
92
|
|
|
// removal of stage params |
93
|
|
|
true, |
94
|
|
|
[ |
95
|
|
|
'http://some-locale/some-page/?stage=Stage', |
96
|
|
|
'http://some-locale/some-page/?stage=Live', |
97
|
|
|
'https://some-locale/some-page/?stage=Live&deviceContext=mobile', |
98
|
|
|
], |
99
|
|
|
[ |
100
|
|
|
'https://some-locale/some-page/', |
101
|
|
|
'https://some-locale/some-page/?deviceContext=mobile', |
102
|
|
|
], |
103
|
|
|
], |
104
|
|
|
[ |
105
|
|
|
// enforce trailing slash |
106
|
|
|
false, |
107
|
|
|
[ |
108
|
|
|
'http://some-locale/some-page', |
109
|
|
|
], |
110
|
|
|
[ |
111
|
|
|
'http://some-locale/some-page/', |
112
|
|
|
], |
113
|
|
|
], |
114
|
|
|
[ |
115
|
|
|
// enforce trailing slash |
116
|
|
|
true, |
117
|
|
|
[ |
118
|
|
|
'http://some-locale/some-page', |
119
|
|
|
], |
120
|
|
|
[ |
121
|
|
|
'https://some-locale/some-page/', |
122
|
|
|
], |
123
|
|
|
], |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function formattedUrlsProvider() |
128
|
|
|
{ |
129
|
|
|
return [ |
130
|
|
|
[ |
131
|
|
|
false, |
132
|
|
|
[ |
133
|
|
|
'http://some-locale/some-page/', |
134
|
|
|
'http://some-locale/some-other-page/', |
135
|
|
|
], |
136
|
|
|
[ |
137
|
|
|
'http://some-locale/some-page/', |
138
|
|
|
'http://some-locale/some-other-page/', |
139
|
|
|
] |
140
|
|
|
], |
141
|
|
|
[ |
142
|
|
|
true, |
143
|
|
|
[ |
144
|
|
|
'http://some-locale/some-page/', |
145
|
|
|
'http://some-locale/some-other-page/', |
146
|
|
|
], |
147
|
|
|
[ |
148
|
|
|
'http://some-locale/some-page/' => 0, |
149
|
|
|
'http://some-locale/some-other-page/' => 1, |
150
|
|
|
], |
151
|
|
|
], |
152
|
|
|
]; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|