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