1<?php
2/**
3 * @group plugin_bureaucracyau
4 * @group plugins
5 */
6class syntax_plugin_bureaucracyau_fieldfile_test extends DokuWikiTest {
7
8    protected $pluginsEnabled = array('bureaucracy');
9
10    /**
11     * Parse doku $syntax and check if any resulting xhtml element can be selected by $pqSelector
12     *
13     * @param $syntax
14     * @param $pqSelector
15     */
16    protected function assertPqSelector($syntax, $pqSelector) {
17        $xhtml = p_render('xhtml', p_get_instructions($syntax), $info);
18        $doc = phpQuery::newDocument($xhtml);
19        $result = pq($pqSelector, $doc);
20        $this->assertEquals(1, $result->length, "selector: \"$pqSelector\" not found in\n$xhtml\n");
21    }
22
23    /**
24     * Chceck if defined namespace doesn't violate $standardArgs
25     */
26    function test_syntax() {
27        $standardArgs = array(
28            '!' => 'input[type=file].edit',
29            '^' => 'input[type=file][required].edit.required',
30            '@' => 'input[type=file][required].edit.required',
31            '! /regex/' => 'input[type=file].edit',
32            '@ /regex/ "**Example error"' => 'input[type=file][required].edit.required'
33        );
34
35        //upload namespace not defined
36        foreach ($standardArgs as $arg => $pqSelector) {
37            $input = "<form>\nfile \"Some label\" $arg\n</form>";
38            $this->assertPqSelector($input, $pqSelector);
39        }
40
41        //upload namespace defined, nothing shoud change in syntax
42        foreach ($standardArgs as $arg => $pqSelector) {
43            $input = "<form>\nfile \"Some label\" upload:here $arg\n</form>";
44            $this->assertPqSelector($input, $pqSelector);
45        }
46
47        //upload namespace in ""
48        foreach ($standardArgs as $arg => $pqSelector) {
49            $input = "<form>\nfile \"Some label\" \"upload:here\" $arg\n</form>";
50            $this->assertPqSelector($input, $pqSelector);
51        }
52    }
53
54    /**
55     * Parse the bureaucracy form syntax and simulate a file upload
56     *
57     * @param $form_syntax bureaucracy form syntax containg only one file field
58     * @return string a name of the uploaded file
59     */
60    protected function simulate_file_upload($form_syntax) {
61        $media = 'img.png';
62        $media_src = mediaFN('wiki:dokuwiki-128.png');
63
64        $syntax_plugin = new syntax_plugin_bureaucracyau();
65        $data = $syntax_plugin->handle($form_syntax, 0, 0, new Doku_Handler());
66
67        $actionData = $data['actions'][0];
68        $action = plugin_load('helper', $actionData['actionname']);
69
70        $fileField = $data['fields'][0];
71
72        //mock file upload
73        $file = array(
74            'name'     => $media,
75            'type'     => 'image/png',
76            'size'     => filesize($media_src),
77            'tmp_name' => $media_src
78        );
79        //this is the only field
80        $index = 0;
81        //this is the only form
82        $form_id = 0;
83        $fileField->handle_post($file, $data['fields'], $index, $form_id);
84
85        //upload file
86        $action->run(
87            $data['fields'],
88            $data['thanks'],
89            $actionData['argv']
90        );
91
92        return $media;
93    }
94
95    function test_action_template_upload_default() {
96        $template_id = 'template_upload_default';
97        $id = 'upload_default';
98
99        saveWikiText($template_id, 'Value:@@Some label@@', 'summary');
100
101        $form_syntax = "<form>action template $template_id $id\nfile \"Some label\"\n</form>";
102        $media = $this->simulate_file_upload($form_syntax);
103
104        //check if file exists where we suspect it to be
105        $this->assertTrue(file_exists(mediaFN("$id:$media")));
106    }
107
108    function test_action_template_upload_absolute() {
109        $template_id = 'template_upload_absolute';
110        $id = 'upload_absolute';
111        $upload_ns = 'upload:ns';
112
113        saveWikiText($template_id, 'Value:@@Some label@@', 'summary');
114
115        $form_syntax = "<form>action template $template_id $id\nfile \"Some label\" $upload_ns\n</form>";
116        $media = $this->simulate_file_upload($form_syntax);
117
118        //check if file exists where we suspect it to be
119        $this->assertTrue(file_exists(mediaFN("$upload_ns:$media")));
120    }
121
122    function test_action_template_upload_relative() {
123        $template_id = 'template_upload_relative';
124        $id = 'upload_relative';
125        $upload_ns_rel = 'upload:ns';
126        $upload_ns = ".:$upload_ns_rel";
127
128        saveWikiText($template_id, 'Value:@@Some label@@', 'summary');
129
130        $form_syntax = "<form>action template $template_id $id\nfile \"Some label\" \"$upload_ns\"\n</form>";
131        $media = $this->simulate_file_upload($form_syntax);
132
133        //check if file exists where we suspect it to be
134        $this->assertTrue(file_exists(mediaFN("$id:$upload_ns:$media")));
135    }
136
137}
138