| 1 | 
           efrain | 
           1 | 
           <?php
  | 
        
        
            | 
            | 
           2 | 
           // This file is part of Moodle - http://moodle.org/
  | 
        
        
            | 
            | 
           3 | 
           //
  | 
        
        
            | 
            | 
           4 | 
           // Moodle is free software: you can redistribute it and/or modify
  | 
        
        
            | 
            | 
           5 | 
           // it under the terms of the GNU General Public License as published by
  | 
        
        
            | 
            | 
           6 | 
           // the Free Software Foundation, either version 3 of the License, or
  | 
        
        
            | 
            | 
           7 | 
           // (at your option) any later version.
  | 
        
        
            | 
            | 
           8 | 
           //
  | 
        
        
            | 
            | 
           9 | 
           // Moodle is distributed in the hope that it will be useful,
  | 
        
        
            | 
            | 
           10 | 
           // but WITHOUT ANY WARRANTY; without even the implied warranty of
  | 
        
        
            | 
            | 
           11 | 
           // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  | 
        
        
            | 
            | 
           12 | 
           // GNU General Public License for more details.
  | 
        
        
            | 
            | 
           13 | 
           //
  | 
        
        
            | 
            | 
           14 | 
           // You should have received a copy of the GNU General Public License
  | 
        
        
            | 
            | 
           15 | 
           // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  | 
        
        
            | 
            | 
           16 | 
              | 
        
        
            | 
            | 
           17 | 
           namespace core;
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           use TCPDF_STATIC;
  | 
        
        
            | 
            | 
           20 | 
              | 
        
        
            | 
            | 
           21 | 
           /**
  | 
        
        
            | 
            | 
           22 | 
            * Tests for PDFlib
  | 
        
        
            | 
            | 
           23 | 
            *
  | 
        
        
            | 
            | 
           24 | 
            * @package    core
  | 
        
        
            | 
            | 
           25 | 
            * @copyright  2021 Brendan Heywood (brendan@catalyst-au.net)
  | 
        
        
            | 
            | 
           26 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
           | 1441 | 
           ariadna | 
           27 | 
            * @covers \pdf
  | 
        
        
           | 1 | 
           efrain | 
           28 | 
            */
  | 
        
        
           | 1441 | 
           ariadna | 
           29 | 
           final class pdflib_test extends \advanced_testcase {
  | 
        
        
           | 1 | 
           efrain | 
           30 | 
              | 
        
        
           | 11 | 
           efrain | 
           31 | 
               public function test_gettcpdf_producer(): void {
  | 
        
        
           | 1 | 
           efrain | 
           32 | 
                   global $CFG;
  | 
        
        
            | 
            | 
           33 | 
                   require_once($CFG->libdir.'/pdflib.php');
  | 
        
        
            | 
            | 
           34 | 
              | 
        
        
            | 
            | 
           35 | 
                   // This is to reduce the information disclosure in PDF metadata.
  | 
        
        
            | 
            | 
           36 | 
                   // If we upgrade TCPDF keep it just the major version.
  | 
        
        
            | 
            | 
           37 | 
                   $producer = TCPDF_STATIC::getTCPDFProducer();
  | 
        
        
            | 
            | 
           38 | 
                   $this->assertEquals('TCPDF (http://www.tcpdf.org)', $producer);
  | 
        
        
            | 
            | 
           39 | 
               }
  | 
        
        
            | 
            | 
           40 | 
              | 
        
        
           | 11 | 
           efrain | 
           41 | 
               public function test_qrcode(): void {
  | 
        
        
           | 1 | 
           efrain | 
           42 | 
                   global $CFG;
  | 
        
        
            | 
            | 
           43 | 
                   require_once($CFG->libdir.'/pdflib.php');
  | 
        
        
            | 
            | 
           44 | 
              | 
        
        
            | 
            | 
           45 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           46 | 
              | 
        
        
            | 
            | 
           47 | 
                   $pdf = new \pdf();
  | 
        
        
            | 
            | 
           48 | 
                   $pdf->AddPage('P', [500, 500]);
  | 
        
        
            | 
            | 
           49 | 
                   $pdf->SetMargins(10, 0, 10);
  | 
        
        
            | 
            | 
           50 | 
              | 
        
        
            | 
            | 
           51 | 
                   $style = [
  | 
        
        
            | 
            | 
           52 | 
                       'border' => 0,
  | 
        
        
            | 
            | 
           53 | 
                       'vpadding' => 'auto',
  | 
        
        
            | 
            | 
           54 | 
                       'hpadding' => 'auto',
  | 
        
        
            | 
            | 
           55 | 
                       'fgcolor' => [0, 0, 0],
  | 
        
        
            | 
            | 
           56 | 
                       'bgcolor' => [255, 255, 255],
  | 
        
        
            | 
            | 
           57 | 
                       'module_width' => 1,
  | 
        
        
            | 
            | 
           58 | 
                       'module_height' => 1
  | 
        
        
            | 
            | 
           59 | 
                   ];
  | 
        
        
            | 
            | 
           60 | 
              | 
        
        
            | 
            | 
           61 | 
                   $pdf->setCellPaddings(0, 0, 0, 0);
  | 
        
        
            | 
            | 
           62 | 
                   $pdf->write2DBarcode('https://www.example.com/moodle/admin/search.php',
  | 
        
        
            | 
            | 
           63 | 
                       'QRCODE,M', null, null, 35, 35, $style, 'N');
  | 
        
        
            | 
            | 
           64 | 
                   $pdf->SetFillColor(255, 255, 255);
  | 
        
        
            | 
            | 
           65 | 
                   $res = $pdf->Output('output', 'S');
  | 
        
        
            | 
            | 
           66 | 
              | 
        
        
            | 
            | 
           67 | 
                   $this->assertGreaterThan(100000, strlen($res));
  | 
        
        
            | 
            | 
           68 | 
                   $this->assertLessThan(120000, strlen($res));
  | 
        
        
            | 
            | 
           69 | 
               }
  | 
        
        
            | 
            | 
           70 | 
              | 
        
        
            | 
            | 
           71 | 
               /**
  | 
        
        
            | 
            | 
           72 | 
                * Test get_export_fontlist function.
  | 
        
        
            | 
            | 
           73 | 
                *
  | 
        
        
           | 1441 | 
           ariadna | 
           74 | 
                * @covers \pdf::get_export_fontlist
  | 
        
        
           | 1 | 
           efrain | 
           75 | 
                *
  | 
        
        
            | 
            | 
           76 | 
                * @return void
  | 
        
        
            | 
            | 
           77 | 
                */
  | 
        
        
            | 
            | 
           78 | 
               public function test_get_export_fontlist(): void {
  | 
        
        
            | 
            | 
           79 | 
                   global $CFG;
  | 
        
        
            | 
            | 
           80 | 
                   require_once($CFG->libdir.'/pdflib.php');
  | 
        
        
            | 
            | 
           81 | 
              | 
        
        
            | 
            | 
           82 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           83 | 
              | 
        
        
            | 
            | 
           84 | 
                   $pdf = new \pdf();
  | 
        
        
            | 
            | 
           85 | 
                   $fontlist = $pdf->get_export_fontlist();
  | 
        
        
            | 
            | 
           86 | 
                   $this->assertCount(1, $fontlist);
  | 
        
        
            | 
            | 
           87 | 
                   $this->assertArrayHasKey('freesans', $fontlist);
  | 
        
        
            | 
            | 
           88 | 
              | 
        
        
            | 
            | 
           89 | 
                   $CFG->pdfexportfont = [
  | 
        
        
            | 
            | 
           90 | 
                       'kozminproregular' => 'Kozmin Pro Regular',
  | 
        
        
            | 
            | 
           91 | 
                       'stsongstdlight' => 'STSong stdlight',
  | 
        
        
            | 
            | 
           92 | 
                       'invalidfont' => 'Invalid'
  | 
        
        
            | 
            | 
           93 | 
                   ];
  | 
        
        
            | 
            | 
           94 | 
                   $fontlist = $pdf->get_export_fontlist();
  | 
        
        
            | 
            | 
           95 | 
                   $this->assertCount(2, $fontlist);
  | 
        
        
            | 
            | 
           96 | 
                   $this->assertArrayNotHasKey('freesans', $fontlist);
  | 
        
        
            | 
            | 
           97 | 
                   $this->assertArrayHasKey('kozminproregular', $fontlist);
  | 
        
        
            | 
            | 
           98 | 
                   $this->assertArrayHasKey('stsongstdlight', $fontlist);
  | 
        
        
            | 
            | 
           99 | 
                   $this->assertArrayNotHasKey('invalidfont', $fontlist);
  | 
        
        
            | 
            | 
           100 | 
               }
  | 
        
        
            | 
            | 
           101 | 
           }
  |