Completed
Pull Request — master (#70)
by Andrew
01:32
created

testPurgeURLAfterSwitchingExtensions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 21
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 21
loc 21
rs 9.3142
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\StaticPublishQueue\Test;
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\Core\Config\Config;
10
use SilverStripe\Dev\SapphireTest;
11
use SilverStripe\StaticPublishQueue\Extension\Publishable\PublishableSiteTree;
12
use SilverStripe\StaticPublishQueue\Publisher\FilesystemPublisher;
13
use SilverStripe\StaticPublishQueue\Test\StaticPublisherTest\Model\StaticPublisherTestPage;
14
use SilverStripe\View\SSViewer;
15
use Symbiote\QueuedJobs\Services\QueuedJobService;
16
17
/**
18
 * Tests for the {@link FilesystemPublisher} class.
19
 *
20
 * @package staticpublishqueue
21
 */
22
class FilesystemPublisherTest extends SapphireTest
23
{
24
    protected $usesDatabase = true;
25
26
    /**
27
     * @var FilesystemPublisher
28
     */
29
    private $fsp = null;
30
31
    protected static $required_extensions = [
32
        SiteTree::class => [
33
            PublishableSiteTree::class,
34
        ],
35
    ];
36
37
    protected function setUp()
38
    {
39
        parent::setUp();
40
41
        Config::modify()->set(FilesystemPublisher::class, 'domain_based_caching', false);
42
        Config::modify()->set(Director::class, 'alternate_base_url', 'http://foo/');
43
        Config::modify()->set(QueuedJobService::class, 'use_shutdown_function', false);
44
    }
45
46
    protected function tearDown()
47
    {
48
        if ($this->fsp !== null && file_exists($this->fsp->getDestPath())) {
49
            Filesystem::removeFolder($this->fsp->getDestPath());
50
        }
51
        parent::tearDown();
52
    }
53
54
    public function testUrlToPathWithRelativeUrls()
55
    {
56
        $reflection = new \ReflectionClass(FilesystemPublisher::class);
57
        $urlToPath = $reflection->getMethod('URLtoPath');
58
        $urlToPath->setAccessible(true);
59
60
        $this->fsp = FilesystemPublisher::create();
61
62
        $this->assertEquals(
63
            'index',
64
            $urlToPath->invokeArgs($this->fsp, ['/'])
65
        );
66
67
        $this->assertEquals(
68
            'about-us',
69
            $urlToPath->invokeArgs($this->fsp, ['about-us'])
70
        );
71
72
        $this->assertEquals(
73
            'parent/child',
74
            $urlToPath->invokeArgs($this->fsp, ['parent/child'])
75
        );
76
    }
77
78
    public function testUrlToPathWithAbsoluteUrls()
79
    {
80
        $reflection = new \ReflectionClass(FilesystemPublisher::class);
81
        $urlToPath = $reflection->getMethod('URLtoPath');
82
        $urlToPath->setAccessible(true);
83
84
        $this->fsp = FilesystemPublisher::create();
85
86
        $url = Director::absoluteBaseUrl();
87
        $this->assertEquals(
88
            'index',
89
            $urlToPath->invokeArgs($this->fsp, [$url])
90
        );
91
92
        $url = Director::absoluteBaseUrl() . 'about-us';
93
        $this->assertEquals(
94
            'about-us',
95
            $urlToPath->invokeArgs($this->fsp, [$url])
96
        );
97
98
        $url = Director::absoluteBaseUrl() . 'parent/child';
99
        $this->assertEquals(
100
            'parent/child',
101
            $urlToPath->invokeArgs($this->fsp, [$url])
102
        );
103
    }
104
105
    public function testUrlToPathWithDomainBasedCaching()
106
    {
107
        Config::modify()->set(FilesystemPublisher::class, 'domain_based_caching', true);
108
109
        $reflection = new \ReflectionClass(FilesystemPublisher::class);
110
        $urlToPath = $reflection->getMethod('URLtoPath');
111
        $urlToPath->setAccessible(true);
112
113
        $this->fsp = FilesystemPublisher::create()->setFileExtension('html');
114
115
        $url = 'http://domain1.com/';
116
        $this->assertEquals(
117
            'domain1.com/index',
118
            $urlToPath->invokeArgs($this->fsp, [$url])
119
        );
120
121
        $url = 'http://domain1.com/about-us';
122
        $this->assertEquals(
123
            'domain1.com/about-us',
124
            $urlToPath->invokeArgs($this->fsp, [$url])
125
        );
126
127
        $url = 'http://domain2.com/parent/child';
128
        $this->assertEquals(
129
            'domain2.com/parent/child',
130
            $urlToPath->invokeArgs($this->fsp, [$url])
131
        );
132
    }
133
134
    public function testMenu2LinkingMode()
135
    {
136
        $this->logInWithPermission('ADMIN');
137
138
        SSViewer::set_themes(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
139
140
        $reflection = new \ReflectionClass(FilesystemPublisher::class);
141
        $urlToPath = $reflection->getMethod('URLtoPath');
142
        $urlToPath->setAccessible(true);
143
144
        $this->fsp = FilesystemPublisher::create()
145
            ->setDestFolder('cache/testing/');
146
147
        $level1 = StaticPublisherTestPage::create();
148
        $level1->URLSegment = 'test-level-1';
149
        $level1->write();
150
        $level1->publishRecursive();
151
152
        $level2_1 = StaticPublisherTestPage::create();
153
        $level2_1->URLSegment = 'test-level-2-1';
154
        $level2_1->ParentID = $level1->ID;
155
        $level2_1->write();
156
        $level2_1->publishRecursive();
157
158
        $this->fsp->publishURL($level1->Link(), true);
159
        $this->fsp->publishURL($level2_1->Link(), true);
160
        $static2_1FilePath = $this->fsp->getDestPath().$urlToPath->invokeArgs($this->fsp, [$level2_1->Link()]);
161
162
        $this->assertFileExists($static2_1FilePath.'.html');
163
        $this->assertFileExists($static2_1FilePath.'.php');
164
        $this->assertContains(
165
            'current',
166
            file_get_contents($static2_1FilePath.'.html')
167
        );
168
169
        $level2_2 = StaticPublisherTestPage::create();
170
        $level2_2->URLSegment = 'test-level-2-2';
171
        $level2_2->ParentID = $level1->ID;
172
        $level2_2->write();
173
        $level2_2->publishRecursive();
174
175
        $this->fsp->publishURL($level2_2->Link(), true);
176
        $static2_2FilePath = $this->fsp->getDestPath().$urlToPath->invokeArgs($this->fsp, [$level2_2->Link()]);
177
178
        $this->assertFileExists($static2_2FilePath.'.html');
179
        $this->assertFileExists($static2_2FilePath.'.php');
180
        $this->assertContains(
181
            'linkcurrent',
182
            file_get_contents($static2_2FilePath.'.html')
183
        );
184
185
    }
186
187
    public function testOnlyHTML()
188
    {
189
        $this->logInWithPermission('ADMIN');
190
191
        $this->fsp = FilesystemPublisher::create()
192
            ->setFileExtension('html')
193
            ->setDestFolder('cache/testing/');
194
        $level1 = StaticPublisherTestPage::create();
195
        $level1->URLSegment = 'mimetype';
196
        $level1->write();
197
        $level1->publishRecursive();
198
199
        $this->fsp->publishURL($level1->Link(), true);
200
        $staticFilePath = $this->fsp->getDestPath().'mimetype';
201
202
        $this->assertFileExists($staticFilePath.'.html');
203
        $this->assertFileNotExists($staticFilePath.'.php');
204
        $this->assertEquals(
205
            "<div class=\"statically-published\" style=\"display: none\"></div>",
206
            trim(file_get_contents($staticFilePath.'.html'))
207
        );
208
    }
209
210 View Code Duplication
    public function testPurgeURL()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
211
    {
212
        $this->logInWithPermission('ADMIN');
213
214
        $this->fsp = FilesystemPublisher::create()
215
            ->setDestFolder('cache/testing/');
216
        $level1 = StaticPublisherTestPage::create();
217
        $level1->URLSegment = 'to-be-purged';
218
        $level1->write();
219
        $level1->publishRecursive();
220
221
        $this->fsp->publishURL('to-be-purged', true);
222
        $this->assertFileExists($this->fsp->getDestPath().'to-be-purged.html');
223
        $this->assertFileExists($this->fsp->getDestPath().'to-be-purged.php');
224
225
        $this->fsp->purgeURL('to-be-purged');
226
        $this->assertFileNotExists($this->fsp->getDestPath().'to-be-purged.html');
227
        $this->assertFileNotExists($this->fsp->getDestPath().'to-be-purged.php');
228
    }
229
230 View Code Duplication
    public function testPurgeURLAfterSwitchingExtensions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
231
    {
232
        $this->logInWithPermission('ADMIN');
233
234
        $this->fsp = FilesystemPublisher::create()
235
            ->setDestFolder('cache/testing/');
236
        $level1 = StaticPublisherTestPage::create();
237
        $level1->URLSegment = 'purge-me';
238
        $level1->write();
239
        $level1->publishRecursive();
240
241
        $this->fsp->publishURL('purge-me', true);
242
        $this->assertFileExists($this->fsp->getDestPath().'purge-me.html');
243
        $this->assertFileExists($this->fsp->getDestPath().'purge-me.php');
244
245
        $this->fsp->setFileExtension('html');
246
247
        $this->fsp->purgeURL('purge-me');
248
        $this->assertFileNotExists($this->fsp->getDestPath().'purge-me.html');
249
        $this->assertFileNotExists($this->fsp->getDestPath().'purge-me.php');
250
    }
251
252
    public function testNoErrorPagesWhenHTMLOnly()
253
    {
254
        $this->logInWithPermission('ADMIN');
255
256
        $this->fsp = FilesystemPublisher::create()
257
            ->setFileExtension('html')
258
            ->setDestFolder('cache/testing/');
259
        $this->fsp->publishURL('not_really_there', true);
260
        $this->assertFileNotExists($this->fsp->getDestPath() . 'not_really_there.html');
261
        $this->assertFileNotExists($this->fsp->getDestPath() . 'not_really_there.php');
262
263
    }
264
265
    public function testErrorPageWhenPHP()
266
    {
267
        $this->logInWithPermission('ADMIN');
268
269
        $this->fsp = FilesystemPublisher::create()
270
            ->setDestFolder('cache/testing/');
271
        $this->fsp->publishURL('not_really_there', true);
272
        $this->assertFileExists($this->fsp->getDestPath() . 'not_really_there.html');
273
        $this->assertFileExists($this->fsp->getDestPath() . 'not_really_there.php');
274
        $phpCacheConfig = require $this->fsp->getDestPath() . 'not_really_there.php';
275
        $this->assertEquals(404, $phpCacheConfig['responseCode']);
276
277
    }
278
279
    public function testRedirectorPageWhenPHP()
280
    {
281
        $this->logInWithPermission('ADMIN');
282
283
        $this->fsp = FilesystemPublisher::create()
284
            ->setDestFolder('cache/testing/');
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->assertFileExists($this->fsp->getDestPath() . 'somewhere-else.php');
300
        $phpCacheConfig = require $this->fsp->getDestPath() . 'somewhere-else.php';
301
        $this->assertEquals(301, $phpCacheConfig['responseCode']);
302
        $this->assertContains('location: http://silverstripe.org', $phpCacheConfig['headers']);
303
304
    }
305
306
    public function testRedirectorPageWhenHTMLOnly()
307
    {
308
        $this->logInWithPermission('ADMIN');
309
310
        $this->fsp = FilesystemPublisher::create()
311
            ->setFileExtension('html')
312
            ->setDestFolder('cache/testing/');
313
314
        $redirectorPage = RedirectorPage::create();
315
        $redirectorPage->URLSegment = 'somewhere-else';
316
        $redirectorPage->RedirectionType = 'External';
317
        $redirectorPage->ExternalURL = 'silverstripe.org';
318
        $redirectorPage->write();
319
        $redirectorPage->publishRecursive();
320
321
        $this->fsp->publishURL('somewhere-else', true);
322
323
        $this->assertFileExists($this->fsp->getDestPath() . 'somewhere-else.html');
324
        $this->assertContains(
325
            'Click this link if your browser does not redirect you',
326
            file_get_contents($this->fsp->getDestPath() . 'somewhere-else.html')
327
        );
328
        $this->assertFileNotExists($this->fsp->getDestPath() . 'somewhere-else.php');
329
330
    }
331
332
    public function testPathToURL()
333
    {
334
        $reflection = new \ReflectionClass(FilesystemPublisher::class);
335
        $pathToURL = $reflection->getMethod('pathToURL');
336
        $pathToURL->setAccessible(true);
337
338
        $this->fsp = FilesystemPublisher::create();
339
340
        $this->assertEquals(
341
            '',
342
            $pathToURL->invokeArgs($this->fsp, ['index.html'])
343
        );
344
345
        $this->assertEquals(
346
            '',
347
            $pathToURL->invokeArgs($this->fsp, ['cache/index.html'])
348
        );
349
350
        $this->assertEquals(
351
            '',
352
            $pathToURL->invokeArgs($this->fsp, ['/cache/index.html'])
353
        );
354
355
        $this->assertEquals(
356
            'about-us',
357
            $pathToURL->invokeArgs($this->fsp, ['about-us.html'])
358
        );
359
360
        $this->assertEquals(
361
            'about-us',
362
            $pathToURL->invokeArgs($this->fsp, ['about-us.php'])
363
        );
364
365
        $this->assertEquals(
366
            'parent/child',
367
            $pathToURL->invokeArgs($this->fsp, ['parent/child.html'])
368
        );
369
370
        $this->assertEquals(
371
            'parent/child',
372
            $pathToURL->invokeArgs($this->fsp, ['cache/parent/child.php'])
373
        );
374
    }
375
376
    public function testGetPublishedURLs()
377
    {
378
        $this->logInWithPermission('ADMIN');
379
380
        $this->fsp = FilesystemPublisher::create()
381
            ->setDestFolder('cache/testing/');
382
        $level1 = StaticPublisherTestPage::create();
383
        $level1->URLSegment = 'find-me';
384
        $level1->write();
385
        $level1->publishRecursive();
386
387
        $this->fsp->publishURL('find-me', true);
388
        $this->assertEquals(['find-me'], $this->fsp->getPublishedURLs());
389
390
        $level2_1 = StaticPublisherTestPage::create();
391
        $level2_1->URLSegment = 'find-me-child';
392
        $level2_1->ParentID = $level1->ID;
393
        $level2_1->write();
394
        $level2_1->publishRecursive();
395
396
        $this->fsp->publishURL($level2_1->Link(), true);
397
        $urls = $this->fsp->getPublishedURLs();
398
        $this->assertContains('find-me', $urls);
399
        $this->assertContains('find-me/find-me-child', $urls);
400
        $this->assertCount(2, $urls);
401
402
        $this->fsp->purgeURL('find-me');
403
        $this->assertEquals(['find-me/find-me-child'], $this->fsp->getPublishedURLs());
404
    }
405
}
406