Skip to content

Commit

Permalink
Merge pull request #217 from creative-commoners/pulls/2.1/duplication…
Browse files Browse the repository at this point in the history
…-logic

API Bump CMS version, implement recursive duplication
  • Loading branch information
NightJar authored Apr 12, 2018
2 parents ea03f12 + eaaa8d0 commit 2bdb720
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ env:
matrix:
include:
- php: 5.6
env: DB=MYSQL RECIPE_VERSION=1.0.x-dev PHPCS_TEST=1 PHPUNIT_TEST=1
env: DB=MYSQL RECIPE_VERSION=1.1.x-dev PHPCS_TEST=1 PHPUNIT_TEST=1
#- php: 7.0
# env: DB=PGSQL PHPUNIT_TEST=1
- php: 7.0
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"silverstripe/cms": "^4.0@dev",
"silverstripe/cms": "^4.1",
"symbiote/silverstripe-gridfieldextensions": "^3.1",
"silverstripe/vendor-plugin": "^1.0"
},
Expand Down
8 changes: 6 additions & 2 deletions src/Extensions/ElementalDuplicationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

namespace DNADesign\Elemental\Extensions;

use SilverStripe\Core\Config\Config;
use SilverStripe\ORM\DataExtension;
use SilverStripe\Core\Config;

/**
* @deprecated 2.1..3.0 This extension is not implemented by default, and will be removed
* in 3.0.0. Please use the `$owns` API with `$cascade_duplicates` instead.
* See {@link ElementalPageExtension} for an example of this.
*/
class ElementalDuplicationExtension extends DataExtension
{

/**
* Duplicate items
*
Expand Down
14 changes: 6 additions & 8 deletions src/Extensions/ElementalPageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@

class ElementalPageExtension extends ElementalAreasExtension
{
/**
* @var array
*/
private static $has_one = [
'ElementalArea' => ElementalArea::class
'ElementalArea' => ElementalArea::class,
];

/**
* @var array
*/
private static $owns = [
'ElementalArea'
'ElementalArea',
];

private static $cascade_duplicates = [
'ElementalArea',
];

/**
Expand Down
35 changes: 9 additions & 26 deletions src/Models/ElementalArea.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,51 +24,34 @@
*/
class ElementalArea extends DataObject
{
/**
* @var array $db
*/
private static $db = [
'OwnerClassName' => 'Varchar(255)',
];

/**
* @var array $has_many
*/
private static $has_many = [
'Elements' => BaseElement::class
'Elements' => BaseElement::class,
];

/**
* @var array
*/
private static $extensions = [
Versioned::class
Versioned::class,
];

/**
* @var array
*/
private static $owns = [
'Elements'
'Elements',
];

/**
* @var array
*/
private static $cascade_deletes = [
'Elements'
'Elements',
];

private static $cascade_duplicates = [
'Elements',
];

/**
* @var array
*/
private static $summary_fields = [
'Title' => 'Title'
'Title' => 'Title',
];

/**
* @var string
*/
private static $table_name = 'ElementalArea';

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/ElementalAreaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,17 @@ public function testCanBePublished()
$element = $this->objFromFixture(TestElement::class, 'element1');
$this->assertTrue($element->canPublish($member));
}

public function testDuplicate()
{
/** @var ElementalArea $area */
$area = $this->objFromFixture(ElementalArea::class, 'area1');
$areaIds = $area->Elements()->column('ID');
$this->assertCount(2, $areaIds);

$duplicatedArea = $area->duplicate(true);
$duplicatedAreaIds = $duplicatedArea->Elements()->column('ID');
$this->assertCount(2, $duplicatedAreaIds);
$this->assertNotEquals($areaIds, $duplicatedAreaIds);
}
}
18 changes: 18 additions & 0 deletions tests/ElementalPageExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,22 @@ public function testGetElementalTypes()

$this->assertEquals('A test element', $types[TestElement::class], 'Types should use their "type"');
}

public function testDuplicatingPageDuplicatesElements()
{
/** @var TestPage $page */
$page = $this->objFromFixture(TestPage::class, 'page_with_elements');
$this->assertCount(2, $page->ElementalArea()->Elements());

/** @var TestPage $newPage */
$newPage = $page->duplicate();
$this->assertNotEquals($page->ElementalArea->ID, $newPage->ElementalArea->ID, 'Area is duplicated');
$this->assertCount(2, $newPage->ElementalArea()->Elements());

$this->assertNotEquals(
$page->ElementalArea()->Elements()->column('ID'),
$newPage->ElementalArea()->Elements()->column('ID'),
'Duplicated page has duplicated area and duplicated elements, i.e. not shared'
);
}
}
20 changes: 16 additions & 4 deletions tests/ElementalPageExtensionTest.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
DNADesign\Elemental\Models\ElementalArea:
area51:
Title: Area 51

DNADesign\Elemental\Models\ElementContent:
content1:
Title: Test Content
ParentID: =>DNADesign\Elemental\Models\ElementalArea.area51
content2:
Title: More content
ParentID: =>DNADesign\Elemental\Models\ElementalArea.area51

DNADesign\Elemental\Tests\Src\TestPage:
elementaldemo:
Title: Test Elemental
ElementalAreaID: =>DNADesign\Elemental\Models\ElementalArea.area51
page_with_elements:
Title: Page with elements
ElementalAreaID: =>DNADesign\Elemental\Models\ElementalArea.area51

SilverStripe\CMS\Model\RedirectorPage:
elementredirectpage:
Title: Redirector Page

DNADesign\Elemental\Models\ElementContent:
content1:
Title: Test Content

0 comments on commit 2bdb720

Please sign in to comment.