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

FilesystemPublisherTest::testMenu2LinkingMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 44
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 33
nc 1
nop 0
dl 0
loc 44
rs 9.392
c 0
b 0
f 0
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\Core\Injector\Injector;
12
use SilverStripe\Dev\SapphireTest;
13
use SilverStripe\Dev\TestKernel;
14
use SilverStripe\StaticPublishQueue\Extension\Publishable\PublishableSiteTree;
15
use SilverStripe\StaticPublishQueue\Publisher\FilesystemPublisher;
16
use SilverStripe\StaticPublishQueue\Test\QueuedJobsTestService;
17
use SilverStripe\StaticPublishQueue\Test\StaticPublisherTest\Model\StaticPublisherTestPage;
18
use SilverStripe\View\SSViewer;
19
use Symbiote\QueuedJobs\Services\QueuedJobHandler;
20
use Symbiote\QueuedJobs\Services\QueuedJobService;
21
use Symbiote\QueuedJobs\Tests\QueuedJobsTest\QueuedJobsTest_Handler;
0 ignored issues
show
Bug introduced by
The type Symbiote\QueuedJobs\Test...\QueuedJobsTest_Handler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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