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 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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  namespace core_course;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->dirroot . '/course/lib.php');
  23  require_once($CFG->dirroot . '/course/tests/fixtures/format_theunittest.php');
  24  require_once($CFG->dirroot . '/course/format/lib.php');
  25  
  26  /**
  27   * Course related unit tests
  28   *
  29   * @package    core_course
  30   * @copyright  2021 Catalyst IT Pty Ltd
  31   * @author     Jason den Dulk
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class course_format_function_test extends \basic_testcase {
  35      public function test_clean_param_if_not_null() {
  36          $this->assertNull(clean_param_if_not_null(null));
  37          $n = '3x';
  38          $this->assertEquals(clean_param($n, PARAM_INT), clean_param_if_not_null($n, PARAM_INT));
  39          $this->assertEquals(clean_param($n, PARAM_RAW), clean_param_if_not_null($n, PARAM_RAW));
  40          $this->assertEquals(clean_param($n, PARAM_ALPHANUM), clean_param_if_not_null($n, PARAM_ALPHANUM));
  41          $this->assertEquals(clean_param($n, PARAM_ALPHA), clean_param_if_not_null($n, PARAM_ALPHA));
  42          $s = '<abc>xyz</abc>';
  43          $this->assertEquals(clean_param($s, PARAM_ALPHANUM), clean_param_if_not_null($s, PARAM_ALPHANUM));
  44          $this->assertEquals(clean_param($s, PARAM_RAW), clean_param_if_not_null($s, PARAM_RAW));
  45      }
  46  
  47      public function test_contract_value() {
  48          $input = [
  49              'abc' => '<p>All together Now</p>',
  50              'abcformat' => '1',
  51              'jolly' => 'Roger'
  52          ];
  53          $expected = [
  54              'abc_editor' => [ 'text' => $input['abc'], 'format' => $input['abcformat'] ],
  55              'jolly' => $input['jolly'],
  56          ];
  57          $defs = [
  58              'abc_editor' => [],
  59              'jolly' => [ 'type' => PARAM_ALPHA ],
  60          ];
  61          $dest = [];
  62  
  63          foreach ($defs as $name => $def) {
  64              contract_value($dest, $input, $def, $name);
  65          }
  66  
  67          $this->assertEquals($expected, $dest);
  68      }
  69  
  70      public function test_expand_value() {
  71          $input = [
  72              'abc_editor' => [ 'text' => '<p>All together Now</p>', 'format' => '1' ],
  73              'jolly' => 'Roger',
  74          ];
  75          $expected = [
  76              'abc' => $input['abc_editor']['text'],
  77              'abcformat' => $input['abc_editor']['format'],
  78              'jolly' => $input['jolly'],
  79          ];
  80          $defs = [
  81              'abc_editor' => [],
  82              'jolly' => [ 'type' => PARAM_ALPHA ],
  83          ];
  84          $dest = [];
  85  
  86          foreach ($defs as $name => $def) {
  87              expand_value($dest, $input, $def, $name);
  88          }
  89  
  90          $this->assertEquals($expected, $dest);
  91      }
  92  }