diff --git a/code/Controllers/CMSPageHistoryController.php b/code/Controllers/CMSPageHistoryController.php index 7847cb43..c023fc70 100644 --- a/code/Controllers/CMSPageHistoryController.php +++ b/code/Controllers/CMSPageHistoryController.php @@ -423,7 +423,7 @@ class CMSPageHistoryController extends CMSMain { if(!$record) { return null; } - $form = $this->getEditForm($id, null, null, true); + $form = $this->getEditForm($id, null, $fromVersion, $toVersion); $form->setActions(new FieldList()); $form->addExtraClass('compare'); diff --git a/code/Model/SiteTree.php b/code/Model/SiteTree.php index 42705c33..da6abc78 100755 --- a/code/Model/SiteTree.php +++ b/code/Model/SiteTree.php @@ -720,9 +720,12 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid if($children) { /** @var SiteTree $child */ + $sort = 0; foreach($children as $child) { $childClone = $child->duplicateWithChildren(); $childClone->ParentID = $clone->ID; + //retain sort order by manually setting sort values + $childClone->Sort = ++$sort; $childClone->write(); } } diff --git a/tests/behat/features/duplicate-a-page.feature b/tests/behat/features/duplicate-a-page.feature new file mode 100644 index 00000000..96a3a5dc --- /dev/null +++ b/tests/behat/features/duplicate-a-page.feature @@ -0,0 +1,24 @@ +Feature: Duplicate a page + As an author + I want to duplicate a page in the CMS + So that I can grow my website + + Background: + Given I am logged in with "ADMIN" permissions + Given a "page" "Page1" + And the "page" "Page1a" is a child of the "page" "Page1" + And the "page" "Page1b" is a child of the "page" "Page1" + And the "page" "Page1b1" is a child of the "page" "Page1b" + + @javascript + Scenario: I can duplicate a page in the pages section + When I go to "/admin/pages" + And I right click on "Page1" in the tree + And I hover on "Duplicate" in the context menu + And I click on "This page and subpages" in the context menu + Then I should see a "Duplicated 'Page1' and children successfully" notice + + When I fill in "Title" with "Duplicate Page" + And I press the "Save & publish" button + Then I should see "Page1" in the tree + And I should see "Duplicate Page" in the tree diff --git a/tests/model/SiteTreeTest.php b/tests/model/SiteTreeTest.php index d7c3e02d..5c72a954 100644 --- a/tests/model/SiteTreeTest.php +++ b/tests/model/SiteTreeTest.php @@ -390,6 +390,39 @@ class SiteTreeTest extends SapphireTest { $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() { Config::inst()->update('SilverStripe\\CMS\\Model\\SiteTree', 'enforce_strict_hierarchy', false); $pageAbout = $this->objFromFixture('Page', 'about');