summaryrefslogtreecommitdiff
path: root/_test/tests/inc/form/checkableelement.test.php
blob: a0e4173e860b470efaae56885875c4fb9003c6a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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'));
    }
}