2011-03-18 04:17:04 +01:00
|
|
|
<?php
|
2016-06-16 06:57:19 +02:00
|
|
|
|
2017-08-09 04:53:38 +02:00
|
|
|
namespace SilverStripe\CMS\Tests\Controllers;
|
2017-08-09 03:25:12 +02:00
|
|
|
|
2017-06-21 06:29:40 +02:00
|
|
|
use SilverStripe\CMS\Controllers\ContentController;
|
2016-07-22 01:32:32 +02:00
|
|
|
use SilverStripe\CMS\Controllers\RootURLController;
|
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Control\Director;
|
2016-09-09 01:26:24 +02:00
|
|
|
use SilverStripe\Control\HTTPResponse_Exception;
|
2017-06-21 06:29:40 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
2017-06-21 06:29:40 +02:00
|
|
|
use SilverStripe\Versioned\Versioned;
|
2017-08-09 03:25:12 +02:00
|
|
|
use Page;
|
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
class ContentControllerTest extends FunctionalTest
|
|
|
|
{
|
|
|
|
protected static $fixture_file = 'ContentControllerTest.yml';
|
2016-01-06 00:42:07 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
protected static $disable_themes = true;
|
2016-01-06 00:42:07 +01:00
|
|
|
|
2017-06-21 06:29:40 +02:00
|
|
|
protected static $extra_dataobjects = [
|
|
|
|
ContentControllerTest_Page::class,
|
|
|
|
ContentControllerTestPage::class,
|
|
|
|
ContentControllerTestPageWithoutController::class,
|
|
|
|
];
|
|
|
|
|
2021-10-27 23:40:52 +02:00
|
|
|
protected function setUp(): void
|
2018-03-21 05:44:24 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
Config::modify()->set(SiteTree::class, 'nested_urls', true);
|
|
|
|
|
|
|
|
// Ensure all pages are published
|
|
|
|
/** @var Page $page */
|
|
|
|
foreach (Page::get() as $page) {
|
|
|
|
$page->publishSingle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
/**
|
|
|
|
* Test that nested pages, basic actions, and nested/non-nested URL switching works properly
|
|
|
|
*/
|
2011-03-18 04:17:04 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
public function testNestedPages()
|
|
|
|
{
|
2017-07-17 07:19:00 +02:00
|
|
|
Config::modify()->set(SiteTree::class, 'nested_urls', true);
|
2011-03-18 04:17:04 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
$this->assertEquals('Home Page', $this->get('/')->getBody());
|
|
|
|
$this->assertEquals('Home Page', $this->get('/home/index/')->getBody());
|
|
|
|
$this->assertEquals('Home Page', $this->get('/home/second-index/')->getBody());
|
2011-03-18 04:17:04 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
$this->assertEquals('Second Level Page', $this->get('/home/second-level/')->getBody());
|
|
|
|
$this->assertEquals('Second Level Page', $this->get('/home/second-level/index/')->getBody());
|
|
|
|
$this->assertEquals('Second Level Page', $this->get('/home/second-level/second-index/')->getBody());
|
2011-03-18 04:17:04 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
$this->assertEquals('Third Level Page', $this->get('/home/second-level/third-level/')->getBody());
|
|
|
|
$this->assertEquals('Third Level Page', $this->get('/home/second-level/third-level/index/')->getBody());
|
|
|
|
$this->assertEquals('Third Level Page', $this->get('/home/second-level/third-level/second-index/')->getBody());
|
2011-03-18 04:17:04 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
RootURLController::reset();
|
2017-07-17 07:19:00 +02:00
|
|
|
Config::modify()->set(SiteTree::class, 'nested_urls', false);
|
2011-03-18 04:17:04 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
$this->assertEquals('Home Page', $this->get('/')->getBody());
|
|
|
|
$this->assertEquals('Home Page', $this->get('/home/')->getBody());
|
|
|
|
$this->assertEquals('Home Page', $this->get('/home/second-index/')->getBody());
|
2011-03-18 04:17:04 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
$this->assertEquals('Second Level Page', $this->get('/second-level/')->getBody());
|
|
|
|
$this->assertEquals('Second Level Page', $this->get('/second-level/index/')->getBody());
|
|
|
|
$this->assertEquals('Second Level Page', $this->get('/second-level/second-index/')->getBody());
|
2011-03-18 04:17:04 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
$this->assertEquals('Third Level Page', $this->get('/third-level/')->getBody());
|
|
|
|
$this->assertEquals('Third Level Page', $this->get('/third-level/index/')->getBody());
|
|
|
|
$this->assertEquals('Third Level Page', $this->get('/third-level/second-index/')->getBody());
|
|
|
|
}
|
2011-03-18 04:17:04 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
/**
|
|
|
|
* Tests {@link ContentController::ChildrenOf()}
|
|
|
|
*/
|
|
|
|
public function testChildrenOf()
|
|
|
|
{
|
|
|
|
$controller = new ContentController();
|
2011-03-18 04:17:04 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
$this->assertEquals(1, $controller->ChildrenOf('/')->Count());
|
|
|
|
$this->assertEquals(1, $controller->ChildrenOf('/home/')->Count());
|
|
|
|
$this->assertEquals(2, $controller->ChildrenOf('/home/second-level/')->Count());
|
|
|
|
$this->assertEquals(0, $controller->ChildrenOf('/home/second-level/third-level/')->Count());
|
|
|
|
|
2018-03-21 05:44:24 +01:00
|
|
|
SiteTree::config()->set('nested_urls', false);
|
2011-03-18 04:17:04 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
$this->assertEquals(1, $controller->ChildrenOf('/')->Count());
|
|
|
|
$this->assertEquals(1, $controller->ChildrenOf('/home/')->Count());
|
|
|
|
$this->assertEquals(2, $controller->ChildrenOf('/second-level/')->Count());
|
|
|
|
$this->assertEquals(0, $controller->ChildrenOf('/third-level/')->Count());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeepNestedURLs()
|
|
|
|
{
|
2011-03-18 04:17:04 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
$page = new Page();
|
|
|
|
$page->URLSegment = 'base-page';
|
|
|
|
$page->write();
|
2018-03-21 05:44:24 +01:00
|
|
|
$page->publishSingle();
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
for ($i = 0; $i < 10; $i++) {
|
|
|
|
$parentID = $page->ID;
|
2011-03-18 04:17:04 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
$page = new ContentControllerTest_Page();
|
|
|
|
$page->ParentID = $parentID;
|
2017-08-09 04:53:38 +02:00
|
|
|
$page->Title = "Page Level $i";
|
2017-01-25 21:59:25 +01:00
|
|
|
$page->URLSegment = "level-$i";
|
|
|
|
$page->write();
|
2018-03-21 05:44:24 +01:00
|
|
|
$page->publishSingle();
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
$relativeLink = Director::makeRelative($page->Link());
|
|
|
|
$this->assertEquals($page->Title, $this->get($relativeLink)->getBody());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testViewDraft()
|
|
|
|
{
|
|
|
|
// test when user does not have permission, should get login form
|
|
|
|
$this->logInWithPermission('EDITOR');
|
|
|
|
try {
|
|
|
|
$response = $this->get('/contact/?stage=Stage');
|
|
|
|
} catch (HTTPResponse_Exception $responseException) {
|
|
|
|
$response = $responseException->getResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEquals('403', $response->getstatusCode());
|
|
|
|
|
|
|
|
// test when user does have permission, should show page title and header ok.
|
|
|
|
$this->logInWithPermission('ADMIN');
|
2017-06-21 06:29:40 +02:00
|
|
|
$this->assertEquals('200', $this->get('contact/?stage=Stage')->getstatusCode());
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLinkShortcodes()
|
|
|
|
{
|
|
|
|
$linkedPage = new SiteTree();
|
|
|
|
$linkedPage->URLSegment = 'linked-page';
|
|
|
|
$linkedPage->write();
|
2018-04-06 05:53:57 +02:00
|
|
|
$linkedPage->publishSingle();
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
$page = new SiteTree();
|
|
|
|
$page->URLSegment = 'linking-page';
|
|
|
|
$page->Content = sprintf('<a href="[sitetree_link,id=%s]">Testlink</a>', $linkedPage->ID);
|
|
|
|
$page->write();
|
2018-04-06 05:53:57 +02:00
|
|
|
$page->publishSingle();
|
2017-01-25 21:59:25 +01:00
|
|
|
|
2018-03-21 05:44:24 +01:00
|
|
|
$link = $page->RelativeLink();
|
|
|
|
$response = $this->get($link);
|
2021-10-27 23:40:52 +02:00
|
|
|
$this->assertStringContainsString(
|
2017-01-25 21:59:25 +01:00
|
|
|
sprintf('<a href="%s">Testlink</a>', $linkedPage->Link()),
|
2018-03-21 05:44:24 +01:00
|
|
|
$response->getBody(),
|
2017-01-25 21:59:25 +01:00
|
|
|
'"sitetree_link" shortcodes get parsed properly'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that {@link ContentController::getViewer()} chooses the correct templates.
|
|
|
|
*
|
2017-08-09 04:53:38 +02:00
|
|
|
* @covers \SilverStripe\CMS\Controllers\ContentController::getViewer()
|
|
|
|
**/
|
2017-01-25 21:59:25 +01:00
|
|
|
public function testGetViewer()
|
|
|
|
{
|
2017-08-09 04:53:38 +02:00
|
|
|
$this->useTestTheme(__DIR__, 'controllertest', function () {
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
// Test a page without a controller (ContentControllerTest_PageWithoutController.ss)
|
|
|
|
$page = new ContentControllerTestPageWithoutController();
|
|
|
|
$page->URLSegment = "test";
|
|
|
|
$page->write();
|
|
|
|
$page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
|
|
|
|
2017-08-09 04:53:38 +02:00
|
|
|
$response = $this->get($page->RelativeLink());
|
2022-04-13 07:07:59 +02:00
|
|
|
$this->assertEquals("ContentControllerTestPageWithoutController", trim($response->getBody() ?? ''));
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
// // This should fall over to user Page.ss
|
|
|
|
$page = new ContentControllerTestPage();
|
|
|
|
$page->URLSegment = "test";
|
|
|
|
$page->write();
|
|
|
|
$page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
|
|
|
|
2017-08-09 04:53:38 +02:00
|
|
|
$response = $this->get($page->RelativeLink());
|
2022-04-13 07:07:59 +02:00
|
|
|
$this->assertEquals("Page", trim($response->getBody() ?? ''));
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
// Test that the action template is rendered.
|
|
|
|
$page = new ContentControllerTestPage();
|
|
|
|
$page->URLSegment = "page-without-controller";
|
|
|
|
$page->write();
|
|
|
|
$page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
|
|
|
|
2017-08-09 04:53:38 +02:00
|
|
|
$response = $this->get($page->RelativeLink("test"));
|
2022-04-13 07:07:59 +02:00
|
|
|
$this->assertEquals("ContentControllerTestPage_test", trim($response->getBody() ?? ''));
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
// Test that an action without a template will default to the index template, which is
|
|
|
|
// to say the default Page.ss template
|
2017-08-09 04:53:38 +02:00
|
|
|
$response = $this->get($page->RelativeLink("testwithouttemplate"));
|
2022-04-13 07:07:59 +02:00
|
|
|
$this->assertEquals("Page", trim($response->getBody() ?? ''));
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
// Test that an action with a template will render the both action template *and* the
|
|
|
|
// correct parent template
|
|
|
|
$controller = new ContentController($page);
|
|
|
|
$viewer = $controller->getViewer('test');
|
2017-08-09 04:53:38 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
__DIR__
|
|
|
|
. '/themes/controllertest/templates/SilverStripe/CMS/Tests/Controllers/'
|
|
|
|
. 'ContentControllerTestPage_test.ss',
|
|
|
|
$viewer->templates()['main']
|
|
|
|
);
|
2017-01-25 21:59:25 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|