Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 39 and 400]

   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  /**
  18   * Tests for the dataformat_pdf writer
  19   *
  20   * @package    dataformat_pdf
  21   * @copyright  2020 Paul Holden <paulh@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace dataformat_pdf;
  26  
  27  use core\dataformat;
  28  use context_system;
  29  use html_writer;
  30  use moodle_url;
  31  
  32  /**
  33   * Writer tests
  34   *
  35   * @package    dataformat_pdf
  36   * @copyright  2020 Paul Holden <paulh@moodle.com>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class writer_test extends \advanced_testcase {
  40  
  41      /**
  42       * Test writing data whose content contains an image with pluginfile.php source
  43       */
  44      public function test_write_data_with_pluginfile_image(): void {
  45          global $CFG;
  46  
  47          $this->resetAfterTest(true);
  48  
  49          $imagefixture = "{$CFG->dirroot}/lib/filestorage/tests/fixtures/testimage.jpg";
  50          $image = get_file_storage()->create_file_from_pathname([
  51              'contextid' => context_system::instance()->id,
  52              'component' => 'dataformat_pdf',
  53              'filearea'  => 'test',
  54              'itemid'    => 0,
  55              'filepath'  => '/',
  56              'filename'  => basename($imagefixture),
  57  
  58          ], $imagefixture);
  59  
  60          $imageurl = moodle_url::make_pluginfile_url($image->get_contextid(), $image->get_component(), $image->get_filearea(),
  61              $image->get_itemid(), $image->get_filepath(), $image->get_filename());
  62  
  63          // Insert out test image into the data so it is exported.
  64          $columns = ['animal', 'image'];
  65          $row = ['cat', html_writer::img($imageurl->out(), 'My image')];
  66  
  67          // Export to file. Assert that the exported file exists.
  68          $exportfile = dataformat::write_data('My export', 'pdf', $columns, [$row]);
  69          $this->assertFileExists($exportfile);
  70  
  71          // The exported file should be a reasonable size (~275kb).
  72          $this->assertGreaterThan(270000, filesize($exportfile));
  73      }
  74  }