ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
ilWkhtmlToPdfConfigTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
29 class ilWkhtmlToPdfConfigTest extends TestCase
30 {
31  private const COOKIE_STRING = '--cookie "PHPSESSID" "" --cookie "ilClientId" "1" ';
32 
34  protected string $default_start = ' --zoom 1 --enable-external-links --disable-forms ';
35  protected string $default_end = "--orientation Portrait --page-size 'A4' --javascript-delay 500 --margin-bottom '0.5cm' --margin-left '0.5cm' --margin-right '2cm' --margin-top '2cm' --quiet " . self::COOKIE_STRING;
36  protected string $default_quiet = "--quiet ";
37  protected string $second_quiet = '--quiet --cookie "PHPSESSID" "" --cookie "ilClientId" "1" ';
38 
39  protected function setUp(): void
40  {
41  if (!defined('CLIENT_ID')) {
42  define('CLIENT_ID', '1');
43  }
44  $this->config = new ilWkhtmlToPdfConfig();
45  }
46 
47  public function testInstanceCanBeCreated(): void
48  {
49  $this->assertInstanceOf('ilWkhtmlToPdfConfig', $this->config);
50  }
51 
52  public function testDefaultConfig(): void
53  {
54  $this->assertFalse($this->config->getEnabledForms());
55  $this->assertTrue($this->config->getExternalLinks());
56  $this->assertSame(500, $this->config->getJavascriptDelay());
57  $this->assertSame(1.0, $this->config->getZoom());
58  $this->assertSame('Portrait', $this->config->getOrientation());
59  $this->assertSame('A4', $this->config->getPageSize());
60  $this->assertSame('0.5cm', $this->config->getMarginLeft());
61  $this->assertSame('2cm', $this->config->getMarginRight());
62  $this->assertSame('0.5cm', $this->config->getMarginBottom());
63  $this->assertSame('2cm', $this->config->getMarginTop());
64  }
65 
66  public function testDefaultConfigCommandline(): void
67  {
68  $cmd = " --zoom 1 --enable-external-links --disable-forms --orientation Portrait --page-size 'A4' --javascript-delay 500 --margin-bottom '0.5cm' --margin-left '0.5cm' --margin-right '2cm' --margin-top '2cm' --quiet " . self::COOKIE_STRING;
69 
70  $this->assertSame($cmd, $this->config->getCommandLineConfig());
71  }
72 
73  public function testGetCommandLineConfigSimple(): void
74  {
75  $this->config->setOrientation('Portrait');
76  $this->config->setPageSize('A1');
77  $this->config->setZoom(0.5);
78  $this->config->setJavascriptDelay(500);
79  $this->config->setMarginLeft('2');
80  $this->config->setMarginRight('2');
81  $this->config->setMarginTop('2');
82  $this->config->setMarginBottom('2');
83  $exp = " --zoom 0.5 --enable-external-links --disable-forms --orientation Portrait --page-size 'A1' --javascript-delay 500 --margin-bottom '2' --margin-left '2' --margin-right '2' --margin-top '2' --quiet " . self::COOKIE_STRING;
84  $this->assertSame($exp, $this->config->getCommandLineConfig());
85  }
86 
87  public function testGetCommandLineConfigOnObject(): void
88  {
89  $exp = $this->default_start . $this->default_end;
90  $this->assertSame($exp, $this->config->getCommandLineConfig());
91  }
92 
93  public function testGetCommandLineConfigWithGrayscale(): void
94  {
95  $this->config->setGreyscale(true);
96  $exp = $this->default_start . "--grayscale " . $this->default_end;
97  $this->assertSame($exp, $this->config->getCommandLineConfig());
98  }
99 
101  {
102  $this->config->setHeaderType(ilPDFGenerationConstants::HEADER_TEXT);
103  $this->config->setHeaderTextLeft('Left');
104  $this->config->setHeaderTextCenter('Center');
105  $this->config->setHeaderTextRight('Right');
106  $this->config->setHeaderTextSpacing(2);
107  $exp = $this->default_start . "--orientation Portrait --page-size 'A4' --javascript-delay 500 --margin-bottom '0.5cm' --margin-left '0.5cm' --margin-right '2cm' --margin-top '2cm' --header-left 'Left' --header-center 'Center' --header-right 'Right' --header-spacing 2 --quiet --cookie \"PHPSESSID\" \"\" --cookie \"ilClientId\" \"1\" ";
108  $this->assertSame($exp, $this->config->getCommandLineConfig());
109  }
110 
112  {
113  $this->config->setHeaderType(ilPDFGenerationConstants::HEADER_HTML);
114  $this->config->setHeaderHtml('<div><b>Test</b></div>');
115  $this->config->setHeaderHtmlSpacing(2);
116  $exp = $this->default_start . "--orientation Portrait --page-size 'A4' --javascript-delay 500 --margin-bottom '0.5cm' --margin-left '0.5cm' --margin-right '2cm' --margin-top '2cm' --header-html '<div><b>Test</b></div>' --header-spacing 2 --quiet " . self::COOKIE_STRING;
117  $this->assertSame($exp, $this->config->getCommandLineConfig());
118  }
119 
121  {
122  $this->config->setHeaderType(ilPDFGenerationConstants::HEADER_HTML);
123  $this->config->setHeaderHtml('<div><b>Test</b></div>');
124  $this->config->setHeaderHtmlSpacing(1);
125  $this->config->setHeaderTextLeft('Left');
126  $this->config->setHeaderTextCenter('Center');
127  $this->config->setHeaderTextRight('Right');
128  $exp = $this->default_start . "--orientation Portrait --page-size 'A4' --javascript-delay 500 --margin-bottom '0.5cm' --margin-left '0.5cm' --margin-right '2cm' --margin-top '2cm' --header-html '<div><b>Test</b></div>' --header-spacing 1 --quiet " . self::COOKIE_STRING;
129  $this->assertSame($exp, $this->config->getCommandLineConfig());
130  }
131 
133  {
134  $this->config->setFooterType(ilPDFGenerationConstants::FOOTER_TEXT);
135  $this->config->setFooterTextLeft('Left');
136  $this->config->setFooterTextCenter('Center');
137  $this->config->setFooterTextRight('Right');
138  $this->config->setFooterTextSpacing(2);
139  $exp = $this->default_start . "--orientation Portrait --page-size 'A4' --javascript-delay 500 --margin-bottom '0.5cm' --margin-left '0.5cm' --margin-right '2cm' --margin-top '2cm' --footer-left 'Left' --footer-center 'Center' --footer-right 'Right' --footer-spacing 2 --quiet " . self::COOKIE_STRING;
140  $this->assertSame($exp, $this->config->getCommandLineConfig());
141  }
142 
144  {
145  $this->config->setFooterType(ilPDFGenerationConstants::FOOTER_HTML);
146  $this->config->setFooterHtml('<div><b>Test</b></div>');
147  $this->config->setFooterHtmlSpacing(2);
148  $exp = $this->default_start . "--orientation Portrait --page-size 'A4' --javascript-delay 500 --margin-bottom '0.5cm' --margin-left '0.5cm' --margin-right '2cm' --margin-top '2cm' --footer-html '<div><b>Test</b></div>' --footer-spacing 2 --quiet " . self::COOKIE_STRING;
149  $this->assertSame($exp, $this->config->getCommandLineConfig());
150  }
151 
153  {
154  $this->config->setFooterType(ilPDFGenerationConstants::FOOTER_HTML);
155  $this->config->setFooterHtml('<div><b>Test</b></div>');
156  $this->config->setFooterHtmlLine(true);
157  $this->config->setFooterHtmlSpacing(1);
158  $exp = $this->default_start . "--orientation Portrait --page-size 'A4' --javascript-delay 500 --margin-bottom '0.5cm' --margin-left '0.5cm' --margin-right '2cm' --margin-top '2cm' --footer-html '<div><b>Test</b></div>' --footer-spacing 1 --footer-line --quiet " . self::COOKIE_STRING;
159  $this->assertSame($exp, $this->config->getCommandLineConfig());
160  }
161 
163  {
164  $this->config->setEnabledForms(true);
165  $exp = " --zoom 1 --enable-external-links --enable-forms --orientation Portrait --page-size 'A4' --javascript-delay 500 --margin-bottom '0.5cm' --margin-left '0.5cm' --margin-right '2cm' --margin-top '2cm' --quiet " . self::COOKIE_STRING;
166  $this->assertSame($exp, $this->config->getCommandLineConfig());
167  }
168 
170  {
171  $this->config->setExternalLinks(true);
172  $exp = " --zoom 1 --enable-external-links --disable-forms --orientation Portrait --page-size 'A4' --javascript-delay 500 --margin-bottom '0.5cm' --margin-left '0.5cm' --margin-right '2cm' --margin-top '2cm' --quiet " . self::COOKIE_STRING;
173  $this->assertSame($exp, $this->config->getCommandLineConfig());
174  }
175 
177  {
178  $this->config->setLowQuality(true);
179  $exp = " --zoom 1 --enable-external-links --disable-forms --lowquality --orientation Portrait --page-size 'A4' --javascript-delay 500 --margin-bottom '0.5cm' --margin-left '0.5cm' --margin-right '2cm' --margin-top '2cm' --quiet " . self::COOKIE_STRING;
180  $this->assertSame($exp, $this->config->getCommandLineConfig());
181  }
182 
183  protected string $default_margin_args = "--margin-bottom '0.5cm' --margin-left '0.5cm' --margin-right '2cm' --margin-top '2cm' ";
184 
186  {
187  $this->config->setPrintMediaType(true);
188  $exp = " --zoom 1 --enable-external-links --disable-forms --orientation Portrait --print-media-type --page-size 'A4' --javascript-delay 500 --margin-bottom '0.5cm' --margin-left '0.5cm' --margin-right '2cm' --margin-top '2cm' --quiet " . self::COOKIE_STRING;
189  $this->assertSame($exp, $this->config->getCommandLineConfig());
190  }
191 
192  public function testGetCommandLineConfigWithCheckbox(): void
193  {
194  $this->config->setCheckboxSvg('checkbox.svg');
195  $exp = " --zoom 1 --enable-external-links --disable-forms --orientation Portrait --page-size 'A4' --javascript-delay 500 --checkbox-svg 'checkbox.svg' --margin-bottom '0.5cm' --margin-left '0.5cm' --margin-right '2cm' --margin-top '2cm' --quiet " . self::COOKIE_STRING;
196  $this->assertSame($exp, $this->config->getCommandLineConfig());
197  }
198 
200  {
201  $this->config->setCheckboxCheckedSvg('checkbox_checked.svg');
202  $exp = " --zoom 1 --enable-external-links --disable-forms --orientation Portrait --page-size 'A4' --javascript-delay 500 --checkbox-checked-svg 'checkbox_checked.svg' --margin-bottom '0.5cm' --margin-left '0.5cm' --margin-right '2cm' --margin-top '2cm' --quiet " . self::COOKIE_STRING;
203  $this->assertSame($exp, $this->config->getCommandLineConfig());
204  }
205 
207  {
208  $this->config->setRadioButtonSvg('radiobutton.svg');
209  $exp = " --zoom 1 --enable-external-links --disable-forms --orientation Portrait --page-size 'A4' --javascript-delay 500 --radiobutton-svg 'radiobutton.svg' --margin-bottom '0.5cm' --margin-left '0.5cm' --margin-right '2cm' --margin-top '2cm' --quiet " . self::COOKIE_STRING;
210  $this->assertSame($exp, $this->config->getCommandLineConfig());
211  }
212 
214  {
215  $this->config->setRadioButtonCheckedSvg('radiobutton_checked.svg');
216  $exp = " --zoom 1 --enable-external-links --disable-forms --orientation Portrait --page-size 'A4' --javascript-delay 500 --radiobutton-checked-svg 'radiobutton_checked.svg' --margin-bottom '0.5cm' --margin-left '0.5cm' --margin-right '2cm' --margin-top '2cm' --quiet " . self::COOKIE_STRING;
217  $this->assertSame($exp, $this->config->getCommandLineConfig());
218  }
219 
221  {
222  $this->config->setExternalLinks(false);
223  $exp = " --zoom 1 --disable-external-links --disable-forms --orientation Portrait --page-size 'A4' --javascript-delay 500 --margin-bottom '0.5cm' --margin-left '0.5cm' --margin-right '2cm' --margin-top '2cm' --quiet " . self::COOKIE_STRING;
224  $this->assertSame($exp, $this->config->getCommandLineConfig());
225  }
226 
228  {
229  $this->config->setOrientation('Landscape');
230  $exp = " --zoom 1 --enable-external-links --disable-forms --orientation Landscape --page-size 'A4' --javascript-delay 500 --margin-bottom '0.5cm' --margin-left '0.5cm' --margin-right '2cm' --margin-top '2cm' --quiet " . self::COOKIE_STRING;
231  $this->assertSame($exp, $this->config->getCommandLineConfig());
232  }
233 
234  public function testSupportMultiSourceFiles(): void
235  {
236  $this->assertTrue($this->config::supportMultiSourcesFiles());
237  }
238 
240  {
241  $this->assertSame([], $this->config->getConfig());
242  }
243 
244  public function testReadConfigFromObject(): void
245  {
246  $this->config->setExternalLinks(false);
247  $this->config->setEnabledForms(true);
248  $cfg = new ilWkhtmlToPdfConfig($this->config);
249  $this->assertTrue($cfg->getEnabledForms());
250  $this->assertFalse($cfg->getExternalLinks());
251  }
252 
253  public function testReadConfigFromJson(): void
254  {
255  $json = [
256  "zoom" => "0.4",
257  "enable_forms" => "true",
258  "external_links" => "true",
259  "user_stylesheet" => "my_style_sheet.css",
260  "low_quality" => "0",
261  "greyscale" => "0",
262  "orientation" => "Landscape",
263  "page_size" => 'A1',
264  "margin_left" => "1cm",
265  "margin_right" => '2cm',
266  "footer_html_spacing" => 3,
267  "footer_html" => "<div>my html </div>",
268  "footer_text_line" => "1",
269  "footer_text_center" => "my footer text",
270  "footer_text_spacing" => 1,
271  "footer_text_right" => "right text",
272  "footer_text_left" => "left text",
273  "footer_select" => "0",
274  "head_html_spacing" => "1",
275  "head_html_line" => "0",
276  "head_text_line" => "1",
277  "head_text_spacing" => "1",
278  "head_text_right" => "head text right",
279  "head_text_center" => "head text center",
280  "head_text_left" => "head text left",
281  "header_select" => "1",
282  "radio_button_checked_svg" => "r_c.svg",
283  "radio_button_svg" => "r.svg",
284  "checkbox_checked_svg" => "c_c.svg",
285  "checkbox_svg" => "c.svg",
286  "javascript_delay" => "231",
287  "print_media_type" => "1",
288  "margin_top" => "5cm",
289  "margin_bottom" => "6cm",
290  ];
291  $cfg = new ilWkhtmlToPdfConfig($json);
292  $this->assertSame(1, $cfg->getHeaderHtmlSpacing());
293  $this->assertFalse($cfg->isHeaderHtmlLine());
294  $this->assertTrue($cfg->isHeaderTextLine());
295  $this->assertSame(1, $cfg->getHeaderTextSpacing());
296  $this->assertSame("head text right", $cfg->getHeaderTextRight());
297  $this->assertSame("head text center", $cfg->getHeaderTextCenter());
298  $this->assertSame("head text left", $cfg->getHeaderTextLeft());
299  $this->assertSame(1, $cfg->getHeaderType());
300  $this->assertSame("r_c.svg", $cfg->getRadioButtonCheckedSvg());
301  $this->assertSame("r.svg", $cfg->getRadioButtonSvg());
302  $this->assertSame("c_c.svg", $cfg->getCheckboxCheckedSvg());
303  $this->assertSame("c.svg", $cfg->getCheckboxSvg());
304  $this->assertSame(231, $cfg->getJavascriptDelay());
305  $this->assertTrue($cfg->getPrintMediaType());
306  $this->assertSame('5cm', $cfg->getMarginTop());
307  $this->assertSame('6cm', $cfg->getMarginBottom());
308  $this->assertSame(0.4, $cfg->getZoom());
309  $this->assertTrue($cfg->getExternalLinks());
310  $this->assertFalse($cfg->getLowQuality());
311  $this->assertFalse($cfg->getGreyscale());
312  $this->assertSame('Landscape', $cfg->getOrientation());
313  $this->assertSame('A1', $cfg->getPageSize());
314  $this->assertSame('1cm', $cfg->getMarginLeft());
315  $this->assertSame('2cm', $cfg->getMarginRight());
316  $this->assertSame(3, $cfg->getFooterHtmlSpacing());
317  $this->assertSame('<div>my html </div>', $cfg->getFooterHtml());
318  $this->assertSame('my footer text', $cfg->getFooterTextCenter());
319  $this->assertTrue($cfg->isFooterTextLine());
320  $this->assertSame(1, $cfg->getFooterTextSpacing());
321  $this->assertSame('right text', $cfg->getFooterTextRight());
322  $this->assertSame('left text', $cfg->getFooterTextLeft());
323  $this->assertSame(0, $cfg->getFooterType());
324  }
325 }