Passed
Pull Request — master (#133)
by
unknown
02:04
created

FilesystemPublisherTest::testRedirectorPageWhenPHP()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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