diff options
author | Andreas Gohr <andi@splitbrain.org> | 2015-08-03 18:47:19 +0200 |
---|---|---|
committer | Andreas Gohr <andi@splitbrain.org> | 2015-08-03 18:47:19 +0200 |
commit | 8909ab7629ec67708b589c35d380c68da04a8b44 (patch) | |
tree | a35ec57bebc7c0fcc45853fe377b5b0fcc2d5846 /_test | |
parent | b20571a714d774d231f80f95643c6f84ef9833ed (diff) | |
parent | 3c8e094acdcf6d556f022bc224b81ef17e449281 (diff) | |
download | rpg-8909ab7629ec67708b589c35d380c68da04a8b44.tar.gz rpg-8909ab7629ec67708b589c35d380c68da04a8b44.tar.bz2 |
Merge pull request #1265 from splitbrain/form2
Form 2
Diffstat (limited to '_test')
-rw-r--r-- | _test/tests/inc/form/checkableelement.test.php | 49 | ||||
-rw-r--r-- | _test/tests/inc/form/form.test.php | 115 | ||||
-rw-r--r-- | _test/tests/inc/form/inputelement.test.php | 41 |
3 files changed, 205 insertions, 0 deletions
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 new file mode 100644 index 000000000..3ae832b2c --- /dev/null +++ b/_test/tests/inc/form/form.test.php @@ -0,0 +1,115 @@ +<?php + +use dokuwiki\Form; + +/** + * makes form internals accessible for testing + */ +class TestForm extends Form\Form { + /** + * @return array list of element types + */ + function getElementTypeList() { + $list = array(); + foreach($this->elements as $element) $list[] = $element->getType(); + return $list; + } + + public function balanceFieldsets() { + parent::balanceFieldsets(); + } + +} + +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); + } + + + function test_fieldsetbalance() { + $form = new TestForm(); + $form->addFieldsetOpen(); + $form->addHTML('ignored'); + $form->addFieldsetClose(); + $form->balanceFieldsets(); + + $this->assertEquals( + array( + 'fieldsetopen', + 'html', + 'fieldsetclose' + ), + $form->getElementTypeList() + ); + + $form = new TestForm(); + $form->addHTML('ignored'); + $form->addFieldsetClose(); + $form->balanceFieldsets(); + + $this->assertEquals( + array( + 'fieldsetopen', + 'html', + 'fieldsetclose' + ), + $form->getElementTypeList() + ); + + + $form = new TestForm(); + $form->addFieldsetOpen(); + $form->addHTML('ignored'); + $form->balanceFieldsets(); + + $this->assertEquals( + array( + 'fieldsetopen', + 'html', + 'fieldsetclose' + ), + $form->getElementTypeList() + ); + + $form = new TestForm(); + $form->addHTML('ignored'); + $form->addFieldsetClose(); + $form->addHTML('ignored'); + $form->addFieldsetOpen(); + $form->addHTML('ignored'); + $form->balanceFieldsets(); + + $this->assertEquals( + array( + 'fieldsetopen', + 'html', + 'fieldsetclose', + 'html', + 'fieldsetopen', + 'html', + 'fieldsetclose' + ), + $form->getElementTypeList() + ); + } + +} 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()); + } + +} |