xref: /dokuwiki/_test/tests/Form/CheckableElementTest.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 CheckableElementTest 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        $form->addRadioButton('foo', 'label text first')->val('first')->attr('checked', 'checked');
15*98640fd3SAndreas Gohr        $form->addRadioButton('foo', 'label text second')->val('second');
16*98640fd3SAndreas Gohr
17*98640fd3SAndreas Gohr        $html = $form->toHTML();
18*98640fd3SAndreas Gohr        $pq = (new Document())->html($html);
19*98640fd3SAndreas Gohr
20*98640fd3SAndreas Gohr        $input = $pq->find('input[name=foo]');
21*98640fd3SAndreas Gohr        $this->assertTrue($input->count() == 2);
22*98640fd3SAndreas Gohr
23*98640fd3SAndreas Gohr        $label = $pq->find('label');
24*98640fd3SAndreas Gohr        $this->assertTrue($label->count() == 2);
25*98640fd3SAndreas Gohr
26*98640fd3SAndreas Gohr        $inputs = $pq->find('input[name=foo]');
27*98640fd3SAndreas Gohr        $this->assertEquals('first', $inputs->get(0)->attr('value'));
28*98640fd3SAndreas Gohr        $this->assertEquals('second', $inputs->get(1)->attr('value'));
29*98640fd3SAndreas Gohr        $this->assertEquals('checked', $inputs->get(0)->attr('checked'));
30*98640fd3SAndreas Gohr        $this->assertEquals('', $inputs->get(1)->attr('checked'));
31*98640fd3SAndreas Gohr        $this->assertEquals('radio', $inputs->get(0)->attr('type'));
32*98640fd3SAndreas Gohr    }
33*98640fd3SAndreas Gohr
34*98640fd3SAndreas Gohr    /**
35*98640fd3SAndreas Gohr     * check that posted values overwrite preset default
36*98640fd3SAndreas Gohr     */
37*98640fd3SAndreas Gohr    function testPrefill()
38*98640fd3SAndreas Gohr    {
39*98640fd3SAndreas Gohr        global $INPUT;
40*98640fd3SAndreas Gohr        $INPUT->post->set('foo', 'second');
41*98640fd3SAndreas Gohr
42*98640fd3SAndreas Gohr        $form = new Form\Form();
43*98640fd3SAndreas Gohr        $form->addRadioButton('foo', 'label text first')->val('first')->attr('checked', 'checked');
44*98640fd3SAndreas Gohr        $form->addRadioButton('foo', 'label text second')->val('second');
45*98640fd3SAndreas Gohr
46*98640fd3SAndreas Gohr        $html = $form->toHTML();
47*98640fd3SAndreas Gohr        $pq = (new Document())->html($html);
48*98640fd3SAndreas Gohr
49*98640fd3SAndreas Gohr        $inputs = $pq->find('input[name=foo]');
50*98640fd3SAndreas Gohr        $this->assertEquals('first', $inputs->get(0)->attr('value'));
51*98640fd3SAndreas Gohr        $this->assertEquals('second', $inputs->get(1)->attr('value'));
52*98640fd3SAndreas Gohr        $this->assertEquals('', $inputs->get(0)->attr('checked'));
53*98640fd3SAndreas Gohr        $this->assertEquals('checked', $inputs->get(1)->attr('checked'));
54*98640fd3SAndreas Gohr        $this->assertEquals('radio', $inputs->get(0)->attr('type'));
55*98640fd3SAndreas Gohr    }
56*98640fd3SAndreas Gohr}
57