From 94192f4b6679341f799077cb1a6546440a002cf5 Mon Sep 17 00:00:00 2001 From: Stephen McMahon Date: Thu, 5 Jul 2018 16:08:19 +1000 Subject: [PATCH 1/5] Add(config) create disable_form_content_interpolation option for ignoring $UserDefinedForm in $this->Content --- _config/config.yml | 5 +++++ code/Control/UserDefinedFormController.php | 3 +++ .../Control/UserDefinedFormControllerTest.php | 18 +++++++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 _config/config.yml diff --git a/_config/config.yml b/_config/config.yml new file mode 100644 index 0000000..9ab47f4 --- /dev/null +++ b/_config/config.yml @@ -0,0 +1,5 @@ +--- +Name: userformsconfig +--- +SilverStripe\UserForms\Control\UserDefinedFormController: + disable_form_content_interpolation: false diff --git a/code/Control/UserDefinedFormController.php b/code/Control/UserDefinedFormController.php index b37bf93..6226093 100644 --- a/code/Control/UserDefinedFormController.php +++ b/code/Control/UserDefinedFormController.php @@ -108,6 +108,9 @@ class UserDefinedFormController extends PageController */ public function index(HTTPRequest $request = null) { + if ($this->config()->disable_form_content_interpolation) { + return []; + } if ($this->Content && $form = $this->Form()) { $hasLocation = stristr($this->Content, '$UserDefinedForm'); if ($hasLocation) { diff --git a/tests/Control/UserDefinedFormControllerTest.php b/tests/Control/UserDefinedFormControllerTest.php index 1fa8207..64ddb5c 100644 --- a/tests/Control/UserDefinedFormControllerTest.php +++ b/tests/Control/UserDefinedFormControllerTest.php @@ -255,7 +255,7 @@ class UserDefinedFormControllerTest extends FunctionalTest $controller = new UserDefinedFormController($form); - // check to see if $Form is replaced to inside the content + // check to see if $Form is placed in the template $index = new ArrayData($controller->index()); $parser = new CSSContentParser($index->renderWith(__CLASS__)); @@ -274,6 +274,22 @@ class UserDefinedFormControllerTest extends FunctionalTest $this->checkTemplateIsCorrect($parser, $form); } + + public function testRenderingIntoTemplateWithDisabledInterpolation() + { + $form = $this->setupFormFrontend(); + + $controller = new UserDefinedFormController($form); + $controller->config()->set('disable_form_content_interpolation', true); + // check to see if $Form is replaced to inside the content + $index = new ArrayData($controller->index()); + $html = $index->renderWith(__CLASS__); + $parser = new CSSContentParser($html); + + $this->assertContains('$UserDefinedForm', $html); + + $this->checkTemplateIsCorrect($parser, $form); + } /** * Publish a form for use on the frontend * From 0a708d9ea3ff89c4bd9fea48dbe485c714c2b1ff Mon Sep 17 00:00:00 2001 From: Stephen McMahon Date: Thu, 5 Jul 2018 17:01:45 +1000 Subject: [PATCH 2/5] Fix(tests) change asserts in to check correct values --- tests/Control/UserDefinedFormControllerTest.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/Control/UserDefinedFormControllerTest.php b/tests/Control/UserDefinedFormControllerTest.php index 64ddb5c..9e8c574 100644 --- a/tests/Control/UserDefinedFormControllerTest.php +++ b/tests/Control/UserDefinedFormControllerTest.php @@ -286,9 +286,13 @@ class UserDefinedFormControllerTest extends FunctionalTest $html = $index->renderWith(__CLASS__); $parser = new CSSContentParser($html); - $this->assertContains('$UserDefinedForm', $html); + // Assert no Content has been rendered + $this->assertNotContains('

Here is my form

$UserDefinedForm

Thank you for filling it out

', $html); + // And no form + $this->assertArrayNotHasKey(0, $parser->getBySelector('form#UserForm_Form_' . $form->ID)); + // check for the input + $this->assertArrayNotHasKey(0, $parser->getBySelector('input.text')); - $this->checkTemplateIsCorrect($parser, $form); } /** * Publish a form for use on the frontend From a3a2c3d286b6643eb57b9565d8c66382eb4c4c9a Mon Sep 17 00:00:00 2001 From: Stephen McMahon Date: Fri, 6 Jul 2018 09:27:28 +1000 Subject: [PATCH 3/5] Fix(UserDefinedFormController) change return type of index() when not using shortcode --- code/Control/UserDefinedFormController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/Control/UserDefinedFormController.php b/code/Control/UserDefinedFormController.php index 6226093..d381ece 100644 --- a/code/Control/UserDefinedFormController.php +++ b/code/Control/UserDefinedFormController.php @@ -104,12 +104,12 @@ class UserDefinedFormController extends PageController * where the form should be rendered into. If it does not exist * then default back to $Form. * - * @return array + * @return array|Object */ public function index(HTTPRequest $request = null) { if ($this->config()->disable_form_content_interpolation) { - return []; + return $this; } if ($this->Content && $form = $this->Form()) { $hasLocation = stristr($this->Content, '$UserDefinedForm'); From ed1e9858af38d924dc9cd6132aa5f7b947b0f25e Mon Sep 17 00:00:00 2001 From: Stephen McMahon Date: Fri, 6 Jul 2018 09:28:16 +1000 Subject: [PATCH 4/5] Fix(test) clean up format to make linter happy --- tests/Control/UserDefinedFormControllerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Control/UserDefinedFormControllerTest.php b/tests/Control/UserDefinedFormControllerTest.php index 9e8c574..dc2bfd8 100644 --- a/tests/Control/UserDefinedFormControllerTest.php +++ b/tests/Control/UserDefinedFormControllerTest.php @@ -292,8 +292,8 @@ class UserDefinedFormControllerTest extends FunctionalTest $this->assertArrayNotHasKey(0, $parser->getBySelector('form#UserForm_Form_' . $form->ID)); // check for the input $this->assertArrayNotHasKey(0, $parser->getBySelector('input.text')); - } + /** * Publish a form for use on the frontend * From 01520c22bdad530ca6b6108473696b55a611bf64 Mon Sep 17 00:00:00 2001 From: Stephen McMahon Date: Mon, 9 Jul 2018 12:32:34 +1000 Subject: [PATCH 5/5] Fix(shortcode) re-order if block check $UserDefinedForm in $this->Content. Rename config value to be clearer --- _config/config.yml | 2 +- code/Control/UserDefinedFormController.php | 8 +++----- tests/Control/UserDefinedFormControllerTest.php | 12 ++++++------ 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/_config/config.yml b/_config/config.yml index 9ab47f4..604a306 100644 --- a/_config/config.yml +++ b/_config/config.yml @@ -2,4 +2,4 @@ Name: userformsconfig --- SilverStripe\UserForms\Control\UserDefinedFormController: - disable_form_content_interpolation: false + disable_form_content_shortcode: false diff --git a/code/Control/UserDefinedFormController.php b/code/Control/UserDefinedFormController.php index d381ece..d4001ed 100644 --- a/code/Control/UserDefinedFormController.php +++ b/code/Control/UserDefinedFormController.php @@ -104,14 +104,12 @@ class UserDefinedFormController extends PageController * where the form should be rendered into. If it does not exist * then default back to $Form. * - * @return array|Object + * @return array */ public function index(HTTPRequest $request = null) { - if ($this->config()->disable_form_content_interpolation) { - return $this; - } - if ($this->Content && $form = $this->Form()) { + $form = $this->Form(); + if ($this->Content && $form && !$this->config()->disable_form_content_shortcode) { $hasLocation = stristr($this->Content, '$UserDefinedForm'); if ($hasLocation) { /** @see Requirements_Backend::escapeReplacement */ diff --git a/tests/Control/UserDefinedFormControllerTest.php b/tests/Control/UserDefinedFormControllerTest.php index dc2bfd8..7e8b19c 100644 --- a/tests/Control/UserDefinedFormControllerTest.php +++ b/tests/Control/UserDefinedFormControllerTest.php @@ -280,18 +280,18 @@ class UserDefinedFormControllerTest extends FunctionalTest $form = $this->setupFormFrontend(); $controller = new UserDefinedFormController($form); - $controller->config()->set('disable_form_content_interpolation', true); + $controller->config()->set('disable_form_content_shortcode', true); // check to see if $Form is replaced to inside the content $index = new ArrayData($controller->index()); $html = $index->renderWith(__CLASS__); $parser = new CSSContentParser($html); - // Assert no Content has been rendered - $this->assertNotContains('

Here is my form

$UserDefinedForm

Thank you for filling it out

', $html); - // And no form - $this->assertArrayNotHasKey(0, $parser->getBySelector('form#UserForm_Form_' . $form->ID)); + // Assert Content has been rendered with the shortcode in place + $this->assertContains('

Here is my form

$UserDefinedForm

Thank you for filling it out

', $html); + // And the form in the $From area + $this->assertArrayHasKey(0, $parser->getBySelector('form#UserForm_Form_' . $form->ID)); // check for the input - $this->assertArrayNotHasKey(0, $parser->getBySelector('input.text')); + $this->assertArrayHasKey(0, $parser->getBySelector('input.text')); } /**