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