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

FilesystemPublisherTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
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\Control\HTTPResponse_Exception;
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
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
    }
34
35
    protected function tearDown()
36
    {
37
        SiteTree::remove_extension(PublishableSiteTree::class);
38
39
        parent::tearDown();
40
    }
41
42
    public function testUrlToPathWithRelativeUrls()
43
    {
44
        $reflection = new \ReflectionClass(FilesystemPublisher::class);
45
        $urlToPath = $reflection->getMethod('URLtoPath');
46
        $urlToPath->setAccessible(true);
47
48
        $fsp = FilesystemPublisher::create()->setFileExtension('html');
49
50
        $this->assertEquals(
51
            'index.html',
52
            $urlToPath->invokeArgs($fsp, ['/'])
53
        );
54
55
        $this->assertEquals(
56
           'about-us.html',
57
            $urlToPath->invokeArgs($fsp, ['about-us'])
58
        );
59
60
        $this->assertEquals(
61
           'parent/child.html',
62
            $urlToPath->invokeArgs($fsp, ['parent/child'])
63
        );
64
    }
65
66
    public function testUrlToPathWithAbsoluteUrls()
67
    {
68
        $reflection = new \ReflectionClass(FilesystemPublisher::class);
69
        $urlToPath = $reflection->getMethod('URLtoPath');
70
        $urlToPath->setAccessible(true);
71
72
        $fsp = FilesystemPublisher::create()->setFileExtension('html');
73
74
        $url = Director::absoluteBaseUrl();
75
        $this->assertEquals(
76
            'index.html',
77
            $urlToPath->invokeArgs($fsp, [$url])
78
        );
79
80
        $url = Director::absoluteBaseUrl() . 'about-us';
81
        $this->assertEquals(
82
            'about-us.html',
83
            $urlToPath->invokeArgs($fsp, [$url])
84
        );
85
86
        $url = Director::absoluteBaseUrl() . 'parent/child';
87
        $this->assertEquals(
88
            'parent/child.html',
89
            $urlToPath->invokeArgs($fsp, [$url])
90
        );
91
    }
92
93
    public function testUrlToPathWithDomainBasedCaching()
94
    {
95
        Config::modify()->set(FilesystemPublisher::class, 'domain_based_caching', true);
96
97
        $reflection = new \ReflectionClass(FilesystemPublisher::class);
98
        $urlToPath = $reflection->getMethod('URLtoPath');
99
        $urlToPath->setAccessible(true);
100
101
        $fsp = FilesystemPublisher::create()->setFileExtension('html');
102
103
        $url = 'http://domain1.com/';
104
        $this->assertEquals(
105
            'domain1.com/index.html',
106
            $urlToPath->invokeArgs($fsp, [$url])
107
        );
108
109
        $url = 'http://domain1.com/about-us';
110
        $this->assertEquals(
111
            'domain1.com/about-us.html',
112
            $urlToPath->invokeArgs($fsp, [$url])
113
        );
114
115
        $url = 'http://domain2.com/parent/child';
116
        $this->assertEquals(
117
            'domain2.com/parent/child.html',
118
            $urlToPath->invokeArgs($fsp, [$url])
119
        );
120
    }
121
122
    public function testMenu2LinkingMode()
123
    {
124
        $this->logInWithPermission('ADMIN');
125
126
        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...
127
128
        $level1 = StaticPublisherTestPage::create();
129
        $level1->URLSegment = strtolower(__CLASS__) . '-level-1';
130
        $level1->write();
131
        $level1->publishRecursive();
132
133
        $level2_1 = StaticPublisherTestPage::create();
134
        $level2_1->URLSegment = strtolower(__CLASS__) . '-level-2-1';
135
        $level2_1->ParentID = $level1->ID;
136
        $level2_1->write();
137
138
        $level2_1->publishRecursive();
139
140
        try {
141
            $response = Director::test($level2_1->AbsoluteLink());
142
        } catch (HTTPResponse_Exception $e) {
143
            $response = $e->getResponse();
144
        }
145
146
        $this->assertEquals("current", trim($response->getBody()));
147
148
        $level2_2 = new StaticPublisherTestPage();
149
        $level2_2->URLSegment = strtolower(__CLASS__) . '-level-2-2';
150
        $level2_2->ParentID = $level1->ID;
151
        $level2_2->write();
152
        $level2_2->publishRecursive();
153
154
        try {
155
            $response = Director::test($level2_2->AbsoluteLink());
156
        } catch (HTTPResponse_Exception $e) {
157
            $response = $e->getResponse();
158
        }
159
        $this->assertEquals("linkcurrent", trim($response->getBody()));
160
    }
161
162
    public function testContentTypeHTML()
163
    {
164
        $fsp = FilesystemPublisher::create()
165
            ->setFileExtension('html')
166
            ->setDestFolder('/cache/testing/');
167
        $level1 = new StaticPublisherTestPage();
168
        $level1->URLSegment = 'mimetype';
169
        $level1->write();
170
171
        $fsp->publishURL($level1->Link(), true);
172
        try {
173
            $response = Director::test('mimetype');
174
        } catch (HTTPResponse_Exception $e) {
175
            $response = $e->getResponse();
176
        }
177
        $this->assertEquals('text/html; charset=utf-8', $response->getHeader('Content-Type'));
178
        if (file_exists($fsp->getDestPath())) {
179
            Filesystem::removeFolder($fsp->getDestPath());
180
        }
181
    }
182
}
183