1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\StaticPublishQueue\Test; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Assets\Filesystem; |
6
|
|
|
use SilverStripe\CMS\Model\RedirectorPage; |
7
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
8
|
|
|
use SilverStripe\Control\Director; |
9
|
|
|
use SilverStripe\Core\Config\Config; |
10
|
|
|
use SilverStripe\Dev\SapphireTest; |
11
|
|
|
use SilverStripe\StaticPublishQueue\Extension\Publishable\PublishableSiteTree; |
12
|
|
|
use SilverStripe\StaticPublishQueue\Publisher\FilesystemPublisher; |
13
|
|
|
use SilverStripe\StaticPublishQueue\Test\StaticPublisherTest\Model\StaticPublisherTestPage; |
14
|
|
|
use SilverStripe\View\SSViewer; |
15
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJobService; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Tests for the {@link FilesystemPublisher} class. |
19
|
|
|
* |
20
|
|
|
* @package staticpublishqueue |
21
|
|
|
*/ |
22
|
|
|
class FilesystemPublisherTest extends SapphireTest |
23
|
|
|
{ |
24
|
|
|
protected $usesDatabase = true; |
25
|
|
|
|
26
|
|
|
protected function setUp() |
27
|
|
|
{ |
28
|
|
|
parent::setUp(); |
29
|
|
|
|
30
|
|
|
SiteTree::add_extension(PublishableSiteTree::class); |
31
|
|
|
|
32
|
|
|
Config::modify()->set(FilesystemPublisher::class, 'domain_based_caching', false); |
33
|
|
|
Config::modify()->set(Director::class, 'alternate_base_url', 'http://foo/'); |
34
|
|
|
Config::modify()->set(QueuedJobService::class, 'use_shutdown_function', false); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function tearDown() |
38
|
|
|
{ |
39
|
|
|
SiteTree::remove_extension(PublishableSiteTree::class); |
40
|
|
|
|
41
|
|
|
parent::tearDown(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testUrlToPathWithRelativeUrls() |
45
|
|
|
{ |
46
|
|
|
$reflection = new \ReflectionClass(FilesystemPublisher::class); |
47
|
|
|
$urlToPath = $reflection->getMethod('URLtoPath'); |
48
|
|
|
$urlToPath->setAccessible(true); |
49
|
|
|
|
50
|
|
|
$fsp = FilesystemPublisher::create(); |
51
|
|
|
|
52
|
|
|
$this->assertEquals( |
53
|
|
|
'index', |
54
|
|
|
$urlToPath->invokeArgs($fsp, ['/']) |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
$this->assertEquals( |
58
|
|
|
'about-us', |
59
|
|
|
$urlToPath->invokeArgs($fsp, ['about-us']) |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
$this->assertEquals( |
63
|
|
|
'parent/child', |
64
|
|
|
$urlToPath->invokeArgs($fsp, ['parent/child']) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testUrlToPathWithAbsoluteUrls() |
69
|
|
|
{ |
70
|
|
|
$reflection = new \ReflectionClass(FilesystemPublisher::class); |
71
|
|
|
$urlToPath = $reflection->getMethod('URLtoPath'); |
72
|
|
|
$urlToPath->setAccessible(true); |
73
|
|
|
|
74
|
|
|
$fsp = FilesystemPublisher::create(); |
75
|
|
|
|
76
|
|
|
$url = Director::absoluteBaseUrl(); |
77
|
|
|
$this->assertEquals( |
78
|
|
|
'index', |
79
|
|
|
$urlToPath->invokeArgs($fsp, [$url]) |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$url = Director::absoluteBaseUrl() . 'about-us'; |
83
|
|
|
$this->assertEquals( |
84
|
|
|
'about-us', |
85
|
|
|
$urlToPath->invokeArgs($fsp, [$url]) |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$url = Director::absoluteBaseUrl() . 'parent/child'; |
89
|
|
|
$this->assertEquals( |
90
|
|
|
'parent/child', |
91
|
|
|
$urlToPath->invokeArgs($fsp, [$url]) |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testUrlToPathWithDomainBasedCaching() |
96
|
|
|
{ |
97
|
|
|
Config::modify()->set(FilesystemPublisher::class, 'domain_based_caching', true); |
98
|
|
|
|
99
|
|
|
$reflection = new \ReflectionClass(FilesystemPublisher::class); |
100
|
|
|
$urlToPath = $reflection->getMethod('URLtoPath'); |
101
|
|
|
$urlToPath->setAccessible(true); |
102
|
|
|
|
103
|
|
|
$fsp = FilesystemPublisher::create()->setFileExtension('html'); |
104
|
|
|
|
105
|
|
|
$url = 'http://domain1.com/'; |
106
|
|
|
$this->assertEquals( |
107
|
|
|
'domain1.com/index', |
108
|
|
|
$urlToPath->invokeArgs($fsp, [$url]) |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$url = 'http://domain1.com/about-us'; |
112
|
|
|
$this->assertEquals( |
113
|
|
|
'domain1.com/about-us', |
114
|
|
|
$urlToPath->invokeArgs($fsp, [$url]) |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
$url = 'http://domain2.com/parent/child'; |
118
|
|
|
$this->assertEquals( |
119
|
|
|
'domain2.com/parent/child', |
120
|
|
|
$urlToPath->invokeArgs($fsp, [$url]) |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testMenu2LinkingMode() |
125
|
|
|
{ |
126
|
|
|
$this->logInWithPermission('ADMIN'); |
127
|
|
|
|
128
|
|
|
SSViewer::set_themes(null); |
|
|
|
|
129
|
|
|
|
130
|
|
|
$reflection = new \ReflectionClass(FilesystemPublisher::class); |
131
|
|
|
$urlToPath = $reflection->getMethod('URLtoPath'); |
132
|
|
|
$urlToPath->setAccessible(true); |
133
|
|
|
|
134
|
|
|
$fsp = FilesystemPublisher::create() |
135
|
|
|
->setDestFolder('cache/testing/'); |
136
|
|
|
|
137
|
|
|
$level1 = StaticPublisherTestPage::create(); |
138
|
|
|
$level1->URLSegment = 'test-level-1'; |
139
|
|
|
$level1->write(); |
140
|
|
|
$level1->publishRecursive(); |
141
|
|
|
|
142
|
|
|
$level2_1 = StaticPublisherTestPage::create(); |
143
|
|
|
$level2_1->URLSegment = 'test-level-2-1'; |
144
|
|
|
$level2_1->ParentID = $level1->ID; |
145
|
|
|
$level2_1->write(); |
146
|
|
|
$level2_1->publishRecursive(); |
147
|
|
|
|
148
|
|
|
$fsp->publishURL($level1->Link(), true); |
149
|
|
|
$fsp->publishURL($level2_1->Link(), true); |
150
|
|
|
$static2_1FilePath = $fsp->getDestPath().$urlToPath->invokeArgs($fsp, [$level2_1->Link()]); |
151
|
|
|
|
152
|
|
|
$this->assertFileExists($static2_1FilePath.'.html'); |
153
|
|
|
$this->assertFileExists($static2_1FilePath.'.php'); |
154
|
|
|
$this->assertContains( |
155
|
|
|
'current', |
156
|
|
|
file_get_contents($static2_1FilePath.'.html') |
157
|
|
|
); |
158
|
|
|
|
159
|
|
|
$level2_2 = StaticPublisherTestPage::create(); |
160
|
|
|
$level2_2->URLSegment = 'test-level-2-2'; |
161
|
|
|
$level2_2->ParentID = $level1->ID; |
162
|
|
|
$level2_2->write(); |
163
|
|
|
$level2_2->publishRecursive(); |
164
|
|
|
|
165
|
|
|
$fsp->publishURL($level2_2->Link(), true); |
166
|
|
|
$static2_2FilePath = $fsp->getDestPath().$urlToPath->invokeArgs($fsp, [$level2_2->Link()]); |
167
|
|
|
|
168
|
|
|
$this->assertFileExists($static2_2FilePath.'.html'); |
169
|
|
|
$this->assertFileExists($static2_2FilePath.'.php'); |
170
|
|
|
$this->assertContains( |
171
|
|
|
'linkcurrent', |
172
|
|
|
file_get_contents($static2_2FilePath.'.html') |
173
|
|
|
); |
174
|
|
|
|
175
|
|
|
if (file_exists($fsp->getDestPath())) { |
176
|
|
|
Filesystem::removeFolder($fsp->getDestPath()); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function testOnlyHTML() |
181
|
|
|
{ |
182
|
|
|
$this->logInWithPermission('ADMIN'); |
183
|
|
|
|
184
|
|
|
$fsp = FilesystemPublisher::create() |
185
|
|
|
->setFileExtension('html') |
186
|
|
|
->setDestFolder('cache/testing/'); |
187
|
|
|
$level1 = StaticPublisherTestPage::create(); |
188
|
|
|
$level1->URLSegment = 'mimetype'; |
189
|
|
|
$level1->write(); |
190
|
|
|
$level1->publishRecursive(); |
191
|
|
|
|
192
|
|
|
$fsp->publishURL($level1->Link(), true); |
193
|
|
|
$staticFilePath = $fsp->getDestPath().'mimetype'; |
194
|
|
|
|
195
|
|
|
$this->assertFileExists($staticFilePath.'.html'); |
196
|
|
|
$this->assertFileNotExists($staticFilePath.'.php'); |
197
|
|
|
$this->assertEquals( |
198
|
|
|
"<div class=\"statically-published\" style=\"display: none\"></div>", |
199
|
|
|
trim(file_get_contents($staticFilePath.'.html')) |
200
|
|
|
); |
201
|
|
|
if (file_exists($fsp->getDestPath())) { |
202
|
|
|
Filesystem::removeFolder($fsp->getDestPath()); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function testNoErrorPagesWhenHTMLOnly() |
207
|
|
|
{ |
208
|
|
|
$this->logInWithPermission('ADMIN'); |
209
|
|
|
|
210
|
|
|
$fsp = FilesystemPublisher::create() |
211
|
|
|
->setFileExtension('html') |
212
|
|
|
->setDestFolder('cache/testing/'); |
213
|
|
|
$fsp->publishURL('not_really_there', true); |
214
|
|
|
$this->assertFileNotExists($fsp->getDestPath() . 'not_really_there.html'); |
215
|
|
|
$this->assertFileNotExists($fsp->getDestPath() . 'not_really_there.php'); |
216
|
|
|
|
217
|
|
|
if (file_exists($fsp->getDestPath())) { |
218
|
|
|
Filesystem::removeFolder($fsp->getDestPath()); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function testErrorPageWhenPHP() |
223
|
|
|
{ |
224
|
|
|
$this->logInWithPermission('ADMIN'); |
225
|
|
|
|
226
|
|
|
$fsp = FilesystemPublisher::create() |
227
|
|
|
->setDestFolder('cache/testing/'); |
228
|
|
|
$fsp->publishURL('not_really_there', true); |
229
|
|
|
$this->assertFileExists($fsp->getDestPath() . 'not_really_there.html'); |
230
|
|
|
$this->assertFileExists($fsp->getDestPath() . 'not_really_there.php'); |
231
|
|
|
$phpCacheConfig = require $fsp->getDestPath() . 'not_really_there.php'; |
232
|
|
|
$this->assertEquals(404, $phpCacheConfig['responseCode']); |
233
|
|
|
|
234
|
|
|
if (file_exists($fsp->getDestPath())) { |
235
|
|
|
Filesystem::removeFolder($fsp->getDestPath()); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function testRedirectorPageWhenPHP() |
240
|
|
|
{ |
241
|
|
|
$this->logInWithPermission('ADMIN'); |
242
|
|
|
|
243
|
|
|
$fsp = FilesystemPublisher::create() |
244
|
|
|
->setDestFolder('cache/testing/'); |
245
|
|
|
$redirectorPage = RedirectorPage::create(); |
246
|
|
|
$redirectorPage->URLSegment = 'somewhere-else'; |
247
|
|
|
$redirectorPage->RedirectionType = 'External'; |
248
|
|
|
$redirectorPage->ExternalURL = 'silverstripe.org'; |
249
|
|
|
$redirectorPage->write(); |
250
|
|
|
$redirectorPage->publishRecursive(); |
251
|
|
|
|
252
|
|
|
$fsp->publishURL('somewhere-else', true); |
253
|
|
|
|
254
|
|
|
$this->assertFileExists($fsp->getDestPath() . 'somewhere-else.html'); |
255
|
|
|
$this->assertContains( |
256
|
|
|
'Click this link if your browser does not redirect you', |
257
|
|
|
file_get_contents($fsp->getDestPath() . 'somewhere-else.html') |
258
|
|
|
); |
259
|
|
|
$this->assertFileExists($fsp->getDestPath() . 'somewhere-else.php'); |
260
|
|
|
$phpCacheConfig = require $fsp->getDestPath() . 'somewhere-else.php'; |
261
|
|
|
$this->assertEquals(301, $phpCacheConfig['responseCode']); |
262
|
|
|
$this->assertContains('location: http://silverstripe.org', $phpCacheConfig['headers']); |
263
|
|
|
|
264
|
|
|
if (file_exists($fsp->getDestPath())) { |
265
|
|
|
Filesystem::removeFolder($fsp->getDestPath()); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function testRedirectorPageWhenHTMLOnly() |
270
|
|
|
{ |
271
|
|
|
$this->logInWithPermission('ADMIN'); |
272
|
|
|
|
273
|
|
|
$fsp = FilesystemPublisher::create() |
274
|
|
|
->setFileExtension('html') |
275
|
|
|
->setDestFolder('cache/testing/'); |
276
|
|
|
|
277
|
|
|
$redirectorPage = RedirectorPage::create(); |
278
|
|
|
$redirectorPage->URLSegment = 'somewhere-else'; |
279
|
|
|
$redirectorPage->RedirectionType = 'External'; |
280
|
|
|
$redirectorPage->ExternalURL = 'silverstripe.org'; |
281
|
|
|
$redirectorPage->write(); |
282
|
|
|
$redirectorPage->publishRecursive(); |
283
|
|
|
|
284
|
|
|
$fsp->publishURL('somewhere-else', true); |
285
|
|
|
|
286
|
|
|
$this->assertFileExists($fsp->getDestPath() . 'somewhere-else.html'); |
287
|
|
|
$this->assertContains( |
288
|
|
|
'Click this link if your browser does not redirect you', |
289
|
|
|
file_get_contents($fsp->getDestPath() . 'somewhere-else.html') |
290
|
|
|
); |
291
|
|
|
$this->assertFileNotExists($fsp->getDestPath() . 'somewhere-else.php'); |
292
|
|
|
|
293
|
|
|
if (file_exists($fsp->getDestPath())) { |
294
|
|
|
Filesystem::removeFolder($fsp->getDestPath()); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: