_routes = Router::get(); Router::reset(); Router::connect('/{:controller}/{:action}/{:id}.{:type}', array('id' => null)); Router::connect('/{:controller}/{:action}/{:args}'); $request = new Request(); $request->params = array('controller' => 'posts', 'action' => 'index'); $request->persist = array('controller'); $this->context = new MockFormRenderer(compact('request')); $this->form = new Form(array('context' => $this->context)); $this->base = $this->context->request()->env('base') . '/'; } public function tearDown() { Router::reset(); foreach ($this->_routes as $route) { Router::connect($route); } } public function testFormCreation() { $result = $this->form->create(); $this->assertTags($result, array('form' => array( 'action' => "{$this->base}posts", 'method' => 'post' ))); $result = $this->form->create(null, array('method' => 'get')); $this->assertTags($result, array( 'form' => array('action' => "{$this->base}posts", 'method' => 'get') )); $result = $this->form->create(null, array('type' => 'file')); $this->assertTags($result, array('form' => array( 'action' => "{$this->base}posts", 'enctype' => 'multipart/form-data', 'method' => 'post' ))); $result = $this->form->create(null, array('method' => 'get', 'type' => 'file')); $this->assertTags($result, array('form' => array( 'action' => "{$this->base}posts", 'method' => 'post', 'enctype' => 'multipart/form-data' ))); $result = $this->form->create(null, array('id' => 'Registration')); $this->assertTags($result, array( 'form' => array( 'action' => "{$this->base}posts", 'method' => 'post', 'id' => 'Registration' ) )); } /** * Tests creating forms with non-browser compatible HTTP methods, required for REST interfaces. */ public function testRestFormCreation() { $result = $this->form->create(null, array('action' => 'delete', 'method' => 'delete')); $this->assertTags($result, array( 'form' => array( 'action' => "{$this->base}posts/delete", 'method' => 'post' ), 'input' => array('type' => "hidden", 'name' => '_method', 'value' => 'DELETE') )); $result = $this->form->create(null, array('method' => 'put', 'type' => 'file')); $this->assertTags($result, array( 'form' => array( 'action' => "{$this->base}posts", 'method' => 'post', 'enctype' => 'multipart/form-data' ), 'input' => array('type' => "hidden", 'name' => '_method', 'value' => 'PUT') )); $record = new Record(array('exists' => true, 'model' => $this->_model)); $result = $this->form->create($record); $this->assertTags($result, array( 'form' => array('action' => "{$this->base}posts", 'method' => 'post'), 'input' => array('type' => "hidden", 'name' => '_method', 'value' => 'PUT') )); } public function testFormCreationWithBinding() { $record = new Record(array('model' => $this->_model, 'data' => array( 'id' => '5', 'author_id' => '2', 'title' => 'This is a saved post', 'body' => 'This is the body of the saved post' ))); $this->assertTags($this->form->create($record), array( 'form' => array('action' => "{$this->base}posts", 'method' => 'post') )); } /** * Ensures that password fields aren't rendered with pre-populated values from bound record or * document objects. */ public function testPasswordWithBindingValue() { $this->form->create(new Record(array( 'model' => $this->_model, 'data' => array('pass' => 'foobar') ))); $result = $this->form->password('pass'); $this->assertTags($result, array( 'input' => array('type' => 'password', 'name' => 'pass', 'id' => 'MockFormPostPass') )); } public function testFormDataBinding() { try { MockFormPost::config(array('meta' => array('connection' => false))); } catch (Exception $e) { MockFormPost::config(array('meta' => array('connection' => false))); } $record = new Record(array('model' => $this->_model, 'data' => array( 'id' => '5', 'author_id' => '2', 'title' => 'This is a saved post', 'body' => 'This is the body of the saved post', 'zeroInt' => 0, 'zeroString' => "0" ))); $result = $this->form->create($record); $this->assertTags($result, array( 'form' => array('action' => "{$this->base}posts", 'method' => 'post') )); $result = $this->form->text('title'); $this->assertTags($result, array('input' => array( 'type' => 'text', 'name' => 'title', 'value' => 'This is a saved post', 'id' => 'MockFormPostTitle' ))); $result = $this->form->text('zeroInt'); $this->assertTags($result, array('input' => array( 'type' => 'text', 'name' => 'zeroInt', 'value' => '0', 'id' => 'MockFormPostZeroInt' ))); $result = $this->form->text('zeroString'); $this->assertTags($result, array('input' => array( 'type' => 'text', 'name' => 'zeroString', 'value' => '0', 'id' => 'MockFormPostZeroString' ))); $this->assertEqual('', $this->form->end()); $this->assertTags($this->form->text('title'), array('input' => array( 'type' => 'text', 'name' => 'title', 'id' => 'Title' ))); } public function testTextBox() { $result = $this->form->text('foo'); $this->assertTags($result, array('input' => array( 'type' => 'text', 'name' => 'foo', 'id' => 'Foo' ))); } public function testElementsWithDefaultConfiguration() { $this->form = new Form(array( 'context' => new MockFormRenderer(), 'base' => array('class' => 'editable') )); $result = $this->form->text('foo'); $this->assertTags($result, array('input' => array( 'type' => 'text', 'name' => 'foo', 'class' => 'editable', 'id' => 'Foo' ))); $this->form->config(array('base' => array('maxlength' => 255))); $result = $this->form->text('foo'); $this->assertTags($result, array('input' => array( 'type' => 'text', 'name' => 'foo', 'class' => 'editable', 'maxlength' => '255', 'id' => 'Foo' ))); $this->form->config(array('text' => array('class' => 'locked'))); $result = $this->form->text('foo'); $this->assertTags($result, array('input' => array( 'type' => 'text', 'name' => 'foo', 'class' => 'locked', 'maxlength' => '255', 'id' => 'Foo' ))); $result = $this->form->config(); $expected = array( 'base' => array('class' => 'editable', 'maxlength' => 255), 'text' => array('class' => 'locked'), 'textarea' => array(), 'templates' => array('create' => 'form', 'end' => 'form-end'), 'attributes' => array( 'id' => $result['attributes']['id'], 'name' => $result['attributes']['name'] ) ); $this->assertEqual($expected, $result); $this->assertTrue(is_callable($result['attributes']['id'])); $this->assertTrue(is_callable($result['attributes']['name'])); } public function testFormElementWithDefaultValue() { $result = $this->form->text('foo', array('default' => 'Message here')); $this->assertTags($result, array('input' => array( 'type' => 'text', 'name' => 'foo', 'value' => 'Message here', 'id' => 'Foo' ))); $result = $this->form->text('foo', array( 'default' => 'Message here', 'value' => 'My Name Is Jonas', 'id' => 'Foo' )); $this->assertTags($result, array('input' => array( 'type' => 'text', 'name' => 'foo', 'value' => 'My Name Is Jonas', 'id' => 'Foo' ))); $result = $this->form->text('foo', array('value' => 'My Name Is Jonas')); $this->assertTags($result, array('input' => array( 'type' => 'text', 'name' => 'foo', 'value' => 'My Name Is Jonas', 'id' => 'Foo' ))); } public function testFormInputField() { $tag = array('input' => array('type' => 'file', 'name' => 'upload', 'id' => 'Upload')); $result = $this->form->file('upload'); $this->assertTags($result, $tag); $value = new Document(array('model' => $this->_model)); $result = $this->form->file('upload', compact('value')); $tag['input']['value'] = ''; $this->assertTags($result, $tag); } public function testHiddenFieldWithId() { $result = $this->form->hidden('my_field'); $this->assertTags($result, array('input' => array( 'type' => 'hidden', 'name' => 'my_field', 'id' => 'MyField' ))); } public function testLabelGeneration() { $result = $this->form->label('next', 'Enter the next value >>'); $this->assertTags($result, array( 'label' => array('for' => 'next'), 'Enter the next value >>', '/label' )); $result = $this->form->label('user_name'); $this->assertTags($result, array( 'label' => array('for' => 'user_name'), 'User Name', '/label' )); $result = $this->form->label('first_name', array( 'First Name' => array('id' => 'first_name_label') )); $this->assertTags($result, array( 'label' => array('for' => 'first_name', 'id' => 'first_name_label'), 'First Name', '/label' )); $result = $this->form->label('first_name', array( null => array('id' => 'first_name_label') )); $this->assertTags($result, array( 'label' => array('for' => 'first_name', 'id' => 'first_name_label'), 'First Name', '/label' )); } public function testLabelGenerationWithNoEscape() { $result = $this->form->label('next', 'Enter the next value >>', array('escape' => false)); $this->assertTags($result, array( 'label' => array('for' => 'next'), 'Enter the next value >>', '/label' )); } public function testSubmitGeneration() { $result = $this->form->submit('Continue >'); $this->assertTags($result, array('input' => array( 'type' => 'submit', 'value' => 'Continue >' ))); $result = $this->form->submit('Continue >', array('class' => 'special')); $this->assertTags($result, array('input' => array( 'type' => 'submit', 'value' => 'Continue >', 'class' => 'special' ))); } public function testTextareaGeneration() { $result = $this->form->textarea('foo', array('value' => 'some content >')); $this->assertTags($result, array( 'textarea' => array('name' => 'foo', 'id' => 'Foo'), 'some content >', '/textarea' )); } public function testCheckboxGeneration() { $result = $this->form->checkbox('foo'); $this->assertTags($result, array( array('input' => array('type' => 'hidden', 'value' => '', 'name' => 'foo')), array('input' => array( 'type' => 'checkbox', 'value' => '1', 'name' => 'foo', 'id' => 'Foo' )) )); $result = $this->form->checkbox('foo', array('checked' => false)); $this->assertTags($result, array( array('input' => array('type' => 'hidden', 'value' => '', 'name' => 'foo')), array('input' => array( 'type' => 'checkbox', 'value' => '1', 'name' => 'foo', 'id' => 'Foo' )) )); $result = $this->form->checkbox('foo', array('checked' => true)); $this->assertTags($result, array( array('input' => array('type' => 'hidden', 'value' => '', 'name' => 'foo')), array('input' => array( 'type' => 'checkbox', 'value' => '1', 'name' => 'foo', 'checked' => 'checked', 'id' => 'Foo' )) )); $record = new Record(array('model' => $this->_model, 'data' => array('foo' => true))); $this->form->create($record); $result = $this->form->checkbox('foo'); $this->assertTags($result, array( array('input' => array('type' => 'hidden', 'value' => '', 'name' => 'foo')), array('input' => array( 'type' => 'checkbox', 'value' => '1', 'name' => 'foo', 'id' => 'MockFormPostFoo', 'checked' => 'checked' )) )); $record = new Record(array('model' => $this->_model, 'data' => array('foo' => false))); $this->form->create($record); $result = $this->form->checkbox('foo'); $this->assertTags($result, array( array('input' => array('type' => 'hidden', 'value' => '', 'name' => 'foo')), array('input' => array( 'type' => 'checkbox', 'value' => '1', 'name' => 'foo', 'id' => 'MockFormPostFoo' )) )); $document = new Document(array( 'model' => $this->_model, 'data' => array( 'subdocument' => array( 'foo' => true ) ) )); $this->form->create($document); $result = $this->form->checkbox('subdocument.foo'); $this->assertTags($result, array( array('input' => array( 'type' => 'hidden', 'value' => '', 'name' => 'subdocument[foo]') ), array('input' => array( 'type' => 'checkbox', 'value' => '1', 'name' => 'subdocument[foo]', 'checked' => 'checked', 'id' => 'MockFormPostSubdocumentFoo' )) )); } public function testCustomCheckbox() { $result = $this->form->checkbox('foo', array('value' => '1')); $this->assertTags($result, array( array('input' => array('type' => 'hidden', 'value' => '', 'name' => 'foo')), array('input' => array( 'type' => 'checkbox', 'value' => '1', 'name' => 'foo', 'id' => 'Foo' )) )); $result = $this->form->checkbox('foo', array('checked' => true, 'value' => '1')); $this->assertTags($result, array( array('input' => array('type' => 'hidden', 'value' => '', 'name' => 'foo')), array('input' => array( 'type' => 'checkbox', 'value' => '1', 'name' => 'foo', 'checked' => 'checked', 'id' => 'Foo' )) )); $record = new Record(array('model' => $this->_model, 'data' => array('foo' => true))); $this->form->create($record); $result = $this->form->checkbox('foo', array('value' => '1')); $this->assertTags($result, array( array('input' => array('type' => 'hidden', 'value' => '', 'name' => 'foo')), array('input' => array( 'type' => 'checkbox', 'value' => '1', 'name' => 'foo', 'id' => 'MockFormPostFoo', 'checked' => 'checked' )) )); $result = $this->form->checkbox('foo', array('value' => true)); $this->assertTags($result, array( array('input' => array('type' => 'hidden', 'value' => '', 'name' => 'foo')), array('input' => array( 'type' => 'checkbox', 'value' => '1', 'name' => 'foo', 'id' => 'MockFormPostFoo', 'checked' => 'checked' )) )); } public function testCustomValueCheckbox() { $result = $this->form->checkbox('foo', array('value' => 'HERO')); $this->assertTags($result, array( array('input' => array('type' => 'hidden', 'value' => '', 'name' => 'foo')), array('input' => array( 'type' => 'checkbox', 'value' => 'HERO', 'name' => 'foo', 'id' => 'Foo' )) )); $result = $this->form->checkbox('foo', array('value' => 'nose')); $this->assertTags($result, array( array('input' => array('type' => 'hidden', 'value' => '', 'name' => 'foo')), array('input' => array( 'type' => 'checkbox', 'value' => 'nose', 'name' => 'foo', 'id' => 'Foo' )) )); $record = new Record(array('model' => $this->_model, 'data' => array('foo' => 'nose'))); $this->form->create($record); $result = $this->form->checkbox('foo', array('value' => 'nose')); $this->assertTags($result, array( array('input' => array('type' => 'hidden', 'value' => '', 'name' => 'foo')), array('input' => array( 'type' => 'checkbox', 'value' => 'nose', 'name' => 'foo', 'id' => 'MockFormPostFoo', 'checked' => 'checked' )) )); $record = new Record(array('model' => $this->_model, 'data' => array('foo' => 'foot'))); $this->form->create($record); $result = $this->form->checkbox('foo', array('value' => 'nose')); $this->assertTags($result, array( array('input' => array('type' => 'hidden', 'value' => '', 'name' => 'foo')), array('input' => array( 'type' => 'checkbox', 'value' => 'nose', 'name' => 'foo', 'id' => 'MockFormPostFoo' )) )); $record = new Record(array('model' => $this->_model, 'data' => array('foo' => false))); $this->form->create($record); $result = $this->form->checkbox('foo', array('value' => '0')); $this->assertTags($result, array( array('input' => array('type' => 'hidden', 'value' => '', 'name' => 'foo')), array('input' => array( 'type' => 'checkbox', 'value' => '0', 'name' => 'foo', 'id' => 'MockFormPostFoo', 'checked' => 'checked' )) )); } public function testRadioGeneration() { $result = $this->form->radio('foo'); $this->assertTags($result, array(array( 'input' => array('type' => 'radio', 'value' => '1', 'name' => 'foo', 'id' => 'Foo')) )); $result = $this->form->radio('foo', array('checked' => false)); $this->assertTags($result, array(array( 'input' => array('type' => 'radio', 'value' => '1', 'name' => 'foo', 'id' => 'Foo')) )); $result = $this->form->radio('foo', array('checked' => true)); $this->assertTags($result, array(array( 'input' => array( 'type' => 'radio', 'value' => '1', 'name' => 'foo', 'checked' => 'checked', 'id' => 'Foo') ) )); $record = new Record(array('model' => $this->_model, 'data' => array('foo' => true))); $this->form->create($record); $result = $this->form->radio('foo'); $this->assertTags($result, array( array('input' => array( 'type' => 'radio', 'value' => '1', 'name' => 'foo', 'id' => 'MockFormPostFoo', 'checked' => 'checked' )) )); $record = new Record(array('model' => $this->_model, 'data' => array('foo' => false))); $this->form->create($record); $result = $this->form->radio('foo'); $this->assertTags($result, array( array('input' => array( 'type' => 'radio', 'value' => '1', 'name' => 'foo', 'id' => 'MockFormPostFoo' )) )); } public function testCustomRadio() { $result = $this->form->radio('foo', array('value' => '1')); $this->assertTags($result, array( array('input' => array( 'type' => 'radio', 'value' => '1', 'name' => 'foo', 'id' => 'Foo' )) )); $result = $this->form->radio('foo', array('checked' => true, 'value' => '1')); $this->assertTags($result, array( array('input' => array( 'type' => 'radio', 'value' => '1', 'name' => 'foo', 'checked' => 'checked', 'id' => 'Foo' )) )); $record = new Record(array('model' => $this->_model, 'data' => array('foo' => true))); $this->form->create($record); $result = $this->form->radio('foo', array('value' => '1')); $this->assertTags($result, array( array('input' => array( 'type' => 'radio', 'value' => '1', 'name' => 'foo', 'id' => 'MockFormPostFoo', 'checked' => 'checked' )) )); $result = $this->form->radio('foo', array('value' => true)); $this->assertTags($result, array( array('input' => array( 'type' => 'radio', 'value' => '1', 'name' => 'foo', 'id' => 'MockFormPostFoo', 'checked' => 'checked' )) )); } public function testCustomValueRadio() { $result = $this->form->radio('foo', array('value' => 'HERO')); $this->assertTags($result, array( array('input' => array( 'type' => 'radio', 'value' => 'HERO', 'name' => 'foo', 'id' => 'Foo' )) )); $result = $this->form->radio('foo', array('value' => 'nose')); $this->assertTags($result, array( array('input' => array( 'type' => 'radio', 'value' => 'nose', 'name' => 'foo', 'id' => 'Foo' )) )); $record = new Record(array('model' => $this->_model, 'data' => array('foo' => 'nose'))); $this->form->create($record); $result = $this->form->radio('foo', array('value' => 'nose')); $this->assertTags($result, array( array('input' => array( 'type' => 'radio', 'value' => 'nose', 'name' => 'foo', 'id' => 'MockFormPostFoo', 'checked' => 'checked' )) )); $record = new Record(array('model' => $this->_model, 'data' => array('foo' => 'foot'))); $this->form->create($record); $result = $this->form->checkbox('foo', array('value' => 'nose')); $this->assertTags($result, array( array('input' => array('type' => 'hidden', 'value' => '', 'name' => 'foo')), array('input' => array( 'type' => 'checkbox', 'value' => 'nose', 'name' => 'foo', 'id' => 'MockFormPostFoo' )) )); $record = new Record(array('model' => $this->_model, 'data' => array('foo' => false))); $this->form->create($record); $result = $this->form->radio('foo', array('value' => '0')); $this->assertTags($result, array( array('input' => array( 'type' => 'radio', 'value' => '0', 'name' => 'foo', 'id' => 'MockFormPostFoo', 'checked' => 'checked' )) )); } public function testSelectGeneration() { $result = $this->form->select('foo'); $this->assertTags($result, array( 'select' => array('name' => 'foo', 'id' => 'Foo'), '/select' )); $result = $this->form->select( 'colors', array('r' => 'red', 'g "' => 'green', 'b' => 'blue'), array('id' => 'Colors', 'value' => 'g "') ); $this->assertTags($result, array( 'select' => array('name' => 'colors', 'id' => 'Colors'), array('option' => array('value' => 'r')), 'red', '/option', array('option' => array('value' => 'g "', 'selected' => 'selected')), 'green', '/option', array('option' => array('value' => 'b')), 'blue', '/option', '/select' )); } /** * When trying to determine which option of a select box should be selected, we should be * integer/string agnostic because it all looks the same in HTML. * */ public function testSelectTypeAgnosticism() { $taglist = array( 'select' => array('name' => 'numbers', 'id' => 'Numbers'), array('option' => array('value' => '0')), 'Zero', '/option', array('option' => array('value' => '1', 'selected' => 'selected')), 'One', '/option', array('option' => array('value' => '2')), 'Two', '/option', '/select' ); $result = $this->form->select( 'numbers', array(0 => 'Zero', 1 => 'One', 2 => 'Two'), array('id' => 'Numbers', 'value' => '1') ); $this->assertTags($result, $taglist); $result = $this->form->select( 'numbers', array('0' => 'Zero', '1' => 'One', '2' => 'Two'), array('id' => 'Numbers', 'value' => 1) ); $this->assertTags($result, $taglist); } public function testSelectWithEmptyOption() { $result = $this->form->select('numbers', array('zero', 'first', 'second'), array( 'empty' => true )); $this->assertTags($result, array( 'select' => array('id' => 'Numbers', 'name' => 'numbers'), array('option' => array('value' => '', 'selected' => 'selected')), '/option', array('option' => array('value' => '0')), 'zero', '/option', array('option' => array('value' => '1')), 'first', '/option', array('option' => array('value' => '2')), 'second', '/option', '/select' )); $result = $this->form->select('numbers', array('zero', 'first', 'second'), array( 'empty' => '> Make a selection' )); $this->assertTags($result, array( 'select' => array('name' => 'numbers', 'id' => 'Numbers'), array('option' => array('value' => '', 'selected' => 'selected')), '> Make a selection', '/option', array('option' => array('value' => '0')), 'zero', '/option', array('option' => array('value' => '1')), 'first', '/option', array('option' => array('value' => '2')), 'second', '/option', '/select' )); } /** * Tests that calling `select()` with nested arrays will produce lists of `` elements. */ public function testRecursiveSelect() { $list = array( 'Linux' => array( '1' => 'Ubuntu 10.10', '2' => 'CentOS 5' ), 'Other' => array( '4' => 'Solaris', '5' => 'Windows Server 2010 R2' ) ); $result = $this->form->select('opsys', $list, array( 'empty' => 'Select one', 'value' => '5' )); $this->assertTags($result, array( 'select' => array('name' => 'opsys', 'id' => 'Opsys'), array('option' => array('value' => '')), 'Select one', '/option', array('optgroup' => array('label' => 'Linux')), array('option' => array('value' => '1')), 'Ubuntu 10.10', '/option', array('option' => array('value' => '2')), 'CentOS 5', '/option', '/optgroup', array('optgroup' => array('label' => 'Other')), array('option' => array('value' => '4')), 'Solaris', '/option', array('option' => array('value' => '5', 'selected' => 'selected')), 'Windows Server 2010 R2', '/option', '/optgroup', '/select' )); } public function testTemplateRemapping() { $result = $this->form->password('passwd'); $this->assertTags($result, array('input' => array( 'type' => 'password', 'name' => 'passwd', 'id' => 'Passwd' ))); $this->form->config(array('templates' => array('password' => 'text'))); $result = $this->form->password('passwd'); $this->assertTags($result, array('input' => array( 'type' => 'text', 'name' => 'passwd', 'id' => 'Passwd' ))); } public function testMultiSelect() { $expected = array( 'select' => array('name' => 'numbers[]', 'id' => 'Numbers', 'multiple' => 'multiple'), array('option' => array('value' => '', 'selected' => 'selected')), '> Make a selection', '/option', array('option' => array('value' => '1')), 'first', '/option', array('option' => array('value' => '2')), 'second', '/option', '/select' ); $result = $this->form->select('numbers', array('1' => 'first', '2' => 'second'), array( 'empty' => '> Make a selection', 'multiple' => true )); $this->assertTags($result, $expected); $expected = array( 'select' => array( 'name' => 'numbers[]', 'multiple' => 'multiple', 'size' => 5, 'id' => 'Numbers' ), array('option' => array('value' => '1')), 'first', '/option', array('option' => array('value' => '2')), 'second', '/option', '/select' ); $result = $this->form->select('numbers', array('1' => 'first', '2' => 'second'), array( 'multiple' => true, 'size' => 5 )); $this->assertTags($result, $expected); } public function testMultiselected() { $expected = array( 'select' => array('name' => 'numbers[]', 'id' => 'Numbers', 'multiple' => 'multiple'), array('option' => array('value' => '1', 'selected' => 'selected')), 'first', '/option', array('option' => array('value' => '2')), 'second', '/option', array('option' => array('value' => '3', 'selected' => 'selected')), 'third', '/option', array('option' => array('value' => '4', 'selected' => 'selected')), 'fourth', '/option', '/select' ); $list = array(1 => 'first', 2 => 'second', 3 => 'third', 4 => 'fourth'); $options = array('value' => array(1, 3, 4), 'multiple' => true); $result = $this->form->select('numbers', $list, $options); $this->assertTags($result, $expected); } public function testFormCreateWithMoreParams() { $request = new Request(); $request->params = array('controller' => 'mock', 'action' => 'test', 'args' => array('1')); $context = new MockFormRenderer(compact('request')); $form = new Form(compact('context')); $result = $form->create(); $this->assertTags($result, array('form' => array( 'action' => "{$this->base}mock/test/1", 'method' => 'post' ))); } public function testFormCreateWithMoreParamsButSpecifiedAction() { $request = new Request(); $request->params = array('controller' => 'mock', 'action' => 'test', 'args' => array('1')); $request->persist = array('controller'); $context = new MockFormRenderer(compact('request')); $form = new Form(compact('context')); $result = $form->create(null, array('action' => 'radness')); $this->assertTags($result, array('form' => array( 'action' => "{$this->base}mock/radness", 'method' => 'post' ))); } public function testFormField() { $result = $this->form->field('name'); $this->assertTags($result, array( 'div' => array(), 'label' => array('for' => 'Name'), 'Name', '/label', 'input' => array('type' => 'text', 'name' => 'name', 'id' => 'Name'), '/div' )); $result = $this->form->field('name', array('type' => 'radio', 'value' => 'foo')); $this->assertTags($result, array( 'div' => array(), 'input' => array('type' => 'radio', 'name' => 'name', 'value' => 'foo', 'id' => 'Name'), 'label' => array('for' => 'Name'), 'Name', '/label', '/div' )); $result = $this->form->field('name', array('type' => 'checkbox')); $expected = array( '
', '', '', '
' ); $this->assertEqual(join('', $expected), $result); } public function testFormFieldWithCustomConfig() { $this->form->config(array('field' => array('class' => 'custom-field'))); $result = $this->form->field('username'); $this->assertTags($result, array( 'div' => array(), 'label' => array('for' => 'Username'), 'Username', '/label', 'input' => array( 'type' => 'text', 'name' => 'username', 'class' => 'custom-field', 'id' => 'Username' ), '/div' )); $this->assertTags($this->form->end(), array('/form')); $this->form->config(array('templates' => array('end' => ""))); $this->assertTags($this->form->end(), array('/table', '/form')); } /** * Verifies that calls to `field()` with `'type' => 'hidden'` do not produce `