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

testUrlToPathWithAbsoluteUrls()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 24
rs 8.9713
cc 1
eloc 16
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
        $this->fsp = FilesystemPublisher::create()->setDestFolder('cache/testing/');
46
    }
47
48
    protected function tearDown()
49
    {
50
        if ($this->fsp !== null && file_exists($this->fsp->getDestPath())) {
51
            Filesystem::removeFolder($this->fsp->getDestPath());
52
        }
53
        parent::tearDown();
54
    }
55
56
    public function testUrlToPathWithRelativeUrls()
57
    {
58
        $reflection = new \ReflectionClass(FilesystemPublisher::class);
59
        $urlToPath = $reflection->getMethod('URLtoPath');
60
        $urlToPath->setAccessible(true);
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
        $url = Director::absoluteBaseUrl();
85
        $this->assertEquals(
86
            'index',
87
            $urlToPath->invokeArgs($this->fsp, [$url])
88
        );
89
90
        $url = Director::absoluteBaseUrl() . 'about-us';
91
        $this->assertEquals(
92
            'about-us',
93
            $urlToPath->invokeArgs($this->fsp, [$url])
94
        );
95
96
        $url = Director::absoluteBaseUrl() . 'parent/child';
97
        $this->assertEquals(
98
            'parent/child',
99
            $urlToPath->invokeArgs($this->fsp, [$url])
100
        );
101
    }
102
103
    public function testUrlToPathWithDomainBasedCaching()
104
    {
105
        Config::modify()->set(FilesystemPublisher::class, 'domain_based_caching', true);
106
107
        $reflection = new \ReflectionClass(FilesystemPublisher::class);
108
        $urlToPath = $reflection->getMethod('URLtoPath');
109
        $urlToPath->setAccessible(true);
110
111
        $this->fsp->setFileExtension('html');
112
113
        $url = 'http://domain1.com/';
114
        $this->assertEquals(
115
            'domain1.com/index',
116
            $urlToPath->invokeArgs($this->fsp, [$url])
117
        );
118
119
        $url = 'http://domain1.com/about-us';
120
        $this->assertEquals(
121
            'domain1.com/about-us',
122
            $urlToPath->invokeArgs($this->fsp, [$url])
123
        );
124
125
        $url = 'http://domain2.com/parent/child';
126
        $this->assertEquals(
127
            'domain2.com/parent/child',
128
            $urlToPath->invokeArgs($this->fsp, [$url])
129
        );
130
    }
131
132
    public function testMenu2LinkingMode()
133
    {
134
        $this->logInWithPermission('ADMIN');
135
136
        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...
137
138
        $reflection = new \ReflectionClass(FilesystemPublisher::class);
139
        $urlToPath = $reflection->getMethod('URLtoPath');
140
        $urlToPath->setAccessible(true);
141
142
        $level1 = StaticPublisherTestPage::create();
143
        $level1->URLSegment = 'test-level-1';
144
        $level1->write();
145
        $level1->publishRecursive();
146
147
        $level2_1 = StaticPublisherTestPage::create();
148
        $level2_1->URLSegment = 'test-level-2-1';
149
        $level2_1->ParentID = $level1->ID;
150
        $level2_1->write();
151
        $level2_1->publishRecursive();
152
153
        $this->fsp->publishURL($level1->Link(), true);
154
        $this->fsp->publishURL($level2_1->Link(), true);
155
        $static2_1FilePath = $this->fsp->getDestPath().$urlToPath->invokeArgs($this->fsp, [$level2_1->Link()]);
156
157
        $this->assertFileExists($static2_1FilePath.'.html');
158
        $this->assertFileExists($static2_1FilePath.'.php');
159
        $this->assertContains(
160
            'current',
161
            file_get_contents($static2_1FilePath.'.html')
162
        );
163
164
        $level2_2 = StaticPublisherTestPage::create();
165
        $level2_2->URLSegment = 'test-level-2-2';
166
        $level2_2->ParentID = $level1->ID;
167
        $level2_2->write();
168
        $level2_2->publishRecursive();
169
170
        $this->fsp->publishURL($level2_2->Link(), true);
171
        $static2_2FilePath = $this->fsp->getDestPath().$urlToPath->invokeArgs($this->fsp, [$level2_2->Link()]);
172
173
        $this->assertFileExists($static2_2FilePath.'.html');
174
        $this->assertFileExists($static2_2FilePath.'.php');
175
        $this->assertContains(
176
            'linkcurrent',
177
            file_get_contents($static2_2FilePath.'.html')
178
        );
179
    }
180
181
    public function testOnlyHTML()
182
    {
183
        $this->logInWithPermission('ADMIN');
184
185
        $this->fsp->setFileExtension('html');
186
187
        $level1 = StaticPublisherTestPage::create();
188
        $level1->URLSegment = 'mimetype';
189
        $level1->write();
190
        $level1->publishRecursive();
191
192
        $this->fsp->publishURL($level1->Link(), true);
193
        $staticFilePath = $this->fsp->getDestPath().'mimetype';
194
195
        $this->assertFileExists($staticFilePath.'.html');
196
        $this->assertFileNotExists($staticFilePath.'.php');
197
        $this->assertEquals(
198
            "<div class=\"statically-published\" style=\"display: none\"></div>",
199
            trim(file_get_contents($staticFilePath.'.html'))
200
        );
201
    }
