From 70f05b891e0eec22a4fd1d1f2a31848eb1579ce0 Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Sat, 12 Dec 2009 08:59:20 +0000 Subject: [PATCH] BUGFIX: fixed conflict of Versioned extension functions. MINOR: added tests for publishing multiple option fields --- code/UserDefinedForm.php | 6 +- code/editor/EditableFormField.php | 19 ++++++ code/editor/EditableMultipleOptionField.php | 8 +-- tests/UserDefinedFormEditorTest.php | 65 +++++++++++++++++---- 4 files changed, 81 insertions(+), 17 deletions(-) diff --git a/code/UserDefinedForm.php b/code/UserDefinedForm.php index 658808a..5bae03f 100755 --- a/code/UserDefinedForm.php +++ b/code/UserDefinedForm.php @@ -119,14 +119,14 @@ class UserDefinedForm extends Page { if($live) { foreach($live as $field) { - $field->deleteFromStage('Live'); + $field->doDeleteFromStage('Live'); } } // publish the draft pages if($this->Fields()) { foreach($this->Fields() as $field) { - $field->publish('Stage', 'Live'); + $field->doPublish('Stage', 'Live'); } } @@ -144,7 +144,7 @@ class UserDefinedForm extends Page { public function doUnpublish() { if($this->Fields()) { foreach($this->Fields() as $field) { - $field->deleteFromStage('Live'); + $field->doDeleteFromStage('Live'); } } diff --git a/code/editor/EditableFormField.php b/code/editor/EditableFormField.php index 40d8e57..9409c9d 100755 --- a/code/editor/EditableFormField.php +++ b/code/editor/EditableFormField.php @@ -81,6 +81,25 @@ class EditableFormField extends DataObject { return $this->Parent()->canEdit(); } + /** + * Publish this Form Field to the live site + * + * Wrapper for the {@link Versioned} publish function + */ + public function doPublish($fromStage, $toStage, $createNewVersion = false) { + $this->publish($fromStage, $toStage, $createNewVersion); + } + + /** + * Delete this form from a given stage + * + * Wrapper for the {@link Versioned} deleteFromStage function + */ + public function doDeleteFromStage($stage) { + $this->deleteFromStage($stage); + } + + /** * Show this form on load or not * diff --git a/code/editor/EditableMultipleOptionField.php b/code/editor/EditableMultipleOptionField.php index 27fa7d8..61109fa 100644 --- a/code/editor/EditableMultipleOptionField.php +++ b/code/editor/EditableMultipleOptionField.php @@ -27,7 +27,7 @@ class EditableMultipleOptionField extends EditableFormField { * * @return void */ - public function publish($fromStage, $toStage, $createNewVersion = false) { + public function doPublish($fromStage, $toStage, $createNewVersion = false) { $live = Versioned::get_by_stage("EditableOption", "Live", "\"EditableOption\".\"ParentID\" = $this->ID"); if($live) { @@ -41,7 +41,7 @@ class EditableMultipleOptionField extends EditableFormField { } } - parent::publish($fromStage, $toStage, $createNewVersion); + $this->publish($fromStage, $toStage, $createNewVersion); } /** @@ -51,14 +51,14 @@ class EditableMultipleOptionField extends EditableFormField { * * @return void */ - public function deleteFromStage($stage) { + public function doDeleteFromStage($stage) { if($this->Options()) { foreach($this->Options() as $option) { $option->deleteFromStage($stage); } } - parent::deleteFromStage($stage); + $this->deleteFromStage($stage); } /** diff --git a/tests/UserDefinedFormEditorTest.php b/tests/UserDefinedFormEditorTest.php index adca9dd..27e0645 100644 --- a/tests/UserDefinedFormEditorTest.php +++ b/tests/UserDefinedFormEditorTest.php @@ -13,31 +13,76 @@ class UserDefinedFormEditorTest extends FunctionalTest { function setUp() { parent::setUp(); + $this->logInWithPermssion('ADMIN'); $this->form = new UserDefinedForm(); $this->form->write(); } - function testPublishing() { - $this->logInWithPermssion('ADMIN'); - + function testPublishingNormalField() { $id = $this->form->ID; - $this->form->Fields()->add(new EditableFormField()); + + // test a normal field + $field = new EditableFormField(); + $field->write(); + + $this->form->Fields()->add($field); + + // upon adding it, it shouldn't be on the live site + $live = Versioned::get_one_by_stage("UserDefinedForm", "Live", "\"UserDefinedForm_Live\".\"ID\" = $id"); + $this->assertFalse($live); + + // upon publishing the field should exist $this->form->doPublish(); - $whereClause = defined('DB::USE_ANSI_SQL') ? "\"UserDefinedForm_Live\".\"ID\" = $id" : "UserDefinedForm_Live.ID = $id"; - $live = Versioned::get_one_by_stage("UserDefinedForm", "Live", $whereClause); + $live = Versioned::get_one_by_stage("UserDefinedForm", "Live", "\"UserDefinedForm_Live\".\"ID\" = $id"); $this->assertEquals($live->Fields()->Count(), 1); } + function testPublishingMultipleOptions() { + $id = $this->form->ID; + $this->form->Fields()->removeAll(); + + // test a editable option field + $dropdown = new EditableDropdown(); + $dropdown->write(); + + $checkbox = new EditableCheckboxGroupField(); + $checkbox->write(); + + $option = new EditableOption(); + $option->write(); + + $option2 = new EditableOption(); + $option2->write(); + + $dropdown->Options()->add($option); + $checkbox->Options()->add($option2); + + $this->form->Fields()->add($dropdown); + $this->form->Fields()->add($checkbox); + + // upon adding it, it shouldn't be on the live site + $live = Versioned::get_one_by_stage("UserDefinedForm", "Live", "\"UserDefinedForm_Live\".\"ID\" = $id"); + $this->assertFalse($live); + + // and when published it should exist and the option + $this->form->doPublish(); + $live = Versioned::get_one_by_stage("UserDefinedForm", "Live", "\"UserDefinedForm_Live\".\"ID\" = $id"); + $this->assertEquals($live->Fields()->Count(), 2); + + // check they have options attached + foreach($live->Fields() as $field) { + $this->assertEquals($field->Options()->Count(), 1); + } + } + function testUnpublishing() { $id = $this->form->ID; $this->form->Fields()->removeAll(); $this->form->Fields()->add(new EditableFormField()); $this->form->doUnPublish(); - $whereClauseStage = defined('DB::USE_ANSI_SQL') ? "\"UserDefinedForm\".\"ID\" = $id" : "UserDefinedForm.ID = $id"; - $whereClauseLive = defined('DB::USE_ANSI_SQL') ? "\"UserDefinedForm_Live\".\"ID\" = $id" : "UserDefinedForm_Live.ID = $id"; - $live = Versioned::get_one_by_stage("UserDefinedForm", "Live", $whereClauseLive); - $stage = Versioned::get_one_by_stage("UserDefinedForm", "Stage", $whereClauseStage); + $live = Versioned::get_one_by_stage("UserDefinedForm", "Live", "\"UserDefinedForm_Live\".\"ID\" = $id"); + $stage = Versioned::get_one_by_stage("UserDefinedForm", "Stage", "\"UserDefinedForm\".\"ID\" = $id"); $this->assertEquals($live, false); $this->assertEquals($stage->Fields()->Count(), 1); }