mirror of
https://github.com/silverstripe/silverstripe-userforms.git
synced 2024-10-22 17:05:42 +02:00
ENHANCEMENT: added inital versioning / staging support to userforms. Supports different forms on stage / live. ENHANCEMENT: added unit tests to cover versioning / staging
This commit is contained in:
parent
61edb74f36
commit
379bcc7921
@ -42,6 +42,10 @@ class UserDefinedForm extends Page {
|
||||
'OnCompleteMessage' => '<p>Thanks, we\'ve received your submission.</p>'
|
||||
);
|
||||
|
||||
static $extensions = array(
|
||||
"Versioned('Stage', 'Live')"
|
||||
);
|
||||
|
||||
/**
|
||||
* @var Array
|
||||
*/
|
||||
@ -94,26 +98,71 @@ class UserDefinedForm extends Page {
|
||||
}
|
||||
|
||||
/**
|
||||
* Called on before delete remove all the fields from the database
|
||||
* Publishing Versioning support.
|
||||
*
|
||||
* When publishing copy the editable form fields to the live database
|
||||
* Not going to version emails and submissions as they are likely to
|
||||
* persist over multiple versions
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function delete() {
|
||||
public function doPublish() {
|
||||
if($this->Fields()) {
|
||||
foreach($this->Fields() as $field) {
|
||||
$field->delete();
|
||||
$field->publish('Stage', 'Live');
|
||||
}
|
||||
parent::delete();
|
||||
}
|
||||
|
||||
parent::doPublish();
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom Form Actions for the form
|
||||
* Unpublishing Versioning support
|
||||
*
|
||||
* @param bool Is the Form readonly
|
||||
* @return FieldSet
|
||||
* When unpublishing the page it has to remove all the fields from
|
||||
* the live database table
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function customFormActions($isReadonly = false) {
|
||||
return new FieldSet(
|
||||
new TextField("SubmitButtonText", _t('UserDefinedForm.TEXTONSUBMIT', 'Text on submit button:'), $this->SubmitButtonText),
|
||||
new CheckboxField("ShowClearButton", _t('UserDefinedForm.SHOWCLEARFORM', 'Show Clear Form Button'), $this->ShowClearButton)
|
||||
);
|
||||
public function doUnpublish() {
|
||||
if($this->Fields()) {
|
||||
foreach($this->Fields() as $field) {
|
||||
$field->deleteFromStage('Live');
|
||||
}
|
||||
}
|
||||
|
||||
parent::doUnpublish();
|
||||
}
|
||||
|
||||
/**
|
||||
* Roll back a form to a previous version
|
||||
*
|
||||
* @param String|int Version to roll back to
|
||||
*/
|
||||
public function doRollbackTo($version) {
|
||||
if($this->Fields()) {
|
||||
foreach($this->Fields() as $field) {
|
||||
$field->publish($version, "Stage", true);
|
||||
$field->writeWithoutVersion();
|
||||
}
|
||||
}
|
||||
|
||||
parent::doRollbackTo($version);
|
||||
}
|
||||
|
||||
/**
|
||||
* Revert the draft site to the current live site
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function doRevertToLive() {
|
||||
if($this->Fields()) {
|
||||
foreach($this->Fields() as $field) {
|
||||
$field->writeToStage('Live', 'Stage');
|
||||
}
|
||||
}
|
||||
|
||||
parent::doRevertToLive();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -131,6 +180,19 @@ class UserDefinedForm extends Page {
|
||||
}
|
||||
return $page;
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom Form Actions for the form
|
||||
*
|
||||
* @param bool Is the Form readonly
|
||||
* @return FieldSet
|
||||
*/
|
||||
public function customFormActions($isReadonly = false) {
|
||||
return new FieldSet(
|
||||
new TextField("SubmitButtonText", _t('UserDefinedForm.TEXTONSUBMIT', 'Text on submit button:'), $this->SubmitButtonText),
|
||||
new CheckboxField("ShowClearButton", _t('UserDefinedForm.SHOWCLEARFORM', 'Show Clear Form Button'), $this->ShowClearButton)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -31,6 +31,9 @@ class EditableFormField extends DataObject {
|
||||
"Parent" => "SiteTree",
|
||||
);
|
||||
|
||||
static $extensions = array(
|
||||
"Versioned('Stage', 'Live')"
|
||||
);
|
||||
/**
|
||||
* @var FieldEditor The current editor
|
||||
*/
|
||||
|
52
tests/UserDefinedFormEditorTest.php
Normal file
52
tests/UserDefinedFormEditorTest.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Tests covering the form editor / builder and
|
||||
* some of the user interface
|
||||
*
|
||||
* @package userforms
|
||||
*/
|
||||
|
||||
class UserDefinedFormEditorTest extends FunctionalTest {
|
||||
|
||||
protected $form;
|
||||
|
||||
function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->form = new UserDefinedForm();
|
||||
$this->form->write();
|
||||
}
|
||||
|
||||
function testPublishing() {
|
||||
$id = $this->form->ID;
|
||||
$this->form->Fields()->add(new EditableFormField());
|
||||
$this->form->doPublish();
|
||||
$live = Versioned::get_one_by_stage("UserDefinedForm", "Live", "`UserDefinedForm`.ID = $id");
|
||||
$this->assertEquals($live->Fields()->Count(), 1);
|
||||
}
|
||||
|
||||
function testUnpublishing() {
|
||||
$id = $this->form->ID;
|
||||
$this->form->Fields()->removeAll();
|
||||
$this->form->Fields()->add(new EditableFormField());
|
||||
$this->form->doUnPublish();
|
||||
$live = Versioned::get_one_by_stage("UserDefinedForm", "Live", "`UserDefinedForm`.ID = $id");
|
||||
$stage = Versioned::get_one_by_stage("UserDefinedForm", "Stage", "`UserDefinedForm`.ID = $id");
|
||||
$this->assertEquals($live, false);
|
||||
$this->assertEquals($stage->Fields()->Count(), 1);
|
||||
}
|
||||
|
||||
function testDuplicatingPage() {
|
||||
$this->form->Fields()->add(new EditableFormField());
|
||||
$form_copy = $this->form->duplicate();
|
||||
|
||||
$this->assertEquals($this->form->Fields()->Count(), $form_copy->Fields()->Count());
|
||||
}
|
||||
|
||||
function tearDown() {
|
||||
$this->form->delete();
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
}
|
@ -8,7 +8,7 @@
|
||||
* @package userforms
|
||||
*/
|
||||
|
||||
class UserDefinedFormTest extends SapphireTest {
|
||||
class UserDefinedFormFieldTest extends SapphireTest {
|
||||
|
||||
/**
|
||||
* Basic Test creating all the editable form fields
|
||||
@ -26,4 +26,3 @@ class UserDefinedFormTest extends SapphireTest {
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
Reference in New Issue
Block a user