1<?php
2
3namespace dokuwiki\plugin\struct\test;
4
5use dokuwiki\plugin\bureaucracy\test\BureaucracyTest;
6use dokuwiki\plugin\struct\meta\AccessTable;
7
8/**
9 * Tests for the integration with Bureaucracy plugin
10 *
11 * @group plugin_struct
12 * @group plugins
13 *
14 */
15class Bureaucracy_struct_test extends StructTest
16{
17
18    /** @var array alway enable the needed plugins */
19    protected $pluginsEnabled = array('struct', 'sqlite', 'bureaucracy');
20
21    /** @var array of lookup data */
22    protected $lookup = array();
23
24    public function setUp(): void
25    {
26        parent::setUp();
27
28        $this->loadSchemaJSON('bureaucracy_lookup');
29        $this->loadSchemaJSON('bureaucracy');
30
31        //insert some data to lookup
32        for ($i = 1; $i <= 10; ++$i) {
33            $data = array(
34                'lookup_first' => 'value first ' . $i,
35                'lookup_second' => 'value second ' . $i
36            );
37
38            $lookupData = AccessTable::getGlobalAccess('bureaucracy_lookup');
39            $lookupData->saveData($data);
40            $this->lookup[] = $lookupData;
41        }
42    }
43
44    public function test_bureaucracy_lookup_replacement_empty()
45    {
46        //page created by bureaucracy
47        $id = 'bureaucracy_lookup_replacement_empty';
48        //id of template page
49        $template_id = 'template';
50
51        //create template
52        saveWikiText($template_id, 'Value:@@bureaucracy.lookup_select@@', 'summary');
53
54        //build form
55        $fields = array();
56
57        $lookup_field = plugin_load('helper', 'struct_field');
58        $lookup_field->opt['label'] = 'bureaucracy.lookup_select';
59        //empty lookup value
60        $lookup_field->opt['value'] = '';
61        //left pagename undefined
62        //$lookup_field->opt['pagename'];
63
64        //$args are ommited in struct_field
65        $lookup_field->initialize(array());
66        $fields[] = $lookup_field;
67
68        /** @var  \helper_plugin_bureaucracy_actiontemplate $actiontemplate */
69        $actiontemplate = plugin_load('helper', 'bureaucracy_actiontemplate');
70        $actiontemplate->run($fields, '', array($template_id, $id, '_'));
71
72        $page_content = io_readWikiPage(wikiFN($id), $id);
73
74        $this->assertEquals('Value:', $page_content);
75    }
76
77    public function test_bureaucracy_lookup_replacement()
78    {
79        //page created by bureaucracy
80        $id = 'bureaucracy_lookup_replacement';
81        //id of template page
82        $template_id = 'template';
83        //rid of selected value
84        $lookup_rid = $this->lookup[0]->getRid();
85        //selected value
86        $lookup_value = $this->lookup[0]->getData()['lookup_first']->getValue();
87
88        //create template
89        saveWikiText($template_id, 'Value:@@bureaucracy.lookup_select@@', 'summary');
90
91        //build form
92        $fields = array();
93
94        $lookup_field = plugin_load('helper', 'struct_field');
95        $lookup_field->opt['label'] = 'bureaucracy.lookup_select';
96        $lookup_field->opt['value'] = json_encode(['', $lookup_rid]);
97        //left pagename undefined
98        //$lookup_field->opt['pagename'];
99
100        //$args are ommited in struct_field
101        $lookup_field->initialize(array());
102        $fields[] = $lookup_field;
103
104        /* @var \helper_plugin_bureaucracy_actiontemplate $actiontemplate */
105        $actiontemplate = plugin_load('helper', 'bureaucracy_actiontemplate');
106        $actiontemplate->run($fields, '', array($template_id, $id, '_'));
107
108        $page_content = io_readWikiPage(wikiFN($id), $id);
109
110        $this->assertEquals('Value:' . $lookup_value, $page_content);
111    }
112
113    public function test_bureaucracy_multi_field()
114    {
115        $this->loadSchemaJSON('schema1');
116
117        $formSyntax = [
118            'struct_field "schema1.first"',
119            'struct_field "schema1.second"',
120        ];
121        $templateSyntax = "staticPrefix @@schema1.first@@ staticPostfix\nmulti: @@schema1.second@@ multipost";
122        $values = ['foo', ['bar', 'baz']];
123
124        $bWrapper = new bureaucracyTestWrapper();
125        $actualWikitext = $bWrapper->send_form_action_template(
126            $formSyntax,
127            $templateSyntax,
128            $errors,
129            ...$values
130        );
131
132        $expectedSyntax = "staticPrefix foo staticPostfix\nmulti: bar, baz multipost";
133        $this->assertEquals($expectedSyntax, $actualWikitext);
134        $this->assertEmpty($errors);
135    }
136}
137
138
139class bureaucracyTestWrapper extends BureaucracyTest
140{
141    public function send_form_action_template($form_syntax, $template_syntax, &$validation_errors, ...$values)
142    {
143        return parent::send_form_action_template($form_syntax, $template_syntax, $validation_errors, ...$values);
144    }
145}
146