summaryrefslogtreecommitdiff
path: root/platform/www/lib/plugins/bureaucracy/_test/syntax.test.php
blob: 9b552e0f2b2f61955e98fe76baffbc5225c203ec (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
 * @group plugin_bureaucracy
 * @group plugins
 */
class syntax_plugin_bureaucracy_test extends DokuWikiTest {

    protected $pluginsEnabled = array('bureaucracy');

    public function test_generalFormOutput() {
        $input = file_get_contents(dirname(__FILE__) . '/input.txt');
        $xhtml = p_render('xhtml', p_get_instructions($input), $info);

        $doc = phpQuery::newDocument($xhtml);

        $this->assertEquals(1, pq('form.bureaucracy__plugin', $doc)->length);
        $this->assertEquals(6, pq('form.bureaucracy__plugin fieldset', $doc)->length);

        // standard input types
        $this->checkField($doc, 'Employee Name', 'input[type=text][value=Your Name].edit', true);
        $this->checkField($doc, 'Your Age', 'input[type=text].edit', true);
        $this->checkField($doc, 'Your E-Mail Address', 'input[type=text].edit', true);
        $this->checkField($doc, 'Occupation (optional)', 'input[type=text].edit');
        $this->checkField($doc, 'Some password', 'input[type=password].edit', true);

        // select field
        $select = $this->checkField($doc, 'Please select an option', 'select');
        $this->assertEquals(3, pq('option', $select)->length);
        $this->assertEquals(1, pq('option:selected', $select)->length);
        $this->assertEquals('Peaches', pq('option:selected', $select)->val());

        // static text
        $this->assertEquals(1, pq('p:contains(Some static text)', $doc)->length);

        // checkbox
        $cb = $this->checkField($doc, 'Read the agreement?', 'input[type=checkbox][value=1]');
        $this->assertEquals('1', pq('input[type=hidden][value=0]', $cb->parent())->length);

        // text area
        $this->checkField($doc, 'Tell me about your self', 'textarea.edit', true);

        // file field
        $this->checkField($doc, 'File1', 'input[type=file].edit', true);

        // submit button
        $this->assertEquals(1, pq('button[type=submit]:contains(Submit Query)')->length);

    }

    public function test_HTMLinclusion() {
        $input = file_get_contents(dirname(__FILE__) . '/input.txt');
        $xhtml = p_render('xhtml', p_get_instructions($input), $info);

        $doc = phpQuery::newDocument($xhtml);

        // HTML Check - there should be no bold tag anywhere
        $this->assertEquals(0, pq('bold', $doc)->length);
    }

    private function checkField($doc, $name, $check, $required=false) {

        $field = pq('form.bureaucracy__plugin label span:contains(' . $name . ')', $doc);
        $this->assertEquals(1, $field->length, "find span of $name");

        if($required){
            $this->assertEquals(1, pq('sup', $field)->length, "is mandatory of $name");
        }

        $label = $field->parent();
        $this->assertTrue($label->is('label'), "find label of $name");

        $input = pq($check, $label);
        $this->assertEquals(1, $input->length, "find check of $name");

        return $input;
    }

    public function test_parseline() {
        $match = 'textbox label0 "Test with spaces"
textbox LabelWithoutSpaces
textbox Label Without Spaces
textbox "Label with spaces" "Text with a quote""in text"
textbox Label2 " "
textbox Label3 """"
textbox Label4 " """ " """   " """
textbox Label5 """ "
textbox Label6 "" " "
textbox Label7 " "" "
textbox Label7 " ""
 "" ss"
textbox Label8';

        $expected = array(
            array('textbox', 'label0', 'Test with spaces'),
            array('textbox', 'LabelWithoutSpaces'),
            array('textbox', 'Label', 'Without', 'Spaces'),
            array('textbox', 'Label with spaces', 'Text with a quote"in text'),
            array('textbox', 'Label2', ' '),
            array('textbox', 'Label3', '"'),
            array('textbox', 'Label4', ' "', ' "', ' "'),
            array('textbox', 'Label5', '" '),
            array('textbox', 'Label6', '', ' '),
            array('textbox', 'Label7', ' " '),
            array('textbox', 'Label7', ' "
 " ss'),
            array('textbox', 'Label8')
        );

        $lines = explode("\n", $match);
        $i = 0;
        while(count($lines) > 0) {
            $line = trim(array_shift($lines));

            $syntaxcomponent = new syntax_plugin_bureaucracy();
            $actual = $this->callNonaccessibleMethod($syntaxcomponent, '_parse_line', array($line, &$lines));

            $this->assertEquals($expected[$i], $actual);
            $i++;
        }

    }

    /**
     * Test not accessible methods..
     *
     * @param string|object $obj
     * @param string $name
     * @param array $args
     * @return mixed
     */
    protected function callNonaccessibleMethod($obj, $name, array $args) {
        $class = new ReflectionClass($obj);
        $method = $class->getMethod($name);
        $method->setAccessible(true);
        return $method->invokeArgs($obj, $args);
    }

}