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 310 and 401] [Versions 39 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_question;
  18  
  19  use question_utils;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  global $CFG;
  24  require_once (__DIR__ . '/../lib.php');
  25  
  26  /**
  27   * Unit tests for the {@link question_utils} class.
  28   *
  29   * @package    core_question
  30   * @category   test
  31   * @copyright  2010 The Open University
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class questionutils_test extends \advanced_testcase {
  35      public function test_arrays_have_same_keys_and_values() {
  36          $this->assertTrue(question_utils::arrays_have_same_keys_and_values(
  37                  array(),
  38                  array()));
  39          $this->assertTrue(question_utils::arrays_have_same_keys_and_values(
  40                  array('key' => 1),
  41                  array('key' => '1')));
  42          $this->assertFalse(question_utils::arrays_have_same_keys_and_values(
  43                  array(),
  44                  array('key' => 1)));
  45          $this->assertFalse(question_utils::arrays_have_same_keys_and_values(
  46                  array('key' => 2),
  47                  array('key' => 1)));
  48          $this->assertFalse(question_utils::arrays_have_same_keys_and_values(
  49                  array('key' => 1),
  50                  array('otherkey' => 1)));
  51          $this->assertFalse(question_utils::arrays_have_same_keys_and_values(
  52                  array('sub0' => '2', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1'),
  53                  array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1')));
  54      }
  55  
  56      public function test_arrays_same_at_key() {
  57          $this->assertTrue(question_utils::arrays_same_at_key(
  58                  array(),
  59                  array(),
  60                  'key'));
  61          $this->assertFalse(question_utils::arrays_same_at_key(
  62                  array(),
  63                  array('key' => 1),
  64                  'key'));
  65          $this->assertFalse(question_utils::arrays_same_at_key(
  66                  array('key' => 1),
  67                  array(),
  68                  'key'));
  69          $this->assertTrue(question_utils::arrays_same_at_key(
  70                  array('key' => 1),
  71                  array('key' => 1),
  72                  'key'));
  73          $this->assertFalse(question_utils::arrays_same_at_key(
  74                  array('key' => 1),
  75                  array('key' => 2),
  76                  'key'));
  77          $this->assertTrue(question_utils::arrays_same_at_key(
  78                  array('key' => 1),
  79                  array('key' => '1'),
  80                  'key'));
  81          $this->assertFalse(question_utils::arrays_same_at_key(
  82                  array('key' => 0),
  83                  array('key' => ''),
  84                  'key'));
  85          $this->assertFalse(question_utils::arrays_same_at_key(
  86                  array(),
  87                  array('key' => ''),
  88                  'key'));
  89      }
  90  
  91      public function test_arrays_same_at_key_missing_is_blank() {
  92          $this->assertTrue(question_utils::arrays_same_at_key_missing_is_blank(
  93                  array(),
  94                  array(),
  95                  'key'));
  96          $this->assertFalse(question_utils::arrays_same_at_key_missing_is_blank(
  97                  array(),
  98                  array('key' => 1),
  99                  'key'));
 100          $this->assertFalse(question_utils::arrays_same_at_key_missing_is_blank(
 101                  array('key' => 1),
 102                  array(),
 103                  'key'));
 104          $this->assertTrue(question_utils::arrays_same_at_key_missing_is_blank(
 105                  array('key' => 1),
 106                  array('key' => 1),
 107                  'key'));
 108          $this->assertFalse(question_utils::arrays_same_at_key_missing_is_blank(
 109                  array('key' => 1),
 110                  array('key' => 2),
 111                  'key'));
 112          $this->assertTrue(question_utils::arrays_same_at_key_missing_is_blank(
 113                  array('key' => 1),
 114                  array('key' => '1'),
 115                  'key'));
 116          $this->assertFalse(question_utils::arrays_same_at_key_missing_is_blank(
 117                  array('key' => '0'),
 118                  array('key' => ''),
 119                  'key'));
 120          $this->assertTrue(question_utils::arrays_same_at_key_missing_is_blank(
 121                  array(),
 122                  array('key' => ''),
 123                  'key'));
 124      }
 125  
 126      public function test_arrays_same_at_key_integer() {
 127          $this->assertTrue(question_utils::arrays_same_at_key_integer(
 128                  array(),
 129                  array(),
 130                  'key'));
 131          $this->assertFalse(question_utils::arrays_same_at_key_integer(
 132                  array(),
 133                  array('key' => 1),
 134                  'key'));
 135          $this->assertFalse(question_utils::arrays_same_at_key_integer(
 136                  array('key' => 1),
 137                  array(),
 138                  'key'));
 139          $this->assertTrue(question_utils::arrays_same_at_key_integer(
 140                  array('key' => 1),
 141                  array('key' => 1),
 142                  'key'));
 143          $this->assertFalse(question_utils::arrays_same_at_key_integer(
 144                  array('key' => 1),
 145                  array('key' => 2),
 146                  'key'));
 147          $this->assertTrue(question_utils::arrays_same_at_key_integer(
 148                  array('key' => 1),
 149                  array('key' => '1'),
 150                  'key'));
 151          $this->assertTrue(question_utils::arrays_same_at_key_integer(
 152                  array('key' => '0'),
 153                  array('key' => ''),
 154                  'key'));
 155          $this->assertTrue(question_utils::arrays_same_at_key_integer(
 156                  array(),
 157                  array('key' => 0),
 158                  'key'));
 159      }
 160  
 161      public function test_int_to_roman() {
 162          $this->assertSame('i', question_utils::int_to_roman(1));
 163          $this->assertSame('iv', question_utils::int_to_roman(4));
 164          $this->assertSame('v', question_utils::int_to_roman(5));
 165          $this->assertSame('vi', question_utils::int_to_roman(6));
 166          $this->assertSame('ix', question_utils::int_to_roman(9));
 167          $this->assertSame('xi', question_utils::int_to_roman(11));
 168          $this->assertSame('xlviii', question_utils::int_to_roman(48));
 169          $this->assertSame('lxxxvii', question_utils::int_to_roman(87));
 170          $this->assertSame('c', question_utils::int_to_roman(100));
 171          $this->assertSame('mccxxxiv', question_utils::int_to_roman(1234));
 172          $this->assertSame('mmmcmxcix', question_utils::int_to_roman(3999));
 173      }
 174  
 175      public function test_int_to_letter() {
 176          $this->assertEquals('A', question_utils::int_to_letter(1));
 177          $this->assertEquals('B', question_utils::int_to_letter(2));
 178          $this->assertEquals('C', question_utils::int_to_letter(3));
 179          $this->assertEquals('D', question_utils::int_to_letter(4));
 180          $this->assertEquals('E', question_utils::int_to_letter(5));
 181          $this->assertEquals('F', question_utils::int_to_letter(6));
 182          $this->assertEquals('G', question_utils::int_to_letter(7));
 183          $this->assertEquals('H', question_utils::int_to_letter(8));
 184          $this->assertEquals('I', question_utils::int_to_letter(9));
 185          $this->assertEquals('J', question_utils::int_to_letter(10));
 186          $this->assertEquals('K', question_utils::int_to_letter(11));
 187          $this->assertEquals('L', question_utils::int_to_letter(12));
 188          $this->assertEquals('M', question_utils::int_to_letter(13));
 189          $this->assertEquals('N', question_utils::int_to_letter(14));
 190          $this->assertEquals('O', question_utils::int_to_letter(15));
 191          $this->assertEquals('P', question_utils::int_to_letter(16));
 192          $this->assertEquals('Q', question_utils::int_to_letter(17));
 193          $this->assertEquals('R', question_utils::int_to_letter(18));
 194          $this->assertEquals('S', question_utils::int_to_letter(19));
 195          $this->assertEquals('T', question_utils::int_to_letter(20));
 196          $this->assertEquals('U', question_utils::int_to_letter(21));
 197          $this->assertEquals('V', question_utils::int_to_letter(22));
 198          $this->assertEquals('W', question_utils::int_to_letter(23));
 199          $this->assertEquals('X', question_utils::int_to_letter(24));
 200          $this->assertEquals('Y', question_utils::int_to_letter(25));
 201          $this->assertEquals('Z', question_utils::int_to_letter(26));
 202      }
 203  
 204      public function test_int_to_roman_too_small() {
 205          $this->expectException(\moodle_exception::class);
 206          question_utils::int_to_roman(0);
 207      }
 208  
 209      public function test_int_to_roman_too_big() {
 210          $this->expectException(\moodle_exception::class);
 211          question_utils::int_to_roman(4000);
 212      }
 213  
 214      public function test_int_to_roman_not_int() {
 215          $this->expectException(\moodle_exception::class);
 216          question_utils::int_to_roman(1.5);
 217      }
 218  
 219      public function test_clean_param_mark() {
 220          $this->assertNull(question_utils::clean_param_mark(null));
 221          $this->assertNull(question_utils::clean_param_mark('frog'));
 222          $this->assertSame('', question_utils::clean_param_mark(''));
 223          $this->assertSame(0.0, question_utils::clean_param_mark('0'));
 224          $this->assertSame(1.5, question_utils::clean_param_mark('1.5'));
 225          $this->assertSame(1.5, question_utils::clean_param_mark('1,5'));
 226          $this->assertSame(-1.5, question_utils::clean_param_mark('-1.5'));
 227          $this->assertSame(-1.5, question_utils::clean_param_mark('-1,5'));
 228      }
 229  }