xref: /plugin/dw2pdf/_test/ConfigTest.php (revision a9cd524ed3465a8385c4a367213f90bb9717f5b7)
192200750SAndreas Gohr<?php
292200750SAndreas Gohr
392200750SAndreas Gohrnamespace dokuwiki\plugin\dw2pdf\test;
492200750SAndreas Gohr
592200750SAndreas Gohruse dokuwiki\plugin\dw2pdf\src\Config;
692200750SAndreas Gohruse DokuWikiTest;
792200750SAndreas Gohr
892200750SAndreas Gohr/**
992200750SAndreas Gohr * @group plugin_dw2pdf
1092200750SAndreas Gohr * @group plugins
1192200750SAndreas Gohr */
1292200750SAndreas Gohrclass ConfigTest extends DokuWikiTest
1392200750SAndreas Gohr{
1492200750SAndreas Gohr    public function setUp(): void
1592200750SAndreas Gohr    {
1692200750SAndreas Gohr        parent::setUp();
1792200750SAndreas Gohr        $_REQUEST = [];
1892200750SAndreas Gohr    }
1992200750SAndreas Gohr
2092200750SAndreas Gohr    /**
21212a1ea5SAndreas Gohr     * Check default values as set in the Config class
2292200750SAndreas Gohr     */
23212a1ea5SAndreas Gohr    public function testDefaults(): void
24212a1ea5SAndreas Gohr    {
25212a1ea5SAndreas Gohr        global $conf, $ID;
26212a1ea5SAndreas Gohr        $ID = '';
27212a1ea5SAndreas Gohr
28212a1ea5SAndreas Gohr        $config = new Config();
29212a1ea5SAndreas Gohr        $mpdfConfig = $config->getMPdfConfig();
30212a1ea5SAndreas Gohr
31212a1ea5SAndreas Gohr        $this->assertSame('A4', $config->getFormat(), 'default pagesize/orientation');
32212a1ea5SAndreas Gohr        $this->assertFalse($config->hasToc(), 'default toc');
33212a1ea5SAndreas Gohr        $this->assertSame(5, $config->getMaxBookmarks(), 'default maxbookmarks');
34212a1ea5SAndreas Gohr        $this->assertFalse($config->useNumberedHeaders(), 'default headernumber');
35212a1ea5SAndreas Gohr        $this->assertSame('', $config->getWatermarkText(), 'default watermark');
36212a1ea5SAndreas Gohr        $this->assertSame('default', $config->getTemplateName(), 'default template');
37212a1ea5SAndreas Gohr        $this->assertSame('file', $config->getOutputTarget(), 'default output');
38212a1ea5SAndreas Gohr        $this->assertSame([], $config->getStyledExtensions(), 'default usestyles');
39212a1ea5SAndreas Gohr        $this->assertSame(0.0, $config->getQRScale(), 'default qrcodescale');
40212a1ea5SAndreas Gohr        $this->assertFalse($config->isDebugEnabled(), 'default debug');
41212a1ea5SAndreas Gohr        $this->assertNull($config->getBookTitle(), 'default book_title');
42212a1ea5SAndreas Gohr        $this->assertSame('', $config->getBookNamespace(), 'default book_ns');
43212a1ea5SAndreas Gohr        $this->assertSame('natural', $config->getBookSortOrder(), 'default book_order');
44212a1ea5SAndreas Gohr        $this->assertSame(0, $config->getBookNamespaceDepth(), 'default book_nsdepth');
45212a1ea5SAndreas Gohr        $this->assertSame([], $config->getBookExcludedPages(), 'default excludes');
46212a1ea5SAndreas Gohr        $this->assertSame([], $config->getBookExcludedNamespaces(), 'default excludesns');
47212a1ea5SAndreas Gohr        $this->assertFalse($config->hasLiveSelection(), 'default selection flag');
48212a1ea5SAndreas Gohr        $this->assertNull($config->getLiveSelection(), 'default selection');
49212a1ea5SAndreas Gohr        $this->assertFalse($config->hasSavedSelection(), 'default savedselection flag');
50212a1ea5SAndreas Gohr        $this->assertNull($config->getSavedSelection(), 'default savedselection');
51212a1ea5SAndreas Gohr        $this->assertSame('', $config->getExportId(), 'default exportid');
52212a1ea5SAndreas Gohr
53212a1ea5SAndreas Gohr        $this->assertSame('A4', $mpdfConfig['format'], 'default pagesize/orientation');
54212a1ea5SAndreas Gohr        $this->assertSame(11, $mpdfConfig['default_font_size'], 'default font-size');
55212a1ea5SAndreas Gohr        $this->assertSame($conf['tmpdir'] . '/mpdf', $mpdfConfig['tempDir'], 'default tmpdir');
56212a1ea5SAndreas Gohr        $this->assertFalse($mpdfConfig['mirrorMargins'], 'default doublesided');
57212a1ea5SAndreas Gohr        $this->assertSame([], $mpdfConfig['h2toc'], 'default toc levels');
58212a1ea5SAndreas Gohr        $this->assertFalse($mpdfConfig['showWatermarkText'], 'default watermark');
59212a1ea5SAndreas Gohr        $this->assertSame('stretch', $mpdfConfig['setAutoTopMargin'], 'default mpdf auto top margin');
60212a1ea5SAndreas Gohr        $this->assertSame('stretch', $mpdfConfig['setAutoBottomMargin'], 'default mpdf auto bottom margin');
61212a1ea5SAndreas Gohr        $this->assertTrue($mpdfConfig['autoScriptToLang'], 'default mpdf autoScriptToLang');
62212a1ea5SAndreas Gohr        $this->assertSame(1, $mpdfConfig['baseScript'], 'default mpdf baseScript');
63212a1ea5SAndreas Gohr        $this->assertTrue($mpdfConfig['autoVietnamese'], 'default mpdf autoVietnamese');
64212a1ea5SAndreas Gohr        $this->assertTrue($mpdfConfig['autoArabic'], 'default mpdf autoArabic');
65212a1ea5SAndreas Gohr        $this->assertTrue($mpdfConfig['autoLangToFont'], 'default mpdf autoLangToFont');
66212a1ea5SAndreas Gohr        $this->assertTrue($mpdfConfig['ignore_invalid_utf8'], 'default mpdf ignore_invalid_utf8');
67212a1ea5SAndreas Gohr        $this->assertSame(4, $mpdfConfig['tabSpaces'], 'default mpdf tabSpaces');
68212a1ea5SAndreas Gohr    }
69212a1ea5SAndreas Gohr
70212a1ea5SAndreas Gohr    /**
71212a1ea5SAndreas Gohr     * Ensure overrides from config work as expected
72212a1ea5SAndreas Gohr     */
73212a1ea5SAndreas Gohr    public function testloadPluginConfig(): void
7492200750SAndreas Gohr    {
7592200750SAndreas Gohr        $config = new Config([
76212a1ea5SAndreas Gohr            'exportid' => 'playground:start',
77212a1ea5SAndreas Gohr            'pagesize' => 'Legal',
7892200750SAndreas Gohr            'orientation' => 'landscape',
7992200750SAndreas Gohr            'font-size' => 14,
8092200750SAndreas Gohr            'doublesided' => 0,
8192200750SAndreas Gohr            'toc' => 1,
8292200750SAndreas Gohr            'toclevels' => '2-4',
8392200750SAndreas Gohr            'maxbookmarks' => 3,
8492200750SAndreas Gohr            'headernumber' => 1,
85212a1ea5SAndreas Gohr            'template' => 'modern',
86212a1ea5SAndreas Gohr            'output' => 'inline',
8792200750SAndreas Gohr            'usestyles' => 'wrap,foo ',
8892200750SAndreas Gohr            'watermark' => 'CONFIDENTIAL',
8992200750SAndreas Gohr            'qrcodescale' => '2.5',
90212a1ea5SAndreas Gohr            'debug' => 1,
91212a1ea5SAndreas Gohr            'booktitle' => 'My Book',
92212a1ea5SAndreas Gohr            'booknamespace' => 'playground:sub',
93212a1ea5SAndreas Gohr            'booksortorder' => 'date',
94212a1ea5SAndreas Gohr            'booknamespacedepth' => 2,
95212a1ea5SAndreas Gohr            'bookexcludepages' => ['playground:sub:skip'],
96212a1ea5SAndreas Gohr            'bookexcludenamespaces' => ['playground:private'],
97212a1ea5SAndreas Gohr            'liveselection' => '["playground:start","playground:Sub:Child"]',
98212a1ea5SAndreas Gohr            'savedselection' => 'fav:123',
9992200750SAndreas Gohr        ]);
10092200750SAndreas Gohr        $mpdfConfig = $config->getMPdfConfig();
101212a1ea5SAndreas Gohr
102212a1ea5SAndreas Gohr        $this->assertSame('playground:start', $config->getExportId(), 'from exportid');
103212a1ea5SAndreas Gohr        $this->assertSame('Legal-L', $config->getFormat(), 'from pagesize + orientation');
104212a1ea5SAndreas Gohr        $this->assertSame(14, $mpdfConfig['default_font_size'], 'from font-size');
105212a1ea5SAndreas Gohr        $this->assertFalse($mpdfConfig['mirrorMargins'], 'from doublesided');
106212a1ea5SAndreas Gohr        $this->assertTrue($config->hasToc(), 'from toc');
107212a1ea5SAndreas Gohr        $this->assertSame(['H2' => 1, 'H3' => 2, 'H4' => 3], $mpdfConfig['h2toc'], 'from toclevels');
108212a1ea5SAndreas Gohr        $this->assertSame(3, $config->getMaxBookmarks(), 'from maxbookmarks');
109212a1ea5SAndreas Gohr        $this->assertTrue($config->useNumberedHeaders(), 'from headernumber');
110212a1ea5SAndreas Gohr        $this->assertSame('modern', $config->getTemplateName(), 'from template');
111212a1ea5SAndreas Gohr        $this->assertSame('inline', $config->getOutputTarget(), 'from output');
112212a1ea5SAndreas Gohr        $this->assertSame(['wrap', 'foo'], $config->getStyledExtensions(), 'from usestyles');
113212a1ea5SAndreas Gohr        $this->assertSame('CONFIDENTIAL', $config->getWatermarkText(), 'from watermark');
114212a1ea5SAndreas Gohr        $this->assertTrue($mpdfConfig['showWatermarkText'], 'from watermark');
115212a1ea5SAndreas Gohr        $this->assertSame(2.5, $config->getQRScale(), 'from qrcodescale');
116212a1ea5SAndreas Gohr        $this->assertTrue($config->isDebugEnabled(), 'from debug');
117212a1ea5SAndreas Gohr        $this->assertSame('My Book', $config->getBookTitle(), 'from booktitle');
118212a1ea5SAndreas Gohr        $this->assertSame('playground:sub', $config->getBookNamespace(), 'from booknamespace');
119212a1ea5SAndreas Gohr        $this->assertSame('date', $config->getBookSortOrder(), 'from booksortorder');
120212a1ea5SAndreas Gohr        $this->assertSame(2, $config->getBookNamespaceDepth(), 'from booknamespacedepth');
121212a1ea5SAndreas Gohr        $this->assertSame(['playground:sub:skip'], $config->getBookExcludedPages(), 'from bookexcludepages');
122212a1ea5SAndreas Gohr        $this->assertSame(['playground:private'], $config->getBookExcludedNamespaces(), 'from bookexcludenamespaces');
123212a1ea5SAndreas Gohr        $this->assertTrue($config->hasLiveSelection(), 'from liveselection');
124212a1ea5SAndreas Gohr        $this->assertSame('["playground:start","playground:Sub:Child"]', $config->getLiveSelection(), 'from liveselection');
125212a1ea5SAndreas Gohr        $this->assertTrue($config->hasSavedSelection(), 'from savedselection');
126212a1ea5SAndreas Gohr        $this->assertSame('fav:123', $config->getSavedSelection(), 'from savedselection');
127212a1ea5SAndreas Gohr        $this->assertNotEmpty($config->getCacheKey(), 'from combined plugin config values');
12892200750SAndreas Gohr    }
12992200750SAndreas Gohr
13092200750SAndreas Gohr    /**
131212a1ea5SAndreas Gohr     * Ensure toc levels are set to DokuWiki's default when toc is enabled but no levels are set
13292200750SAndreas Gohr     */
133212a1ea5SAndreas Gohr    public function testDefaultTocLevels()
13492200750SAndreas Gohr    {
135212a1ea5SAndreas Gohr        $config = new Config(['toc' => 1]);
136212a1ea5SAndreas Gohr        $mpdfConfig = $config->getMPdfConfig();
137212a1ea5SAndreas Gohr        $this->assertSame(['H1' => 0, 'H2' => 1, 'H3' => 2], $mpdfConfig['h2toc'], 'from toclevels');
138212a1ea5SAndreas Gohr    }
139212a1ea5SAndreas Gohr
140212a1ea5SAndreas Gohr    /**
141212a1ea5SAndreas Gohr     * Ensure request parameters take precedence over defaults
142212a1ea5SAndreas Gohr     */
143212a1ea5SAndreas Gohr    public function testloadInputConfig(): void
144212a1ea5SAndreas Gohr    {
145*a9cd524eSAndreas Gohr        global $INPUT, $ID, $conf;
146212a1ea5SAndreas Gohr        $ID = 'playground:start';
147*a9cd524eSAndreas Gohr        $conf['allowdebug'] = 1; // required for the debug URL parameter to take effect
14892200750SAndreas Gohr        $INPUT->set('pagesize', 'Legal');
14992200750SAndreas Gohr        $INPUT->set('orientation', 'landscape');
150212a1ea5SAndreas Gohr        $INPUT->set('font-size', '14');
15192200750SAndreas Gohr        $INPUT->set('doublesided', '0');
152212a1ea5SAndreas Gohr        $INPUT->set('toc', '1');
153212a1ea5SAndreas Gohr        $INPUT->set('toclevels', '2-4');
154212a1ea5SAndreas Gohr        $INPUT->set('watermark', 'CONFIDENTIAL');
155212a1ea5SAndreas Gohr        $INPUT->set('tpl', 'modern');
15692200750SAndreas Gohr        $INPUT->set('debug', '1');
157212a1ea5SAndreas Gohr        $INPUT->set('outputTarget', 'inline');
15892200750SAndreas Gohr        $INPUT->set('book_title', 'My Book');
15992200750SAndreas Gohr        $INPUT->set('book_ns', 'playground:sub');
16092200750SAndreas Gohr        $INPUT->set('book_order', 'date');
16192200750SAndreas Gohr        $INPUT->set('book_nsdepth', 2);
16292200750SAndreas Gohr        $INPUT->set('excludes', ['playground:sub:skip']);
16392200750SAndreas Gohr        $INPUT->set('excludesns', ['playground:private']);
16492200750SAndreas Gohr        $INPUT->set('selection', '["playground:start","playground:Sub:Child"]');
16592200750SAndreas Gohr        $INPUT->set('savedselection', 'fav:123');
166212a1ea5SAndreas Gohr
16792200750SAndreas Gohr
16892200750SAndreas Gohr        $config = new Config();
169212a1ea5SAndreas Gohr        $mpdfConfig = $config->getMPdfConfig();
17092200750SAndreas Gohr
171212a1ea5SAndreas Gohr        $this->assertSame('playground:start', $config->getExportId(), 'from $ID');
172212a1ea5SAndreas Gohr        $this->assertSame('Legal-L', $config->getFormat(), 'from pagesize + orientation');
173212a1ea5SAndreas Gohr        $this->assertSame(14, $mpdfConfig['default_font_size'], 'from font-size');
174212a1ea5SAndreas Gohr        $this->assertFalse($mpdfConfig['mirrorMargins'], 'from doublesided');
175212a1ea5SAndreas Gohr        $this->assertSame(['H2' => 1, 'H3' => 2, 'H4' => 3], $mpdfConfig['h2toc'], 'from toclevels');
176212a1ea5SAndreas Gohr        $this->assertTrue($mpdfConfig['showWatermarkText'], 'from watermark');
177212a1ea5SAndreas Gohr        $this->assertSame('CONFIDENTIAL', $config->getWatermarkText(), 'from watermark');
178212a1ea5SAndreas Gohr        $this->assertSame('modern', $config->getTemplateName(), 'from tpl');
179212a1ea5SAndreas Gohr        $this->assertTrue($config->isDebugEnabled(), 'from debug');
180212a1ea5SAndreas Gohr        $this->assertSame('inline', $config->getOutputTarget(), 'from outputTarget');
181212a1ea5SAndreas Gohr        $this->assertSame('My Book', $config->getBookTitle(), 'from book_title');
182212a1ea5SAndreas Gohr        $this->assertSame('playground:sub', $config->getBookNamespace(), 'from book_ns');
183212a1ea5SAndreas Gohr        $this->assertSame('date', $config->getBookSortOrder(), 'from book_order');
184212a1ea5SAndreas Gohr        $this->assertSame(2, $config->getBookNamespaceDepth(), 'from book_nsdepth');
185212a1ea5SAndreas Gohr        $this->assertSame(['playground:sub:skip'], $config->getBookExcludedPages(), 'from excludes');
186212a1ea5SAndreas Gohr        $this->assertSame(['playground:private'], $config->getBookExcludedNamespaces(), 'from excludesns');
187212a1ea5SAndreas Gohr        $this->assertTrue($config->hasLiveSelection(), 'from selection');
188212a1ea5SAndreas Gohr        $this->assertSame('["playground:start","playground:Sub:Child"]', $config->getLiveSelection(), 'from selection');
189212a1ea5SAndreas Gohr        $this->assertTrue($config->hasSavedSelection(), 'from savedselection');
190212a1ea5SAndreas Gohr        $this->assertSame('fav:123', $config->getSavedSelection(), 'from savedselection');
191212a1ea5SAndreas Gohr
19292200750SAndreas Gohr    }
193212a1ea5SAndreas Gohr
194*a9cd524eSAndreas Gohr    /**
195*a9cd524eSAndreas Gohr     * The debug flag may only be enabled through the URL when core debugging is allowed
196*a9cd524eSAndreas Gohr     */
197*a9cd524eSAndreas Gohr    public function testDebugUrlRequiresAllowDebug(): void
198*a9cd524eSAndreas Gohr    {
199*a9cd524eSAndreas Gohr        global $INPUT, $conf;
200*a9cd524eSAndreas Gohr        $INPUT->set('debug', '1');
201212a1ea5SAndreas Gohr
202*a9cd524eSAndreas Gohr        $conf['allowdebug'] = 0;
203*a9cd524eSAndreas Gohr        $this->assertFalse((new Config())->isDebugEnabled(), 'debug via URL ignored without allowdebug');
204*a9cd524eSAndreas Gohr
205*a9cd524eSAndreas Gohr        $conf['allowdebug'] = 1;
206*a9cd524eSAndreas Gohr        $this->assertTrue((new Config())->isDebugEnabled(), 'debug via URL honored with allowdebug');
207*a9cd524eSAndreas Gohr    }
208*a9cd524eSAndreas Gohr
209*a9cd524eSAndreas Gohr    /**
210*a9cd524eSAndreas Gohr     * The debug flag set through the plugin configuration is trusted regardless of allowdebug
211*a9cd524eSAndreas Gohr     */
212*a9cd524eSAndreas Gohr    public function testDebugConfigIgnoresAllowDebug(): void
213*a9cd524eSAndreas Gohr    {
214*a9cd524eSAndreas Gohr        global $conf;
215*a9cd524eSAndreas Gohr        $conf['allowdebug'] = 0;
216*a9cd524eSAndreas Gohr
217*a9cd524eSAndreas Gohr        $this->assertTrue((new Config(['debug' => 1]))->isDebugEnabled(), 'debug via config honored');
218*a9cd524eSAndreas Gohr    }
21992200750SAndreas Gohr}
220