Skip to content
Permalink
Browse files

fixed default/schemaDefault null-case scenario (isset not ==! null), …

…added tests for that
  • Loading branch information...
Jonas Hartmann
Jonas Hartmann committed Aug 26, 2016
1 parent cb979eb commit 4eecbfee0ef30bc2cd3645b70bf8dd47b1d8412c
Showing with 76 additions and 3 deletions.
  1. +3 −3 src/View/Helper/FormHelper.php
  2. +73 −0 tests/TestCase/View/Helper/FormHelperTest.php
@@ -2727,7 +2727,7 @@ public function setValuesSources($sources)
*
* @param string $fieldname The fieldname to fetch the value for.
* @param array|null $options The options containing default values.
* @return string|null Field value derived from sources.
* @return string|null Field value derived from sources or defaults.
*/
public function getSourceValue($fieldname, $options = [])
{
@@ -2739,10 +2739,10 @@ public function getSourceValue($fieldname, $options = [])
return $this->request->{$valuesSource}($fieldname);
}
}
if ($options['default'] !== null && $options['default'] !== false) {
if (isset($options['default'])) {
return $options['default'];
}
if ($options['schemaDefault'] !== null && $options['schemaDefault'] !== false) {
if (isset($options['schemaDefault'])) {
return $options['schemaDefault'];
}
}
@@ -8483,4 +8483,77 @@ public function testFormValuesSourcesResetViaEnd()
$result = $this->Form->getValuesSources();
$this->assertEquals($expected, $result);
}
/**
* Test sources values schema defaults handling
*
* @return void
*/
public function testFormValuesSourcesSchemaDefaults()
{
$Articles = TableRegistry::get('Articles');
$entity = $Articles->newEntity();
$this->Form->create($entity);
$result = $this->Form->getSourceValue('title');
$expected = '';
$this->assertEquals($expected, $result);
$Articles = TableRegistry::get('Articles');
$title = $Articles->schema()->column('title');
$Articles->schema()->addColumn(
'title',
['default' => 'default title'] + $title
);
$entity = $Articles->newEntity();
$this->Form->create($entity);
$result = $this->Form->getSourceValue('title');
$expected = 'default title';
$this->assertEquals($expected, $result);
$Articles = TableRegistry::get('Articles');
$title = $Articles->schema()->column('title');
$Articles->schema()->addColumn(
'title',
['default' => 'default title'] + $title
);
$entity = $Articles->newEntity();
$this->Form->create($entity);
$this->Form->setValuesSources(['query']);
$result = $this->Form->getSourceValue('title');
$expected = '';
$this->assertEquals($expected, $result);
$this->Form->setValuesSources(['context']);
$result = $this->Form->getSourceValue('title');
$expected = 'default title';
$this->assertEquals($expected, $result);
}
/**
* Test sources values defaults handling
*
* @return void
*/
public function testFormValuesSourcesDefaults()
{
$this->Form->request->query['password'] = 'open Sesame';
$this->Form->create();
$result = $this->Form->password('password');
$expected = ['input' => ['type' => 'password', 'name' => 'password']];
$this->assertHtml($expected, $result);
$result = $this->Form->password('password', ['default' => 'helloworld']);
$expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
$this->assertHtml($expected, $result);
$this->Form->setValuesSources('query');
$result = $this->Form->password('password', ['default' => 'helloworld']);
$expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
$this->assertHtml($expected, $result);
$this->Form->setValuesSources('data');
$result = $this->Form->password('password', ['default' => 'helloworld']);
$expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
$this->assertHtml($expected, $result);
}
}

0 comments on commit 4eecbfe

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