From ec9f0fc2bb6f5f1d71ca65121f253587a59c009d Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 9 Feb 2014 22:50:48 -0500 Subject: [PATCH] Move create() tests closer to the other create() tests. --- tests/TestCase/View/Helper/FormHelperTest.php | 12001 ++++++++-------- 1 file changed, 6010 insertions(+), 5991 deletions(-) diff --git a/tests/TestCase/View/Helper/FormHelperTest.php b/tests/TestCase/View/Helper/FormHelperTest.php index abe6fcd895b..7be0d453980 100644 --- a/tests/TestCase/View/Helper/FormHelperTest.php +++ b/tests/TestCase/View/Helper/FormHelperTest.php @@ -598,1376 +598,1151 @@ public function testCreateContextSelectionBuiltIn($data, $class) { } /** - * testFormCreateWithSecurity method - * - * Test form->create() with security key. + * test the create() method * * @return void */ - public function testCreateWithSecurity() { - $this->Form->request->params['_csrfToken'] = 'testKey'; + public function testCreate() { + $this->markTestIncomplete('Need to revisit once models work again.'); + $result = $this->Form->create('Contact'); $encoding = strtolower(Configure::read('App.encoding')); - $article = new Article(); - $result = $this->Form->create($article, [ - 'url' => '/contacts/add', - ]); $expected = array( - 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding), - 'div' => array('style' => 'display:none;'), - array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')), - array('input' => array( - 'type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testKey', 'id' - )), + 'form' => array( + 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add', + 'accept-charset' => $encoding + ), + 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), '/div' ); $this->assertTags($result, $expected); - $result = $this->Form->create($article, ['url' => '/contacts/add', 'id' => 'MyForm']); - $expected['form']['id'] = 'MyForm'; + $result = $this->Form->create('Contact', array('type' => 'GET')); + $expected = array('form' => array( + 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add', + 'accept-charset' => $encoding + )); $this->assertTags($result, $expected); - } - -/** - * testFormCreateGetNoSecurity method - * - * Test form->create() with no security key as its a get form - * - * @return void - */ - public function testCreateEndGetNoSecurity() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->request->params['_csrfToken'] = 'testKey'; - $encoding = strtolower(Configure::read('App.encoding')); - $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add')); - $this->assertNotContains('testKey', $result); - - $result = $this->Form->end('Save'); - $this->assertNotContains('testKey', $result); - } - -/** - * test that create() clears the fields property so it starts fresh - * - * @return void - */ - public function testCreateClearingFields() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->fields = array('model_id'); - $this->Form->create('Contact'); - $this->assertEquals(array(), $this->Form->fields); - } - -/** - * Tests form hash generation with model-less data - * - * @return void - */ - public function testValidateHashNoModel() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->request->params['_csrfToken'] = 'foo'; - $result = $this->Form->secure(array('anything')); - $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result); - } - -/** - * Tests that models with identical field names get resolved properly - * - * @return void - */ - public function testDuplicateFieldNameResolution() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->create('ValidateUser'); - $this->assertEquals(array('ValidateUser'), $this->Form->entity()); - - $result = $this->Form->input('ValidateItem.name'); - $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity()); - - $result = $this->Form->input('ValidateUser.name'); - $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity()); - $this->assertContains('name="ValidateUser[name]"', $result); - $this->assertContains('type="text"', $result); - - $result = $this->Form->input('ValidateItem.name'); - $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity()); - $this->assertContains('name="ValidateItem[name]"', $result); - $this->assertContains('Form->input('name'); - $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity()); - $this->assertContains('name="ValidateUser[name]"', $result); - $this->assertContains('type="text"', $result); - } - -/** - * Tests that hidden fields generated for checkboxes don't get locked - * - * @return void - */ - public function testNoCheckboxLocking() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->request->params['_csrfToken'] = 'foo'; - $this->assertSame(array(), $this->Form->fields); - - $this->Form->checkbox('check', array('value' => '1')); - $this->assertSame($this->Form->fields, array('check')); - } - -/** - * testFormSecurityFields method - * - * Test generation of secure form hash generation. - * - * @return void - */ - public function testFormSecurityFields() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $key = 'testKey'; - $fields = array('Model.password', 'Model.username', 'Model.valid' => '0'); - $this->Form->request->params['_csrfToken'] = $key; - $result = $this->Form->secure($fields); + $result = $this->Form->create('Contact', array('type' => 'get')); + $expected = array('form' => array( + 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add', + 'accept-charset' => $encoding + )); + $this->assertTags($result, $expected); - $hash = Security::hash(serialize($fields) . Configure::read('Security.salt')); - $hash .= ':' . 'Model.valid'; - $hash = urlencode($hash); + $result = $this->Form->create('Contact', array('type' => 'put')); + $expected = array( + 'form' => array( + 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add', + 'accept-charset' => $encoding + ), + 'div' => array('style' => 'display:none;'), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'), + '/div' + ); + $this->assertTags($result, $expected); + $result = $this->Form->create('Contact', array('type' => 'file')); $expected = array( + 'form' => array( + 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add', + 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data' + ), 'div' => array('style' => 'display:none;'), - array('input' => array( - 'type' => 'hidden', 'name' => '_Token[fields]', - 'value' => $hash, 'id' => 'preg:/TokenFields\d+/' - )), - array('input' => array( - 'type' => 'hidden', 'name' => '_Token[unlocked]', - 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/' - )), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), '/div' ); $this->assertTags($result, $expected); - $path = CAKE . 'Test/TestApp/Config/'; - $this->Form->Html->loadConfig('htmlhelper_tags', $path); - $result = $this->Form->secure($fields); + $this->Form->request->data['Contact']['id'] = 1; + $this->Form->request->here = '/contacts/edit/1'; + $this->Form->request['action'] = 'edit'; + $result = $this->Form->create('Contact'); $expected = array( - 'div' => array('class' => 'hidden'), - array('input' => array( - 'type' => 'hidden', 'name' => '_Token[fields]', - 'value' => $hash, 'id' => 'preg:/TokenFields\d+/' - )), - array('input' => array( - 'type' => 'hidden', 'name' => '_Token[unlocked]', - 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/' - )), + 'form' => array( + 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1', + 'accept-charset' => $encoding + ), + 'div' => array('style' => 'display:none;'), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'), '/div' ); $this->assertTags($result, $expected); - } -/** - * Tests correct generation of number fields for double and float fields - * - * @return void - */ - public function testTextFieldGenerationForFloats() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $model->setSchema(array('foo' => array( - 'type' => 'float', - 'null' => false, - 'default' => null, - 'length' => 10 - ))); + $this->Form->request->data['Contact']['id'] = 1; + $this->Form->request->here = '/contacts/edit/1'; + $this->Form->request['action'] = 'edit'; + $result = $this->Form->create('Contact', array('type' => 'file')); + $expected = array( + 'form' => array( + 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1', + 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data' + ), + 'div' => array('style' => 'display:none;'), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'), + '/div' + ); + $this->assertTags($result, $expected); - $this->Form->create('Contact'); - $result = $this->Form->input('foo'); + $this->Form->request->data['ContactNonStandardPk']['pk'] = 1; + $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit'))); $expected = array( - 'div' => array('class' => 'input number'), - 'label' => array('for' => 'ContactFoo'), - 'Foo', - '/label', - array('input' => array( - 'type' => 'number', - 'name' => 'Contact[foo]', - 'id' => 'ContactFoo', - 'step' => 'any' - )), + 'form' => array( + 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post', + 'action' => '/contact_non_standard_pks/edit/1', 'accept-charset' => $encoding + ), + 'div' => array('style' => 'display:none;'), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'), '/div' ); $this->assertTags($result, $expected); - $result = $this->Form->input('foo', array('step' => 0.5)); + $result = $this->Form->create('Contact', array('id' => 'TestId')); $expected = array( - 'div' => array('class' => 'input number'), - 'label' => array('for' => 'ContactFoo'), - 'Foo', - '/label', - array('input' => array( - 'type' => 'number', - 'name' => 'Contact[foo]', - 'id' => 'ContactFoo', - 'step' => '0.5' - )), + 'form' => array( + 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1', + 'accept-charset' => $encoding + ), + 'div' => array('style' => 'display:none;'), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'), '/div' ); $this->assertTags($result, $expected); - } -/** - * Tests correct generation of number fields for integer fields - * - * @return void - */ - public function testTextFieldTypeNumberGenerationForIntegers() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $model->setSchema(array('foo' => array( - 'type' => 'integer', - 'null' => false, - 'default' => null, - 'length' => null - ))); - - $this->Form->create('Contact'); - $result = $this->Form->input('foo'); + $this->Form->request['action'] = 'add'; + $result = $this->Form->create('User', array('url' => array('action' => 'login'))); $expected = array( - 'div' => array('class' => 'input number'), - 'label' => array('for' => 'ContactFoo'), - 'Foo', - '/label', - array('input' => array( - 'type' => 'number', 'name' => 'Contact[foo]', - 'id' => 'ContactFoo' - )), + 'form' => array( + 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login', + 'accept-charset' => $encoding + ), + 'div' => array('style' => 'display:none;'), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), '/div' ); $this->assertTags($result, $expected); - } - -/** - * Tests correct generation of file upload fields for binary fields - * - * @return void - */ - public function testFileUploadFieldTypeGenerationForBinaries() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $model->setSchema(array('foo' => array( - 'type' => 'binary', - 'null' => false, - 'default' => null, - 'length' => 1024 - ))); - $this->Form->create('Contact'); - $result = $this->Form->input('foo'); + $result = $this->Form->create('User', array('action' => 'login')); $expected = array( - 'div' => array('class' => 'input file'), - 'label' => array('for' => 'ContactFoo'), - 'Foo', - '/label', - array('input' => array( - 'type' => 'file', 'name' => 'data[Contact][foo]', - 'id' => 'ContactFoo' - )), + 'form' => array( + 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login', + 'accept-charset' => $encoding + ), + 'div' => array('style' => 'display:none;'), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), '/div' ); $this->assertTags($result, $expected); - } - -/** - * testFormSecurityMultipleFields method - * - * Test secure() with multiple row form. Ensure hash is correct. - * - * @return void - */ - public function testFormSecurityMultipleFields() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $key = 'testKey'; - - $fields = array( - 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value', - 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username', - 'Model.1.hidden' => 'value', 'Model.1.valid' => '0' - ); - $this->Form->request->params['_csrfToken'] = $key; - $result = $this->Form->secure($fields); - - $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid'; - $hash .= '%7CModel.1.hidden%7CModel.1.valid'; + $result = $this->Form->create('User', array('url' => '/users/login')); $expected = array( + 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'), 'div' => array('style' => 'display:none;'), - array('input' => array( - 'type' => 'hidden', 'name' => '_Token[fields]', - 'value' => $hash, 'id' => 'preg:/TokenFields\d+/' - )), - array('input' => array( - 'type' => 'hidden', 'name' => '_Token[unlocked]', - 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/' - )), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), '/div' ); $this->assertTags($result, $expected); - } - -/** - * testFormSecurityMultipleSubmitButtons - * - * test form submit generation and ensure that _Token is only created on end() - * - * @return void - */ - public function testFormSecurityMultipleSubmitButtons() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $key = 'testKey'; - $this->Form->request->params['_csrfToken'] = $key; - - $this->Form->create('Addresses'); - $this->Form->input('Address.title'); - $this->Form->input('Address.first_name'); - $result = $this->Form->submit('Save', array('name' => 'save')); - $expected = array( - 'div' => array('class' => 'submit'), - 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'), - '/div', - ); - $this->assertTags($result, $expected); - $result = $this->Form->submit('Cancel', array('name' => 'cancel')); + $this->Form->request['controller'] = 'pages'; + $result = $this->Form->create('User', array('action' => 'signup')); $expected = array( - 'div' => array('class' => 'submit'), - 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'), - '/div', + 'form' => array( + 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup', + 'accept-charset' => $encoding + ), + 'div' => array('style' => 'display:none;'), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), + '/div' ); $this->assertTags($result, $expected); - $result = $this->Form->end(null); + $this->Form->request->data = array(); + $this->Form->request['controller'] = 'contacts'; + $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact')); + $result = $this->Form->create(array('url' => array('action' => 'index', 'param'))); $expected = array( + 'form' => array( + 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param', + 'accept-charset' => 'utf-8' + ), 'div' => array('style' => 'display:none;'), - array('input' => array( - 'type' => 'hidden', 'name' => '_Token[fields]', - 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/' - )), - array('input' => array( - 'type' => 'hidden', 'name' => '_Token[unlocked]', - 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/' - )), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), '/div' ); $this->assertTags($result, $expected); } /** - * Test that buttons created with foo[bar] name attributes are unlocked correctly. + * Test the onsubmit option for create() * * @return void */ - public function testSecurityButtonNestedNamed() { + public function testCreateOnSubmit() { $this->markTestIncomplete('Need to revisit once models work again.'); - $key = 'testKey'; - $this->Form->request->params['_csrfToken'] = $key; - - $this->Form->create('Addresses'); - $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]')); - $result = $this->Form->unlockField(); - $this->assertEquals(array('Address.button'), $result); - } + $this->Form->request->data = array(); + $this->Form->request['controller'] = 'contacts'; + $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact')); + $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false)); + $expected = array( + 'form' => array( + 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param', + 'accept-charset' => 'utf-8' + ), + 'div' => array('style' => 'display:none;'), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), + '/div' + ); + $this->assertTags($result, $expected); -/** - * Test that submit inputs created with foo[bar] name attributes are unlocked correctly. - * - * @return void - */ - public function testSecuritySubmitNestedNamed() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $key = 'testKey'; - $this->Form->request->params['_csrfToken'] = $key; + $this->Form->request->data = array(); + $this->Form->request['controller'] = 'contacts'; + $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact')); + $result = $this->Form->create(array( + 'url' => array('action' => 'index', 'param'), + 'default' => false, + 'onsubmit' => 'someFunction();' + )); - $this->Form->create('Addresses'); - $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]')); - $result = $this->Form->unlockField(); - $this->assertEquals(array('Address.button'), $result); + $expected = array( + 'form' => array( + 'id' => 'ContactAddForm', 'method' => 'post', + 'onsubmit' => 'someFunction();event.returnValue = false; return false;', + 'action' => '/contacts/index/param', + 'accept-charset' => 'utf-8' + ), + 'div' => array('style' => 'display:none;'), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), + '/div' + ); + $this->assertTags($result, $expected); } /** - * Test that the correct fields are unlocked for image submits with no names. + * test create() with automatic url generation * * @return void */ - public function testSecuritySubmitImageNoName() { + public function testCreateAutoUrl() { $this->markTestIncomplete('Need to revisit once models work again.'); - $key = 'testKey'; - $this->Form->request->params['_csrfToken'] = $key; - - $this->Form->create('User'); - $result = $this->Form->submit('save.png'); + $this->Form->request['action'] = 'delete'; + $this->Form->request->here = '/contacts/delete/10'; + $this->Form->request->base = ''; + $result = $this->Form->create('Contact'); $expected = array( - 'div' => array('class' => 'submit'), - 'input' => array('type' => 'image', 'src' => 'img/save.png'), + 'form' => array( + 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/contacts/delete/10', + 'accept-charset' => 'utf-8' + ), + 'div' => array('style' => 'display:none;'), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), '/div' ); $this->assertTags($result, $expected); - $this->assertEquals(array('x', 'y'), $this->Form->unlockField()); } /** - * Test that the correct fields are unlocked for image submits with names. + * test create() with a custom route * * @return void */ - public function testSecuritySubmitImageName() { + public function testCreateCustomRoute() { $this->markTestIncomplete('Need to revisit once models work again.'); - $key = 'testKey'; - $this->Form->request->params['_csrfToken'] = $key; + Router::connect('/login', array('controller' => 'users', 'action' => 'login')); + $encoding = strtolower(Configure::read('App.encoding')); - $this->Form->create('User'); - $result = $this->Form->submit('save.png', array('name' => 'test')); + $result = $this->Form->create('User', array('action' => 'login')); $expected = array( - 'div' => array('class' => 'submit'), - 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'), + 'form' => array( + 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login', + 'accept-charset' => $encoding + ), + 'div' => array('style' => 'display:none;'), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), '/div' ); $this->assertTags($result, $expected); - $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField()); } /** - * testFormSecurityMultipleInputFields method - * - * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types + * test that inputDefaults are stored and used. * * @return void */ - public function testFormSecurityMultipleInputFields() { + public function testCreateWithInputDefaults() { $this->markTestIncomplete('Need to revisit once models work again.'); - $key = 'testKey'; - $this->Form->request->params['_csrfToken'] = $key; - $this->Form->create('Addresses'); - - $this->Form->hidden('Addresses.0.id', array('value' => '123456')); - $this->Form->input('Addresses.0.title'); - $this->Form->input('Addresses.0.first_name'); - $this->Form->input('Addresses.0.last_name'); - $this->Form->input('Addresses.0.address'); - $this->Form->input('Addresses.0.city'); - $this->Form->input('Addresses.0.phone'); - $this->Form->input('Addresses.0.primary', array('type' => 'checkbox')); - - $this->Form->hidden('Addresses.1.id', array('value' => '654321')); - $this->Form->input('Addresses.1.title'); - $this->Form->input('Addresses.1.first_name'); - $this->Form->input('Addresses.1.last_name'); - $this->Form->input('Addresses.1.address'); - $this->Form->input('Addresses.1.city'); - $this->Form->input('Addresses.1.phone'); - $this->Form->input('Addresses.1.primary', array('type' => 'checkbox')); - - $result = $this->Form->secure($this->Form->fields); - - $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id'; + $this->Form->create('User', array( + 'inputDefaults' => array( + 'div' => false, + 'label' => false, + 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')), + 'format' => array('before', 'label', 'between', 'input', 'after', 'error') + ) + )); + $result = $this->Form->input('username'); + $expected = array( + 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername') + ); + $this->assertTags($result, $expected); + $result = $this->Form->input('username', array('div' => true, 'label' => 'username')); $expected = array( - 'div' => array('style' => 'display:none;'), - array('input' => array( - 'type' => 'hidden', 'name' => '_Token[fields]', - 'value' => $hash, 'id' => 'preg:/TokenFields\d+/' - )), - array('input' => array( - 'type' => 'hidden', 'name' => '_Token[unlocked]', - 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/' - )), + 'div' => array('class' => 'input text'), + 'label' => array('for' => 'UserUsername'), 'username', '/label', + 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'), '/div' ); $this->assertTags($result, $expected); - } - -/** - * Test form security with Model.field.0 style inputs - * - * @return void - */ - public function testFormSecurityArrayFields() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $key = 'testKey'; - $this->Form->request->params['_csrfToken'] = $key; - $this->Form->create('Address'); - $this->Form->input('Address.primary.1'); - $this->assertEquals('Address.primary', $this->Form->fields[0]); + $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label'))); + $expected = array( + 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'), + 'label' => array('for' => 'UserUsername'), 'Username', '/label', + ); + $this->assertTags($result, $expected); - $this->Form->input('Address.secondary.1.0'); - $this->assertEquals('Address.secondary', $this->Form->fields[1]); + $this->Form->create('User', array( + 'inputDefaults' => array( + 'div' => false, + 'label' => array('class' => 'nice', 'for' => 'changed'), + ) + )); + $result = $this->Form->input('username', array('div' => true)); + $expected = array( + 'div' => array('class' => 'input text'), + 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label', + 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'), + '/div' + ); + $this->assertTags($result, $expected); } /** - * testFormSecurityMultipleInputDisabledFields method - * - * test secure form generation with multiple records and disabled fields. + * test automatic accept-charset overriding * * @return void */ - public function testFormSecurityMultipleInputDisabledFields() { + public function testCreateWithAcceptCharset() { $this->markTestIncomplete('Need to revisit once models work again.'); - $key = 'testKey'; - $this->Form->request->params['_csrfToken'] = $key; - $this->Form->request->params['_Token'] = array( - 'unlockedFields' => array('first_name', 'address') + $result = $this->Form->create('UserForm', array( + 'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1' + ) ); - $this->Form->create(); - - $this->Form->hidden('Addresses.0.id', array('value' => '123456')); - $this->Form->input('Addresses.0.title'); - $this->Form->input('Addresses.0.first_name'); - $this->Form->input('Addresses.0.last_name'); - $this->Form->input('Addresses.0.address'); - $this->Form->input('Addresses.0.city'); - $this->Form->input('Addresses.0.phone'); - $this->Form->hidden('Addresses.1.id', array('value' => '654321')); - $this->Form->input('Addresses.1.title'); - $this->Form->input('Addresses.1.first_name'); - $this->Form->input('Addresses.1.last_name'); - $this->Form->input('Addresses.1.address'); - $this->Form->input('Addresses.1.city'); - $this->Form->input('Addresses.1.phone'); - - $result = $this->Form->secure($this->Form->fields); - $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id'; - $expected = array( + 'form' => array( + 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm', + 'accept-charset' => 'iso-8859-1' + ), 'div' => array('style' => 'display:none;'), - array('input' => array( - 'type' => 'hidden', 'name' => '_Token[fields]', - 'value' => $hash, 'id' => 'preg:/TokenFields\d+/' - )), - array('input' => array( - 'type' => 'hidden', 'name' => '_Token[unlocked]', - 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/' - )), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), '/div' ); $this->assertTags($result, $expected); } /** - * testFormSecurityInputDisabledFields method - * - * Test single record form with disabled fields. + * Test base form URL when url param is passed with multiple parameters (&) * - * @return void */ - public function testFormSecurityInputUnlockedFields() { + public function testCreateQuerystringrequest() { $this->markTestIncomplete('Need to revisit once models work again.'); - $key = 'testKey'; - $this->Form->request->params['_csrfToken'] = $key; - $this->Form->request['_Token'] = array( - 'unlockedFields' => array('first_name', 'address') - ); - $this->Form->create(); - $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField()); - - $this->Form->hidden('Addresses.id', array('value' => '123456')); - $this->Form->input('Addresses.title'); - $this->Form->input('Addresses.first_name'); - $this->Form->input('Addresses.last_name'); - $this->Form->input('Addresses.address'); - $this->Form->input('Addresses.city'); - $this->Form->input('Addresses.phone'); - - $result = $this->Form->fields; + $encoding = strtolower(Configure::read('App.encoding')); + $result = $this->Form->create('Contact', array( + 'type' => 'post', + 'escape' => false, + 'url' => array( + 'controller' => 'controller', + 'action' => 'action', + '?' => array('param1' => 'value1', 'param2' => 'value2') + ) + )); $expected = array( - 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name', - 'Addresses.city', 'Addresses.phone' + 'form' => array( + 'id' => 'ContactAddForm', + 'method' => 'post', + 'action' => '/controller/action?param1=value1&param2=value2', + 'accept-charset' => $encoding + ), + 'div' => array('style' => 'display:none;'), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), + '/div' ); - $this->assertEquals($expected, $result); - - $result = $this->Form->secure($expected); + $this->assertTags($result, $expected); - $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id'; + $result = $this->Form->create('Contact', array( + 'type' => 'post', + 'url' => array( + 'controller' => 'controller', + 'action' => 'action', + '?' => array('param1' => 'value1', 'param2' => 'value2') + ) + )); $expected = array( + 'form' => array( + 'id' => 'ContactAddForm', + 'method' => 'post', + 'action' => '/controller/action?param1=value1&param2=value2', + 'accept-charset' => $encoding + ), 'div' => array('style' => 'display:none;'), - array('input' => array( - 'type' => 'hidden', 'name' => '_Token[fields]', - 'value' => $hash, 'id' => 'preg:/TokenFields\d+/' - )), - array('input' => array( - 'type' => 'hidden', 'name' => '_Token[unlocked]', - 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/' - )), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), '/div' ); $this->assertTags($result, $expected); } /** - * test securing inputs with custom name attributes. + * test that create() doesn't cause errors by multiple id's being in the primary key + * as could happen with multiple select or checkboxes. * * @return void */ - public function testFormSecureWithCustomNameAttribute() { + public function testCreateWithMultipleIdInData() { $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->request->params['_csrfToken'] = 'testKey'; - - $this->Form->text('UserForm.published', array('name' => 'User[custom]')); - $this->assertEquals('User.custom', $this->Form->fields[0]); + $encoding = strtolower(Configure::read('App.encoding')); - $this->Form->text('UserForm.published', array('name' => 'User[custom][another][value]')); - $this->assertEquals('User.custom.another.value', $this->Form->fields[1]); + $this->Form->request->data['Contact']['id'] = array(1, 2); + $result = $this->Form->create('Contact'); + $expected = array( + 'form' => array( + 'id' => 'ContactAddForm', + 'method' => 'post', + 'action' => '/contacts/add', + 'accept-charset' => $encoding + ), + 'div' => array('style' => 'display:none;'), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), + '/div' + ); + $this->assertTags($result, $expected); } /** - * testFormSecuredInput method - * - * Test generation of entire secure form, assertions made on input() output. + * test that create() doesn't add in extra passed params. * * @return void */ - public function testFormSecuredInput() { + public function testCreatePassedArgs() { $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->request->params['_csrfToken'] = 'testKey'; - - $result = $this->Form->create('Contact', array('url' => '/contacts/add')); $encoding = strtolower(Configure::read('App.encoding')); + $this->Form->request->data['Contact']['id'] = 1; + $result = $this->Form->create('Contact', array( + 'type' => 'post', + 'escape' => false, + 'url' => array( + 'action' => 'edit', + 'myparam' + ) + )); $expected = array( - 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'), - 'div' => array('style' => 'display:none;'), - array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')), - array('input' => array( - 'type' => 'hidden', 'name' => '_csrfToken', - 'value' => 'testKey', 'id' => 'preg:/Token\d+/' - )), + 'form' => array( + 'id' => 'ContactAddForm', + 'method' => 'post', + 'action' => '/contacts/edit/myparam', + 'accept-charset' => $encoding + ), + 'div' => array('style' => 'display:none;'), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), '/div' ); $this->assertTags($result, $expected); + } - $result = $this->Form->input('UserForm.published', array('type' => 'text')); - $expected = array( - 'div' => array('class' => 'input text'), - 'label' => array('for' => 'UserFormPublished'), - 'Published', - '/label', - array('input' => array( - 'type' => 'text', 'name' => 'UserForm[published]', - 'id' => 'UserFormPublished' - )), - '/div' - ); - $this->assertTags($result, $expected); +/** + * test creating a get form, and get form inputs. + * + * @return void + */ + public function testGetFormCreate() { + $this->markTestIncomplete('Need to revisit once models work again.'); + $encoding = strtolower(Configure::read('App.encoding')); + $result = $this->Form->create('Contact', array('type' => 'get')); + $this->assertTags($result, array('form' => array( + 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add', + 'accept-charset' => $encoding + ))); - $result = $this->Form->input('UserForm.other', array('type' => 'text')); - $expected = array( - 'div' => array('class' => 'input text'), - 'label' => array('for' => 'UserFormOther'), - 'Other', - '/label', - array('input' => array( - 'type' => 'text', 'name' => 'UserForm[other]', - 'id' => 'UserFormOther' - )), - '/div' - ); - $this->assertTags($result, $expected); + $result = $this->Form->text('Contact.name'); + $this->assertTags($result, array('input' => array( + 'name' => 'name', 'type' => 'text', 'id' => 'ContactName', + ))); - $result = $this->Form->hidden('UserForm.stuff'); - $expected = array( - 'input' => array( - 'type' => 'hidden', 'name' => 'UserForm[stuff]', - 'id' => 'UserFormStuff' - )); - $this->assertTags($result, $expected); + $result = $this->Form->password('password'); + $this->assertTags($result, array('input' => array( + 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword' + ))); + $this->assertNotRegExp('/]+[^id|name|type|value]=[^<>]*>$/', $result); - $result = $this->Form->hidden('UserForm.hidden', array('value' => '0')); - $expected = array('input' => array( - 'type' => 'hidden', 'name' => 'UserForm[hidden]', - 'value' => '0', 'id' => 'UserFormHidden' + $result = $this->Form->text('user_form'); + $this->assertTags($result, array('input' => array( + 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm' + ))); + } + +/** + * test get form, and inputs when the model param is false + * + * @return void + */ + public function testGetFormWithFalseModel() { + $this->markTestIncomplete('Need to revisit once models work again.'); + $encoding = strtolower(Configure::read('App.encoding')); + $this->Form->request['controller'] = 'contact_test'; + $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test'))); + + $expected = array('form' => array( + 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add', + 'accept-charset' => $encoding )); $this->assertTags($result, $expected); - $result = $this->Form->input('UserForm.something', array('type' => 'checkbox')); + $result = $this->Form->text('reason'); $expected = array( - 'div' => array('class' => 'input checkbox'), - array('input' => array( - 'type' => 'hidden', 'name' => 'UserForm[something]', - 'value' => '0', 'id' => 'UserFormSomething_' - )), - array('input' => array( - 'type' => 'checkbox', 'name' => 'UserForm[something]', - 'value' => '1', 'id' => 'UserFormSomething' - )), - 'label' => array('for' => 'UserFormSomething'), - 'Something', - '/label', - '/div' + 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason') ); $this->assertTags($result, $expected); + } - $result = $this->Form->fields; - $expected = array( - 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '', - 'UserForm.hidden' => '0', 'UserForm.something' - ); - $this->assertEquals($expected, $result); - - $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff'; - $result = $this->Form->secure($this->Form->fields); +/** + * testFormCreateWithSecurity method + * + * Test form->create() with security key. + * + * @return void + */ + public function testCreateWithSecurity() { + $this->Form->request->params['_csrfToken'] = 'testKey'; + $encoding = strtolower(Configure::read('App.encoding')); + $article = new Article(); + $result = $this->Form->create($article, [ + 'url' => '/contacts/add', + ]); $expected = array( + 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding), 'div' => array('style' => 'display:none;'), + array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')), array('input' => array( - 'type' => 'hidden', 'name' => '_Token[fields]', - 'value' => $hash, 'id' => 'preg:/TokenFields\d+/' - )), - array('input' => array( - 'type' => 'hidden', 'name' => '_Token[unlocked]', - 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/' + 'type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testKey', 'id' )), '/div' ); $this->assertTags($result, $expected); + + $result = $this->Form->create($article, ['url' => '/contacts/add', 'id' => 'MyForm']); + $expected['form']['id'] = 'MyForm'; + $this->assertTags($result, $expected); } /** - * Test secured inputs with custom names. + * testFormCreateGetNoSecurity method + * + * Test form->create() with no security key as its a get form * * @return void */ - public function testSecuredInputCustomName() { - $this->markTestIncomplete('Need to revisit once models work again.'); + public function testCreateEndGetNoSecurity() { $this->Form->request->params['_csrfToken'] = 'testKey'; - $this->assertEquals(array(), $this->Form->fields); - - $this->Form->input('text_input', array( - 'name' => 'data[Option][General.default_role]', - )); - $expected = array('Option.General.default_role'); - $this->assertEquals($expected, $this->Form->fields); + $encoding = strtolower(Configure::read('App.encoding')); + $article = new Article(); + $result = $this->Form->create($article, [ + 'type' => 'get', + 'url' => '/contacts/add' + ]); + $this->assertNotContains('testKey', $result); - $this->Form->input('select_box', array( - 'name' => 'data[Option][General.select_role]', - 'type' => 'select', - 'options' => array(1, 2), - )); - $expected = array('Option.General.default_role', 'Option.General.select_role'); - $this->assertEquals($expected, $this->Form->fields); + $result = $this->Form->end('Save'); + $this->assertNotContains('testKey', $result); } /** - * Tests that the correct keys are added to the field hash index + * test that create() clears the fields property so it starts fresh * * @return void */ - public function testFormSecuredFileInput() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->request->params['_csrfToken'] = 'testKey'; + public function testCreateClearingFields() { + $this->Form->fields = array('model_id'); + $this->Form->create(new Article()); $this->assertEquals(array(), $this->Form->fields); - - $this->Form->file('Attachment.file'); - $expected = array( - 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name', - 'Attachment.file.error', 'Attachment.file.size' - ); - $this->assertEquals($expected, $this->Form->fields); } /** - * test that multiple selects keys are added to field hash + * Tests form hash generation with model-less data * * @return void */ - public function testFormSecuredMultipleSelect() { + public function testValidateHashNoModel() { $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->request->params['_csrfToken'] = 'testKey'; - $this->assertEquals(array(), $this->Form->fields); - $options = array('1' => 'one', '2' => 'two'); - - $this->Form->select('Model.select', $options); - $expected = array('Model.select'); - $this->assertEquals($expected, $this->Form->fields); - - $this->Form->fields = array(); - $this->Form->select('Model.select', $options, array('multiple' => true)); - $this->assertEquals($expected, $this->Form->fields); + $this->Form->request->params['_csrfToken'] = 'foo'; + $result = $this->Form->secure(array('anything')); + $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result); } /** - * testFormSecuredRadio method + * Tests that models with identical field names get resolved properly * * @return void */ - public function testFormSecuredRadio() { + public function testDuplicateFieldNameResolution() { $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->request->params['_csrfToken'] = 'testKey'; - $this->assertEquals(array(), $this->Form->fields); - $options = array('1' => 'option1', '2' => 'option2'); + $result = $this->Form->create('ValidateUser'); + $this->assertEquals(array('ValidateUser'), $this->Form->entity()); - $this->Form->radio('Test.test', $options); - $expected = array('Test.test'); - $this->assertEquals($expected, $this->Form->fields); + $result = $this->Form->input('ValidateItem.name'); + $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity()); + + $result = $this->Form->input('ValidateUser.name'); + $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity()); + $this->assertContains('name="ValidateUser[name]"', $result); + $this->assertContains('type="text"', $result); + + $result = $this->Form->input('ValidateItem.name'); + $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity()); + $this->assertContains('name="ValidateItem[name]"', $result); + $this->assertContains('Form->input('name'); + $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity()); + $this->assertContains('name="ValidateUser[name]"', $result); + $this->assertContains('type="text"', $result); } /** - * Test that when disabled is in a list based attribute array it works. + * Tests that hidden fields generated for checkboxes don't get locked * * @return void */ - public function testFormSecuredAndDisabledNotAssoc() { + public function testNoCheckboxLocking() { $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->request->params['_csrfToken'] = 'testKey'; - - $this->Form->select('Model.select', array(1, 2), array('disabled')); - $this->Form->checkbox('Model.checkbox', array('disabled')); - $this->Form->text('Model.text', array('disabled')); - $this->Form->textarea('Model.textarea', array('disabled')); - $this->Form->password('Model.password', array('disabled')); - $this->Form->radio('Model.radio', array(1, 2), array('disabled')); + $this->Form->request->params['_csrfToken'] = 'foo'; + $this->assertSame(array(), $this->Form->fields); - $expected = array( - 'Model.radio' => '' - ); - $this->assertEquals($expected, $this->Form->fields); + $this->Form->checkbox('check', array('value' => '1')); + $this->assertSame($this->Form->fields, array('check')); } /** - * test that forms with disabled inputs + secured forms leave off the inputs from the form - * hashing. + * testFormSecurityFields method + * + * Test generation of secure form hash generation. * * @return void */ - public function testFormSecuredAndDisabled() { + public function testFormSecurityFields() { $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->request->params['_csrfToken'] = 'testKey'; + $key = 'testKey'; + $fields = array('Model.password', 'Model.username', 'Model.valid' => '0'); - $this->Form->checkbox('Model.checkbox', array('disabled' => true)); - $this->Form->text('Model.text', array('disabled' => true)); - $this->Form->password('Model.text', array('disabled' => true)); - $this->Form->textarea('Model.textarea', array('disabled' => true)); - $this->Form->select('Model.select', array(1, 2), array('disabled' => true)); - $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2))); - $this->Form->year('Model.year', null, null, array('disabled' => true)); - $this->Form->month('Model.month', array('disabled' => true)); - $this->Form->day('Model.day', array('disabled' => true)); - $this->Form->hour('Model.hour', false, array('disabled' => true)); - $this->Form->minute('Model.minute', array('disabled' => true)); - $this->Form->meridian('Model.meridian', array('disabled' => true)); + $this->Form->request->params['_csrfToken'] = $key; + $result = $this->Form->secure($fields); - $expected = array( - 'Model.radio' => '' - ); - $this->assertEquals($expected, $this->Form->fields); - } + $hash = Security::hash(serialize($fields) . Configure::read('Security.salt')); + $hash .= ':' . 'Model.valid'; + $hash = urlencode($hash); -/** - * testDisableSecurityUsingForm method - * - * @return void - */ - public function testDisableSecurityUsingForm() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->request->params['_csrfToken'] = 'testKey'; - $this->Form->request['_Token'] = array( - 'disabledFields' => array() + $expected = array( + 'div' => array('style' => 'display:none;'), + array('input' => array( + 'type' => 'hidden', 'name' => '_Token[fields]', + 'value' => $hash, 'id' => 'preg:/TokenFields\d+/' + )), + array('input' => array( + 'type' => 'hidden', 'name' => '_Token[unlocked]', + 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/' + )), + '/div' ); - $this->Form->create(); - - $this->Form->hidden('Addresses.id', array('value' => '123456')); - $this->Form->input('Addresses.title'); - $this->Form->input('Addresses.first_name', array('secure' => false)); - $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false)); - $this->Form->input('Addresses.zip', array( - 'type' => 'select', 'options' => array(1, 2), 'secure' => false - )); + $this->assertTags($result, $expected); - $result = $this->Form->fields; + $path = CAKE . 'Test/TestApp/Config/'; + $this->Form->Html->loadConfig('htmlhelper_tags', $path); + $result = $this->Form->secure($fields); $expected = array( - 'Addresses.id' => '123456', 'Addresses.title', + 'div' => array('class' => 'hidden'), + array('input' => array( + 'type' => 'hidden', 'name' => '_Token[fields]', + 'value' => $hash, 'id' => 'preg:/TokenFields\d+/' + )), + array('input' => array( + 'type' => 'hidden', 'name' => '_Token[unlocked]', + 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/' + )), + '/div' ); - $this->assertEquals($expected, $result); + $this->assertTags($result, $expected); } /** - * test disableField + * Tests correct generation of number fields for double and float fields * * @return void */ - public function testUnlockFieldAddsToList() { + public function testTextFieldGenerationForFloats() { $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->request->params['_csrfToken'] = 'testKey'; - $this->Form->request['_Token'] = array( - 'unlockedFields' => array() - ); + $model->setSchema(array('foo' => array( + 'type' => 'float', + 'null' => false, + 'default' => null, + 'length' => 10 + ))); + $this->Form->create('Contact'); - $this->Form->unlockField('Contact.name'); - $this->Form->text('Contact.name'); + $result = $this->Form->input('foo'); + $expected = array( + 'div' => array('class' => 'input number'), + 'label' => array('for' => 'ContactFoo'), + 'Foo', + '/label', + array('input' => array( + 'type' => 'number', + 'name' => 'Contact[foo]', + 'id' => 'ContactFoo', + 'step' => 'any' + )), + '/div' + ); + $this->assertTags($result, $expected); - $this->assertEquals(array('Contact.name'), $this->Form->unlockField()); - $this->assertEquals(array(), $this->Form->fields); + $result = $this->Form->input('foo', array('step' => 0.5)); + $expected = array( + 'div' => array('class' => 'input number'), + 'label' => array('for' => 'ContactFoo'), + 'Foo', + '/label', + array('input' => array( + 'type' => 'number', + 'name' => 'Contact[foo]', + 'id' => 'ContactFoo', + 'step' => '0.5' + )), + '/div' + ); + $this->assertTags($result, $expected); } /** - * test unlockField removing from fields array. + * Tests correct generation of number fields for integer fields * * @return void */ - public function testUnlockFieldRemovingFromFields() { + public function testTextFieldTypeNumberGenerationForIntegers() { $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->request->params['_csrfToken'] = 'testKey'; - $this->Form->request['_Token'] = array( - 'unlockedFields' => array() - ); - $this->Form->create('Contact'); - $this->Form->hidden('Contact.id', array('value' => 1)); - $this->Form->text('Contact.name'); - - $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.'); - $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.'); + $model->setSchema(array('foo' => array( + 'type' => 'integer', + 'null' => false, + 'default' => null, + 'length' => null + ))); - $this->Form->unlockField('Contact.name'); - $this->Form->unlockField('Contact.id'); - $this->assertEquals(array(), $this->Form->fields); + $this->Form->create('Contact'); + $result = $this->Form->input('foo'); + $expected = array( + 'div' => array('class' => 'input number'), + 'label' => array('for' => 'ContactFoo'), + 'Foo', + '/label', + array('input' => array( + 'type' => 'number', 'name' => 'Contact[foo]', + 'id' => 'ContactFoo' + )), + '/div' + ); + $this->assertTags($result, $expected); } /** - * testTagIsInvalid method + * Tests correct generation of file upload fields for binary fields * * @return void */ - public function testTagIsInvalid() { + public function testFileUploadFieldTypeGenerationForBinaries() { $this->markTestIncomplete('Need to revisit once models work again.'); - $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email'); - - $this->Form->setEntity('Contact.0.email'); - $result = $this->Form->tagIsInvalid(); - $this->assertEquals($expected, $result); - - $this->Form->setEntity('Contact.1.email'); - $result = $this->Form->tagIsInvalid(); - $this->assertFalse($result); + $model->setSchema(array('foo' => array( + 'type' => 'binary', + 'null' => false, + 'default' => null, + 'length' => 1024 + ))); - $this->Form->setEntity('Contact.0.name'); - $result = $this->Form->tagIsInvalid(); - $this->assertFalse($result); + $this->Form->create('Contact'); + $result = $this->Form->input('foo'); + $expected = array( + 'div' => array('class' => 'input file'), + 'label' => array('for' => 'ContactFoo'), + 'Foo', + '/label', + array('input' => array( + 'type' => 'file', 'name' => 'data[Contact][foo]', + 'id' => 'ContactFoo' + )), + '/div' + ); + $this->assertTags($result, $expected); } /** - * Test tagIsInvalid with validation errors from a saveMany + * testFormSecurityMultipleFields method + * + * Test secure() with multiple row form. Ensure hash is correct. * * @return void */ - public function testTagIsInvalidSaveMany() { + public function testFormSecurityMultipleFields() { $this->markTestIncomplete('Need to revisit once models work again.'); - $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email'); + $key = 'testKey'; - $this->Form->create('Contact'); + $fields = array( + 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value', + 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username', + 'Model.1.hidden' => 'value', 'Model.1.valid' => '0' + ); + $this->Form->request->params['_csrfToken'] = $key; + $result = $this->Form->secure($fields); - $this->Form->setEntity('0.email'); - $result = $this->Form->tagIsInvalid(); - $this->assertEquals($expected, $result); + $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid'; + $hash .= '%7CModel.1.hidden%7CModel.1.valid'; - $this->Form->setEntity('0.Contact.email'); - $result = $this->Form->tagIsInvalid(); - $this->assertEquals($expected, $result); + $expected = array( + 'div' => array('style' => 'display:none;'), + array('input' => array( + 'type' => 'hidden', 'name' => '_Token[fields]', + 'value' => $hash, 'id' => 'preg:/TokenFields\d+/' + )), + array('input' => array( + 'type' => 'hidden', 'name' => '_Token[unlocked]', + 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/' + )), + '/div' + ); + $this->assertTags($result, $expected); } /** - * Test validation errors. + * testFormSecurityMultipleSubmitButtons + * + * test form submit generation and ensure that _Token is only created on end() * * @return void */ - public function testPasswordValidation() { + public function testFormSecurityMultipleSubmitButtons() { $this->markTestIncomplete('Need to revisit once models work again.'); - $Contact->validationErrors['password'] = array('Please provide a password'); + $key = 'testKey'; + $this->Form->request->params['_csrfToken'] = $key; - $result = $this->Form->input('Contact.password'); + $this->Form->create('Addresses'); + $this->Form->input('Address.title'); + $this->Form->input('Address.first_name'); + + $result = $this->Form->submit('Save', array('name' => 'save')); $expected = array( - 'div' => array('class' => 'input password error'), - 'label' => array('for' => 'ContactPassword'), - 'Password', - '/label', - 'input' => array( - 'type' => 'password', 'name' => 'Contact[password]', - 'id' => 'ContactPassword', 'class' => 'form-error' - ), - array('div' => array('class' => 'error-message')), - 'Please provide a password', + 'div' => array('class' => 'submit'), + 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'), '/div', - '/div' ); $this->assertTags($result, $expected); + $result = $this->Form->submit('Cancel', array('name' => 'cancel')); + $expected = array( + 'div' => array('class' => 'submit'), + 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'), + '/div', + ); + $this->assertTags($result, $expected); + $result = $this->Form->end(null); - $result = $this->Form->input('Contact.password', array('errorMessage' => false)); $expected = array( - 'div' => array('class' => 'input password error'), - 'label' => array('for' => 'ContactPassword'), - 'Password', - '/label', - 'input' => array( - 'type' => 'password', 'name' => 'Contact[password]', - 'id' => 'ContactPassword', 'class' => 'form-error' - ), + 'div' => array('style' => 'display:none;'), + array('input' => array( + 'type' => 'hidden', 'name' => '_Token[fields]', + 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/' + )), + array('input' => array( + 'type' => 'hidden', 'name' => '_Token[unlocked]', + 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/' + )), '/div' ); $this->assertTags($result, $expected); } /** - * Test validation errors, when validation message is an empty string. + * Test that buttons created with foo[bar] name attributes are unlocked correctly. * * @return void */ - public function testEmptyErrorValidation() { + public function testSecurityButtonNestedNamed() { $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->validationErrors['Contact']['password'] = ''; - - $result = $this->Form->input('Contact.password'); - $expected = array( - 'div' => array('class' => 'input password error'), - 'label' => array('for' => 'ContactPassword'), - 'Password', - '/label', - 'input' => array( - 'type' => 'password', 'name' => 'Contact[password]', - 'id' => 'ContactPassword', 'class' => 'form-error' - ), - array('div' => array('class' => 'error-message')), - array(), - '/div', - '/div' - ); - $this->assertTags($result, $expected); + $key = 'testKey'; + $this->Form->request->params['_csrfToken'] = $key; - $result = $this->Form->input('Contact.password', array('errorMessage' => false)); - $expected = array( - 'div' => array('class' => 'input password error'), - 'label' => array('for' => 'ContactPassword'), - 'Password', - '/label', - 'input' => array( - 'type' => 'password', 'name' => 'Contact[password]', - 'id' => 'ContactPassword', 'class' => 'form-error' - ), - '/div' - ); - $this->assertTags($result, $expected); + $this->Form->create('Addresses'); + $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]')); + $result = $this->Form->unlockField(); + $this->assertEquals(array('Address.button'), $result); } /** - * Test validation errors, when calling input() overriding validation message by an empty string. + * Test that submit inputs created with foo[bar] name attributes are unlocked correctly. * * @return void */ - public function testEmptyInputErrorValidation() { + public function testSecuritySubmitNestedNamed() { $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->validationErrors['Contact']['password'] = 'Please provide a password'; + $key = 'testKey'; + $this->Form->request->params['_csrfToken'] = $key; - $result = $this->Form->input('Contact.password', array('error' => '')); - $expected = array( - 'div' => array('class' => 'input password error'), - 'label' => array('for' => 'ContactPassword'), - 'Password', - '/label', - 'input' => array( - 'type' => 'password', 'name' => 'Contact[password]', - 'id' => 'ContactPassword', 'class' => 'form-error' - ), - array('div' => array('class' => 'error-message')), - array(), - '/div', - '/div' - ); - $this->assertTags($result, $expected); + $this->Form->create('Addresses'); + $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]')); + $result = $this->Form->unlockField(); + $this->assertEquals(array('Address.button'), $result); + } - $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false)); +/** + * Test that the correct fields are unlocked for image submits with no names. + * + * @return void + */ + public function testSecuritySubmitImageNoName() { + $this->markTestIncomplete('Need to revisit once models work again.'); + $key = 'testKey'; + $this->Form->request->params['_csrfToken'] = $key; + + $this->Form->create('User'); + $result = $this->Form->submit('save.png'); $expected = array( - 'div' => array('class' => 'input password error'), - 'label' => array('for' => 'ContactPassword'), - 'Password', - '/label', - 'input' => array( - 'type' => 'password', 'name' => 'Contact[password]', - 'id' => 'ContactPassword', 'class' => 'form-error' - ), + 'div' => array('class' => 'submit'), + 'input' => array('type' => 'image', 'src' => 'img/save.png'), '/div' ); $this->assertTags($result, $expected); + $this->assertEquals(array('x', 'y'), $this->Form->unlockField()); } /** - * testFormValidationAssociated method - * - * test display of form errors in conjunction with model::validates. + * Test that the correct fields are unlocked for image submits with names. * * @return void */ - public function testFormValidationAssociated() { + public function testSecuritySubmitImageName() { $this->markTestIncomplete('Need to revisit once models work again.'); + $key = 'testKey'; + $this->Form->request->params['_csrfToken'] = $key; - $data = array( - 'UserForm' => array('name' => 'user'), - 'OpenidUrl' => array('url' => 'http://www.cakephp.org') - ); - - $result = $this->UserForm->OpenidUrl->create($data); - $this->assertFalse(empty($result)); - $this->assertFalse($this->UserForm->OpenidUrl->validates()); - - $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login')); - $encoding = strtolower(Configure::read('App.encoding')); + $this->Form->create('User'); + $result = $this->Form->submit('save.png', array('name' => 'test')); $expected = array( - 'form' => array( - 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm', - 'accept-charset' => $encoding - ), - 'div' => array('style' => 'display:none;'), - 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), + 'div' => array('class' => 'submit'), + 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'), '/div' ); $this->assertTags($result, $expected); - - $result = $this->Form->error( - 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false) - ); - $this->assertEquals('Error, not registered', $result); - - unset($this->UserForm->OpenidUrl, $this->UserForm); + $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField()); } /** - * testFormValidationAssociatedFirstLevel method + * testFormSecurityMultipleInputFields method * - * test form error display with associated model. + * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types * * @return void */ - public function testFormValidationAssociatedFirstLevel() { + public function testFormSecurityMultipleInputFields() { $this->markTestIncomplete('Need to revisit once models work again.'); + $key = 'testKey'; + $this->Form->request->params['_csrfToken'] = $key; + $this->Form->create('Addresses'); - $data = array( - 'ValidateUser' => array('name' => 'mariano'), - 'ValidateProfile' => array('full_name' => 'Mariano Iglesias') - ); + $this->Form->hidden('Addresses.0.id', array('value' => '123456')); + $this->Form->input('Addresses.0.title'); + $this->Form->input('Addresses.0.first_name'); + $this->Form->input('Addresses.0.last_name'); + $this->Form->input('Addresses.0.address'); + $this->Form->input('Addresses.0.city'); + $this->Form->input('Addresses.0.phone'); + $this->Form->input('Addresses.0.primary', array('type' => 'checkbox')); - $result = $this->ValidateUser->create($data); - $this->assertFalse(empty($result)); - $this->assertFalse($this->ValidateUser->validates()); - $this->assertFalse($this->ValidateUser->ValidateProfile->validates()); + $this->Form->hidden('Addresses.1.id', array('value' => '654321')); + $this->Form->input('Addresses.1.title'); + $this->Form->input('Addresses.1.first_name'); + $this->Form->input('Addresses.1.last_name'); + $this->Form->input('Addresses.1.address'); + $this->Form->input('Addresses.1.city'); + $this->Form->input('Addresses.1.phone'); + $this->Form->input('Addresses.1.primary', array('type' => 'checkbox')); + + $result = $this->Form->secure($this->Form->fields); + + $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id'; - $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add')); - $encoding = strtolower(Configure::read('App.encoding')); $expected = array( - 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding), 'div' => array('style' => 'display:none;'), - 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), + array('input' => array( + 'type' => 'hidden', 'name' => '_Token[fields]', + 'value' => $hash, 'id' => 'preg:/TokenFields\d+/' + )), + array('input' => array( + 'type' => 'hidden', 'name' => '_Token[unlocked]', + 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/' + )), '/div' ); $this->assertTags($result, $expected); + } - $result = $this->Form->error( - 'ValidateUser.email', 'Invalid email', array('wrap' => false) - ); - $this->assertEquals('Invalid email', $result); - - $result = $this->Form->error( - 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false) - ); - $this->assertEquals('Invalid name', $result); +/** + * Test form security with Model.field.0 style inputs + * + * @return void + */ + public function testFormSecurityArrayFields() { + $this->markTestIncomplete('Need to revisit once models work again.'); + $key = 'testKey'; + $this->Form->request->params['_csrfToken'] = $key; - $result = $this->Form->error( - 'ValidateProfile.city', 'Invalid city', array('wrap' => false) - ); - $this->assertEquals('Invalid city', $result); + $this->Form->create('Address'); + $this->Form->input('Address.primary.1'); + $this->assertEquals('Address.primary', $this->Form->fields[0]); - unset($this->ValidateUser->ValidateProfile); - unset($this->ValidateUser); + $this->Form->input('Address.secondary.1.0'); + $this->assertEquals('Address.secondary', $this->Form->fields[1]); } /** - * testFormValidationAssociatedSecondLevel method + * testFormSecurityMultipleInputDisabledFields method * - * test form error display with associated model. + * test secure form generation with multiple records and disabled fields. * * @return void */ - public function testFormValidationAssociatedSecondLevel() { + public function testFormSecurityMultipleInputDisabledFields() { $this->markTestIncomplete('Need to revisit once models work again.'); + $key = 'testKey'; + $this->Form->request->params['_csrfToken'] = $key; + $this->Form->request->params['_Token'] = array( + 'unlockedFields' => array('first_name', 'address') + ); + $this->Form->create(); - $data = array( - 'ValidateUser' => array('name' => 'mariano'), - 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'), - 'ValidateItem' => array('name' => 'Item') - ); + $this->Form->hidden('Addresses.0.id', array('value' => '123456')); + $this->Form->input('Addresses.0.title'); + $this->Form->input('Addresses.0.first_name'); + $this->Form->input('Addresses.0.last_name'); + $this->Form->input('Addresses.0.address'); + $this->Form->input('Addresses.0.city'); + $this->Form->input('Addresses.0.phone'); + $this->Form->hidden('Addresses.1.id', array('value' => '654321')); + $this->Form->input('Addresses.1.title'); + $this->Form->input('Addresses.1.first_name'); + $this->Form->input('Addresses.1.last_name'); + $this->Form->input('Addresses.1.address'); + $this->Form->input('Addresses.1.city'); + $this->Form->input('Addresses.1.phone'); - $result = $this->ValidateUser->create($data); - $this->assertFalse(empty($result)); - $this->assertFalse($this->ValidateUser->validates()); - $this->assertFalse($this->ValidateUser->ValidateProfile->validates()); - $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates()); + $result = $this->Form->secure($this->Form->fields); + $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id'; - $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add')); - $encoding = strtolower(Configure::read('App.encoding')); $expected = array( - 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding), 'div' => array('style' => 'display:none;'), - 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), + array('input' => array( + 'type' => 'hidden', 'name' => '_Token[fields]', + 'value' => $hash, 'id' => 'preg:/TokenFields\d+/' + )), + array('input' => array( + 'type' => 'hidden', 'name' => '_Token[unlocked]', + 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/' + )), '/div' ); $this->assertTags($result, $expected); - - $result = $this->Form->error( - 'ValidateUser.email', 'Invalid email', array('wrap' => false) - ); - $this->assertEquals('Invalid email', $result); - - $result = $this->Form->error( - 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false) - ); - $this->assertEquals('Invalid name', $result); - - $result = $this->Form->error( - 'ValidateProfile.city', 'Invalid city', array('wrap' => false) - ); - - $result = $this->Form->error( - 'ValidateItem.description', 'Invalid description', array('wrap' => false) - ); - $this->assertEquals('Invalid description', $result); - - unset($this->ValidateUser->ValidateProfile->ValidateItem); - unset($this->ValidateUser->ValidateProfile); - unset($this->ValidateUser); } /** - * testFormValidationMultiRecord method + * testFormSecurityInputDisabledFields method * - * test form error display with multiple records. + * Test single record form with disabled fields. * * @return void */ - public function testFormValidationMultiRecord() { + public function testFormSecurityInputUnlockedFields() { $this->markTestIncomplete('Need to revisit once models work again.'); - $Contact->validationErrors[2] = array( - 'name' => array('The provided value is invalid') + $key = 'testKey'; + $this->Form->request->params['_csrfToken'] = $key; + $this->Form->request['_Token'] = array( + 'unlockedFields' => array('first_name', 'address') ); - $result = $this->Form->input('Contact.2.name'); + $this->Form->create(); + $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField()); + + $this->Form->hidden('Addresses.id', array('value' => '123456')); + $this->Form->input('Addresses.title'); + $this->Form->input('Addresses.first_name'); + $this->Form->input('Addresses.last_name'); + $this->Form->input('Addresses.address'); + $this->Form->input('Addresses.city'); + $this->Form->input('Addresses.phone'); + + $result = $this->Form->fields; $expected = array( - 'div' => array('class' => 'input text error'), - 'label' => array('for' => 'Contact2Name'), - 'Name', - '/label', - 'input' => array( - 'type' => 'text', 'name' => 'Contact[2][name]', 'id' => 'Contact2Name', - 'class' => 'form-error', 'maxlength' => 255 - ), - array('div' => array('class' => 'error-message')), - 'The provided value is invalid', - '/div', + 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name', + 'Addresses.city', 'Addresses.phone' + ); + $this->assertEquals($expected, $result); + + $result = $this->Form->secure($expected); + + $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id'; + $expected = array( + 'div' => array('style' => 'display:none;'), + array('input' => array( + 'type' => 'hidden', 'name' => '_Token[fields]', + 'value' => $hash, 'id' => 'preg:/TokenFields\d+/' + )), + array('input' => array( + 'type' => 'hidden', 'name' => '_Token[unlocked]', + 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/' + )), '/div' ); $this->assertTags($result, $expected); } /** - * testMultipleInputValidation method - * - * test multiple record form validation error display. + * test securing inputs with custom name attributes. * * @return void */ - public function testMultipleInputValidation() { + public function testFormSecureWithCustomNameAttribute() { $this->markTestIncomplete('Need to revisit once models work again.'); - $Address->validationErrors[0] = array( - 'title' => array('This field cannot be empty'), - 'first_name' => array('This field cannot be empty') - ); - $Address->validationErrors[1] = array( - 'last_name' => array('You must have a last name') - ); - $this->Form->create(); - - $result = $this->Form->input('Address.0.title'); - $expected = array( - 'div' => array('class'), - 'label' => array('for'), - 'preg:/[^<]+/', - '/label', - 'input' => array( - 'type' => 'text', 'name', 'id', 'class' => 'form-error' - ), - array('div' => array('class' => 'error-message')), - 'This field cannot be empty', - '/div', - '/div' - ); - $this->assertTags($result, $expected); - - $result = $this->Form->input('Address.0.first_name'); - $expected = array( - 'div' => array('class'), - 'label' => array('for'), - 'preg:/[^<]+/', - '/label', - 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'), - array('div' => array('class' => 'error-message')), - 'This field cannot be empty', - '/div', - '/div' - ); - $this->assertTags($result, $expected); + $this->Form->request->params['_csrfToken'] = 'testKey'; - $result = $this->Form->input('Address.0.last_name'); - $expected = array( - 'div' => array('class'), - 'label' => array('for'), - 'preg:/[^<]+/', - '/label', - 'input' => array('type' => 'text', 'name', 'id'), - '/div' - ); - $this->assertTags($result, $expected); + $this->Form->text('UserForm.published', array('name' => 'User[custom]')); + $this->assertEquals('User.custom', $this->Form->fields[0]); - $result = $this->Form->input('Address.1.last_name'); - $expected = array( - 'div' => array('class'), - 'label' => array('for'), - 'preg:/[^<]+/', - '/label', - 'input' => array( - 'type' => 'text', 'name' => 'preg:/[^<]+/', - 'id' => 'preg:/[^<]+/', 'class' => 'form-error' - ), - array('div' => array('class' => 'error-message')), - 'You must have a last name', - '/div', - '/div' - ); - $this->assertTags($result, $expected); + $this->Form->text('UserForm.published', array('name' => 'User[custom][another][value]')); + $this->assertEquals('User.custom.another.value', $this->Form->fields[1]); } /** - * testInput method + * testFormSecuredInput method * - * Test various incarnations of input(). + * Test generation of entire secure form, assertions made on input() output. * * @return void */ - public function testInput() { + public function testFormSecuredInput() { $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->input('ValidateUser.balance'); - $expected = array( - 'div' => array('class'), - 'label' => array('for'), - 'Balance', - '/label', - 'input' => array('name', 'type' => 'number', 'id'), - '/div', - ); - $this->assertTags($result, $expected); + $this->Form->request->params['_csrfToken'] = 'testKey'; - $result = $this->Form->input('ValidateUser.cost_decimal'); + $result = $this->Form->create('Contact', array('url' => '/contacts/add')); + $encoding = strtolower(Configure::read('App.encoding')); $expected = array( - 'div' => array('class'), - 'label' => array('for'), - 'Cost Decimal', - '/label', - 'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'), - '/div', + 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'), + 'div' => array('style' => 'display:none;'), + array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')), + array('input' => array( + 'type' => 'hidden', 'name' => '_csrfToken', + 'value' => 'testKey', 'id' => 'preg:/Token\d+/' + )), + '/div' ); $this->assertTags($result, $expected); @@ -1993,1712 +1768,1458 @@ public function testInput() { ); $this->assertTags($result, $expected); - $result = $this->Form->input('Contact.email', array('id' => 'custom')); + $result = $this->Form->input('UserForm.published', array('type' => 'text')); $expected = array( - 'div' => array('class' => 'input email'), - 'label' => array('for' => 'custom'), - 'Email', + 'div' => array('class' => 'input text'), + 'label' => array('for' => 'UserFormPublished'), + 'Published', '/label', array('input' => array( - 'type' => 'email', 'name' => 'Contact[email]', - 'id' => 'custom', 'maxlength' => 255 + 'type' => 'text', 'name' => 'UserForm[published]', + 'id' => 'UserFormPublished' )), '/div' ); $this->assertTags($result, $expected); - $result = $this->Form->input('Contact.email', array('div' => array('class' => false))); + $result = $this->Form->input('UserForm.other', array('type' => 'text')); $expected = array( - ' array('for' => 'ContactEmail'), - 'Email', + 'div' => array('class' => 'input text'), + 'label' => array('for' => 'UserFormOther'), + 'Other', '/label', array('input' => array( - 'type' => 'email', 'name' => 'Contact[email]', - 'id' => 'ContactEmail', 'maxlength' => 255 + 'type' => 'text', 'name' => 'UserForm[other]', + 'id' => 'UserFormOther' )), '/div' ); $this->assertTags($result, $expected); - $result = $this->Form->hidden('Contact.idontexist'); - $expected = array('input' => array( - 'type' => 'hidden', 'name' => 'Contact[idontexist]', - 'id' => 'ContactIdontexist' + $result = $this->Form->hidden('UserForm.stuff'); + $expected = array( + 'input' => array( + 'type' => 'hidden', 'name' => 'UserForm[stuff]', + 'id' => 'UserFormStuff' )); $this->assertTags($result, $expected); - $result = $this->Form->input('Contact.email', array('type' => 'text')); - $expected = array( - 'div' => array('class' => 'input text'), - 'label' => array('for' => 'ContactEmail'), - 'Email', - '/label', - array('input' => array( - 'type' => 'text', 'name' => 'Contact[email]', - 'id' => 'ContactEmail' - )), - '/div' - ); + $result = $this->Form->hidden('UserForm.hidden', array('value' => '0')); + $expected = array('input' => array( + 'type' => 'hidden', 'name' => 'UserForm[hidden]', + 'value' => '0', 'id' => 'UserFormHidden' + )); $this->assertTags($result, $expected); - $result = $this->Form->input('Contact.5.email', array('type' => 'text')); + $result = $this->Form->input('UserForm.something', array('type' => 'checkbox')); $expected = array( - 'div' => array('class' => 'input text'), - 'label' => array('for' => 'Contact5Email'), - 'Email', - '/label', + 'div' => array('class' => 'input checkbox'), array('input' => array( - 'type' => 'text', 'name' => 'Contact[5][email]', - 'id' => 'Contact5Email' + 'type' => 'hidden', 'name' => 'UserForm[something]', + 'value' => '0', 'id' => 'UserFormSomething_' )), - '/div' - ); - $this->assertTags($result, $expected); - - $result = $this->Form->input('Contact.password'); - $expected = array( - 'div' => array('class' => 'input password'), - 'label' => array('for' => 'ContactPassword'), - 'Password', - '/label', array('input' => array( - 'type' => 'password', 'name' => 'Contact[password]', - 'id' => 'ContactPassword' + 'type' => 'checkbox', 'name' => 'UserForm[something]', + 'value' => '1', 'id' => 'UserFormSomething' )), + 'label' => array('for' => 'UserFormSomething'), + 'Something', + '/label', '/div' ); $this->assertTags($result, $expected); - $result = $this->Form->input('Contact.email', array( - 'type' => 'file', 'class' => 'textbox' - )); + $result = $this->Form->fields; $expected = array( - 'div' => array('class' => 'input file'), - 'label' => array('for' => 'ContactEmail'), - 'Email', - '/label', - array('input' => array( - 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox', - 'id' => 'ContactEmail' - )), - '/div' + 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '', + 'UserForm.hidden' => '0', 'UserForm.something' ); - $this->assertTags($result, $expected); + $this->assertEquals($expected, $result); - $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars')); - $result = $this->Form->input('Contact.phone'); + $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff'; + + $result = $this->Form->secure($this->Form->fields); $expected = array( - 'div' => array('class' => 'input tel'), - 'label' => array('for' => 'ContactPhone'), - 'Phone', - '/label', + 'div' => array('style' => 'display:none;'), array('input' => array( - 'type' => 'tel', 'name' => 'Contact[phone]', - 'value' => 'Hello & World > weird chars', - 'id' => 'ContactPhone', 'maxlength' => 255 + 'type' => 'hidden', 'name' => '_Token[fields]', + 'value' => $hash, 'id' => 'preg:/TokenFields\d+/' + )), + array('input' => array( + 'type' => 'hidden', 'name' => '_Token[unlocked]', + 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/' )), '/div' ); $this->assertTags($result, $expected); + } - $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value'; - $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId')); - $expected = array( - 'div' => array('class' => 'input text'), - 'label' => array('for' => 'myId'), - 'Field', - '/label', - 'input' => array( - 'type' => 'text', 'name' => 'Model[0][OtherModel][field]', - 'value' => 'My value', 'id' => 'myId' - ), - '/div' - ); - $this->assertTags($result, $expected); - - unset($this->Form->request->data); - - $Contact->validationErrors['field'] = array('Badness!'); - $result = $this->Form->input('Contact.field'); - $expected = array( - 'div' => array('class' => 'input text error'), - 'label' => array('for' => 'ContactField'), - 'Field', - '/label', - 'input' => array( - 'type' => 'text', 'name' => 'Contact[field]', - 'id' => 'ContactField', 'class' => 'form-error' - ), - array('div' => array('class' => 'error-message')), - 'Badness!', - '/div', - '/div' - ); - $this->assertTags($result, $expected); +/** + * Test secured inputs with custom names. + * + * @return void + */ + public function testSecuredInputCustomName() { + $this->markTestIncomplete('Need to revisit once models work again.'); + $this->Form->request->params['_csrfToken'] = 'testKey'; + $this->assertEquals(array(), $this->Form->fields); - $result = $this->Form->input('Contact.field', array( - 'div' => false, 'error' => array('attributes' => array('wrap' => 'span')) + $this->Form->input('text_input', array( + 'name' => 'data[Option][General.default_role]', )); - $expected = array( - 'label' => array('for' => 'ContactField'), - 'Field', - '/label', - 'input' => array( - 'type' => 'text', 'name' => 'Contact[field]', - 'id' => 'ContactField', 'class' => 'form-error' - ), - array('span' => array('class' => 'error-message')), - 'Badness!', - '/span' - ); - $this->assertTags($result, $expected); + $expected = array('Option.General.default_role'); + $this->assertEquals($expected, $this->Form->fields); - $result = $this->Form->input('Contact.field', array( - 'type' => 'text', 'error' => array('attributes' => array('class' => 'error')) + $this->Form->input('select_box', array( + 'name' => 'data[Option][General.select_role]', + 'type' => 'select', + 'options' => array(1, 2), )); - $expected = array( - 'div' => array('class' => 'input text error'), - 'label' => array('for' => 'ContactField'), - 'Field', - '/label', - 'input' => array( - 'type' => 'text', 'name' => 'Contact[field]', - 'id' => 'ContactField', 'class' => 'form-error' - ), - array('div' => array('class' => 'error')), - 'Badness!', - '/div' - ); - $this->assertTags($result, $expected); + $expected = array('Option.General.default_role', 'Option.General.select_role'); + $this->assertEquals($expected, $this->Form->fields); + } - $result = $this->Form->input('Contact.field', array( - 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false)) - )); - $expected = array( - 'span' => array('class' => 'input text error'), - 'label' => array('for' => 'ContactField'), - 'Field', - '/label', - 'input' => array( - 'type' => 'text', 'name' => 'Contact[field]', - 'id' => 'ContactField', 'class' => 'form-error' - ), - 'Badness!', - '/span' - ); - $this->assertTags($result, $expected); +/** + * Tests that the correct keys are added to the field hash index + * + * @return void + */ + public function testFormSecuredFileInput() { + $this->markTestIncomplete('Need to revisit once models work again.'); + $this->Form->request->params['_csrfToken'] = 'testKey'; + $this->assertEquals(array(), $this->Form->fields); - $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy')); + $this->Form->file('Attachment.file'); $expected = array( - 'div' => array('class' => 'input text error'), - 'label' => array('for' => 'ContactField'), - 'Field', - '/label', - 'input' => array( - 'type' => 'text', 'name' => 'Contact[field]', - 'id' => 'ContactField', 'class' => 'form-error' - ), - 'A message to you, Rudy', - array('div' => array('class' => 'error-message')), - 'Badness!', - '/div', - '/div' + 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name', + 'Attachment.file.error', 'Attachment.file.size' ); - $this->assertTags($result, $expected); + $this->assertEquals($expected, $this->Form->fields); + } - $this->Form->setEntity(null); - $this->Form->setEntity('Contact.field'); - $result = $this->Form->input('Contact.field', array( - 'after' => 'A message to you, Rudy', 'error' => false - )); - $expected = array( - 'div' => array('class' => 'input text'), - 'label' => array('for' => 'ContactField'), - 'Field', - '/label', - 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'), - 'A message to you, Rudy', - '/div' - ); - $this->assertTags($result, $expected); +/** + * test that multiple selects keys are added to field hash + * + * @return void + */ + public function testFormSecuredMultipleSelect() { + $this->markTestIncomplete('Need to revisit once models work again.'); + $this->Form->request->params['_csrfToken'] = 'testKey'; + $this->assertEquals(array(), $this->Form->fields); + $options = array('1' => 'one', '2' => 'two'); - $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy')); - $expected = array( - 'div' => array('class' => 'input text'), - 'label' => array('for' => 'ObjectField'), - 'Field', - '/label', - 'input' => array('type' => 'text', 'name' => 'Object[field]', 'id' => 'ObjectField'), - 'A message to you, Rudy', - '/div' - ); - $this->assertTags($result, $expected); + $this->Form->select('Model.select', $options); + $expected = array('Model.select'); + $this->assertEquals($expected, $this->Form->fields); - $Contact->validationErrors['field'] = array('minLength'); - $result = $this->Form->input('Contact.field', array( - 'error' => array( - 'minLength' => 'Le login doit contenir au moins 2 caractères', - 'maxLength' => 'login too large' - ) - )); - $expected = array( - 'div' => array('class' => 'input text error'), - 'label' => array('for' => 'ContactField'), - 'Field', - '/label', - 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'), - array('div' => array('class' => 'error-message')), - 'Le login doit contenir au moins 2 caractères', - '/div', - '/div' - ); - $this->assertTags($result, $expected); + $this->Form->fields = array(); + $this->Form->select('Model.select', $options, array('multiple' => true)); + $this->assertEquals($expected, $this->Form->fields); + } - $Contact->validationErrors['field'] = array('maxLength'); - $result = $this->Form->input('Contact.field', array( - 'error' => array( - 'attributes' => array('wrap' => 'span', 'rel' => 'fake'), - 'minLength' => 'Le login doit contenir au moins 2 caractères', - 'maxLength' => 'login too large', - ) - )); - $expected = array( - 'div' => array('class' => 'input text error'), - 'label' => array('for' => 'ContactField'), - 'Field', - '/label', - 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'), - array('span' => array('class' => 'error-message', 'rel' => 'fake')), - 'login too large', - '/span', - '/div' - ); - $this->assertTags($result, $expected); +/** + * testFormSecuredRadio method + * + * @return void + */ + public function testFormSecuredRadio() { + $this->markTestIncomplete('Need to revisit once models work again.'); + $this->Form->request->params['_csrfToken'] = 'testKey'; + $this->assertEquals(array(), $this->Form->fields); + $options = array('1' => 'option1', '2' => 'option2'); + + $this->Form->radio('Test.test', $options); + $expected = array('Test.test'); + $this->assertEquals($expected, $this->Form->fields); } /** - * Test that inputs with 0 can be created. + * Test that when disabled is in a list based attribute array it works. * * @return void */ - public function testInputZero() { + public function testFormSecuredAndDisabledNotAssoc() { $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->create('User'); - $result = $this->Form->input('0'); + $this->Form->request->params['_csrfToken'] = 'testKey'; + + $this->Form->select('Model.select', array(1, 2), array('disabled')); + $this->Form->checkbox('Model.checkbox', array('disabled')); + $this->Form->text('Model.text', array('disabled')); + $this->Form->textarea('Model.textarea', array('disabled')); + $this->Form->password('Model.password', array('disabled')); + $this->Form->radio('Model.radio', array(1, 2), array('disabled')); + $expected = array( - 'div' => array('class' => 'input text'), - 'label' => array('for' => 'User0'), '/label', - 'input' => array('type' => 'text', 'name' => 'User[0]', 'id' => 'User0'), - '/div' + 'Model.radio' => '' ); - $this->assertTags($result, $expected); + $this->assertEquals($expected, $this->Form->fields); } /** - * test input() with checkbox creation + * test that forms with disabled inputs + secured forms leave off the inputs from the form + * hashing. * * @return void */ - public function testInputCheckbox() { + public function testFormSecuredAndDisabled() { $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->input('User.active', array('label' => false, 'checked' => true)); - $expected = array( - 'div' => array('class' => 'input checkbox'), - 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'), - array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')), - '/div' - ); - $this->assertTags($result, $expected); + $this->Form->request->params['_csrfToken'] = 'testKey'; + + $this->Form->checkbox('Model.checkbox', array('disabled' => true)); + $this->Form->text('Model.text', array('disabled' => true)); + $this->Form->password('Model.text', array('disabled' => true)); + $this->Form->textarea('Model.textarea', array('disabled' => true)); + $this->Form->select('Model.select', array(1, 2), array('disabled' => true)); + $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2))); + $this->Form->year('Model.year', null, null, array('disabled' => true)); + $this->Form->month('Model.month', array('disabled' => true)); + $this->Form->day('Model.day', array('disabled' => true)); + $this->Form->hour('Model.hour', false, array('disabled' => true)); + $this->Form->minute('Model.minute', array('disabled' => true)); + $this->Form->meridian('Model.meridian', array('disabled' => true)); - $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1)); $expected = array( - 'div' => array('class' => 'input checkbox'), - 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'), - array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')), - '/div' + 'Model.radio' => '' ); - $this->assertTags($result, $expected); + $this->assertEquals($expected, $this->Form->fields); + } - $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1')); - $expected = array( - 'div' => array('class' => 'input checkbox'), - 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'), - array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')), - '/div' +/** + * testDisableSecurityUsingForm method + * + * @return void + */ + public function testDisableSecurityUsingForm() { + $this->markTestIncomplete('Need to revisit once models work again.'); + $this->Form->request->params['_csrfToken'] = 'testKey'; + $this->Form->request['_Token'] = array( + 'disabledFields' => array() ); - $this->assertTags($result, $expected); + $this->Form->create(); - $result = $this->Form->input('User.disabled', array( - 'label' => 'Disabled', - 'type' => 'checkbox', - 'data-foo' => 'disabled' + $this->Form->hidden('Addresses.id', array('value' => '123456')); + $this->Form->input('Addresses.title'); + $this->Form->input('Addresses.first_name', array('secure' => false)); + $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false)); + $this->Form->input('Addresses.zip', array( + 'type' => 'select', 'options' => array(1, 2), 'secure' => false )); + + $result = $this->Form->fields; $expected = array( - 'div' => array('class' => 'input checkbox'), - 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'), - array('input' => array( - 'type' => 'checkbox', - 'name' => 'data[User][disabled]', - 'value' => '1', - 'id' => 'UserDisabled', - 'data-foo' => 'disabled' - )), - 'label' => array('for' => 'UserDisabled'), - 'Disabled', - '/label', - '/div' + 'Addresses.id' => '123456', 'Addresses.title', ); - $this->assertTags($result, $expected); + $this->assertEquals($expected, $result); } /** - * test form->input() with time types. + * test disableField * + * @return void */ - public function testInputTime() { + public function testUnlockFieldAddsToList() { $this->markTestIncomplete('Need to revisit once models work again.'); - extract($this->dateRegex); - $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24)); - $result = explode(':', $result); - $this->assertRegExp('/option value="23"/', $result[0]); - $this->assertNotRegExp('/option value="24"/', $result[0]); - - $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24)); - $result = explode(':', $result); - $this->assertRegExp('/option value="23"/', $result[0]); - $this->assertNotRegExp('/option value="24"/', $result[0]); - - $result = $this->Form->input('Model.field', array( - 'type' => 'time', 'timeFormat' => 24, 'interval' => 15 - )); - $result = explode(':', $result); - $this->assertNotRegExp('##', $result[1]); - $this->assertNotRegExp('##', $result[1]); - $this->assertRegExp('##', $result[1]); - - $result = $this->Form->input('Model.field', array( - 'type' => 'time', 'timeFormat' => 12, 'interval' => 15 - )); - $result = explode(':', $result); - $this->assertNotRegExp('##', $result[1]); - $this->assertNotRegExp('##', $result[1]); - $this->assertRegExp('##', $result[1]); + $this->Form->request->params['_csrfToken'] = 'testKey'; + $this->Form->request['_Token'] = array( + 'unlockedFields' => array() + ); + $this->Form->create('Contact'); + $this->Form->unlockField('Contact.name'); + $this->Form->text('Contact.name'); - $result = $this->Form->input('prueba', array( - 'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008, - 'maxYear' => date('Y') + 1, 'interval' => 15 - )); - $result = explode(':', $result); - $this->assertNotRegExp('##', $result[1]); - $this->assertNotRegExp('##', $result[1]); - $this->assertRegExp('##', $result[1]); - $this->assertRegExp('##', $result[1]); + $this->assertEquals(array('Contact.name'), $this->Form->unlockField()); + $this->assertEquals(array(), $this->Form->fields); + } - $result = $this->Form->input('Random.start_time', array( - 'type' => 'time', - 'selected' => '18:15' - )); - $this->assertContains('', $result); - $this->assertContains('', $result); - $this->assertContains('', $result); +/** + * test unlockField removing from fields array. + * + * @return void + */ + public function testUnlockFieldRemovingFromFields() { + $this->markTestIncomplete('Need to revisit once models work again.'); + $this->Form->request->params['_csrfToken'] = 'testKey'; + $this->Form->request['_Token'] = array( + 'unlockedFields' => array() + ); + $this->Form->create('Contact'); + $this->Form->hidden('Contact.id', array('value' => 1)); + $this->Form->text('Contact.name'); - $result = $this->Form->input('published', array('type' => 'time')); - $now = strtotime('now'); - $this->assertContains('', $result); + $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.'); + $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.'); - $now = strtotime('2013-03-09 00:42:21'); - $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now)); - $this->assertContains('', $result); - $this->assertContains('', $result); + $this->Form->unlockField('Contact.name'); + $this->Form->unlockField('Contact.id'); + $this->assertEquals(array(), $this->Form->fields); } /** - * Test interval + selected near the hour roll over. + * testTagIsInvalid method * * @return void */ - public function testTimeSelectedWithInterval() { + public function testTagIsInvalid() { $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->input('Model.start_time', array( - 'type' => 'time', - 'interval' => 15, - 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm') - )); - $this->assertContains('', $result); - $this->assertContains('', $result); - $this->assertContains('', $result); + $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email'); - $result = $this->Form->input('Model.start_time', array( - 'type' => 'time', - 'interval' => 15, - 'selected' => '2012-10-23 15:57:00' - )); - $this->assertContains('', $result); - $this->assertContains('', $result); - $this->assertContains('', $result); - - $result = $this->Form->input('Model.start_time', array( - 'timeFormat' => 24, - 'type' => 'time', - 'interval' => 15, - 'selected' => '15:57' - )); - $this->assertContains('', $result); - $this->assertContains('', $result); + $this->Form->setEntity('Contact.0.email'); + $result = $this->Form->tagIsInvalid(); + $this->assertEquals($expected, $result); - $result = $this->Form->input('Model.start_time', array( - 'timeFormat' => 24, - 'type' => 'time', - 'interval' => 15, - 'selected' => '23:57' - )); - $this->assertContains('', $result); - $this->assertContains('', $result); + $this->Form->setEntity('Contact.1.email'); + $result = $this->Form->tagIsInvalid(); + $this->assertFalse($result); - $result = $this->Form->input('Model.created', array( - 'timeFormat' => 24, - 'type' => 'datetime', - 'interval' => 15, - 'selected' => '2012-09-30 23:56' - )); - $this->assertContains('', $result); - $this->assertContains('', $result); - $this->assertContains('', $result); - $this->assertContains('', $result); - $this->assertContains('', $result); + $this->Form->setEntity('Contact.0.name'); + $result = $this->Form->tagIsInvalid(); + $this->assertFalse($result); } /** - * Test time with selected values around 12:xx:xx + * Test tagIsInvalid with validation errors from a saveMany * * @return void */ - public function testTimeSelectedWithIntervalTwelve() { + public function testTagIsInvalidSaveMany() { $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->input('Model.start_time', array( - 'type' => 'time', - 'timeFormat' => 12, - 'interval' => 15, - 'selected' => '00:00:00' - )); - $this->assertContains('', $result); - $this->assertContains('', $result); - $this->assertContains('', $result); + $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email'); - $result = $this->Form->input('Model.start_time', array( - 'type' => 'time', - 'timeFormat' => 12, - 'interval' => 15, - 'selected' => '12:00:00' - )); - $this->assertContains('', $result); - $this->assertContains('', $result); - $this->assertContains('', $result); + $this->Form->create('Contact'); - $result = $this->Form->input('Model.start_time', array( - 'type' => 'time', - 'timeFormat' => 12, - 'interval' => 15, - 'selected' => '12:15:00' - )); - $this->assertContains('', $result); - $this->assertContains('', $result); - $this->assertContains('', $result); + $this->Form->setEntity('0.email'); + $result = $this->Form->tagIsInvalid(); + $this->assertEquals($expected, $result); + + $this->Form->setEntity('0.Contact.email'); + $result = $this->Form->tagIsInvalid(); + $this->assertEquals($expected, $result); } /** - * Test interval & timeFormat = 12 + * Test validation errors. * * @return void */ - public function testInputTimeWithIntervalAnd12HourFormat() { + public function testPasswordValidation() { $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->input('Model.start_time', array( - 'type' => 'time', - 'timeFormat' => 12, - 'interval' => 5, - 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm') - )); - $this->assertContains('', $result); - $this->assertContains('', $result); - $this->assertContains('', $result); - - $result = $this->Form->input('Model.start_time', array( - 'type' => 'time', - 'timeFormat' => '12', - 'interval' => 5, - 'selected' => '2013-04-19 16:30:00' - )); - $this->assertContains('', $result); - $this->assertContains('', $result); - $this->assertContains('', $result); - - $result = $this->Form->input('Model.start_time', array( - 'type' => 'time', - 'timeFormat' => '12', - 'interval' => 10, - 'selected' => '2013-05-19 00:33:00' - )); - $this->assertContains('', $result); - $this->assertContains('', $result); - $this->assertContains('', $result); + $Contact->validationErrors['password'] = array('Please provide a password'); - $result = $this->Form->input('Model.start_time', array( - 'type' => 'time', - 'timeFormat' => '12', - 'interval' => 10, - 'selected' => '2013-05-19 13:33:00' - )); - $this->assertContains('', $result); - $this->assertContains('', $result); - $this->assertContains('', $result); + $result = $this->Form->input('Contact.password'); + $expected = array( + 'div' => array('class' => 'input password error'), + 'label' => array('for' => 'ContactPassword'), + 'Password', + '/label', + 'input' => array( + 'type' => 'password', 'name' => 'Contact[password]', + 'id' => 'ContactPassword', 'class' => 'form-error' + ), + array('div' => array('class' => 'error-message')), + 'Please provide a password', + '/div', + '/div' + ); + $this->assertTags($result, $expected); - $result = $this->Form->input('Model.start_time', array( - 'type' => 'time', - 'timeFormat' => '12', - 'interval' => 10, - 'selected' => '2013-05-19 01:33:00' - )); - $this->assertContains('', $result); - $this->assertContains('', $result); - $this->assertContains('', $result); + $result = $this->Form->input('Contact.password', array('errorMessage' => false)); + $expected = array( + 'div' => array('class' => 'input password error'), + 'label' => array('for' => 'ContactPassword'), + 'Password', + '/label', + 'input' => array( + 'type' => 'password', 'name' => 'Contact[password]', + 'id' => 'ContactPassword', 'class' => 'form-error' + ), + '/div' + ); + $this->assertTags($result, $expected); } /** - * test form->input() with datetime, date and time types + * Test validation errors, when validation message is an empty string. * * @return void */ - public function testInputDatetime() { + public function testEmptyErrorValidation() { $this->markTestIncomplete('Need to revisit once models work again.'); - extract($this->dateRegex); - $result = $this->Form->input('prueba', array( - 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008, - 'maxYear' => date('Y') + 1, 'interval' => 15 - )); - $result = explode(':', $result); - $this->assertNotRegExp('##', $result[1]); - $this->assertNotRegExp('##', $result[1]); - $this->assertRegExp('##', $result[1]); - $this->assertRegExp('##', $result[1]); - - //related to ticket #5013 - $result = $this->Form->input('Contact.date', array( - 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}' - )); - $this->assertRegExp('/class="customClass"/', $result); - $this->assertRegExp('/onChange="function\(\)\{\}"/', $result); - - $result = $this->Form->input('Contact.date', array( - 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}' - )); - $this->assertRegExp('/id="customIdDay"/', $result); - $this->assertRegExp('/id="customIdMonth"/', $result); - $this->assertRegExp('/onChange="function\(\)\{\}"/', $result); - - $result = $this->Form->input('Model.field', array( - 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID' - )); - $this->assertRegExp('/id="customIDDay"/', $result); - $this->assertRegExp('/id="customIDHour"/', $result); - $result = explode('assertRegExp('/option value="23"/', $result[0]); - $this->assertNotRegExp('/option value="24"/', $result[0]); - - $result = $this->Form->input('Model.field', array( - 'type' => 'datetime', 'timeFormat' => 12 - )); - $result = explode('assertRegExp('/option value="12"/', $result[0]); - $this->assertNotRegExp('/option value="13"/', $result[0]); + $this->Form->validationErrors['Contact']['password'] = ''; - $this->Form->request->data = array('Contact' => array('created' => null)); - $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown')); + $result = $this->Form->input('Contact.password'); $expected = array( - 'div' => array('class' => 'input date'), - 'label' => array('for' => 'ContactCreatedMonth'), - 'Created', + 'div' => array('class' => 'input password error'), + 'label' => array('for' => 'ContactPassword'), + 'Password', '/label', - array('select' => array('name' => 'Contact[created][month]', 'id' => 'ContactCreatedMonth')), - array('option' => array('value' => '')), 'Date Unknown', '/option', - $monthsRegex, - '/select', '-', - array('select' => array('name' => 'Contact[created][day]', 'id' => 'ContactCreatedDay')), - array('option' => array('value' => '')), 'Date Unknown', '/option', - $daysRegex, - '/select', '-', - array('select' => array('name' => 'Contact[created][year]', 'id' => 'ContactCreatedYear')), - array('option' => array('value' => '')), 'Date Unknown', '/option', - $yearsRegex, - '/select', + 'input' => array( + 'type' => 'password', 'name' => 'Contact[password]', + 'id' => 'ContactPassword', 'class' => 'form-error' + ), + array('div' => array('class' => 'error-message')), + array(), + '/div', '/div' ); $this->assertTags($result, $expected); - $this->Form->request->data = array('Contact' => array('created' => null)); - $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE')); - $this->assertRegExp('/for\="ContactCreatedHour"/', $result); - - $this->Form->request->data = array('Contact' => array('created' => null)); - $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE')); - $this->assertRegExp('/for\="ContactCreatedMonth"/', $result); - - $result = $this->Form->input('Contact.created', array( - 'type' => 'date', - 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'), - 'timeFormat' => 'NONE' - )); - $this->assertRegExp('/for\="created-month"/', $result); - } - -/** - * Test generating checkboxes in a loop. - * - * @return void - */ - public function testInputCheckboxesInLoop() { - $this->markTestIncomplete('Need to revisit once models work again.'); - for ($i = 1; $i < 5; $i++) { - $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i)); - $expected = array( - 'div' => array('class' => 'input checkbox'), - 'input' => array('type' => 'hidden', 'name' => "Contact[{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"), - array('input' => array('type' => 'checkbox', 'name' => "Contact[{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")), - 'label' => array('for' => "Contact{$i}Email"), - 'Email', - '/label', - '/div' - ); - $this->assertTags($result, $expected); - } - } + $result = $this->Form->input('Contact.password', array('errorMessage' => false)); + $expected = array( + 'div' => array('class' => 'input password error'), + 'label' => array('for' => 'ContactPassword'), + 'Password', + '/label', + 'input' => array( + 'type' => 'password', 'name' => 'Contact[password]', + 'id' => 'ContactPassword', 'class' => 'form-error' + ), + '/div' + ); + $this->assertTags($result, $expected); + } /** - * Test generating checkboxes with disabled elements. + * Test validation errors, when calling input() overriding validation message by an empty string. * * @return void */ - public function testInputCheckboxWithDisabledElements() { + public function testEmptyInputErrorValidation() { $this->markTestIncomplete('Need to revisit once models work again.'); - $options = array(1 => 'One', 2 => 'Two', '3' => 'Three'); - $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options)); + $this->Form->validationErrors['Contact']['password'] = 'Please provide a password'; + $result = $this->Form->input('Contact.password', array('error' => '')); $expected = array( - array('div' => array('class' => 'input select')), - array('label' => array('for' => "ContactMultiple")), - 'Multiple', - '/label', - array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")), - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")), - array('label' => array('for' => "ContactMultiple1")), - 'One', - '/label', - '/div', - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")), - array('label' => array('for' => "ContactMultiple2")), - 'Two', - '/label', - '/div', - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")), - array('label' => array('for' => "ContactMultiple3")), - 'Three', + 'div' => array('class' => 'input password error'), + 'label' => array('for' => 'ContactPassword'), + 'Password', '/label', + 'input' => array( + 'type' => 'password', 'name' => 'Contact[password]', + 'id' => 'ContactPassword', 'class' => 'form-error' + ), + array('div' => array('class' => 'error-message')), + array(), '/div', '/div' ); $this->assertTags($result, $expected); - $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options)); - $this->assertTags($result, $expected); - - $disabled = array('2', 3); - + $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false)); $expected = array( - array('div' => array('class' => 'input select')), - array('label' => array('for' => "ContactMultiple")), - 'Multiple', - '/label', - array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")), - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'id' => "ContactMultiple1")), - array('label' => array('for' => "ContactMultiple1")), - 'One', - '/label', - '/div', - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")), - array('label' => array('for' => "ContactMultiple2")), - 'Two', - '/label', - '/div', - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")), - array('label' => array('for' => "ContactMultiple3")), - 'Three', + 'div' => array('class' => 'input password error'), + 'label' => array('for' => 'ContactPassword'), + 'Password', '/label', - '/div', + 'input' => array( + 'type' => 'password', 'name' => 'Contact[password]', + 'id' => 'ContactPassword', 'class' => 'form-error' + ), '/div' ); - $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options)); $this->assertTags($result, $expected); + } - // make sure 50 does only disable 50, and not 50f5c0cf - $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy'); - $disabled = array(50); +/** + * testFormValidationAssociated method + * + * test display of form errors in conjunction with model::validates. + * + * @return void + */ + public function testFormValidationAssociated() { + $this->markTestIncomplete('Need to revisit once models work again.'); + + $data = array( + 'UserForm' => array('name' => 'user'), + 'OpenidUrl' => array('url' => 'http://www.cakephp.org') + ); + + $result = $this->UserForm->OpenidUrl->create($data); + $this->assertFalse(empty($result)); + $this->assertFalse($this->UserForm->OpenidUrl->validates()); + $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login')); + $encoding = strtolower(Configure::read('App.encoding')); $expected = array( - array('div' => array('class' => 'input select')), - array('label' => array('for' => "ContactMultiple")), - 'Multiple', - '/label', - array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")), - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")), - array('label' => array('for' => "ContactMultiple50")), - 'Fifty', - '/label', - '/div', - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")), - array('label' => array('for' => "ContactMultiple50f5c0cf")), - 'Stringy', - '/label', - '/div', + 'form' => array( + 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm', + 'accept-charset' => $encoding + ), + 'div' => array('style' => 'display:none;'), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), '/div' ); - $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options)); $this->assertTags($result, $expected); + + $result = $this->Form->error( + 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false) + ); + $this->assertEquals('Error, not registered', $result); + + unset($this->UserForm->OpenidUrl, $this->UserForm); } /** - * test input name with leading integer, ensure attributes are generated correctly. + * testFormValidationAssociatedFirstLevel method + * + * test form error display with associated model. * * @return void */ - public function testInputWithLeadingInteger() { + public function testFormValidationAssociatedFirstLevel() { $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->text('0.Node.title'); + + $data = array( + 'ValidateUser' => array('name' => 'mariano'), + 'ValidateProfile' => array('full_name' => 'Mariano Iglesias') + ); + + $result = $this->ValidateUser->create($data); + $this->assertFalse(empty($result)); + $this->assertFalse($this->ValidateUser->validates()); + $this->assertFalse($this->ValidateUser->ValidateProfile->validates()); + + $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add')); + $encoding = strtolower(Configure::read('App.encoding')); $expected = array( - 'input' => array('name' => '0[Node][title]', 'id' => '0NodeTitle', 'type' => 'text') + 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding), + 'div' => array('style' => 'display:none;'), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), + '/div' ); $this->assertTags($result, $expected); + + $result = $this->Form->error( + 'ValidateUser.email', 'Invalid email', array('wrap' => false) + ); + $this->assertEquals('Invalid email', $result); + + $result = $this->Form->error( + 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false) + ); + $this->assertEquals('Invalid name', $result); + + $result = $this->Form->error( + 'ValidateProfile.city', 'Invalid city', array('wrap' => false) + ); + $this->assertEquals('Invalid city', $result); + + unset($this->ValidateUser->ValidateProfile); + unset($this->ValidateUser); } /** - * test form->input() with select type inputs. + * testFormValidationAssociatedSecondLevel method + * + * test form error display with associated model. * * @return void */ - public function testInputSelectType() { + public function testFormValidationAssociatedSecondLevel() { $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->input('email', array( - 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true) + + $data = array( + 'ValidateUser' => array('name' => 'mariano'), + 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'), + 'ValidateItem' => array('name' => 'Item') ); + + $result = $this->ValidateUser->create($data); + $this->assertFalse(empty($result)); + $this->assertFalse($this->ValidateUser->validates()); + $this->assertFalse($this->ValidateUser->ValidateProfile->validates()); + $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates()); + + $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add')); + $encoding = strtolower(Configure::read('App.encoding')); $expected = array( - 'div' => array('class' => 'input select'), - 'label' => array('for' => 'email'), - 'Email', - '/label', - array('select' => array('name' => 'email', 'id' => 'email')), - array('option' => array('value' => '')), - '/option', - array('option' => array('value' => 'è')), - 'Firést', - '/option', - array('option' => array('value' => 'é')), - 'Secoènd', - '/option', - '/select', + 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding), + 'div' => array('style' => 'display:none;'), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), '/div' ); $this->assertTags($result, $expected); - $result = $this->Form->input('email', array( - 'options' => array('First', 'Second'), 'empty' => true) + $result = $this->Form->error( + 'ValidateUser.email', 'Invalid email', array('wrap' => false) ); - $expected = array( - 'div' => array('class' => 'input select'), - 'label' => array('for' => 'email'), - 'Email', - '/label', - array('select' => array('name' => 'email', 'id' => 'email')), - array('option' => array('value' => '')), - '/option', - array('option' => array('value' => '0')), - 'First', - '/option', - array('option' => array('value' => '1')), - 'Second', - '/option', - '/select', - '/div' + $this->assertEquals('Invalid email', $result); + + $result = $this->Form->error( + 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false) ); - $this->assertTags($result, $expected); + $this->assertEquals('Invalid name', $result); - $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad'); - $this->Form->request->data = array('Model' => array('user_id' => 'value')); + $result = $this->Form->error( + 'ValidateProfile.city', 'Invalid city', array('wrap' => false) + ); - $result = $this->Form->input('Model.user_id', array('empty' => true)); + $result = $this->Form->error( + 'ValidateItem.description', 'Invalid description', array('wrap' => false) + ); + $this->assertEquals('Invalid description', $result); + + unset($this->ValidateUser->ValidateProfile->ValidateItem); + unset($this->ValidateUser->ValidateProfile); + unset($this->ValidateUser); + } + +/** + * testFormValidationMultiRecord method + * + * test form error display with multiple records. + * + * @return void + */ + public function testFormValidationMultiRecord() { + $this->markTestIncomplete('Need to revisit once models work again.'); + $Contact->validationErrors[2] = array( + 'name' => array('The provided value is invalid') + ); + $result = $this->Form->input('Contact.2.name'); $expected = array( - 'div' => array('class' => 'input select'), - 'label' => array('for' => 'ModelUserId'), - 'User', + 'div' => array('class' => 'input text error'), + 'label' => array('for' => 'Contact2Name'), + 'Name', '/label', - 'select' => array('name' => 'Model[user_id]', 'id' => 'ModelUserId'), - array('option' => array('value' => '')), - '/option', - array('option' => array('value' => 'value', 'selected' => 'selected')), - 'good', - '/option', - array('option' => array('value' => 'other')), - 'bad', - '/option', - '/select', + 'input' => array( + 'type' => 'text', 'name' => 'Contact[2][name]', 'id' => 'Contact2Name', + 'class' => 'form-error', 'maxlength' => 255 + ), + array('div' => array('class' => 'error-message')), + 'The provided value is invalid', + '/div', '/div' ); $this->assertTags($result, $expected); + } - $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad'); - $this->Form->request->data = array('Thing' => array('user_id' => null)); - $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty')); +/** + * testMultipleInputValidation method + * + * test multiple record form validation error display. + * + * @return void + */ + public function testMultipleInputValidation() { + $this->markTestIncomplete('Need to revisit once models work again.'); + $Address->validationErrors[0] = array( + 'title' => array('This field cannot be empty'), + 'first_name' => array('This field cannot be empty') + ); + $Address->validationErrors[1] = array( + 'last_name' => array('You must have a last name') + ); + $this->Form->create(); + + $result = $this->Form->input('Address.0.title'); $expected = array( - 'div' => array('class' => 'input select'), - 'label' => array('for' => 'ThingUserId'), - 'User', + 'div' => array('class'), + 'label' => array('for'), + 'preg:/[^<]+/', '/label', - 'select' => array('name' => 'Thing[user_id]', 'id' => 'ThingUserId'), - array('option' => array('value' => '')), - 'Some Empty', - '/option', - array('option' => array('value' => 'value')), - 'good', - '/option', - array('option' => array('value' => 'other')), - 'bad', - '/option', - '/select', + 'input' => array( + 'type' => 'text', 'name', 'id', 'class' => 'form-error' + ), + array('div' => array('class' => 'error-message')), + 'This field cannot be empty', + '/div', '/div' ); $this->assertTags($result, $expected); - $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad'); - $this->Form->request->data = array('Thing' => array('user_id' => 'value')); - $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty')); + $result = $this->Form->input('Address.0.first_name'); $expected = array( - 'div' => array('class' => 'input select'), - 'label' => array('for' => 'ThingUserId'), - 'User', + 'div' => array('class'), + 'label' => array('for'), + 'preg:/[^<]+/', '/label', - 'select' => array('name' => 'Thing[user_id]', 'id' => 'ThingUserId'), - array('option' => array('value' => '')), - 'Some Empty', - '/option', - array('option' => array('value' => 'value', 'selected' => 'selected')), - 'good', - '/option', - array('option' => array('value' => 'other')), - 'bad', - '/option', - '/select', + 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'), + array('div' => array('class' => 'error-message')), + 'This field cannot be empty', + '/div', '/div' ); $this->assertTags($result, $expected); - $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad'); - $this->Form->request->data = array('User' => array('User' => array('value'))); - $result = $this->Form->input('User.User', array('empty' => true)); + $result = $this->Form->input('Address.0.last_name'); $expected = array( - 'div' => array('class' => 'input select'), - 'label' => array('for' => 'UserUser'), - 'User', + 'div' => array('class'), + 'label' => array('for'), + 'preg:/[^<]+/', '/label', - 'input' => array('type' => 'hidden', 'name' => 'User[User]', 'value' => '', 'id' => 'UserUser_'), - 'select' => array('name' => 'User[User][]', 'id' => 'UserUser', 'multiple' => 'multiple'), - array('option' => array('value' => '')), - '/option', - array('option' => array('value' => 'value', 'selected' => 'selected')), - 'good', - '/option', - array('option' => array('value' => 'other')), - 'bad', - '/option', - '/select', + 'input' => array('type' => 'text', 'name', 'id'), '/div' ); $this->assertTags($result, $expected); - $this->Form->data = array(); - $result = $this->Form->input('Publisher.id', array( - 'label' => 'Publisher', - 'type' => 'select', - 'multiple' => 'checkbox', - 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2') - )); + $result = $this->Form->input('Address.1.last_name'); $expected = array( - array('div' => array('class' => 'input select')), - array('label' => array('for' => 'PublisherId')), - 'Publisher', - '/label', - 'input' => array('type' => 'hidden', 'name' => 'Publisher[id]', 'value' => '', 'id' => 'PublisherId'), - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')), - array('label' => array('for' => 'PublisherIdValue1')), - 'Label 1', - '/label', - '/div', - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')), - array('label' => array('for' => 'PublisherIdValue2')), - 'Label 2', - '/label', - '/div', + 'div' => array('class'), + 'label' => array('for'), + 'preg:/[^<]+/', + '/label', + 'input' => array( + 'type' => 'text', 'name' => 'preg:/[^<]+/', + 'id' => 'preg:/[^<]+/', 'class' => 'form-error' + ), + array('div' => array('class' => 'error-message')), + 'You must have a last name', + '/div', '/div' ); $this->assertTags($result, $expected); } /** - * test that input() and a non standard primary key makes a hidden input by default. + * testInput method + * + * Test various incarnations of input(). * * @return void */ - public function testInputWithNonStandardPrimaryKeyMakesHidden() { + public function testInput() { $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->create('User'); - $this->Form->fieldset = array( - 'User' => array( - 'fields' => array( - 'model_id' => array('type' => 'integer') - ), - 'validates' => array(), - 'key' => 'model_id' - ) + $result = $this->Form->input('ValidateUser.balance'); + $expected = array( + 'div' => array('class'), + 'label' => array('for'), + 'Balance', + '/label', + 'input' => array('name', 'type' => 'number', 'id'), + '/div', ); - $result = $this->Form->input('model_id'); + $this->assertTags($result, $expected); + + $result = $this->Form->input('ValidateUser.cost_decimal'); $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'User[model_id]', 'id' => 'UserModelId'), + 'div' => array('class'), + 'label' => array('for'), + 'Cost Decimal', + '/label', + 'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'), + '/div', ); $this->assertTags($result, $expected); - } -/** - * test that overriding the magic select type widget is possible - * - * @return void - */ - public function testInputOverridingMagicSelectType() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad'); - $result = $this->Form->input('Model.user_id', array('type' => 'text')); + $result = $this->Form->input('Contact.email', array('id' => 'custom')); $expected = array( - 'div' => array('class' => 'input text'), - 'label' => array('for' => 'ModelUserId'), 'User', '/label', - 'input' => array('name' => 'Model[user_id]', 'type' => 'text', 'id' => 'ModelUserId'), + 'div' => array('class' => 'input email'), + 'label' => array('for' => 'custom'), + 'Email', + '/label', + array('input' => array( + 'type' => 'email', 'name' => 'Contact[email]', + 'id' => 'custom', 'maxlength' => 255 + )), '/div' ); $this->assertTags($result, $expected); - //Check that magic types still work for plural/singular vars - $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad'); - $result = $this->Form->input('Model.type'); + $result = $this->Form->input('Contact.email', array('div' => array('class' => false))); $expected = array( - 'div' => array('class' => 'input select'), - 'label' => array('for' => 'ModelType'), 'Type', '/label', - 'select' => array('name' => 'Model[type]', 'id' => 'ModelType'), - array('option' => array('value' => 'value')), 'good', '/option', - array('option' => array('value' => 'other')), 'bad', '/option', - '/select', + ' array('for' => 'ContactEmail'), + 'Email', + '/label', + array('input' => array( + 'type' => 'email', 'name' => 'Contact[email]', + 'id' => 'ContactEmail', 'maxlength' => 255 + )), '/div' ); $this->assertTags($result, $expected); - } -/** - * Test that inferred types do not override developer input - * - * @return void - */ - public function testInputMagicTypeDoesNotOverride() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad'); - $result = $this->Form->input('Model.user', array('type' => 'checkbox')); + $result = $this->Form->hidden('Contact.idontexist'); + $expected = array('input' => array( + 'type' => 'hidden', 'name' => 'Contact[idontexist]', + 'id' => 'ContactIdontexist' + )); + $this->assertTags($result, $expected); + + $result = $this->Form->input('Contact.email', array('type' => 'text')); $expected = array( - 'div' => array('class' => 'input checkbox'), - array('input' => array( - 'type' => 'hidden', - 'name' => 'data[Model][user]', - 'id' => 'ModelUser_', - 'value' => 0, - )), + 'div' => array('class' => 'input text'), + 'label' => array('for' => 'ContactEmail'), + 'Email', + '/label', array('input' => array( - 'name' => 'data[Model][user]', - 'type' => 'checkbox', - 'id' => 'ModelUser', - 'value' => 1 + 'type' => 'text', 'name' => 'Contact[email]', + 'id' => 'ContactEmail' )), - 'label' => array('for' => 'ModelUser'), 'User', '/label', '/div' ); $this->assertTags($result, $expected); - } -/** - * Test that magic input() selects are created for type=number - * - * @return void - */ - public function testInputMagicSelectForTypeNumber() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot'); - $this->Form->request->data = array('ValidateUser' => array('balance' => 1)); - $result = $this->Form->input('ValidateUser.balance'); + $result = $this->Form->input('Contact.5.email', array('type' => 'text')); $expected = array( - 'div' => array('class' => 'input select'), - 'label' => array('for' => 'ValidateUserBalance'), - 'Balance', + 'div' => array('class' => 'input text'), + 'label' => array('for' => 'Contact5Email'), + 'Email', '/label', - 'select' => array('name' => 'ValidateUser[balance]', 'id' => 'ValidateUserBalance'), - array('option' => array('value' => '0')), - 'nothing', - '/option', - array('option' => array('value' => '1', 'selected' => 'selected')), - 'some', - '/option', - array('option' => array('value' => '100')), - 'a lot', - '/option', - '/select', + array('input' => array( + 'type' => 'text', 'name' => 'Contact[5][email]', + 'id' => 'Contact5Email' + )), '/div' ); $this->assertTags($result, $expected); - } - -/** - * Test that magic input() selects can easily be converted into radio types without error. - * - * @return void - */ - public function testInputMagicSelectChangeToRadio() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad'); - $result = $this->Form->input('Model.user_id', array('type' => 'radio')); - $this->assertRegExp('/input type="radio"/', $result); - } -/** - * fields with the same name as the model should work. - * - * @return void - */ - public function testInputWithMatchingFieldAndModelName() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->create('User'); - $this->Form->fieldset = array( - 'User' => array( - 'fields' => array( - 'User' => array('type' => 'text') - ), - 'validates' => array(), - 'key' => 'id' - ) - ); - $this->Form->request->data['User']['User'] = 'ABC, Inc.'; - $result = $this->Form->input('User', array('type' => 'text')); + $result = $this->Form->input('Contact.password'); $expected = array( - 'div' => array('class' => 'input text'), - 'label' => array('for' => 'UserUser'), 'User', '/label', - 'input' => array('name' => 'User[User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'), + 'div' => array('class' => 'input password'), + 'label' => array('for' => 'ContactPassword'), + 'Password', + '/label', + array('input' => array( + 'type' => 'password', 'name' => 'Contact[password]', + 'id' => 'ContactPassword' + )), '/div' ); $this->assertTags($result, $expected); - } -/** - * testFormInputs method - * - * test correct results from form::inputs(). - * - * @return void - */ - public function testFormInputs() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->create('Cake\Test\TestCase\View\Helper\Contact'); - $result = $this->Form->inputs('The Legend'); + $result = $this->Form->input('Contact.email', array( + 'type' => 'file', 'class' => 'textbox' + )); $expected = array( - ' array('class' => 'input file'), + 'label' => array('for' => 'ContactEmail'), + 'Email', + '/label', + array('input' => array( + 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox', + 'id' => 'ContactEmail' + )), + '/div' ); $this->assertTags($result, $expected); - $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff')); + $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars')); + $result = $this->Form->input('Contact.phone'); $expected = array( - 'fieldset' => array('class' => 'classy-stuff'), - ' array('class' => 'input tel'), + 'label' => array('for' => 'ContactPhone'), + 'Phone', + '/label', + array('input' => array( + 'type' => 'tel', 'name' => 'Contact[phone]', + 'value' => 'Hello & World > weird chars', + 'id' => 'ContactPhone', 'maxlength' => 255 + )), + '/div' ); $this->assertTags($result, $expected); - $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff')); + $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value'; + $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId')); + $expected = array( + 'div' => array('class' => 'input text'), + 'label' => array('for' => 'myId'), + 'Field', + '/label', + 'input' => array( + 'type' => 'text', 'name' => 'Model[0][OtherModel][field]', + 'value' => 'My value', 'id' => 'myId' + ), + '/div' + ); $this->assertTags($result, $expected); - $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff')); - $this->assertTags($result, $expected); + unset($this->Form->request->data); - $this->Form->create('Contact'); - $this->Form->request['prefix'] = 'admin'; - $this->Form->request['action'] = 'admin_edit'; - $result = $this->Form->inputs(); + $Contact->validationErrors['field'] = array('Badness!'); + $result = $this->Form->input('Contact.field'); $expected = array( - ' array('class' => 'input text error'), + 'label' => array('for' => 'ContactField'), + 'Field', + '/label', + 'input' => array( + 'type' => 'text', 'name' => 'Contact[field]', + 'id' => 'ContactField', 'class' => 'form-error' + ), + array('div' => array('class' => 'error-message')), + 'Badness!', + '/div', + '/div' ); $this->assertTags($result, $expected); - $this->Form->create('Contact'); - $result = $this->Form->inputs(false); + $result = $this->Form->input('Contact.field', array( + 'div' => false, 'error' => array('attributes' => array('wrap' => 'span')) + )); $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'), - array('div' => array('class' => 'input text')), - '*/div', - array('div' => array('class' => 'input email')), - '*/div', - array('div' => array('class' => 'input tel')), - '*/div', - array('div' => array('class' => 'input password')), - '*/div', - array('div' => array('class' => 'input date')), - '*/div', - array('div' => array('class' => 'input date')), - '*/div', - array('div' => array('class' => 'input datetime')), - '*/div', - array('div' => array('class' => 'input number')), - '*/div', - array('div' => array('class' => 'input select')), - '*/div', + 'label' => array('for' => 'ContactField'), + 'Field', + '/label', + 'input' => array( + 'type' => 'text', 'name' => 'Contact[field]', + 'id' => 'ContactField', 'class' => 'form-error' + ), + array('span' => array('class' => 'error-message')), + 'Badness!', + '/span' ); $this->assertTags($result, $expected); - $this->Form->create('Contact'); - $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false)); + $result = $this->Form->input('Contact.field', array( + 'type' => 'text', 'error' => array('attributes' => array('class' => 'error')) + )); $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'), - array('div' => array('class' => 'input text')), - '*/div', - array('div' => array('class' => 'input email')), - '*/div', - array('div' => array('class' => 'input tel')), - '*/div', - array('div' => array('class' => 'input password')), - '*/div', - array('div' => array('class' => 'input date')), - '*/div', - array('div' => array('class' => 'input date')), - '*/div', - array('div' => array('class' => 'input datetime')), - '*/div', - array('div' => array('class' => 'input number')), - '*/div', - array('div' => array('class' => 'input select')), - '*/div', + 'div' => array('class' => 'input text error'), + 'label' => array('for' => 'ContactField'), + 'Field', + '/label', + 'input' => array( + 'type' => 'text', 'name' => 'Contact[field]', + 'id' => 'ContactField', 'class' => 'form-error' + ), + array('div' => array('class' => 'error')), + 'Badness!', + '/div' ); $this->assertTags($result, $expected); - $this->Form->create('Contact'); - $result = $this->Form->inputs(null, null, array('fieldset' => false)); - $this->assertTags($result, $expected); - - $this->Form->create('Contact'); - $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false)); + $result = $this->Form->input('Contact.field', array( + 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false)) + )); $expected = array( - 'fieldset' => array(), - 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'), - array('div' => array('class' => 'input text')), - '*/div', - array('div' => array('class' => 'input email')), - '*/div', - array('div' => array('class' => 'input tel')), - '*/div', - array('div' => array('class' => 'input password')), - '*/div', - array('div' => array('class' => 'input date')), - '*/div', - array('div' => array('class' => 'input date')), - '*/div', - array('div' => array('class' => 'input datetime')), - '*/div', - array('div' => array('class' => 'input number')), - '*/div', - array('div' => array('class' => 'input select')), - '*/div', - '/fieldset' + 'span' => array('class' => 'input text error'), + 'label' => array('for' => 'ContactField'), + 'Field', + '/label', + 'input' => array( + 'type' => 'text', 'name' => 'Contact[field]', + 'id' => 'ContactField', 'class' => 'form-error' + ), + 'Badness!', + '/span' ); $this->assertTags($result, $expected); - $this->Form->create('Contact'); - $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello')); + $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy')); $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'), - array('div' => array('class' => 'input text')), - '*/div', - array('div' => array('class' => 'input email')), - '*/div', - array('div' => array('class' => 'input tel')), - '*/div', - array('div' => array('class' => 'input password')), - '*/div', - array('div' => array('class' => 'input date')), - '*/div', - array('div' => array('class' => 'input date')), - '*/div', - array('div' => array('class' => 'input datetime')), - '*/div', - array('div' => array('class' => 'input number')), - '*/div', - array('div' => array('class' => 'input select')), - '*/div', + 'div' => array('class' => 'input text error'), + 'label' => array('for' => 'ContactField'), + 'Field', + '/label', + 'input' => array( + 'type' => 'text', 'name' => 'Contact[field]', + 'id' => 'ContactField', 'class' => 'form-error' + ), + 'A message to you, Rudy', + array('div' => array('class' => 'error-message')), + 'Badness!', + '/div', + '/div' ); $this->assertTags($result, $expected); - $this->Form->create('Contact'); - $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello')); - $this->assertTags($result, $expected); - - $this->Form->create('Contact'); - $result = $this->Form->inputs('Hello'); + $this->Form->setEntity(null); + $this->Form->setEntity('Contact.field'); + $result = $this->Form->input('Contact.field', array( + 'after' => 'A message to you, Rudy', 'error' => false + )); $expected = array( - 'fieldset' => array(), - 'legend' => array(), - 'Hello', - '/legend', - 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'), - array('div' => array('class' => 'input text')), - '*/div', - array('div' => array('class' => 'input email')), - '*/div', - array('div' => array('class' => 'input tel')), - '*/div', - array('div' => array('class' => 'input password')), - '*/div', - array('div' => array('class' => 'input date')), - '*/div', - array('div' => array('class' => 'input date')), - '*/div', - array('div' => array('class' => 'input datetime')), - '*/div', - array('div' => array('class' => 'input number')), - '*/div', - array('div' => array('class' => 'input select')), - '*/div', - '/fieldset' + 'div' => array('class' => 'input text'), + 'label' => array('for' => 'ContactField'), + 'Field', + '/label', + 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'), + 'A message to you, Rudy', + '/div' ); $this->assertTags($result, $expected); - $this->Form->create('Contact'); - $result = $this->Form->inputs(array('legend' => 'Hello')); + $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy')); $expected = array( - 'fieldset' => array(), - 'legend' => array(), - 'Hello', - '/legend', - 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'), - array('div' => array('class' => 'input text')), - '*/div', - array('div' => array('class' => 'input email')), - '*/div', - array('div' => array('class' => 'input tel')), - '*/div', - array('div' => array('class' => 'input password')), - '*/div', - array('div' => array('class' => 'input date')), - '*/div', - array('div' => array('class' => 'input date')), - '*/div', - array('div' => array('class' => 'input datetime')), - '*/div', - array('div' => array('class' => 'input number')), - '*/div', - array('div' => array('class' => 'input select')), - '*/div', - '/fieldset' + 'div' => array('class' => 'input text'), + 'label' => array('for' => 'ObjectField'), + 'Field', + '/label', + 'input' => array('type' => 'text', 'name' => 'Object[field]', 'id' => 'ObjectField'), + 'A message to you, Rudy', + '/div' ); $this->assertTags($result, $expected); - $this->Form->create('Contact'); - $result = $this->Form->inputs(null, null, array('legend' => 'Hello')); + $Contact->validationErrors['field'] = array('minLength'); + $result = $this->Form->input('Contact.field', array( + 'error' => array( + 'minLength' => 'Le login doit contenir au moins 2 caractères', + 'maxLength' => 'login too large' + ) + )); + $expected = array( + 'div' => array('class' => 'input text error'), + 'label' => array('for' => 'ContactField'), + 'Field', + '/label', + 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'), + array('div' => array('class' => 'error-message')), + 'Le login doit contenir au moins 2 caractères', + '/div', + '/div' + ); $this->assertTags($result, $expected); - $this->Form->end(); - $this->Form->create(false); + $Contact->validationErrors['field'] = array('maxLength'); + $result = $this->Form->input('Contact.field', array( + 'error' => array( + 'attributes' => array('wrap' => 'span', 'rel' => 'fake'), + 'minLength' => 'Le login doit contenir au moins 2 caractères', + 'maxLength' => 'login too large', + ) + )); $expected = array( - 'fieldset' => array(), - array('div' => array('class' => 'input text')), - 'label' => array('for' => 'foo'), - 'Foo', + 'div' => array('class' => 'input text error'), + 'label' => array('for' => 'ContactField'), + 'Field', '/label', - 'input' => array('type' => 'text', 'name' => 'foo', 'id' => 'foo'), - '*/div', - '/fieldset' - ); - $result = $this->Form->inputs( - array('foo' => array('type' => 'text')), - array(), - array('legend' => false) + 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'), + array('span' => array('class' => 'error-message', 'rel' => 'fake')), + 'login too large', + '/span', + '/div' ); $this->assertTags($result, $expected); } /** - * Tests inputs() works with plugin models + * Test that inputs with 0 can be created. * * @return void */ - public function testInputsPluginModel() { + public function testInputZero() { $this->markTestIncomplete('Need to revisit once models work again.'); - $this->loadFixtures('Post'); - Plugin::load('TestPlugin'); - $this->Form->request['models'] = array( - 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost') + $this->Form->create('User'); + $result = $this->Form->input('0'); + $expected = array( + 'div' => array('class' => 'input text'), + 'label' => array('for' => 'User0'), '/label', + 'input' => array('type' => 'text', 'name' => 'User[0]', 'id' => 'User0'), + '/div' ); - $this->Form->create('TestPlugin.TestPluginPost'); - $result = $this->Form->inputs(); - - $this->assertContains('TestPluginPost[id]', $result); - $this->assertContains('TestPluginPost[author_id]', $result); - $this->assertContains('TestPluginPost[title]', $result); - $this->assertEquals('TestPluginPost', $this->Form->model()); + $this->assertTags($result, $expected); } /** - * testSelectAsCheckbox method - * - * test multi-select widget with checkbox formatting. + * test input() with checkbox creation * * @return void */ - public function testSelectAsCheckbox() { + public function testInputCheckbox() { $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1))); + $result = $this->Form->input('User.active', array('label' => false, 'checked' => true)); $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'), - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')), - array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')), - 'first', - '/label', - '/div', - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')), - array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')), - 'second', - '/label', - '/div', - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')), - array('label' => array('for' => 'ModelMultiField2')), - 'third', - '/label', - '/div', + 'div' => array('class' => 'input checkbox'), + 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'), + array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')), + '/div' ); $this->assertTags($result, $expected); - $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox')); + $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1)); $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'), - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')), - array('label' => array('for' => 'ModelMultiField12')), - 'half', + 'div' => array('class' => 'input checkbox'), + 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'), + array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')), + '/div' + ); + $this->assertTags($result, $expected); + + $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1')); + $expected = array( + 'div' => array('class' => 'input checkbox'), + 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'), + array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')), + '/div' + ); + $this->assertTags($result, $expected); + + $result = $this->Form->input('User.disabled', array( + 'label' => 'Disabled', + 'type' => 'checkbox', + 'data-foo' => 'disabled' + )); + $expected = array( + 'div' => array('class' => 'input checkbox'), + 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'), + array('input' => array( + 'type' => 'checkbox', + 'name' => 'data[User][disabled]', + 'value' => '1', + 'id' => 'UserDisabled', + 'data-foo' => 'disabled' + )), + 'label' => array('for' => 'UserDisabled'), + 'Disabled', '/label', - '/div', + '/div' ); $this->assertTags($result, $expected); } /** - * testLabel method - * - * test label generation. + * test form->input() with time types. * - * @return void */ - public function testLabel() { + public function testInputTime() { $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->text('Person.name'); - $result = $this->Form->label(); - $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label')); + extract($this->dateRegex); + $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24)); + $result = explode(':', $result); + $this->assertRegExp('/option value="23"/', $result[0]); + $this->assertNotRegExp('/option value="24"/', $result[0]); - $this->Form->text('Person.name'); - $result = $this->Form->label(); - $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label')); + $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24)); + $result = explode(':', $result); + $this->assertRegExp('/option value="23"/', $result[0]); + $this->assertNotRegExp('/option value="24"/', $result[0]); - $result = $this->Form->label('Person.first_name'); - $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label')); + $result = $this->Form->input('Model.field', array( + 'type' => 'time', 'timeFormat' => 24, 'interval' => 15 + )); + $result = explode(':', $result); + $this->assertNotRegExp('##', $result[1]); + $this->assertNotRegExp('##', $result[1]); + $this->assertRegExp('##', $result[1]); - $result = $this->Form->label('Person.first_name', 'Your first name'); - $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label')); + $result = $this->Form->input('Model.field', array( + 'type' => 'time', 'timeFormat' => 12, 'interval' => 15 + )); + $result = explode(':', $result); + $this->assertNotRegExp('##', $result[1]); + $this->assertNotRegExp('##', $result[1]); + $this->assertRegExp('##', $result[1]); - $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class')); - $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label')); + $result = $this->Form->input('prueba', array( + 'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008, + 'maxYear' => date('Y') + 1, 'interval' => 15 + )); + $result = explode(':', $result); + $this->assertNotRegExp('##', $result[1]); + $this->assertNotRegExp('##', $result[1]); + $this->assertRegExp('##', $result[1]); + $this->assertRegExp('##', $result[1]); - $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID')); - $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label')); + $result = $this->Form->input('Random.start_time', array( + 'type' => 'time', + 'selected' => '18:15' + )); + $this->assertContains('', $result); + $this->assertContains('', $result); + $this->assertContains('', $result); - $result = $this->Form->label('Person.first_name', ''); - $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label')); + $result = $this->Form->input('published', array('type' => 'time')); + $now = strtotime('now'); + $this->assertContains('', $result); - $result = $this->Form->label('Person.2.name', ''); - $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label')); + $now = strtotime('2013-03-09 00:42:21'); + $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now)); + $this->assertContains('', $result); + $this->assertContains('', $result); } /** - * testTextbox method - * - * test textbox element generation + * Test interval + selected near the hour roll over. * * @return void */ - public function testTextbox() { + public function testTimeSelectedWithInterval() { $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->text('Model.field'); - $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'ModelField'))); - - $result = $this->Form->text('Model.field', array('type' => 'password')); - $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Model[field]', 'id' => 'ModelField'))); + $result = $this->Form->input('Model.start_time', array( + 'type' => 'time', + 'interval' => 15, + 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm') + )); + $this->assertContains('', $result); + $this->assertContains('', $result); + $this->assertContains('', $result); - $result = $this->Form->text('Model.field', array('id' => 'theID')); - $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'theID'))); + $result = $this->Form->input('Model.start_time', array( + 'type' => 'time', + 'interval' => 15, + 'selected' => '2012-10-23 15:57:00' + )); + $this->assertContains('', $result); + $this->assertContains('', $result); + $this->assertContains('', $result); - $this->Form->request->data['Model']['text'] = 'test HTML values'; - $result = $this->Form->text('Model.text'); - $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[text]', 'value' => 'test <strong>HTML</strong> values', 'id' => 'ModelText'))); + $result = $this->Form->input('Model.start_time', array( + 'timeFormat' => 24, + 'type' => 'time', + 'interval' => 15, + 'selected' => '15:57' + )); + $this->assertContains('', $result); + $this->assertContains('', $result); - $Contact->validationErrors['text'] = array(true); - $this->Form->request->data['Contact']['text'] = 'test'; - $result = $this->Form->text('Contact.text', array('id' => 'theID')); - $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Contact[text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error'))); + $result = $this->Form->input('Model.start_time', array( + 'timeFormat' => 24, + 'type' => 'time', + 'interval' => 15, + 'selected' => '23:57' + )); + $this->assertContains('', $result); + $this->assertContains('', $result); - $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value'; - $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId')); - $expected = array( - 'input' => array('type' => 'text', 'name' => 'Model[0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId') - ); - $this->assertTags($result, $expected); + $result = $this->Form->input('Model.created', array( + 'timeFormat' => 24, + 'type' => 'datetime', + 'interval' => 15, + 'selected' => '2012-09-30 23:56' + )); + $this->assertContains('', $result); + $this->assertContains('', $result); + $this->assertContains('', $result); + $this->assertContains('', $result); + $this->assertContains('', $result); } /** - * testDefaultValue method - * - * Test default value setting + * Test time with selected values around 12:xx:xx * * @return void */ - public function testDefaultValue() { + public function testTimeSelectedWithIntervalTwelve() { $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->request->data['Model']['field'] = 'test'; - $result = $this->Form->text('Model.field', array('default' => 'default value')); - $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'value' => 'test', 'id' => 'ModelField'))); + $result = $this->Form->input('Model.start_time', array( + 'type' => 'time', + 'timeFormat' => 12, + 'interval' => 15, + 'selected' => '00:00:00' + )); + $this->assertContains('', $result); + $this->assertContains('', $result); + $this->assertContains('', $result); - unset($this->Form->request->data['Model']['field']); - $result = $this->Form->text('Model.field', array('default' => 'default value')); - $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'value' => 'default value', 'id' => 'ModelField'))); + $result = $this->Form->input('Model.start_time', array( + 'type' => 'time', + 'timeFormat' => 12, + 'interval' => 15, + 'selected' => '12:00:00' + )); + $this->assertContains('', $result); + $this->assertContains('', $result); + $this->assertContains('', $result); + + $result = $this->Form->input('Model.start_time', array( + 'type' => 'time', + 'timeFormat' => 12, + 'interval' => 15, + 'selected' => '12:15:00' + )); + $this->assertContains('', $result); + $this->assertContains('', $result); + $this->assertContains('', $result); } /** - * testCheckboxDefaultValue method - * - * Test default value setting on checkbox() method + * Test interval & timeFormat = 12 * * @return void */ - public function testCheckboxDefaultValue() { + public function testInputTimeWithIntervalAnd12HourFormat() { $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->request->data['Model']['field'] = false; - $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false)); - $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField'))); - - unset($this->Form->request->data['Model']['field']); - $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false)); - $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))); + $result = $this->Form->input('Model.start_time', array( + 'type' => 'time', + 'timeFormat' => 12, + 'interval' => 5, + 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm') + )); + $this->assertContains('', $result); + $this->assertContains('', $result); + $this->assertContains('', $result); - $this->Form->request->data['Model']['field'] = true; - $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false)); - $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))); + $result = $this->Form->input('Model.start_time', array( + 'type' => 'time', + 'timeFormat' => '12', + 'interval' => 5, + 'selected' => '2013-04-19 16:30:00' + )); + $this->assertContains('', $result); + $this->assertContains('', $result); + $this->assertContains('', $result); - unset($this->Form->request->data['Model']['field']); - $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false)); - $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField'))); + $result = $this->Form->input('Model.start_time', array( + 'type' => 'time', + 'timeFormat' => '12', + 'interval' => 10, + 'selected' => '2013-05-19 00:33:00' + )); + $this->assertContains('', $result); + $this->assertContains('', $result); + $this->assertContains('', $result); + + $result = $this->Form->input('Model.start_time', array( + 'type' => 'time', + 'timeFormat' => '12', + 'interval' => 10, + 'selected' => '2013-05-19 13:33:00' + )); + $this->assertContains('', $result); + $this->assertContains('', $result); + $this->assertContains('', $result); + + $result = $this->Form->input('Model.start_time', array( + 'type' => 'time', + 'timeFormat' => '12', + 'interval' => 10, + 'selected' => '2013-05-19 01:33:00' + )); + $this->assertContains('', $result); + $this->assertContains('', $result); + $this->assertContains('', $result); } /** - * testError method - * - * Test field error generation + * test form->input() with datetime, date and time types * * @return void */ - public function testError() { + public function testInputDatetime() { $this->markTestIncomplete('Need to revisit once models work again.'); - $Contact->validationErrors['field'] = array(1); - $result = $this->Form->error('Contact.field'); - $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div')); - - $result = $this->Form->error('Contact.field', null, array('wrap' => false)); - $this->assertEquals('Error in field Field', $result); - - $Contact->validationErrors['field'] = array("This field contains invalid input"); - $result = $this->Form->error('Contact.field', null, array('wrap' => false)); - $this->assertEquals('This field contains invalid input', $result); - - $Contact->validationErrors['field'] = array("This field contains invalid input"); - $result = $this->Form->error('Contact.field', null, array('wrap' => 'span')); - $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span')); - - $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span')); - $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span')); - - $result = $this->Form->error('Contact.field', "Badness!", array('wrap' => false)); - $this->assertEquals('<strong>Badness!</strong>', $result); - - $result = $this->Form->error('Contact.field', "Badness!", array('wrap' => false, 'escape' => true)); - $this->assertEquals('<strong>Badness!</strong>', $result); + extract($this->dateRegex); + $result = $this->Form->input('prueba', array( + 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008, + 'maxYear' => date('Y') + 1, 'interval' => 15 + )); + $result = explode(':', $result); + $this->assertNotRegExp('##', $result[1]); + $this->assertNotRegExp('##', $result[1]); + $this->assertRegExp('##', $result[1]); + $this->assertRegExp('##', $result[1]); - $result = $this->Form->error('Contact.field', "Badness!", array('wrap' => false, 'escape' => false)); - $this->assertEquals('Badness!', $result); + //related to ticket #5013 + $result = $this->Form->input('Contact.date', array( + 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}' + )); + $this->assertRegExp('/class="customClass"/', $result); + $this->assertRegExp('/onChange="function\(\)\{\}"/', $result); - $Contact->validationErrors['field'] = array("email"); - $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!')); - $expected = array( - 'div' => array('class' => 'field-error'), - 'No good!', - '/div' - ); - $this->assertTags($result, $expected); + $result = $this->Form->input('Contact.date', array( + 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}' + )); + $this->assertRegExp('/id="customIdDay"/', $result); + $this->assertRegExp('/id="customIdMonth"/', $result); + $this->assertRegExp('/onChange="function\(\)\{\}"/', $result); - $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else'); - $result = $this->Form->error('Contact.field', array( - 'notEmpty' => 'Cannot be empty', - 'email' => 'No good!' + $result = $this->Form->input('Model.field', array( + 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID' )); - $expected = array( - 'div' => array('class' => 'error-message'), - 'ul' => array(), - 'assertTags($result, $expected); + $this->assertRegExp('/id="customIDDay"/', $result); + $this->assertRegExp('/id="customIDHour"/', $result); + $result = explode('assertRegExp('/option value="23"/', $result[0]); + $this->assertNotRegExp('/option value="24"/', $result[0]); - // Testing error messages list options - $Contact->validationErrors['field'] = array('notEmpty', 'email'); + $result = $this->Form->input('Model.field', array( + 'type' => 'datetime', 'timeFormat' => 12 + )); + $result = explode('assertRegExp('/option value="12"/', $result[0]); + $this->assertNotRegExp('/option value="13"/', $result[0]); - $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol')); + $this->Form->request->data = array('Contact' => array('created' => null)); + $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown')); $expected = array( - 'div' => array('class' => 'error-message'), - 'ol' => array(), - ' array('class' => 'input date'), + 'label' => array('for' => 'ContactCreatedMonth'), + 'Created', + '/label', + array('select' => array('name' => 'Contact[created][month]', 'id' => 'ContactCreatedMonth')), + array('option' => array('value' => '')), 'Date Unknown', '/option', + $monthsRegex, + '/select', '-', + array('select' => array('name' => 'Contact[created][day]', 'id' => 'ContactCreatedDay')), + array('option' => array('value' => '')), 'Date Unknown', '/option', + $daysRegex, + '/select', '-', + array('select' => array('name' => 'Contact[created][year]', 'id' => 'ContactCreatedYear')), + array('option' => array('value' => '')), 'Date Unknown', '/option', + $yearsRegex, + '/select', '/div' ); $this->assertTags($result, $expected); - $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol'))); - $expected = array( - 'div' => array('class' => 'error-message'), - 'ol' => array(), - 'assertTags($result, $expected); + $this->Form->request->data = array('Contact' => array('created' => null)); + $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE')); + $this->assertRegExp('/for\="ContactCreatedHour"/', $result); - $result = $this->Form->error('Contact.field', null, array( - 'listOptions' => array( - 'class' => 'ul-class', - 'itemOptions' => array( - 'class' => 'li-class' - ) - ) + $this->Form->request->data = array('Contact' => array('created' => null)); + $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE')); + $this->assertRegExp('/for\="ContactCreatedMonth"/', $result); + + $result = $this->Form->input('Contact.created', array( + 'type' => 'date', + 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'), + 'timeFormat' => 'NONE' )); - $expected = array( - 'div' => array('class' => 'error-message'), - 'ul' => array('class' => 'ul-class'), - array('li' => array('class' => 'li-class')), 'notEmpty', '/li', - array('li' => array('class' => 'li-class')), 'email', '/li', - '/ul', - '/div' - ); - $this->assertTags($result, $expected); + $this->assertRegExp('/for\="created-month"/', $result); } /** - * test error options when using form->input(); + * Test generating checkboxes in a loop. * * @return void */ - public function testInputErrorEscape() { + public function testInputCheckboxesInLoop() { $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->create('ValidateProfile'); - $ValidateProfile->validationErrors['city'] = array('required
'); - $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true)))); - $this->assertRegExp('/required<br>/', $result); - - $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false)))); - $this->assertRegExp('/required
/', $result); + for ($i = 1; $i < 5; $i++) { + $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i)); + $expected = array( + 'div' => array('class' => 'input checkbox'), + 'input' => array('type' => 'hidden', 'name' => "Contact[{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"), + array('input' => array('type' => 'checkbox', 'name' => "Contact[{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")), + 'label' => array('for' => "Contact{$i}Email"), + 'Email', + '/label', + '/div' + ); + $this->assertTags($result, $expected); + } } /** - * testPassword method - * - * Test password element generation + * Test generating checkboxes with disabled elements. * * @return void */ - public function testPassword() { + public function testInputCheckboxWithDisabledElements() { $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->password('Contact.field'); - $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[field]', 'id' => 'ContactField'))); - - $Contact->validationErrors['passwd'] = 1; - $this->Form->request->data['Contact']['passwd'] = 'test'; - $result = $this->Form->password('Contact.passwd', array('id' => 'theID')); - $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error'))); - } + $options = array(1 => 'One', 2 => 'Two', '3' => 'Three'); + $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options)); -/** - * testRadio method - * - * Test radio element set generation - * - * @return void - */ - public function testRadio() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->radio('Model.field', array('option A')); $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'), - array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')), - 'label' => array('for' => 'ModelField0'), - 'option A', - '/label' + array('div' => array('class' => 'input select')), + array('label' => array('for' => "ContactMultiple")), + 'Multiple', + '/label', + array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")), + array('div' => array('class' => 'checkbox')), + array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")), + array('label' => array('for' => "ContactMultiple1")), + 'One', + '/label', + '/div', + array('div' => array('class' => 'checkbox')), + array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")), + array('label' => array('for' => "ContactMultiple2")), + 'Two', + '/label', + '/div', + array('div' => array('class' => 'checkbox')), + array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")), + array('label' => array('for' => "ContactMultiple3")), + 'Three', + '/label', + '/div', + '/div' ); $this->assertTags($result, $expected); @@ -3712,2319 +3233,2103 @@ public function testRadio() { ); $this->assertTags($result, $expected); - $result = $this->Form->radio('Model.field', array('option A', 'option B')); + $disabled = array('2', 3); + $expected = array( - 'fieldset' => array(), - 'legend' => array(), - 'Field', - '/legend', - 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'), - array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')), - array('label' => array('for' => 'ModelField0')), - 'option A', + array('div' => array('class' => 'input select')), + array('label' => array('for' => "ContactMultiple")), + 'Multiple', '/label', - array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')), - array('label' => array('for' => 'ModelField1')), - 'option B', + array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")), + array('div' => array('class' => 'checkbox')), + array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'id' => "ContactMultiple1")), + array('label' => array('for' => "ContactMultiple1")), + 'One', '/label', - '/fieldset' + '/div', + array('div' => array('class' => 'checkbox')), + array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")), + array('label' => array('for' => "ContactMultiple2")), + 'Two', + '/label', + '/div', + array('div' => array('class' => 'checkbox')), + array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")), + array('label' => array('for' => "ContactMultiple3")), + 'Three', + '/label', + '/div', + '/div' ); + $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options)); $this->assertTags($result, $expected); - $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '
')); + // make sure 50 does only disable 50, and not 50f5c0cf + $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy'); + $disabled = array(50); + $expected = array( - 'fieldset' => array(), - 'legend' => array(), - 'Field', - '/legend', - 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'), - array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')), - array('label' => array('for' => 'ModelField0')), - 'option A', + array('div' => array('class' => 'input select')), + array('label' => array('for' => "ContactMultiple")), + 'Multiple', '/label', - 'br' => array(), - array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')), - array('label' => array('for' => 'ModelField1')), - 'option B', + array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")), + array('div' => array('class' => 'checkbox')), + array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")), + array('label' => array('for' => "ContactMultiple50")), + 'Fifty', '/label', - '/fieldset' + '/div', + array('div' => array('class' => 'checkbox')), + array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")), + array('label' => array('for' => "ContactMultiple50f5c0cf")), + 'Stringy', + '/label', + '/div', + '/div' ); + $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options)); $this->assertTags($result, $expected); + } - $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female')); +/** + * test input name with leading integer, ensure attributes are generated correctly. + * + * @return void + */ + public function testInputWithLeadingInteger() { + $this->markTestIncomplete('Need to revisit once models work again.'); + $result = $this->Form->text('0.Node.title'); $expected = array( - 'fieldset' => array(), - 'legend' => array(), - 'Gender', - '/legend', - 'input' => array('type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'id' => 'EmployeeGender_'), - array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')), - array('label' => array('for' => 'EmployeeGenderMale')), - 'Male', - '/label', - array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')), - array('label' => array('for' => 'EmployeeGenderFemale')), - 'Female', - '/label', - '/fieldset', + 'input' => array('name' => '0[Node][title]', 'id' => '0NodeTitle', 'type' => 'text') ); $this->assertTags($result, $expected); + } - $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female')); +/** + * test form->input() with select type inputs. + * + * @return void + */ + public function testInputSelectType() { + $this->markTestIncomplete('Need to revisit once models work again.'); + $result = $this->Form->input('email', array( + 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true) + ); $expected = array( - 'fieldset' => array(), - 'legend' => array(), - 'Gender', - '/legend', - 'input' => array('type' => 'hidden', 'name' => 'Officer[gender]', 'value' => '', 'id' => 'OfficerGender_'), - array('input' => array('type' => 'radio', 'name' => 'Officer[gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')), - array('label' => array('for' => 'OfficerGenderMale')), - 'Male', - '/label', - array('input' => array('type' => 'radio', 'name' => 'Officer[gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')), - array('label' => array('for' => 'OfficerGenderFemale')), - 'Female', + 'div' => array('class' => 'input select'), + 'label' => array('for' => 'email'), + 'Email', '/label', - '/fieldset', + array('select' => array('name' => 'email', 'id' => 'email')), + array('option' => array('value' => '')), + '/option', + array('option' => array('value' => 'è')), + 'Firést', + '/option', + array('option' => array('value' => 'é')), + 'Secoènd', + '/option', + '/select', + '/div' ); $this->assertTags($result, $expected); - $result = $this->Form->radio('Contact.1.imrequired', array('option A')); + $result = $this->Form->input('email', array( + 'options' => array('First', 'Second'), 'empty' => true) + ); $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'Contact[1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'), - array('input' => array('type' => 'radio', 'name' => 'Contact[1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0', 'required' => 'required')), - 'label' => array('for' => 'Contact1Imrequired0'), - 'option A', - '/label' + 'div' => array('class' => 'input select'), + 'label' => array('for' => 'email'), + 'Email', + '/label', + array('select' => array('name' => 'email', 'id' => 'email')), + array('option' => array('value' => '')), + '/option', + array('option' => array('value' => '0')), + 'First', + '/option', + array('option' => array('value' => '1')), + 'Second', + '/option', + '/select', + '/div' ); $this->assertTags($result, $expected); - $result = $this->Form->radio('Model.1.field', array('option A')); + $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad'); + $this->Form->request->data = array('Model' => array('user_id' => 'value')); + + $result = $this->Form->input('Model.user_id', array('empty' => true)); $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'Model[1][field]', 'value' => '', 'id' => 'Model1Field_'), - array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0')), - 'label' => array('for' => 'Model1Field0'), - 'option A', - '/label' + 'div' => array('class' => 'input select'), + 'label' => array('for' => 'ModelUserId'), + 'User', + '/label', + 'select' => array('name' => 'Model[user_id]', 'id' => 'ModelUserId'), + array('option' => array('value' => '')), + '/option', + array('option' => array('value' => 'value', 'selected' => 'selected')), + 'good', + '/option', + array('option' => array('value' => 'other')), + 'bad', + '/option', + '/select', + '/div' ); $this->assertTags($result, $expected); - $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'Model[custom]')); + $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad'); + $this->Form->request->data = array('Thing' => array('user_id' => null)); + $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty')); $expected = array( - 'fieldset' => array(), - 'legend' => array(), - 'Field', - '/legend', - 'input' => array('type' => 'hidden', 'name' => 'Model[custom]', 'value' => '', 'id' => 'ModelField_'), - array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'ModelField0')), - array('label' => array('for' => 'ModelField0')), - 'option A', - '/label', - array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'ModelField1')), - array('label' => array('for' => 'ModelField1')), - 'option B', + 'div' => array('class' => 'input select'), + 'label' => array('for' => 'ThingUserId'), + 'User', '/label', - '/fieldset' + 'select' => array('name' => 'Thing[user_id]', 'id' => 'ThingUserId'), + array('option' => array('value' => '')), + 'Some Empty', + '/option', + array('option' => array('value' => 'value')), + 'good', + '/option', + array('option' => array('value' => 'other')), + 'bad', + '/option', + '/select', + '/div' ); $this->assertTags($result, $expected); - $result = $this->Form->radio( - 'Model.field', - array('a>b' => 'first', 'a 'second', 'a"b' => 'third') - ); + $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad'); + $this->Form->request->data = array('Thing' => array('user_id' => 'value')); + $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty')); $expected = array( - 'fieldset' => array(), - 'legend' => array(), - 'Field', - '/legend', - 'input' => array( - 'type' => 'hidden', 'name' => 'data[Model][field]', - 'id' => 'ModelField_', 'value' => '', - ), - array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', - 'id' => 'ModelFieldAB', 'value' => 'a>b')), - array('label' => array('for' => 'ModelFieldAB')), - 'first', - '/label', - array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', - 'id' => 'ModelFieldAB1', 'value' => 'a<b')), - array('label' => array('for' => 'ModelFieldAB1')), - 'second', + 'div' => array('class' => 'input select'), + 'label' => array('for' => 'ThingUserId'), + 'User', '/label', - array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', - 'id' => 'ModelFieldAB2', 'value' => 'a"b')), - array('label' => array('for' => 'ModelFieldAB2')), - 'third', + 'select' => array('name' => 'Thing[user_id]', 'id' => 'ThingUserId'), + array('option' => array('value' => '')), + 'Some Empty', + '/option', + array('option' => array('value' => 'value', 'selected' => 'selected')), + 'good', + '/option', + array('option' => array('value' => 'other')), + 'bad', + '/option', + '/select', + '/div' + ); + $this->assertTags($result, $expected); + + $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad'); + $this->Form->request->data = array('User' => array('User' => array('value'))); + $result = $this->Form->input('User.User', array('empty' => true)); + $expected = array( + 'div' => array('class' => 'input select'), + 'label' => array('for' => 'UserUser'), + 'User', '/label', - '/fieldset' + 'input' => array('type' => 'hidden', 'name' => 'User[User]', 'value' => '', 'id' => 'UserUser_'), + 'select' => array('name' => 'User[User][]', 'id' => 'UserUser', 'multiple' => 'multiple'), + array('option' => array('value' => '')), + '/option', + array('option' => array('value' => 'value', 'selected' => 'selected')), + 'good', + '/option', + array('option' => array('value' => 'other')), + 'bad', + '/option', + '/select', + '/div' + ); + $this->assertTags($result, $expected); + + $this->Form->data = array(); + $result = $this->Form->input('Publisher.id', array( + 'label' => 'Publisher', + 'type' => 'select', + 'multiple' => 'checkbox', + 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2') + )); + $expected = array( + array('div' => array('class' => 'input select')), + array('label' => array('for' => 'PublisherId')), + 'Publisher', + '/label', + 'input' => array('type' => 'hidden', 'name' => 'Publisher[id]', 'value' => '', 'id' => 'PublisherId'), + array('div' => array('class' => 'checkbox')), + array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')), + array('label' => array('for' => 'PublisherIdValue1')), + 'Label 1', + '/label', + '/div', + array('div' => array('class' => 'checkbox')), + array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')), + array('label' => array('for' => 'PublisherIdValue2')), + 'Label 2', + '/label', + '/div', + '/div' ); $this->assertTags($result, $expected); } /** - * Test radio inputs with between as string or array. Also ensure - * that an array with less between elements works. + * test that input() and a non standard primary key makes a hidden input by default. * * @return void */ - public function testRadioBetween() { + public function testInputWithNonStandardPrimaryKeyMakesHidden() { $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->radio( - 'Model.field', - array('option A', 'option B'), - array('between' => 'I am between') - ); - $expected = array( - 'fieldset' => array(), - 'legend' => array(), - 'Field', - '/legend', - 'I am between', - 'input' => array( - 'type' => 'hidden', 'name' => 'Model[field]', - 'value' => '', 'id' => 'ModelField_' - ), - array('input' => array( - 'type' => 'radio', 'name' => 'Model[field]', - 'value' => '0', 'id' => 'ModelField0' - )), - array('label' => array('for' => 'ModelField0')), - 'option A', - '/label', - array('input' => array( - 'type' => 'radio', 'name' => 'Model[field]', - 'value' => '1', 'id' => 'ModelField1' - )), - array('label' => array('for' => 'ModelField1')), - 'option B', - '/label', - '/fieldset' - ); - $this->assertTags($result, $expected); - - $result = $this->Form->radio( - 'Model.field', - array('option A', 'option B', 'option C'), - array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C')) + $this->Form->create('User'); + $this->Form->fieldset = array( + 'User' => array( + 'fields' => array( + 'model_id' => array('type' => 'integer') + ), + 'validates' => array(), + 'key' => 'model_id' + ) ); - + $result = $this->Form->input('model_id'); $expected = array( - 'fieldset' => array(), - 'legend' => array(), - 'Field', - '/legend', - 'input' => array( - 'type' => 'hidden', 'name' => 'Model[field]', - 'value' => '', 'id' => 'ModelField_' - ), - array('input' => array( - 'type' => 'radio', 'name' => 'Model[field]', - 'value' => '0', 'id' => 'ModelField0' - )), - array('label' => array('for' => 'ModelField0')), - 'option A', - '/label', - 'between A', - '--separator--', - array('input' => array( - 'type' => 'radio', 'name' => 'Model[field]', - 'value' => '1', 'id' => 'ModelField1' - )), - array('label' => array('for' => 'ModelField1')), - 'option B', - '/label', - 'between B', - '--separator--', - array('input' => array( - 'type' => 'radio', 'name' => 'Model[field]', - 'value' => '2', 'id' => 'ModelField2' - )), - array('label' => array('for' => 'ModelField2')), - 'option C', - '/label', - 'between C', - '/fieldset' + 'input' => array('type' => 'hidden', 'name' => 'User[model_id]', 'id' => 'UserModelId'), ); $this->assertTags($result, $expected); + } - $result = $this->Form->input('Model.field', array( - 'options' => array('1' => 'first', '2' => 'second'), - 'type' => 'radio', - 'before' => '--before--', - 'after' => '--after--', - 'separator' => '--separator--', - 'between' => array('--between first--', '--between second--') - )); - +/** + * test that overriding the magic select type widget is possible + * + * @return void + */ + public function testInputOverridingMagicSelectType() { + $this->markTestIncomplete('Need to revisit once models work again.'); + $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad'); + $result = $this->Form->input('Model.user_id', array('type' => 'text')); $expected = array( - 'div' => array('class' => 'input radio'), - '--before--', - 'fieldset' => array(), - 'legend' => array(), - 'Field', - '/legend', - array('input' => array('type' => 'hidden', 'name' => 'Model[field]', 'id' => 'ModelField_', 'value' => '')), - array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')), - array('label' => array('for' => 'ModelField1')), - 'first', - '/label', - '--between first--', - '--separator--', - array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '2', 'id' => 'ModelField2')), - array('label' => array('for' => 'ModelField2')), - 'second', - '/label', - '--between second--', - '/fieldset', - '--after--', + 'div' => array('class' => 'input text'), + 'label' => array('for' => 'ModelUserId'), 'User', '/label', + 'input' => array('name' => 'Model[user_id]', 'type' => 'text', 'id' => 'ModelUserId'), '/div' ); $this->assertTags($result, $expected); - $result = $this->Form->input('Model.field', array( - 'options' => array('1' => 'first', '2' => 'second'), - 'type' => 'radio', - 'before' => '--before--', - 'after' => '--after--', - 'separator' => '--separator--', - 'between' => array('--between first--') - )); - + //Check that magic types still work for plural/singular vars + $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad'); + $result = $this->Form->input('Model.type'); $expected = array( - 'div' => array('class' => 'input radio'), - '--before--', - 'fieldset' => array(), - 'legend' => array(), - 'Field', - '/legend', - array('input' => array('type' => 'hidden', 'name' => 'Model[field]', 'id' => 'ModelField_', 'value' => '')), - array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')), - array('label' => array('for' => 'ModelField1')), - 'first', - '/label', - '--between first--', - '--separator--', - array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '2', 'id' => 'ModelField2')), - array('label' => array('for' => 'ModelField2')), - 'second', - '/label', - '/fieldset', - '--after--', + 'div' => array('class' => 'input select'), + 'label' => array('for' => 'ModelType'), 'Type', '/label', + 'select' => array('name' => 'Model[type]', 'id' => 'ModelType'), + array('option' => array('value' => 'value')), 'good', '/option', + array('option' => array('value' => 'other')), 'bad', '/option', + '/select', '/div' ); $this->assertTags($result, $expected); } /** - * Test that radios with a 0 value are selected under the correct conditions. - * Also ensure that values that are booleanish are handled correctly. + * Test that inferred types do not override developer input * * @return void */ - public function testRadioOptionWithBooleanishValues() { + public function testInputMagicTypeDoesNotOverride() { $this->markTestIncomplete('Need to revisit once models work again.'); + $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad'); + $result = $this->Form->input('Model.user', array('type' => 'checkbox')); $expected = array( - 'fieldset' => array(), - 'legend' => array(), - 'Field', - '/legend', - array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')), - array('label' => array('for' => 'ModelField1')), - 'Yes', - '/label', - array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')), - array('label' => array('for' => 'ModelField0')), - 'No', - '/label', - '/fieldset' + 'div' => array('class' => 'input checkbox'), + array('input' => array( + 'type' => 'hidden', + 'name' => 'data[Model][user]', + 'id' => 'ModelUser_', + 'value' => 0, + )), + array('input' => array( + 'name' => 'data[Model][user]', + 'type' => 'checkbox', + 'id' => 'ModelUser', + 'value' => 1 + )), + 'label' => array('for' => 'ModelUser'), 'User', '/label', + '/div' ); - $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0')); - $this->assertTags($result, $expected); - - $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0)); - $this->assertTags($result, $expected); - - $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false)); $this->assertTags($result, $expected); + } +/** + * Test that magic input() selects are created for type=number + * + * @return void + */ + public function testInputMagicSelectForTypeNumber() { + $this->markTestIncomplete('Need to revisit once models work again.'); + $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot'); + $this->Form->request->data = array('ValidateUser' => array('balance' => 1)); + $result = $this->Form->input('ValidateUser.balance'); $expected = array( - 'fieldset' => array(), - 'legend' => array(), - 'Field', - '/legend', - 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'), - array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')), - array('label' => array('for' => 'ModelField1')), - 'Yes', - '/label', - array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')), - array('label' => array('for' => 'ModelField0')), - 'No', + 'div' => array('class' => 'input select'), + 'label' => array('for' => 'ValidateUserBalance'), + 'Balance', '/label', - '/fieldset' + 'select' => array('name' => 'ValidateUser[balance]', 'id' => 'ValidateUserBalance'), + array('option' => array('value' => '0')), + 'nothing', + '/option', + array('option' => array('value' => '1', 'selected' => 'selected')), + 'some', + '/option', + array('option' => array('value' => '100')), + 'a lot', + '/option', + '/select', + '/div' ); - $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null)); - $this->assertTags($result, $expected); - - $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '')); $this->assertTags($result, $expected); + } - $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No')); - $this->assertTags($result, $expected); +/** + * Test that magic input() selects can easily be converted into radio types without error. + * + * @return void + */ + public function testInputMagicSelectChangeToRadio() { + $this->markTestIncomplete('Need to revisit once models work again.'); + $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad'); + $result = $this->Form->input('Model.user_id', array('type' => 'radio')); + $this->assertRegExp('/input type="radio"/', $result); + } +/** + * fields with the same name as the model should work. + * + * @return void + */ + public function testInputWithMatchingFieldAndModelName() { + $this->markTestIncomplete('Need to revisit once models work again.'); + $this->Form->create('User'); + $this->Form->fieldset = array( + 'User' => array( + 'fields' => array( + 'User' => array('type' => 'text') + ), + 'validates' => array(), + 'key' => 'id' + ) + ); + $this->Form->request->data['User']['User'] = 'ABC, Inc.'; + $result = $this->Form->input('User', array('type' => 'text')); $expected = array( - 'fieldset' => array(), - 'legend' => array(), - 'Field', - '/legend', - array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')), - array('label' => array('for' => 'ModelField1')), - 'Yes', - '/label', - array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')), - array('label' => array('for' => 'ModelField0')), - 'No', - '/label', - '/fieldset' + 'div' => array('class' => 'input text'), + 'label' => array('for' => 'UserUser'), 'User', '/label', + 'input' => array('name' => 'User[User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'), + '/div' ); - $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1)); - $this->assertTags($result, $expected); - - $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1')); - $this->assertTags($result, $expected); - - $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true)); $this->assertTags($result, $expected); } /** - * test disabled radio options + * testFormInputs method + * + * test correct results from form::inputs(). * * @return void */ - public function testRadioDisabled() { + public function testFormInputs() { $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->radio( - 'Model.field', - array('option A', 'option B'), - array('disabled' => array(0), 'value' => '0') - ); + $this->Form->create('Cake\Test\TestCase\View\Helper\Contact'); + $result = $this->Form->inputs('The Legend'); $expected = array( - 'fieldset' => array(), - 'legend' => array(), - 'Field', + ' array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')), - array('label' => array('for' => 'ModelField0')), - 'option A', - '/label', - array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')), - array('label' => array('for' => 'ModelField1')), - 'option B', - '/label', - '/fieldset' + '*/fieldset', ); $this->assertTags($result, $expected); - $result = $this->Form->radio( - 'Model.field', - array('option A', 'option B'), - array('disabled' => true, 'value' => '0') - ); + $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff')); $expected = array( - 'fieldset' => array(), - 'legend' => array(), - 'Field', + 'fieldset' => array('class' => 'classy-stuff'), + ' array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')), - array('label' => array('for' => 'ModelField0')), - 'option A', - '/label', - array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')), - array('label' => array('for' => 'ModelField1')), - 'option B', - '/label', - '/fieldset' + '*/fieldset' ); $this->assertTags($result, $expected); - $result = $this->Form->radio( - 'Model.field', - array('option A', 'option B'), - array('disabled' => 'disabled', 'value' => '0') - ); - $expected = array( - 'fieldset' => array(), - 'legend' => array(), - 'Field', - '/legend', - array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')), - array('label' => array('for' => 'ModelField0')), - 'option A', - '/label', - array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')), - array('label' => array('for' => 'ModelField1')), - 'option B', - '/label', - '/fieldset' - ); + $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff')); $this->assertTags($result, $expected); - $result = $this->Form->input('Model.field', array( - 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'), - 'type' => 'radio', - 'disabled' => array(2, '3x'), - )); + $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff')); + $this->assertTags($result, $expected); + $this->Form->create('Contact'); + $this->Form->request['prefix'] = 'admin'; + $this->Form->request['action'] = 'admin_edit'; + $result = $this->Form->inputs(); $expected = array( - 'div' => array('class' => 'input radio'), - 'fieldset' => array(), - 'legend' => array(), - 'Field', + ' array('type' => 'hidden', 'name' => 'Model[field]', 'id' => 'ModelField_', 'value' => '')), - array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')), - array('label' => array('for' => 'ModelField1')), - 'first', - '/label', - array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')), - array('label' => array('for' => 'ModelField2')), - 'second', - '/label', - array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '2x', 'id' => 'ModelField2x')), - array('label' => array('for' => 'ModelField2x')), - '2x', - '/label', - array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '3', 'id' => 'ModelField3')), - array('label' => array('for' => 'ModelField3')), - 'third', - '/label', - array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')), - array('label' => array('for' => 'ModelField3x')), - '3x', - '/label', - '/fieldset', - '/div' + '*/fieldset', ); $this->assertTags($result, $expected); - $result = $this->Form->input('Model.field', array( - 'type' => 'radio', - 'options' => array( - 1 => 'A', - 2 => 'B', - 3 => 'C' - ), - 'disabled' => array(1) - )); + $this->Form->create('Contact'); + $result = $this->Form->inputs(false); + $expected = array( + 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'), + array('div' => array('class' => 'input text')), + '*/div', + array('div' => array('class' => 'input email')), + '*/div', + array('div' => array('class' => 'input tel')), + '*/div', + array('div' => array('class' => 'input password')), + '*/div', + array('div' => array('class' => 'input date')), + '*/div', + array('div' => array('class' => 'input date')), + '*/div', + array('div' => array('class' => 'input datetime')), + '*/div', + array('div' => array('class' => 'input number')), + '*/div', + array('div' => array('class' => 'input select')), + '*/div', + ); + $this->assertTags($result, $expected); + $this->Form->create('Contact'); + $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false)); + $expected = array( + 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'), + array('div' => array('class' => 'input text')), + '*/div', + array('div' => array('class' => 'input email')), + '*/div', + array('div' => array('class' => 'input tel')), + '*/div', + array('div' => array('class' => 'input password')), + '*/div', + array('div' => array('class' => 'input date')), + '*/div', + array('div' => array('class' => 'input date')), + '*/div', + array('div' => array('class' => 'input datetime')), + '*/div', + array('div' => array('class' => 'input number')), + '*/div', + array('div' => array('class' => 'input select')), + '*/div', + ); + $this->assertTags($result, $expected); + + $this->Form->create('Contact'); + $result = $this->Form->inputs(null, null, array('fieldset' => false)); + $this->assertTags($result, $expected); + + $this->Form->create('Contact'); + $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false)); + $expected = array( + 'fieldset' => array(), + 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'), + array('div' => array('class' => 'input text')), + '*/div', + array('div' => array('class' => 'input email')), + '*/div', + array('div' => array('class' => 'input tel')), + '*/div', + array('div' => array('class' => 'input password')), + '*/div', + array('div' => array('class' => 'input date')), + '*/div', + array('div' => array('class' => 'input date')), + '*/div', + array('div' => array('class' => 'input datetime')), + '*/div', + array('div' => array('class' => 'input number')), + '*/div', + array('div' => array('class' => 'input select')), + '*/div', + '/fieldset' + ); + $this->assertTags($result, $expected); + + $this->Form->create('Contact'); + $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello')); + $expected = array( + 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'), + array('div' => array('class' => 'input text')), + '*/div', + array('div' => array('class' => 'input email')), + '*/div', + array('div' => array('class' => 'input tel')), + '*/div', + array('div' => array('class' => 'input password')), + '*/div', + array('div' => array('class' => 'input date')), + '*/div', + array('div' => array('class' => 'input date')), + '*/div', + array('div' => array('class' => 'input datetime')), + '*/div', + array('div' => array('class' => 'input number')), + '*/div', + array('div' => array('class' => 'input select')), + '*/div', + ); + $this->assertTags($result, $expected); + + $this->Form->create('Contact'); + $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello')); + $this->assertTags($result, $expected); + + $this->Form->create('Contact'); + $result = $this->Form->inputs('Hello'); $expected = array( - 'div' => array('class' => 'input radio'), 'fieldset' => array(), 'legend' => array(), - 'Field', + 'Hello', '/legend', - array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')), - array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField1', 'disabled' => 'disabled', 'value' => '1')), - array('label' => array('for' => 'ModelField1')), - 'A', - '/label', - array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField2', 'value' => '2')), - array('label' => array('for' => 'ModelField2')), - 'B', - '/label', - array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField3', 'value' => '3')), - array('label' => array('for' => 'ModelField3')), - 'C', + 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'), + array('div' => array('class' => 'input text')), + '*/div', + array('div' => array('class' => 'input email')), + '*/div', + array('div' => array('class' => 'input tel')), + '*/div', + array('div' => array('class' => 'input password')), + '*/div', + array('div' => array('class' => 'input date')), + '*/div', + array('div' => array('class' => 'input date')), + '*/div', + array('div' => array('class' => 'input datetime')), + '*/div', + array('div' => array('class' => 'input number')), + '*/div', + array('div' => array('class' => 'input select')), + '*/div', + '/fieldset' + ); + $this->assertTags($result, $expected); + + $this->Form->create('Contact'); + $result = $this->Form->inputs(array('legend' => 'Hello')); + $expected = array( + 'fieldset' => array(), + 'legend' => array(), + 'Hello', + '/legend', + 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'), + array('div' => array('class' => 'input text')), + '*/div', + array('div' => array('class' => 'input email')), + '*/div', + array('div' => array('class' => 'input tel')), + '*/div', + array('div' => array('class' => 'input password')), + '*/div', + array('div' => array('class' => 'input date')), + '*/div', + array('div' => array('class' => 'input date')), + '*/div', + array('div' => array('class' => 'input datetime')), + '*/div', + array('div' => array('class' => 'input number')), + '*/div', + array('div' => array('class' => 'input select')), + '*/div', + '/fieldset' + ); + $this->assertTags($result, $expected); + + $this->Form->create('Contact'); + $result = $this->Form->inputs(null, null, array('legend' => 'Hello')); + $this->assertTags($result, $expected); + $this->Form->end(); + + $this->Form->create(false); + $expected = array( + 'fieldset' => array(), + array('div' => array('class' => 'input text')), + 'label' => array('for' => 'foo'), + 'Foo', '/label', - '/fieldset', - '/div' + 'input' => array('type' => 'text', 'name' => 'foo', 'id' => 'foo'), + '*/div', + '/fieldset' + ); + $result = $this->Form->inputs( + array('foo' => array('type' => 'text')), + array(), + array('legend' => false) ); $this->assertTags($result, $expected); } /** - * test disabling the hidden input for radio buttons + * Tests inputs() works with plugin models * * @return void */ - public function testRadioHiddenInputDisabling() { + public function testInputsPluginModel() { $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->input('Model.1.field', array( - 'type' => 'radio', - 'options' => array('option A'), - 'hiddenField' => false - ) - ); - $expected = array( - 'div' => array('class' => 'input radio'), - 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0'), - 'label' => array('for' => 'Model1Field0'), - 'option A', - '/label', - '/div' + $this->loadFixtures('Post'); + Plugin::load('TestPlugin'); + $this->Form->request['models'] = array( + 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost') ); - $this->assertTags($result, $expected); + $this->Form->create('TestPlugin.TestPluginPost'); + $result = $this->Form->inputs(); - $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false)); - $expected = array( - 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0'), - 'label' => array('for' => 'Model1Field0'), - 'option A', - '/label' - ); - $this->assertTags($result, $expected); + $this->assertContains('TestPluginPost[id]', $result); + $this->assertContains('TestPluginPost[author_id]', $result); + $this->assertContains('TestPluginPost[title]', $result); + $this->assertEquals('TestPluginPost', $this->Form->model()); } /** - * test adding an empty option for radio buttons + * testSelectAsCheckbox method + * + * test multi-select widget with checkbox formatting. * * @return void */ - public function testRadioAddEmptyOption() { + public function testSelectAsCheckbox() { $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->input('Model.1.field', array( - 'type' => 'radio', - 'options' => array('option A'), - 'empty' => true, - 'hiddenField' => false - )); + $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1))); $expected = array( - 'div' => array('class' => 'input radio'), - 'fieldset' => array(), - 'legend' => array(), - 'Field', - '/legend', - array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '', 'id' => 'Model1Field')), - array('label' => array('for' => 'Model1Field')), - __('empty'), - '/label', - array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0')), - array('label' => array('for' => 'Model1Field0')), - 'option A', - '/label', - '/fieldset', - '/div' + 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'), + array('div' => array('class' => 'checkbox')), + array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')), + array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')), + 'first', + '/label', + '/div', + array('div' => array('class' => 'checkbox')), + array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')), + array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')), + 'second', + '/label', + '/div', + array('div' => array('class' => 'checkbox')), + array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')), + array('label' => array('for' => 'ModelMultiField2')), + 'third', + '/label', + '/div', ); $this->assertTags($result, $expected); - $result = $this->Form->input('Model.1.field', array( - 'type' => 'radio', - 'options' => array('option A'), - 'empty' => 'CustomEmptyLabel', - 'hiddenField' => false - )); + $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox')); $expected = array( - 'div' => array('class' => 'input radio'), - 'fieldset' => array(), - 'legend' => array(), - 'Field', - '/legend', - array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '', 'id' => 'Model1Field')), - array('label' => array('for' => 'Model1Field')), - 'CustomEmptyLabel', - '/label', - array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0')), - array('label' => array('for' => 'Model1Field0')), - 'option A', - '/label', - '/fieldset', - '/div' + 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'), + array('div' => array('class' => 'checkbox')), + array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField1/22')), + array('label' => array('for' => 'ModelMultiField1/22')), + 'half', + '/label', + '/div', ); $this->assertTags($result, $expected); - - $result = $this->Form->input('Model.1.field', array( - 'type' => 'radio', - 'options' => array('option A'), - 'empty' => false, - 'hiddenField' => false - )); - $this->assertTextNotContains('"Model1Field"', $result); } /** - * Test that radio() accepts an array for label + * testLabel method + * + * test label generation. * * @return void */ - public function testRadioLabelArray() { + public function testLabel() { $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->input('Model.field', array( - 'type' => 'radio', - 'legend' => false, - 'label' => array( - 'class' => 'checkbox float-left', - ), - 'options' => array('1' => 'Option A', '2' => 'Option B.') - )); - $this->assertTextContains( - '', - $result - ); + $this->Form->text('Person.name'); + $result = $this->Form->label(); + $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label')); + + $this->Form->text('Person.name'); + $result = $this->Form->label(); + $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label')); + + $result = $this->Form->label('Person.first_name'); + $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label')); + + $result = $this->Form->label('Person.first_name', 'Your first name'); + $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label')); + + $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class')); + $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label')); + + $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID')); + $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label')); + + $result = $this->Form->label('Person.first_name', ''); + $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label')); + + $result = $this->Form->label('Person.2.name', ''); + $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label')); } /** - * Test that label id's match the input element id's when radio is called after create(). + * testTextbox method + * + * test textbox element generation * * @return void */ - public function testRadioWithCreate() { + public function testTextbox() { $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->create('Model'); - $result = $this->Form->radio('recipient', - array('1' => '1', '2' => '2', '3' => '3'), - array('legend' => false, 'value' => '1') - ); - $this->assertTextNotContains( - '', - $result - ); - $this->assertTextContains( - '', - $result + $result = $this->Form->text('Model.field'); + $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'ModelField'))); + + $result = $this->Form->text('Model.field', array('type' => 'password')); + $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Model[field]', 'id' => 'ModelField'))); + + $result = $this->Form->text('Model.field', array('id' => 'theID')); + $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'theID'))); + + $this->Form->request->data['Model']['text'] = 'test HTML values'; + $result = $this->Form->text('Model.text'); + $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[text]', 'value' => 'test <strong>HTML</strong> values', 'id' => 'ModelText'))); + + $Contact->validationErrors['text'] = array(true); + $this->Form->request->data['Contact']['text'] = 'test'; + $result = $this->Form->text('Contact.text', array('id' => 'theID')); + $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Contact[text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error'))); + + $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value'; + $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId')); + $expected = array( + 'input' => array('type' => 'text', 'name' => 'Model[0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId') ); + $this->assertTags($result, $expected); } /** - * testDomIdSuffix method + * testDefaultValue method + * + * Test default value setting * * @return void */ - public function testDomIdSuffix() { + public function testDefaultValue() { $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs'); - $this->assertEquals('1StringWith1DollarSigns', $result); - - $result = $this->Form->domIdSuffix(''); - $this->assertEquals('AbcXFooYBar', $result); - - $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs', 'html5'); - $this->assertEquals('1StringWith1$-dollarSigns', $result); + $this->Form->request->data['Model']['field'] = 'test'; + $result = $this->Form->text('Model.field', array('default' => 'default value')); + $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'value' => 'test', 'id' => 'ModelField'))); - $result = $this->Form->domIdSuffix('', 'html5'); - $this->assertEquals('AbcX=FooY=Bar', $result); + unset($this->Form->request->data['Model']['field']); + $result = $this->Form->text('Model.field', array('default' => 'default value')); + $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'value' => 'default value', 'id' => 'ModelField'))); } /** - * testDomIdSuffixCollisionResolvement() + * testCheckboxDefaultValue method + * + * Test default value setting on checkbox() method * * @return void */ - public function testDomIdSuffixCollisionResolvement() { + public function testCheckboxDefaultValue() { $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->domIdSuffix('a>b'); - $this->assertEquals('AB', $result); - - $result = $this->Form->domIdSuffix('aassertEquals('AB1', $result); - - $result = $this->Form->domIdSuffix('a\'b'); - $this->assertEquals('AB2', $result); + $this->Form->request->data['Model']['field'] = false; + $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false)); + $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField'))); - $result = $this->Form->domIdSuffix('1 string with 1$-dollar'); - $this->assertEquals('1StringWith1Dollar', $result); + unset($this->Form->request->data['Model']['field']); + $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false)); + $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))); - $result = $this->Form->domIdSuffix('1 string with 1$-dollar'); - $this->assertEquals('1StringWith1Dollar1', $result); + $this->Form->request->data['Model']['field'] = true; + $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false)); + $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))); - $result = $this->Form->domIdSuffix('1 string with 1$-dollar'); - $this->assertEquals('1StringWith1Dollar2', $result); + unset($this->Form->request->data['Model']['field']); + $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false)); + $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField'))); } /** - * testSelect method + * testError method * - * Test select element generation. + * Test field error generation * * @return void */ - public function testSelect() { + public function testError() { $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->select('Model.field', array()); - $expected = array( - 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'), - array('option' => array('value' => '')), - '/option', - '/select' - ); - $this->assertTags($result, $expected); + $Contact->validationErrors['field'] = array(1); + $result = $this->Form->error('Contact.field'); + $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div')); - $this->Form->request->data = array('Model' => array('field' => 'value')); - $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad')); + $result = $this->Form->error('Contact.field', null, array('wrap' => false)); + $this->assertEquals('Error in field Field', $result); + + $Contact->validationErrors['field'] = array("This field contains invalid input"); + $result = $this->Form->error('Contact.field', null, array('wrap' => false)); + $this->assertEquals('This field contains invalid input', $result); + + $Contact->validationErrors['field'] = array("This field contains invalid input"); + $result = $this->Form->error('Contact.field', null, array('wrap' => 'span')); + $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span')); + + $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span')); + $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span')); + + $result = $this->Form->error('Contact.field', "Badness!", array('wrap' => false)); + $this->assertEquals('<strong>Badness!</strong>', $result); + + $result = $this->Form->error('Contact.field', "Badness!", array('wrap' => false, 'escape' => true)); + $this->assertEquals('<strong>Badness!</strong>', $result); + + $result = $this->Form->error('Contact.field', "Badness!", array('wrap' => false, 'escape' => false)); + $this->assertEquals('Badness!', $result); + + $Contact->validationErrors['field'] = array("email"); + $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!')); $expected = array( - 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'), - array('option' => array('value' => '')), - '/option', - array('option' => array('value' => 'value', 'selected' => 'selected')), - 'good', - '/option', - array('option' => array('value' => 'other')), - 'bad', - '/option', - '/select' + 'div' => array('class' => 'field-error'), + 'No good!', + '/div' ); $this->assertTags($result, $expected); - $this->Form->request->data = array(); - $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad')); - $expected = array( - 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'), - array('option' => array('value' => '')), - '/option', - array('option' => array('value' => 'value')), - 'good', - '/option', - array('option' => array('value' => 'other')), - 'bad', - '/option', - '/select' - ); - $this->assertTags($result, $expected); - - $result = $this->Form->select( - 'Model.field', array('first' => 'first "html" ', 'second' => 'value'), - array('empty' => false) - ); - $expected = array( - 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'), - array('option' => array('value' => 'first')), - 'first "html" <chars>', - '/option', - array('option' => array('value' => 'second')), - 'value', - '/option', - '/select' - ); - $this->assertTags($result, $expected); - - $result = $this->Form->select( - 'Model.field', - array('first' => 'first "html" ', 'second' => 'value'), - array('escape' => false, 'empty' => false) - ); - $expected = array( - 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'), - array('option' => array('value' => 'first')), - 'first "html" ', - '/option', - array('option' => array('value' => 'second')), - 'value', - '/option', - '/select' - ); - $this->assertTags($result, $expected); - - $options = array( - array('value' => 'first', 'name' => 'First'), - array('value' => 'first', 'name' => 'Another First'), - ); - $result = $this->Form->select( - 'Model.field', - $options, - array('escape' => false, 'empty' => false) - ); - $expected = array( - 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'), - array('option' => array('value' => 'first')), - 'First', - '/option', - array('option' => array('value' => 'first')), - 'Another First', - '/option', - '/select' - ); - $this->assertTags($result, $expected); - - $this->Form->request->data = array('Model' => array('contact_id' => 228)); - $result = $this->Form->select( - 'Model.contact_id', - array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'), - array('escape' => false, 'empty' => 'pick something') - ); - + $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else'); + $result = $this->Form->error('Contact.field', array( + 'notEmpty' => 'Cannot be empty', + 'email' => 'No good!' + )); $expected = array( - 'select' => array('name' => 'Model[contact_id]', 'id' => 'ModelContactId'), - array('option' => array('value' => '')), 'pick something', '/option', - array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option', - array('option' => array('value' => '228-1')), '228-1 value', '/option', - array('option' => array('value' => '228-2')), '228-2 value', '/option', - '/select' + 'div' => array('class' => 'error-message'), + 'ul' => array(), + 'assertTags($result, $expected); - $this->Form->request->data['Model']['field'] = 0; - $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes')); - $expected = array( - 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'), - array('option' => array('value' => '')), '/option', - array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option', - array('option' => array('value' => '1')), 'Yes', '/option', - '/select' - ); - $this->assertTags($result, $expected); + // Testing error messages list options + $Contact->validationErrors['field'] = array('notEmpty', 'email'); - $this->Form->request->data['Model']['field'] = 50; - $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty')); + $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol')); $expected = array( - 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'), - array('option' => array('value' => '')), '/option', - array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option', - array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option', - '/select' + 'div' => array('class' => 'error-message'), + 'ol' => array(), + 'assertTags($result, $expected); - $result = $this->Form->select('Contact.required_one', array('option A')); + $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol'))); $expected = array( - 'select' => array( - 'name' => 'data[Contact][required_one]', - 'id' => 'ContactRequiredOne', - 'required' => 'required' - ), - array('option' => array('value' => '')), '/option', - array('option' => array('value' => '0')), 'option A', '/option', - '/select' + 'div' => array('class' => 'error-message'), + 'ol' => array(), + 'assertTags($result, $expected); - $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true)); + $result = $this->Form->error('Contact.field', null, array( + 'listOptions' => array( + 'class' => 'ul-class', + 'itemOptions' => array( + 'class' => 'li-class' + ) + ) + )); $expected = array( - 'select' => array( - 'name' => 'data[Contact][required_one]', - 'id' => 'ContactRequiredOne', - 'disabled' => 'disabled' - ), - array('option' => array('value' => '')), '/option', - array('option' => array('value' => '0')), 'option A', '/option', - '/select' + 'div' => array('class' => 'error-message'), + 'ul' => array('class' => 'ul-class'), + array('li' => array('class' => 'li-class')), 'notEmpty', '/li', + array('li' => array('class' => 'li-class')), 'email', '/li', + '/ul', + '/div' ); $this->assertTags($result, $expected); } /** - * test that select() with optiongroups listens to the escape param. + * test error options when using form->input(); * * @return void */ - public function testSelectOptionGroupEscaping() { + public function testInputErrorEscape() { $this->markTestIncomplete('Need to revisit once models work again.'); - $options = array( - '>< Key' => array( - 1 => 'One', - 2 => 'Two' - ) - ); - $result = $this->Form->select('Model.field', $options, array('empty' => false)); - $expected = array( - 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'), - 'optgroup' => array('label' => '>< Key'), - array('option' => array('value' => '1')), 'One', '/option', - array('option' => array('value' => '2')), 'Two', '/option', - '/optgroup', - '/select' - ); - $this->assertTags($result, $expected); + $this->Form->create('ValidateProfile'); + $ValidateProfile->validationErrors['city'] = array('required
'); + $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true)))); + $this->assertRegExp('/required<br>/', $result); - $options = array( - '>< Key' => array( - 1 => 'One', - 2 => 'Two' - ) - ); - $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false)); - $expected = array( - 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'), - 'optgroup' => array('label' => '>< Key'), - array('option' => array('value' => '1')), 'One', '/option', - array('option' => array('value' => '2')), 'Two', '/option', - '/optgroup', - '/select' - ); - $this->assertTags($result, $expected); + $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false)))); + $this->assertRegExp('/required
/', $result); } /** - * Tests that FormHelper::select() allows null to be passed in the $attributes parameter + * testPassword method + * + * Test password element generation * * @return void */ - public function testSelectWithNullAttributes() { + public function testPassword() { $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false)); - $expected = array( - 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'), - array('option' => array('value' => '0')), - 'first', - '/option', - array('option' => array('value' => '1')), - 'second', - '/option', - '/select' - ); - $this->assertTags($result, $expected); + $result = $this->Form->password('Contact.field'); + $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[field]', 'id' => 'ContactField'))); + + $Contact->validationErrors['passwd'] = 1; + $this->Form->request->data['Contact']['passwd'] = 'test'; + $result = $this->Form->password('Contact.passwd', array('id' => 'theID')); + $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error'))); } /** - * testNestedSelect method + * testRadio method * - * test select element generation with optgroups + * Test radio element set generation * * @return void */ - public function testNestedSelect() { + public function testRadio() { $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->select( - 'Model.field', - array(1 => 'One', 2 => 'Two', 'Three' => array( - 3 => 'Three', 4 => 'Four', 5 => 'Five' - )), array('empty' => false) + $result = $this->Form->radio('Model.field', array('option A')); + $expected = array( + 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'), + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')), + 'label' => array('for' => 'ModelField0'), + 'option A', + '/label' ); + $this->assertTags($result, $expected); + + $result = $this->Form->radio('Model.field', array('1/2' => 'half')); $expected = array( - 'select' => array('name' => 'Model[field]', - 'id' => 'ModelField'), - array('option' => array('value' => 1)), - 'One', - '/option', - array('option' => array('value' => 2)), - 'Two', - '/option', - array('optgroup' => array('label' => 'Three')), - array('option' => array('value' => 4)), - 'Four', - '/option', - array('option' => array('value' => 5)), - 'Five', - '/option', - '/optgroup', - '/select' - ); + 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'), + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1/2', 'id' => 'ModelField1/2')), + 'label' => array('for' => 'ModelField1/2'), + 'half', + '/label' + ); $this->assertTags($result, $expected); - $result = $this->Form->select( - 'Model.field', - array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')), - array('showParents' => true, 'empty' => false) + $result = $this->Form->radio('Model.field', array('option A', 'option B')); + $expected = array( + 'fieldset' => array(), + 'legend' => array(), + 'Field', + '/legend', + 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'), + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')), + array('label' => array('for' => 'ModelField0')), + 'option A', + '/label', + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')), + array('label' => array('for' => 'ModelField1')), + 'option B', + '/label', + '/fieldset' ); + $this->assertTags($result, $expected); + $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '
')); $expected = array( - 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'), - array('option' => array('value' => 1)), - 'One', - '/option', - array('option' => array('value' => 2)), - 'Two', - '/option', - array('optgroup' => array('label' => 'Three')), - array('option' => array('value' => 3)), - 'Three', - '/option', - array('option' => array('value' => 4)), - 'Four', - '/option', - '/optgroup', - '/select' + 'fieldset' => array(), + 'legend' => array(), + 'Field', + '/legend', + 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'), + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')), + array('label' => array('for' => 'ModelField0')), + 'option A', + '/label', + 'br' => array(), + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')), + array('label' => array('for' => 'ModelField1')), + 'option B', + '/label', + '/fieldset' ); $this->assertTags($result, $expected); - } -/** - * testSelectMultiple method - * - * test generation of multiple select elements - * - * @return void - */ - public function testSelectMultiple() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $options = array('first', 'second', 'third'); - $result = $this->Form->select( - 'Model.multi_field', $options, array('multiple' => true) - ); + $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female')); $expected = array( - 'input' => array( - 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField_' - ), - 'select' => array( - 'name' => 'Model[multi_field][]', - 'id' => 'ModelMultiField', 'multiple' => 'multiple' - ), - array('option' => array('value' => '0')), - 'first', - '/option', - array('option' => array('value' => '1')), - 'second', - '/option', - array('option' => array('value' => '2')), - 'third', - '/option', - '/select' + 'fieldset' => array(), + 'legend' => array(), + 'Gender', + '/legend', + 'input' => array('type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'id' => 'EmployeeGender_'), + array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')), + array('label' => array('for' => 'EmployeeGenderMale')), + 'Male', + '/label', + array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')), + array('label' => array('for' => 'EmployeeGenderFemale')), + 'Female', + '/label', + '/fieldset', ); $this->assertTags($result, $expected); - $result = $this->Form->select( - 'Model.multi_field', $options, array('multiple' => 'multiple') + $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female')); + $expected = array( + 'fieldset' => array(), + 'legend' => array(), + 'Gender', + '/legend', + 'input' => array('type' => 'hidden', 'name' => 'Officer[gender]', 'value' => '', 'id' => 'OfficerGender_'), + array('input' => array('type' => 'radio', 'name' => 'Officer[gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')), + array('label' => array('for' => 'OfficerGenderMale')), + 'Male', + '/label', + array('input' => array('type' => 'radio', 'name' => 'Officer[gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')), + array('label' => array('for' => 'OfficerGenderFemale')), + 'Female', + '/label', + '/fieldset', ); + $this->assertTags($result, $expected); + + $result = $this->Form->radio('Contact.1.imrequired', array('option A')); $expected = array( - 'input' => array( - 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField_' - ), - 'select' => array( - 'name' => 'Model[multi_field][]', - 'id' => 'ModelMultiField', 'multiple' => 'multiple' - ), - array('option' => array('value' => '0')), - 'first', - '/option', - array('option' => array('value' => '1')), - 'second', - '/option', - array('option' => array('value' => '2')), - 'third', - '/option', - '/select' + 'input' => array('type' => 'hidden', 'name' => 'Contact[1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'), + array('input' => array('type' => 'radio', 'name' => 'Contact[1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0', 'required' => 'required')), + 'label' => array('for' => 'Contact1Imrequired0'), + 'option A', + '/label' ); $this->assertTags($result, $expected); - $result = $this->Form->select( - 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1)) + $result = $this->Form->radio('Model.1.field', array('option A')); + $expected = array( + 'input' => array('type' => 'hidden', 'name' => 'Model[1][field]', 'value' => '', 'id' => 'Model1Field_'), + array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0')), + 'label' => array('for' => 'Model1Field0'), + 'option A', + '/label' ); + $this->assertTags($result, $expected); + + $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'Model[custom]')); $expected = array( - 'input' => array( - 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField_' - ), - 'select' => array( - 'name' => 'Model[multi_field][]', 'id' => 'ModelMultiField', - 'multiple' => 'multiple' - ), - array('option' => array('value' => '0', 'selected' => 'selected')), - 'first', - '/option', - array('option' => array('value' => '1', 'selected' => 'selected')), - 'second', - '/option', - array('option' => array('value' => '2')), - 'third', - '/option', - '/select' + 'fieldset' => array(), + 'legend' => array(), + 'Field', + '/legend', + 'input' => array('type' => 'hidden', 'name' => 'Model[custom]', 'value' => '', 'id' => 'ModelField_'), + array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'ModelField0')), + array('label' => array('for' => 'ModelField0')), + 'option A', + '/label', + array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'ModelField1')), + array('label' => array('for' => 'ModelField1')), + 'option B', + '/label', + '/fieldset' ); $this->assertTags($result, $expected); - $result = $this->Form->select( - 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1)) + $result = $this->Form->radio( + 'Model.field', + array('a>b' => 'first', 'a 'second', 'a"b' => 'third') ); $expected = array( - 'select' => array( - 'name' => 'Model[multi_field]', 'id' => 'ModelMultiField' + 'fieldset' => array(), + 'legend' => array(), + 'Field', + '/legend', + 'input' => array( + 'type' => 'hidden', 'name' => 'data[Model][field]', + 'id' => 'ModelField_', 'value' => '', ), - array('option' => array('value' => '0', 'selected' => 'selected')), + array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', + 'id' => 'ModelFieldAB', 'value' => 'a>b')), + array('label' => array('for' => 'ModelFieldAB')), 'first', - '/option', - array('option' => array('value' => '1', 'selected' => 'selected')), + '/label', + array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', + 'id' => 'ModelFieldAB1', 'value' => 'a<b')), + array('label' => array('for' => 'ModelFieldAB1')), 'second', - '/option', - array('option' => array('value' => '2')), + '/label', + array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', + 'id' => 'ModelFieldAB2', 'value' => 'a"b')), + array('label' => array('for' => 'ModelFieldAB2')), 'third', - '/option', - '/select' + '/label', + '/fieldset' ); $this->assertTags($result, $expected); + } - $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy'); - $selected = array('2', '3x'); - $result = $this->Form->select( - 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected) +/** + * Test radio inputs with between as string or array. Also ensure + * that an array with less between elements works. + * + * @return void + */ + public function testRadioBetween() { + $this->markTestIncomplete('Need to revisit once models work again.'); + $result = $this->Form->radio( + 'Model.field', + array('option A', 'option B'), + array('between' => 'I am between') ); $expected = array( + 'fieldset' => array(), + 'legend' => array(), + 'Field', + '/legend', + 'I am between', 'input' => array( - 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField_' - ), - 'select' => array( - 'name' => 'Model[multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField' + 'type' => 'hidden', 'name' => 'Model[field]', + 'value' => '', 'id' => 'ModelField_' ), - array('option' => array('value' => '1')), - 'One', - '/option', - array('option' => array('value' => '2', 'selected' => 'selected')), - 'Two', - '/option', - array('option' => array('value' => '3')), - 'Three', - '/option', - array('option' => array('value' => '3x', 'selected' => 'selected')), - 'Stringy', - '/option', - '/select' + array('input' => array( + 'type' => 'radio', 'name' => 'Model[field]', + 'value' => '0', 'id' => 'ModelField0' + )), + array('label' => array('for' => 'ModelField0')), + 'option A', + '/label', + array('input' => array( + 'type' => 'radio', 'name' => 'Model[field]', + 'value' => '1', 'id' => 'ModelField1' + )), + array('label' => array('for' => 'ModelField1')), + 'option B', + '/label', + '/fieldset' ); $this->assertTags($result, $expected); - $result = $this->Form->select('Contact.required_one', array( - '1' => 'option A', - '2' => 'option B' - ), array('multiple' => true)); - $expected = array( - 'input' => array( - 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_' - ), - 'select' => array( - 'name' => 'data[Contact][required_one][]', - 'id' => 'ContactRequiredOne', - 'required' => 'required', - 'multiple' => 'multiple' - ), - array('option' => array('value' => '1')), 'option A', '/option', - array('option' => array('value' => '2')), 'option B', '/option', - '/select' + $result = $this->Form->radio( + 'Model.field', + array('option A', 'option B', 'option C'), + array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C')) ); - $this->assertTags($result, $expected); - $result = $this->Form->select( - 'Model.multi_field', - array('a>b' => 'first', 'a 'second', 'a"b' => 'third'), - array('multiple' => true) - ); $expected = array( + 'fieldset' => array(), + 'legend' => array(), + 'Field', + '/legend', 'input' => array( - 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', - 'id' => 'ModelMultiField_' + 'type' => 'hidden', 'name' => 'Model[field]', + 'value' => '', 'id' => 'ModelField_' ), - array('select' => array('name' => 'data[Model][multi_field][]', - 'multiple' => 'multiple', 'id' => 'ModelMultiField' + array('input' => array( + 'type' => 'radio', 'name' => 'Model[field]', + 'value' => '0', 'id' => 'ModelField0' )), - array('option' => array('value' => 'a>b')), - 'first', - '/option', - array('option' => array('value' => 'a<b')), - 'second', - '/option', - array('option' => array( - 'value' => 'a"b' + array('label' => array('for' => 'ModelField0')), + 'option A', + '/label', + 'between A', + '--separator--', + array('input' => array( + 'type' => 'radio', 'name' => 'Model[field]', + 'value' => '1', 'id' => 'ModelField1' )), - 'third', - '/option', - '/select' + array('label' => array('for' => 'ModelField1')), + 'option B', + '/label', + 'between B', + '--separator--', + array('input' => array( + 'type' => 'radio', 'name' => 'Model[field]', + 'value' => '2', 'id' => 'ModelField2' + )), + array('label' => array('for' => 'ModelField2')), + 'option C', + '/label', + 'between C', + '/fieldset' ); $this->assertTags($result, $expected); - } -/** - * Test generating multiple select with disabled elements. - * - * @return void - */ - public function testSelectMultipleWithDisabledElements() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy'); - $disabled = array(2, 3); - $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled)); - $expected = array( - 'input' => array( - 'type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '', 'id' => 'ContactMultiple_' - ), - 'select' => array( - 'name' => 'Contact[multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple' - ), - array('option' => array('value' => '1')), - 'One', - '/option', - array('option' => array('value' => '2', 'disabled' => 'disabled')), - 'Two', - '/option', - array('option' => array('value' => '3', 'disabled' => 'disabled')), - 'Three', - '/option', - array('option' => array('value' => '3x')), - 'Stringy', - '/option', - '/select' - ); - $this->assertTags($result, $expected); + $result = $this->Form->input('Model.field', array( + 'options' => array('1' => 'first', '2' => 'second'), + 'type' => 'radio', + 'before' => '--before--', + 'after' => '--after--', + 'separator' => '--separator--', + 'between' => array('--between first--', '--between second--') + )); - $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy'); - $disabled = array('2', '3x'); - $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options)); $expected = array( - array('div' => array('class' => 'input select')), - array('label' => array('for' => 'ContactMultiple')), - 'Multiple', + 'div' => array('class' => 'input radio'), + '--before--', + 'fieldset' => array(), + 'legend' => array(), + 'Field', + '/legend', + array('input' => array('type' => 'hidden', 'name' => 'Model[field]', 'id' => 'ModelField_', 'value' => '')), + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')), + array('label' => array('for' => 'ModelField1')), + 'first', '/label', - 'input' => array( - 'type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '', 'id' => 'ContactMultiple_' - ), - 'select' => array( - 'name' => 'Contact[multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple' - ), - array('option' => array('value' => '1')), - 'One', - '/option', - array('option' => array('value' => '2', 'disabled' => 'disabled')), - 'Two', - '/option', - array('option' => array('value' => '3')), - 'Three', - '/option', - array('option' => array('value' => '3x', 'disabled' => 'disabled')), - 'Stringy', - '/option', - '/select', + '--between first--', + '--separator--', + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '2', 'id' => 'ModelField2')), + array('label' => array('for' => 'ModelField2')), + 'second', + '/label', + '--between second--', + '/fieldset', + '--after--', '/div' ); $this->assertTags($result, $expected); - $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy'); - $disabled = true; - $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options)); + $result = $this->Form->input('Model.field', array( + 'options' => array('1' => 'first', '2' => 'second'), + 'type' => 'radio', + 'before' => '--before--', + 'after' => '--after--', + 'separator' => '--separator--', + 'between' => array('--between first--') + )); + $expected = array( - array('div' => array('class' => 'input select')), - array('label' => array('for' => 'ContactMultiple')), - 'Multiple', + 'div' => array('class' => 'input radio'), + '--before--', + 'fieldset' => array(), + 'legend' => array(), + 'Field', + '/legend', + array('input' => array('type' => 'hidden', 'name' => 'Model[field]', 'id' => 'ModelField_', 'value' => '')), + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')), + array('label' => array('for' => 'ModelField1')), + 'first', '/label', - 'input' => array( - 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_' - ), - 'select' => array( - 'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple' - ), - array('option' => array('value' => '1')), - 'One', - '/option', - array('option' => array('value' => '2')), - 'Two', - '/option', - array('option' => array('value' => '3')), - 'Three', - '/option', - array('option' => array('value' => '3x')), - 'Stringy', - '/option', - '/select', + '--between first--', + '--separator--', + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '2', 'id' => 'ModelField2')), + array('label' => array('for' => 'ModelField2')), + 'second', + '/label', + '/fieldset', + '--after--', '/div' ); $this->assertTags($result, $expected); } /** - * Test generating select with disabled elements. + * Test that radios with a 0 value are selected under the correct conditions. + * Also ensure that values that are booleanish are handled correctly. * * @return void */ - public function testSelectWithDisabledElements() { + public function testRadioOptionWithBooleanishValues() { $this->markTestIncomplete('Need to revisit once models work again.'); - $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy'); - $disabled = array(2, 3); - $result = $this->Form->select('Model.field', $options, array('disabled' => $disabled)); $expected = array( - 'select' => array( - 'name' => 'data[Model][field]', 'id' => 'ModelField' - ), - array('option' => array('value' => '')), - '/option', - array('option' => array('value' => '1')), - 'One', - '/option', - array('option' => array('value' => '2', 'disabled' => 'disabled')), - 'Two', - '/option', - array('option' => array('value' => '3', 'disabled' => 'disabled')), - 'Three', - '/option', - array('option' => array('value' => '3x')), - 'Stringy', - '/option', - '/select' + 'fieldset' => array(), + 'legend' => array(), + 'Field', + '/legend', + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')), + array('label' => array('for' => 'ModelField1')), + 'Yes', + '/label', + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')), + array('label' => array('for' => 'ModelField0')), + 'No', + '/label', + '/fieldset' ); + $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0')); + $this->assertTags($result, $expected); + + $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0)); + $this->assertTags($result, $expected); + + $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false)); $this->assertTags($result, $expected); - $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy'); - $disabled = array('2', '3x'); - $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options)); $expected = array( - array('div' => array('class' => 'input select')), - array('label' => array('for' => 'ModelField')), + 'fieldset' => array(), + 'legend' => array(), 'Field', + '/legend', + 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'), + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')), + array('label' => array('for' => 'ModelField1')), + 'Yes', '/label', - 'select' => array( - 'name' => 'data[Model][field]', 'id' => 'ModelField' - ), - array('option' => array('value' => '1')), - 'One', - '/option', - array('option' => array('value' => '2', 'disabled' => 'disabled')), - 'Two', - '/option', - array('option' => array('value' => '3')), - 'Three', - '/option', - array('option' => array('value' => '3x', 'disabled' => 'disabled')), - 'Stringy', - '/option', - '/select', - '/div' + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')), + array('label' => array('for' => 'ModelField0')), + 'No', + '/label', + '/fieldset' ); + $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null)); + $this->assertTags($result, $expected); + + $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '')); + $this->assertTags($result, $expected); + + $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No')); $this->assertTags($result, $expected); - $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy'); - $disabled = true; - $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options)); $expected = array( - array('div' => array('class' => 'input select')), - array('label' => array('for' => 'ModelField')), + 'fieldset' => array(), + 'legend' => array(), 'Field', + '/legend', + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')), + array('label' => array('for' => 'ModelField1')), + 'Yes', '/label', - 'select' => array( - 'name' => 'data[Model][field]', 'id' => 'ModelField', 'disabled' => 'disabled' - ), - array('option' => array('value' => '1')), - 'One', - '/option', - array('option' => array('value' => '2')), - 'Two', - '/option', - array('option' => array('value' => '3')), - 'Three', - '/option', - array('option' => array('value' => '3x')), - 'Stringy', - '/option', - '/select', - '/div' + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')), + array('label' => array('for' => 'ModelField0')), + 'No', + '/label', + '/fieldset' ); + $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1)); + $this->assertTags($result, $expected); + + $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1')); + $this->assertTags($result, $expected); + + $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true)); $this->assertTags($result, $expected); } /** - * test generation of habtm select boxes. + * test disabled radio options * * @return void */ - public function testHabtmSelectBox() { + public function testRadioDisabled() { $this->markTestIncomplete('Need to revisit once models work again.'); - $this->View->viewVars['contactTags'] = array( - 1 => 'blue', - 2 => 'red', - 3 => 'green' - ); - $this->Form->request->data = array( - 'Contact' => array(), - 'ContactTag' => array( - array( - 'id' => '1', - 'name' => 'blue' - ), - array( - 'id' => 3, - 'name' => 'green' - ) - ) + $result = $this->Form->radio( + 'Model.field', + array('option A', 'option B'), + array('disabled' => array(0), 'value' => '0') ); - $this->Form->create('Contact'); - $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false)); $expected = array( - 'input' => array( - 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_' - ), - 'select' => array( - 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag', - 'multiple' => 'multiple' - ), - array('option' => array('value' => '1', 'selected' => 'selected')), - 'blue', - '/option', - array('option' => array('value' => '2')), - 'red', - '/option', - array('option' => array('value' => '3', 'selected' => 'selected')), - 'green', - '/option', - '/select' + 'fieldset' => array(), + 'legend' => array(), + 'Field', + '/legend', + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')), + array('label' => array('for' => 'ModelField0')), + 'option A', + '/label', + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')), + array('label' => array('for' => 'ModelField1')), + 'option B', + '/label', + '/fieldset' ); $this->assertTags($result, $expected); - // make sure only 50 is selected, and not 50f5c0cf - $this->View->viewVars['contactTags'] = array( - '1' => 'blue', - '50f5c0cf' => 'red', - '50' => 'green' - ); - $this->Form->request->data = array( - 'Contact' => array(), - 'ContactTag' => array( - array( - 'id' => 1, - 'name' => 'blue' - ), - array( - 'id' => 50, - 'name' => 'green' - ) - ) + $result = $this->Form->radio( + 'Model.field', + array('option A', 'option B'), + array('disabled' => true, 'value' => '0') ); - $this->Form->create('Contact'); - $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false)); $expected = array( - 'input' => array( - 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_' - ), - 'select' => array( - 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag', - 'multiple' => 'multiple' - ), - array('option' => array('value' => '1', 'selected' => 'selected')), - 'blue', - '/option', - array('option' => array('value' => '50f5c0cf')), - 'red', - '/option', - array('option' => array('value' => '50', 'selected' => 'selected')), - 'green', - '/option', - '/select' + 'fieldset' => array(), + 'legend' => array(), + 'Field', + '/legend', + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')), + array('label' => array('for' => 'ModelField0')), + 'option A', + '/label', + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')), + array('label' => array('for' => 'ModelField1')), + 'option B', + '/label', + '/fieldset' ); $this->assertTags($result, $expected); - } -/** - * test generation of multi select elements in checkbox format - * - * @return void - */ - public function testSelectMultipleCheckboxes() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->select( - 'Model.multi_field', - array('first', 'second', 'third'), - array('multiple' => 'checkbox') + $result = $this->Form->radio( + 'Model.field', + array('option A', 'option B'), + array('disabled' => 'disabled', 'value' => '0') ); - $expected = array( - 'input' => array( - 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField' - ), - array('div' => array('class' => 'checkbox')), - array('input' => array( - 'type' => 'checkbox', 'name' => 'Model[multi_field][]', - 'value' => '0', 'id' => 'ModelMultiField0' - )), - array('label' => array('for' => 'ModelMultiField0')), - 'first', - '/label', - '/div', - array('div' => array('class' => 'checkbox')), - array('input' => array( - 'type' => 'checkbox', 'name' => 'Model[multi_field][]', - 'value' => '1', 'id' => 'ModelMultiField1' - )), - array('label' => array('for' => 'ModelMultiField1')), - 'second', + 'fieldset' => array(), + 'legend' => array(), + 'Field', + '/legend', + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')), + array('label' => array('for' => 'ModelField0')), + 'option A', '/label', - '/div', - array('div' => array('class' => 'checkbox')), - array('input' => array( - 'type' => 'checkbox', 'name' => 'Model[multi_field][]', - 'value' => '2', 'id' => 'ModelMultiField2' - )), - array('label' => array('for' => 'ModelMultiField2')), - 'third', + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')), + array('label' => array('for' => 'ModelField1')), + 'option B', '/label', - '/div' + '/fieldset' ); $this->assertTags($result, $expected); - $result = $this->Form->select( - 'Model.multi_field', - array('a' => 'first', 'b' => 'second', 'c' => 'third'), - array('multiple' => 'checkbox') - ); + $result = $this->Form->input('Model.field', array( + 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'), + 'type' => 'radio', + 'disabled' => array(2, '3x'), + )); + $expected = array( - 'input' => array( - 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField' - ), - array('div' => array('class' => 'checkbox')), - array('input' => array( - 'type' => 'checkbox', 'name' => 'Model[multi_field][]', - 'value' => 'a', 'id' => 'ModelMultiFieldA' - )), - array('label' => array('for' => 'ModelMultiFieldA')), + 'div' => array('class' => 'input radio'), + 'fieldset' => array(), + 'legend' => array(), + 'Field', + '/legend', + array('input' => array('type' => 'hidden', 'name' => 'Model[field]', 'id' => 'ModelField_', 'value' => '')), + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')), + array('label' => array('for' => 'ModelField1')), 'first', '/label', - '/div', - array('div' => array('class' => 'checkbox')), - array('input' => array( - 'type' => 'checkbox', 'name' => 'Model[multi_field][]', - 'value' => 'b', 'id' => 'ModelMultiFieldB' - )), - array('label' => array('for' => 'ModelMultiFieldB')), + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')), + array('label' => array('for' => 'ModelField2')), 'second', '/label', - '/div', - array('div' => array('class' => 'checkbox')), - array('input' => array( - 'type' => 'checkbox', 'name' => 'Model[multi_field][]', - 'value' => 'c', 'id' => 'ModelMultiFieldC' - )), - array('label' => array('for' => 'ModelMultiFieldC')), + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '2x', 'id' => 'ModelField2x')), + array('label' => array('for' => 'ModelField2x')), + '2x', + '/label', + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '3', 'id' => 'ModelField3')), + array('label' => array('for' => 'ModelField3')), 'third', '/label', + array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')), + array('label' => array('for' => 'ModelField3x')), + '3x', + '/label', + '/fieldset', '/div' ); $this->assertTags($result, $expected); - $result = $this->Form->select( - 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox') - ); - $expected = array( - 'input' => array( - 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField' + $result = $this->Form->input('Model.field', array( + 'type' => 'radio', + 'options' => array( + 1 => 'A', + 2 => 'B', + 3 => 'C' ), - array('div' => array('class' => 'checkbox')), - array('input' => array( - 'type' => 'checkbox', 'name' => 'Model[multi_field][]', - 'value' => '1', 'id' => 'ModelMultiField1' - )), - array('label' => array('for' => 'ModelMultiField1')), - 'first', - '/label', - '/div' - ); - $this->assertTags($result, $expected); + 'disabled' => array(1) + )); - $this->Form->request->data = array('Model' => array('tags' => array(1))); - $result = $this->Form->select( - 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox') - ); $expected = array( - 'input' => array( - 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '', 'id' => 'ModelTags' - ), - array('div' => array('class' => 'checkbox')), - array('input' => array( - 'type' => 'checkbox', 'name' => 'Model[tags][]', - 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked' - )), - array('label' => array('for' => 'ModelTags1', 'class' => 'selected')), - 'first', + 'div' => array('class' => 'input radio'), + 'fieldset' => array(), + 'legend' => array(), + 'Field', + '/legend', + array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')), + array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField1', 'disabled' => 'disabled', 'value' => '1')), + array('label' => array('for' => 'ModelField1')), + 'A', '/label', - '/div', - - array('div' => array('class' => 'checkbox')), - array('input' => array( - 'type' => 'checkbox', 'name' => 'Model[tags][]', - 'value' => 'Array', 'id' => 'ModelTagsArray' - )), - array('label' => array('for' => 'ModelTagsArray')), - 'Array', + array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField2', 'value' => '2')), + array('label' => array('for' => 'ModelField2')), + 'B', + '/label', + array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField3', 'value' => '3')), + array('label' => array('for' => 'ModelField3')), + 'C', '/label', + '/fieldset', '/div' ); $this->assertTags($result, $expected); + } - $result = $this->Form->select( - 'Model.multi_field', - array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'), - array('multiple' => 'checkbox') +/** + * test disabling the hidden input for radio buttons + * + * @return void + */ + public function testRadioHiddenInputDisabling() { + $this->markTestIncomplete('Need to revisit once models work again.'); + $result = $this->Form->input('Model.1.field', array( + 'type' => 'radio', + 'options' => array('option A'), + 'hiddenField' => false + ) ); $expected = array( - 'input' => array( - 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField' - ), - array('div' => array('class' => 'checkbox')), - array('input' => array( - 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]', - 'value' => 'a+', 'id' => 'ModelMultiFieldA2' - )), - array('label' => array('for' => 'ModelMultiFieldA2')), - 'first', - '/label', - '/div', - array('div' => array('class' => 'checkbox')), - array('input' => array( - 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]', - 'value' => 'a++', 'id' => 'ModelMultiFieldA1' - )), - array('label' => array('for' => 'ModelMultiFieldA1')), - 'second', - '/label', - '/div', - array('div' => array('class' => 'checkbox')), - array('input' => array( - 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]', - 'value' => 'a+++', 'id' => 'ModelMultiFieldA' - )), - array('label' => array('for' => 'ModelMultiFieldA')), - 'third', + 'div' => array('class' => 'input radio'), + 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0'), + 'label' => array('for' => 'Model1Field0'), + 'option A', '/label', '/div' ); - $this->assertTags($result, $expected); - $result = $this->Form->select( - 'Model.multi_field', - array('a>b' => 'first', 'a 'second', 'a"b' => 'third'), - array('multiple' => 'checkbox') - ); + $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false)); $expected = array( - 'input' => array( - 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField' - ), - array('div' => array('class' => 'checkbox')), - array('input' => array( - 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]', - 'value' => 'a>b', 'id' => 'ModelMultiFieldAB2' - )), - array('label' => array('for' => 'ModelMultiFieldAB2')), - 'first', - '/label', - '/div', - array('div' => array('class' => 'checkbox')), - array('input' => array( - 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]', - 'value' => 'a<b', 'id' => 'ModelMultiFieldAB1' - )), - array('label' => array('for' => 'ModelMultiFieldAB1')), - 'second', - '/label', - '/div', - array('div' => array('class' => 'checkbox')), - array('input' => array( - 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]', - 'value' => 'a"b', 'id' => 'ModelMultiFieldAB' - )), - array('label' => array('for' => 'ModelMultiFieldAB')), - 'third', - '/label', - '/div' + 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0'), + 'label' => array('for' => 'Model1Field0'), + 'option A', + '/label' ); $this->assertTags($result, $expected); } /** - * test multiple checkboxes with div styles. + * test adding an empty option for radio buttons * * @return void */ - public function testSelectMultipleCheckboxDiv() { + public function testRadioAddEmptyOption() { $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->select( - 'Model.tags', - array('first', 'second'), - array('multiple' => 'checkbox', 'class' => 'my-class') - ); + $result = $this->Form->input('Model.1.field', array( + 'type' => 'radio', + 'options' => array('option A'), + 'empty' => true, + 'hiddenField' => false + )); $expected = array( - 'input' => array( - 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '', 'id' => 'ModelTags' - ), - array('div' => array('class' => 'my-class')), - array('input' => array( - 'type' => 'checkbox', 'name' => 'Model[tags][]', - 'value' => '0', 'id' => 'ModelTags0' - )), - array('label' => array('for' => 'ModelTags0')), 'first', '/label', - '/div', - - array('div' => array('class' => 'my-class')), - array('input' => array( - 'type' => 'checkbox', 'name' => 'Model[tags][]', - 'value' => '1', 'id' => 'ModelTags1' - )), - array('label' => array('for' => 'ModelTags1')), 'second', '/label', + 'div' => array('class' => 'input radio'), + 'fieldset' => array(), + 'legend' => array(), + 'Field', + '/legend', + array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '', 'id' => 'Model1Field')), + array('label' => array('for' => 'Model1Field')), + __('empty'), + '/label', + array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0')), + array('label' => array('for' => 'Model1Field0')), + 'option A', + '/label', + '/fieldset', '/div' ); $this->assertTags($result, $expected); - $result = $this->Form->input('Model.tags', array( - 'options' => array('first', 'second'), - 'multiple' => 'checkbox', - 'class' => 'my-class', - 'div' => false, - 'label' => false - )); - $this->assertTags($result, $expected); - - $Contact->validationErrors['tags'] = 'Select atleast one option'; - $result = $this->Form->input('Contact.tags', array( - 'options' => array('one'), - 'multiple' => 'checkbox', - 'label' => false, - 'div' => false + $result = $this->Form->input('Model.1.field', array( + 'type' => 'radio', + 'options' => array('option A'), + 'empty' => 'CustomEmptyLabel', + 'hiddenField' => false )); $expected = array( - 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[tags]', 'value' => '', 'id' => 'ContactTags'), - array('div' => array('class' => 'checkbox form-error')), - array('input' => array('type' => 'checkbox', 'name' => 'Contact[tags][]', 'value' => '0', 'id' => 'ContactTags0')), - array('label' => array('for' => 'ContactTags0')), - 'one', - '/label', + 'div' => array('class' => 'input radio'), + 'fieldset' => array(), + 'legend' => array(), + 'Field', + '/legend', + array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '', 'id' => 'Model1Field')), + array('label' => array('for' => 'Model1Field')), + 'CustomEmptyLabel', + '/label', + array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0')), + array('label' => array('for' => 'Model1Field0')), + 'option A', + '/label', + '/fieldset', '/div' ); + $this->assertTags($result, $expected); - $result = $this->Form->input('Contact.tags', array( - 'options' => array('one'), - 'multiple' => 'checkbox', - 'class' => 'mycheckbox', - 'label' => false, - 'div' => false + $result = $this->Form->input('Model.1.field', array( + 'type' => 'radio', + 'options' => array('option A'), + 'empty' => false, + 'hiddenField' => false )); - $expected = array( - 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[tags]', 'value' => '', 'id' => 'ContactTags'), - array('div' => array('class' => 'mycheckbox form-error')), - array('input' => array('type' => 'checkbox', 'name' => 'Contact[tags][]', 'value' => '0', 'id' => 'ContactTags0')), - array('label' => array('for' => 'ContactTags0')), - 'one', - '/label', - '/div' - ); - $this->assertTags($result, $expected); + $this->assertTextNotContains('"Model1Field"', $result); } /** - * Checks the security hash array generated for multiple-input checkbox elements + * Test that radio() accepts an array for label * * @return void */ - public function testSelectMultipleCheckboxSecurity() { + public function testRadioLabelArray() { $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->request->params['_csrfToken'] = 'testKey'; - $this->assertEquals(array(), $this->Form->fields); - - $result = $this->Form->select( - 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'), - array('multiple' => 'checkbox') + $result = $this->Form->input('Model.field', array( + 'type' => 'radio', + 'legend' => false, + 'label' => array( + 'class' => 'checkbox float-left', + ), + 'options' => array('1' => 'Option A', '2' => 'Option B.') + )); + $this->assertTextContains( + '', + $result ); - $this->assertEquals(array('Model.multi_field'), $this->Form->fields); + } - $result = $this->Form->secure($this->Form->fields); - $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A'; - $this->assertRegExp('/"' . $key . '"/', $result); +/** + * Test that label id's match the input element id's when radio is called after create(). + * + * @return void + */ + public function testRadioWithCreate() { + $this->markTestIncomplete('Need to revisit once models work again.'); + $this->Form->create('Model'); + $result = $this->Form->radio('recipient', + array('1' => '1', '2' => '2', '3' => '3'), + array('legend' => false, 'value' => '1') + ); + $this->assertTextNotContains( + '', + $result + ); + $this->assertTextContains( + '', + $result + ); } /** - * Multiple select elements should always be secured as they always participate - * in the POST data. + * testDomIdSuffix method * * @return void */ - public function testSelectMultipleSecureWithNoOptions() { + public function testDomIdSuffix() { $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->request->params['_csrfToken'] = 'testkey'; - $this->assertEquals(array(), $this->Form->fields); + $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs'); + $this->assertEquals('1StringWith1$-dollarSigns', $result); - $this->Form->select( - 'Model.select', - array(), - array('multiple' => true) - ); - $this->assertEquals(array('Model.select'), $this->Form->fields); + $result = $this->Form->domIdSuffix(''); + $this->assertEquals('AbcX=FooY=Bar', $result); + + $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs', 'xhtml'); + $this->assertEquals('1StringWith1-dollarSigns', $result); + + $result = $this->Form->domIdSuffix('', 'xhtml'); + $this->assertEquals('AbcXFooYBar', $result); } + /** - * When a select box has no options it should not be added to the fields list - * as it always fail post validation. + * testDomIdSuffixCollisionResolvement() * * @return void */ - public function testSelectNoSecureWithNoOptions() { + public function testDomIdSuffixCollisionResolvement() { $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->request->params['_csrfToken'] = 'testkey'; - $this->assertEquals(array(), $this->Form->fields); + $result = $this->Form->domIdSuffix('a>b'); + $this->assertEquals('AB', $result); - $this->Form->select( - 'Model.select', - array() - ); - $this->assertEquals(array(), $this->Form->fields); + $result = $this->Form->domIdSuffix('aassertEquals('AB1', $result); - $this->Form->select( - 'Model.select', - array(), - array('empty' => true) - ); - $this->assertEquals(array('Model.select'), $this->Form->fields); + $result = $this->Form->domIdSuffix('a\'b'); + $this->assertEquals('AB2', $result); + + $result = $this->Form->domIdSuffix('1 string with 1$-dollar', 'xhtml'); + $this->assertEquals('1StringWith1-dollar', $result); + + $result = $this->Form->domIdSuffix('1 string with 1€-dollar', 'xhtml'); + $this->assertEquals('1StringWith1-dollar1', $result); + + $result = $this->Form->domIdSuffix('1 string with 1$-dollar', 'xhtml'); + $this->assertEquals('1StringWith1-dollar2', $result); } /** - * testInputMultipleCheckboxes method + * testSelect method * - * test input() resulting in multi select elements being generated. + * Test select element generation. * * @return void */ - public function testInputMultipleCheckboxes() { + public function testSelect() { $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->input('Model.multi_field', array( - 'options' => array('first', 'second', 'third'), - 'multiple' => 'checkbox' - )); + $result = $this->Form->select('Model.field', array()); $expected = array( - array('div' => array('class' => 'input select')), - array('label' => array('for' => 'ModelMultiField')), - 'Multi Field', - '/label', - 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'), - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')), - array('label' => array('for' => 'ModelMultiField0')), - 'first', - '/label', - '/div', - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')), - array('label' => array('for' => 'ModelMultiField1')), - 'second', - '/label', - '/div', - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')), - array('label' => array('for' => 'ModelMultiField2')), - 'third', - '/label', - '/div', - '/div' + 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'), + array('option' => array('value' => '')), + '/option', + '/select' ); $this->assertTags($result, $expected); - $result = $this->Form->input('Model.multi_field', array( - 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'), - 'multiple' => 'checkbox' - )); + $this->Form->request->data = array('Model' => array('field' => 'value')); + $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad')); $expected = array( - array('div' => array('class' => 'input select')), - array('label' => array('for' => 'ModelMultiField')), - 'Multi Field', - '/label', - 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'), - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')), - array('label' => array('for' => 'ModelMultiFieldA')), - 'first', - '/label', - '/div', - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')), - array('label' => array('for' => 'ModelMultiFieldB')), - 'second', - '/label', - '/div', - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')), - array('label' => array('for' => 'ModelMultiFieldC')), - 'third', - '/label', - '/div', - '/div' + 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'), + array('option' => array('value' => '')), + '/option', + array('option' => array('value' => 'value', 'selected' => 'selected')), + 'good', + '/option', + array('option' => array('value' => 'other')), + 'bad', + '/option', + '/select' ); $this->assertTags($result, $expected); - $result = $this->Form->input('Model.multi_field', array( - 'options' => array('1' => 'first'), - 'multiple' => 'checkbox', - 'label' => false, - 'div' => false - )); + $this->Form->request->data = array(); + $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad')); $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'), - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')), - array('label' => array('for' => 'ModelMultiField1')), - 'first', - '/label', - '/div' + 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'), + array('option' => array('value' => '')), + '/option', + array('option' => array('value' => 'value')), + 'good', + '/option', + array('option' => array('value' => 'other')), + 'bad', + '/option', + '/select' ); $this->assertTags($result, $expected); - $result = $this->Form->input('Model.multi_field', array( - 'options' => array('2' => 'second'), - 'multiple' => 'checkbox', - 'label' => false, - 'div' => false - )); + $result = $this->Form->select( + 'Model.field', array('first' => 'first "html" ', 'second' => 'value'), + array('empty' => false) + ); $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'), - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')), - array('label' => array('for' => 'ModelMultiField2')), - 'second', - '/label', - '/div' + 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'), + array('option' => array('value' => 'first')), + 'first "html" <chars>', + '/option', + array('option' => array('value' => 'second')), + 'value', + '/option', + '/select' ); $this->assertTags($result, $expected); - } -/** - * testSelectHiddenFieldOmission method - * - * test that select() with 'hiddenField' => false omits the hidden field - * - * @return void - */ - public function testSelectHiddenFieldOmission() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->select('Model.multi_field', - array('first', 'second'), - array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null) + $result = $this->Form->select( + 'Model.field', + array('first' => 'first "html" ', 'second' => 'value'), + array('escape' => false, 'empty' => false) ); $expected = array( - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')), - array('label' => array('for' => 'ModelMultiField0')), - 'first', - '/label', - '/div', - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')), - array('label' => array('for' => 'ModelMultiField1')), - 'second', - '/label', - '/div' + 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'), + array('option' => array('value' => 'first')), + 'first "html" ', + '/option', + array('option' => array('value' => 'second')), + 'value', + '/option', + '/select' ); $this->assertTags($result, $expected); - $result = $this->Form->input('Model.multi_field', array( - 'options' => array('first', 'second'), - 'multiple' => 'checkbox', - 'hiddenField' => false - )); - $expected = array( - array('div' => array('class' => 'input select')), - array('label' => array('for' => 'ModelMultiField')), - 'Multi Field', - '/label', - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')), - array('label' => array('for' => 'ModelMultiField0')), - 'first', - '/label', - '/div', - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')), - array('label' => array('for' => 'ModelMultiField1')), - 'second', - '/label', - '/div', - '/div' + $options = array( + array('value' => 'first', 'name' => 'First'), + array('value' => 'first', 'name' => 'Another First'), + ); + $result = $this->Form->select( + 'Model.field', + $options, + array('escape' => false, 'empty' => false) ); - $this->assertTags($result, $expected); - } - -/** - * test that select() with multiple = checkbox works with overriding name attribute. - * - * @return void - */ - public function testSelectCheckboxMultipleOverrideName() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->input('category', array( - 'type' => 'select', - 'multiple' => 'checkbox', - 'name' => 'fish', - 'options' => array('1', '2'), - 'div' => false, - 'label' => false, - )); $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'), - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'Category0')), - array('label' => array('for' => 'Category0')), '1', '/label', - '/div', - array('div' => array('class' => 'checkbox')), - array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'Category1')), - array('label' => array('for' => 'Category1')), '2', '/label', - '/div' + 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'), + array('option' => array('value' => 'first')), + 'First', + '/option', + array('option' => array('value' => 'first')), + 'Another First', + '/option', + '/select' ); $this->assertTags($result, $expected); - } -/** - * Test that 'id' overrides all the checkbox id's as well. - * - * @return void - */ - public function testSelectCheckboxMultipleId() { - $this->markTestIncomplete('Need to revisit once models work again.'); + $this->Form->request->data = array('Model' => array('contact_id' => 228)); $result = $this->Form->select( - 'Model.multi_field', - array('first', 'second', 'third'), - array('multiple' => 'checkbox', 'id' => 'CustomId') + 'Model.contact_id', + array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'), + array('escape' => false, 'empty' => 'pick something') ); $expected = array( - 'input' => array( - 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'CustomId' - ), - array('div' => array('class' => 'checkbox')), - array('input' => array( - 'type' => 'checkbox', 'name' => 'Model[multi_field][]', - 'value' => '0', 'id' => 'CustomId0' - )), - array('label' => array('for' => 'CustomId0')), - 'first', - '/label', - '/div', - array('div' => array('class' => 'checkbox')), - array('input' => array( - 'type' => 'checkbox', 'name' => 'Model[multi_field][]', - 'value' => '1', 'id' => 'CustomId1' - )), - array('label' => array('for' => 'CustomId1')), - 'second', - '/label', - '/div', - array('div' => array('class' => 'checkbox')), - array('input' => array( - 'type' => 'checkbox', 'name' => 'Model[multi_field][]', - 'value' => '2', 'id' => 'CustomId2' - )), - array('label' => array('for' => 'CustomId2')), - 'third', - '/label', - '/div' + 'select' => array('name' => 'Model[contact_id]', 'id' => 'ModelContactId'), + array('option' => array('value' => '')), 'pick something', '/option', + array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option', + array('option' => array('value' => '228-1')), '228-1 value', '/option', + array('option' => array('value' => '228-2')), '228-2 value', '/option', + '/select' ); $this->assertTags($result, $expected); - } -/** - * testCheckbox method - * - * Test generation of checkboxes - * - * @return void - */ - public function testCheckbox() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->checkbox('Model.field'); + $this->Form->request->data['Model']['field'] = 0; + $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes')); $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'), - array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField')) + 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'), + array('option' => array('value' => '')), '/option', + array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option', + array('option' => array('value' => '1')), 'Yes', '/option', + '/select' ); $this->assertTags($result, $expected); - $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue')); + $this->Form->request->data['Model']['field'] = 50; + $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty')); $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'theID_'), - array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => 'myvalue', 'id' => 'theID')) + 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'), + array('option' => array('value' => '')), '/option', + array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option', + array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option', + '/select' ); $this->assertTags($result, $expected); - $Contact->validationErrors['field'] = 1; - $this->Form->request->data['Contact']['field'] = 'myvalue'; - $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue')); + $result = $this->Form->select('Contact.required_one', array('option A')); $expected = array( - 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[field]', 'value' => '0', 'id' => 'theID_'), - array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error')) + 'select' => array( + 'name' => 'data[Contact][required_one]', + 'id' => 'ContactRequiredOne', + 'required' => 'required' + ), + array('option' => array('value' => '')), '/option', + array('option' => array('value' => '0')), 'option A', '/option', + '/select' ); $this->assertTags($result, $expected); - $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue')); + $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true)); $expected = array( - 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[field]', 'value' => '0', 'id' => 'ContactField_'), - array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error')) - ); - $this->assertTags($result, $expected); - - $this->Form->request->data['Contact']['field'] = ''; - $result = $this->Form->checkbox('Contact.field', array('id' => 'theID')); - $expected = array( - 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[field]', 'value' => '0', 'id' => 'theID_'), - array('input' => array('type' => 'checkbox', 'name' => 'Contact[field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error')) - ); - $this->assertTags($result, $expected); - - $Contact->validationErrors = array(); - $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue')); - $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'Contact[field]', 'value' => '0', 'id' => 'ContactField_'), - array('input' => array('type' => 'checkbox', 'name' => 'Contact[field]', 'value' => 'myvalue', 'id' => 'ContactField')) + 'select' => array( + 'name' => 'data[Contact][required_one]', + 'id' => 'ContactRequiredOne', + 'disabled' => 'disabled' + ), + array('option' => array('value' => '')), '/option', + array('option' => array('value' => '0')), 'option A', '/option', + '/select' ); $this->assertTags($result, $expected); + } - $this->Form->request->data['Contact']['published'] = 1; - $result = $this->Form->checkbox('Contact.published', array('id' => 'theID')); - $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'Contact[published]', 'value' => '0', 'id' => 'theID_'), - array('input' => array('type' => 'checkbox', 'name' => 'Contact[published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked')) +/** + * test that select() with optiongroups listens to the escape param. + * + * @return void + */ + public function testSelectOptionGroupEscaping() { + $this->markTestIncomplete('Need to revisit once models work again.'); + $options = array( + '>< Key' => array( + 1 => 'One', + 2 => 'Two' + ) ); - $this->assertTags($result, $expected); - - $this->Form->request->data['Contact']['published'] = 0; - $result = $this->Form->checkbox('Contact.published', array('id' => 'theID')); + $result = $this->Form->select('Model.field', $options, array('empty' => false)); $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'Contact[published]', 'value' => '0', 'id' => 'theID_'), - array('input' => array('type' => 'checkbox', 'name' => 'Contact[published]', 'value' => '1', 'id' => 'theID')) + 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'), + 'optgroup' => array('label' => '>< Key'), + array('option' => array('value' => '1')), 'One', '/option', + array('option' => array('value' => '2')), 'Two', '/option', + '/optgroup', + '/select' ); $this->assertTags($result, $expected); - $result = $this->Form->checkbox('Model.CustomField.1.value'); - $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'Model[CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'), - array('input' => array('type' => 'checkbox', 'name' => 'Model[CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value')) + $options = array( + '>< Key' => array( + 1 => 'One', + 2 => 'Two' + ) ); - $this->assertTags($result, $expected); - - $result = $this->Form->checkbox('CustomField.1.value'); + $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false)); $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'CustomField[1][value]', 'value' => '0', 'id' => 'CustomField1Value_'), - array('input' => array('type' => 'checkbox', 'name' => 'CustomField[1][value]', 'value' => '1', 'id' => 'CustomField1Value')) + 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'), + 'optgroup' => array('label' => '>< Key'), + array('option' => array('value' => '1')), 'One', '/option', + array('option' => array('value' => '2')), 'Two', '/option', + '/optgroup', + '/select' ); $this->assertTags($result, $expected); } /** - * test checkbox() with a custom name attribute + * Tests that FormHelper::select() allows null to be passed in the $attributes parameter * * @return void */ - public function testCheckboxCustomNameAttribute() { + public function testSelectWithNullAttributes() { $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->checkbox('Test.test', array('name' => 'myField')); + $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false)); $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'), - array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest')) - ); + 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'), + array('option' => array('value' => '0')), + 'first', + '/option', + array('option' => array('value' => '1')), + 'second', + '/option', + '/select' + ); $this->assertTags($result, $expected); } /** - * test the checked option for checkboxes. + * testNestedSelect method + * + * test select element generation with optgroups * * @return void */ - public function testCheckboxCheckedOption() { + public function testNestedSelect() { $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->checkbox('Model.field', array('checked' => 'checked')); - $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'), - array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')) - ); - $this->assertTags($result, $expected); - - $result = $this->Form->checkbox('Model.field', array('checked' => 1)); - $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'), - array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')) + $result = $this->Form->select( + 'Model.field', + array(1 => 'One', 2 => 'Two', 'Three' => array( + 3 => 'Three', 4 => 'Four', 5 => 'Five' + )), array('empty' => false) ); - $this->assertTags($result, $expected); - - $result = $this->Form->checkbox('Model.field', array('checked' => true)); $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'), - array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')) - ); + 'select' => array('name' => 'Model[field]', + 'id' => 'ModelField'), + array('option' => array('value' => 1)), + 'One', + '/option', + array('option' => array('value' => 2)), + 'Two', + '/option', + array('optgroup' => array('label' => 'Three')), + array('option' => array('value' => 4)), + 'Four', + '/option', + array('option' => array('value' => 5)), + 'Five', + '/option', + '/optgroup', + '/select' + ); $this->assertTags($result, $expected); - $result = $this->Form->checkbox('Model.field', array('checked' => false)); - $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'), - array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField')) + $result = $this->Form->select( + 'Model.field', + array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')), + array('showParents' => true, 'empty' => false) ); - $this->assertTags($result, $expected); - $this->Form->request->data['Model']['field'] = 1; - $result = $this->Form->checkbox('Model.field', array('checked' => false)); $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'), - array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField')) + 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'), + array('option' => array('value' => 1)), + 'One', + '/option', + array('option' => array('value' => 2)), + 'Two', + '/option', + array('optgroup' => array('label' => 'Three')), + array('option' => array('value' => 3)), + 'Three', + '/option', + array('option' => array('value' => 4)), + 'Four', + '/option', + '/optgroup', + '/select' ); $this->assertTags($result, $expected); } /** - * Test that disabled attribute works on both the checkbox and hidden input. + * testSelectMultiple method + * + * test generation of multiple select elements * * @return void */ - public function testCheckboxDisabling() { + public function testSelectMultiple() { $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled')); + $options = array('first', 'second', 'third'); + $result = $this->Form->select( + 'Model.multi_field', $options, array('multiple' => true) + ); $expected = array( - array('input' => array('type' => 'hidden', 'name' => 'Account[show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')), - array('input' => array('type' => 'checkbox', 'name' => 'Account[show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled')) + 'input' => array( + 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField_' + ), + 'select' => array( + 'name' => 'Model[multi_field][]', + 'id' => 'ModelMultiField', 'multiple' => 'multiple' + ), + array('option' => array('value' => '0')), + 'first', + '/option', + array('option' => array('value' => '1')), + 'second', + '/option', + array('option' => array('value' => '2')), + 'third', + '/option', + '/select' ); $this->assertTags($result, $expected); - $result = $this->Form->checkbox('Account.show_name', array('disabled' => false)); + $result = $this->Form->select( + 'Model.multi_field', $options, array('multiple' => 'multiple') + ); $expected = array( - array('input' => array('type' => 'hidden', 'name' => 'Account[show_name]', 'value' => '0', 'id' => 'AccountShowName_')), - array('input' => array('type' => 'checkbox', 'name' => 'Account[show_name]', 'value' => '1', 'id' => 'AccountShowName')) + 'input' => array( + 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField_' + ), + 'select' => array( + 'name' => 'Model[multi_field][]', + 'id' => 'ModelMultiField', 'multiple' => 'multiple' + ), + array('option' => array('value' => '0')), + 'first', + '/option', + array('option' => array('value' => '1')), + 'second', + '/option', + array('option' => array('value' => '2')), + 'third', + '/option', + '/select' ); $this->assertTags($result, $expected); - } /** - * Test that the hidden input for checkboxes can be omitted or set to a - * specific value. + * Test that a checkbox can have 0 for the value and 1 for the hidden input. * * @return void */ - public function testCheckboxHiddenField() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $result = $this->Form->input('UserForm.something', array( + public function testCheckboxZeroValue() { + $result = $this->Form->input('User.get_spam', array( 'type' => 'checkbox', - 'hiddenField' => false + 'value' => '0', + 'hiddenField' => '1', )); $expected = array( 'div' => array('class' => 'input checkbox'), array('input' => array( - 'type' => 'checkbox', 'name' => 'UserForm[something]', - 'value' => '1', 'id' => 'UserFormSomething' + 'type' => 'hidden', 'name' => 'data[User][get_spam]', + 'value' => '1', 'id' => 'UserGetSpam_' )), - 'label' => array('for' => 'UserFormSomething'), - 'Something', + array('input' => array( + 'type' => 'checkbox', 'name' => 'data[User][get_spam]', + 'value' => '0', 'id' => 'UserGetSpam' + )), + 'label' => array('for' => 'UserGetSpam'), + 'Get Spam', '/label', '/div' ); $this->assertTags($result, $expected); - - $result = $this->Form->input('UserForm.something', array( - 'type' => 'checkbox', - 'value' => 'Y', - 'hiddenField' => 'N', - )); - $expected = array( - 'div' => array('class' => 'input checkbox'), - array('input' => array( - 'type' => 'hidden', 'name' => 'UserForm[something]', - 'value' => 'N', 'id' => 'UserFormSomething_' - )), - array('input' => array( - 'type' => 'checkbox', 'name' => 'UserForm[something]', - 'value' => 'Y', 'id' => 'UserFormSomething' - )), - 'label' => array('for' => 'UserFormSomething'), - 'Something', - '/label', - '/div' - ); - $this->assertTags($result, $expected); - } - -/** - * Test that a checkbox can have 0 for the value and 1 for the hidden input. - * - * @return void - */ - public function testCheckboxZeroValue() { - $result = $this->Form->input('User.get_spam', array( - 'type' => 'checkbox', - 'value' => '0', - 'hiddenField' => '1', - )); - $expected = array( - 'div' => array('class' => 'input checkbox'), - array('input' => array( - 'type' => 'hidden', 'name' => 'data[User][get_spam]', - 'value' => '1', 'id' => 'UserGetSpam_' - )), - array('input' => array( - 'type' => 'checkbox', 'name' => 'data[User][get_spam]', - 'value' => '0', 'id' => 'UserGetSpam' - )), - 'label' => array('for' => 'UserGetSpam'), - 'Get Spam', - '/label', - '/div' - ); - $this->assertTags($result, $expected); - } + } /** * testDateTime method @@ -6035,2482 +5340,3196 @@ public function testCheckboxZeroValue() { */ public function testDateTime() { $this->markTestIncomplete('Need to revisit once models work again.'); - extract($this->dateRegex); - - $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false)); - $now = strtotime('now'); + $result = $this->Form->select( + 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1)) + ); $expected = array( - array('select' => array('name' => 'Contact[date][day]', 'id' => 'ContactDateDay')), - $daysRegex, - array('option' => array('value' => date('d', $now), 'selected' => 'selected')), - date('j', $now), + 'input' => array( + 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField_' + ), + 'select' => array( + 'name' => 'Model[multi_field][]', 'id' => 'ModelMultiField', + 'multiple' => 'multiple' + ), + array('option' => array('value' => '0', 'selected' => 'selected')), + 'first', '/option', - '*/select', - '-', - array('select' => array('name' => 'Contact[date][month]', 'id' => 'ContactDateMonth')), - $monthsRegex, - array('option' => array('value' => date('m', $now), 'selected' => 'selected')), - date('F', $now), + array('option' => array('value' => '1', 'selected' => 'selected')), + 'second', '/option', - '*/select', - '-', - array('select' => array('name' => 'Contact[date][year]', 'id' => 'ContactDateYear')), - $yearsRegex, - array('option' => array('value' => date('Y', $now), 'selected' => 'selected')), - date('Y', $now), + array('option' => array('value' => '2')), + 'third', '/option', - '*/select', - array('select' => array('name' => 'Contact[date][hour]', 'id' => 'ContactDateHour')), - $hoursRegex, - array('option' => array('value' => date('h', $now), 'selected' => 'selected')), - date('g', $now), + '/select' + ); + $this->assertTags($result, $expected); + + $result = $this->Form->select( + 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1)) + ); + $expected = array( + 'select' => array( + 'name' => 'Model[multi_field]', 'id' => 'ModelMultiField' + ), + array('option' => array('value' => '0', 'selected' => 'selected')), + 'first', '/option', - '*/select', - ':', - array('select' => array('name' => 'Contact[date][min]', 'id' => 'ContactDateMin')), - $minutesRegex, - array('option' => array('value' => date('i', $now), 'selected' => 'selected')), - date('i', $now), + array('option' => array('value' => '1', 'selected' => 'selected')), + 'second', '/option', - '*/select', - ' ', - array('select' => array('name' => 'Contact[date][meridian]', 'id' => 'ContactDateMeridian')), - $meridianRegex, - array('option' => array('value' => date('a', $now), 'selected' => 'selected')), - date('a', $now), + array('option' => array('value' => '2')), + 'third', '/option', - '*/select' + '/select' ); $this->assertTags($result, $expected); - $result = $this->Form->dateTime('Contact.date', 'DMY', '12'); + $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy'); + $selected = array('2', '3x'); + $result = $this->Form->select( + 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected) + ); $expected = array( - array('select' => array('name' => 'Contact[date][day]', 'id' => 'ContactDateDay')), - $daysRegex, - array('option' => array('value' => '')), - '/option', - '*/select', - '-', - array('select' => array('name' => 'Contact[date][month]', 'id' => 'ContactDateMonth')), - $monthsRegex, - array('option' => array('value' => '')), - '/option', - '*/select', - '-', - array('select' => array('name' => 'Contact[date][year]', 'id' => 'ContactDateYear')), - $yearsRegex, - array('option' => array('value' => '')), + 'input' => array( + 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField_' + ), + 'select' => array( + 'name' => 'Model[multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField' + ), + array('option' => array('value' => '1')), + 'One', '/option', - '*/select', - array('select' => array('name' => 'Contact[date][hour]', 'id' => 'ContactDateHour')), - $hoursRegex, - array('option' => array('value' => '')), + array('option' => array('value' => '2', 'selected' => 'selected')), + 'Two', '/option', - '*/select', - ':', - array('select' => array('name' => 'Contact[date][min]', 'id' => 'ContactDateMin')), - $minutesRegex, - array('option' => array('value' => '')), + array('option' => array('value' => '3')), + 'Three', '/option', - '*/select', - ' ', - array('select' => array('name' => 'Contact[date][meridian]', 'id' => 'ContactDateMeridian')), - $meridianRegex, - array('option' => array('value' => '')), + array('option' => array('value' => '3x', 'selected' => 'selected')), + 'Stringy', '/option', - '*/select' + '/select' ); $this->assertTags($result, $expected); - $this->assertNotRegExp('/]+value=""[^<>]+selected="selected"[^>]*>/', $result); - - $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false)); - $this->assertTags($result, $expected); - $this->assertNotRegExp('/]+value=""[^<>]+selected="selected"[^>]*>/', $result); - $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => '')); + $result = $this->Form->select('Contact.required_one', array( + '1' => 'option A', + '2' => 'option B' + ), array('multiple' => true)); + $expected = array( + 'input' => array( + 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_' + ), + 'select' => array( + 'name' => 'data[Contact][required_one][]', + 'id' => 'ContactRequiredOne', + 'required' => 'required', + 'multiple' => 'multiple' + ), + array('option' => array('value' => '1')), 'option A', '/option', + array('option' => array('value' => '2')), 'option B', '/option', + '/select' + ); $this->assertTags($result, $expected); - $this->assertNotRegExp('/]+value=""[^<>]+selected="selected"[^>]*>/', $result); - $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => '')); + $result = $this->Form->select( + 'Model.multi_field', + array('a>b' => 'first', 'a 'second', 'a"b' => 'third'), + array('multiple' => true) + ); $expected = array( - array('select' => array('name' => 'Contact[date][day]', 'id' => 'ContactDateDay')), - $daysRegex, - array('option' => array('value' => '')), - '/option', - '*/select', - '-', - array('select' => array('name' => 'Contact[date][month]', 'id' => 'ContactDateMonth')), - $monthsRegex, - array('option' => array('value' => '')), - '/option', - '*/select', - '-', - array('select' => array('name' => 'Contact[date][year]', 'id' => 'ContactDateYear')), - $yearsRegex, - array('option' => array('value' => '')), - '/option', - '*/select', - array('select' => array('name' => 'Contact[date][hour]', 'id' => 'ContactDateHour')), - $hoursRegex, - array('option' => array('value' => '')), - '/option', - '*/select', - ':', - array('select' => array('name' => 'Contact[date][min]', 'id' => 'ContactDateMin')), - $minutesRegex, - array('option' => array('value' => '')), - '/option', - array('option' => array('value' => '00')), - '00', - '/option', - array('option' => array('value' => '05')), - '05', + 'input' => array( + 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', + 'id' => 'ModelMultiField_' + ), + array('select' => array('name' => 'data[Model][multi_field][]', + 'multiple' => 'multiple', 'id' => 'ModelMultiField' + )), + array('option' => array('value' => 'a>b')), + 'first', '/option', - array('option' => array('value' => '10')), - '10', + array('option' => array('value' => 'a<b')), + 'second', '/option', - '*/select', - ' ', - array('select' => array('name' => 'Contact[date][meridian]', 'id' => 'ContactDateMeridian')), - $meridianRegex, - array('option' => array('value' => '')), + array('option' => array( + 'value' => 'a"b' + )), + 'third', '/option', - '*/select' + '/select' ); $this->assertTags($result, $expected); - $this->assertNotRegExp('/]+value=""[^<>]+selected="selected"[^>]*>/', $result); - - $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => '')); - $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => '')); + } - $this->Form->request->data['Contact']['data'] = null; - $result = $this->Form->dateTime('Contact.date', 'DMY', '12'); +/** + * Test generating multiple select with disabled elements. + * + * @return void + */ + public function testSelectMultipleWithDisabledElements() { + $this->markTestIncomplete('Need to revisit once models work again.'); + $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy'); + $disabled = array(2, 3); + $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled)); $expected = array( - array('select' => array('name' => 'Contact[date][day]', 'id' => 'ContactDateDay')), - $daysRegex, - array('option' => array('value' => '')), - '/option', - '*/select', - '-', - array('select' => array('name' => 'Contact[date][month]', 'id' => 'ContactDateMonth')), - $monthsRegex, - array('option' => array('value' => '')), - '/option', - '*/select', - '-', - array('select' => array('name' => 'Contact[date][year]', 'id' => 'ContactDateYear')), - $yearsRegex, - array('option' => array('value' => '')), + 'input' => array( + 'type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '', 'id' => 'ContactMultiple_' + ), + 'select' => array( + 'name' => 'Contact[multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple' + ), + array('option' => array('value' => '1')), + 'One', '/option', - '*/select', - array('select' => array('name' => 'Contact[date][hour]', 'id' => 'ContactDateHour')), - $hoursRegex, - array('option' => array('value' => '')), + array('option' => array('value' => '2', 'disabled' => 'disabled')), + 'Two', '/option', - '*/select', - ':', - array('select' => array('name' => 'Contact[date][min]', 'id' => 'ContactDateMin')), - $minutesRegex, - array('option' => array('value' => '')), + array('option' => array('value' => '3', 'disabled' => 'disabled')), + 'Three', '/option', - '*/select', - ' ', - array('select' => array('name' => 'Contact[date][meridian]', 'id' => 'ContactDateMeridian')), - $meridianRegex, - array('option' => array('value' => '')), + array('option' => array('value' => '3x')), + 'Stringy', '/option', - '*/select' + '/select' ); $this->assertTags($result, $expected); - $this->assertNotRegExp('/]+value=""[^<>]+selected="selected"[^>]*>/', $result); - $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00'; - $now = strtotime($this->Form->data['Model']['field']); - $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false)); + $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy'); + $disabled = array('2', '3x'); + $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options)); $expected = array( - array('select' => array('name' => 'Model[field][day]', 'id' => 'ModelFieldDay')), - $daysRegex, - array('option' => array('value' => date('d', $now), 'selected' => 'selected')), - date('j', $now), - '/option', - '*/select', - '-', - array('select' => array('name' => 'Model[field][month]', 'id' => 'ModelFieldMonth')), - $monthsRegex, - array('option' => array('value' => date('m', $now), 'selected' => 'selected')), - date('F', $now), - '/option', - '*/select', - '-', - array('select' => array('name' => 'Model[field][year]', 'id' => 'ModelFieldYear')), - $yearsRegex, - array('option' => array('value' => date('Y', $now), 'selected' => 'selected')), - date('Y', $now), + array('div' => array('class' => 'input select')), + array('label' => array('for' => 'ContactMultiple')), + 'Multiple', + '/label', + 'input' => array( + 'type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '', 'id' => 'ContactMultiple_' + ), + 'select' => array( + 'name' => 'Contact[multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple' + ), + array('option' => array('value' => '1')), + 'One', '/option', - '*/select', - array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')), - $hoursRegex, - array('option' => array('value' => date('h', $now), 'selected' => 'selected')), - date('g', $now), + array('option' => array('value' => '2', 'disabled' => 'disabled')), + 'Two', '/option', - '*/select', - ':', - array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')), - $minutesRegex, - array('option' => array('value' => date('i', $now), 'selected' => 'selected')), - date('i', $now), + array('option' => array('value' => '3')), + 'Three', '/option', - '*/select', - ' ', - array('select' => array('name' => 'Model[field][meridian]', 'id' => 'ModelFieldMeridian')), - $meridianRegex, - array('option' => array('value' => date('a', $now), 'selected' => 'selected')), - date('a', $now), + array('option' => array('value' => '3x', 'disabled' => 'disabled')), + 'Stringy', '/option', - '*/select' + '/select', + '/div' ); $this->assertTags($result, $expected); - $selected = strtotime('2008-10-26 12:33:00'); - $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected)); - $this->assertRegExp('/]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result); - $this->assertRegExp('/]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result); - $this->assertRegExp('/]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result); - $this->assertRegExp('/]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result); - $this->assertRegExp('/]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result); - $this->assertRegExp('/]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result); - - $this->Form->create('Contact'); - $result = $this->Form->input('published'); - $now = strtotime('now'); + $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy'); + $disabled = true; + $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options)); $expected = array( - 'div' => array('class' => 'input date'), - 'label' => array('for' => 'ContactPublishedMonth'), - 'Published', + array('div' => array('class' => 'input select')), + array('label' => array('for' => 'ContactMultiple')), + 'Multiple', '/label', - array('select' => array('name' => 'Contact[published][month]', 'id' => 'ContactPublishedMonth')), - $monthsRegex, - array('option' => array('value' => date('m', $now), 'selected' => 'selected')), - date('F', $now), + 'input' => array( + 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_' + ), + 'select' => array( + 'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple' + ), + array('option' => array('value' => '1')), + 'One', '/option', - '*/select', - '-', - array('select' => array('name' => 'Contact[published][day]', 'id' => 'ContactPublishedDay')), - $daysRegex, - array('option' => array('value' => date('d', $now), 'selected' => 'selected')), - date('j', $now), + array('option' => array('value' => '2')), + 'Two', '/option', - '*/select', - '-', - array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')), - $yearsRegex, - array('option' => array('value' => date('Y', $now), 'selected' => 'selected')), - date('Y', $now), + array('option' => array('value' => '3')), + 'Three', '/option', - '*/select', + array('option' => array('value' => '3x')), + 'Stringy', + '/option', + '/select', '/div' ); $this->assertTags($result, $expected); + } - $result = $this->Form->input('published2', array('type' => 'date')); - $now = strtotime('now'); +/** + * Test generating select with disabled elements. + * + * @return void + */ + public function testSelectWithDisabledElements() { + $this->markTestIncomplete('Need to revisit once models work again.'); + $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy'); + $disabled = array(2, 3); + $result = $this->Form->select('Model.field', $options, array('disabled' => $disabled)); $expected = array( - 'div' => array('class' => 'input date'), - 'label' => array('for' => 'ContactPublished2Month'), - 'Published2', - '/label', - array('select' => array('name' => 'Contact[published2][month]', 'id' => 'ContactPublished2Month')), - $monthsRegex, - array('option' => array('value' => date('m', $now), 'selected' => 'selected')), - date('F', $now), + 'select' => array( + 'name' => 'data[Model][field]', 'id' => 'ModelField' + ), + array('option' => array('value' => '')), '/option', - '*/select', - '-', - array('select' => array('name' => 'Contact[published2][day]', 'id' => 'ContactPublished2Day')), - $daysRegex, - array('option' => array('value' => date('d', $now), 'selected' => 'selected')), - date('j', $now), + array('option' => array('value' => '1')), + 'One', '/option', - '*/select', - '-', - array('select' => array('name' => 'Contact[published2][year]', 'id' => 'ContactPublished2Year')), - $yearsRegex, - array('option' => array('value' => date('Y', $now), 'selected' => 'selected')), - date('Y', $now), + array('option' => array('value' => '2', 'disabled' => 'disabled')), + 'Two', '/option', - '*/select', - '/div' + array('option' => array('value' => '3', 'disabled' => 'disabled')), + 'Three', + '/option', + array('option' => array('value' => '3x')), + 'Stringy', + '/option', + '/select' ); $this->assertTags($result, $expected); - $this->Form->create('Contact'); - $result = $this->Form->input('published', array('monthNames' => false)); - $now = strtotime('now'); + $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy'); + $disabled = array('2', '3x'); + $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options)); $expected = array( - 'div' => array('class' => 'input date'), - 'label' => array('for' => 'ContactPublishedMonth'), - 'Published', + array('div' => array('class' => 'input select')), + array('label' => array('for' => 'ModelField')), + 'Field', '/label', - array('select' => array('name' => 'Contact[published][month]', 'id' => 'ContactPublishedMonth')), - 'preg:/(?: