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