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