Skip to content

Commit

Permalink
Merge pull request silverstripe#9 from open-sausages/pulls/4.0/6258-u…
Browse files Browse the repository at this point in the history
…pdate-archive-warning-msg

ENHANCEMENT Remove item from change sets when it's archived
  • Loading branch information
flamerohr authored Apr 5, 2017
2 parents 5779037 + e1a361b commit 073f6a2
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Versioned.php
Original file line number Diff line number Diff line change
Expand Up @@ -1612,13 +1612,31 @@ public function doArchive()
}

$owner->invokeWithExtensions('onBeforeArchive', $this);
$owner->deleteFromChangeSets();
$owner->doUnpublish();
$owner->deleteFromStage(static::DRAFT);
$owner->invokeWithExtensions('onAfterArchive', $this);

return true;
}

public function deleteFromChangeSets()
{
$owner = $this->owner;
if (!$owner->canArchive()) {
return false;
}

$ids = [$owner->ID];
if ($owner->hasMethod('getDescendantIDList')) {
$ids = array_merge($ids, $owner->getDescendantIDList());
}

ChangeSetItem::get()
->filter(['ObjectID' => $ids])
->removeAll();
}

/**
* Removes this record from the live site
*
Expand Down
43 changes: 43 additions & 0 deletions tests/php/VersionedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use SilverStripe\ORM\DataObjectSchema;
use SilverStripe\ORM\DB;
use SilverStripe\Versioned\Versioned;
use SilverStripe\Versioned\ChangeSet;
use SilverStripe\Versioned\ChangeSetItem;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\Core\Convert;
Expand All @@ -32,6 +34,7 @@ class VersionedTest extends SapphireTest
VersionedTest\PublicStage::class,
VersionedTest\PublicViaExtension::class,
VersionedTest\CustomTable::class,
VersionedTest\ChangeSetTestObject::class,
];

public function testUniqueIndexes()
Expand Down Expand Up @@ -391,6 +394,46 @@ public function testDeleteFromStage()
);
}

public function testDeleteFromChangeSets()
{
$page1 = $this->objFromFixture(VersionedTest\ChangeSetTestObject::class, 'page1');
$page2 = $this->objFromFixture(VersionedTest\ChangeSetTestObject::class, 'page2');
$page2a = $this->objFromFixture(VersionedTest\ChangeSetTestObject::class, 'page2a');
$page2b = $this->objFromFixture(VersionedTest\ChangeSetTestObject::class, 'page2b');
$page3 = $this->objFromFixture(VersionedTest\ChangeSetTestObject::class, 'page3');

$cs1 = new ChangeSet();
$cs1->write();

$cs2 = new ChangeSet();
$cs2->write();

// "cs1" will contain 2 items
$cs1->addObject($page1);
$cs1->addObject($page2);

// "cs2" will contain 3 items
$cs2->addObject($page2a);
$cs2->addObject($page2b);
$cs2->addObject($page3);

$this->assertEquals(2, $cs1->Changes()->count());
$this->assertEquals(3, $cs2->Changes()->count());

// "cs1" will now contain 1 item
$page1->deleteFromChangeSets();

$this->assertEquals(1, $cs1->Changes()->count());
$this->assertEquals(3, $cs2->Changes()->count());

// "cs2" will now contain 1 ("page3") since deleting "page2" from "cs1" will also
// delete "page2a" and "page2b" from change "cs2"
$page2->deleteFromChangeSets();
$this->assertEquals(0, $cs1->Changes()->count());
$this->assertEquals(1, $cs2->Changes()->count());
$this->assertEquals('Page 3', $cs2->Changes()->first()->Title);
}

public function testWritingNewToStage()
{
$origReadingMode = Versioned::get_reading_mode();
Expand Down
14 changes: 14 additions & 0 deletions tests/php/VersionedTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,17 @@ SilverStripe\Versioned\Tests\VersionedTest\AnotherSubclass:
SilverStripe\Versioned\Tests\VersionedTest\SingleStage:
single:
Title: 'Singlestage Title'

SilverStripe\Versioned\Tests\VersionedTest\ChangeSetTestObject:
page1:
Title: Page 1
page2:
Title: Page 2
page3:
Title: Page 3
page2a:
Parent: =>SilverStripe\Versioned\Tests\VersionedTest\ChangeSetTestObject.page2
Title: Page 2a
page2b:
Parent: =>SilverStripe\Versioned\Tests\VersionedTest\ChangeSetTestObject.page2
Title: Page 2b
46 changes: 46 additions & 0 deletions tests/php/VersionedTest/ChangeSetTestObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace SilverStripe\Versioned\Tests\VersionedTest;

use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\HasManyList;
use SilverStripe\ORM\ManyManyList;
use SilverStripe\Versioned\Versioned;
use SilverStripe\ORM\Hierarchy\Hierarchy;

/**
* @property string $Name
* @property string $Title
* @property string $Content
* @method ChangeSetTestObject Parent()
* @method HasManyList Children()
* @method ManyManyList Related()
* @mixin Versioned
*/
class ChangeSetTestObject extends DataObject implements TestOnly
{
private static $table_name = 'ChangeSetVersionedTest_DataObject';

private static $db = [
"Name" => "Varchar",
'Title' => 'Varchar',
];

private static $extensions = [
Versioned::class,
Hierarchy::class
];

private static $has_one = [
'Parent' => ChangeSetTestObject::class,
];

private static $has_many = [
'Children' => ChangeSetTestObject::class,
];

private static $many_many = [
'Related' => RelatedWithoutversion::class,
];
}

0 comments on commit 073f6a2

Please sign in to comment.