Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 311 and 401]

   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/format/tests/fixtures/format_theunittest.php');
  24  require_once($CFG->dirroot . '/course/format/lib.php');
  25  
  26  /**
  27   * Course format function 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  
  36      /**
  37       * Tests clean_param_if_not_null function
  38       * @covers ::clean_param_if_not_null
  39       */
  40      public function test_clean_param_if_not_null() {
  41          $this->assertNull(clean_param_if_not_null(null));
  42          $n = '3x';
  43          $this->assertEquals(clean_param($n, PARAM_INT), clean_param_if_not_null($n, PARAM_INT));
  44          $this->assertEquals(clean_param($n, PARAM_RAW), clean_param_if_not_null($n, PARAM_RAW));
  45          $this->assertEquals(clean_param($n, PARAM_ALPHANUM), clean_param_if_not_null($n, PARAM_ALPHANUM));
  46          $this->assertEquals(clean_param($n, PARAM_ALPHA), clean_param_if_not_null($n, PARAM_ALPHA));
  47          $s = '<abc>xyz</abc>';
  48          $this->assertEquals(clean_param($s, PARAM_ALPHANUM), clean_param_if_not_null($s, PARAM_ALPHANUM));
  49          $this->assertEquals(clean_param($s, PARAM_RAW), clean_param_if_not_null($s, PARAM_RAW));
  50      }
  51  
  52      /**
  53       * Tests contract_value function
  54       * @covers ::contract_value
  55       */
  56      public function test_contract_value() {
  57          $input = [
  58              'abc' => '<p>All together Now</p>',
  59              'abcformat' => '1',
  60              'jolly' => 'Roger'
  61          ];
  62          $expected = [
  63              'abc_editor' => [ 'text' => $input['abc'], 'format' => $input['abcformat'] ],
  64              'jolly' => $input['jolly'],
  65          ];
  66          $defs = [
  67              'abc_editor' => [],
  68              'jolly' => [ 'type' => PARAM_ALPHA ],
  69          ];
  70          $dest = [];
  71  
  72          foreach ($defs as $name => $def) {
  73              contract_value($dest, $input, $def, $name);
  74          }
  75  
  76          $this->assertEquals($expected, $dest);
  77      }
  78  
  79      /**
  80       * Tests expand_value function
  81       * @covers ::expand_value
  82       */
  83      public function test_expand_value() {
  84          $input = [
  85              'abc_editor' => [ 'text' => '<p>All together Now</p>', 'format' => '1' ],
  86              'jolly' => 'Roger',
  87          ];
  88          $expected = [
  89              'abc' => $input['abc_editor']['text'],
  90              'abcformat' => $input['abc_editor']['format'],
  91              'jolly' => $input['jolly'],
  92          ];
  93          $defs = [
  94              'abc_editor' => [],
  95              'jolly' => [ 'type' => PARAM_ALPHA ],
  96          ];
  97          $dest = [];
  98  
  99          foreach ($defs as $name => $def) {
 100              expand_value($dest, $input, $def, $name);
 101          }
 102  
 103          $this->assertEquals($expected, $dest);
 104      }
 105  }