1<?php
2/**
3 * @group plugin_bureaucracyau
4 * @group plugins
5 */
6class syntax_plugin_bureaucracyau_test extends DokuWikiTest {
7
8    protected $pluginsEnabled = array('bureaucracy');
9
10    public function test_generalFormOutput() {
11        $input = file_get_contents(dirname(__FILE__) . '/input.txt');
12        $xhtml = p_render('xhtml', p_get_instructions($input), $info);
13
14        $doc = phpQuery::newDocument($xhtml);
15
16        $this->assertEquals(1, pq('form.bureaucracyau__plugin', $doc)->length);
17        $this->assertEquals(6, pq('form.bureaucracyau__plugin fieldset', $doc)->length);
18
19        // standard input types
20        $this->checkField($doc, 'Employee Name', 'input[type=text][value=Your Name].edit', true);
21        $this->checkField($doc, 'Your Age', 'input[type=text].edit', true);
22        $this->checkField($doc, 'Your E-Mail Address', 'input[type=text].edit', true);
23        $this->checkField($doc, 'Occupation (optional)', 'input[type=text].edit');
24        $this->checkField($doc, 'Some password', 'input[type=password].edit', true);
25
26        // select field
27        $select = $this->checkField($doc, 'Please select an option', 'select');
28        $this->assertEquals(3, pq('option', $select)->length);
29        $this->assertEquals(1, pq('option:selected', $select)->length);
30        $this->assertEquals('Peaches', pq('option:selected', $select)->val());
31
32        // static text
33        $this->assertEquals(1, pq('p:contains(Some static text)', $doc)->length);
34
35        // checkbox
36        $cb = $this->checkField($doc, 'Read the agreement?', 'input[type=checkbox][value=1]');
37        $this->assertEquals('1', pq('input[type=hidden][value=0]', $cb->parent())->length);
38
39        // text area
40        $this->checkField($doc, 'Tell me about your self', 'textarea.edit', true);
41
42        // file field
43        $this->checkField($doc, 'File1', 'input[type=file].edit', true);
44
45        // submit button
46        $this->assertEquals(1, pq('button[type=submit]:contains(Submit Query)')->length);
47
48    }
49
50    public function test_HTMLinclusion() {
51        $input = file_get_contents(dirname(__FILE__) . '/input.txt');
52        $xhtml = p_render('xhtml', p_get_instructions($input), $info);
53
54        $doc = phpQuery::newDocument($xhtml);
55
56        // HTML Check - there should be no bold tag anywhere
57        $this->assertEquals(0, pq('bold', $doc)->length);
58    }
59
60    private function checkField($doc, $name, $check, $required=false) {
61
62        $field = pq('form.bureaucracyau__plugin label span:contains(' . $name . ')', $doc);
63        $this->assertEquals(1, $field->length, "find span of $name");
64
65        if($required){
66            $this->assertEquals(1, pq('sup', $field)->length, "is mandatory of $name");
67        }
68
69        $label = $field->parent();
70        $this->assertTrue($label->is('label'), "find label of $name");
71
72        $input = pq($check, $label);
73        $this->assertEquals(1, $input->length, "find check of $name");
74
75        return $input;
76    }
77
78    public function test_parseline() {
79        $match = 'textbox label0 "Test with spaces"
80textbox LabelWithoutSpaces
81textbox Label Without Spaces
82textbox "Label with spaces" "Text with a quote""in text"
83textbox Label2 " "
84textbox Label3 """"
85textbox Label4 " """ " """   " """
86textbox Label5 """ "
87textbox Label6 "" " "
88textbox Label7 " "" "
89textbox Label7 " ""
90 "" ss"
91textbox Label8';
92
93        $expected = array(
94            array('textbox', 'label0', 'Test with spaces'),
95            array('textbox', 'LabelWithoutSpaces'),
96            array('textbox', 'Label', 'Without', 'Spaces'),
97            array('textbox', 'Label with spaces', 'Text with a quote"in text'),
98            array('textbox', 'Label2', ' '),
99            array('textbox', 'Label3', '"'),
100            array('textbox', 'Label4', ' "', ' "', ' "'),
101            array('textbox', 'Label5', '" '),
102            array('textbox', 'Label6', '', ' '),
103            array('textbox', 'Label7', ' " '),
104            array('textbox', 'Label7', ' "
105 " ss'),
106            array('textbox', 'Label8')
107        );
108
109        $lines = explode("\n", $match);
110        $i = 0;
111        while(count($lines) > 0) {
112            $line = trim(array_shift($lines));
113
114            $syntaxcomponent = new syntax_plugin_bureaucracyau();
115            $actual = $this->callNonaccessibleMethod($syntaxcomponent, '_parse_line', array($line, &$lines));
116
117            $this->assertEquals($expected[$i], $actual);
118            $i++;
119        }
120
121    }
122
123    /**
124     * Test not accessible methods..
125     *
126     * @param string|object $obj
127     * @param string $name
128     * @param array $args
129     * @return mixed
130     */
131    protected function callNonaccessibleMethod($obj, $name, array $args) {
132        $class = new ReflectionClass($obj);
133        $method = $class->getMethod($name);
134        $method->setAccessible(true);
135        return $method->invokeArgs($obj, $args);
136    }
137
138}
139