Skip to content

Commit

Permalink
Update FormHelper and it's tests as per core changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Nov 15, 2019
1 parent 15817af commit 234b758
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 43 deletions.
18 changes: 11 additions & 7 deletions src/View/Helper/FormHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,13 @@ public function __construct(View $View, array $config = [])
/**
* Returns an HTML FORM element.
*
* @param mixed $model The context for which the form is being defined. Can
* be an ORM entity, ORM resultset, or an array of meta data. You can use false or null
* to make a model-less form.
* @param mixed $context The context for which the form is being defined.
* Can be a ContextInterface instance, ORM entity, ORM resultset, or an
* array of meta data. You can use `null` to make a context-less form.
* @param array $options An array of html attributes and options.
* @return string An formatted opening FORM tag.
*/
public function create($model = null, array $options = []): string
public function create($context = null, array $options = []): string
{
// @codeCoverageIgnoreStart
if (isset($options['horizontal'])) {
Expand All @@ -235,7 +235,7 @@ public function create($model = null, array $options = []): string
'templates' => [],
];

return parent::create($model, $this->_formAlignment($options));
return parent::create($context, $this->_formAlignment($options));
}

/**
Expand Down Expand Up @@ -651,8 +651,12 @@ public function staticControl(string $fieldName, array $options = []): string
return $static;
}

if ($secure === true) {
$this->_secure(true, $this->_secureFieldName($options['name']), (string)$options['val']);
if ($secure === true && $this->formProtector) {
$this->formProtector->addField(
$options['name'],
true,
(string)$options['val']
);
}

$options['type'] = 'hidden';
Expand Down
58 changes: 22 additions & 36 deletions tests/TestCase/View/Helper/FormHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,22 @@ public function setUp(): void
Configure::write('App.namespace', 'BootstrapUI\Test\TestCase\View\Helper');
Configure::delete('Asset');

$request = new ServerRequest(['url' => '/articles/add']);
$request = $request
->withRequestTarget('/articles/add')
->withParam('controller', 'articles')
->withParam('action', 'add')
->withAttribute('webroot', '')
->withAttribute('base', '');
$request = new ServerRequest([
'webroot' => '',
'base' => '',
'url' => '/articles/add',
'params' => [
'controller' => 'articles',
'action' => 'add',
'plugin' => null,
],
]);
$this->View = new View($request);
$this->Form = new FormHelper($this->View);

Router::reload();
Router::setRequest($request);

$this->article = [
'schema' => [
'id' => ['type' => 'integer'],
Expand Down Expand Up @@ -194,6 +200,8 @@ public function testSelectControl()
*/
public function testStaticControl()
{
$this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));

unset($this->article['required']['title']);
$this->article['defaults']['title'] = 'foo <u>bar</u>';
$this->Form->create($this->article);
Expand All @@ -216,7 +224,10 @@ public function testStaticControl()
'/div',
];
$this->assertHtml($expected, $result);
$this->assertSame(['title' => 'foo <u>bar</u>'], $this->Form->fields);
$this->assertSame(
['title' => 'foo <u>bar</u>'],
$this->Form->getFormProtector()->__debugInfo()['fields']
);

$this->Form->fields = [];

Expand Down Expand Up @@ -939,13 +950,6 @@ public function testBasicFormCreate()
'role' => 'form',
'action' => '/articles/add',
],
'div' => ['style' => 'display:none;'],
'input' => [
'type' => 'hidden',
'name' => '_method',
'value' => 'POST',
],
'/div',
];
$this->assertHtml($expected, $result);

Expand Down Expand Up @@ -1006,13 +1010,6 @@ public function testInlineFormCreate()
'action' => '/articles/add',
'class' => 'form-inline',
],
'div' => ['style' => 'display:none;'],
'input' => [
'type' => 'hidden',
'name' => '_method',
'value' => 'POST',
],
'/div',
];
$this->assertHtml($expected, $result);
}
Expand Down Expand Up @@ -1069,13 +1066,6 @@ public function testHorizontalFormCreate()
'action' => '/articles/add',
'class' => 'form-horizontal',
],
'div' => ['style' => 'display:none;'],
'input' => [
'type' => 'hidden',
'name' => '_method',
'value' => 'POST',
],
'/div',
];
$this->assertHtml($expected, $result);

Expand Down Expand Up @@ -1217,13 +1207,6 @@ public function testHorizontalFormCreateFromConfig()
'action' => '/articles/add',
'class' => 'form-horizontal',
],
'div' => ['style' => 'display:none;'],
'input' => [
'type' => 'hidden',
'name' => '_method',
'value' => 'POST',
],
'/div',
];
$this->assertHtml($expected, $result);

Expand Down Expand Up @@ -1830,6 +1813,7 @@ public function testDefaultAlignDatetimeControlTime()
'name' => 'created',
'id' => 'created',
'class' => 'form-control',
'step' => '1',
'value' => date('H:i:s', $now),
],
'/div',
Expand Down Expand Up @@ -2026,6 +2010,7 @@ public function testHorizontalAlignDatetimeControlTime()
'name' => 'created',
'id' => 'created',
'class' => 'form-control',
'step' => '1',
'value' => date('H:i:s', $now),
],
'/div',
Expand Down Expand Up @@ -2223,6 +2208,7 @@ public function testInlineAlignDatetimeControlTime()
'name' => 'created',
'id' => 'created',
'class' => 'form-control',
'step' => '1',
'value' => date('H:i:s', $now),
],
'/div',
Expand Down

0 comments on commit 234b758

Please sign in to comment.