Passed
Pull Request — master (#133)
by
unknown
02:07
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 test123()
71
    {
72
    }
73
74
//    public function testUrlToPathWithRelativeUrls()
75
//    {
76
//        $reflection = new \ReflectionClass(FilesystemPublisher::class);
77
//        $urlToPath = $reflection->getMethod('URLtoPath');
78
//        $urlToPath->setAccessible(true);
79
//
80
//        $this->assertSame(
81
//            'index',
82
//            $urlToPath->invokeArgs($this->fsp, ['/'])
83
//        );
84
//
85
//        $this->assertSame(
86
//            'about-us',
87
//            $urlToPath->invokeArgs($this->fsp, ['about-us'])
88
//        );
89
//
90
//        $this->assertSame(
91
//            'parent/child',
92
//            $urlToPath->invokeArgs($this->fsp, ['parent/child'])
93
//        );
94
//    }
95
//
96
//    public function testUrlToPathWithAbsoluteUrls()
97
//    {
98
//        $reflection = new \ReflectionClass(FilesystemPublisher::class);
99
//        $urlToPath = $reflection->getMethod('URLtoPath');
100
//        $urlToPath->setAccessible(true);
101
//
102
//        $url = Director::absoluteBaseUrl();
103
//        $this->assertSame(
104
//            'index',
105
//            $urlToPath->invokeArgs($this->fsp, [$url])
106
//        );
107
//
108
//        $url = Director::absoluteBaseUrl() . 'about-us';
109
//        $this->assertSame(
110
//            'about-us',
111
//            $urlToPath->invokeArgs($this->fsp, [$url])
112
//        );
113
//
114
//        $url = Director::absoluteBaseUrl() . 'parent/child';
115
//        $this->assertSame(
116
//            'parent/child',
117
//            $urlToPath->invokeArgs($this->fsp, [$url])
118
//        );
119
//    }
120
//
121
//    public function testUrlToPathWithDomainBasedCaching()
122
//    {
123
//        Config::modify()->set(FilesystemPublisher::class, 'domain_based_caching', true);
124
//
125
//        $reflection = new \ReflectionClass(FilesystemPublisher::class);
126
//        $urlToPath = $reflection->getMethod('URLtoPath');
127
//        $urlToPath->setAccessible(true);
128
//
129
//        $this->fsp->setFileExtension('html');
130
//
131
//        $url = 'http://domain1.com/';
132
//        $this->assertSame(
133
//            'domain1.com/index',
134
//            $urlToPath->invokeArgs($this->fsp, [$url])
135
//        );
136
//
137
//        $url = 'http://domain1.com/about-us';
138
//        $this->assertSame(
139
//            'domain1.com/about-us',
140
//            $urlToPath->invokeArgs($this->fsp, [$url])
141
//        );
142
//
143
//        $url = 'http://domain2.com/parent/child';
144
//        $this->assertSame(
145
//            'domain2.com/parent/child',
146
//            $urlToPath->invokeArgs($this->fsp, [$url])
147
//        );
148
//    }
149
//
150
//    public function testMenu2LinkingMode()
151
//    {
152
//        SSViewer::set_themes(null);
153
//
154
//        $reflection = new \ReflectionClass(FilesystemPublisher::class);
155
//        $urlToPath = $reflection->getMethod('URLtoPath');
156
//        $urlToPath->setAccessible(true);
157
//
158
//        $level1 = new StaticPublisherTestPage();
159
//        $level1->URLSegment = 'test-level-1';
160
//        $level1->write();
161
//        $level1->publishRecursive();
162
//
163
//        $level2_1 = new StaticPublisherTestPage();
164
//        $level2_1->URLSegment = 'test-level-2-1';
165
//        $level2_1->ParentID = $level1->ID;
166
//        $level2_1->write();
167
//        $level2_1->publishRecursive();
168
//
169
//        $this->fsp->publishURL($level1->Link(), true);
170
//        $this->fsp->publishURL($level2_1->Link(), true);
171
//        $static2_1FilePath = $this->fsp->getDestPath() . $urlToPath->invokeArgs($this->fsp, [$level2_1->Link()]);
172
//
173
//        $this->assertFileExists($static2_1FilePath . '.html');
174
//        $this->assertFileExists($static2_1FilePath . '.php');
175
//        $this->assertContains(
176
//            'current',
177
//            file_get_contents($static2_1FilePath . '.html')
178
//        );
179
//
180
//        $level2_2 = new StaticPublisherTestPage();
181
//        $level2_2->URLSegment = 'test-level-2-2';
182
//        $level2_2->ParentID = $level1->ID;
183
//        $level2_2->write();
184
//        $level2_2->publishRecursive();
185
//
186
//        $this->fsp->publishURL($level2_2->Link(), true);
187
//        $static2_2FilePath = $this->fsp->getDestPath() . $urlToPath->invokeArgs($this->fsp, [$level2_2->Link()]);
188
//
189
//        $this->assertFileExists($static2_2FilePath . '.html');
190
//        $this->assertFileExists($static2_2FilePath . '.php');
191
//        $this->assertContains(
192
//            'linkcurrent',
193
//            file_get_contents($static2_2FilePath . '.html')
194
//        );
195
//    }
196
//
197
//    public function testOnlyHTML()
198
//    {
199
//        $this->fsp->setFileExtension('html');
200
//
201
//        $level1 = new StaticPublisherTestPage();
202
//        $level1->URLSegment = 'mimetype';
203
//        $level1->write();
204
//        $level1->publishRecursive();
205
//
206
//        $this->fsp->publishURL($level1->Link(), true);
207
//        $staticFilePath = $this->fsp->getDestPath() . 'mimetype';
208
//
209
//        $this->assertFileExists($staticFilePath . '.html');
210
//        $this->assertFileNotExists($staticFilePath . '.php');
211
//        $this->assertSame(
212
//            '<div class="statically-published" style="display: none"></div>',
213
//            trim(file_get_contents($staticFilePath . '.html'))
214
//        );
215
//    }
216
//
217
//    public function testPurgeURL()
218
//    {
219
//        $level1 = new StaticPublisherTestPage();
220
//        $level1->URLSegment = 'to-be-purged';
221
//        $level1->write();
222
//        $level1->publishRecursive();
223
//
224
//        $this->fsp->publishURL('to-be-purged', true);
225
//        $this->assertFileExists($this->fsp->getDestPath() . 'to-be-purged.html');
226
//        $this->assertFileExists($this->fsp->getDestPath() . 'to-be-purged.php');
227
//
228
//        $this->fsp->purgeURL('to-be-purged');
229
//        $this->assertFileNotExists($this->fsp->getDestPath() . 'to-be-purged.html');
230
//        $this->assertFileNotExists($this->fsp->getDestPath() . 'to-be-purged.php');
231
//    }
232
//
233
//    public function testPurgeURLAfterSwitchingExtensions()
234
//    {
235
//        $level1 = new StaticPublisherTestPage();
236
//        $level1->URLSegment = 'purge-me';
237
//        $level1->write();
238
//        $level1->publishRecursive();
239
//
240
//        $this->fsp->publishURL('purge-me', true);
241
//        $this->assertFileExists($this->fsp->getDestPath() . 'purge-me.html');
242
//        $this->assertFileExists($this->fsp->getDestPath() . 'purge-me.php');
243
//
244
//        $this->fsp->setFileExtension('html');
245
//
246
//        $this->fsp->purgeURL('purge-me');
247
//        $this->assertFileNotExists($this->fsp->getDestPath() . 'purge-me.html');
248
//        $this->assertFileNotExists($this->fsp->getDestPath() . 'purge-me.php');
249
//    }
250
//
251
//    public function testNoErrorPagesWhenHTMLOnly()
252
//    {
253
//        $this->fsp->setFileExtension('html');
254
//
255
//        $this->fsp->publishURL('not_really_there', true);
256
//        $this->assertFileNotExists($this->fsp->getDestPath() . 'not_really_there.html');
257
//        $this->assertFileNotExists($this->fsp->getDestPath() . 'not_really_there.php');
258
//    }
259
//
260
//    public function testErrorPageWhenPHP()
261
//    {
262
//        $this->fsp->publishURL('not_really_there', true);
263
//        $this->assertFileExists($this->fsp->getDestPath() . 'not_really_there.html');
264
//        $this->assertFileExists($this->fsp->getDestPath() . 'not_really_there.php');
265
//        $phpCacheConfig = require $this->fsp->getDestPath() . 'not_really_there.php';
266
//        $this->assertSame(404, $phpCacheConfig['responseCode']);
267
//    }
268
//
269
//    public function testRedirectorPageWhenPHP()
270
//    {
271
//        $redirectorPage = RedirectorPage::create();
272
//        $redirectorPage->URLSegment = 'somewhere-else';
273
//        $redirectorPage->RedirectionType = 'External';
274
//        $redirectorPage->ExternalURL = 'silverstripe.org';
275
//        $redirectorPage->write();
276
//        $redirectorPage->publishRecursive();
277
//
278
//        $this->fsp->publishURL('somewhere-else', true);
279
//
280
//        $this->assertFileExists($this->fsp->getDestPath() . 'somewhere-else.html');
281
//        $this->assertContains(
282
//            'Click this link if your browser does not redirect you',
283
//            file_get_contents($this->fsp->getDestPath() . 'somewhere-else.html')
284
//        );
285
//        $this->assertFileExists($this->fsp->getDestPath() . 'somewhere-else.php');
286
//        $phpCacheConfig = require $this->fsp->getDestPath() . 'somewhere-else.php';
287
//        $this->assertSame(301, $phpCacheConfig['responseCode']);
288
//        $this->assertContains('location: http://silverstripe.org', $phpCacheConfig['headers']);
289
//    }
290
//
291
//    public function testRedirectorPageWhenHTMLOnly()
292
//    {
293
//        $this->fsp->setFileExtension('html');
294
//
295
//        $redirectorPage = RedirectorPage::create();
296
//        $redirectorPage->URLSegment = 'somewhere-else';
297
//        $redirectorPage->RedirectionType = 'External';
298
//        $redirectorPage->ExternalURL = 'silverstripe.org';
299
//        $redirectorPage->write();
300
//        $redirectorPage->publishRecursive();
301
//
302
//        $this->fsp->publishURL('somewhere-else', true);
303
//
304
//        $this->assertFileExists($this->fsp->getDestPath() . 'somewhere-else.html');
305
//        $this->assertContains(
306
//            'Click this link if your browser does not redirect you',
307
//            file_get_contents($this->fsp->getDestPath() . 'somewhere-else.html')
308
//        );
309
//        $this->assertFileNotExists($this->fsp->getDestPath() . 'somewhere-else.php');
310
//    }
311
//
312
//    /**
313
//     * @dataProvider providePathsToURL
314
//     */
315
//    public function testPathToURL($expected, $path)
316
//    {
317
//        $reflection = new \ReflectionClass(FilesystemPublisher::class);
318
//        $pathToURL = $reflection->getMethod('pathToURL');
319
//        $pathToURL->setAccessible(true);
320
//
321
//        $this->assertSame(
322
//            $expected,
323
//            $pathToURL->invoke($this->fsp, $this->fsp->getDestPath() . $path)
324
//        );
325
//    }
326
//
327
//    public function providePathsToURL()
328
//    {
329
//        return [
330
//            ['http://example.com/', 'index.html'],
331
//            ['http://example.com/about-us', 'about-us.html'],
332
//            ['http://example.com/about-us', 'about-us.php'],
333
//            ['http://example.com/parent/child', 'parent/child.html'],
334
//        ];
335
//    }
336
//
337
//    public function testGetPublishedURLs()
338
//    {
339
//        $level1 = new StaticPublisherTestPage();
340
//        $level1->URLSegment = 'find-me';
341
//        $level1->write();
342
//        $level1->publishRecursive();
343
//
344
//        $this->fsp->publishURL('find-me', true);
345
//        // We have to redeclare this config because the testkernel wipes it when we generate the page response
346
//        Director::config()->set('alternate_base_url', 'http://example.com');
347
//
348
//        $this->assertSame(['http://example.com/find-me'], $this->fsp->getPublishedURLs());
349
//
350
//        $level2_1 = new StaticPublisherTestPage();
351
//        $level2_1->URLSegment = 'find-me-child';
352
//        $level2_1->ParentID = $level1->ID;
353
//        $level2_1->write();
354
//        $level2_1->publishRecursive();
355
//
356
//        $this->fsp->publishURL($level2_1->Link(), true);
357
//        Director::config()->set('alternate_base_url', 'http://example.com');
358
//
359
//        $urls = $this->fsp->getPublishedURLs();
360
//        $this->assertContains('http://example.com/find-me', $urls);
361
//        $this->assertContains('http://example.com/find-me/find-me-child', $urls);
362
//        $this->assertCount(2, $urls);
363
//
364
//        $this->fsp->purgeURL('find-me');
365
//        $this->assertSame(['http://example.com/find-me/find-me-child'], $this->fsp->getPublishedURLs());
366
//    }
367
}
368