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