Passed
Push — master ( c268eb...3967d6 )
by Daniel
04:20
created

ContentControllerTest::testLinkShortcodes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\CMS\Tests\Controllers;
4
5
use SilverStripe\CMS\Controllers\ContentController;
6
use SilverStripe\CMS\Controllers\RootURLController;
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\FunctionalTest;
12
use SilverStripe\Versioned\Versioned;
13
use Page;
0 ignored issues
show
Bug introduced by
The type Page was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
class ContentControllerTest extends FunctionalTest
16
{
17
    protected static $fixture_file = 'ContentControllerTest.yml';
18
19
    protected static $disable_themes = true;
20
21
    protected static $extra_dataobjects = [
22
        ContentControllerTest_Page::class,
23
        ContentControllerTestPage::class,
24
        ContentControllerTestPageWithoutController::class,
25
    ];
26
27
    protected function setUp()
28
    {
29
        parent::setUp();
30
31
        Config::modify()->set(SiteTree::class, 'nested_urls', true);
32
33
        // Ensure all pages are published
34
        /** @var Page $page */
35
        foreach (Page::get() as $page) {
36
            $page->publishSingle();
37
        }
38
    }
39
40
    /**
41
     * Test that nested pages, basic actions, and nested/non-nested URL switching works properly
42
     */
43
44
    public function testNestedPages()
45
    {
46
        Config::modify()->set(SiteTree::class, 'nested_urls', true);
47
48
        $this->assertEquals('Home Page', $this->get('/')->getBody());
49
        $this->assertEquals('Home Page', $this->get('/home/index/')->getBody());
50
        $this->assertEquals('Home Page', $this->get('/home/second-index/')->getBody());
51
52
        $this->assertEquals('Second Level Page', $this->get('/home/second-level/')->getBody());
53
        $this->assertEquals('Second Level Page', $this->get('/home/second-level/index/')->getBody());
54
        $this->assertEquals('Second Level Page', $this->get('/home/second-level/second-index/')->getBody());
55
56
        $this->assertEquals('Third Level Page', $this->get('/home/second-level/third-level/')->getBody());
57
        $this->assertEquals('Third Level Page', $this->get('/home/second-level/third-level/index/')->getBody());
58
        $this->assertEquals('Third Level Page', $this->get('/home/second-level/third-level/second-index/')->getBody());
59
60
        RootURLController::reset();
61
        Config::modify()->set(SiteTree::class, 'nested_urls', false);
62
63
        $this->assertEquals('Home Page', $this->get('/')->getBody());
64
        $this->assertEquals('Home Page', $this->get('/home/')->getBody());
65
        $this->assertEquals('Home Page', $this->get('/home/second-index/')->getBody());
66
67
        $this->assertEquals('Second Level Page', $this->get('/second-level/')->getBody());
68
        $this->assertEquals('Second Level Page', $this->get('/second-level/index/')->getBody());
69
        $this->assertEquals('Second Level Page', $this->get('/second-level/second-index/')->getBody());
70
71
        $this->assertEquals('Third Level Page', $this->get('/third-level/')->getBody());
72
        $this->assertEquals('Third Level Page', $this->get('/third-level/index/')->getBody());
73
        $this->assertEquals('Third Level Page', $this->get('/third-level/second-index/')->getBody());
74
    }
75
76
    /**
77
     * Tests {@link ContentController::ChildrenOf()}
78
     */
79
    public function testChildrenOf()
80
    {
81
        $controller = new ContentController();
82
83
        $this->assertEquals(1, $controller->ChildrenOf('/')->Count());
84
        $this->assertEquals(1, $controller->ChildrenOf('/home/')->Count());
85
        $this->assertEquals(2, $controller->ChildrenOf('/home/second-level/')->Count());
86
        $this->assertEquals(0, $controller->ChildrenOf('/home/second-level/third-level/')->Count());
87
88
        SiteTree::config()->set('nested_urls', false);
89
90
        $this->assertEquals(1, $controller->ChildrenOf('/')->Count());
91
        $this->assertEquals(1, $controller->ChildrenOf('/home/')->Count());
92
        $this->assertEquals(2, $controller->ChildrenOf('/second-level/')->Count());
93
        $this->assertEquals(0, $controller->ChildrenOf('/third-level/')->Count());
94
    }
95
96
    public function testDeepNestedURLs()
97
    {
98
99
        $page = new Page();
100
        $page->URLSegment = 'base-page';
101
        $page->write();
102
        $page->publishSingle();
103
104
        for ($i = 0; $i < 10; $i++) {
105
            $parentID = $page->ID;
106
107
            $page = new ContentControllerTest_Page();
108
            $page->ParentID = $parentID;
109
            $page->Title = "Page Level $i";
110
            $page->URLSegment = "level-$i";
111
            $page->write();
112
            $page->publishSingle();
113
114
            $relativeLink = Director::makeRelative($page->Link());
115
            $this->assertEquals($page->Title, $this->get($relativeLink)->getBody());
116
        }
117
    }
118
119
    public function testViewDraft()
120
    {
121
        // test when user does not have permission, should get login form
122
        $this->logInWithPermission('EDITOR');
123
        try {
124
            $response = $this->get('/contact/?stage=Stage');
125
        } catch (HTTPResponse_Exception $responseException) {
126
            $response = $responseException->getResponse();
127
        }
128
129
        $this->assertEquals('403', $response->getstatusCode());
130
131
        // test when user does have permission, should show page title and header ok.
132
        $this->logInWithPermission('ADMIN');
133
        $this->assertEquals('200', $this->get('contact/?stage=Stage')->getstatusCode());
134
    }
135
136
    public function testLinkShortcodes()
137
    {
138
        $linkedPage = new SiteTree();
139
        $linkedPage->URLSegment = 'linked-page';
140
        $linkedPage->write();
141
        $linkedPage->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
142
143
        $page = new SiteTree();
144
        $page->URLSegment = 'linking-page';
145
        $page->Content = sprintf('<a href="[sitetree_link,id=%s]">Testlink</a>', $linkedPage->ID);
146
        $page->write();
147
        $page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
148
149
        $link = $page->RelativeLink();
150
        $response = $this->get($link);
151
        $this->assertContains(
152
            sprintf('<a href="%s">Testlink</a>', $linkedPage->Link()),
153
            $response->getBody(),
154
            '"sitetree_link" shortcodes get parsed properly'
155
        );
156
    }
157
158
159
    /**
160
     * Tests that {@link ContentController::getViewer()} chooses the correct templates.
161
     *
162
     * @covers \SilverStripe\CMS\Controllers\ContentController::getViewer()
163
     **/
164
    public function testGetViewer()
165
    {
166
        $this->useTestTheme(__DIR__, 'controllertest', function () {
167
168
            // Test a page without a controller (ContentControllerTest_PageWithoutController.ss)
169
            $page = new ContentControllerTestPageWithoutController();
170
            $page->URLSegment = "test";
171
            $page->write();
172
            $page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
173
174
            $response = $this->get($page->RelativeLink());
175
            $this->assertEquals("ContentControllerTestPageWithoutController", trim($response->getBody()));
176
177
            // // This should fall over to user Page.ss
178
            $page = new ContentControllerTestPage();
179
            $page->URLSegment = "test";
180
            $page->write();
181
            $page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
182
183
            $response = $this->get($page->RelativeLink());
184
            $this->assertEquals("Page", trim($response->getBody()));
185
186
187
            // Test that the action template is rendered.
188
            $page = new ContentControllerTestPage();
189
            $page->URLSegment = "page-without-controller";
190
            $page->write();
191
            $page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
192
193
            $response = $this->get($page->RelativeLink("test"));
194
            $this->assertEquals("ContentControllerTestPage_test", trim($response->getBody()));
195
196
            // Test that an action without a template will default to the index template, which is
197
            // to say the default Page.ss template
198
            $response = $this->get($page->RelativeLink("testwithouttemplate"));
199
            $this->assertEquals("Page", trim($response->getBody()));
200
201
            // Test that an action with a template will render the both action template *and* the
202
            // correct parent template
203
            $controller = new ContentController($page);
204
            $viewer = $controller->getViewer('test');
205
            $this->assertEquals(
206
                __DIR__
207
                . '/themes/controllertest/templates/SilverStripe/CMS/Tests/Controllers/'
208
                . 'ContentControllerTestPage_test.ss',
209
                $viewer->templates()['main']
210
            );
211
        });
212
    }
213
}
214