1<?php
2
3namespace dokuwiki\plugin\struct\test;
4
5use Doku_Event;
6
7/**
8 * @group plugin_struct
9 * @group plugins
10 *
11 * @covers \action_plugin_struct_output
12 */
13class output_struct_test extends StructTest
14{
15
16    /** @var array add the extra plugins */
17    protected $pluginsEnabled = array('struct', 'sqlite', 'log', 'include');
18
19    public function setUp(): void
20    {
21        parent::setUp();
22
23        $this->loadSchemaJSON('schema1');
24        $this->loadSchemaJSON('schema_3');
25
26        $page = 'page01';
27        $includedPage = 'foo';
28        $now = time();
29        $this->saveData(
30            $page,
31            'schema1',
32            array(
33                'first' => 'first data',
34                'second' => array('second data', 'more data', 'even more'),
35                'third' => 'third data',
36                'fourth' => 'fourth data'
37            ),
38            $now
39        );
40        $this->saveData(
41            $page,
42            'schema_3',
43            array(
44                'first_field' => 'first data',
45                'second field' => array('second data', 'more data', 'even more'),
46                'third%field' => 'third data',
47                'fourth.field?' => 'fourth data'
48            ),
49            $now
50        );
51        $this->saveData(
52            $includedPage,
53            'schema1',
54            array(
55                'first' => 'first data',
56                'second' => array('second data', 'more data', 'even more'),
57                'third' => 'third data',
58                'fourth' => 'fourth data'
59            ),
60            $now
61        );
62    }
63
64    public function test_output()
65    {
66        global $ID;
67        $page = 'page01';
68        $ID = $page;
69
70        saveWikiText($page, "====== abc ======\ndef", '');
71        $instructions = p_cached_instructions(wikiFN($page), false, $page);
72        $this->assertEquals('document_start', $instructions[0][0]);
73        $this->assertEquals('header', $instructions[1][0]);
74        $this->assertEquals('plugin', $instructions[2][0]);
75        $this->assertEquals('struct_output', $instructions[2][1][0]);
76    }
77
78    public function test_include_missing_output()
79    {
80        global $ID;
81        $page = 'page01';
82        $includedPage = 'foo';
83
84        saveWikiText($page, "====== abc ======\n{{page>foo}}\n", '');
85        saveWikiText($includedPage, "====== included page ======\nqwe\n", '');
86
87
88        plugin_load('action', 'struct_output', true);
89        $ID = $page;
90        $insMainPage = p_cached_instructions(wikiFN($page), false, $page);
91        $this->assertEquals('document_start', $insMainPage[0][0]);
92        $this->assertEquals('header', $insMainPage[1][0]);
93        $this->assertEquals('plugin', $insMainPage[2][0]);
94        $this->assertEquals('struct_output', $insMainPage[2][1][0]);
95
96        plugin_load('action', 'struct_output', true);
97        $ID = $includedPage;
98        $insIncludedPage = p_cached_instructions(wikiFN($includedPage), false, $includedPage);
99        $this->assertEquals('document_start', $insIncludedPage[0][0]);
100        $this->assertEquals('header', $insIncludedPage[1][0]);
101        $this->assertEquals('plugin', $insIncludedPage[2][0], 'The struct data of a page that has been included should still be displayed when viewed on its own.');
102        $this->assertEquals('struct_output', $insIncludedPage[2][1][0]);
103
104    }
105
106    public function test_log_conflict()
107    {
108        global $ID;
109        $page = 'page01';
110        $ID = $page;
111
112        saveWikiText($page, "====== abc ======\n{{log}}\n", '');
113        saveWikiText($page . ':log', '====== abc log ======
114Log for [[page01]]:
115
116  * 2017-02-24 10:54:13 //Example User//: foo bar', '');
117        $instructions = p_cached_instructions(wikiFN($page), false, $page);
118        $this->assertEquals('document_start', $instructions[0][0]);
119        $this->assertEquals('header', $instructions[1][0]);
120        $this->assertEquals('plugin', $instructions[2][0], 'The struct data should be rendererd after the first headline');
121        $this->assertEquals('struct_output', $instructions[2][1][0]);
122    }
123
124    /**
125     * Replace and clean up template placeholders
126     * provided by a dw2pdf event
127     */
128    public function test_dw2pdf_replacements()
129    {
130        $page = 'page01';
131        $replace = ['@ID@' => $page];
132        $content = <<< HTML
133<table class="pdffooter">
134    <tr>
135        <td style="text-align: left">ID: @ID@</td>
136        <td style="text-align: center">Version: @ID@@@PLUGIN_STRUCT_schema_3_version@~@PLUGIN_STRUCT_schema_3_first_field@</td>
137        <td style="text-align: right">Second data: @PLUGIN_STRUCT_schema_3_second field@</td>
138    </tr>
139</table>
140HTML;
141        $processed = <<< HTML
142<table class="pdffooter">
143    <tr>
144        <td style="text-align: left">ID: page01</td>
145        <td style="text-align: center">Version: page01@~first data</td>
146        <td style="text-align: right">Second data: second data, more data, even more</td>
147    </tr>
148</table>
149HTML;
150
151        $evdata = ['id' => $page, 'replace' => &$replace, 'content' => &$content];
152        $event = new Doku_Event('PLUGIN_DW2PDF_REPLACE', $evdata);
153        if ($event->advise_before()) {
154            $content = str_replace(array_keys($replace), array_values($replace), $content);
155        }
156        $event->advise_after();
157
158        $this->assertEquals($processed, $content);
159    }
160}
161