Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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 the convert helper.
  19   *
  20   * @package    core_backup
  21   * @category   test
  22   * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core_backup;
  27  
  28  use backup;
  29  use convert_helper;
  30  
  31  defined('MOODLE_INTERNAL') || die();
  32  
  33  // Include all the needed stuff
  34  global $CFG;
  35  require_once($CFG->dirroot . '/backup/util/helper/convert_helper.class.php');
  36  
  37  /**
  38   * Test the convert helper.
  39   *
  40   * @package    core_backup
  41   * @category   test
  42   * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  43   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  44   */
  45  class converterhelper_test extends \basic_testcase {
  46  
  47      public function test_choose_conversion_path() {
  48  
  49          // no converters available
  50          $descriptions = array();
  51          $path = testable_convert_helper::choose_conversion_path(backup::FORMAT_MOODLE1, $descriptions);
  52          $this->assertEquals($path, array());
  53  
  54          // missing source and/or targets
  55          $descriptions = array(
  56              // some custom converter
  57              'exporter' => array(
  58                  'from'  => backup::FORMAT_MOODLE1,
  59                  'to'    => 'some_custom_format',
  60                  'cost'  => 10,
  61              ),
  62              // another custom converter
  63              'converter' => array(
  64                  'from'  => 'yet_another_crazy_custom_format',
  65                  'to'    => backup::FORMAT_MOODLE,
  66                  'cost'  => 10,
  67              ),
  68          );
  69          $path = testable_convert_helper::choose_conversion_path(backup::FORMAT_MOODLE1, $descriptions);
  70          $this->assertEquals($path, array());
  71  
  72          $path = testable_convert_helper::choose_conversion_path('some_other_custom_format', $descriptions);
  73          $this->assertEquals($path, array());
  74  
  75          // single step conversion
  76          $path = testable_convert_helper::choose_conversion_path('yet_another_crazy_custom_format', $descriptions);
  77          $this->assertEquals($path, array('converter'));
  78  
  79          // no conversion needed - this is supposed to be detected by the caller
  80          $path = testable_convert_helper::choose_conversion_path(backup::FORMAT_MOODLE, $descriptions);
  81          $this->assertEquals($path, array());
  82  
  83          // two alternatives
  84          $descriptions = array(
  85              // standard moodle 1.9 -> 2.x converter
  86              'moodle1' => array(
  87                  'from'  => backup::FORMAT_MOODLE1,
  88                  'to'    => backup::FORMAT_MOODLE,
  89                  'cost'  => 10,
  90              ),
  91              // alternative moodle 1.9 -> 2.x converter
  92              'alternative' => array(
  93                  'from'  => backup::FORMAT_MOODLE1,
  94                  'to'    => backup::FORMAT_MOODLE,
  95                  'cost'  => 8,
  96              )
  97          );
  98          $path = testable_convert_helper::choose_conversion_path(backup::FORMAT_MOODLE1, $descriptions);
  99          $this->assertEquals($path, array('alternative'));
 100  
 101          // complex case
 102          $descriptions = array(
 103              // standard moodle 1.9 -> 2.x converter
 104              'moodle1' => array(
 105                  'from'  => backup::FORMAT_MOODLE1,
 106                  'to'    => backup::FORMAT_MOODLE,
 107                  'cost'  => 10,
 108              ),
 109              // alternative moodle 1.9 -> 2.x converter
 110              'alternative' => array(
 111                  'from'  => backup::FORMAT_MOODLE1,
 112                  'to'    => backup::FORMAT_MOODLE,
 113                  'cost'  => 8,
 114              ),
 115              // custom converter from 1.9 -> custom 'CFv1' format
 116              'cc1' => array(
 117                  'from'  => backup::FORMAT_MOODLE1,
 118                  'to'    => 'CFv1',
 119                  'cost'  => 2,
 120              ),
 121              // custom converter from custom 'CFv1' format -> moodle 2.0 format
 122              'cc2' => array(
 123                  'from'  => 'CFv1',
 124                  'to'    => backup::FORMAT_MOODLE,
 125                  'cost'  => 5,
 126              ),
 127              // custom converter from CFv1 -> CFv2 format
 128              'cc3' => array(
 129                  'from'  => 'CFv1',
 130                  'to'    => 'CFv2',
 131                  'cost'  => 2,
 132              ),
 133              // custom converter from CFv2 -> moodle 2.0 format
 134              'cc4' => array(
 135                  'from'  => 'CFv2',
 136                  'to'    => backup::FORMAT_MOODLE,
 137                  'cost'  => 2,
 138              ),
 139          );
 140  
 141          // ask the helper to find the most effective way
 142          $path = testable_convert_helper::choose_conversion_path(backup::FORMAT_MOODLE1, $descriptions);
 143          $this->assertEquals($path, array('cc1', 'cc3', 'cc4'));
 144      }
 145  }
 146  
 147  /**
 148   * Provides access to the protected methods we need to test
 149   */
 150  class testable_convert_helper extends convert_helper {
 151  
 152      public static function choose_conversion_path($format, array $descriptions) {
 153          return parent::choose_conversion_path($format, $descriptions);
 154      }
 155  }