1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\StaticPublishQueue\Test\Publisher; |
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\Control\HTTPApplication; |
10
|
|
|
use SilverStripe\Core\Config\Config; |
11
|
|
|
use SilverStripe\Dev\SapphireTest; |
12
|
|
|
use SilverStripe\Dev\TestKernel; |
13
|
|
|
use SilverStripe\StaticPublishQueue\Extension\Publishable\PublishableSiteTree; |
14
|
|
|
use SilverStripe\StaticPublishQueue\Publisher\FilesystemPublisher; |
15
|
|
|
use SilverStripe\StaticPublishQueue\Test\StaticPublisherTest\Model\StaticPublisherTestPage; |
16
|
|
|
use SilverStripe\View\SSViewer; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Tests for the {@link FilesystemPublisher} class. |
20
|
|
|
* |
21
|
|
|
* @package staticpublishqueue |
22
|
|
|
*/ |
23
|
|
|
class FilesystemPublisherTest extends SapphireTest |
24
|
|
|
{ |
25
|
|
|
protected $usesDatabase = true; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var FilesystemPublisher |
29
|
|
|
*/ |
30
|
|
|
private $fsp = null; |
31
|
|
|
|
32
|
|
|
protected static $required_extensions = [ |
33
|
|
|
SiteTree::class => [ |
34
|
|
|
PublishableSiteTree::class, |
35
|
|
|
], |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
protected function setUp() |
39
|
|
|
{ |
40
|
|
|
parent::setUp(); |
41
|
|
|
|
42
|
|
|
Config::modify()->set(FilesystemPublisher::class, 'domain_based_caching', false); |
43
|
|
|
Config::modify()->set(Director::class, 'alternate_base_url', 'http://example.com/'); |
44
|
|
|
|
45
|
|
|
$mockFSP = $this->getMockBuilder(FilesystemPublisher::class)->setMethods([ |
46
|
|
|
'getHTTPApplication', |
47
|
|
|
])->getMock(); |
48
|
|
|
|
49
|
|
|
$mockFSP->method('getHTTPApplication')->willReturnCallback(function () { |
50
|
|
|
return new HTTPApplication(new TestKernel(BASE_PATH)); |
51
|
|
|
}); |
52
|
|
|
|
53
|
|
|
$this->fsp = $mockFSP->setDestFolder('cache/testing/'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function tearDown() |
57
|
|
|
{ |
58
|
|
|
if ($this->fsp !== null && file_exists($this->fsp->getDestPath())) { |
59
|
|
|
Filesystem::removeFolder($this->fsp->getDestPath()); |
60
|
|
|
} |
61
|
|
|
parent::tearDown(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testUrlToPathWithRelativeUrls() |
65
|
|
|
{ |
66
|
|
|
$reflection = new \ReflectionClass(FilesystemPublisher::class); |
67
|
|
|
$urlToPath = $reflection->getMethod('URLtoPath'); |
68
|
|
|
$urlToPath->setAccessible(true); |
69
|
|
|
|
70
|
|
|
$this->assertSame( |
71
|
|
|
'index', |
72
|
|
|
$urlToPath->invokeArgs($this->fsp, ['/']) |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
$this->assertSame( |
76
|
|
|
'about-us', |
77
|
|
|
$urlToPath->invokeArgs($this->fsp, ['about-us']) |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
$this->assertSame( |
81
|
|
|
'parent/child', |
82
|
|
|
$urlToPath->invokeArgs($this->fsp, ['parent/child']) |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testUrlToPathWithAbsoluteUrls() |
87
|
|
|
{ |
88
|
|
|
$reflection = new \ReflectionClass(FilesystemPublisher::class); |
89
|
|
|
$urlToPath = $reflection->getMethod('URLtoPath'); |
90
|
|
|
$urlToPath->setAccessible(true); |
91
|
|
|
|
92
|
|
|
$url = Director::absoluteBaseUrl(); |
93
|
|
|
$this->assertSame( |
94
|
|
|
'index', |
95
|
|
|
$urlToPath->invokeArgs($this->fsp, [$url]) |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
$url = Director::absoluteBaseUrl() . 'about-us'; |
99
|
|
|
$this->assertSame( |
100
|
|
|
'about-us', |
101
|
|
|
$urlToPath->invokeArgs($this->fsp, [$url]) |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
$url = Director::absoluteBaseUrl() . 'parent/child'; |
105
|
|
|
$this->assertSame( |
106
|
|
|
'parent/child', |
107
|
|
|
$urlToPath->invokeArgs($this->fsp, [$url]) |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testUrlToPathWithDomainBasedCaching() |
112
|
|
|
{ |
113
|
|
|
Config::modify()->set(FilesystemPublisher::class, 'domain_based_caching', true); |
114
|
|
|
|
115
|
|
|
$reflection = new \ReflectionClass(FilesystemPublisher::class); |
116
|
|
|
$urlToPath = $reflection->getMethod('URLtoPath'); |
117
|
|
|
$urlToPath->setAccessible(true); |
118
|
|
|
|
119
|
|
|
$this->fsp->setFileExtension('html'); |
120
|
|
|
|
121
|
|
|
$url = 'http://domain1.com/'; |
122
|
|
|
$this->assertSame( |
123
|
|
|
'domain1.com/index', |
124
|
|
|
$urlToPath->invokeArgs($this->fsp, [$url]) |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
$url = 'http://domain1.com/about-us'; |
128
|
|
|
$this->assertSame( |
129
|
|
|
'domain1.com/about-us', |
130
|
|
|
$urlToPath->invokeArgs($this->fsp, [$url]) |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
$url = 'http://domain2.com/parent/child'; |
134
|
|
|
$this->assertSame( |
135
|
|
|
'domain2.com/parent/child', |
136
|
|
|
$urlToPath->invokeArgs($this->fsp, [$url]) |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function testMenu2LinkingMode() |
141
|
|
|
{ |
142
|
|
|
SSViewer::set_themes(null); |
143
|
|
|
|
144
|
|
|
$reflection = new \ReflectionClass(FilesystemPublisher::class); |
145
|
|
|
$urlToPath = $reflection->getMethod('URLtoPath'); |
146
|
|
|
$urlToPath->setAccessible(true); |
147
|
|
|
|
148
|
|
|
$level1 = new StaticPublisherTestPage(); |
149
|
|
|
$level1->URLSegment = 'test-level-1'; |
150
|
|
|
$level1->write(); |
151
|
|
|
$level1->publishRecursive(); |
152
|
|
|
|
153
|
|
|
$level2_1 = new StaticPublisherTestPage(); |
154
|
|
|
$level2_1->URLSegment = 'test-level-2-1'; |
155
|
|
|
$level2_1->ParentID = $level1->ID; |
156
|
|
|
$level2_1->write(); |
157
|
|
|
$level2_1->publishRecursive(); |
158
|
|
|
|
159
|
|
|
$this->fsp->publishURL($level1->Link(), true); |
160
|
|
|
$this->fsp->publishURL($level2_1->Link(), true); |
161
|
|
|
$static2_1FilePath = $this->fsp->getDestPath() . $urlToPath->invokeArgs($this->fsp, [$level2_1->Link()]); |
162
|
|
|
|
163
|
|
|
$this->assertFileExists($static2_1FilePath . '.html'); |
164
|
|
|
$this->assertFileExists($static2_1FilePath . '.php'); |
165
|
|
|
$this->assertContains( |
166
|
|
|
'current', |
167
|
|
|
file_get_contents($static2_1FilePath . '.html') |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
$level2_2 = new StaticPublisherTestPage(); |
171
|
|
|
$level2_2->URLSegment = 'test-level-2-2'; |
172
|
|
|
$level2_2->ParentID = $level1->ID; |
173
|
|
|
$level2_2->write(); |
174
|
|
|
$level2_2->publishRecursive(); |
175
|
|
|
|
176
|
|
|
$this->fsp->publishURL($level2_2->Link(), true); |
177
|
|
|
$static2_2FilePath = $this->fsp->getDestPath() . $urlToPath->invokeArgs($this->fsp, [$level2_2->Link()]); |
178
|
|
|
|
179
|
|
|
$this->assertFileExists($static2_2FilePath . '.html'); |
180
|
|
|
$this->assertFileExists($static2_2FilePath . '.php'); |
181
|
|
|
$this->assertContains( |
182
|
|
|
'linkcurrent', |
183
|
|
|
file_get_contents($static2_2FilePath . '.html') |
184
|
|
|
); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function testOnlyHTML() |
188
|
|
|
{ |
189
|
|
|
$this->fsp->setFileExtension('html'); |
190
|
|
|
|
191
|
|
|
$level1 = new StaticPublisherTestPage(); |
192
|
|
|
$level1->URLSegment = 'mimetype'; |
193
|
|
|
$level1->write(); |
194
|
|
|
$level1->publishRecursive(); |
195
|
|
|
|
196
|
|
|
$this->fsp->publishURL($level1->Link(), true); |
197
|
|
|
$staticFilePath = $this->fsp->getDestPath() . 'mimetype'; |
198
|
|
|
|
199
|
|
|
$this->assertFileExists($staticFilePath . '.html'); |
200
|
|
|
$this->assertFileNotExists($staticFilePath . '.php'); |
201
|
|
|
$this->assertSame( |
202
|
|
|
'<div class="statically-published" style="display: none"></div>', |
203
|
|
|
trim(file_get_contents($staticFilePath . '.html')) |
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function testPurgeURL() |
208
|
|
|
{ |
209
|
|
|
$level1 = new StaticPublisherTestPage(); |
210
|
|
|
$level1->URLSegment = 'to-be-purged'; |
211
|
|
|
$level1->write(); |
212
|
|
|
$level1->publishRecursive(); |
213
|
|
|
|
214
|
|
|
$this->fsp->publishURL('to-be-purged', true); |
215
|
|
|
$this->assertFileExists($this->fsp->getDestPath() . 'to-be-purged.html'); |
216
|
|
|
$this->assertFileExists($this->fsp->getDestPath() . 'to-be-purged.php'); |
217
|
|
|
|
218
|
|
|
$this->fsp->purgeURL('to-be-purged'); |
219
|
|
|
$this->assertFileNotExists($this->fsp->getDestPath() . 'to-be-purged.html'); |
220
|
|
|
$this->assertFileNotExists($this->fsp->getDestPath() . 'to-be-purged.php'); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function testPurgeURLAfterSwitchingExtensions() |
224
|
|
|
{ |
225
|
|
|
$level1 = new StaticPublisherTestPage(); |
226
|
|
|
$level1->URLSegment = 'purge-me'; |
227
|
|
|
$level1->write(); |
228
|
|
|
$level1->publishRecursive(); |
229
|
|
|
|
230
|
|
|
$this->fsp->publishURL('purge-me', true); |
231
|
|
|
$this->assertFileExists($this->fsp->getDestPath() . 'purge-me.html'); |
232
|
|
|
$this->assertFileExists($this->fsp->getDestPath() . 'purge-me.php'); |
233
|
|
|
|
234
|
|
|
$this->fsp->setFileExtension('html'); |
235
|
|
|
|
236
|
|
|
$this->fsp->purgeURL('purge-me'); |
237
|
|
|
$this->assertFileNotExists($this->fsp->getDestPath() . 'purge-me.html'); |
238
|
|
|
$this->assertFileNotExists($this->fsp->getDestPath() . 'purge-me.php'); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
public function testNoErrorPagesWhenHTMLOnly() |
242
|
|
|
{ |
243
|
|
|
$this->fsp->setFileExtension('html'); |
244
|
|
|
|
245
|
|
|
$this->fsp->publishURL('not_really_there', true); |
246
|
|
|
$this->assertFileNotExists($this->fsp->getDestPath() . 'not_really_there.html'); |
247
|
|
|
$this->assertFileNotExists($this->fsp->getDestPath() . 'not_really_there.php'); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function testErrorPageWhenPHP() |
251
|
|
|
{ |
252
|
|
|
$this->fsp->publishURL('not_really_there', true); |
253
|
|
|
$this->assertFileExists($this->fsp->getDestPath() . 'not_really_there.html'); |
254
|
|
|
$this->assertFileExists($this->fsp->getDestPath() . 'not_really_there.php'); |
255
|
|
|
$phpCacheConfig = require $this->fsp->getDestPath() . 'not_really_there.php'; |
256
|
|
|
$this->assertSame(404, $phpCacheConfig['responseCode']); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function testRedirectorPageWhenPHP() |
260
|
|
|
{ |
261
|
|
|
$redirectorPage = RedirectorPage::create(); |
262
|
|
|
$redirectorPage->URLSegment = 'somewhere-else'; |
263
|
|
|
$redirectorPage->RedirectionType = 'External'; |
264
|
|
|
$redirectorPage->ExternalURL = 'silverstripe.org'; |
265
|
|
|
$redirectorPage->write(); |
266
|
|
|
$redirectorPage->publishRecursive(); |
267
|
|
|
|
268
|
|
|
$this->fsp->publishURL('somewhere-else', true); |
269
|
|
|
|
270
|
|
|
$this->assertFileExists($this->fsp->getDestPath() . 'somewhere-else.html'); |
271
|
|
|
$this->assertContains( |
272
|
|
|
'Click this link if your browser does not redirect you', |
273
|
|
|
file_get_contents($this->fsp->getDestPath() . 'somewhere-else.html') |
274
|
|
|
); |
275
|
|
|
$this->assertFileExists($this->fsp->getDestPath() . 'somewhere-else.php'); |
276
|
|
|
$phpCacheConfig = require $this->fsp->getDestPath() . 'somewhere-else.php'; |
277
|
|
|
$this->assertSame(301, $phpCacheConfig['responseCode']); |
278
|
|
|
$this->assertContains('location: http://silverstripe.org', $phpCacheConfig['headers']); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
public function testRedirectorPageWhenHTMLOnly() |
282
|
|
|
{ |
283
|
|
|
$this->fsp->setFileExtension('html'); |
284
|
|
|
|
285
|
|
|
$redirectorPage = RedirectorPage::create(); |
286
|
|
|
$redirectorPage->URLSegment = 'somewhere-else'; |
287
|
|
|
$redirectorPage->RedirectionType = 'External'; |
288
|
|
|
$redirectorPage->ExternalURL = 'silverstripe.org'; |
289
|
|
|
$redirectorPage->write(); |
290
|
|
|
$redirectorPage->publishRecursive(); |
291
|
|
|
|
292
|
|
|
$this->fsp->publishURL('somewhere-else', true); |
293
|
|
|
|
294
|
|
|
$this->assertFileExists($this->fsp->getDestPath() . 'somewhere-else.html'); |
295
|
|
|
$this->assertContains( |
296
|
|
|
'Click this link if your browser does not redirect you', |
297
|
|
|
file_get_contents($this->fsp->getDestPath() . 'somewhere-else.html') |
298
|
|
|
); |
299
|
|
|
$this->assertFileNotExists($this->fsp->getDestPath() . 'somewhere-else.php'); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @dataProvider providePathsToURL |
304
|
|
|
*/ |
305
|
|
|
public function testPathToURL($expected, $path) |
306
|
|
|
{ |
307
|
|
|
$reflection = new \ReflectionClass(FilesystemPublisher::class); |
308
|
|
|
$pathToURL = $reflection->getMethod('pathToURL'); |
309
|
|
|
$pathToURL->setAccessible(true); |
310
|
|
|
|
311
|
|
|
$this->assertSame( |
312
|
|
|
$expected, |
313
|
|
|
$pathToURL->invoke($this->fsp, $this->fsp->getDestPath() . $path) |
314
|
|
|
); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
public function providePathsToURL() |
318
|
|
|
{ |
319
|
|
|
return [ |
320
|
|
|
['http://example.com/', 'index.html'], |
321
|
|
|
['http://example.com/about-us', 'about-us.html'], |
322
|
|
|
['http://example.com/about-us', 'about-us.php'], |
323
|
|
|
['http://example.com/parent/child', 'parent/child.html'], |
324
|
|
|
]; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
public function testGetPublishedURLs() |
328
|
|
|
{ |
329
|
|
|
$level1 = new StaticPublisherTestPage(); |
330
|
|
|
$level1->URLSegment = 'find-me'; |
331
|
|
|
$level1->write(); |
332
|
|
|
$level1->publishRecursive(); |
333
|
|
|
|
334
|
|
|
$this->fsp->publishURL('find-me', true); |
335
|
|
|
// We have to redeclare this config because the testkernel wipes it when we generate the page response |
336
|
|
|
Director::config()->set('alternate_base_url', 'http://example.com'); |
337
|
|
|
|
338
|
|
|
$this->assertSame(['http://example.com/find-me'], $this->fsp->getPublishedURLs()); |
339
|
|
|
|
340
|
|
|
$level2_1 = new StaticPublisherTestPage(); |
341
|
|
|
$level2_1->URLSegment = 'find-me-child'; |
342
|
|
|
$level2_1->ParentID = $level1->ID; |
343
|
|
|
$level2_1->write(); |
344
|
|
|
$level2_1->publishRecursive(); |
345
|
|
|
|
346
|
|
|
$this->fsp->publishURL($level2_1->Link(), true); |
347
|
|
|
Director::config()->set('alternate_base_url', 'http://example.com'); |
348
|
|
|
|
349
|
|
|
$urls = $this->fsp->getPublishedURLs(); |
350
|
|
|
$this->assertContains('http://example.com/find-me', $urls); |
351
|
|
|
$this->assertContains('http://example.com/find-me/find-me-child', $urls); |
352
|
|
|
$this->assertCount(2, $urls); |
353
|
|
|
|
354
|
|
|
$this->fsp->purgeURL('find-me'); |
355
|
|
|
$this->assertSame(['http://example.com/find-me/find-me-child'], $this->fsp->getPublishedURLs()); |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
|