MINOR Using BlankPage template in SecurityTest, BasicAuthTest to remove ContentController dependency

MINOR Checking for SiteTree class existence in Security, Translatable
MINOR Checking for ContentController existence in FulltextSearchable
MINOR Removed unnecessary ContentController tests from ObjectTest
MINOR Replaced CMS specific examples in PermissionCheckboxSetFieldTest, DataObjectTest
MINOR Changed SecurityTest to make assertions against Security/login rather than relying on redirection from admin/cms
This commit is contained in:
Paul Meyrick 2011-03-18 15:01:09 +13:00 committed by Ingo Schommer
parent 7f06f97761
commit dc36725869
14 changed files with 147 additions and 129 deletions

View File

@ -453,7 +453,7 @@ class Translatable extends DataObjectDecorator implements PermissionProvider {
* @deprecated 2.4 Use Object::add_extension('SiteTree', 'Translatable') * @deprecated 2.4 Use Object::add_extension('SiteTree', 'Translatable')
*/ */
static function enable() { static function enable() {
Object::add_extension('SiteTree', 'Translatable'); if(class_exists('SiteTree')) Object::add_extension('SiteTree', 'Translatable');
} }
/** /**
@ -462,7 +462,7 @@ class Translatable extends DataObjectDecorator implements PermissionProvider {
* @deprecated 2.4 Use Object::remove_extension('SiteTree', 'Translatable') * @deprecated 2.4 Use Object::remove_extension('SiteTree', 'Translatable')
*/ */
static function disable() { static function disable() {
Object::remove_extension('SiteTree', 'Translatable'); if(class_exists('SiteTree')) Object::remove_extension('SiteTree', 'Translatable');
} }
/** /**
@ -472,7 +472,11 @@ class Translatable extends DataObjectDecorator implements PermissionProvider {
* @return boolean True if enabled * @return boolean True if enabled
*/ */
static function is_enabled() { static function is_enabled() {
return Object::has_extension('SiteTree', 'Translatable'); if(class_exists('SiteTree')){
return Object::has_extension('SiteTree', 'Translatable');
}else{
return false;
}
} }
@ -620,7 +624,7 @@ class Translatable extends DataObjectDecorator implements PermissionProvider {
))->column(); ))->column();
if(!$idsWithoutLocale) return; if(!$idsWithoutLocale) return;
if($this->owner->class == 'SiteTree') { if(class_exists('SiteTree') && $this->owner->class == 'SiteTree') {
foreach(array('Stage', 'Live') as $stage) { foreach(array('Stage', 'Live') as $stage) {
foreach($idsWithoutLocale as $id) { foreach($idsWithoutLocale as $id) {
$obj = Versioned::get_one_by_stage( $obj = Versioned::get_one_by_stage(
@ -779,7 +783,7 @@ class Translatable extends DataObjectDecorator implements PermissionProvider {
// Caution: This logic is very sensitve to infinite loops when translation status isn't determined properly // Caution: This logic is very sensitve to infinite loops when translation status isn't determined properly
// If a parent for the newly written translation was existing before this // If a parent for the newly written translation was existing before this
// onBeforeWrite() call, it will already have been linked correctly through createTranslation() // onBeforeWrite() call, it will already have been linked correctly through createTranslation()
if($this->owner->hasField('ParentID') && $this->owner instanceof SiteTree) { if(class_exists('SiteTree') && $this->owner->hasField('ParentID') && $this->owner instanceof SiteTree) {
if( if(
!$this->owner->ID !$this->owner->ID
&& $this->owner->ParentID && $this->owner->ParentID
@ -898,16 +902,19 @@ class Translatable extends DataObjectDecorator implements PermissionProvider {
* seeing readonly fields as well. * seeing readonly fields as well.
*/ */
function updateCMSFields(FieldSet &$fields) { function updateCMSFields(FieldSet &$fields) {
if(!class_exists('SiteTree')) return;
// Don't apply these modifications for normal DataObjects - they rely on CMSMain logic // Don't apply these modifications for normal DataObjects - they rely on CMSMain logic
if(!($this->owner instanceof SiteTree)) return; if(!($this->owner instanceof SiteTree)) return;
// used in CMSMain->init() to set language state when reading/writing record // used in CMSMain->init() to set language state when reading/writing record
$fields->push(new HiddenField("Locale", "Locale", $this->owner->Locale) ); $fields->push(new HiddenField("Locale", "Locale", $this->owner->Locale) );
// Don't allow translation of virtual pages because of data inconsistencies (see #5000) // Don't allow translation of virtual pages because of data inconsistencies (see #5000)
$excludedPageTypes = array('VirtualPage'); if(class_exists('VirtualPage')){
foreach($excludedPageTypes as $excludedPageType) { $excludedPageTypes = array('VirtualPage');
if(is_a($this->owner, $excludedPageType)) return; foreach($excludedPageTypes as $excludedPageType) {
if(is_a($this->owner, $excludedPageType)) return;
}
} }
$excludeFields = array( $excludeFields = array(
@ -1271,7 +1278,7 @@ class Translatable extends DataObjectDecorator implements PermissionProvider {
} }
function providePermissions() { function providePermissions() {
if(!Object::has_extension('SiteTree', 'Translatable')) return false; if(!Object::has_extension('SiteTree', 'Translatable') || !class_exists('SiteTree')) return false;
$locales = self::get_allowed_locales(); $locales = self::get_allowed_locales();
@ -1324,23 +1331,6 @@ class Translatable extends DataObjectDecorator implements PermissionProvider {
return $returnMap; return $returnMap;
} }
/**
* Get the RelativeLink value for a home page in another locale. This is found by searching for the default home
* page in the default language, then returning the link to the translated version (if one exists).
*
* @return string
*/
public static function get_homepage_link_by_locale($locale) {
$originalLocale = self::get_current_locale();
self::set_current_locale(self::default_locale());
$original = SiteTree::get_by_link(RootURLController::get_default_homepage_link());
self::set_current_locale($originalLocale);
if($original) {
if($translation = $original->getTranslation($locale)) return trim($translation->RelativeLink(true), '/');
}
}
/** /**
* @deprecated 2.4 Use {@link Translatable::get_homepage_link_by_locale()} * @deprecated 2.4 Use {@link Translatable::get_homepage_link_by_locale()}

View File

@ -335,12 +335,13 @@ class Debug {
if(Director::is_ajax()) { if(Director::is_ajax()) {
echo $friendlyErrorMessage; echo $friendlyErrorMessage;
} else { } else {
$errorFilePath = ErrorPage::get_filepath_for_errorcode($statusCode, Translatable::get_current_locale()); if(class_exists('ErrorPage')){
if(file_exists($errorFilePath)) { $errorFilePath = ErrorPage::get_filepath_for_errorcode($statusCode, Translatable::get_current_locale());
$content = file_get_contents(ASSETS_PATH . "/error-$statusCode.html"); if(file_exists($errorFilePath)) {
// $BaseURL is left dynamic in error-###.html, so that multi-domain sites don't get broken $content = file_get_contents(ASSETS_PATH . "/error-$statusCode.html");
echo str_replace('$BaseURL', Director::absoluteBaseURL(), $content); // $BaseURL is left dynamic in error-###.html, so that multi-domain sites don't get broken
echo str_replace('$BaseURL', Director::absoluteBaseURL(), $content);
}
} else { } else {
$renderer = new DebugView(); $renderer = new DebugView();
$renderer->writeHeader(); $renderer->writeHeader();

View File

@ -52,8 +52,9 @@ class FulltextSearchable extends DataObjectDecorator {
} }
} }
self::$searchable_classes = $searchableClasses; self::$searchable_classes = $searchableClasses;
if(class_exists("ContentController")){
Object::add_extension("ContentController", "ContentControllerSearchExtension"); Object::add_extension("ContentController", "ContentControllerSearchExtension");
}
} }
/** /**

View File

@ -417,7 +417,7 @@ class Security extends Controller {
Session::clear('Security.Message'); Session::clear('Security.Message');
// custom processing // custom processing
return $customisedController->renderWith(array('Security_login', 'Security', $this->stat('template_main'), 'ContentController')); return $customisedController->renderWith(array('Security_login', 'Security', $this->stat('template_main'), 'BlankPage'));
} }
function basicauthlogin() { function basicauthlogin() {
@ -455,7 +455,7 @@ class Security extends Controller {
)); ));
//Controller::$currentController = $controller; //Controller::$currentController = $controller;
return $customisedController->renderWith(array('Security_lostpassword', 'Security', $this->stat('template_main'), 'ContentController')); return $customisedController->renderWith(array('Security_lostpassword', 'Security', $this->stat('template_main'), 'BlankPage'));
} }
@ -514,7 +514,7 @@ class Security extends Controller {
)); ));
//Controller::$currentController = $controller; //Controller::$currentController = $controller;
return $customisedController->renderWith(array('Security_passwordsent', 'Security', $this->stat('template_main'), 'ContentController')); return $customisedController->renderWith(array('Security_passwordsent', 'Security', $this->stat('template_main'), 'BlankPage'));
} }
@ -594,7 +594,7 @@ class Security extends Controller {
} }
} }
return $customisedController->renderWith(array('Security_changepassword', 'Security', $this->stat('template_main'), 'ContentController')); return $customisedController->renderWith(array('Security_changepassword', 'Security', $this->stat('template_main'), 'BlankPage'));
} }
/** /**

15
templates/BlankPage.ss Normal file
View File

@ -0,0 +1,15 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<title>$Title</title>
<% base_tag %>
</head>
<body class="$CSSClasses">
$Content
<div class="right">
$Form
</div>
</body>
</html>

View File

@ -697,22 +697,20 @@ class DataObjectTest extends SapphireTest {
} }
function testNewClassInstance() { function testNewClassInstance() {
$page = $this->objFromFixture('Page', 'page1'); $dataObject = $this->objFromFixture('DataObjectTest_TeamComment', 'comment1');
$changedPage = $page->newClassInstance('RedirectorPage'); $changedDO = $dataObject->newClassInstance('File');
$changedFields = $changedPage->getChangedFields(); $changedFields = $changedDO->getChangedFields();
// Don't write the record, it will reset changed fields // Don't write the record, it will reset changed fields
$this->assertType('RedirectorPage', $changedPage); $this->assertType('File', $changedDO);
$this->assertEquals($changedPage->ClassName, 'RedirectorPage'); $this->assertEquals($changedDO->ClassName, 'File');
$this->assertEquals($changedPage->RedirectionType, 'Internal');
//$this->assertEquals($changedPage->RecordClassName, 'RedirectorPage');
$this->assertContains('ClassName', array_keys($changedFields)); $this->assertContains('ClassName', array_keys($changedFields));
$this->assertEquals($changedFields['ClassName']['before'], 'Page'); $this->assertEquals($changedFields['ClassName']['before'], 'DataObjectTest_TeamComment');
$this->assertEquals($changedFields['ClassName']['after'], 'RedirectorPage'); $this->assertEquals($changedFields['ClassName']['after'], 'File');
$changedPage->write(); $changedDO->write();
$this->assertType('RedirectorPage', $changedPage); $this->assertType('File', $changedDO);
$this->assertEquals($changedPage->ClassName, 'RedirectorPage'); $this->assertEquals($changedDO->ClassName, 'File');
} }
function testManyManyExtraFields() { function testManyManyExtraFields() {

View File

@ -1,54 +1,54 @@
Page: Page:
home: home:
Title: Home Title: Home
page1: page1:
Title: First Page Title: First Page
Content: <p>Some test content</p> Content: <p>Some test content</p>
page2: page2:
Title: Second Page Title: Second Page
DataObjectTest_Team: DataObjectTest_Team:
team1: team1:
Title: Team 1 Title: Team 1
team2: team2:
Title: Team 2 Title: Team 2
DataObjectTest_Player: DataObjectTest_Player:
captain1: captain1:
FirstName: Captain 1 FirstName: Captain
FavouriteTeam: =>DataObjectTest_Team.team1 FavouriteTeam: =>DataObjectTest_Team.team1
Teams: =>DataObjectTest_Team.team1 Teams: =>DataObjectTest_Team.team1
captain2: captain2:
FirstName: Captain 2 FirstName: Captain 2
Teams: =>DataObjectTest_Team.team2 Teams: =>DataObjectTest_Team.team2
player1: player1:
FirstName: Player 1 FirstName: Player 1
player2: player2:
FirstName: Player 2 FirstName: Player 2
Teams: =>DataObjectTest_Team.team1,=>DataObjectTest_Team.team2 Teams: =>DataObjectTest_Team.team1,=>DataObjectTest_Team.team2
DataObjectTest_SubTeam: DataObjectTest_SubTeam:
subteam1: subteam1:
Title: Subteam 1 Title: Subteam 1
SubclassDatabaseField: Subclassed 1 SubclassDatabaseField: Subclassed 1
DecoratedDatabaseField: Decorated 1 DecoratedDatabaseField: Decorated 1
subteam2_with_player_relation: subteam2_with_player_relation:
Title: Subteam 2 Title: Subteam 2
SubclassDatabaseField: Subclassed 2 SubclassDatabaseField: Subclassed 2
DecoratedHasOneRelationship: =>DataObjectTest_Player.player1 DecoratedHasOneRelationship: =>DataObjectTest_Player.player1
subteam3_with_empty_fields: subteam3_with_empty_fields:
Title: Subteam 3 Title: Subteam 3
DataObjectTest_TeamComment: DataObjectTest_TeamComment:
comment1: comment1:
Name: Joe Name: Joe
Comment: This is a team comment by Joe Comment: This is a team comment by Joe
Team: =>DataObjectTest_Team.team1 Team: =>DataObjectTest_Team.team1
comment2: comment2:
Name: Bob Name: Bob
Comment: This is a team comment by Bob Comment: This is a team comment by Bob
Team: =>DataObjectTest_Team.team1 Team: =>DataObjectTest_Team.team1
comment3: comment3:
Name: Phil Name: Phil
Comment: Phil is a unique guy, and comments on team2 Comment: Phil is a unique guy, and comments on team2
Team: =>DataObjectTest_Team.team2 Team: =>DataObjectTest_Team.team2

View File

@ -20,7 +20,6 @@ class ObjectTest extends SapphireTest {
/* SiteTree should have all of the methods that Versioned has, because Versioned is listed in SiteTree's /* SiteTree should have all of the methods that Versioned has, because Versioned is listed in SiteTree's
* extensions */ * extensions */
$st = new SiteTree(); $st = new SiteTree();
$cc = new ContentController($st);
$this->assertTrue($st->hasMethod('publish'), "Test SiteTree has publish"); $this->assertTrue($st->hasMethod('publish'), "Test SiteTree has publish");
$this->assertTrue($st->hasMethod('migrateVersion'), "Test SiteTree has migrateVersion"); $this->assertTrue($st->hasMethod('migrateVersion'), "Test SiteTree has migrateVersion");
@ -29,14 +28,6 @@ class ObjectTest extends SapphireTest {
$this->assertTrue($st->hasMethod('PuBliSh'), "Test SiteTree has PuBliSh"); $this->assertTrue($st->hasMethod('PuBliSh'), "Test SiteTree has PuBliSh");
$this->assertTrue($st->hasMethod('MiGratEVersIOn'), "Test SiteTree has MiGratEVersIOn"); $this->assertTrue($st->hasMethod('MiGratEVersIOn'), "Test SiteTree has MiGratEVersIOn");
/* In a similar manner, all of SiteTree's methods should be available on ContentController, because $failover is set */
$this->assertTrue($cc->hasMethod('canView'), "Test ContentController has canView");
$this->assertTrue($cc->hasMethod('linkorcurrent'), "Test ContentController has linkorcurrent");
/* This 'method copying' is transitive, so all of Versioned's methods should be available on ContentControler.
* Once again, this is case-insensitive */
$this->assertTrue($cc->hasMethod('MiGratEVersIOn'), "Test ContentController has MiGratEVersIOn");
/* The above examples make use of SiteTree, Versioned and ContentController. Let's test defineMethods() more /* The above examples make use of SiteTree, Versioned and ContentController. Let's test defineMethods() more
* directly, with some sample objects */ * directly, with some sample objects */
$objs = array(); $objs = array();

View File

@ -317,7 +317,10 @@ class RequestHandlingTest_Controller extends Controller implements TestOnly {
public function throwhttperror() { public function throwhttperror() {
$this->httpError(404, 'This page does not exist.'); $this->httpError(404, 'This page does not exist.');
} }
public function getViewer(){
return new SSViewer('ContentController');
}
} }
class RequestHandlingTest_FormActionController extends Controller { class RequestHandlingTest_FormActionController extends Controller {
@ -369,6 +372,11 @@ class RequestHandlingTest_FormActionController extends Controller {
function formactionInAllowedActions($data, $form = null) { function formactionInAllowedActions($data, $form = null) {
return 'formactionInAllowedActions'; return 'formactionInAllowedActions';
} }
public function getViewer(){
return new SSViewer('ContentController');
}
} }
/** /**

View File

@ -422,6 +422,11 @@ class FormTest_Controller extends Controller implements TestOnly {
$form->sessionMessage('Test save was successful', 'good'); $form->sessionMessage('Test save was successful', 'good');
return $this->redirectBack(); return $this->redirectBack();
} }
function getViewer(){
return new SSViewer('ContentController');
}
} }
class FormTest_ControllerWithSecurityToken extends Controller implements TestOnly { class FormTest_ControllerWithSecurityToken extends Controller implements TestOnly {
@ -454,6 +459,10 @@ class FormTest_ControllerWithSecurityToken extends Controller implements TestOnl
$form->sessionMessage('Test save was successful', 'good'); $form->sessionMessage('Test save was successful', 'good');
return $this->redirectBack(); return $this->redirectBack();
} }
function getViewer(){
return new SSViewer('ContentController');
}
} }
Director::addRules(50, array( Director::addRules(50, array(

View File

@ -107,34 +107,38 @@ class BasicAuthTest extends FunctionalTest {
} }
class BasicAuthTest_ControllerSecuredWithPermission extends ContentController implements TestOnly { class BasicAuthTest_ControllerSecuredWithPermission extends Controller implements TestOnly {
static $post_init_called = false; static $post_init_called = false;
static $index_called = false; static $index_called = false;
protected $template = '../sapphire/templates/BlankPage.ss';
function init() { function init() {
self::$post_init_called = false; self::$post_init_called = false;
self::$index_called = false; self::$index_called = false;
BasicAuth::protect_entire_site(true, 'MYCODE'); BasicAuth::protect_entire_site(true, 'MYCODE');
parent::init(); parent::init();
self::$post_init_called = true; self::$post_init_called = true;
} }
function index() { function index() {
self::$index_called = true; self::$index_called = true;
} }
} }
class BasicAuthTest_ControllerSecuredWithoutPermission extends ContentController implements TestOnly { class BasicAuthTest_ControllerSecuredWithoutPermission extends Controller implements TestOnly {
protected $template = '../sapphire/templates/BlankPage.ss';
function init() { function init() {
BasicAuth::protect_entire_site(true, null); BasicAuth::protect_entire_site(true, null);
parent::init(); parent::init();
} }

View File

@ -14,14 +14,14 @@ class PermissionCheckboxSetFieldTest extends SapphireTest {
'GroupID' 'GroupID'
); );
$f->setHiddenPermissions( $f->setHiddenPermissions(
array('CMS_ACCESS_ReportAdmin') array('NON-ADMIN')
); );
$this->assertEquals( $this->assertEquals(
$f->getHiddenPermissions(), $f->getHiddenPermissions(),
array('CMS_ACCESS_ReportAdmin') array('NON-ADMIN')
); );
$this->assertContains('CMS_ACCESS_CMSMain', $f->Field()); $this->assertContains('ADMIN', $f->Field());
$this->assertNotContains('CMS_ACCESS_ReportAdmin', $f->Field()); $this->assertNotContains('NON-ADMIN', $f->Field());
} }
function testSaveInto() { function testSaveInto() {
@ -53,7 +53,7 @@ class PermissionCheckboxSetFieldTest extends SapphireTest {
// add some permissions // add some permissions
$field->setValue(array( $field->setValue(array(
'ADMIN'=>true, 'ADMIN'=>true,
'CMS_ACCESS_AssetAdmin'=>true 'NON-ADMIN'=>true
)); ));
$field->saveInto($group); $field->saveInto($group);
@ -61,7 +61,7 @@ class PermissionCheckboxSetFieldTest extends SapphireTest {
$untouchable->flushCache(); $untouchable->flushCache();
$this->assertEquals($group->Permissions()->Count(), 2, 'The tested group has two permissions permission'); $this->assertEquals($group->Permissions()->Count(), 2, 'The tested group has two permissions permission');
$this->assertEquals($group->Permissions("\"Code\"='ADMIN'")->Count(), 1, 'The tested group has ADMIN permission'); $this->assertEquals($group->Permissions("\"Code\"='ADMIN'")->Count(), 1, 'The tested group has ADMIN permission');
$this->assertEquals($group->Permissions("\"Code\"='CMS_ACCESS_AssetAdmin'")->Count(), 1, 'The tested group has CMS_ACCESS_AssetAdmin permission'); $this->assertEquals($group->Permissions("\"Code\"='NON-ADMIN'")->Count(), 1, 'The tested group has CMS_ACCESS_AssetAdmin permission');
$this->assertEquals($untouchable->Permissions()->Count(), 1, 'The other group has one permission'); $this->assertEquals($untouchable->Permissions()->Count(), 1, 'The other group has one permission');
$this->assertEquals($untouchable->Permissions("\"Code\"='ADMIN'")->Count(), 1, 'The other group has ADMIN permission'); $this->assertEquals($untouchable->Permissions("\"Code\"='ADMIN'")->Count(), 1, 'The other group has ADMIN permission');

View File

@ -1,10 +1,11 @@
Group: Group:
group: group:
Code: group Code: group
untouchable: untouchable:
Code: untouchable Code: untouchable
Permission: Permission:
perm1: perm1:
Code: ADMIN Code: ADMIN
Group: =>Group.untouchable Group: =>Group.untouchable
perm2:
Code: NON-ADMIN

View File

@ -59,7 +59,7 @@ class SecurityTest extends FunctionalTest {
$this->session()->inst_set('loggedInAs', $member->ID); $this->session()->inst_set('loggedInAs', $member->ID);
/* View the Security/login page */ /* View the Security/login page */
$this->get('Security/login'); $response = $this->get('Security/login');
$items = $this->cssParser()->getBySelector('#MemberLoginForm_LoginForm input.action'); $items = $this->cssParser()->getBySelector('#MemberLoginForm_LoginForm input.action');
@ -93,7 +93,7 @@ class SecurityTest extends FunctionalTest {
$this->autoFollowRedirection = true; $this->autoFollowRedirection = true;
/* Attempt to get into the admin section */ /* Attempt to get into the admin section */
$response = $this->get('admin/cms/'); $response = $this->get('Security/login/');
$items = $this->cssParser()->getBySelector('#MemberLoginForm_LoginForm input.text'); $items = $this->cssParser()->getBySelector('#MemberLoginForm_LoginForm input.text');