FIX Sort order for duplicated child pages is now retained

This commit is contained in:
Daniel Hensby 2016-10-03 18:10:41 +01:00 committed by Daniel Hensby
parent c754882e0a
commit 98d95cd707
No known key found for this signature in database
GPG Key ID: 229831A941962E26
2 changed files with 36 additions and 0 deletions

View File

@ -699,11 +699,14 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
$children = $this->AllChildren(); $children = $this->AllChildren();
if($children) { if($children) {
$sort = 0;
foreach($children as $child) { foreach($children as $child) {
$childClone = method_exists($child, 'duplicateWithChildren') $childClone = method_exists($child, 'duplicateWithChildren')
? $child->duplicateWithChildren() ? $child->duplicateWithChildren()
: $child->duplicate(); : $child->duplicate();
$childClone->ParentID = $clone->ID; $childClone->ParentID = $clone->ID;
//retain sort order by manually setting sort values
$childClone->Sort = ++$sort;
$childClone->write(); $childClone->write();
} }
} }

View File

@ -342,6 +342,39 @@ class SiteTreeTest extends SapphireTest {
$this->assertStringEndsWith('changed-on-live/my-staff/?stage=Live', $child->getAbsoluteLiveLink()); $this->assertStringEndsWith('changed-on-live/my-staff/?stage=Live', $child->getAbsoluteLiveLink());
} }
public function testDuplicateChildrenRetainSort() {
$parent = new Page();
$parent->Title = 'Parent';
$parent->write();
$child1 = new Page();
$child1->ParentID = $parent->ID;
$child1->Title = 'Child 1';
$child1->Sort = 2;
$child1->write();
$child2 = new Page();
$child2->ParentID = $parent->ID;
$child2->Title = 'Child 2';
$child2->Sort = 1;
$child2->write();
$duplicateParent = $parent->duplicateWithChildren();
$duplicateChildren = $duplicateParent->AllChildren()->toArray();
$this->assertCount(2, $duplicateChildren);
$duplicateChild2 = array_shift($duplicateChildren);
$duplicateChild1 = array_shift($duplicateChildren);
$this->assertEquals('Child 1', $duplicateChild1->Title);
$this->assertEquals('Child 2', $duplicateChild2->Title);
// assertGreaterThan works by having the LOWER value first
$this->assertGreaterThan($duplicateChild2->Sort, $duplicateChild1->Sort);
}
public function testDeleteFromStageOperatesRecursively() { public function testDeleteFromStageOperatesRecursively() {
Config::inst()->update('SiteTree', 'enforce_strict_hierarchy', false); Config::inst()->update('SiteTree', 'enforce_strict_hierarchy', false);
$pageAbout = $this->objFromFixture('Page', 'about'); $pageAbout = $this->objFromFixture('Page', 'about');