API Cleanup inconsistent SiteTree::duplicate API

This commit is contained in:
Damian Mooyman 2016-06-13 15:47:47 +12:00
parent 9feec1f1da
commit c50bc91772
2 changed files with 13 additions and 18 deletions

View File

@ -609,26 +609,13 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
} }
/** /**
* Create a duplicate of this node. Doesn't affect joined data - create a custom overloading of this if you need * Reset Sort on duped page
* such behaviour.
* *
* @param bool $doWrite Whether to write the new object before returning it * @param SiteTree $original
* @return self The duplicated object * @param bool $doWrite
*/ */
public function duplicate($doWrite = true) { public function onBeforeDuplicate($original, $doWrite) {
$this->Sort = 0;
$page = parent::duplicate(false);
$page->Sort = 0;
$this->invokeWithExtensions('onBeforeDuplicate', $page);
if($doWrite) {
$page->write();
$page = $this->duplicateManyManyRelations($this, $page);
}
$this->invokeWithExtensions('onAfterDuplicate', $page);
return $page;
} }
/** /**

View File

@ -368,6 +368,14 @@ class SiteTreeTest extends SapphireTest {
$this->assertFalse(DataObject::get_by_id('Page', $pageStaffDuplicate->ID)); $this->assertFalse(DataObject::get_by_id('Page', $pageStaffDuplicate->ID));
} }
public function testDuplicate() {
$pageAbout = $this->objFromFixture('Page', 'about');
$dupe = $pageAbout->duplicate();
$this->assertEquals($pageAbout->Title, $dupe->Title);
$this->assertNotEquals($pageAbout->URLSegment, $dupe->URLSegment);
$this->assertNotEquals($pageAbout->Sort, $dupe->Sort);
}
public function testDeleteFromLiveOperatesRecursively() { public function testDeleteFromLiveOperatesRecursively() {
Config::inst()->update('SiteTree', 'enforce_strict_hierarchy', false); Config::inst()->update('SiteTree', 'enforce_strict_hierarchy', false);
$this->logInWithPermission('ADMIN'); $this->logInWithPermission('ADMIN');