xref: /plugin/bpmnio/_test/syntax_plugin_bpmnio_bpmnio.test.php (revision c88bd154bd573c8ceefeb9b009eba97536aec54c)
1<?php
2
3/**
4 * @group plugin_bpmnio
5 * @group plugins
6 */
7class syntax_plugin_bpmnio_test extends DokuWikiTest
8{
9    protected $pluginsEnabled = array('bpmnio');
10
11    public function test_syntax_bpmn()
12    {
13        $info = array();
14        $expected = <<<OUT
15        <div class="plugin-bpmnio" id="__bpmn_js_1"><div class="bpmn_js_data">
16            WE1MLi4u
17        </div>
18        <div class="bpmn_js_links">
19            W10=
20        </div><div class="bpmn_js_canvas sectionedit1">
21            <div class="bpmn_js_container"></div>
22        </div><!-- EDIT{&quot;target&quot;:&quot;plugin_bpmnio_bpmn&quot;,&quot;secid&quot;:1,&quot;range&quot;:&quot;21-29&quot;} --></div>
23        OUT;
24
25        $input = <<<IN
26        <bpmnio type="bpmn">
27        XML...
28        </bpmnio>
29        IN;
30
31        $instructions = p_get_instructions($input);
32        $xhtml = p_render('xhtml', $instructions, $info);
33
34        $this->assertEquals($expected, $xhtml);
35    }
36
37    public function test_syntax_dmn()
38    {
39        $info = array();
40        $expected = <<<OUT
41        <div class="plugin-bpmnio" id="__dmn_js_1"><div class="dmn_js_data">
42            WE1MLi4u
43        </div>
44        <div class="dmn_js_links">
45            W10=
46        </div><div class="dmn_js_canvas sectionedit1">
47            <div class="dmn_js_container"></div>
48        </div><!-- EDIT{&quot;target&quot;:&quot;plugin_bpmnio_dmn&quot;,&quot;secid&quot;:1,&quot;range&quot;:&quot;20-28&quot;} --></div>
49        OUT;
50
51        $input = <<<IN
52        <bpmnio type="dmn">
53        XML...
54        </bpmnio>
55        IN;
56
57        $instructions = p_get_instructions($input);
58        $xhtml = p_render('xhtml', $instructions, $info);
59
60        $this->assertEquals($expected, $xhtml);
61    }
62
63    /**
64     * Test that type defaults to bpmn when not specified
65     */
66    public function test_syntax_default_type()
67    {
68        $info = array();
69
70        $input = <<<IN
71        <bpmnio>
72        XML...
73        </bpmnio>
74        IN;
75
76        $instructions = p_get_instructions($input);
77        $xhtml = p_render('xhtml', $instructions, $info);
78
79        $this->assertStringContainsString('bpmn_js_data', $xhtml);
80        $this->assertStringContainsString('bpmn_js_canvas', $xhtml);
81        $this->assertStringContainsString('bpmn_js_container', $xhtml);
82    }
83
84    /**
85     * Test empty content between tags
86     */
87    public function test_syntax_empty_content()
88    {
89        $info = array();
90
91        $input = <<<IN
92        <bpmnio type="bpmn">
93        </bpmnio>
94        IN;
95
96        $instructions = p_get_instructions($input);
97        $xhtml = p_render('xhtml', $instructions, $info);
98
99        // Should still produce the structure, with base64 of whitespace/empty
100        $this->assertStringContainsString('plugin-bpmnio', $xhtml);
101        $this->assertStringContainsString('bpmn_js_data', $xhtml);
102    }
103
104    /**
105     * Test that multiline XML content is properly base64-encoded
106     */
107    public function test_syntax_multiline_content()
108    {
109        $info = array();
110
111        $input = <<<IN
112        <bpmnio type="bpmn">
113        <?xml version="1.0"?>
114        <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL">
115        </definitions>
116        </bpmnio>
117        IN;
118
119        $instructions = p_get_instructions($input);
120        $xhtml = p_render('xhtml', $instructions, $info);
121
122        $this->assertStringContainsString('bpmn_js_data', $xhtml);
123        // Verify the data section contains valid base64
124        preg_match('/<div class="bpmn_js_data">\s*(.*?)\s*<\/div>/s', $xhtml, $matches);
125        $this->assertNotEmpty($matches[1]);
126        $decoded = base64_decode(trim($matches[1]), true);
127        $this->assertNotFalse($decoded, 'Content should be valid base64');
128        $this->assertStringContainsString('definitions', $decoded);
129    }
130
131    /**
132     * Test that the plugin produces section edit markers for inline content
133     */
134    public function test_syntax_section_edit_bpmn()
135    {
136        $info = array();
137
138        $input = <<<IN
139        <bpmnio type="bpmn">
140        Content
141        </bpmnio>
142        IN;
143
144        $instructions = p_get_instructions($input);
145        $xhtml = p_render('xhtml', $instructions, $info);
146
147        $this->assertStringContainsString('sectionedit', $xhtml);
148        $this->assertStringContainsString('plugin_bpmnio_bpmn', $xhtml);
149    }
150
151    /**
152     * Test that the plugin produces section edit markers for DMN inline content
153     */
154    public function test_syntax_section_edit_dmn()
155    {
156        $info = array();
157
158        $input = <<<IN
159        <bpmnio type="dmn">
160        Content
161        </bpmnio>
162        IN;
163
164        $instructions = p_get_instructions($input);
165        $xhtml = p_render('xhtml', $instructions, $info);
166
167        $this->assertStringContainsString('sectionedit', $xhtml);
168        $this->assertStringContainsString('plugin_bpmnio_dmn', $xhtml);
169    }
170
171    /**
172     * Test that unrecognized text outside <bpmnio> is not affected
173     */
174    public function test_syntax_no_interference()
175    {
176        $info = array();
177
178        $input = <<<IN
179        Hello World
180        <bpmnio type="bpmn">
181        XML...
182        </bpmnio>
183        Goodbye World
184        IN;
185
186        $instructions = p_get_instructions($input);
187        $xhtml = p_render('xhtml', $instructions, $info);
188
189        $this->assertStringContainsString('Hello World', $xhtml);
190        $this->assertStringContainsString('Goodbye World', $xhtml);
191        $this->assertStringContainsString('plugin-bpmnio', $xhtml);
192    }
193
194    public function test_syntax_zoom_attribute()
195    {
196        $info = array();
197
198        $input = <<<IN
199        <bpmnio type="bpmn" zoom="0.5">
200        XML...
201        </bpmnio>
202        IN;
203
204        $instructions = p_get_instructions($input);
205        $xhtml = p_render('xhtml', $instructions, $info);
206
207        $this->assertStringContainsString('data-zoom="0.5"', $xhtml);
208    }
209
210    public function test_syntax_ignores_invalid_zoom_attribute()
211    {
212        $info = array();
213
214        $input = <<<IN
215        <bpmnio type="bpmn" zoom="0">
216        XML...
217        </bpmnio>
218        IN;
219
220        $instructions = p_get_instructions($input);
221        $xhtml = p_render('xhtml', $instructions, $info);
222
223        $this->assertStringNotContainsString('data-zoom=', $xhtml);
224    }
225
226    public function test_syntax_lint_attribute()
227    {
228        $info = array();
229
230        $input = <<<IN
231        <bpmnio type="bpmn" lint="on">
232        XML...
233        </bpmnio>
234        IN;
235
236        $instructions = p_get_instructions($input);
237        $xhtml = p_render('xhtml', $instructions, $info);
238
239        $this->assertStringContainsString('data-lint="on"', $xhtml);
240    }
241
242    public function test_syntax_lint_off_attribute()
243    {
244        $info = array();
245
246        $input = <<<IN
247        <bpmnio type="bpmn" lint="off">
248        XML...
249        </bpmnio>
250        IN;
251
252        $instructions = p_get_instructions($input);
253        $xhtml = p_render('xhtml', $instructions, $info);
254
255        $this->assertStringContainsString('data-lint="off"', $xhtml);
256    }
257
258    public function test_syntax_ignores_invalid_lint_attribute()
259    {
260        $info = array();
261
262        $input = <<<IN
263        <bpmnio type="bpmn" lint="bogus">
264        XML...
265        </bpmnio>
266        IN;
267
268        $instructions = p_get_instructions($input);
269        $xhtml = p_render('xhtml', $instructions, $info);
270
271        $this->assertStringNotContainsString('data-lint=', $xhtml);
272    }
273
274    public function test_syntax_lint_and_zoom_coexist()
275    {
276        $info = array();
277
278        $input = <<<IN
279        <bpmnio type="bpmn" zoom="1.5" lint="inactive">
280        XML...
281        </bpmnio>
282        IN;
283
284        $instructions = p_get_instructions($input);
285        $xhtml = p_render('xhtml', $instructions, $info);
286
287        $this->assertStringContainsString('data-zoom="1.5"', $xhtml);
288        $this->assertStringContainsString('data-lint="inactive"', $xhtml);
289    }
290
291        public function test_syntax_builds_link_payload_for_named_elements()
292        {
293                $info = array();
294                io_mkdir_p(dirname(wikiFN('docs:start')));
295                io_saveFile(wikiFN('docs:start'), 'Read docs');
296
297                $input = <<<IN
298                <bpmnio type="bpmn">
299                <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL">
300                    <process id="Process_1">
301                        <task id="Task_1" name="[[:docs:start|Read docs]]" />
302                    </process>
303                </definitions>
304                </bpmnio>
305                IN;
306
307                $instructions = p_get_instructions($input);
308                $xhtml = p_render('xhtml', $instructions, $info);
309
310                preg_match('/<div class="bpmn_js_data">\s*(.*?)\s*<\/div>/s', $xhtml, $xmlMatch);
311                $this->assertNotEmpty($xmlMatch[1]);
312                $decodedXml = base64_decode(trim($xmlMatch[1]), true);
313                $this->assertNotFalse($decodedXml);
314                $this->assertStringContainsString('name="Read docs"', $decodedXml);
315                $this->assertStringNotContainsString('[[', $decodedXml);
316
317                preg_match('/<div class="bpmn_js_links">\s*(.*?)\s*<\/div>/s', $xhtml, $linkMatch);
318                $this->assertNotEmpty($linkMatch[1]);
319                $decodedLinks = base64_decode(trim($linkMatch[1]), true);
320                $this->assertNotFalse($decodedLinks);
321                $links = json_decode($decodedLinks, true);
322
323                $this->assertIsArray($links);
324                $this->assertArrayHasKey('Task_1', $links);
325                $this->assertEquals('/doku.php?id=docs%3Astart', $links['Task_1']['href']);
326                $this->assertEquals('docs:start', $links['Task_1']['target']);
327        }
328
329        public function test_syntax_keeps_unlinked_names_unchanged()
330        {
331                $info = array();
332
333                $input = <<<IN
334                <bpmnio type="dmn">
335                <?xml version="1.0" encoding="UTF-8"?>
336                <definitions xmlns="https://www.omg.org/spec/DMN/20191111/MODEL/" id="Definitions_1">
337                    <decision id="Decision_1" name="Approve Order" />
338                </definitions>
339                </bpmnio>
340                IN;
341
342                $instructions = p_get_instructions($input);
343                $xhtml = p_render('xhtml', $instructions, $info);
344
345                preg_match('/<div class="dmn_js_data">\s*(.*?)\s*<\/div>/s', $xhtml, $xmlMatch);
346                $decodedXml = base64_decode(trim($xmlMatch[1]), true);
347                $this->assertNotFalse($decodedXml);
348                $this->assertStringContainsString('Approve Order', $decodedXml);
349
350                preg_match('/<div class="dmn_js_links">\s*(.*?)\s*<\/div>/s', $xhtml, $linkMatch);
351                $decodedLinks = base64_decode(trim($linkMatch[1]), true);
352                $this->assertSame('[]', $decodedLinks);
353        }
354
355    /**
356     * Test the handle method directly for ENTER state
357     */
358    public function test_handle_enter_state()
359    {
360        $plugin = plugin_load('syntax', 'bpmnio_bpmnio');
361        $this->assertNotNull($plugin, 'Plugin should be loadable');
362
363        $handler = new Doku_Handler();
364        $result = $plugin->handle('<bpmnio type="bpmn">', DOKU_LEXER_ENTER, 0, $handler);
365
366        $this->assertEquals(DOKU_LEXER_ENTER, $result[0]);
367        $this->assertEquals('bpmn', $result[1]);
368    }
369
370    /**
371     * Test the handle method directly for EXIT state
372     */
373    public function test_handle_exit_state()
374    {
375        $plugin = plugin_load('syntax', 'bpmnio_bpmnio');
376        $handler = new Doku_Handler();
377        $result = $plugin->handle('</bpmnio>', DOKU_LEXER_EXIT, 0, $handler);
378
379        $this->assertEquals(DOKU_LEXER_EXIT, $result[0]);
380    }
381
382    /**
383     * Test that the plugin is correctly registered
384     */
385    public function test_plugin_registration()
386    {
387        $plugin = plugin_load('syntax', 'bpmnio_bpmnio');
388        $this->assertNotNull($plugin, 'Plugin should be loadable');
389        $this->assertEquals('block', $plugin->getPType());
390        $this->assertEquals('protected', $plugin->getType());
391        $this->assertEquals(0, $plugin->getSort());
392    }
393}
394