Skip to content
Permalink
Browse files

make FormHelper::create() not overwrite existing valuesSources config…

…urations, added tests
  • Loading branch information...
Jonas Hartmann
Jonas Hartmann committed Aug 20, 2016
1 parent 6bcd8a1 commit 375a4e4ff736971ff26ab21cc066c6fc5a9fba48
Showing with 261 additions and 1 deletion.
  1. +1 −1 src/View/Helper/FormHelper.php
  2. +260 −0 tests/TestCase/View/Helper/FormHelperTest.php
@@ -366,7 +366,7 @@ public function create($model = null, array $options = [])
'encoding' => strtolower(Configure::read('App.encoding')),
'templates' => null,
'idPrefix' => null,
'valuesSources' => 'context',
'valuesSources' => $this->getValuesSources(),
];
if (isset($options['action'])) {
@@ -8082,4 +8082,264 @@ public function testAutoDomId()
];
$this->assertHtml($expected, $result);
}
/**
* Test the basic setters and getters for value sources
*
* @return void
*/
public function testFormValuesSourcesSettersGetters()
{
$expected = ['context'];
$result = $this->Form->getValuesSources();
$this->assertEquals($expected, $result);
$expected = null;
$result = $this->Form->getSourceValue('id');
$this->assertEquals($expected, $result);
$this->Form->request->data['id'] = '1';
$this->Form->request->query['id'] = '2';
$expected = '1';
$result = $this->Form->getSourceValue('id');
$this->assertEquals($expected, $result);
$this->Form->setValuesSources('query');
$expected = ['query'];
$result = $this->Form->getValuesSources();
$this->assertEquals($expected, $result);
$expected = '2';
$result = $this->Form->getSourceValue('id');
$this->assertEquals($expected, $result);
$this->Form->setValuesSources(['data']);
$expected = '1';
$result = $this->Form->getSourceValue('id');
$this->assertEquals($expected, $result);
$this->Form->setValuesSources(['query', 'data']);
$expected = '2';
$result = $this->Form->getSourceValue('id');
$this->assertEquals($expected, $result);
}
/**
* Tests the different input rendering values based on sources values switching
*
* @return void
*/
public function testFormValuesSourcesSwitchInputs()
{
$this->loadFixtures('Articles');
$articles = TableRegistry::get('Articles');
$article = new Article();
$articles->patchEntity($article, ['id' => '3']);
$this->Form->create($article);
$this->Form->setValuesSources(['context']);
$result = $this->Form->input('id');
$expected = [
['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
];
$this->assertHtml($expected, $result);
$this->Form->request->query['id'] = '5';
$this->Form->setValuesSources(['query']);
$this->Form->create($article);
$result = $this->Form->input('id');
$expected = [
['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
];
$this->assertHtml($expected, $result);
$this->Form->create($article);
$this->Form->setValuesSources(['query']);
$result = $this->Form->input('id');
$expected = [
['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
];
$this->assertHtml($expected, $result);
$this->Form->request->query['id'] = '5a';
$this->Form->request->data['id'] = '5b';
$this->Form->setValuesSources(['context']);
$this->Form->create($article);
$result = $this->Form->input('id');
$expected = [
['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
];
$this->assertHtml($expected, $result);
$this->Form->setValuesSources(['data']);
$this->Form->create($article);
$result = $this->Form->input('id');
$expected = [
['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
];
$this->assertHtml($expected, $result);
$this->Form->request->data['id'] = '6';
$this->Form->request->query['id'] = '7';
$this->Form->create($article);
$this->Form->setValuesSources(['data']);
$result = $this->Form->input('id');
$expected = [
['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '6']],
];
$this->assertHtml($expected, $result);
$this->Form->request->data['id'] = '8';
$this->Form->request->query['id'] = '9';
$this->Form->create($article);
$this->Form->setValuesSources(['query']);
$result = $this->Form->input('id');
$expected = [
['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
];
$this->assertHtml($expected, $result);
unset($this->Form->request->data['id']);
$this->Form->request->query['id'] = '9';
$this->Form->create($article);
$this->Form->setValuesSources(['context']);
$result = $this->Form->input('id');
$expected = [
['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
];
$this->assertHtml($expected, $result);
$this->Form->setValuesSources(['query']);
$result = $this->Form->input('id');
$expected = [
['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
];
$this->assertHtml($expected, $result);
$this->Form->setValuesSources(['context', 'query']);
$result = $this->Form->input('id');
$expected = [
['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
];
$this->assertHtml($expected, $result);
$this->Form->setValuesSources(['query', 'context']);
$result = $this->Form->input('id');
$expected = [
['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
];
$this->assertHtml($expected, $result);
$this->Form->setValuesSources(['data', 'query', 'context']);
$result = $this->Form->input('id');
$expected = [
['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
];
$this->assertHtml($expected, $result);
$this->Form->request->data['id'] = '8';
$this->Form->request->query['id'] = '9';
$this->Form->setValuesSources(['data', 'query', 'context']);
$result = $this->Form->input('id');
$expected = [
['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
];
$this->assertHtml($expected, $result);
}
/**
* Test the different form input renderings based on values sources switchings through form options
*
* @return void
*/
public function testFormValuesSourcesSwitchViaOptions()
{
$this->loadFixtures('Articles');
$articles = TableRegistry::get('Articles');
$article = new Article();
$articles->patchEntity($article, ['id' => '3']);
$this->Form->request->data['id'] = '4';
$this->Form->request->query['id'] = '5';
$this->Form->create($article, ['valuesSources' => 'query']);
$result = $this->Form->input('id');
$expected = [
['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
];
$this->assertHtml($expected, $result);
$result = $this->Form->getSourceValue('id');
$this->assertEquals('5', $result);
$this->Form->request->data['id'] = '6';
$this->Form->request->query['id'] = '7';
$this->Form->setValuesSources(['context']);
$this->Form->create($article, ['valuesSources' => 'query']);
$result = $this->Form->input('id');
$expected = [
['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '7']],
];
$this->assertHtml($expected, $result);
$result = $this->Form->getSourceValue('id');
$this->assertEquals('7', $result);
$this->Form->setValuesSources(['query']);
$this->Form->create($article, ['valuesSources' => 'data']);
$result = $this->Form->input('id');
$expected = [
['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '6']],
];
$this->assertHtml($expected, $result);
$result = $this->Form->getSourceValue('id');
$this->assertEquals('6', $result);
$this->Form->request->data['id'] = '8';
$this->Form->request->query['id'] = '9';
$this->Form->setValuesSources(['query']);
$this->Form->create($article, ['valuesSources' => ['context', 'data']]);
$result = $this->Form->input('id');
$expected = [
['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
];
$this->assertHtml($expected, $result);
$result = $this->Form->getSourceValue('id');
$this->assertEquals('8', $result);
$this->Form->request->data['id'] = '10';
$this->Form->request->query['id'] = '11';
$this->Form->setValuesSources(['context'])->create($article, ['valuesSources' => ['query', 'data']]);
$result = $this->Form->input('id');
$expected = [
['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
];
$this->assertHtml($expected, $result);
$result = $this->Form->getSourceValue('id');
$this->assertEquals('11', $result);
unset($this->Form->request->query['id']);
$this->Form->setValuesSources(['context'])->create($article, ['valuesSources' => ['query', 'data']]);
$result = $this->Form->input('id');
$expected = [
['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
];
$this->assertHtml($expected, $result);
$result = $this->Form->getSourceValue('id');
$this->assertEquals('10', $result);
}
}

0 comments on commit 375a4e4

Please sign in to comment.
You can’t perform that action at this time.