diff --git a/_test/tests/inc/form/checkableelement.test.php b/_test/tests/inc/form/checkableelement.test.php new file mode 100644 index 0000000000000000000000000000000000000000..a0e4173e860b470efaae56885875c4fb9003c6a7 --- /dev/null +++ b/_test/tests/inc/form/checkableelement.test.php @@ -0,0 +1,49 @@ +<?php + +use dokuwiki\Form; + +class form_checkableelement_test extends DokuWikiTest { + + function test_defaults() { + $form = new Form\Form(); + $form->addRadioButton('foo', 'label text first')->val('first')->attr('checked', 'checked'); + $form->addRadioButton('foo', 'label text second')->val('second'); + + $html = $form->toHTML(); + $pq = phpQuery::newDocumentXHTML($html); + + $input = $pq->find('input[name=foo]'); + $this->assertTrue($input->length == 2); + + $label = $pq->find('label'); + $this->assertTrue($label->length == 2); + + $inputs = $pq->find('input[name=foo]'); + $this->assertEquals('first', pq($inputs->elements[0])->val()); + $this->assertEquals('second', pq($inputs->elements[1])->val()); + $this->assertEquals('checked', pq($inputs->elements[0])->attr('checked')); + $this->assertEquals('', pq($inputs->elements[1])->attr('checked')); + } + + /** + * check that posted values overwrite preset default + */ + function test_prefill() { + global $INPUT; + $INPUT->post->set('foo', 'second'); + + + $form = new Form\Form(); + $form->addRadioButton('foo', 'label text first')->val('first')->attr('checked', 'checked'); + $form->addRadioButton('foo', 'label text second')->val('second'); + + $html = $form->toHTML(); + $pq = phpQuery::newDocumentXHTML($html); + + $inputs = $pq->find('input[name=foo]'); + $this->assertEquals('first', pq($inputs->elements[0])->val()); + $this->assertEquals('second', pq($inputs->elements[1])->val()); + $this->assertEquals('', pq($inputs->elements[0])->attr('checked')); + $this->assertEquals('checked', pq($inputs->elements[1])->attr('checked')); + } +} diff --git a/_test/tests/inc/form/form.test.php b/_test/tests/inc/form/form.test.php index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..cdf3e5a3aa544c320a11a02d029d27a4a7399225 100644 --- a/_test/tests/inc/form/form.test.php +++ b/_test/tests/inc/form/form.test.php @@ -0,0 +1,28 @@ +<?php + +use dokuwiki\Form; + +class form_form_test extends DokuWikiTest { + + /** + * checks that an empty form is initialized correctly + */ + function test_defaults() { + global $INPUT; + global $ID; + $ID = 'some:test'; + $INPUT->get->set('id', $ID); + $INPUT->get->set('foo', 'bar'); + + $form = new Form\Form(); + $html = $form->toHTML(); + $pq = phpQuery::newDocumentXHTML($html); + + $this->assertTrue($pq->find('form')->hasClass('doku_form')); + $this->assertEquals(wl($ID, array('foo' => 'bar'), false, '&'), $pq->find('form')->attr('action')); + $this->assertEquals('post', $pq->find('form')->attr('method')); + + $this->assertTrue($pq->find('input[name=sectok]')->length == 1); + } + +} diff --git a/_test/tests/inc/form/inputelement.test.php b/_test/tests/inc/form/inputelement.test.php new file mode 100644 index 0000000000000000000000000000000000000000..7a5e6d2ea944f349c702bc1907008972c99e0d31 --- /dev/null +++ b/_test/tests/inc/form/inputelement.test.php @@ -0,0 +1,41 @@ +<?php + +use dokuwiki\Form; + +class form_inputelement_test extends DokuWikiTest { + + function test_defaults() { + $form = new Form\Form(); + $form->addTextInput('foo', 'label text')->val('this is text'); + + $html = $form->toHTML(); + $pq = phpQuery::newDocumentXHTML($html); + + $input = $pq->find('input[name=foo]'); + $this->assertTrue($input->length == 1); + $this->assertEquals('this is text', $input->val()); + + $label = $pq->find('label'); + $this->assertTrue($label->length == 1); + $this->assertEquals('label text', $label->find('span')->text()); + } + + /** + * check that posted values overwrite preset default + */ + function test_prefill() { + global $INPUT; + $INPUT->post->set('foo', 'a new text'); + + $form = new Form\Form(); + $form->addTextInput('foo', 'label text')->val('this is text'); + + $html = $form->toHTML(); + $pq = phpQuery::newDocumentXHTML($html); + + $input = $pq->find('input[name=foo]'); + $this->assertTrue($input->length == 1); + $this->assertEquals('a new text', $input->val()); + } + +} diff --git a/inc/Form/Element.php b/inc/Form/Element.php index 6f23e26315e3d52800695a694c0811f64b8401d3..3fd170a80bfc2b81191e3f8facd43d57efcc7c03 100644 --- a/inc/Form/Element.php +++ b/inc/Form/Element.php @@ -101,6 +101,7 @@ abstract class Element { $classes = explode(' ', $this->attr('class')); $classes[] = $class; $classes = array_unique($classes); + $classes = array_filter($classes); $this->attr('class', join(' ', $classes)); return $this; } diff --git a/inc/Form/Form.php b/inc/Form/Form.php index 3a8b590e78c281ec8d0ae38dac54ab64bc82f85e..dc502e0219faea5b8c3dcb3989897b63b5030a70 100644 --- a/inc/Form/Form.php +++ b/inc/Form/Form.php @@ -34,7 +34,7 @@ class Form extends Element { if(!$this->attr('action')) { $get = $_GET; if(isset($get['id'])) unset($get['id']); - $self = wl($ID, $get); + $self = wl($ID, $get, false, '&'); //attributes are escaped later $this->attr('action', $self); } @@ -51,8 +51,8 @@ class Form extends Element { // add the security token by default $this->setHiddenField('sectok', getSecurityToken()); - // identify this as a form2 based form in HTML - $this->addClass('doku_form2'); + // identify this as a new form based form in HTML + $this->addClass('doku_form'); } /** diff --git a/inc/Form/InputElement.php b/inc/Form/InputElement.php index 59310174e231a3b57fca0cd8fc8edafc3856b986..4f644c0f124fb180c59d33debe7fa8b8aa601343 100644 --- a/inc/Form/InputElement.php +++ b/inc/Form/InputElement.php @@ -28,6 +28,7 @@ class InputElement extends Element { public function __construct($type, $name, $label) { parent::__construct($type, array('name' => $name)); $this->attr('name', $name); + $this->label = new Label($label); } /**