MDL-22015 AMOS - making room for new string manager
[moodle.git] / lib / simpletest / teststringmanager.php
CommitLineData
8926f844 1<?php
bb45fe62 2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.org //
9// //
10// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
25
26/**
27 * Tests for get_string in ../moodlelib.php.
28 *
29 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
30 * @package moodlecore
31 */
32
33if (!defined('MOODLE_INTERNAL')) {
34 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
35}
36
37require_once($CFG->libdir . '/moodlelib.php');
38
39/**
40 * Test subclass that makes all the protected methods we want to test pubic.
41 */
ef330d95 42class testable_string_manager extends legacy_string_manager {
bb45fe62 43 public function locations_to_search($module) {
44 return parent::locations_to_search($module);
45 }
46 public function parse_module_name($module) {
47 return parent::parse_module_name($module);
48 }
49 public function get_parent_language($lang) {
50 return parent::get_parent_language($lang);
51 }
52 public function load_lang_file($langfile) {
53 return parent::load_lang_file($langfile);
54 }
55 public function get_string_from_file($identifier, $langfile, $a) {
56 return parent::get_string_from_file($identifier, $langfile, $a);
57 }
58}
59
bb45fe62 60class string_manager_test extends UnitTestCase {
081a63a9 61
62 public static $includecoverage = array('lib/moodlelib.php');
bb45fe62 63 protected $originallang;
7e3a668c 64 protected $workspace = 'temp/get_string_fixtures'; // Path inside $CFG->dataroot where we work.
bb45fe62 65 protected $basedir;
66 protected $stringmanager;
7e3a668c 67 protected $fileswritten = array();
bb45fe62 68
69 public function setUp() {
70 global $CFG, $SESSION;
71 if (isset($SESSION->lang)) {
72 $this->originallang = $SESSION->lang;
73 } else {
74 $this->originallang = null;
75 }
7e3a668c 76 $this->basedir = $CFG->dataroot . '/' . $this->workspace . '/';
bb45fe62 77 $this->stringmanager = new testable_string_manager($this->basedir . 'moodle',
64208b5d 78 $this->basedir . 'moodledata', false);
7e3a668c 79 make_upload_directory($this->workspace . '/' . 'moodle');
80 make_upload_directory($this->workspace . '/' . 'moodledata');
bb45fe62 81 }
82
83 public function tearDown() {
84 global $SESSION;
85 if (is_null($this->originallang)) {
86 unset($SESSION->lang);
87 } else {
88 $SESSION->lang = $this->originallang;
89 }
7e3a668c 90 foreach ($this->fileswritten as $path) {
91 unlink($path);
92 }
93 $this->fileswritten = array();
94 }
95
96 /**
97 * Write a teest language file under $this->basedir
98 * @param string $path e.g. 'moodle/lang/en_htf8/test.php' or 'moodledata/lang/fr_utf8/test/php'.
99 * @param array $strings the strings to store in the file.
100 */
101 protected function write_lang_file($path, $strings) {
102 $contents = "<?php\n";
103 foreach ($strings as $key => $string) {
104 $string = str_replace("'", "\'", $string);
105 if (substr($string, -1) == '\\') {
106 $string .= '\\';
107 }
108 $contents .= "\$string['$key'] = '" . $string . "';\n";
109 }
110 $contents .= "?>\n";
111 make_upload_directory($this->workspace . '/' . dirname($path));
112 $path = $this->basedir . $path;
113 file_put_contents($path, $contents);
114 $this->fileswritten[] = $path;
bb45fe62 115 }
116
117 public function test_locations_to_search_moodle() {
118 $this->assertEqual($this->stringmanager->locations_to_search('moodle'), array(
119 $this->basedir . 'moodle/lang/' => '',
120 $this->basedir . 'moodledata/lang/' => '',
121 ));
122 }
123
124 public function test_locations_to_search_langconfig() {
125 $this->assertEqual($this->stringmanager->locations_to_search('langconfig'), array(
126 $this->basedir . 'moodle/lang/' => '',
127 $this->basedir . 'moodledata/lang/' => '',
128 ));
129 }
130
131 public function test_locations_to_search_module() {
132 $this->assertEqual($this->stringmanager->locations_to_search('forum'), array(
133 $this->basedir . 'moodle/lang/' => 'forum/',
134 $this->basedir . 'moodledata/lang/' => 'forum/',
135 $this->basedir . 'moodle/mod/forum/lang/' => 'forum/',
136 ));
137 }
138
139 public function test_locations_to_search_question_type() {
140 $this->assertEqual($this->stringmanager->locations_to_search('qtype_matrix'), array(
141 $this->basedir . 'moodle/lang/' => 'qtype_matrix/',
142 $this->basedir . 'moodledata/lang/' => 'qtype_matrix/',
143 $this->basedir . 'moodle/question/type/matrix/lang/' => 'matrix/',
144 ));
145 }
146
147 public function test_locations_to_search_local() {
148 $this->assertEqual($this->stringmanager->locations_to_search('local'), array(
149 $this->basedir . 'moodle/lang/' => 'local/',
150 $this->basedir . 'moodledata/lang/' => 'local/',
64208b5d 151 $this->basedir . 'moodle/mod/local/lang/' => 'local/',
bb45fe62 152 ));
153 }
154
155 public function test_locations_to_search_report() {
64208b5d 156 global $CFG;
bb45fe62 157 $this->assertEqual($this->stringmanager->locations_to_search('report_super'), array(
158 $this->basedir . 'moodle/lang/' => 'report_super/',
159 $this->basedir . 'moodledata/lang/' => 'report_super/',
64208b5d 160 $this->basedir . 'moodle/' . $CFG->admin . '/report/super/lang/' => 'super/',
bb45fe62 161 ));
162 }
163
164 public function test_parse_module_name_module() {
165 $this->assertEqual($this->stringmanager->parse_module_name('forum'),
166 array('', 'forum'));
167 }
168
169 public function test_parse_module_name_grade_report() {
170 $this->assertEqual($this->stringmanager->parse_module_name('gradereport_magic'),
171 array('gradereport_', 'magic'));
172 }
173
174 public function test_get_parent_language_normal() {
7e3a668c 175 // Setup fixture.
176 $this->write_lang_file('moodledata/lang/fr_ca_utf8/langconfig.php', array(
177 'parentlanguage' => 'fr_utf8',
178 ));
179 // Exercise SUT.
bb45fe62 180 $this->assertEqual($this->stringmanager->get_parent_language('fr_ca_utf8'), 'fr_utf8');
181 }
182
183 public function test_get_parent_language_local_override() {
7e3a668c 184 // Setup fixture.
185 $this->write_lang_file('moodledata/lang/es_ar_utf8/langconfig.php', array(
186 'parentlanguage' => 'es_utf8',
187 ));
188 $this->write_lang_file('moodle/lang/es_ar_utf8_local/langconfig.php', array(
189 'parentlanguage' => 'es_mx_utf8',
190 ));
191 // Exercise SUT.
bb45fe62 192 $this->assertEqual($this->stringmanager->get_parent_language('es_ar_utf8'), 'es_mx_utf8');
193 }
194
195 public function test_load_lang_file() {
7e3a668c 196 // Setup fixture.
197 $this->write_lang_file('moodle/lang/en_utf8/test.php', array(
198 'hello' => 'Hello \'world\'!',
199 'hellox' => 'Hello $a!',
200 'results' => 'Dear $a->firstname $a->lastname,\n\nOn test \"$a->testname\" you scored $a->grade%% which earns you \$100.',
201 ));
202 // Exercise SUT.
bb45fe62 203 $this->assertEqual($this->stringmanager->load_lang_file($this->basedir . 'moodle/lang/en_utf8/test.php'), array(
204 'hello' => "Hello 'world'!",
205 'hellox' => 'Hello $a!',
206 'results' => 'Dear $a->firstname $a->lastname,\n\nOn test \"$a->testname\" you scored $a->grade%% which earns you \$100.',
207 ));
208 }
209
5c2fec8b 210 public function test_get_string_from_file_empty() {
7e3a668c 211 // Setup fixture.
212 $this->write_lang_file('moodle/lang/en_utf8/test.php', array(
213 'emptyen' => '',
214 ));
215 // Exercise SUT.
5c2fec8b 216 $this->assertIdentical($this->stringmanager->get_string_from_file(
217 'emptyen', $this->basedir . 'moodle/lang/en_utf8/test.php', NULL),
218 '');
219 }
220
bb45fe62 221 public function test_get_string_from_file_simple() {
7e3a668c 222 // Setup fixture.
223 $this->write_lang_file('moodle/lang/en_utf8/test.php', array(
224 'hello' => 'Hello \'world\'!',
225 ));
226 // Exercise SUT.
bb45fe62 227 $this->assertEqual($this->stringmanager->get_string_from_file(
228 'hello', $this->basedir . 'moodle/lang/en_utf8/test.php', NULL),
229 "Hello 'world'!");
230 }
231
232 public function test_get_string_from_file_simple_interp_with_special_chars() {
7e3a668c 233 // Setup fixture.
234 $this->write_lang_file('moodle/lang/en_utf8/test.php', array(
235 'hellox' => 'Hello $a!',
236 ));
237 // Exercise SUT.
bb45fe62 238 $this->assertEqual($this->stringmanager->get_string_from_file(
239 'hellox', $this->basedir . 'moodle/lang/en_utf8/test.php', 'Fred. $100 = 100%'),
240 "Hello Fred. $100 = 100%!");
241 }
242
243 public function test_get_string_from_file_complex_interp() {
7e3a668c 244 // Setup fixture.
245 $this->write_lang_file('moodle/lang/en_utf8/test.php', array(
246 'results' => 'Dear $a->firstname $a->lastname,\n\nOn test \"$a->testname\" you scored $a->grade%% which earns you \$100.',
247 ));
248 // Exercise SUT.
bb45fe62 249 $a = new stdClass;
250 $a->firstname = 'Tim';
251 $a->lastname = 'Hunt';
252 $a->testname = 'The song "\'Right\' said Fred"';
253 $a->grade = 75;
254 $this->assertEqual($this->stringmanager->get_string_from_file(
255 'results', $this->basedir . 'moodle/lang/en_utf8/test.php', $a),
256 "Dear Tim Hunt,\n\nOn test \"The song \"'Right' said Fred\"\" you scored 75% which earns you $100.");
257 }
258
259 public function test_default_lang() {
7e3a668c 260 // Setup fixture.
261 $this->write_lang_file('moodle/lang/en_utf8/moodle.php', array(
262 'test' => 'Test',
263 ));
264 $this->write_lang_file('moodle/lang/en_utf8/test.php', array(
265 'hello' => 'Hello \'world\'!',
266 'hellox' => 'Hello $a!',
267 'results' => 'Dear $a->firstname $a->lastname,\n\nOn test \"$a->testname\" you scored $a->grade%% which earns you \$100.',
268 'emptyen' => '',
269 ));
270 $this->write_lang_file('moodle/blocks/mrbs/lang/en_utf8/block_mrbs.php', array(
271 'yes' => 'Yes',
272 ));
bb45fe62 273 global $SESSION;
274 $SESSION->lang = 'en_utf8';
7e3a668c 275 // Exercise SUT.
bb45fe62 276 $this->assertEqual($this->stringmanager->get_string('test'), 'Test');
277 $this->assertEqual($this->stringmanager->get_string('hello', 'test'), "Hello 'world'!");
278 $this->assertEqual($this->stringmanager->get_string('hellox', 'test', 'Tim'), 'Hello Tim!');
279 $this->assertEqual($this->stringmanager->get_string('yes', 'block_mrbs'), 'Yes');
280 $this->assertEqual($this->stringmanager->get_string('stringnotdefinedanywhere'), '[[stringnotdefinedanywhere]]');
5c2fec8b 281 $this->assertEqual($this->stringmanager->get_string('emptyen', 'test'), '');
bb45fe62 282 }
283
284 public function test_non_default_no_parent() {
7e3a668c 285 // Setup fixture.
286 $this->write_lang_file('moodle/lang/en_utf8/moodle.php', array(
287 'test' => 'Test',
288 ));
289 $this->write_lang_file('moodle/lang/fr_utf8/test.php', array(
290 'hello' => 'Bonjour tout le monde!',
291 'hellox' => 'Bonjour $a!',
292 'emptyfr' => '',
293 ));
294 $this->write_lang_file('moodle/blocks/mrbs/lang/fr_utf8/block_mrbs.php', array(
295 'yes' => 'Oui',
296 ));
bb45fe62 297 global $SESSION;
298 $SESSION->lang = 'fr_utf8';
7e3a668c 299 // Exercise SUT.
bb45fe62 300 $this->assertEqual($this->stringmanager->get_string('test'), 'Test');
301 $this->assertEqual($this->stringmanager->get_string('hello', 'test'), 'Bonjour tout le monde!');
302 $this->assertEqual($this->stringmanager->get_string('hellox', 'test', 'Jean-Paul'), 'Bonjour Jean-Paul!');
303 $this->assertEqual($this->stringmanager->get_string('yes', 'block_mrbs'), 'Oui');
304 $this->assertEqual($this->stringmanager->get_string('stringnotdefinedanywhere'), '[[stringnotdefinedanywhere]]');
5c2fec8b 305 $this->assertEqual($this->stringmanager->get_string('emptyfr', 'test'), '');
bb45fe62 306 }
307
308 public function test_lang_with_parent() {
7e3a668c 309 // Setup fixture.
310 $this->write_lang_file('moodledata/lang/fr_ca_utf8/langconfig.php', array(
311 'parentlanguage' => 'fr_utf8',
312 ));
313 $this->write_lang_file('moodle/lang/en_utf8/moodle.php', array(
314 'test' => 'Test',
315 ));
316 $this->write_lang_file('moodle/lang/fr_utf8/test.php', array(
317 'hello' => 'Bonjour tout le monde!',
318 'hellox' => 'Bonjour $a!',
319 'emptyfr' => '',
320 ));
321 $this->write_lang_file('moodle/blocks/mrbs/lang/fr_utf8/block_mrbs.php', array(
322 'yes' => 'Oui',
323 ));
324 $this->write_lang_file('moodledata/lang/fr_ca_utf8/test.php', array(
325 'hello' => 'Bonjour Québec!',
326 ));
bb45fe62 327 global $SESSION;
328 $SESSION->lang = 'fr_ca_utf8';
7e3a668c 329 // Exercise SUT.
bb45fe62 330 $this->assertEqual($this->stringmanager->get_string('test'), 'Test');
331 $this->assertEqual($this->stringmanager->get_string('hello', 'test'), 'Bonjour Québec!');
332 $this->assertEqual($this->stringmanager->get_string('hellox', 'test', 'Jean-Paul'), 'Bonjour Jean-Paul!');
333 $this->assertEqual($this->stringmanager->get_string('yes', 'block_mrbs'), 'Oui');
334 $this->assertEqual($this->stringmanager->get_string('stringnotdefinedanywhere'), '[[stringnotdefinedanywhere]]');
335 }
5c2fec8b 336
7409013e 337 public function test_get_list_of_countries_en_utf8() {
338 global $CFG, $SESSION;
339 // Setup fixture.
340 $countriesen = array(
341 'AU' => 'Australia',
342 'GB' => 'United Kingdom',
343 );
344 $this->write_lang_file('moodle/lang/en_utf8/countries.php', $countriesen);
345
346 $oldlist = $CFG->allcountrycodes;
347 $CFG->allcountrycodes = '';
348 $SESSION->lang = 'en_utf8';
349
350 // Exercise SUT.
351 $this->assertEqual($this->stringmanager->get_list_of_countries(),
352 $countriesen);
353
354 // Tear down.
355 $CFG->allcountrycodes = $oldlist;
356 }
357
358 public function test_get_list_of_countries_specific_list() {
359 global $CFG, $SESSION;
360 // Setup fixture.
361 $countriesen = array(
362 'AU' => 'Australia',
363 'GB' => 'United Kingdom',
364 );
365 $this->write_lang_file('moodle/lang/en_utf8/countries.php', $countriesen);
366
367 $oldlist = $CFG->allcountrycodes;
368 $CFG->allcountrycodes = 'AU';
369 $SESSION->lang = 'en_utf8';
370
371 // Exercise SUT.
372 $this->assertEqual($this->stringmanager->get_list_of_countries(),
373 array('AU' => $this->stringmanager->get_string('AU', 'countries')));
374
375 // Tear down.
376 $CFG->allcountrycodes = $oldlist;
377 }
378
379 public function test_get_list_of_countries_fr_utf8() {
380 global $CFG, $SESSION;
381 // Setup fixture.
382 $countriesen = array(
383 'AU' => 'Australia',
384 'GB' => 'United Kingdom',
385 );
386 $this->write_lang_file('moodle/lang/en_utf8/countries.php', $countriesen);
387
388 $countriesfr = array(
389 'AU' => 'Australie',
390 'FR' => 'France',
391 'GB' => 'Royaume-Uni',
392 );
393 $this->write_lang_file('moodledata/lang/fr_utf8/countries.php', $countriesfr);
394
395 $oldlist = $CFG->allcountrycodes;
396 $CFG->allcountrycodes = '';
397 $SESSION->lang = 'fr_utf8';
398
399 // Exercise SUT.
400 unset($countriesfr['FR']);
401 $this->assertEqual($this->stringmanager->get_list_of_countries(),
402 $countriesfr);
403
404 // Tear down.
405 $CFG->allcountrycodes = $oldlist;
406 }
407
408 public function test_get_list_of_countries_specific_list_fr() {
409 global $CFG, $SESSION;
410 // Setup fixture.
411 $countriesen = array(
412 'AU' => 'Australia',
413 'GB' => 'United Kingdom',
414 );
415 $this->write_lang_file('moodle/lang/en_utf8/countries.php', $countriesen);
416
417 $countriesfr = array(
418 'AU' => 'Australie',
419 'FR' => 'France',
420 'GB' => 'Royaume-Uni',
421 );
422 $this->write_lang_file('moodledata/lang/fr_utf8/countries.php', $countriesfr);
423
424 $oldlist = $CFG->allcountrycodes;
425 $CFG->allcountrycodes = 'FR';
426 $SESSION->lang = 'fr_utf8';
427
428 // Exercise SUT.
429 unset($countriesfr['FR']);
430 $this->assertEqual($this->stringmanager->get_list_of_countries(),
431 array('FR' => 'France'));
432 // Tear down.
433 $CFG->allcountrycodes = $oldlist;
434 }
435
436 public function test_get_list_of_countries_lang_with_parent_local_override() {
437 global $CFG, $SESSION;
438
439 // Setup fixture.
440 $this->write_lang_file('moodledata/lang/fr_ca_utf8/langconfig.php', array(
441 'parentlanguage' => 'fr_utf8',
442 ));
443
444 $countriesen = array(
445 'AU' => 'Australia',
446 'GB' => 'United Kingdom',
447 );
448 $this->write_lang_file('moodle/lang/en_utf8/countries.php', $countriesen);
449
450 $countriesfr = array(
451 'AU' => 'Australie',
452 'GB' => 'Royaume-Uni',
453 );
454 $this->write_lang_file('moodledata/lang/fr_utf8/countries.php', $countriesfr);
455
456 $this->write_lang_file('moodle/lang/fr_ca_utf8_local/countries.php', array(
457 'AU' => 'Aussie',
458 ));
459
460 $oldlist = $CFG->allcountrycodes;
461 $CFG->allcountrycodes = '';
462 $SESSION->lang = 'fr_ca_utf8';
463
464 // Exercise SUT.
465 $this->assertEqual($this->stringmanager->get_list_of_countries(),
466 array('AU' => 'Aussie', 'GB' => 'Royaume-Uni'));
8926f844 467
7409013e 468 // Tear down.
469 $CFG->allcountrycodes = $oldlist;
470 }
bb45fe62 471}
472
8926f844 473