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

FilesystemPublisherTest::testContentTypeHTML()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 2
eloc 17
nc 2
nop 0
1
<?php
2
3
namespace SilverStripe\StaticPublishQueue\Test;
4
5
use SilverStripe\Assets\Filesystem;
6
use SilverStripe\CMS\Model\SiteTree;
7
use SilverStripe\Control\Director;
8
use SilverStripe\Core\Config\Config;
9
use SilverStripe\Dev\SapphireTest;
10
use SilverStripe\StaticPublishQueue\Extension\Publishable\PublishableSiteTree;
11
use SilverStripe\StaticPublishQueue\Publisher\FilesystemPublisher;
12
use SilverStripe\StaticPublishQueue\Test\StaticPublisherTest\Model\StaticPublisherTestPage;
13
use SilverStripe\View\SSViewer;
14
use Symbiote\QueuedJobs\Services\QueuedJobService;
15
16
/**
17
 * Tests for the {@link FilesystemPublisher} class.
18
 *
19
 * @package staticpublisher
20
 */
21
class FilesystemPublisherTest extends SapphireTest
22
{
23
    protected $usesDatabase = true;
24
25
    protected function setUp()
26
    {
27
        parent::setUp();
28
29
        SiteTree::add_extension(PublishableSiteTree::class);
30
31
        Config::modify()->set(FilesystemPublisher::class, 'domain_based_caching', false);
32
        Config::modify()->set(Director::class, 'alternate_base_url', 'http://foo/');
33
        Config::modify()->set(QueuedJobService::class, 'use_shutdown_function', false);
34
    }
35
36
    protected function tearDown()
37
    {
38
        SiteTree::remove_extension(PublishableSiteTree::class);
39
40
        parent::tearDown();
41
    }
42
43
    public function testUrlToPathWithRelativeUrls()
44
    {
45
        $reflection = new \ReflectionClass(FilesystemPublisher::class);
46
        $urlToPath = $reflection->getMethod('URLtoPath');
47
        $urlToPath->setAccessible(true);
48
49
        $fsp = FilesystemPublisher::create()->setFileExtension('html');
50
51
        $this->assertEquals(
52
            'index.html',
53
            $urlToPath->invokeArgs($fsp, ['/'])
54
        );
55
56
        $this->assertEquals(
57
            'about-us.html',
58
            $urlToPath->invokeArgs($fsp, ['about-us'])
59
        );
60
61
        $this->assertEquals(
62
            'parent/child.html',
63
            $urlToPath->invokeArgs($fsp, ['parent/child'])
64
        );
65
    }
66
67
    public function testUrlToPathWithAbsoluteUrls()
68
    {
69
        $reflection = new \ReflectionClass(FilesystemPublisher::class);
70
        $urlToPath = $reflection->getMethod('URLtoPath');
71
        $urlToPath->setAccessible(true);
72
73
        $fsp = FilesystemPublisher::create()->setFileExtension('html');
74
75
        $url = Director::absoluteBaseUrl();
76
        $this->assertEquals(
77
            'index.html',
78
            $urlToPath->invokeArgs($fsp, [$url])
79
        );
80
81
        $url = Director::absoluteBaseUrl() . 'about-us';
82
        $this->assertEquals(
83
            'about-us.html',
84
            $urlToPath->invokeArgs($fsp, [$url])
85
        );
86
87
        $url = Director::absoluteBaseUrl() . 'parent/child';
88
        $this->assertEquals(
89
            'parent/child.html',
90
            $urlToPath->invokeArgs($fsp, [$url])
91
        );
92
    }
93
94
    public function testUrlToPathWithDomainBasedCaching()
95
    {
96
        Config::modify()->set(FilesystemPublisher::class, 'domain_based_caching', true);
97
98
        $reflection = new \ReflectionClass(FilesystemPublisher::class);
99
        $urlToPath = $reflection->getMethod('URLtoPath');
100
        $urlToPath->setAccessible(true);
101
102
        $fsp = FilesystemPublisher::create()->setFileExtension('html');
103
104
        $url = 'http://domain1.com/';
105
        $this->assertEquals(
106
            'domain1.com/index.html',
107
            $urlToPath->invokeArgs($fsp, [$url])
108
        );
109
110
        $url = 'http://domain1.com/about-us';
111
        $this->assertEquals(
112
            'domain1.com/about-us.html',
113
            $urlToPath->invokeArgs($fsp, [$url])
114
        );
115
116
        $url = 'http://domain2.com/parent/child';
117
        $this->assertEquals(
118
            'domain2.com/parent/child.html',
119
            $urlToPath->invokeArgs($fsp, [$url])
120
        );
121
    }
122
123
    public function testMenu2LinkingMode()
124
    {
125
        $this->logInWithPermission('ADMIN');
126
127
        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...
128
129
        $reflection = new \ReflectionClass(FilesystemPublisher::class);
130
        $urlToPath = $reflection->getMethod('URLtoPath');
131
        $urlToPath->setAccessible(true);
132
133
        $fsp = FilesystemPublisher::create()
134
            ->setDestFolder('cache/testing/');
135
136
        $level1 = StaticPublisherTestPage::create();
137
        $level1->URLSegment = 'test-level-1';
138
        $level1->write();
139
        $level1->publishRecursive();
140
141
        $level2_1 = StaticPublisherTestPage::create();
142
        $level2_1->URLSegment = 'test-level-2-1';
143
        $level2_1->ParentID = $level1->ID;
144
        $level2_1->write();
145
        $level2_1->publishRecursive();
146
147
        $fsp->publishURL($level1->Link(), true);
148
        $fsp->publishURL($level2_1->Link(), true);
149
        $static2_1FilePath = $fsp->getDestPath().$urlToPath->invokeArgs($fsp, [$level2_1->Link()]);
150
151
        $this->assertFileExists($static2_1FilePath);
152
        $this->assertContains(
153
            'current',
154
            file_get_contents($static2_1FilePath)
155
        );
156
157
        $level2_2 = new StaticPublisherTestPage();
158
        $level2_2->URLSegment = 'test-level-2-2';
159
        $level2_2->ParentID = $level1->ID;
160
        $level2_2->write();
161
        $level2_2->publishRecursive();
162
163
        $fsp->publishURL($level2_2->Link(), true);
164
        $static2_2FilePath = $fsp->getDestPath().$urlToPath->invokeArgs($fsp, [$level2_2->Link()]);
165
166
        $this->assertFileExists($static2_2FilePath);
167
        $this->assertContains(
168
            'linkcurrent',
169
            file_get_contents($static2_2FilePath)
170
        );
171
172
        if (file_exists($fsp->getDestPath())) {
173
            Filesystem::removeFolder($fsp->getDestPath());
174
        }
175
    }
176
177
    public function testContentTypeHTML()
178
    {
179
        $this->logInWithPermission('ADMIN');
180
181
        $fsp = FilesystemPublisher::create()
182
            ->setFileExtension('html')
183
            ->setDestFolder('cache/testing/');
184
        $level1 = new StaticPublisherTestPage();
185
        $level1->URLSegment = 'mimetype';
186
        $level1->write();
187
        $level1->publishRecursive();
188
189
        $fsp->publishURL($level1->Link(), true);
190
        $staticFilePath = $fsp->getDestPath().'mimetype.html';
191
192
        $this->assertFileExists($staticFilePath);
193
        $this->assertEquals(
194
            "<div class=\"statically-published\" style=\"display: none\"></div>",
195
            trim(file_get_contents($staticFilePath))
196
        );
197
        if (file_exists($fsp->getDestPath())) {
198
            Filesystem::removeFolder($fsp->getDestPath());
199
        }
200
    }
201
}
202