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