MDL-22015 AMOS - making room for new string manager
[moodle.git] / lib / simpletest / teststringmanager.php
1 <?php
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 ///////////////////////////////////////////////////////////////////////////
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  */
33 if (!defined('MOODLE_INTERNAL')) {
34     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
35 }
37 require_once($CFG->libdir . '/moodlelib.php');
39 /**
40  * Test subclass that makes all the protected methods we want to test pubic.
41  */
42 class testable_string_manager extends legacy_string_manager {
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 }
60 class string_manager_test extends UnitTestCase {
62     public static $includecoverage = array('lib/moodlelib.php');
63     protected $originallang;
64     protected $workspace = 'temp/get_string_fixtures'; // Path inside $CFG->dataroot where we work.
65     protected $basedir;
66     protected $stringmanager;
67     protected $fileswritten = array();
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         }
76         $this->basedir = $CFG->dataroot . '/' . $this->workspace . '/';
77         $this->stringmanager = new testable_string_manager($this->basedir . 'moodle',
78                 $this->basedir . 'moodledata', false);
79         make_upload_directory($this->workspace . '/' . 'moodle');
80         make_upload_directory($this->workspace . '/' . 'moodledata');
81     }
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         }
90         foreach ($this->fileswritten as $path) {
91             unlink($path);
92         }
93         $this->fileswritten = array();
94     }
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;
115     }
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     }
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     }
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     }
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     }
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/',
151             $this->basedir . 'moodle/mod/local/lang/' => 'local/',
152         ));
153     }
155     public function test_locations_to_search_report() {
156         global $CFG;
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/',
160             $this->basedir . 'moodle/' . $CFG->admin . '/report/super/lang/' => 'super/',
161         ));
162     }
164     public function test_parse_module_name_module() {
165         $this->assertEqual($this->stringmanager->parse_module_name('forum'),
166                 array('', 'forum'));
167     }
169     public function test_parse_module_name_grade_report() {
170         $this->assertEqual($this->stringmanager->parse_module_name('gradereport_magic'),
171                 array('gradereport_', 'magic'));
172     }
174     public function test_get_parent_language_normal() {
175         // Setup fixture.
176         $this->write_lang_file('moodledata/lang/fr_ca_utf8/langconfig.php', array(
177             'parentlanguage' => 'fr_utf8',
178         ));
179         // Exercise SUT.
180         $this->assertEqual($this->stringmanager->get_parent_language('fr_ca_utf8'), 'fr_utf8');
181     }
183     public function test_get_parent_language_local_override() {
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.
192         $this->assertEqual($this->stringmanager->get_parent_language('es_ar_utf8'), 'es_mx_utf8');
193     }
195     public function test_load_lang_file() {
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.
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     }
210     public function test_get_string_from_file_empty() {
211         // Setup fixture.
212         $this->write_lang_file('moodle/lang/en_utf8/test.php', array(
213             'emptyen' => '',
214         ));
215         // Exercise SUT.
216         $this->assertIdentical($this->stringmanager->get_string_from_file(
217                 'emptyen', $this->basedir . 'moodle/lang/en_utf8/test.php', NULL),
218                 '');
219     }
221     public function test_get_string_from_file_simple() {
222         // Setup fixture.
223         $this->write_lang_file('moodle/lang/en_utf8/test.php', array(
224             'hello' => 'Hello \'world\'!',
225         ));
226         // Exercise SUT.
227         $this->assertEqual($this->stringmanager->get_string_from_file(
228                 'hello', $this->basedir . 'moodle/lang/en_utf8/test.php', NULL),
229                 "Hello 'world'!");
230     }
232     public function test_get_string_from_file_simple_interp_with_special_chars() {
233         // Setup fixture.
234         $this->write_lang_file('moodle/lang/en_utf8/test.php', array(
235             'hellox' => 'Hello $a!',
236         ));
237         // Exercise SUT.
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     }
243     public function test_get_string_from_file_complex_interp() {
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.
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     }
259     public function test_default_lang() {
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         ));
273         global $SESSION;
274         $SESSION->lang = 'en_utf8';
275         // Exercise SUT.
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]]');
281         $this->assertEqual($this->stringmanager->get_string('emptyen', 'test'), '');
282     }
284     public function test_non_default_no_parent() {
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         ));
297         global $SESSION;
298         $SESSION->lang = 'fr_utf8';
299         // Exercise SUT.
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]]');
305         $this->assertEqual($this->stringmanager->get_string('emptyfr', 'test'), '');
306     }
308     public function test_lang_with_parent() {
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         ));
327         global $SESSION;
328         $SESSION->lang = 'fr_ca_utf8';
329         // Exercise SUT.
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     }
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);
346         $oldlist = $CFG->allcountrycodes;
347         $CFG->allcountrycodes = '';
348         $SESSION->lang = 'en_utf8';
350         // Exercise SUT.
351         $this->assertEqual($this->stringmanager->get_list_of_countries(),
352                 $countriesen);
354         // Tear down.
355         $CFG->allcountrycodes = $oldlist;
356     }
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);
367         $oldlist = $CFG->allcountrycodes;
368         $CFG->allcountrycodes = 'AU';
369         $SESSION->lang = 'en_utf8';
371         // Exercise SUT.
372         $this->assertEqual($this->stringmanager->get_list_of_countries(),
373                 array('AU' => $this->stringmanager->get_string('AU', 'countries')));
375         // Tear down.
376         $CFG->allcountrycodes = $oldlist;
377     }
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);
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);
395         $oldlist = $CFG->allcountrycodes;
396         $CFG->allcountrycodes = '';
397         $SESSION->lang = 'fr_utf8';
399         // Exercise SUT.
400         unset($countriesfr['FR']);
401         $this->assertEqual($this->stringmanager->get_list_of_countries(),
402                 $countriesfr);
404         // Tear down.
405         $CFG->allcountrycodes = $oldlist;
406     }
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);
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);
424         $oldlist = $CFG->allcountrycodes;
425         $CFG->allcountrycodes = 'FR';
426         $SESSION->lang = 'fr_utf8';
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     }
436     public function test_get_list_of_countries_lang_with_parent_local_override() {
437         global $CFG, $SESSION;
439         // Setup fixture.
440         $this->write_lang_file('moodledata/lang/fr_ca_utf8/langconfig.php', array(
441             'parentlanguage' => 'fr_utf8',
442         ));
444         $countriesen = array(
445             'AU' => 'Australia',
446             'GB' => 'United Kingdom',
447         );
448         $this->write_lang_file('moodle/lang/en_utf8/countries.php', $countriesen);
450         $countriesfr = array(
451             'AU' => 'Australie',
452             'GB' => 'Royaume-Uni',
453         );
454         $this->write_lang_file('moodledata/lang/fr_utf8/countries.php', $countriesfr);
456         $this->write_lang_file('moodle/lang/fr_ca_utf8_local/countries.php', array(
457             'AU' => 'Aussie',
458         ));
460         $oldlist = $CFG->allcountrycodes;
461         $CFG->allcountrycodes = '';
462         $SESSION->lang = 'fr_ca_utf8';
464         // Exercise SUT.
465         $this->assertEqual($this->stringmanager->get_list_of_countries(),
466                 array('AU' => 'Aussie', 'GB' => 'Royaume-Uni'));
468         // Tear down.
469         $CFG->allcountrycodes = $oldlist;
470     }