1<?php 2 3namespace dokuwiki\plugin\dw2pdf\test; 4 5use dokuwiki\plugin\dw2pdf\src\Config; 6use DokuWikiTest; 7 8/** 9 * @group plugin_dw2pdf 10 * @group plugins 11 */ 12class ConfigTest extends DokuWikiTest 13{ 14 public function setUp(): void 15 { 16 parent::setUp(); 17 $_REQUEST = []; 18 } 19 20 /** 21 * Check default values as set in the Config class 22 */ 23 public function testDefaults(): void 24 { 25 global $conf, $ID; 26 $ID = ''; 27 28 $config = new Config(); 29 $mpdfConfig = $config->getMPdfConfig(); 30 31 $this->assertSame('A4', $config->getFormat(), 'default pagesize/orientation'); 32 $this->assertFalse($config->hasToc(), 'default toc'); 33 $this->assertSame(5, $config->getMaxBookmarks(), 'default maxbookmarks'); 34 $this->assertFalse($config->useNumberedHeaders(), 'default headernumber'); 35 $this->assertSame('', $config->getWatermarkText(), 'default watermark'); 36 $this->assertSame('default', $config->getTemplateName(), 'default template'); 37 $this->assertSame('file', $config->getOutputTarget(), 'default output'); 38 $this->assertSame([], $config->getStyledExtensions(), 'default usestyles'); 39 $this->assertSame(0.0, $config->getQRScale(), 'default qrcodescale'); 40 $this->assertFalse($config->isDebugEnabled(), 'default debug'); 41 $this->assertNull($config->getBookTitle(), 'default book_title'); 42 $this->assertSame('', $config->getBookNamespace(), 'default book_ns'); 43 $this->assertSame('natural', $config->getBookSortOrder(), 'default book_order'); 44 $this->assertSame(0, $config->getBookNamespaceDepth(), 'default book_nsdepth'); 45 $this->assertSame([], $config->getBookExcludedPages(), 'default excludes'); 46 $this->assertSame([], $config->getBookExcludedNamespaces(), 'default excludesns'); 47 $this->assertFalse($config->hasLiveSelection(), 'default selection flag'); 48 $this->assertNull($config->getLiveSelection(), 'default selection'); 49 $this->assertFalse($config->hasSavedSelection(), 'default savedselection flag'); 50 $this->assertNull($config->getSavedSelection(), 'default savedselection'); 51 $this->assertSame('', $config->getExportId(), 'default exportid'); 52 53 $this->assertSame('A4', $mpdfConfig['format'], 'default pagesize/orientation'); 54 $this->assertSame(11, $mpdfConfig['default_font_size'], 'default font-size'); 55 $this->assertSame($conf['tmpdir'] . '/mpdf', $mpdfConfig['tempDir'], 'default tmpdir'); 56 $this->assertFalse($mpdfConfig['mirrorMargins'], 'default doublesided'); 57 $this->assertSame([], $mpdfConfig['h2toc'], 'default toc levels'); 58 $this->assertFalse($mpdfConfig['showWatermarkText'], 'default watermark'); 59 $this->assertSame('stretch', $mpdfConfig['setAutoTopMargin'], 'default mpdf auto top margin'); 60 $this->assertSame('stretch', $mpdfConfig['setAutoBottomMargin'], 'default mpdf auto bottom margin'); 61 $this->assertTrue($mpdfConfig['autoScriptToLang'], 'default mpdf autoScriptToLang'); 62 $this->assertSame(1, $mpdfConfig['baseScript'], 'default mpdf baseScript'); 63 $this->assertTrue($mpdfConfig['autoVietnamese'], 'default mpdf autoVietnamese'); 64 $this->assertTrue($mpdfConfig['autoArabic'], 'default mpdf autoArabic'); 65 $this->assertTrue($mpdfConfig['autoLangToFont'], 'default mpdf autoLangToFont'); 66 $this->assertTrue($mpdfConfig['ignore_invalid_utf8'], 'default mpdf ignore_invalid_utf8'); 67 $this->assertSame(4, $mpdfConfig['tabSpaces'], 'default mpdf tabSpaces'); 68 } 69 70 /** 71 * Ensure overrides from config work as expected 72 */ 73 public function testloadPluginConfig(): void 74 { 75 $config = new Config([ 76 'exportid' => 'playground:start', 77 'pagesize' => 'Legal', 78 'orientation' => 'landscape', 79 'font-size' => 14, 80 'doublesided' => 0, 81 'toc' => 1, 82 'toclevels' => '2-4', 83 'maxbookmarks' => 3, 84 'headernumber' => 1, 85 'template' => 'modern', 86 'output' => 'inline', 87 'usestyles' => 'wrap,foo ', 88 'watermark' => 'CONFIDENTIAL', 89 'qrcodescale' => '2.5', 90 'debug' => 1, 91 'booktitle' => 'My Book', 92 'booknamespace' => 'playground:sub', 93 'booksortorder' => 'date', 94 'booknamespacedepth' => 2, 95 'bookexcludepages' => ['playground:sub:skip'], 96 'bookexcludenamespaces' => ['playground:private'], 97 'liveselection' => '["playground:start","playground:Sub:Child"]', 98 'savedselection' => 'fav:123', 99 ]); 100 $mpdfConfig = $config->getMPdfConfig(); 101 102 $this->assertSame('playground:start', $config->getExportId(), 'from exportid'); 103 $this->assertSame('Legal-L', $config->getFormat(), 'from pagesize + orientation'); 104 $this->assertSame(14, $mpdfConfig['default_font_size'], 'from font-size'); 105 $this->assertFalse($mpdfConfig['mirrorMargins'], 'from doublesided'); 106 $this->assertTrue($config->hasToc(), 'from toc'); 107 $this->assertSame(['H2' => 1, 'H3' => 2, 'H4' => 3], $mpdfConfig['h2toc'], 'from toclevels'); 108 $this->assertSame(3, $config->getMaxBookmarks(), 'from maxbookmarks'); 109 $this->assertTrue($config->useNumberedHeaders(), 'from headernumber'); 110 $this->assertSame('modern', $config->getTemplateName(), 'from template'); 111 $this->assertSame('inline', $config->getOutputTarget(), 'from output'); 112 $this->assertSame(['wrap', 'foo'], $config->getStyledExtensions(), 'from usestyles'); 113 $this->assertSame('CONFIDENTIAL', $config->getWatermarkText(), 'from watermark'); 114 $this->assertTrue($mpdfConfig['showWatermarkText'], 'from watermark'); 115 $this->assertSame(2.5, $config->getQRScale(), 'from qrcodescale'); 116 $this->assertTrue($config->isDebugEnabled(), 'from debug'); 117 $this->assertSame('My Book', $config->getBookTitle(), 'from booktitle'); 118 $this->assertSame('playground:sub', $config->getBookNamespace(), 'from booknamespace'); 119 $this->assertSame('date', $config->getBookSortOrder(), 'from booksortorder'); 120 $this->assertSame(2, $config->getBookNamespaceDepth(), 'from booknamespacedepth'); 121 $this->assertSame(['playground:sub:skip'], $config->getBookExcludedPages(), 'from bookexcludepages'); 122 $this->assertSame(['playground:private'], $config->getBookExcludedNamespaces(), 'from bookexcludenamespaces'); 123 $this->assertTrue($config->hasLiveSelection(), 'from liveselection'); 124 $this->assertSame('["playground:start","playground:Sub:Child"]', $config->getLiveSelection(), 'from liveselection'); 125 $this->assertTrue($config->hasSavedSelection(), 'from savedselection'); 126 $this->assertSame('fav:123', $config->getSavedSelection(), 'from savedselection'); 127 $this->assertNotEmpty($config->getCacheKey(), 'from combined plugin config values'); 128 } 129 130 /** 131 * Ensure toc levels are set to DokuWiki's default when toc is enabled but no levels are set 132 */ 133 public function testDefaultTocLevels() 134 { 135 $config = new Config(['toc' => 1]); 136 $mpdfConfig = $config->getMPdfConfig(); 137 $this->assertSame(['H1' => 0, 'H2' => 1, 'H3' => 2], $mpdfConfig['h2toc'], 'from toclevels'); 138 } 139 140 /** 141 * Ensure request parameters take precedence over defaults 142 */ 143 public function testloadInputConfig(): void 144 { 145 global $INPUT, $ID, $conf; 146 $ID = 'playground:start'; 147 $conf['allowdebug'] = 1; // required for the debug URL parameter to take effect 148 $INPUT->set('pagesize', 'Legal'); 149 $INPUT->set('orientation', 'landscape'); 150 $INPUT->set('font-size', '14'); 151 $INPUT->set('doublesided', '0'); 152 $INPUT->set('toc', '1'); 153 $INPUT->set('toclevels', '2-4'); 154 $INPUT->set('watermark', 'CONFIDENTIAL'); 155 $INPUT->set('tpl', 'modern'); 156 $INPUT->set('debug', '1'); 157 $INPUT->set('outputTarget', 'inline'); 158 $INPUT->set('book_title', 'My Book'); 159 $INPUT->set('book_ns', 'playground:sub'); 160 $INPUT->set('book_order', 'date'); 161 $INPUT->set('book_nsdepth', 2); 162 $INPUT->set('excludes', ['playground:sub:skip']); 163 $INPUT->set('excludesns', ['playground:private']); 164 $INPUT->set('selection', '["playground:start","playground:Sub:Child"]'); 165 $INPUT->set('savedselection', 'fav:123'); 166 167 168 $config = new Config(); 169 $mpdfConfig = $config->getMPdfConfig(); 170 171 $this->assertSame('playground:start', $config->getExportId(), 'from $ID'); 172 $this->assertSame('Legal-L', $config->getFormat(), 'from pagesize + orientation'); 173 $this->assertSame(14, $mpdfConfig['default_font_size'], 'from font-size'); 174 $this->assertFalse($mpdfConfig['mirrorMargins'], 'from doublesided'); 175 $this->assertSame(['H2' => 1, 'H3' => 2, 'H4' => 3], $mpdfConfig['h2toc'], 'from toclevels'); 176 $this->assertTrue($mpdfConfig['showWatermarkText'], 'from watermark'); 177 $this->assertSame('CONFIDENTIAL', $config->getWatermarkText(), 'from watermark'); 178 $this->assertSame('modern', $config->getTemplateName(), 'from tpl'); 179 $this->assertTrue($config->isDebugEnabled(), 'from debug'); 180 $this->assertSame('inline', $config->getOutputTarget(), 'from outputTarget'); 181 $this->assertSame('My Book', $config->getBookTitle(), 'from book_title'); 182 $this->assertSame('playground:sub', $config->getBookNamespace(), 'from book_ns'); 183 $this->assertSame('date', $config->getBookSortOrder(), 'from book_order'); 184 $this->assertSame(2, $config->getBookNamespaceDepth(), 'from book_nsdepth'); 185 $this->assertSame(['playground:sub:skip'], $config->getBookExcludedPages(), 'from excludes'); 186 $this->assertSame(['playground:private'], $config->getBookExcludedNamespaces(), 'from excludesns'); 187 $this->assertTrue($config->hasLiveSelection(), 'from selection'); 188 $this->assertSame('["playground:start","playground:Sub:Child"]', $config->getLiveSelection(), 'from selection'); 189 $this->assertTrue($config->hasSavedSelection(), 'from savedselection'); 190 $this->assertSame('fav:123', $config->getSavedSelection(), 'from savedselection'); 191 192 } 193 194 /** 195 * The debug flag may only be enabled through the URL when core debugging is allowed 196 */ 197 public function testDebugUrlRequiresAllowDebug(): void 198 { 199 global $INPUT, $conf; 200 $INPUT->set('debug', '1'); 201 202 $conf['allowdebug'] = 0; 203 $this->assertFalse((new Config())->isDebugEnabled(), 'debug via URL ignored without allowdebug'); 204 205 $conf['allowdebug'] = 1; 206 $this->assertTrue((new Config())->isDebugEnabled(), 'debug via URL honored with allowdebug'); 207 } 208 209 /** 210 * The debug flag set through the plugin configuration is trusted regardless of allowdebug 211 */ 212 public function testDebugConfigIgnoresAllowDebug(): void 213 { 214 global $conf; 215 $conf['allowdebug'] = 0; 216 217 $this->assertTrue((new Config(['debug' => 1]))->isDebugEnabled(), 'debug via config honored'); 218 } 219} 220