Skip to content
Snippets Groups Projects
Commit 6d0ceaf9 authored by Andreas Gohr's avatar Andreas Gohr
Browse files

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
parent e7a32b17
No related branches found
No related tags found
No related merge requests found
<?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'));
}
}
<?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);
}
}
<?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());
}
}
......@@ -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;
}
......
......@@ -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');
}
/**
......
......@@ -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);
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment