Skip to content

Commit

Permalink
reset value Sources via end(), split up tests, extend tests for filte…
Browse files Browse the repository at this point in the history
…ring of invalid sources and form end() resetting
  • Loading branch information
Jonas Hartmann committed Aug 21, 2016
1 parent 375a4e4 commit 39d53a9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/View/Helper/FormHelper.php
Expand Up @@ -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.
Expand All @@ -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;
Expand Down
64 changes: 61 additions & 3 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -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);
Expand Down Expand Up @@ -8131,7 +8137,7 @@ public function testFormValuesSourcesSettersGetters()
*
* @return void
*/
public function testFormValuesSourcesSwitchInputs()
public function testFormValuesSourcesSingleSwitchRendering()
{
$this->loadFixtures('Articles');
$articles = TableRegistry::get('Articles');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -8261,7 +8279,7 @@ public function testFormValuesSourcesSwitchInputs()
*
* @return void
*/
public function testFormValuesSourcesSwitchViaOptions()
public function testFormValuesSourcesSwitchViaOptionsRendering()
{
$this->loadFixtures('Articles');
$articles = TableRegistry::get('Articles');
Expand Down Expand Up @@ -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';
Expand All @@ -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);
}
}

0 comments on commit 39d53a9

Please sign in to comment.