['for' => 'contact-email'],
'Email',
'/label',
['input' => [
'type' => 'email', 'name' => 'Contact[email]',
'id' => 'contact-email', 'maxlength' => 255,
]],
'/div',
];
$this->assertHtml($expected, $result);
$result = $this->Form->control('Contact.email', ['type' => 'text']);
$expected = [
'div' => ['class' => 'input text'],
'label' => ['for' => 'contact-email'],
'Email',
'/label',
['input' => [
'type' => 'text', 'name' => 'Contact[email]',
'id' => 'contact-email', 'maxlength' => '255',
]],
'/div',
];
$this->assertHtml($expected, $result);
$result = $this->Form->control('Contact.5.email', ['type' => 'text']);
$expected = [
'div' => ['class' => 'input text'],
'label' => ['for' => 'contact-5-email'],
'Email',
'/label',
['input' => [
'type' => 'text', 'name' => 'Contact[5][email]',
'id' => 'contact-5-email', 'maxlength' => '255',
]],
'/div',
];
$this->assertHtml($expected, $result);
$result = $this->Form->control('Contact.password');
$expected = [
'div' => ['class' => 'input password'],
'label' => ['for' => 'contact-password'],
'Password',
'/label',
['input' => [
'type' => 'password', 'name' => 'Contact[password]',
'id' => 'contact-password',
]],
'/div',
];
$this->assertHtml($expected, $result);
$result = $this->Form->control('Contact.email', [
'type' => 'file', 'class' => 'textbox',
]);
$expected = [
'div' => ['class' => 'input file'],
'label' => ['for' => 'contact-email'],
'Email',
'/label',
['input' => [
'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
'id' => 'contact-email',
]],
'/div',
];
$this->assertHtml($expected, $result);
$entity = new Entity(['phone' => 'Hello & World > weird chars']);
$this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
$result = $this->Form->control('phone');
$expected = [
'div' => ['class' => 'input tel'],
'label' => ['for' => 'phone'],
'Phone',
'/label',
['input' => [
'type' => 'tel', 'name' => 'phone',
'value' => 'Hello & World > weird chars',
'id' => 'phone', 'maxlength' => 255,
]],
'/div',
];
$this->assertHtml($expected, $result);
$this->View->setRequest(
$this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
);
$this->Form->create();
$result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
$expected = [
'div' => ['class' => 'input text'],
'label' => ['for' => 'myId'],
'Field',
'/label',
'input' => [
'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
'value' => 'My value', 'id' => 'myId',
],
'/div',
];
$this->assertHtml($expected, $result);
$this->View->setRequest($this->View->getRequest()->withParsedBody([]));
$this->Form->create();
$entity->setError('field', 'Badness!');
$this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
$result = $this->Form->control('field');
$expected = [
'div' => ['class' => 'input text error'],
'label' => ['for' => 'field'],
'Field',
'/label',
'input' => [
'type' => 'text',
'name' => 'field',
'id' => 'field',
'class' => 'form-error',
'aria-invalid' => 'true',
'aria-describedby' => 'field-error',
],
['div' => ['class' => 'error-message', 'id' => 'field-error']],
'Badness!',
'/div',
'/div',
];
$this->assertHtml($expected, $result);
$result = $this->Form->control('field', [
'templates' => [
'inputContainerError' => '{{content}}{{error}}',
'error' => '
{{content}}',
],
]);
$expected = [
'label' => ['for' => 'field'],
'Field',
'/label',
'input' => [
'type' => 'text',
'name' => 'field',
'id' => 'field',
'class' => 'form-error',
// No aria-describedby because error template is custom
'aria-invalid' => 'true',
],
['span' => ['class' => 'error-message']],
'Badness!',
'/span',
];
$this->assertHtml($expected, $result);
$entity->setError('field', ['minLength'], true);
$result = $this->Form->control('field', [
'error' => [
'minLength' => 'Le login doit contenir au moins 2 caractères',
'maxLength' => 'login too large',
],
]);
$expected = [
'div' => ['class' => 'input text error'],
'label' => ['for' => 'field'],
'Field',
'/label',
'input' => [
'type' => 'text',
'name' => 'field',
'id' => 'field',
'class' => 'form-error',
'aria-invalid' => 'true',
'aria-describedby' => 'field-error',
],
['div' => ['class' => 'error-message', 'id' => 'field-error']],
'Le login doit contenir au moins 2 caractères',
'/div',
'/div',
];
$this->assertHtml($expected, $result);
$entity->setError('field', ['maxLength'], true);
$result = $this->Form->control('field', [
'error' => [
'minLength' => 'Le login doit contenir au moins 2 caractères',
'maxLength' => 'login too large',
],
]);
$expected = [
'div' => ['class' => 'input text error'],
'label' => ['for' => 'field'],
'Field',
'/label',
'input' => [
'type' => 'text',
'name' => 'field',
'id' => 'field',
'class' => 'form-error',
'aria-invalid' => 'true',
'aria-describedby' => 'field-error',
],
['div' => ['class' => 'error-message', 'id' => 'field-error']],
'login too large',
'/div',
'/div',
];
$this->assertHtml($expected, $result);
}
/**
* testControlWithTemplateFile method
*
* Test that control() accepts a template file.
*/
public function testControlWithTemplateFile(): void
{
$result = $this->Form->control('field', [
'templates' => 'htmlhelper_tags',
]);
$expected = [
'label' => ['for' => 'field'],
'Field',
'/label',
'input' => [
'type' => 'text', 'name' => 'field',
'id' => 'field',
],
];
$this->assertHtml($expected, $result);
}
/**
* testNestedControlsEndWithBrackets method
*
* Test that nested inputs end with brackets.
*/
public function testNestedControlsEndWithBrackets(): void
{
$result = $this->Form->text('nested.text[]');
$expected = [
'input' => [
'type' => 'text', 'name' => 'nested[text][]',
],
];
$this->assertHtml($expected, $result);
$result = $this->Form->file('nested.file[]');
$expected = [
'input' => [
'type' => 'file', 'name' => 'nested[file][]',
],
];
$this->assertHtml($expected, $result);
}
/**
* testCreateIdPrefix method
*
* Test id prefix.
*/
public function testCreateIdPrefix(): void
{
$this->Form->create(null, ['idPrefix' => 'prefix']);
$result = $this->Form->control('field');
$expected = [
'div' => ['class' => 'input text'],
'label' => ['for' => 'prefix-field'],
'Field',
'/label',
'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
'/div',
];
$this->assertHtml($expected, $result);
$result = $this->Form->control('field', ['id' => 'custom-id']);
$expected = [
'div' => ['class' => 'input text'],
'label' => ['for' => 'custom-id'],
'Field',
'/label',
'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
'/div',
];
$this->assertHtml($expected, $result);
$result = $this->Form->radio('Model.field', ['option A']);
$expected = [
'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
'label' => ['for' => 'prefix-model-field-0'],
['input' => [
'type' => 'radio',
'name' => 'Model[field]',
'value' => '0',
'id' => 'prefix-model-field-0',
]],
'option A',
'/label',
];
$this->assertHtml($expected, $result);
$result = $this->Form->radio('Model.field', ['option A', 'option']);
$expected = [
'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
'label' => ['for' => 'prefix-model-field-0'],
['input' => [
'type' => 'radio',
'name' => 'Model[field]',
'value' => '0',
'id' => 'prefix-model-field-0',
]],
'option A',
'/label',
];
$this->assertHtml($expected, $result);
$result = $this->Form->select(
'Model.multi_field',
['first'],
['multiple' => 'checkbox']
);
$expected = [
'input' => [
'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'prefix-model-multi-field',
],
['div' => ['class' => 'checkbox']],
['label' => ['for' => 'prefix-model-multi-field-0']],
['input' => [
'type' => 'checkbox', 'name' => 'Model[multi_field][]',
'value' => '0', 'id' => 'prefix-model-multi-field-0',
]],
'first',
'/label',
'/div',
];
$this->assertHtml($expected, $result);
$this->Form->end();
$result = $this->Form->control('field');
$expected = [
'div' => ['class' => 'input text'],
'label' => ['for' => 'field'],
'Field',
'/label',
'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
'/div',
];
$this->assertHtml($expected, $result);
}
/**
* testControlZero method
*
* Test that inputs with 0 can be created.
*/
public function testControlZero(): void
{
$this->getTableLocator()->get('Contacts', [
'className' => ContactsTable::class,
]);
$this->Form->create([], ['context' => ['table' => 'Contacts']]);
$result = $this->Form->control('0');
$expected = [
'div' => ['class' => 'input text'],
'label' => ['for' => '0'], '/label',
'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
'/div',
];
$this->assertHtml($expected, $result);
}
/**
* testControlCheckbox method
*
* Test control() with checkbox creation.
*/
public function testControlCheckbox(): void
{
$articles = $this->getTableLocator()->get('Articles');
$articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
$article = $articles->newEmptyEntity();
$this->Form->create($article);
$result = $this->Form->control('Articles.active');
$expected = [
'div' => ['class' => 'input checkbox'],
'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
'label' => ['for' => 'articles-active'],
['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
'Active',
'/label',
'/div',
];
$this->assertHtml($expected, $result);
$result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
$expected = [
'div' => ['class' => 'input checkbox'],
'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
'/div',
];
$this->assertHtml($expected, $result);
$result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
$expected = [
'div' => ['class' => 'input checkbox'],
'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
'/div',
];
$this->assertHtml($expected, $result);
$result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
$expected = [
'div' => ['class' => 'input checkbox'],
'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
'/div',
];
$this->assertHtml($expected, $result);
$result = $this->Form->control('Articles.disabled', [
'label' => 'Disabled',
'type' => 'checkbox',
'data-foo' => 'disabled',
]);
$expected = [
'div' => ['class' => 'input checkbox'],
'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
'label' => ['for' => 'articles-disabled'],
['input' => [
'type' => 'checkbox',
'name' => 'Articles[disabled]',
'value' => '1',
'id' => 'articles-disabled',
'data-foo' => 'disabled',
]],
'Disabled',
'/label',
'/div',
];
$this->assertHtml($expected, $result);
$result = $this->Form->control('Articles.confirm', [
'label' => 'Confirm
me!',
'type' => 'checkbox',
'escape' => false,
]);
$expected = [
'div' => ['class' => 'input checkbox'],
'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
'label' => ['for' => 'articles-confirm'],
['input' => [
'type' => 'checkbox',
'name' => 'Articles[confirm]',
'value' => '1',
'id' => 'articles-confirm',
]],
'Confirm
me!',
'/label',
'/div',
];
$this->assertHtml($expected, $result);
}
/**
* testControlHidden method
*
* Test that control() does not create wrapping div and label tag for hidden fields.
*/
public function testControlHidden(): void
{
$this->getTableLocator()->get('ValidateUsers', [
'className' => ValidateUsersTable::class,
]);
$this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
$result = $this->Form->control('ValidateUser.id');
$expected = [
'input' => ['name', 'type' => 'hidden', 'id'],
];
$this->assertHtml($expected, $result);
$result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
$expected = [
'input' => ['name', 'type' => 'hidden', 'id'],
];
$this->assertHtml($expected, $result);
}
/**
* testControlDatetime method
*
* Test form->control() with datetime.
*/
public function testControlDatetime(): void
{
$result = $this->Form->control('prueba', [
'type' => 'datetime',
'value' => new DateTime('2019-09-27 02:52:43'),
]);
$expected = [
'div' => ['class' => 'input datetime'],
'label' => ['for' => 'prueba'],
'Prueba',
'/label',
'input' => [
'name' => 'prueba',
'id' => 'prueba',
'type' => 'datetime-local',
'value' => '2019-09-27T02:52:43',
'step' => '1',
],
'/div',
];
$this->assertHtml($expected, $result);
}
/**
* testControlDatetimeIdPrefix method
*
* Test form->control() with datetime with id prefix.
*/
public function testControlDatetimeIdPrefix(): void
{
$this->Form->create(null, ['idPrefix' => 'prefix']);
$result = $this->Form->control('prueba', [
'type' => 'datetime',
]);
$expected = [
'div' => ['class' => 'input datetime'],
'label' => ['for' => 'prefix-prueba'],
'Prueba',
'/label',
'input' => [
'name' => 'prueba',
'id' => 'prefix-prueba',
'type' => 'datetime-local',
'value' => '',
'step' => '1',
],
'/div',
];
$this->assertHtml($expected, $result);
}
/**
* testControlDatetimeStep method
*
* Test form->control() with datetime with custom step size.
*/
public function testControlDatetimeStep(): void
{
$result = $this->Form->control('prueba', [
'type' => 'datetime',
'value' => new DateTime('2019-09-27 02:52:43'),
'step' => '0.5',
]);
$expected = [
'div' => ['class' => 'input datetime'],
'label' => ['for' => 'prueba'],
'Prueba',
'/label',
'input' => [
'name' => 'prueba',
'id' => 'prueba',
'type' => 'datetime-local',
'value' => '2019-09-27T02:52:43.000',
'step' => '0.5',
],
'/div',
];
$this->assertHtml($expected, $result);
}
/**
* testControlCheckboxWithDisabledElements method
*
* Test generating checkboxes with disabled elements.
*/
public function testControlCheckboxWithDisabledElements(): void
{
$options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
$result = $this->Form->control('Contact.multiple', [
'multiple' => 'checkbox',
'disabled' => 'disabled',
'options' => $options,
]);
$expected = [
['div' => ['class' => 'input select']],
['label' => ['for' => 'contact-multiple']],
'Multiple',
'/label',
['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '', 'id' => 'contact-multiple']],
['div' => ['class' => 'checkbox']],
['label' => ['for' => 'contact-multiple-1']],
['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
'One',
'/label',
'/div',
['div' => ['class' => 'checkbox']],
['label' => ['for' => 'contact-multiple-2']],
['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
'Two',
'/label',
'/div',
['div' => ['class' => 'checkbox']],
['label' => ['for' => 'contact-multiple-3']],
['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
'Three',
'/label',
'/div',
'/div',
];
$this->assertHtml($expected, $result);
// make sure 50 does only disable 50, and not 50f5c0cf
$options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
$disabled = [50];
$expected = [
['div' => ['class' => 'input select']],
['label' => ['for' => 'contact-multiple']],
'Multiple',
'/label',
['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '', 'id' => 'contact-multiple']],
['div' => ['class' => 'checkbox']],
['label' => ['for' => 'contact-multiple-50']],
['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
'Fifty',
'/label',
'/div',
['div' => ['class' => 'checkbox']],
['label' => ['for' => 'contact-multiple-50f5c0cf']],
['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
'Stringy',
'/label',
'/div',
'/div',
];
$result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
$this->assertHtml($expected, $result);
}
/**
* testControlWithLeadingInteger method
*
* Test input name with leading integer, ensure attributes are generated correctly.
*/
public function testControlWithLeadingInteger(): void
{
$result = $this->Form->text('0.Node.title');
$expected = [
'input' => ['name' => '0[Node][title]', 'type' => 'text'],
];
$this->assertHtml($expected, $result);
}
/**
* testControlSelectType method
*
* Test form->control() with select type inputs.
*/
public function testControlSelectType(): void
{
$result = $this->Form->control(
'email',
[
'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
);
$expected = [
'div' => ['class' => 'input select'],
'label' => ['for' => 'email'],
'Email',
'/label',
['select' => ['name' => 'email', 'id' => 'email']],
['option' => ['value' => '']],
'/option',
['option' => ['value' => 'è']],
'Firést',
'/option',
['option' => ['value' => 'é']],
'Secoènd',
'/option',
'/select',
'/div',
];
$this->assertHtml($expected, $result);
$result = $this->Form->control(
'email',
[
'options' => ['First', 'Second'], 'empty' => true]
);
$expected = [
'div' => ['class' => 'input select'],
'label' => ['for' => 'email'],
'Email',
'/label',
['select' => ['name' => 'email', 'id' => 'email']],
['option' => ['value' => '']],
'/option',
['option' => ['value' => '0']],
'First',
'/option',
['option' => ['value' => '1']],
'Second',
'/option',
'/select',
'/div',
];
$this->assertHtml($expected, $result);
$result = $this->Form->control('email', [
'type' => 'select',
'options' => new ArrayObject(['First', 'Second']),
'empty' => true,
]);
$this->assertHtml($expected, $result);
$this->View->set('users', ['value' => 'good', 'other' => 'bad']);
$this->View->setRequest(
$this->View->getRequest()->withData('Model', ['user_id' => 'value'])
);
$this->Form->create();
$result = $this->Form->control('Model.user_id', ['empty' => true]);
$expected = [
'div' => ['class' => 'input select'],
'label' => ['for' => 'model-user-id'],
'User',
'/label',
'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
['option' => ['value' => '']],
'/option',
['option' => ['value' => 'value', 'selected' => 'selected']],
'good',
'/option',
['option' => ['value' => 'other']],
'bad',
'/option',
'/select',
'/div',
];
$this->assertHtml($expected, $result);
$this->View->set('users', ['value' => 'good', 'other' => 'bad']);
$this->View->setRequest(
$this->View->getRequest()->withData('Thing', ['user_id' => null])
);
$result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
$expected = [
'div' => ['class' => 'input select'],
'label' => ['for' => 'thing-user-id'],
'User',
'/label',
'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
['option' => ['value' => '']],
'Some Empty',
'/option',
['option' => ['value' => 'value']],
'good',
'/option',
['option' => ['value' => 'other']],
'bad',
'/option',
'/select',
'/div',
];
$this->assertHtml($expected, $result);
$this->View->set('users', ['value' => 'good', 'other' => 'bad']);
$this->View->setRequest(
$this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
);
$this->Form->create();
$result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
$expected = [
'div' => ['class' => 'input select'],
'label' => ['for' => 'thing-user-id'],
'User',
'/label',
'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
['option' => ['value' => '']],
'Some Empty',
'/option',
['option' => ['value' => 'value', 'selected' => 'selected']],
'good',
'/option',
['option' => ['value' => 'other']],
'bad',
'/option',
'/select',
'/div',
];
$this->assertHtml($expected, $result);
$result = $this->Form->control('Publisher.id', [
'label' => 'Publisher',
'type' => 'select',
'multiple' => 'checkbox',
'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
]);
$expected = [
['div' => ['class' => 'input select']],
['label' => ['for' => 'publisher-id']],
'Publisher',
'/label',
'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => '', 'id' => 'publisher-id'],
['div' => ['class' => 'checkbox']],
['label' => ['for' => 'publisher-id-value-1']],
['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
'Label 1',
'/label',
'/div',
['div' => ['class' => 'checkbox']],
['label' => ['for' => 'publisher-id-value-2']],
['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
'Label 2',
'/label',
'/div',
'/div',
];
$this->assertHtml($expected, $result);
}
/**
* testControlWithNonStandardPrimaryKeyMakesHidden method
*
* Test that control() and a non standard primary key makes a hidden input by default.
*/
public function testControlWithNonStandardPrimaryKeyMakesHidden(): void
{
$this->article['schema']['_constraints']['primary']['columns'] = ['title'];
$this->Form->create($this->article);
$result = $this->Form->control('title');
$expected = [
'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
];
$this->assertHtml($expected, $result);
$this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
$this->Form->create($this->article);
$result = $this->Form->control('title');
$expected = [
'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
];
$this->assertHtml($expected, $result);
$result = $this->Form->control('body');
$expected = [
'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
];
$this->assertHtml($expected, $result);
}
/**
* testControlOverridingMagicSelectType method
*
* Test that overriding the magic select type widget is possible.
*/
public function testControlOverridingMagicSelectType(): void
{
$this->View->set('users', ['value' => 'good', 'other' => 'bad']);
$result = $this->Form->control('Model.user_id', ['type' => 'text']);
$expected = [
'div' => ['class' => 'input text'],
'label' => ['for' => 'model-user-id'], 'User', '/label',
'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
'/div',
];
$this->assertHtml($expected, $result);
//Check that magic types still work for plural/singular vars
$this->View->set('types', ['value' => 'good', 'other' => 'bad']);
$result = $this->Form->control('Model.type');
$expected = [
'div' => ['class' => 'input select'],
'label' => ['for' => 'model-type'], 'Type', '/label',
'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
['option' => ['value' => 'value']], 'good', '/option',
['option' => ['value' => 'other']], 'bad', '/option',
'/select',
'/div',
];
$this->assertHtml($expected, $result);
}
/**
* testControlMagicTypeDoesNotOverride method
*
* Test that inferred types do not override developer input.
*/
public function testControlMagicTypeDoesNotOverride(): void
{
$this->View->set('users', ['value' => 'good', 'other' => 'bad']);
$result = $this->Form->control('Model.user', ['type' => 'checkbox']);
$expected = [
'div' => ['class' => 'input checkbox'],
['input' => [
'type' => 'hidden',
'name' => 'Model[user]',
'value' => 0,
]],
'label' => ['for' => 'model-user'],
['input' => [
'name' => 'Model[user]',
'type' => 'checkbox',
'id' => 'model-user',
'value' => 1,
]],
'User',
'/label',
'/div',
];
$this->assertHtml($expected, $result);
// make sure that for HABTM the multiple option is not being overwritten in case it's truly
$options = [
1 => 'blue',
2 => 'red',
];
$result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
$expected = [
'div' => ['class' => 'input select'],
'label' => ['for' => 'tags-ids'],
'Tags',
'/label',
'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => '', 'id' => 'tags-ids'],
['div' => ['class' => 'checkbox']],
['label' => ['for' => 'tags-ids-1']],
['input' => [
'id' => 'tags-ids-1', 'type' => 'checkbox',
'value' => '1', 'name' => 'tags[_ids][]',
]],
'blue',
'/label',
'/div',
['div' => ['class' => 'checkbox']],
['label' => ['for' => 'tags-ids-2']],
['input' => [
'id' => 'tags-ids-2', 'type' => 'checkbox',
'value' => '2', 'name' => 'tags[_ids][]',
]],
'red',
'/label',
'/div',
'/div',
];
$this->assertHtml($expected, $result);
}
/**
* testControlMagicSelectForTypeNumber method
*
* Test that magic control() selects are created for type=number.
*/
public function testControlMagicSelectForTypeNumber(): void
{
$this->getTableLocator()->get('ValidateUsers', [
'className' => ValidateUsersTable::class,
]);
$entity = new Entity(['balance' => 1]);
$this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
$this->View->set('balances', [0 => 'nothing', 1 => 'some', 100 => 'a lot']);
$result = $this->Form->control('balance');
$expected = [
'div' => ['class' => 'input select'],
'label' => ['for' => 'balance'],
'Balance',
'/label',
'select' => ['name' => 'balance', 'id' => 'balance'],
['option' => ['value' => '0']],
'nothing',
'/option',
['option' => ['value' => '1', 'selected' => 'selected']],
'some',
'/option',
['option' => ['value' => '100']],
'a lot',
'/option',
'/select',
'/div',
];
$this->assertHtml($expected, $result);
}
/**
* testInvalidControlTypeOption method
*
* Test invalid 'input' type option to control() function.
*/
public function testInvalidControlTypeOption(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid type `input` used for field `text`.');
$this->Form->control('text', ['type' => 'input']);
}
/**
* testControlMagicSelectChangeToRadio method
*
* Test that magic control() selects can easily be converted into radio types without error.
*/
public function testControlMagicSelectChangeToRadio(): void
{
$this->View->set('users', ['value' => 'good', 'other' => 'bad']);
$result = $this->Form->control('Model.user_id', ['type' => 'radio']);
$this->assertStringContainsString('input type="radio"', $result);
}
/**
* testFormControlSubmit method
*
* Test correct results for form::control() and type submit.
*/
public function testFormControlSubmit(): void
{
$result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
$expected = [
'div' => ['class' => 'submit'],
'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
'/div',
];
$this->assertHtml($expected, $result);
}
/**
* testFormControls method
*
* Test correct results from Form::controls().
*/
public function testFormControlsLegendFieldset(): void
{
$this->Form->create($this->article);
$result = $this->Form->allControls([], ['legend' => 'The Legend']);
$expected = [
'