Completed
Pull Request — master (#70)
by Daniel
04:46
created

FilesystemPublisherTest::testMenu2LinkingMode()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 39
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 27
nc 4
nop 0
1
<?php
2
3
namespace SilverStripe\StaticPublishQueue\Test;
4
5
use Page;
6
use SilverStripe\Assets\Filesystem;
7
use SilverStripe\CMS\Model\SiteTree;
8
use SilverStripe\Control\Director;
9
use SilverStripe\Control\HTTPResponse_Exception;
10
use SilverStripe\Core\Config\Config;
11
use SilverStripe\Dev\SapphireTest;
12
use SilverStripe\StaticPublishQueue\Extension\Publisher\FilesystemPublisher;
13
use SilverStripe\StaticPublishQueue\Model\StaticPagesQueue;
14
use SilverStripe\StaticPublishQueue\Test\StaticPublisherTest\Model\StaticPublisherTestPage;
15
use SilverStripe\View\SSViewer;
16
17
/**
18
 * Tests for the {@link FilesystemPublisher} class.
19
 *
20
 * @package staticpublisher
21
 */
22
class FilesystemPublisherTest extends SapphireTest
23
{
24
    protected $usesDatabase = true;
25
26
    protected function setUp()
27
    {
28
        parent::setUp();
29
30
        SiteTree::add_extension("FilesystemPublisher('assets/FilesystemPublisherTest-static-folder/')");
31
32
        Config::modify()->set(StaticPagesQueue::class, 'realtime', true);
33
        Config::modify()->set(FilesystemPublisher::class, 'domain_based_caching', false);
34
        Config::modify()->set(FilesystemPublisher::class, 'static_base_url', 'http://foo');
35
        Config::modify()->set(Director::class, 'alternate_base_url', 'http://foo/');
36
    }
37
38
    protected function tearDown()
39
    {
40
        SiteTree::remove_extension("FilesystemPublisher('assets/FilesystemPublisherTest-static-folder/')");
41
42
        if (file_exists(BASE_PATH . '/assets/FilesystemPublisherTest-static-folder')) {
43
            Filesystem::removeFolder(BASE_PATH . '/assets/FilesystemPublisherTest-static-folder');
44
        }
45
46
        parent::tearDown();
47
    }
48
49
    public function testUrlsToPathsWithRelativeUrls()
50
    {
51
        $fsp = new FilesystemPublisher('.', 'html');
52
53
        $this->assertEquals(
54
            array('/' => './index.html'),
55
            $fsp->urlsToPaths(array('/'))
56
        );
57
58
        $this->assertEquals(
59
            array('about-us' => './about-us.html'),
60
            $fsp->urlsToPaths(array('about-us'))
61
        );
62
63
        $this->assertEquals(
64
            array('parent/child' => 'parent/child.html'),
65
            $fsp->urlsToPaths(array('parent/child'))
66
        );
67
    }
68
69
    public function testUrlsToPathsWithAbsoluteUrls()
70
    {
71
        $fsp = new FilesystemPublisher('.', 'html');
72
73
        $url = Director::absoluteBaseUrl();
74
        $this->assertEquals(
75
            array($url => './index.html'),
76
            $fsp->urlsToPaths(array($url))
77
        );
78
79
        $url = Director::absoluteBaseUrl() . 'about-us';
80
        $this->assertEquals(
81
            array($url => './about-us.html'),
82
            $fsp->urlsToPaths(array($url))
83
        );
84
85
        $url = Director::absoluteBaseUrl() . 'parent/child';
86
        $this->assertEquals(
87
            array($url => 'parent/child.html'),
88
            $fsp->urlsToPaths(array($url))
89
        );
90
    }
91
92
    public function testUrlsToPathsWithDomainBasedCaching()
93
    {
94
        $fsp = new FilesystemPublisher('.', 'html');
95
96
        $url = 'http://domain1.com/';
97
        $this->assertEquals(
98
            array($url => 'domain1.com/index.html'),
99
            $fsp->urlsToPaths(array($url))
100
        );
101
102
        $url = 'http://domain1.com/about-us';
103
        $this->assertEquals(
104
            array($url => 'domain1.com/about-us.html'),
105
            $fsp->urlsToPaths(array($url))
106
        );
107
108
        $url = 'http://domain2.com/parent/child';
109
        $this->assertEquals(
110
            array($url => 'domain2.com/parent/child.html'),
111
            $fsp->urlsToPaths(array($url))
112
        );
113
    }
114
115
    /**
116
     * These are a few simple tests to check that we will be retrieving the
117
     * correct theme when we need it. StaticPublishing needs to be able to
118
     * retrieve a non-null theme at the time publishPages() is called.
119
     */
120
    public function testStaticPublisherTheme()
121
    {
122
123
        // @todo - this needs re-working for 4.x cascading themes
124
        // This will be the name of the default theme of this particular project
125
        $default_theme = Config::inst()->get(SSViewer::class, 'theme');
126
127
        $p1 = new Page();
128
        $p1->URLSegment = strtolower(__CLASS__) . '-page-1';
129
        $p1->HomepageForDomain = '';
130
        $p1->write();
131
        $p1->publishRecursive();
132
133
        $current_theme = Config::inst()->get(SSViewer::class, 'theme_enabled') ? Config::inst()->get(SSViewer::class, 'theme') : null;
134
        $this->assertEquals($default_theme, $current_theme);
135
136
        //We can set the static_publishing theme to something completely different:
137
        //Static publishing will use this one instead of the current_custom_theme if it is not false
138
        Config::modify()->set(FilesystemPublisher::class, 'static_publisher_theme', 'otherTheme');
139
        $current_theme = Config::inst()->get(FilesystemPublisher::class, 'static_publisher_theme');
140
141
        $this->assertNotEquals($default_theme, $current_theme);
142
    }
143
144
    public function testMenu2LinkingMode()
145
    {
146
        $this->logInWithPermission('ADMIN');
147
148
        Config::modify()->set(SSViewer::class, 'theme', null);
149
150
        $l1 = new StaticPublisherTestPage();
151
        $l1->URLSegment = strtolower(__CLASS__) . '-level-1';
152
        $l1->write();
153
        $l1->publishRecursive();
154
155
        $l2_1 = new StaticPublisherTestPage();
156
        $l2_1->URLSegment = strtolower(__CLASS__) . '-level-2-1';
157
        $l2_1->ParentID = $l1->ID;
158
        $l2_1->write();
159
160
        $l2_1->publishRecursive();
161
162
        try {
163
            $response = Director::test($l2_1->AbsoluteLink());
164
        } catch (HTTPResponse_Exception $e) {
165
            $response = $e->getResponse();
166
        }
167
168
        $this->assertEquals("current", trim($response->getBody()));
169
170
        $l2_2 = new StaticPublisherTestPage();
171
        $l2_2->URLSegment = strtolower(__CLASS__) . '-level-2-2';
172
        $l2_2->ParentID = $l1->ID;
173
        $l2_2->write();
174
        $l2_2->publishRecursive();
175
176
        try {
177
            $response = Director::test($l2_2->AbsoluteLink());
178
        } catch (HTTPResponse_Exception $e) {
179
            $response = $e->getResponse();
180
        }
181
        $this->assertEquals("linkcurrent", trim($response->getBody()));
182
    }
183
184 View Code Duplication
    public function testContentTypeHTML()
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...
185
    {
186
        StaticPublisherTestPage::remove_extension('FilesystemPublisher');
187
        StaticPublisherTestPage::add_extension("FilesystemPublisher('assets/FilesystemPublisherTest-static-folder/', 'php')");
188
        $l1 = new StaticPublisherTestPage();
189
        $l1->URLSegment = 'mimetype';
190
        $l1->write();
191
        $l1->publishRecursive();
192
        try {
193
            $response = Director::test('mimetype');
194
        } catch (HTTPResponse_Exception $e) {
195
            $response = $e->getResponse();
196
        }
197
        $this->assertEquals('text/html; charset=utf-8', $response->getHeader('Content-Type'));
198
        StaticPublisherTestPage::remove_extension('FilesystemPublisher');
199
        StaticPublisherTestPage::add_extension('FilesystemPublisher');
200
    }
201
202 View Code Duplication
    public function testContentTypeJSON()
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...
203
    {
204
        StaticPublisherTestPage::remove_extension('FilesystemPublisher');
205
        StaticPublisherTestPage::add_extension("FilesystemPublisher('assets/FilesystemPublisherTest-static-folder/', 'php')");
206
        $l1 = new StaticPublisherTestPage();
207
        $l1->URLSegment = 'mimetype';
208
        $l1->write();
209
        $l1->publishRecursive();
210
        try {
211
            $response = Director::test('mimetype/json');
212
        } catch (HTTPResponse_Exception $e) {
213
            $response = $e->getResponse();
214
        }
215
        $this->assertEquals('application/json', $response->getHeader('Content-Type'));
216
        StaticPublisherTestPage::remove_extension('FilesystemPublisher');
217
        StaticPublisherTestPage::add_extension('FilesystemPublisher');
218
    }
219
}
220