From 6d0ceaf93ca31dfb83fd4325ef2eecd9cef733c0 Mon Sep 17 00:00:00 2001 From: Andreas Gohr <andi@splitbrain.org> Date: Fri, 8 May 2015 17:26:11 +0200 Subject: [PATCH] added a first few tests. this is far from comprehensible, but should give an idea how the new library works and how to write tests --- .../tests/inc/form/checkableelement.test.php | 49 +++++++++++++++++++ _test/tests/inc/form/form.test.php | 28 +++++++++++ _test/tests/inc/form/inputelement.test.php | 41 ++++++++++++++++ inc/Form/Element.php | 1 + inc/Form/Form.php | 6 +-- inc/Form/InputElement.php | 1 + 6 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 _test/tests/inc/form/checkableelement.test.php create mode 100644 _test/tests/inc/form/inputelement.test.php diff --git a/_test/tests/inc/form/checkableelement.test.php b/_test/tests/inc/form/checkableelement.test.php new file mode 100644 index 000000000..a0e4173e8 --- /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 e69de29bb..cdf3e5a3a 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 000000000..7a5e6d2ea --- /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 6f23e2631..3fd170a80 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 3a8b590e7..dc502e021 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 59310174e..4f644c0f1 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); } /** -- GitLab