2011-03-18 04:17:04 +01:00
|
|
|
<?php
|
2016-06-16 06:57:19 +02:00
|
|
|
|
|
|
|
use SilverStripe\ORM\Versioning\Versioned;
|
2016-07-22 01:32:32 +02:00
|
|
|
use SilverStripe\CMS\Controllers\RootURLController;
|
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
|
|
|
use SilverStripe\CMS\Controllers\ContentController;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Control\Director;
|
2016-09-09 01:26:24 +02:00
|
|
|
use SilverStripe\Control\HTTPResponse_Exception;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
2016-06-16 06:57:19 +02:00
|
|
|
|
2011-03-18 04:17:04 +01:00
|
|
|
/**
|
2011-03-22 10:47:26 +01:00
|
|
|
* @package cms
|
2011-03-18 04:17:04 +01:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class ContentControllerTest extends FunctionalTest {
|
2016-01-06 00:42:07 +01:00
|
|
|
|
2013-03-18 11:47:15 +01:00
|
|
|
protected static $fixture_file = 'ContentControllerTest.yml';
|
2016-01-06 00:42:07 +01:00
|
|
|
|
2013-03-18 11:47:15 +01:00
|
|
|
protected static $use_draft_site = true;
|
2014-02-07 03:56:20 +01:00
|
|
|
|
|
|
|
protected static $disable_themes = true;
|
2016-01-06 00:42:07 +01:00
|
|
|
|
2011-03-18 04:17:04 +01:00
|
|
|
/**
|
|
|
|
* Test that nested pages, basic actions, and nested/non-nested URL switching works properly
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function testNestedPages() {
|
|
|
|
RootURLController::reset();
|
2016-07-22 01:32:32 +02:00
|
|
|
Config::inst()->update('SilverStripe\\CMS\\Model\\SiteTree', 'nested_urls', true);
|
2011-03-18 04:17:04 +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());
|
|
|
|
|
|
|
|
$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());
|
|
|
|
|
|
|
|
$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());
|
|
|
|
|
|
|
|
RootURLController::reset();
|
2013-03-18 11:47:15 +01:00
|
|
|
SiteTree::config()->nested_urls = false;
|
2011-03-18 04:17:04 +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());
|
|
|
|
|
|
|
|
$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());
|
|
|
|
|
|
|
|
$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());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests {@link ContentController::ChildrenOf()}
|
|
|
|
*/
|
|
|
|
public function testChildrenOf() {
|
|
|
|
$controller = new ContentController();
|
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
Config::inst()->update('SilverStripe\\CMS\\Model\\SiteTree', 'nested_urls', true);
|
2011-03-18 04:17:04 +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());
|
|
|
|
|
2013-03-18 11:47:15 +01:00
|
|
|
SiteTree::config()->nested_urls = false;
|
2011-03-18 04:17:04 +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() {
|
2016-07-22 01:32:32 +02:00
|
|
|
Config::inst()->update('SilverStripe\\CMS\\Model\\SiteTree', 'nested_urls', true);
|
2011-03-18 04:17:04 +01:00
|
|
|
|
|
|
|
$page = new Page();
|
|
|
|
$page->URLSegment = 'base-page';
|
|
|
|
$page->write();
|
|
|
|
|
|
|
|
for($i = 0; $i < 10; $i++) {
|
|
|
|
$parentID = $page->ID;
|
|
|
|
|
|
|
|
$page = new ContentControllerTest_Page();
|
|
|
|
$page->ParentID = $parentID;
|
|
|
|
$page->Title = "Page Level $i";
|
|
|
|
$page->URLSegment = "level-$i";
|
|
|
|
$page->write();
|
|
|
|
|
|
|
|
$relativeLink = Director::makeRelative($page->Link());
|
|
|
|
$this->assertEquals($page->Title, $this->get($relativeLink)->getBody());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-18 11:47:15 +01:00
|
|
|
SiteTree::config()->nested_urls = false;
|
2011-03-18 04:17:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testViewDraft(){
|
2016-01-06 00:42:07 +01:00
|
|
|
|
2011-03-18 04:17:04 +01:00
|
|
|
// test when user does not have permission, should get login form
|
|
|
|
$this->logInWithPermission('EDITOR');
|
2015-12-22 05:46:07 +01:00
|
|
|
try {
|
|
|
|
$response = $this->get('/contact/?stage=Stage');
|
2016-09-09 01:26:24 +02:00
|
|
|
} catch(HTTPResponse_Exception $responseException) {
|
2015-12-22 05:46:07 +01:00
|
|
|
$response = $responseException->getResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEquals('403', $response->getstatusCode());
|
2016-01-06 00:42:07 +01:00
|
|
|
|
|
|
|
|
2011-03-18 04:17:04 +01:00
|
|
|
// test when user does have permission, should show page title and header ok.
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
$this->assertEquals('200', $this->get('/contact/?stage=Stage')->getstatusCode());
|
2016-01-06 00:42:07 +01:00
|
|
|
|
|
|
|
|
2011-03-18 04:17:04 +01:00
|
|
|
}
|
2016-01-06 00:42:07 +01:00
|
|
|
|
2011-03-18 04:17:04 +01:00
|
|
|
public function testLinkShortcodes() {
|
|
|
|
$linkedPage = new SiteTree();
|
|
|
|
$linkedPage->URLSegment = 'linked-page';
|
|
|
|
$linkedPage->write();
|
2016-04-01 05:17:37 +02:00
|
|
|
$linkedPage->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2016-01-06 00:42:07 +01:00
|
|
|
|
2011-03-18 04:17:04 +01:00
|
|
|
$page = new SiteTree();
|
|
|
|
$page->URLSegment = 'linking-page';
|
2012-07-12 04:51:19 +02:00
|
|
|
$page->Content = sprintf('<a href="[sitetree_link,id=%s]">Testlink</a>', $linkedPage->ID);
|
2011-03-18 04:17:04 +01:00
|
|
|
$page->write();
|
2016-04-01 05:17:37 +02:00
|
|
|
$page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2011-03-18 04:17:04 +01:00
|
|
|
|
|
|
|
$this->assertContains(
|
|
|
|
sprintf('<a href="%s">Testlink</a>', $linkedPage->Link()),
|
|
|
|
$this->get($page->RelativeLink())->getBody(),
|
|
|
|
'"sitetree_link" shortcodes get parsed properly'
|
|
|
|
);
|
2016-01-06 00:42:07 +01:00
|
|
|
}
|
2013-12-20 21:52:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that {@link ContentController::getViewer()} chooses the correct templates.
|
|
|
|
*
|
2016-11-21 03:17:25 +01:00
|
|
|
* @covers SilverStripe\CMS\Controllers\ContentController::getViewer()
|
2013-12-20 21:52:11 +01:00
|
|
|
**/
|
|
|
|
public function testGetViewer() {
|
2016-01-06 00:42:07 +01:00
|
|
|
|
2014-03-25 05:14:05 +01:00
|
|
|
$self = $this;
|
|
|
|
$this->useTestTheme(dirname(__FILE__), 'controllertest', function() use ($self) {
|
2013-12-20 21:52:11 +01:00
|
|
|
|
2014-03-25 05:14:05 +01:00
|
|
|
// Test a page without a controller (ContentControllerTest_PageWithoutController.ss)
|
|
|
|
$page = new ContentControllerTestPageWithoutController();
|
|
|
|
$page->URLSegment = "test";
|
|
|
|
$page->write();
|
2016-04-01 05:17:37 +02:00
|
|
|
$page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2014-03-25 05:14:05 +01:00
|
|
|
|
|
|
|
$response = $self->get($page->RelativeLink());
|
2016-01-06 22:34:01 +01:00
|
|
|
$self->assertEquals("ContentControllerTestPageWithoutController", trim($response->getBody()));
|
|
|
|
|
2014-03-25 05:14:05 +01:00
|
|
|
// // This should fall over to user Page.ss
|
|
|
|
$page = new ContentControllerTestPage();
|
|
|
|
$page->URLSegment = "test";
|
|
|
|
$page->write();
|
2016-04-01 05:17:37 +02:00
|
|
|
$page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2013-12-20 21:52:11 +01:00
|
|
|
|
2014-03-25 05:14:05 +01:00
|
|
|
$response = $self->get($page->RelativeLink());
|
2016-01-06 22:34:01 +01:00
|
|
|
$self->assertEquals("Page", trim($response->getBody()));
|
2013-12-20 21:52:11 +01:00
|
|
|
|
|
|
|
|
2014-03-25 05:14:05 +01:00
|
|
|
// Test that the action template is rendered.
|
|
|
|
$page = new ContentControllerTestPage();
|
|
|
|
$page->URLSegment = "page-without-controller";
|
|
|
|
$page->write();
|
2016-04-01 05:17:37 +02:00
|
|
|
$page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2013-12-20 21:52:11 +01:00
|
|
|
|
2014-03-25 05:14:05 +01:00
|
|
|
$response = $self->get($page->RelativeLink("test"));
|
2016-01-06 22:34:01 +01:00
|
|
|
$self->assertEquals("ContentControllerTestPage_test", trim($response->getBody()));
|
2014-02-14 05:44:53 +01:00
|
|
|
|
2014-03-25 05:14:05 +01:00
|
|
|
// Test that an action without a template will default to the index template, which is
|
|
|
|
// to say the default Page.ss template
|
|
|
|
$response = $self->get($page->RelativeLink("testwithouttemplate"));
|
2016-01-06 22:34:01 +01:00
|
|
|
$self->assertEquals("Page", trim($response->getBody()));
|
2015-07-23 19:24:13 +02: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');
|
|
|
|
$templateList = array('ContentControllerTestPage_test', 'Page');
|
2016-07-15 07:13:53 +02:00
|
|
|
$self->assertEquals(dirname(__FILE__).'/themes/controllertest/templates/ContentControllerTestPage_test.ss', $viewer->templates()['main']);
|
2014-03-25 05:14:05 +01:00
|
|
|
});
|
2011-03-18 04:17:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class ContentControllerTest_Page extends Page { }
|
|
|
|
|
|
|
|
class ContentControllerTest_Page_Controller extends Page_Controller {
|
|
|
|
|
2013-03-18 11:47:15 +01:00
|
|
|
private static $allowed_actions = array (
|
2011-03-18 04:17:04 +01:00
|
|
|
'second_index'
|
|
|
|
);
|
|
|
|
|
|
|
|
public function index() {
|
|
|
|
return $this->Title;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function second_index() {
|
|
|
|
return $this->index();
|
|
|
|
}
|
|
|
|
|
2012-04-12 09:23:20 +02:00
|
|
|
}
|
2013-12-20 21:52:11 +01:00
|
|
|
|
|
|
|
// For testing templates
|
|
|
|
class ContentControllerTestPageWithoutController extends Page { }
|
|
|
|
|
|
|
|
class ContentControllerTestPage extends Page { }
|
|
|
|
class ContentControllerTestPage_Controller extends Page_Controller {
|
|
|
|
private static $allowed_actions = array(
|
|
|
|
"test",
|
2014-02-14 05:44:53 +01:00
|
|
|
"testwithouttemplate"
|
2013-12-20 21:52:11 +01:00
|
|
|
);
|
2014-02-14 05:44:53 +01:00
|
|
|
|
|
|
|
function testwithouttemplate() {
|
|
|
|
return array();
|
|
|
|
}
|
2013-12-20 21:52:11 +01:00
|
|
|
}
|