From 39d53a9d56a869da84f7f666057786b0a36b24f1 Mon Sep 17 00:00:00 2001 From: Jonas Hartmann Date: Sun, 21 Aug 2016 13:21:48 +0000 Subject: [PATCH] reset value Sources via end(), split up tests, extend tests for filtering of invalid sources and form end() resetting --- src/View/Helper/FormHelper.php | 3 + tests/TestCase/View/Helper/FormHelperTest.php | 64 ++++++++++++++++++- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/View/Helper/FormHelper.php b/src/View/Helper/FormHelper.php index 947174bd30f..4c6fd3abbc5 100644 --- a/src/View/Helper/FormHelper.php +++ b/src/View/Helper/FormHelper.php @@ -534,6 +534,8 @@ protected function _csrfField() * Closes an HTML form, cleans up values set by FormHelper::create(), and writes hidden * input fields where appropriate. * + * Resets some parts of the stated shared among multiple FormHelper::create() calls to defaults. + * * @param array $secureAttributes Secure attributes which will be passed as HTML attributes * into the hidden input elements generated for the Security Component. * @return string A closing FORM tag. @@ -555,6 +557,7 @@ public function end(array $secureAttributes = []) $this->templater()->pop(); $this->requestType = null; $this->_context = null; + $this->_valuesSources = ['context']; $this->_idPrefix = $this->config('idPrefix'); return $out; diff --git a/tests/TestCase/View/Helper/FormHelperTest.php b/tests/TestCase/View/Helper/FormHelperTest.php index c5fcb99d8ef..0a3a2d0449c 100644 --- a/tests/TestCase/View/Helper/FormHelperTest.php +++ b/tests/TestCase/View/Helper/FormHelperTest.php @@ -8098,10 +8098,16 @@ public function testFormValuesSourcesSettersGetters() $result = $this->Form->getSourceValue('id'); $this->assertEquals($expected, $result); + $expected = ['query', 'data', 'context']; + $this->Form->setValuesSources(['query', 'data', 'invalid', 'context', 'foo']); + $result = $this->Form->getValuesSources(); + $this->assertEquals($expected, $result); + $this->Form->request->data['id'] = '1'; $this->Form->request->query['id'] = '2'; + $this->Form->setValuesSources(['context']); $expected = '1'; $result = $this->Form->getSourceValue('id'); $this->assertEquals($expected, $result); @@ -8131,7 +8137,7 @@ public function testFormValuesSourcesSettersGetters() * * @return void */ - public function testFormValuesSourcesSwitchInputs() + public function testFormValuesSourcesSingleSwitchRendering() { $this->loadFixtures('Articles'); $articles = TableRegistry::get('Articles'); @@ -8204,9 +8210,21 @@ public function testFormValuesSourcesSwitchInputs() ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']], ]; $this->assertHtml($expected, $result); + } + /** + * Tests the different input rendering values based on sources values switching while supplying + * an entity (base context) and multiple sources (durch as data, query) + * + * @return void + */ + public function testFormValuesSourcesListSwitchRendering() + { + $this->loadFixtures('Articles'); + $articles = TableRegistry::get('Articles'); + $article = new Article(); + $articles->patchEntity($article, ['id' => '3']); - unset($this->Form->request->data['id']); $this->Form->request->query['id'] = '9'; $this->Form->create($article); @@ -8261,7 +8279,7 @@ public function testFormValuesSourcesSwitchInputs() * * @return void */ - public function testFormValuesSourcesSwitchViaOptions() + public function testFormValuesSourcesSwitchViaOptionsRendering() { $this->loadFixtures('Articles'); $articles = TableRegistry::get('Articles'); @@ -8318,6 +8336,19 @@ public function testFormValuesSourcesSwitchViaOptions() $this->assertHtml($expected, $result); $result = $this->Form->getSourceValue('id'); $this->assertEquals('8', $result); + } + + /** + * Test the different form input renderings based on values sources switchings through form options + * + * @return void + */ + public function testFormValuesSourcesSwitchViaOptionsAndSetterRendering() + { + $this->loadFixtures('Articles'); + $articles = TableRegistry::get('Articles'); + $article = new Article(); + $articles->patchEntity($article, ['id' => '3']); $this->Form->request->data['id'] = '10'; @@ -8342,4 +8373,31 @@ public function testFormValuesSourcesSwitchViaOptions() $result = $this->Form->getSourceValue('id'); $this->assertEquals('10', $result); } + + /** + * Test the different form input renderings based on values sources switchings through form options + * + * @return void + */ + public function testFormValuesSourcesResetViaEnd() + { + $expected = ['context']; + $result = $this->Form->getValuesSources(); + $this->assertEquals($expected, $result); + + $expected = ['query', 'context', 'data']; + $this->Form->setValuesSources(['query', 'context', 'data']); + + $result = $this->Form->getValuesSources(); + $this->assertEquals($expected, $result); + + $this->Form->create(); + $result = $this->Form->getValuesSources(); + $this->assertEquals($expected, $result); + + $this->Form->end(); + $expected = ['context']; + $result = $this->Form->getValuesSources(); + $this->assertEquals($expected, $result); + } }