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   * @package    core_backup
  19   * @category   phpunit
  20   * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  21   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  
  24  defined('MOODLE_INTERNAL') || die();
  25  
  26  // Include all the needed stuff
  27  global $CFG;
  28  require_once($CFG->dirroot . '/backup/util/helper/convert_helper.class.php');
  29  
  30  
  31  /**
  32   * Provides access to the protected methods we need to test
  33   */
  34  class testable_convert_helper extends convert_helper {
  35  
  36      public static function choose_conversion_path($format, array $descriptions) {
  37          return parent::choose_conversion_path($format, $descriptions);
  38      }
  39  }
  40  
  41  
  42  /**
  43   * Defines the test methods
  44   */
  45  class backup_convert_helper_testcase 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  }