xref: /plugin/bpmnio/_test/action_plugin_bpmnio_editor.test.php (revision 36b712d809a9afeda77eb7dba8abf621818208c9)
1<?php
2
3/**
4 * @group plugin_bpmnio
5 * @group plugins
6 */
7class action_plugin_bpmnio_editor_test extends DokuWikiTest
8{
9    protected $pluginsEnabled = array('bpmnio');
10
11    public function tearDown(): void
12    {
13        parent::tearDown();
14
15        global $TEXT;
16        global $RANGE;
17        $TEXT = null;
18        $RANGE = null;
19    }
20
21    /**
22     * Test that the editor plugin can be loaded
23     */
24    public function test_plugin_load()
25    {
26        $plugin = plugin_load('action', 'bpmnio_editor');
27        $this->assertNotNull($plugin, 'Editor action plugin should be loadable');
28    }
29
30    /**
31     * Test section edit button for BPMN target
32     */
33    public function test_section_edit_button_bpmn()
34    {
35        $plugin = plugin_load('action', 'bpmnio_editor');
36
37        $data = ['target' => 'plugin_bpmnio_bpmn', 'name' => ''];
38        $event = new \dokuwiki\Extension\Event('HTML_SECEDIT_BUTTON', $data);
39
40        $plugin->sectionEditButton($event);
41
42        $this->assertNotEmpty($data['name'], 'Button name should be set for BPMN target');
43    }
44
45    /**
46     * Test section edit button for DMN target
47     */
48    public function test_section_edit_button_dmn()
49    {
50        $plugin = plugin_load('action', 'bpmnio_editor');
51
52        $data = ['target' => 'plugin_bpmnio_dmn', 'name' => ''];
53        $event = new \dokuwiki\Extension\Event('HTML_SECEDIT_BUTTON', $data);
54
55        $plugin->sectionEditButton($event);
56
57        $this->assertNotEmpty($data['name'], 'Button name should be set for DMN target');
58    }
59
60    /**
61     * Test section edit button ignores non-bpmnio targets
62     */
63    public function test_section_edit_button_ignores_other()
64    {
65        $plugin = plugin_load('action', 'bpmnio_editor');
66
67        $data = ['target' => 'section', 'name' => ''];
68        $event = new \dokuwiki\Extension\Event('HTML_SECEDIT_BUTTON', $data);
69
70        $plugin->sectionEditButton($event);
71
72        $this->assertEmpty($data['name'], 'Button name should not be set for non-bpmnio targets');
73    }
74
75    /**
76     * Test handlePost does nothing when plugin data not in POST
77     */
78    public function test_handle_post_noop_without_data()
79    {
80        $plugin = plugin_load('action', 'bpmnio_editor');
81
82        global $TEXT;
83        $TEXT = 'original';
84        global $INPUT;
85
86        $data = 'edit';
87        $event = new \dokuwiki\Extension\Event('ACTION_ACT_PREPROCESS', $data);
88
89        $plugin->handlePost($event);
90
91        $this->assertEquals('original', $TEXT, '$TEXT should not change when plugin data is not posted');
92    }
93
94    public function test_handle_form_uses_render_payload_but_preserves_original_text()
95    {
96        $plugin = plugin_load('action', 'bpmnio_editor');
97
98        global $TEXT;
99        global $RANGE;
100        $TEXT = '<?xml version="1.0"?><definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"><process id="Process_1"><task id="Task_1" name="[[:docs:start|Read docs]]" /></process></definitions>';
101        $RANGE = '1-10';
102
103        $form = new class {
104            /** @var array<string, string> */
105            public array $hiddenFields = [];
106
107            public string $html = '';
108
109            public function setHiddenField(string $name, string $value): void
110            {
111                $this->hiddenFields[$name] = $value;
112            }
113
114            public function addHTML(string $html): void
115            {
116                $this->html .= $html;
117            }
118        };
119        $data = ['target' => 'plugin_bpmnio_bpmn', 'form' => $form];
120        $event = new \dokuwiki\Extension\Event('EDIT_FORM_ADDTEXTAREA', $data);
121
122        $plugin->handleForm($event);
123
124        $this->assertArrayHasKey('plugin_bpmnio_data', $form->hiddenFields);
125        $this->assertArrayHasKey('plugin_bpmnio_links', $form->hiddenFields);
126        $this->assertEquals(base64_encode($TEXT), $form->hiddenFields['plugin_bpmnio_data']);
127        $this->assertStringContainsString('id="plugin_bpmnio__bpmn_editor"', $form->html);
128        $this->assertStringContainsString('<div class="bpmn_js_canvas">', $form->html);
129        $this->assertSame(5, substr_count($form->html, '<div'));
130        $this->assertSame(5, substr_count($form->html, '</div>'));
131
132        preg_match('/<div class="bpmn_js_data">(.*?)<\/div>/s', $form->html, $xmlMatch);
133        $this->assertNotEmpty($xmlMatch[1]);
134        $decodedXml = base64_decode(trim($xmlMatch[1]), true);
135        $this->assertNotFalse($decodedXml);
136        $this->assertStringContainsString('name="Read docs"', $decodedXml);
137
138        preg_match('/<div class="bpmn_js_links">(.*?)<\/div>/s', $form->html, $linkMatch);
139        $this->assertNotEmpty($linkMatch[1]);
140        $decodedLinks = base64_decode(trim($linkMatch[1]), true);
141        $links = json_decode($decodedLinks, true);
142        $this->assertArrayHasKey('Task_1', $links);
143        $this->assertEquals('docs:start', $links['Task_1']['target']);
144    }
145}
146