2021-01-17 03:29:33 +01:00
|
|
|
<?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,
|
|
|
|
];
|
|
|
|
|
2021-11-01 21:52:58 +01:00
|
|
|
protected function setUp(): void
|
2021-01-17 03:29:33 +01:00
|
|
|
{
|
|
|
|
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);
|
2021-05-27 01:45:39 +02:00
|
|
|
$this->assertCount(2, $page->Fields(), 'Draft UserForm page starts with 2 fields');
|
|
|
|
$this->assertCount(3, $block->Fields(), 'Draft UserForm block starts with 3 fields');
|
2021-01-17 03:29:33 +01:00
|
|
|
|
|
|
|
// ensure setup has affected live mode too
|
|
|
|
$origReadingMode = Versioned::get_reading_mode();
|
2021-05-27 01:45:39 +02:00
|
|
|
Versioned::withVersionedMode(function () {
|
|
|
|
Versioned::set_reading_mode(Versioned::LIVE);
|
|
|
|
$initialLivePage = UserDefinedForm::get()->First();
|
|
|
|
$initialLiveBlock = UserFormBlockStub::get()->First();
|
2021-01-17 03:29:33 +01:00
|
|
|
|
2021-05-27 01:45:39 +02:00
|
|
|
$this->assertSame($initialLivePage->ID, $initialLiveBlock->ID);
|
|
|
|
$this->assertCount(2, $initialLivePage->Fields(), 'Live UserForm page starst with 2 fields');
|
|
|
|
$this->assertCount(3, $initialLiveBlock->Fields(), 'Live UserForm block starst with 3 fields');
|
|
|
|
});
|
2021-01-17 03:29:33 +01:00
|
|
|
|
|
|
|
// 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
|
2021-05-27 01:45:39 +02:00
|
|
|
/** @var UserDefinedForm $checkPage */
|
2021-01-17 03:29:33 +01:00
|
|
|
$checkPage = UserDefinedForm::get()->First();
|
|
|
|
$checkBlock = UserFormBlockStub::get()->First();
|
|
|
|
|
2021-05-27 01:45:39 +02:00
|
|
|
$this->assertCount(3, $checkPage->Fields(), 'Field has been added to draft user form page');
|
|
|
|
$this->assertCount(
|
|
|
|
3,
|
|
|
|
$checkBlock->Fields(),
|
|
|
|
'Draft userform block with same ID is not affected'
|
|
|
|
);
|
2021-01-17 03:29:33 +01:00
|
|
|
|
|
|
|
// ensure this is true for live mode too
|
2021-05-27 01:45:39 +02:00
|
|
|
Versioned::withVersionedMode(function () {
|
|
|
|
Versioned::set_reading_mode(Versioned::LIVE);
|
|
|
|
$checkLivePage = UserDefinedForm::get()->First();
|
|
|
|
$checkLiveBlock = UserFormBlockStub::get()->First();
|
|
|
|
$this->assertCount(
|
|
|
|
3,
|
|
|
|
$checkLivePage->Fields(),
|
|
|
|
'Field has been added to live user form page'
|
|
|
|
);
|
|
|
|
$this->assertCount(
|
|
|
|
3,
|
|
|
|
$checkLiveBlock->Fields(),
|
|
|
|
'Live userform block with same ID is not affected'
|
|
|
|
);
|
|
|
|
});
|
2021-01-17 03:29:33 +01:00
|
|
|
}
|
|
|
|
}
|