xref: /dokuwiki/_test/tests/Form/DropdownElementTest.php (revision 98640fd3947e9ff996e1c9206b5f845465794486)
1*98640fd3SAndreas Gohr<?php
2*98640fd3SAndreas Gohr
3*98640fd3SAndreas Gohrnamespace dokuwiki\test\Form;
4*98640fd3SAndreas Gohr
5*98640fd3SAndreas Gohruse dokuwiki\Form;
6*98640fd3SAndreas Gohruse DOMWrap\Document;
7*98640fd3SAndreas Gohr
8*98640fd3SAndreas Gohrclass DropdownElementTest extends \DokuWikiTest
9*98640fd3SAndreas Gohr{
10*98640fd3SAndreas Gohr
11*98640fd3SAndreas Gohr    function testDefaults()
12*98640fd3SAndreas Gohr    {
13*98640fd3SAndreas Gohr        $form = new Form\Form();
14*98640fd3SAndreas Gohr
15*98640fd3SAndreas Gohr        // basic tests
16*98640fd3SAndreas Gohr        $options = array('first', 'second', 'third');
17*98640fd3SAndreas Gohr        $element = $form->addDropdown('foo', $options, 'label text');
18*98640fd3SAndreas Gohr        $this->assertEquals('first', $element->val());
19*98640fd3SAndreas Gohr        $element->val('second');
20*98640fd3SAndreas Gohr        $this->assertEquals('second', $element->val());
21*98640fd3SAndreas Gohr        $element->val('nope');
22*98640fd3SAndreas Gohr        $this->assertEquals('first', $element->val());
23*98640fd3SAndreas Gohr
24*98640fd3SAndreas Gohr        // associative array
25*98640fd3SAndreas Gohr        $options = array('first' => 'A first Label', 'second' => 'The second Label', 'third' => 'Just 3');
26*98640fd3SAndreas Gohr        $element->options($options);
27*98640fd3SAndreas Gohr        $this->assertEquals('first', $element->val());
28*98640fd3SAndreas Gohr        $element->val('second');
29*98640fd3SAndreas Gohr        $this->assertEquals('second', $element->val());
30*98640fd3SAndreas Gohr        $element->val('nope');
31*98640fd3SAndreas Gohr        $this->assertEquals('first', $element->val());
32*98640fd3SAndreas Gohr
33*98640fd3SAndreas Gohr        // HTML
34*98640fd3SAndreas Gohr        $html = $form->toHTML();
35*98640fd3SAndreas Gohr        $pq = (new Document())->html($html);
36*98640fd3SAndreas Gohr
37*98640fd3SAndreas Gohr        $select = $pq->find('select[name=foo]');
38*98640fd3SAndreas Gohr        $this->assertTrue($select->count() == 1);
39*98640fd3SAndreas Gohr
40*98640fd3SAndreas Gohr        $options = $pq->find('option');
41*98640fd3SAndreas Gohr        $this->assertTrue($options->count() == 3);
42*98640fd3SAndreas Gohr
43*98640fd3SAndreas Gohr        $option = $pq->find('option[selected=selected]');
44*98640fd3SAndreas Gohr        $this->assertTrue($option->count() == 1);
45*98640fd3SAndreas Gohr        $this->assertEquals('first', $option->attr('value'));
46*98640fd3SAndreas Gohr        $this->assertEquals('A first Label', $option->text());
47*98640fd3SAndreas Gohr
48*98640fd3SAndreas Gohr        $label = $pq->find('label');
49*98640fd3SAndreas Gohr        $this->assertTrue($label->count() == 1);
50*98640fd3SAndreas Gohr        $this->assertEquals('label text', $label->find('span')->text());
51*98640fd3SAndreas Gohr    }
52*98640fd3SAndreas Gohr
53*98640fd3SAndreas Gohr    function testExtendedOptions()
54*98640fd3SAndreas Gohr    {
55*98640fd3SAndreas Gohr        $form = new Form\Form();
56*98640fd3SAndreas Gohr
57*98640fd3SAndreas Gohr        $options = array(
58*98640fd3SAndreas Gohr            'first' => array(
59*98640fd3SAndreas Gohr                'label' => 'the label',
60*98640fd3SAndreas Gohr                'attrs' => array(
61*98640fd3SAndreas Gohr                    'id' => 'theID',
62*98640fd3SAndreas Gohr                    'class' => 'two classes',
63*98640fd3SAndreas Gohr                    'data-foo' => 'bar',
64*98640fd3SAndreas Gohr                ),
65*98640fd3SAndreas Gohr            ),
66*98640fd3SAndreas Gohr            'second',
67*98640fd3SAndreas Gohr            '3' => array(
68*98640fd3SAndreas Gohr                'label' => 'the label of the complex third option',
69*98640fd3SAndreas Gohr            ),
70*98640fd3SAndreas Gohr        );
71*98640fd3SAndreas Gohr
72*98640fd3SAndreas Gohr        $form->addDropdown('foo', $options, 'label text');
73*98640fd3SAndreas Gohr        // HTML
74*98640fd3SAndreas Gohr        $html = $form->toHTML();
75*98640fd3SAndreas Gohr        $pq = (new Document())->html($html);;
76*98640fd3SAndreas Gohr
77*98640fd3SAndreas Gohr        $select = $pq->find('select[name=foo]');
78*98640fd3SAndreas Gohr        $this->assertTrue($select->count() == 1);
79*98640fd3SAndreas Gohr
80*98640fd3SAndreas Gohr        $options = $pq->find('option');
81*98640fd3SAndreas Gohr        $this->assertEquals(3, $options->count());
82*98640fd3SAndreas Gohr
83*98640fd3SAndreas Gohr        $option = $pq->find('option#theID');
84*98640fd3SAndreas Gohr        $this->assertEquals(1, $option->count());
85*98640fd3SAndreas Gohr        $this->assertEquals('first', $option->attr('value'));
86*98640fd3SAndreas Gohr        $this->assertEquals('the label', $option->text());
87*98640fd3SAndreas Gohr        $this->assertEquals('bar', $option->attr('data-foo'));
88*98640fd3SAndreas Gohr        $this->assertTrue($option->hasClass('two'));
89*98640fd3SAndreas Gohr        $this->assertTrue($option->hasClass('classes'));
90*98640fd3SAndreas Gohr    }
91*98640fd3SAndreas Gohr
92*98640fd3SAndreas Gohr    public function testOptgroups()
93*98640fd3SAndreas Gohr    {
94*98640fd3SAndreas Gohr        $form = new Form\Form();
95*98640fd3SAndreas Gohr
96*98640fd3SAndreas Gohr        $options1 = array(
97*98640fd3SAndreas Gohr            'first' => 'the label',
98*98640fd3SAndreas Gohr            'second',
99*98640fd3SAndreas Gohr        );
100*98640fd3SAndreas Gohr
101*98640fd3SAndreas Gohr        $options2 = array(
102*98640fd3SAndreas Gohr            'third' => array(
103*98640fd3SAndreas Gohr                'label' => 'label of third option',
104*98640fd3SAndreas Gohr                'attribute' => 'attribute-value',
105*98640fd3SAndreas Gohr            ),
106*98640fd3SAndreas Gohr            'fourth',
107*98640fd3SAndreas Gohr        );
108*98640fd3SAndreas Gohr
109*98640fd3SAndreas Gohr        $dropdown = $form->addDropdown('foo', null, 'label text');
110*98640fd3SAndreas Gohr        $dropdown->addOptGroup('opt1', $options1);
111*98640fd3SAndreas Gohr        $dropdown->addOptGroup('opt2', $options2);
112*98640fd3SAndreas Gohr
113*98640fd3SAndreas Gohr        $dropdown->val('third');
114*98640fd3SAndreas Gohr        $this->assertEquals('third', $dropdown->val());
115*98640fd3SAndreas Gohr
116*98640fd3SAndreas Gohr        /** @var Form\OptGroup[] $optGroups */
117*98640fd3SAndreas Gohr        $optGroups = $dropdown->optGroups();
118*98640fd3SAndreas Gohr        $this->assertEquals(array(
119*98640fd3SAndreas Gohr            'first' => array('label' => 'the label'),
120*98640fd3SAndreas Gohr            'second' => array('label' => 'second'),
121*98640fd3SAndreas Gohr        ), $optGroups['opt1']->options());
122*98640fd3SAndreas Gohr
123*98640fd3SAndreas Gohr        // HTML
124*98640fd3SAndreas Gohr        $html = $form->toHTML();
125*98640fd3SAndreas Gohr        $pq = (new Document())->html($html);
126*98640fd3SAndreas Gohr
127*98640fd3SAndreas Gohr        $optGroupsHTML = $pq->find('optgroup');
128*98640fd3SAndreas Gohr        $this->assertEquals(2, $optGroupsHTML->count());
129*98640fd3SAndreas Gohr
130*98640fd3SAndreas Gohr        $options = $pq->find('option');
131*98640fd3SAndreas Gohr        $this->assertEquals(4, $options->count());
132*98640fd3SAndreas Gohr
133*98640fd3SAndreas Gohr        $selected = $pq->find('option[selected=selected]');
134*98640fd3SAndreas Gohr        $this->assertEquals('third', $selected->attr('value'));
135*98640fd3SAndreas Gohr        $this->assertEquals('label of third option', $selected->text());
136*98640fd3SAndreas Gohr    }
137*98640fd3SAndreas Gohr
138*98640fd3SAndreas Gohr    /**
139*98640fd3SAndreas Gohr     * Prevent double select that might occur because `'Auto' == 0` is true
140*98640fd3SAndreas Gohr     */
141*98640fd3SAndreas Gohr    public function testDoubleSelect()
142*98640fd3SAndreas Gohr    {
143*98640fd3SAndreas Gohr        $form = new Form\Form();
144*98640fd3SAndreas Gohr        $form->addDropdown('foo', ['Auto', 0, 1]);
145*98640fd3SAndreas Gohr
146*98640fd3SAndreas Gohr        $html = $form->toHTML();
147*98640fd3SAndreas Gohr
148*98640fd3SAndreas Gohr        $pq = (new Document())->html($html);
149*98640fd3SAndreas Gohr        $selected = $pq->find('option[selected=selected]');
150*98640fd3SAndreas Gohr        $this->assertEquals(1, $selected->count());
151*98640fd3SAndreas Gohr        $this->assertEquals('Auto', $selected->text());
152*98640fd3SAndreas Gohr    }
153*98640fd3SAndreas Gohr
154*98640fd3SAndreas Gohr    /**
155*98640fd3SAndreas Gohr     * Ensure that there is always only a single one selected option
156*98640fd3SAndreas Gohr     */
157*98640fd3SAndreas Gohr    public function testOptgroupsDoubleselect()
158*98640fd3SAndreas Gohr    {
159*98640fd3SAndreas Gohr        $form = new Form\Form();
160*98640fd3SAndreas Gohr        $options1 = array(
161*98640fd3SAndreas Gohr            'double' => 'the label',
162*98640fd3SAndreas Gohr        );
163*98640fd3SAndreas Gohr
164*98640fd3SAndreas Gohr        $options2 = array(
165*98640fd3SAndreas Gohr            'double' => array(
166*98640fd3SAndreas Gohr                'label' => 'label of third option',
167*98640fd3SAndreas Gohr                'attribute' => 'attribute-value',
168*98640fd3SAndreas Gohr            ),
169*98640fd3SAndreas Gohr        );
170*98640fd3SAndreas Gohr
171*98640fd3SAndreas Gohr        $dropdown = $form->addDropdown('foo', null, 'label text');
172*98640fd3SAndreas Gohr        $dropdown->addOptGroup('opt1', $options1);
173*98640fd3SAndreas Gohr        $dropdown->addOptGroup('opt2', $options2);
174*98640fd3SAndreas Gohr        $dropdown->val('double');
175*98640fd3SAndreas Gohr
176*98640fd3SAndreas Gohr        // HTML
177*98640fd3SAndreas Gohr        $html = $form->toHTML();
178*98640fd3SAndreas Gohr        $pq = (new Document())->html($html);
179*98640fd3SAndreas Gohr        $selected = $pq->find('option[selected=selected]');
180*98640fd3SAndreas Gohr        $this->assertEquals(1, $selected->count());
181*98640fd3SAndreas Gohr        $this->assertEquals('the label', $selected->text());
182*98640fd3SAndreas Gohr    }
183*98640fd3SAndreas Gohr
184*98640fd3SAndreas Gohr    /**
185*98640fd3SAndreas Gohr     * check that posted values overwrite preset default
186*98640fd3SAndreas Gohr     */
187*98640fd3SAndreas Gohr    public function testPrefill()
188*98640fd3SAndreas Gohr    {
189*98640fd3SAndreas Gohr        global $INPUT;
190*98640fd3SAndreas Gohr        $INPUT->post->set('foo', 'second');
191*98640fd3SAndreas Gohr
192*98640fd3SAndreas Gohr        $form = new Form\Form();
193*98640fd3SAndreas Gohr        $options = array('first' => 'A first Label', 'second' => 'The second Label', 'third' => 'Just 3');
194*98640fd3SAndreas Gohr        $element = $form->addDropdown('foo', $options, 'label text')->val('third');
195*98640fd3SAndreas Gohr        $this->assertEquals('third', $element->val());
196*98640fd3SAndreas Gohr
197*98640fd3SAndreas Gohr        $html = $form->toHTML();
198*98640fd3SAndreas Gohr        $pq = (new Document())->html($html);
199*98640fd3SAndreas Gohr
200*98640fd3SAndreas Gohr        $option = $pq->find('option[selected=selected]');
201*98640fd3SAndreas Gohr        $this->assertTrue($option->count() == 1);
202*98640fd3SAndreas Gohr        $this->assertEquals('second', $option->attr('value'));
203*98640fd3SAndreas Gohr        $this->assertEquals('The second Label', $option->text());
204*98640fd3SAndreas Gohr    }
205*98640fd3SAndreas Gohr
206*98640fd3SAndreas Gohr    public function testMultiple()
207*98640fd3SAndreas Gohr    {
208*98640fd3SAndreas Gohr        $form = new Form\Form();
209*98640fd3SAndreas Gohr
210*98640fd3SAndreas Gohr        $options = array('first' => 'A first Label', 'second' => 'The second Label', 'third' => 'Just 3');
211*98640fd3SAndreas Gohr        $element = $form->addDropdown('foo', $options, 'label text')->attr('multiple', '1');
212*98640fd3SAndreas Gohr
213*98640fd3SAndreas Gohr        // only two of these values are valid
214*98640fd3SAndreas Gohr        $element->val(['first', 'third', 'fourth']);
215*98640fd3SAndreas Gohr        $this->assertEquals(['first', 'third'], $element->val());
216*98640fd3SAndreas Gohr
217*98640fd3SAndreas Gohr        // check HTML
218*98640fd3SAndreas Gohr        $html = $form->toHTML();
219*98640fd3SAndreas Gohr        $pq = (new Document())->html($html);;
220*98640fd3SAndreas Gohr        $option = $pq->find('option[selected=selected]');
221*98640fd3SAndreas Gohr
222*98640fd3SAndreas Gohr        $this->assertEquals('A first Label', $option->get(0)->textContent);
223*98640fd3SAndreas Gohr        $this->assertEquals('Just 3', $option->get(1)->textContent);
224*98640fd3SAndreas Gohr    }
225*98640fd3SAndreas Gohr}
226