From 96ef936ebf63be97ced1b7496f27844e184d6127 Mon Sep 17 00:00:00 2001 From: Erwane Breton Date: Mon, 16 Nov 2020 16:55:28 +0100 Subject: [PATCH] FormHelper keep Cake FormHelper default config and wigets --- .editorconfig | 20 ++ src/View/Helper/FormHelper.php | 127 ++++---- tests/TestCase/View/Helper/FormHelperTest.php | 299 ++++++++++++------ 3 files changed, 278 insertions(+), 168 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..b6ed45a --- /dev/null +++ b/.editorconfig @@ -0,0 +1,20 @@ +; This file is for unifying the coding style for different editors and IDEs. +; More information at http://editorconfig.org + +root = true + +[*] +indent_style = space +indent_size = 4 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.bat] +end_of_line = crlf + +[*.yml] +indent_size = 2 + +[Makefile] +indent_style = tab diff --git a/src/View/Helper/FormHelper.php b/src/View/Helper/FormHelper.php index ebcc07e..94a80b6 100644 --- a/src/View/Helper/FormHelper.php +++ b/src/View/Helper/FormHelper.php @@ -13,6 +13,8 @@ namespace Bootstrap\View\Helper; use Bootstrap\View\FlexibleStringTemplateTrait; +use Cake\Utility\Hash; +use Cake\View\View; /** * Form helper library. @@ -21,7 +23,6 @@ * * @property \Bootstrap\View\Helper\HtmlHelper $Html * @property \Cake\View\Helper\UrlHelper $Url - * * @link http://book.cakephp.org/3.0/en/views/helpers/form.html */ class FormHelper extends \Cake\View\Helper\FormHelper @@ -40,68 +41,52 @@ class FormHelper extends \Cake\View\Helper\FormHelper ]; /** - * Default configuration for the helper. - * - * - `idPrefix` See CakePHP `FormHelper`. - * - `errorClass` See CakePHP `FormHelper`. Overriden by `'has-error'`. - * - `typeMap` See CakePHP `FormHelper`. - * - `templates` Templates for the various form elements. - * - `templateClass` Class used to format the various template. Do not override! - * - `buttons` Default options for buttons. - * - `columns` Default column sizes for horizontal forms. - * - `useCustomFileInput` Set to `true` to use the custom file input. Default is `false`. + * Default configuration for this helper. + * Don't override parent::$_defaultConfig for robustness * * @var array */ - protected $_defaultConfig = [ - 'idPrefix' => null, + protected $helperConfig = [ 'errorClass' => 'is-invalid', - 'typeMap' => [ - 'string' => 'text', 'datetime' => 'datetime', 'boolean' => 'checkbox', - 'timestamp' => 'datetime', 'text' => 'textarea', 'time' => 'time', - 'date' => 'date', 'float' => 'number', 'integer' => 'number', - 'decimal' => 'number', 'binary' => 'file', 'uuid' => 'string', - ], - 'templates' => [ - 'button' => '{{text}}', + // Used for checkboxes in checkbox() and multiCheckbox(). 'checkbox' => '', - 'checkboxFormGroup' => '{{label}}', + // Wrapper container for checkboxes. 'checkboxWrapper' => '
{{label}}
', 'checkboxContainer' => '
{{content}}
', 'checkboxContainerHorizontal' => '
{{content}}
', 'multicheckboxContainer' => '
{{content}}
', 'multicheckboxContainerHorizontal' => '
{{content}}
','dateWidget' => '
{{year}}{{month}}{{day}}{{hour}}{{minute}}{{second}}{{meridian}}
', + // Error message wrapper elements. 'error' => '
{{content}}
', 'errorInline' => '
{{content}}
', - 'errorList' => '', - 'errorItem' => '
  • {{text}}
  • ', - 'file' => '', - 'fieldset' => '{{content}}', - 'formStart' => '', - 'formEnd' => '', + // General grouping container for control(). Defines input/label ordering. 'formGroup' => '{{label}}{{prepend}}{{input}}{{append}}', 'formGroupHorizontal' => '{{label}}
    {{prepend}}{{input}}{{append}}{{error}}
    ', - 'hiddenBlock' => '
    {{content}}
    ', + // Generic input element. 'input' => '', - 'inputSubmit' => '', + // Container element used by control(). 'inputContainer' => '
    {{content}}
    ', 'inputContainerHorizontal' => '
    {{content}}
    ', + // Container element used by control() when a field has an error. 'inputContainerError' => '
    {{content}}{{error}}
    ', 'inputContainerErrorHorizontal' => '
    {{content}}
    ', - 'label' => '{{text}}', + // Label horizontal 'labelHorizontal' => '', + // Label inline 'labelInline' => '', + // Label element used for radio and multi-checkbox inputs. 'nestingLabel' => '{{hidden}}', - 'legend' => '{{text}}', 'labelLegend' => '{{text}}', 'labelLegendHorizontal' => '{{text}}', - 'option' => '', - 'optgroup' => '{{content}}', - 'select' => '', - 'selectColumn' => '
    ', - 'selectMultiple' => '', + // Select element, + 'select' => '', + 'selectColumn' => '
    ', + // Multi-select element, + 'selectMultiple' => '', + // Radio input element, 'radio' => '', + // Wrapping container for radio input/label, 'radioWrapper' => '
    {{label}}
    ', 'radioContainer' => '
    {{content}}
    ', 'radioContainerHorizontal' => '
    {{content}}
    ', @@ -109,7 +94,9 @@ class FormHelper extends \Cake\View\Helper\FormHelper 'inlineRadioWrapper' => '
    {{label}}
    ', 'inlineradioContainer' => '
    {{content}}
    ', 'inlineradioContainerHorizontal' => '
    {{content}}
    ', - 'textarea' => '', + // Textarea input element, + 'textarea' => '', + // Container for submit buttons. 'submitContainer' => '
    {{content}}
    ', 'submitContainerHorizontal' => '
    {{content}}
    ', @@ -123,7 +110,6 @@ class FormHelper extends \Cake\View\Helper\FormHelper 'buttonGroupVertical' => '
    {{content}}
    ', 'buttonToolbar' => '', 'fancyFileInput' => '{{fileInput}}
    {{button}}
    {{input}}
    ', - 'confirmJs' => '{{confirm}}', ], 'buttons' => [ 'type' => 'primary', @@ -138,25 +124,16 @@ class FormHelper extends \Cake\View\Helper\FormHelper ]; /** - * Default widgets. + * Default widgets for this helper. + * Don't override parent::$_defaultWidgets for robustness * * @var array */ - protected $_defaultWidgets = [ - '_default' => ['Cake\View\Widget\BasicWidget'], - 'button' => ['Cake\View\Widget\ButtonWidget'], - 'checkbox' => ['Cake\View\Widget\CheckboxWidget'], - 'file' => ['Cake\View\Widget\FileWidget'], + protected $helperWidgets = [ 'fancyFile' => ['Bootstrap\View\Widget\FancyFileWidget', 'file', 'button', 'basic'], 'label' => ['Bootstrap\View\Widget\LabelLegendWidget'], - 'nestingLabel' => ['Cake\View\Widget\NestingLabelWidget'], - 'multicheckbox' => ['Cake\View\Widget\MultiCheckboxWidget', 'nestingLabel'], - 'radio' => ['Cake\View\Widget\RadioWidget', 'nestingLabel'], - 'inlineRadioNestingLabel' => ['Bootstrap\View\Widget\InlineRadioNestingLabelWidget'], 'inlineRadio' => ['Bootstrap\View\Widget\InlineRadioWidget', 'nestingLabel'], - 'select' => ['Cake\View\Widget\SelectBoxWidget'], 'selectColumn' => ['Bootstrap\View\Widget\ColumnSelectBoxWidget'], - 'textarea' => ['Cake\View\Widget\TextareaWidget'], ]; /** @@ -174,10 +151,16 @@ class FormHelper extends \Cake\View\Helper\FormHelper public $inline = false; /** - * {@inheritDoc} + * @inheritDoc */ - public function __construct(\Cake\View\View $View, array $config = []) + public function __construct(View $View, array $config = []) { + // Default config. Use Hash::merge() to keep default values + $this->_defaultConfig = Hash::merge($this->_defaultConfig, $this->helperConfig); + + // Default widgets. Use array_merge to avoid digit key problems + $this->_defaultWidgets = array_merge($this->_defaultWidgets, $this->helperWidgets); + if (!isset($config['templateCallback'])) { $that = $this; $config['templateCallback'] = function ($name, $data) use ($that) { @@ -188,8 +171,8 @@ public function __construct(\Cake\View\View $View, array $config = []) $data['templateName'] .= 'Inline'; } $data += [ - 'inputColumnClass' => $this->_getColumnClass('input'), - 'labelColumnClass' => $this->_getColumnClass('label'), + 'inputColumnClass' => $this->getColumnClass('input'), + 'labelColumnClass' => $this->getColumnClass('label'), ]; if (!$that->getTemplates($data['templateName'])) { $data['templateName'] = $name; @@ -268,9 +251,9 @@ public function getColumnSizes(): array * form helper. * * @param array $columns Array of columns options to set - * @return array + * @return $this */ - public function setColumnSizes(array $columns): array + public function setColumnSizes(array $columns) { return $this->setConfig('columns', $columns, false); } @@ -282,7 +265,7 @@ public function setColumnSizes(array $columns): array * @param string $what The type of the column (`'label'`, `'input'`, `'error'`). * @return string The classes for the size or offset of the specified column. */ - protected function _getColumnClass(string $what): string + public function getColumnClass(string $what): string { $columns = $this->getConfig('columns'); $classes = []; @@ -310,7 +293,7 @@ protected function _getColumnClass(string $what): string * @param string $type Input group type * @return string|null The elements wrapped in a suitable HTML element. */ - protected function _wrapInputGroup($addonOrButtons, string $type): ?string + protected function wrapInputGroup($addonOrButtons, string $type): ?string { if ($addonOrButtons) { if (is_array($addonOrButtons)) { @@ -340,12 +323,12 @@ protected function _wrapInputGroup($addonOrButtons, string $type): ?string * Concatenates and wraps `$input`, `$prepend` and `$append` inside an input group. * * @param string $input The input content. - * @param string $prepend The content to prepend to `$input`. - * @param string $append The content to append to `$input`. + * @param string|null $prepend The content to prepend to `$input`. + * @param string|null $append The content to append to `$input`. * @return string A string containing the three elements concatenated an wrapped inside * an input group `
    `. */ - protected function _wrap(string $input, string $prepend, string $append): string + protected function formatWrap(string $input, ?string $prepend = null, ?string $append = null): string { return $this->formatTemplate('inputGroup', [ 'inputGroupStart' => $this->formatTemplate('inputGroupStart', [ @@ -363,18 +346,18 @@ protected function _wrap(string $input, string $prepend, string $append): string * * @param string|null $input Input to which `$prepend` will be prepend, or * null to create an opening input group. - * @param string|array $prepend The content to prepend., + * @param string|array|null $prepend The content to prepend., * @return string The input with the content of `$prepend` prepended or an * opening `
    ` for an input group. */ - public function prepend(?string $input, $prepend): string + public function prepend(?string $input = null, $prepend = null): string { - $prepend = $this->_wrapInputGroup($prepend, 'prepend'); + $prepend = $this->wrapInputGroup($prepend, 'prepend'); if ($input === null) { return $this->formatTemplate('inputGroupStart', ['prepend' => $prepend]); } - return $this->_wrap($input, $prepend, null); + return $this->formatWrap($input, $prepend, null); } /** @@ -386,14 +369,14 @@ public function prepend(?string $input, $prepend): string * @return string The input with the content of `$append` appended or a * closing `
    ` for an input group. */ - public function append(?string $input, $append = null): string + public function append(?string $input = null, $append = null): string { - $append = $this->_wrapInputGroup($append, 'append'); + $append = $this->wrapInputGroup($append, 'append'); if ($input === null) { return $this->formatTemplate('inputGroupEnd', ['append' => $append]); } - return $this->_wrap($input, null, $append); + return $this->formatWrap($input, null, $append); } /** @@ -505,7 +488,7 @@ public function control(string $fieldName, array $options = []): string } /** - * {@inheritDoc} + * @inheritDoc */ protected function _getInput(string $fieldName, array $options) { @@ -522,7 +505,7 @@ protected function _getInput(string $fieldName, array $options) } /** - * {@inheritDoc} + * @inheritDoc */ protected function _inputLabel(string $fieldName, $label = null, array $options = []): string { @@ -552,7 +535,6 @@ protected function _inputLabel(string $fieldName, $label = null, array $options * @param string $fieldName Name of a field, like this "modelname.fieldname" * @param array|\Traversable $options Radio button options array. * @param array $attributes Array of attributes. - * * @return string Completed radio widget set. */ public function inlineRadio(string $fieldName, $options = [], array $attributes = []): string @@ -715,7 +697,6 @@ public function buttonToolbar(array $buttonGroups, array $options = []): string * @param array $menu HTML elements corresponding to menu options (which will be wrapped * into `
  • ` tag). To add separator, pass 'divider'. See `BootstrapHtml::dropdown()`. * @param array $options Array of options for the button. See `button()`. - * * @return string A HTML string containing the button dropdown. */ public function dropdownButton(string $title, array $menu = [], array $options = []): string diff --git a/tests/TestCase/View/Helper/FormHelperTest.php b/tests/TestCase/View/Helper/FormHelperTest.php index e0bec67..c1165e2 100644 --- a/tests/TestCase/View/Helper/FormHelperTest.php +++ b/tests/TestCase/View/Helper/FormHelperTest.php @@ -13,9 +13,9 @@ namespace Bootstrap\Test\TestCase\View\Helper; use Bootstrap\View\Helper\FormHelper; -use Cake\Core\Configure; use Cake\Http\ServerRequest; use Cake\TestSuite\TestCase; +use Cake\Utility\Inflector; use Cake\View\View; class FormHelperTest extends TestCase @@ -28,10 +28,20 @@ class FormHelperTest extends TestCase /** * Instance of FormHelper. * - * @var FormHelper + * @var \Bootstrap\View\Helper\FormHelper */ public $form; + /** + * @var string[] + */ + private $dateRegex; + + /** + * @var array + */ + private $article; + /** * Setup * @@ -79,14 +89,54 @@ public function setUp(): void 'title' => true, ], ]; + } + + /** + * @test + */ + public function testLoadHelperWithConfiguration() + { + $this->View->loadHelper('Form', [ + 'className' => 'Bootstrap.Form', + 'templates' => ['checkboxFormGroup' => '{{label}}'], + 'widgets' => [ + // override select + 'select' => ['Textarea'], + ] + ]); + $helper = $this->View->Form; - Configure::write('debug', true); + // \Cake\View\Helper\FormHelper configuration is untouched + self::assertSame('{{text}}', $helper->getConfig('templates.button')); + + // \Bootstrap\View\Helper\FormHelper default configuration is loaded + self::assertSame('{{append}}
  • ', $helper->getConfig('templates.inputGroupEnd')); + + // Custom configuration correctly applied + self::assertSame('{{label}}', $helper->getConfig('templates.checkboxFormGroup')); + + $widgets = $helper->getWidgetLocator(); + + // \Cake\View\Helper\FormHelper widgets are untouched + self::assertInstanceOf('Cake\View\Widget\DateTimeWidget', $widgets->get('datetime')); + + // \Bootstrap\View\Helper\FormHelper default widgets are set + self::assertInstanceOf('Bootstrap\View\Widget\FancyFileWidget', $widgets->get('fancyFile')); + self::assertInstanceOf('Bootstrap\View\Widget\LabelLegendWidget', $widgets->get('label')); + self::assertInstanceOf('Bootstrap\View\Widget\InlineRadioWidget', $widgets->get('inlineRadio')); + self::assertInstanceOf('Bootstrap\View\Widget\ColumnSelectBoxWidget', $widgets->get('selectColumn')); + + // Custom widgets are correctly applied + self::assertInstanceOf('Cake\View\Widget\TextareaWidget', $widgets->get('select')); } + /** + * @test + */ public function testCreate() { // Standard form - $this->assertHtml([ + self::assertHtml([ ['form' => [ 'method', 'accept-charset', @@ -94,16 +144,19 @@ public function testCreate() 'action', ]], ], $this->form->create()); + // Horizontal form - $result = $this->form->create(null, ['horizontal' => true]); - $this->assertEquals($this->form->horizontal, true); - // Automatically return to non horizonal form - $result = $this->form->create(); - $this->assertEquals($this->form->horizontal, false); + $this->form->create(null, ['horizontal' => true]); + self::assertEquals(true, $this->form->horizontal); + + // Automatically return to non horizontal form + $this->form->create(); + self::assertEquals(false, $this->form->horizontal); + // Inline form $result = $this->form->create(null, ['inline' => true]); - $this->assertEquals($this->form->inline, true); - $this->assertHtml([ + self::assertEquals(true, $this->form->inline); + self::assertHtml([ ['form' => [ 'method', 'accept-charset', @@ -112,11 +165,15 @@ public function testCreate() 'class' => 'form-inline', ]], ], $result); - // Automatically return to non horizonal form - $result = $this->form->create(); - $this->assertEquals($this->form->inline, false); + + // Automatically return to non horizontal form + $this->form->create(); + self::assertEquals(false, $this->form->inline); } + /** + * @test + */ public function testColumnSizes() { $this->form->setConfig('columns', [ @@ -153,7 +210,7 @@ public function testColumnSizes() '/div', '/div', ]; - $this->assertHtml($expected, $result); + self::assertHtml($expected, $result); $this->article['errors'] = [ 'Article' => [ @@ -201,14 +258,17 @@ public function testColumnSizes() '/div', '/div', ]; - $this->assertHtml($expected, $result, true); + self::assertHtml($expected, $result, true); } + /** + * @test + */ public function testButton() { // default button $button = $this->form->button('Test'); - $this->assertHtml([ + self::assertHtml([ ['button' => [ 'class' => 'btn btn-primary', 'type' => 'submit', @@ -219,7 +279,7 @@ public function testButton() 'bootstrap-type' => 'success', 'bootstrap-size' => 'sm', ]); - $this->assertHtml([ + self::assertHtml([ ['button' => [ 'class' => 'btn btn-success btn-sm', 'type' => 'submit', @@ -230,7 +290,7 @@ public function testButton() 'btype' => 'success', 'size' => 'sm', ]); - $this->assertHtml([ + self::assertHtml([ ['button' => [ 'class' => 'btn btn-success btn-sm', 'type' => 'submit', @@ -240,7 +300,7 @@ public function testButton() $button = $this->form->button('Test', [ 'class' => 'btn btn-primary', ]); - $this->assertHtml([ + self::assertHtml([ ['button' => [ 'class' => 'btn btn-primary', 'type' => 'submit', @@ -248,27 +308,38 @@ public function testButton() ], $button); } + /** + * @test + */ public function testCustomFunctions() { - $this->assertEquals( + self::assertEquals( $this->form->cbutton('b', ['class' => 'cl']), $this->form->button('b', ['class' => 'cl']) ); - $this->assertEquals( + self::assertEquals( $this->form->cbutton('b', 'danger', ['class' => 'cl']), $this->form->button('b', ['class' => 'cl', 'btype' => 'danger']) ); - $this->assertEquals( + self::assertEquals( $this->form->csubmit('b', ['class' => 'cl']), $this->form->submit('b', ['class' => 'cl']) ); - $this->assertEquals( + self::assertEquals( $this->form->csubmit('b', 'danger', ['class' => 'cl']), $this->form->submit('b', ['class' => 'cl', 'btype' => 'danger']) ); } - protected function _testInput($expected, $fieldName, $options = [], $debug = false) + /** + * Check Form::control() output + * + * @param array $expected Expected Html + * @param string $fieldName Field name + * @param array $options Field options + * @param bool $debug Activate Html debug mode + */ + public function assertControl(array $expected, string $fieldName, array $options = [], bool $debug = false) { $formOptions = []; if (isset($options['_formOptions'])) { @@ -277,21 +348,24 @@ protected function _testInput($expected, $fieldName, $options = [], $debug = fal } $this->form->create(null, $formOptions); $result = $this->form->control($fieldName, $options); - $assert = $this->assertHtml($expected, $result, $debug); + self::assertHtml($expected, $result, $debug); } + /** + * @test + */ public function testInput() { $fieldName = 'field'; // Standard form - $this->_testInput([ + $this->assertControl([ ['div' => [ 'class' => 'form-group text', ]], ['label' => [ 'for' => $fieldName, ]], - \Cake\Utility\Inflector::humanize($fieldName), + Inflector::humanize($fieldName), '/label', ['input' => [ 'type' => 'text', @@ -302,7 +376,7 @@ public function testInput() '/div', ], $fieldName); // Horizontal form - $this->_testInput([ + $this->assertControl([ ['div' => [ 'class' => 'form-group row text', ]], @@ -310,7 +384,7 @@ public function testInput() 'class' => 'col-form-label col-md-2', 'for' => $fieldName, ]], - \Cake\Utility\Inflector::humanize($fieldName), + Inflector::humanize($fieldName), '/label', ['div' => [ 'class' => 'col-md-10', @@ -328,17 +402,20 @@ public function testInput() ]); } + /** + * @test + */ public function testInputText() { $fieldName = 'field'; - $this->_testInput([ + $this->assertControl([ ['div' => [ 'class' => 'form-group text', ]], ['label' => [ 'for' => $fieldName, ]], - \Cake\Utility\Inflector::humanize($fieldName), + Inflector::humanize($fieldName), '/label', ['input' => [ 'type' => 'text', @@ -350,6 +427,9 @@ public function testInputText() ], $fieldName, ['type' => 'text']); } + /** + * @test + */ public function testButtonGroup() { // Basic test: @@ -361,7 +441,7 @@ public function testButtonGroup() ['button' => ['class' => 'btn btn-primary', 'type' => 'submit']], '2', '/button', '/div', ]; - $this->assertHtml($expected, $this->form->buttonGroup([ + self::assertHtml($expected, $this->form->buttonGroup([ $this->form->button('1'), $this->form->button('2'), ])); @@ -374,7 +454,7 @@ public function testButtonGroup() ['button' => ['class' => 'btn btn-primary', 'type' => 'submit']], '2', '/button', '/div', ]; - $this->assertHtml($expected, $this->form->buttonGroup([ + self::assertHtml($expected, $this->form->buttonGroup([ $this->form->button('1'), $this->form->button('2'), ], ['class' => 'myclass', 'data-test' => 'mydata'])); @@ -387,11 +467,14 @@ public function testButtonGroup() ['button' => ['class' => 'btn btn-primary', 'type' => 'submit']], '2', '/button', '/div', ]; - $this->assertHtml($expected, $this->form->buttonGroup([ + self::assertHtml($expected, $this->form->buttonGroup([ $this->form->button('1'), $this->form->button('2'), ], ['class' => 'myclass', 'data-test' => 'mydata', 'vertical' => true])); } + /** + * @test + */ public function testInputRadio() { $fieldName = 'color'; @@ -409,7 +492,7 @@ public function testInputRadio() 'class' => 'form-group radio', ]], ['label' => []], - \Cake\Utility\Inflector::humanize($fieldName), + Inflector::humanize($fieldName), '/label', ['input' => [ 'type' => 'hidden', @@ -440,7 +523,7 @@ public function testInputRadio() ]); } $expected = array_merge($expected, ['/fieldset']); - $this->_testInput($expected, $fieldName, $options); + $this->assertControl($expected, $fieldName, $options); // Inline $options += [ @@ -451,7 +534,7 @@ public function testInputRadio() 'class' => 'form-group inlineradio', ]], ['label' => [ ]], - \Cake\Utility\Inflector::humanize($fieldName), + Inflector::humanize($fieldName), '/label', ['input' => [ 'type' => 'hidden', @@ -482,7 +565,7 @@ public function testInputRadio() ]); } $expected = array_merge($expected, ['/fieldset']); - $this->_testInput($expected, $fieldName, $options); + $this->assertControl($expected, $fieldName, $options); // Horizontal $options += [ @@ -499,7 +582,7 @@ public function testInputRadio() ['legend' => [ 'class' => 'col-form-label pt-0 col-md-2', ]], - \Cake\Utility\Inflector::humanize($fieldName), + Inflector::humanize($fieldName), '/legend', ['div' => [ 'class' => 'col-md-10', @@ -533,7 +616,7 @@ public function testInputRadio() ]); } $expected = array_merge($expected, ['/div', '/div', '/fieldset']); - $this->_testInput($expected, $fieldName, $options); + $this->assertControl($expected, $fieldName, $options); // Horizontal + Inline $options['inline'] = true; @@ -547,7 +630,7 @@ public function testInputRadio() ['legend' => [ 'class' => 'col-form-label pt-0 col-md-2', ]], - \Cake\Utility\Inflector::humanize($fieldName), + Inflector::humanize($fieldName), '/legend', ['div' => [ 'class' => 'col-md-10', @@ -581,9 +664,12 @@ public function testInputRadio() ]); } $expected = array_merge($expected, ['/div', '/div', '/fieldset']); - $this->_testInput($expected, $fieldName, $options); + $this->assertControl($expected, $fieldName, $options); } + /** + * @test + */ public function testInputCheckbox() { $fieldName = 'color'; @@ -612,11 +698,11 @@ public function testInputCheckbox() 'value' => "1", 'id' => $fieldName, ]], - \Cake\Utility\Inflector::humanize($fieldName), + Inflector::humanize($fieldName), '/label', '/div', ]; - $this->_testInput($expected, $fieldName, $options); + $this->assertControl($expected, $fieldName, $options); // Horizontal $expected = [ @@ -647,17 +733,20 @@ public function testInputCheckbox() 'value' => "1", 'id' => $fieldName, ]], - \Cake\Utility\Inflector::humanize($fieldName), + Inflector::humanize($fieldName), '/label', '/div', '/div', '/div', ]; - $this->_testInput($expected, $fieldName, $options + [ + $this->assertControl($expected, $fieldName, $options + [ '_formOptions' => ['horizontal' => true], ], true); } + /** + * @test + */ public function testInputGroup() { $fieldName = 'field'; @@ -691,7 +780,7 @@ public function testInputGroup() '/div', '/div', ]; - $this->_testInput($expected, $fieldName, $options + ['prepend' => '@']); + $this->assertControl($expected, $fieldName, $options + ['prepend' => '@']); // Test with append $expected = [ ['div' => [ @@ -718,7 +807,7 @@ public function testInputGroup() '/div', '/div', ]; - $this->_testInput($expected, $fieldName, $options + ['append' => '.00']); + $this->assertControl($expected, $fieldName, $options + ['append' => '.00']); // Test with append + prepend $expected = [ ['div' => [ @@ -754,7 +843,7 @@ public function testInputGroup() '/div', '/div', ]; - $this->_testInput( + $this->assertControl( $expected, $fieldName, $options + ['prepend' => '$', 'append' => '.00'] @@ -787,7 +876,7 @@ public function testInputGroup() '/div', ]; - $this->_testInput( + $this->assertControl( $expected, $fieldName, $options + ['prepend' => $this->form->button('Go!')] @@ -820,7 +909,7 @@ public function testInputGroup() '/div', '/div', ]; - $this->_testInput( + $this->assertControl( $expected, $fieldName, $options + ['append' => $this->form->button('Go!')] @@ -858,11 +947,14 @@ public function testInputGroup() '/div', '/div', ]; - $this->_testInput($expected, $fieldName, $options + [ + $this->assertControl($expected, $fieldName, $options + [ 'append' => [$this->form->button('Go!'), $this->form->button('GoGo!')], ]); } + /** + * @test + */ public function testAppendDropdown() { $fieldName = 'field'; @@ -912,7 +1004,7 @@ public function testAppendDropdown() '/div', '/div', ]; - $this->_testInput($expected, $fieldName, $options + [ + $this->assertControl($expected, $fieldName, $options + [ 'append' => $this->form->dropdownButton('Action', [ ['item' => ['title' => 'Link 1', 'url' => '#']], ['item' => ['title' => 'Link 2', 'url' => '#']], @@ -963,7 +1055,7 @@ public function testAppendDropdown() '/div', '/div', ]; - $this->_testInput($expected, $fieldName, $options + [ + $this->assertControl($expected, $fieldName, $options + [ 'append' => $this->form->dropdownButton('Action', [ ['item' => ['title' => 'Link 1', 'url' => '#']], ['item' => ['title' => 'Link 2', 'url' => '#']], @@ -973,6 +1065,9 @@ public function testAppendDropdown() ]); } + /** + * @test + */ public function testInputTemplateVars() { $fieldName = 'field'; @@ -982,14 +1077,14 @@ public function testInputTemplateVars() 'inputContainer' => '
    {{content}}{{help}}
    ', ]); // Standard form - $this->_testInput([ + $this->assertControl([ ['div' => [ 'class' => 'form-group text', ]], ['label' => [ 'for' => $fieldName, ]], - \Cake\Utility\Inflector::humanize($fieldName), + Inflector::humanize($fieldName), '/label', ['input' => [ 'type' => 'text', @@ -1004,23 +1099,26 @@ public function testInputTemplateVars() ], $fieldName, ['templateVars' => ['help' => $help]]); } + /** + * @test + */ public function testDateTime() { extract($this->dateRegex); - $now = strtotime('now'); $result = $this->form->dateTime('Contact.date'); $expected = [ ['input' => [ 'type' => 'datetime-local', 'name' => 'Contact[date]', 'class' => 'form-control', + 'step' => '1', + 'value' => '', ]], ]; - $this->assertHtml($expected, $result); + self::assertHtml($expected, $result); // Test with input() - $now = strtotime('now'); $result = $this->form->control('Contact.date', ['type' => 'date']); $expected = [ ['div' => [ @@ -1034,12 +1132,16 @@ public function testDateTime() 'name' => 'Contact[date]', 'class' => 'form-control', 'id' => 'contact-date', + 'value' => '', ]], '/div', ]; - $this->assertHtml($expected, $result); + self::assertHtml($expected, $result); } + /** + * @test + */ public function testSubmit() { $this->form->horizontal = false; @@ -1053,7 +1155,7 @@ public function testSubmit() ]], '/div', ]; - $this->assertHtml($expected, $result); + self::assertHtml($expected, $result); // horizontal forms $this->form->horizontal = true; @@ -1070,42 +1172,43 @@ public function testSubmit() '/div', '/div', ]; - $this->assertHtml($expected, $result); + self::assertHtml($expected, $result); } + /** + * @test + */ public function testCustomFileInput() { $this->form->setConfig('useCustomFileInput', true); $result = $this->form->file('Contact.picture'); $expected = [ - ['input' => [ - 'type' => 'file', - 'name' => 'Contact[picture]', - 'id' => 'Contact[picture]', - 'style' => 'display: none;', - 'onchange' => "document.getElementById('Contact[picture]-input').value = (this.files.length <= 1) ? (this.files.length ? this.files[0].name : '') : this.files.length + ' ' + 'files selected';", - ]], - ['div' => ['class' => 'input-group']], - ['div' => ['class' => 'input-group-btn']], - ['button' => [ - 'class' => 'btn btn-primary', - 'type' => 'button', - 'onclick' => "document.getElementById('Contact[picture]').click();", - ]], - __('Choose File'), - '/button', - '/div', - ['input' => [ - 'type' => 'text', - 'name' => 'Contact[picture-text]', - 'class' => 'form-control', - 'readonly' => 'readonly', - 'id' => 'Contact[picture]-input', - 'onclick' => "document.getElementById('Contact[picture]').click();", - ]], - '/div', + [ + 'input' => [ + 'type' => 'file', + 'name' => 'Contact[picture]', + 'id' => 'Contact[picture]', + 'style' => 'display: none;', + 'onchange' => "document.getElementById('Contact[picture]-input').value = (this.files.length <= 1) ? (this.files.length ? this.files[0].name : '') : this.files.length + ' ' + 'files selected';", + ], + ], ['div' => ['class' => 'input-group']], ['div' => ['class' => 'input-group-btn']], [ + 'button' => [ + 'class' => 'btn btn-primary', + 'type' => 'button', + 'onclick' => "document.getElementById('Contact[picture]').click();", + ], + ], __('Choose File'), '/button', '/div', [ + 'input' => [ + 'type' => 'text', + 'name' => 'Contact[picture-text]', + 'class' => 'form-control', + 'readonly' => 'readonly', + 'id' => 'Contact[picture]-input', + 'onclick' => "document.getElementById('Contact[picture]').click();", + ], + ], '/div' ]; - $this->assertHtml($expected, $result); + self::assertHtml($expected, $result); $result = $this->form->file('Contact.picture', ['multiple' => true]); $expected = [ @@ -1137,9 +1240,12 @@ public function testCustomFileInput() ]], '/div', ]; - $this->assertHtml($expected, $result); + self::assertHtml($expected, $result); } + /** + * @test + */ public function testUploadCustomFileInput() { $expected = [ @@ -1173,23 +1279,26 @@ public function testUploadCustomFileInput() $this->form->setConfig('useCustomFileInput', true); $result = $this->form->file('Contact.picture'); - $this->assertHtml($expected, $result); + self::assertHtml($expected, $result); $this->form->getView()->setRequest($this->form->getView()->getRequest()->withData('Contact.picture', [ 'name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0, ])); $result = $this->form->file('Contact.picture'); - $this->assertHtml($expected, $result); + self::assertHtml($expected, $result); $this->form->getView()->setRequest($this->form->getView()->getRequest()->withData( 'Contact.picture', 'no data should be set in value' )); $result = $this->form->file('Contact.picture'); - $this->assertHtml($expected, $result); + self::assertHtml($expected, $result); } + /** + * @test + */ public function testFormSecuredFileControl() { $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [ @@ -1204,6 +1313,6 @@ public function testFormSecuredFileControl() $tokenData = $this->form->getFormProtector()->buildTokenData(); - $this->assertSame('949a50880781bda6c5c21f4ef7e82548c682b7e8%3A', $tokenData['fields']); + self::assertSame('949a50880781bda6c5c21f4ef7e82548c682b7e8%3A', $tokenData['fields']); } }