Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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   * Test unoconv functionality.
  19   *
  20   * @package    core
  21   * @copyright  2016 Damyon Wiese
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * A set of tests for some of the unoconv functionality within Moodle.
  29   *
  30   * @package    core
  31   * @copyright  2016 Damyon Wiese
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class fileconverter_unoconv_converter_testcase extends advanced_testcase {
  35  
  36      /**
  37       * Helper to skip tests which _require_ unoconv.
  38       */
  39      protected function require_unoconv() {
  40          global $CFG;
  41  
  42          if (empty($CFG->pathtounoconv) || !file_is_executable(trim($CFG->pathtounoconv))) {
  43              // No conversions are possible, sorry.
  44              $this->markTestSkipped();
  45          }
  46      }
  47  
  48      /**
  49       * Get a testable mock of the fileconverter_unoconv class.
  50       *
  51       * @param   array   $mockedmethods A list of methods you intend to override
  52       *                  If no methods are specified, only abstract functions are mocked.
  53       * @return  \fileconverter_unoconv\converter
  54       */
  55      protected function get_testable_mock($mockedmethods = null) {
  56          $converter = $this->getMockBuilder(\fileconverter_unoconv\converter::class)
  57              ->setMethods($mockedmethods)
  58              ->getMock();
  59  
  60          return $converter;
  61      }
  62  
  63      /**
  64       * Tests for the start_document_conversion function.
  65       */
  66      public function test_start_document_conversion() {
  67          $this->resetAfterTest();
  68  
  69          $this->require_unoconv();
  70  
  71          // Mock the file to be converted.
  72          $filerecord = [
  73              'contextid' => context_system::instance()->id,
  74              'component' => 'test',
  75              'filearea'  => 'unittest',
  76              'itemid'    => 0,
  77              'filepath'  => '/',
  78              'filename'  => 'test.docx',
  79          ];
  80          $fs = get_file_storage();
  81          $source = __DIR__ . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR . 'unoconv-source.docx';
  82          $testfile = $fs->create_file_from_pathname($filerecord, $source);
  83  
  84          $converter = $this->get_testable_mock();
  85          $conversion = new \core_files\conversion(0, (object) [
  86              'targetformat' => 'pdf',
  87          ]);
  88          $conversion->set_sourcefile($testfile);
  89          $conversion->create();
  90  
  91          // Convert the document.
  92          $converter->start_document_conversion($conversion);
  93          $result = $conversion->get_destfile();
  94          $this->assertNotFalse($result);
  95          $this->assertSame('application/pdf', $result->get_mimetype());
  96          $this->assertGreaterThan(0, $result->get_filesize());
  97      }
  98  
  99      /**
 100       * Tests for the test_unoconv_path function.
 101       *
 102       * @dataProvider provider_test_unoconv_path
 103       * @param   string $path The path to test
 104       * @param   int $status The expected status
 105       */
 106      public function test_test_unoconv_path($path, $status) {
 107          global $CFG;
 108  
 109          $this->resetAfterTest();
 110  
 111          // Set the current path.
 112          $CFG->pathtounoconv = $path;
 113  
 114          // Run the tests.
 115          $result = \fileconverter_unoconv\converter::test_unoconv_path();
 116  
 117          $this->assertEquals($status, $result->status);
 118      }
 119  
 120      /**
 121       * Provider for test_unoconv_path.
 122       *
 123       * @return  array
 124       */
 125      public function provider_test_unoconv_path() {
 126          return [
 127              'Empty path' => [
 128                  'path' => null,
 129                  'status' => \fileconverter_unoconv\converter::UNOCONVPATH_EMPTY,
 130              ],
 131              'Invalid file' => [
 132                  'path' => '/path/to/nonexistent/file',
 133                  'status' => \fileconverter_unoconv\converter::UNOCONVPATH_DOESNOTEXIST,
 134              ],
 135              'Directory' => [
 136                  'path' => __DIR__,
 137                  'status' => \fileconverter_unoconv\converter::UNOCONVPATH_ISDIR,
 138              ],
 139          ];
 140      }
 141  }