FIX account for owner class while removing orphans (#1018)

* FIX account for owner class while removing orphans
* Explicitly set $page and $block ID's in test fixture

Co-authored-by: Steve Boyd <emteknetnz@gmail.com>
This commit is contained in:
Dylan Wagstaff 2021-01-17 15:29:33 +13:00 committed by GitHub
parent e7f51d227a
commit a42729679b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 116 additions and 0 deletions

View File

@ -196,6 +196,7 @@ class UserFormFieldEditorExtension extends DataExtension
$live = Versioned::get_by_stage(EditableFormField::class, Versioned::LIVE)
->filter([
'ParentID' => $this->owner->ID,
'ParentClass' => get_class($this->owner),
]);
if (!empty($seenIDs)) {

View File

@ -0,0 +1,15 @@
<?php
namespace SilverStripe\UserForms\Tests\Extension;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;
use SilverStripe\UserForms\UserForm;
/**
* A stand in for e.g. dnadesigned/silverstripe-elemental-userforms
*/
class UserFormBlockStub extends DataObject implements TestOnly
{
use UserForm;
}

View File

@ -0,0 +1,77 @@
<?php
namespace SilverStripe\UserForms\Tests\Extension;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\UserForms\Model\EditableFormField\EditableEmailField;
use SilverStripe\UserForms\Model\UserDefinedForm;
use SilverStripe\Versioned\Versioned;
class UserFormFieldEditorExtensionTest extends SapphireTest
{
protected static $fixture_file = 'UserFormFieldEditorExtensionTest.yml';
protected static $extra_dataobjects = [
UserFormBlockStub::class,
];
protected function setUp()
{
parent::setUp();
$page = $this->objFromFixture(UserDefinedForm::class, 'page');
$block = $this->objFromFixture(UserFormBlockStub::class, 'block');
$page->publishRecursive();
$block->publishRecursive();
}
public function testOrphanRemovalDoesNotAffectOtherClassesWithTheSameID()
{
$page = $this->objFromFixture(UserDefinedForm::class, 'page');
$block = $this->objFromFixture(UserFormBlockStub::class, 'block');
// assert setup
$this->assertSame($page->ID, $block->ID);
$this->assertCount(1, $page->Fields());
$this->assertCount(3, $block->Fields());
// ensure setup has affected live mode too
$origReadingMode = Versioned::get_reading_mode();
Versioned::set_reading_mode(Versioned::LIVE);
$initialLivePage = UserDefinedForm::get()->First();
$initialLiveBlock = UserFormBlockStub::get()->First();
$this->assertSame($initialLivePage->ID, $initialLiveBlock->ID);
$this->assertCount(1, $initialLivePage->Fields());
$this->assertCount(3, $initialLiveBlock->Fields());
Versioned::set_reading_mode($origReadingMode);
// execute change
$newField = new EditableEmailField();
$newField->update([
'Name' => 'basic_email_name',
'Title' => 'Page Email Field'
]);
$page->Fields()->add($newField);
$page->publishRecursive();
// assert effect of change
$checkPage = UserDefinedForm::get()->First();
$checkBlock = UserFormBlockStub::get()->First();
$this->assertCount(2, $checkPage->Fields());
$this->assertCount(3, $checkBlock->Fields());
// ensure this is true for live mode too
$origReadingMode = Versioned::get_reading_mode();
Versioned::set_reading_mode(Versioned::LIVE);
$checkLivePage = UserDefinedForm::get()->First();
$checkLiveBlock = UserFormBlockStub::get()->First();
$this->assertCount(2, $checkLivePage->Fields());
$this->assertCount(3, $checkLiveBlock->Fields());
Versioned::set_reading_mode($origReadingMode);
}
}

View File

@ -0,0 +1,23 @@
SilverStripe\UserForms\Model\UserDefinedForm:
page:
ID: 9999
SilverStripe\UserForms\Tests\Extension\UserFormBlockStub:
block:
ID: 9999
SilverStripe\UserForms\Model\EditableFormField\EditableTextField:
page-text-field:
Name: basic_text_name
Title: Page Text Field
Parent: =>SilverStripe\UserForms\Model\UserDefinedForm.page
block-text-field-1:
Name: basic_text_name_2
Title: Block Text Field
Parent: =>SilverStripe\UserForms\Tests\Extension\UserFormBlockStub.block
block-text-field-2:
Name: basic_text_name_3
Title: Block Text Field
Parent: =>SilverStripe\UserForms\Tests\Extension\UserFormBlockStub.block
block-text-field-3:
Name: basic_text_name_4
Title: Block Text Field
Parent: =>SilverStripe\UserForms\Tests\Extension\UserFormBlockStub.block