summaryrefslogtreecommitdiff
path: root/_test/tests/inc/form/form.test.php
blob: 3ae832b2cd5d6f5aa81bb633a7a012d8209e91a9 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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()
        );
    }

}