202
203 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...
204
    {
205
        $this->logInWithPermission('ADMIN');
206
207
        $level1 = StaticPublisherTestPage::create();
208
        $level1->URLSegment = 'to-be-purged';
209
        $level1->write();
210
        $level1->publishRecursive();
211
212
        $this->fsp->publishURL('to-be-purged', true);
213
        $this->assertFileExists($this->fsp->getDestPath().'to-be-purged.html');
214
        $this->assertFileExists($this->fsp->getDestPath().'to-be-purged.php');
215
216
        $this->fsp->purgeURL('to-be-purged');
217
        $this->assertFileNotExists($this->fsp->getDestPath().'to-be-purged.html');
218
        $this->assertFileNotExists($this->fsp->getDestPath().'to-be-purged.php');
219
    }
220
221 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...
222
    {
223
        $this->logInWithPermission('ADMIN');
224
225
        $level1 = StaticPublisherTestPage::create();
226
        $level1->URLSegment = 'purge-me';
227
        $level1->write();
228
        $level1->publishRecursive();
229
230
        $this->fsp->publishURL('purge-me', true);
231
        $this->assertFileExists($this->fsp->getDestPath().'purge-me.html');
232
        $this->assertFileExists($this->fsp->getDestPath().'purge-me.php');
233
234
        $this->fsp->setFileExtension('html');
235
236
        $this->fsp->purgeURL('purge-me');
237
        $this->assertFileNotExists($this->fsp->getDestPath().'purge-me.html');
238
        $this->assertFileNotExists($this->fsp->getDestPath().'purge-me.php');
239
    }
240
241
    public function testNoErrorPagesWhenHTMLOnly()
242
    {
243
        $this->logInWithPermission('ADMIN');
244
245
        $this->fsp->setFileExtension('html');
246
247
        $this->fsp->publishURL('not_really_there', true);
248
        $this->assertFileNotExists($this->fsp->getDestPath() . 'not_really_there.html');
249
        $this->assertFileNotExists($this->fsp->getDestPath() . 'not_really_there.php');
250
    }
251
252
    public function testErrorPageWhenPHP()
253
    {
254
        $this->logInWithPermission('ADMIN');
255
256
        $this->fsp->publishURL('not_really_there', true);
257
        $this->assertFileExists($this->fsp->getDestPath() . 'not_really_there.html');
258
        $this->assertFileExists($this->fsp->getDestPath() . 'not_really_there.php');
259
        $phpCacheConfig = require $this->fsp->getDestPath() . 'not_really_there.php';
260
        $this->assertEquals(404, $phpCacheConfig['responseCode']);
261
    }
262
263
    public function testRedirectorPageWhenPHP()
264
    {
265
        $this->logInWithPermission('ADMIN');
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->assertEquals(301, $phpCacheConfig['responseCode']);
284
        $this->assertContains('location: http://silverstripe.org', $phpCacheConfig['headers']);
285
    }
286
287
    public function testRedirectorPageWhenHTMLOnly()
288
    {
289
        $this->logInWithPermission('ADMIN');
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
    public function testPathToURL()
311
    {
312
        $reflection = new \ReflectionClass(FilesystemPublisher::class);
313
        $pathToURL = $reflection->getMethod('pathToURL');
314
        $pathToURL->setAccessible(true);
315
316
        $this->assertEquals(
317
            '/',
318
            $pathToURL->invokeArgs($this->fsp, ['index.html'])
319
        );
320
321
        $this->assertEquals(
322
            'about-us',
323
            $pathToURL->invokeArgs($this->fsp, ['about-us.html'])
324
        );
325
326
        $this->assertEquals(
327
            'about-us',
328
            $pathToURL->invokeArgs($this->fsp, ['about-us.php'])
329
        );
330
331
        $this->assertEquals(
332
            'parent/child',
333
            $pathToURL->invokeArgs($this->fsp, ['parent/child.html'])
334
        );
335
    }
336
337
    public function testGetPublishedURLs()
338
    {
339
        $this->logInWithPermission('ADMIN');
340
341
        $level1 = StaticPublisherTestPage::create();
342
        $level1->URLSegment = 'find-me';
343
        $level1->write();
344
        $level1->publishRecursive();
345
346
        $this->fsp->publishURL('find-me', true);
347
        $this->assertEquals(['find-me'], $this->fsp->getPublishedURLs());
348
349
        $level2_1 = StaticPublisherTestPage::create();
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
        $urls = $this->fsp->getPublishedURLs();
357
        $this->assertContains('find-me', $urls);
358
        $this->assertContains('find-me/find-me-child', $urls);
359
        $this->assertCount(2, $urls);
360
361
        $this->fsp->purgeURL('find-me');
362
        $this->assertEquals(['find-me/find-me-child'], $this->fsp->getPublishedURLs());
363
    }
364
}
365