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:
Will Rossiter 2009-09-22 22:36:53 +00:00
parent 61edb74f36
commit 379bcc7921
4 changed files with 139 additions and 23 deletions

View File

@ -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
*/
@ -92,28 +96,73 @@ class UserDefinedForm extends Page {
return $fields;
}
/**
* Called on before delete remove all the fields from the database
*/
public function delete() {
foreach($this->Fields() as $field) {
$field->delete();
}
parent::delete();
}
/**
* Custom Form Actions for the form
* Publishing Versioning support.
*
* @param bool Is the Form readonly
* @return FieldSet
* 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 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 doPublish() {
if($this->Fields()) {
foreach($this->Fields() as $field) {
$field->publish('Stage', 'Live');
}
}
parent::doPublish();
}
/**
* Unpublishing Versioning support
*
* When unpublishing the page it has to remove all the fields from
* the live database table
*
* @return void
*/
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)
);
}
}
/**

View File

@ -30,7 +30,10 @@ class EditableFormField extends DataObject {
static $has_one = array(
"Parent" => "SiteTree",
);
static $extensions = array(
"Versioned('Stage', 'Live')"
);
/**
* @var FieldEditor The current editor
*/

View 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();
}
}

View File

@ -8,7 +8,7 @@
* @package userforms
*/
class UserDefinedFormTest extends SapphireTest {
class UserDefinedFormFieldTest extends SapphireTest {
/**
* Basic Test creating all the editable form fields
@ -25,5 +25,4 @@ class UserDefinedFormTest extends SapphireTest {
$object->delete();
}
}
}
?>
}