BUGFIX Validate SiteTree::$can_be_root on saving, as well as for VirtualPage originals (AIR-80)

This commit is contained in:
Ingo Schommer 2012-01-14 11:20:54 +01:00
parent 062e3b6a0c
commit 0e4e8034d6
4 changed files with 95 additions and 1 deletions

View File

@ -1511,6 +1511,22 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
);
}
}
// "Can be root" validation
if(!$this->stat('can_be_root') && !$this->ParentID) {
$result->error(
sprintf(
_t(
'SiteTree.PageTypNotAllowedOnRoot',
'Page type "%s" is not allowed on the root level',
PR_MEDIUM,
'First argument is a class name'
),
$this->i18n_singular_name()
),
'CAN_BE_ROOT'
);
}
return $result;
}

View File

@ -244,6 +244,29 @@ class VirtualPage extends Page {
}
}
}
function validate() {
$result = parent::validate();
// "Can be root" validation
$orig = $this->CopyContentFrom();
if(!$orig->stat('can_be_root') && !$this->ParentID) {
$result->error(
sprintf(
_t(
'VirtualPage.PageTypNotAllowedOnRoot',
'Original page type "%s" is not allowed on the root level for this virtual page',
PR_MEDIUM,
'First argument is a class name'
),
$orig->i18n_singular_name()
),
'CAN_BE_ROOT_VIRTUAL'
);
}
return $result;
}
/**
* Ensure we have an up-to-date version of everything.

View File

@ -15,7 +15,8 @@ class SiteTreeTest extends SapphireTest {
'SiteTreeTest_ClassB',
'SiteTreeTest_ClassC',
'SiteTreeTest_ClassD',
'SiteTreeTest_ClassCext'
'SiteTreeTest_ClassCext',
'SiteTreeTest_NotRoot',
);
/**
@ -848,6 +849,24 @@ class SiteTreeTest extends SapphireTest {
Session::set("loggedInAs", null);
}
function testCanBeRoot() {
$page = new SiteTree();
$page->ParentID = 0;
$page->write();
$notRootPage = new SiteTreeTest_NotRoot();
$notRootPage->ParentID = 0;
$isDetected = false;
try {
$notRootPage->write();
} catch(ValidationException $e) {
$this->assertContains('is not allowed on the root level', $e->getMessage());
$isDetected = true;
}
if(!$isDetected) $this->fail('Fails validation with $can_be_root=false');
}
}
/**#@+
@ -905,4 +924,8 @@ class SiteTreeTest_ClassD extends Page implements TestOnly {
class SiteTreeTest_ClassCext extends SiteTreeTest_ClassC implements TestOnly {
// Override SiteTreeTest_ClassC definitions
static $allowed_children = array('SiteTreeTest_ClassB');
}
class SiteTreeTest_NotRoot extends Page implements TestOnly {
static $can_be_root = false;
}

View File

@ -6,6 +6,7 @@ class VirtualPageTest extends SapphireTest {
protected $extraDataObjects = array(
'VirtualPageTest_ClassA',
'VirtualPageTest_ClassB',
'VirtualPageTest_NotRoot',
);
/**
@ -466,6 +467,33 @@ class VirtualPageTest extends SapphireTest {
'write() on original page does increment version on related VirtualPage'
);
}
function testCanBeRoot() {
$page = new SiteTree();
$page->ParentID = 0;
$page->write();
$notRootPage = new VirtualPageTest_NotRoot();
// we don't want the original on root, but rather the VirtualPage pointing to it
$notRootPage->ParentID = $page->ID;
$notRootPage->write();
$virtual = new VirtualPage();
$virtual->CopyContentFromID = $page->ID;
$virtual->write();
$virtual = DataObject::get_by_id('VirtualPage', $virtual->ID, false);
$virtual->CopyContentFromID = $notRootPage->ID;
$isDetected = false;
try {
$virtual->write();
} catch(ValidationException $e) {
$this->assertContains('is not allowed on the root level', $e->getMessage());
$isDetected = true;
}
if(!$isDetected) $this->fail('Fails validation with $can_be_root=false');
}
}
class VirtualPageTest_ClassA extends Page implements TestOnly {
@ -485,4 +513,8 @@ class VirtualPageTest_ClassB extends Page implements TestOnly {
class VirtualPageTest_ClassC extends Page implements TestOnly {
static $allowed_children = array();
}
class VirtualPageTest_NotRoot extends Page implements TestOnly {
static $can_be_root = false;
}