Merge pull request #1525 from tractorcow/pulls/4.0/dupe-api

API Cleanup inconsistent SiteTree::duplicate API
This commit is contained in:
Daniel Hensby 2016-07-14 00:41:40 +01:00 committed by GitHub
commit e5aad336ce
2 changed files with 13 additions and 18 deletions

View File

@ -626,26 +626,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
* such behaviour.
* Reset Sort on duped page
*
* @param bool $doWrite Whether to write the new object before returning it
* @return self The duplicated object
* @param SiteTree $original
* @param bool $doWrite
*/
public function duplicate($doWrite = true) {
$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;
public function onBeforeDuplicate($original, $doWrite) {
$this->Sort = 0;
}
/**

View File

@ -378,6 +378,14 @@ class SiteTreeTest extends SapphireTest {
$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() {
Config::inst()->update('SiteTree', 'enforce_strict_hierarchy', false);
$this->logInWithPermission('ADMIN');