Updated the HEAD build version to 20090410
[moodle.git] / lib / simpletest / teststringmanager.php
CommitLineData
bb45fe62 1<?php // $Id$
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 */
42class testable_string_manager extends string_manager {
43 public function __construct($dirroot, $dataroot, $admin, $runninginstaller) {
44 parent::__construct($dirroot, $dataroot, $admin, $runninginstaller);
45 }
46 public function locations_to_search($module) {
47 return parent::locations_to_search($module);
48 }
49 public function parse_module_name($module) {
50 return parent::parse_module_name($module);
51 }
52 public function get_parent_language($lang) {
53 return parent::get_parent_language($lang);
54 }
55 public function load_lang_file($langfile) {
56 return parent::load_lang_file($langfile);
57 }
58 public function get_string_from_file($identifier, $langfile, $a) {
59 return parent::get_string_from_file($identifier, $langfile, $a);
60 }
61}
62
63/*
64These tests use a shared fixture comprising language files in
65./get_string_fixtures/moodle, which the test class treats as $CFG->dirroot and
66./get_string_fixtures/moodledata, which the test class treats as $CFG->dataroot.
67
68The files we have, and their contents, are
69
70.../moodle/lang/en_utf8/moodle.php:
71$string['test'] = 'Test';
72$string['locallyoverridden'] = 'Not used';
73
74.../moodle/lang/en_utf8/test.php:
75$string['hello'] = 'Hello \'world\'!';
76$string['hellox'] = 'Hello $a!';
77$string['results'] = 'Dear $a->firstname $a->lastname,\n\nOn test \"$a->testname\" you scored $a->grade%% which earns you \$100.';
5c2fec8b 78$string['emptyen'] = '';
79$string['emptyfr'] = 'This string is defined as \'\' in the fr lang pack';
bb45fe62 80
81.../moodle/lang/en_utf8_local/moodle.php:
82$string['locallyoverridden'] = 'Should see this';
83
84.../moodledata/lang/fr_ca_utf8/langconfig.php
85$string['parentlanguage'] = 'fr_utf8';
86
87.../moodledata/lang/es_ar_utf8/langconfig.php
88$string['parentlanguage'] = 'es_utf8';
89
90.../moodle/lang/es_ar_utf8_local/langconfig.php
91$string['parentlanguage'] = 'es_mx_utf8';
92
93.../moodledata/lang/fr_utf8/test.php
94$string['hello'] = 'Bonjour tout le monde!';
95$string['hellox'] = 'Bonjour $a!';
5c2fec8b 96$string['emptyfr'] = '';
bb45fe62 97
98.../moodledata/lang/fr_ca_utf8/test.php
99$string['hello'] = 'Bonjour Québec!';
100
101.../moodle/blocks/mrbs/lang/en_utf8/block_mrbs.php:
102$string['yes'] = 'Yes';
103
104.../moodle/blocks/mrbs/lang/fr_utf8/block_mrbs.php:
105$string['yes'] = 'Oui';
106
107*/
108
109class string_manager_test extends UnitTestCase {
110 protected $originallang;
111 protected $basedir;
112 protected $stringmanager;
113
114 public function setUp() {
115 global $CFG, $SESSION;
116 if (isset($SESSION->lang)) {
117 $this->originallang = $SESSION->lang;
118 } else {
119 $this->originallang = null;
120 }
121 $this->basedir = $CFG->libdir . '/simpletest/get_string_fixtures/';
122 $this->stringmanager = new testable_string_manager($this->basedir . 'moodle',
123 $this->basedir . 'moodledata', 'adminpath', false);
124 }
125
126 public function tearDown() {
127 global $SESSION;
128 if (is_null($this->originallang)) {
129 unset($SESSION->lang);
130 } else {
131 $SESSION->lang = $this->originallang;
132 }
133 }
134
135 public function test_locations_to_search_moodle() {
136 $this->assertEqual($this->stringmanager->locations_to_search('moodle'), array(
137 $this->basedir . 'moodle/lang/' => '',
138 $this->basedir . 'moodledata/lang/' => '',
139 ));
140 }
141
142 public function test_locations_to_search_langconfig() {
143 $this->assertEqual($this->stringmanager->locations_to_search('langconfig'), array(
144 $this->basedir . 'moodle/lang/' => '',
145 $this->basedir . 'moodledata/lang/' => '',
146 ));
147 }
148
149 public function test_locations_to_search_module() {
150 $this->assertEqual($this->stringmanager->locations_to_search('forum'), array(
151 $this->basedir . 'moodle/lang/' => 'forum/',
152 $this->basedir . 'moodledata/lang/' => 'forum/',
153 $this->basedir . 'moodle/mod/forum/lang/' => 'forum/',
154 ));
155 }
156
157 public function test_locations_to_search_question_type() {
158 $this->assertEqual($this->stringmanager->locations_to_search('qtype_matrix'), array(
159 $this->basedir . 'moodle/lang/' => 'qtype_matrix/',
160 $this->basedir . 'moodledata/lang/' => 'qtype_matrix/',
161 $this->basedir . 'moodle/question/type/matrix/lang/' => 'matrix/',
162 ));
163 }
164
165 public function test_locations_to_search_local() {
166 $this->assertEqual($this->stringmanager->locations_to_search('local'), array(
167 $this->basedir . 'moodle/lang/' => 'local/',
168 $this->basedir . 'moodledata/lang/' => 'local/',
169 $this->basedir . 'moodle/local/lang/' => 'local/',
170 ));
171 }
172
173 public function test_locations_to_search_report() {
174 $this->assertEqual($this->stringmanager->locations_to_search('report_super'), array(
175 $this->basedir . 'moodle/lang/' => 'report_super/',
176 $this->basedir . 'moodledata/lang/' => 'report_super/',
177 $this->basedir . 'moodle/adminpath/report/super/lang/' => 'super/',
178 $this->basedir . 'moodle/course/report/super/lang/' => 'super/',
179 ));
180 }
181
e7b81880 182 public function test_register_plugin_type() {
183 $this->stringmanager->register_plugin_type('mymodreport', 'mod/mymod/report');
184 $this->assertEqual($this->stringmanager->locations_to_search('mymodreport_test'), array(
185 $this->basedir . 'moodle/lang/' => 'mymodreport_test/',
186 $this->basedir . 'moodledata/lang/' => 'mymodreport_test/',
187 $this->basedir . 'moodle/mod/mymod/report/test/lang/' => 'test/',
188 ));
189 }
190
191 public function test_register_plugin_type_session_usage() {
192 $this->stringmanager->register_plugin_type('mymodreport', 'mod/mymod/report');
193
194 // Create a new string_manager to see if it picks up the 'mymodreport'
195 // custom plugin type from session without us having to re-register it.
196 // This is required to make help files work.
197 $newstringmanager = new testable_string_manager($this->basedir . 'moodle',
198 $this->basedir . 'moodledata', 'adminpath', false);
199 $this->assertEqual($newstringmanager->locations_to_search('mymodreport_test'), array(
200 $this->basedir . 'moodle/lang/' => 'mymodreport_test/',
201 $this->basedir . 'moodledata/lang/' => 'mymodreport_test/',
202 $this->basedir . 'moodle/mod/mymod/report/test/lang/' => 'test/',
203 ));
204 }
205
bb45fe62 206 public function test_parse_module_name_module() {
207 $this->assertEqual($this->stringmanager->parse_module_name('forum'),
208 array('', 'forum'));
209 }
210
211 public function test_parse_module_name_grade_report() {
212 $this->assertEqual($this->stringmanager->parse_module_name('gradereport_magic'),
213 array('gradereport_', 'magic'));
214 }
215
216 public function test_get_parent_language_normal() {
217 // This is a standard case with parent language defined in
218 // moodledata/lang/fr_ca_utf8/langconfig.php. From the shared fixture:
219 //
220 //.../moodledata/lang/fr_ca_utf8/langconfig.php
221 //$string['parentlanguage'] = 'fr_utf8';
222 $this->assertEqual($this->stringmanager->get_parent_language('fr_ca_utf8'), 'fr_utf8');
223 }
224
225 public function test_get_parent_language_local_override() {
226 // This is an artificial case where the parent from moodledata/lang/es_ar_utf8 is overridden by
227 // a custom file in moodle/lang/es_ar_utf8_local. From the shared fixture:
228 //
229 //.../moodledata/lang/es_ar_utf8/langconfig.php
230 //$string['parentlanguage'] = 'es_utf8';
231 //
232 //.../moodle/lang/es_ar_utf8_local/langconfig.php
233 //$string['parentlanguage'] = 'es_mx_utf8';
234 $this->assertEqual($this->stringmanager->get_parent_language('es_ar_utf8'), 'es_mx_utf8');
235 }
236
237 public function test_load_lang_file() {
238 // From, the shared fixture:
239 //
240 //.../moodle/lang/en_utf8/test.php:
241 //$string['hello'] = 'Hello \'world\'!';
242 //$string['hellox'] = 'Hello $a!';
243 //$string['results'] = 'Dear $a->firstname $a->lastname,\n\nOn test \"$a->testname\" you scored $a->grade%% which earns you \$100.';
244 $this->assertEqual($this->stringmanager->load_lang_file($this->basedir . 'moodle/lang/en_utf8/test.php'), array(
245 'hello' => "Hello 'world'!",
246 'hellox' => 'Hello $a!',
247 'results' => 'Dear $a->firstname $a->lastname,\n\nOn test \"$a->testname\" you scored $a->grade%% which earns you \$100.',
5c2fec8b 248 'emptyen' => '',
249 'emptyfr' => 'This string is defined as \'\' in the fr lang pack'
bb45fe62 250 ));
251 }
252
5c2fec8b 253 public function test_get_string_from_file_empty() {
254 // From the shared fixture:
255 //.../moodle/lang/en_utf8/test.php:
256 //$string['emptyen'] = '';
257 // ...
258 $this->assertIdentical($this->stringmanager->get_string_from_file(
259 'emptyen', $this->basedir . 'moodle/lang/en_utf8/test.php', NULL),
260 '');
261 }
262
bb45fe62 263 public function test_get_string_from_file_simple() {
264 // From the shared fixture:
265 //.../moodle/lang/en_utf8/test.php:
266 //$string['hello'] = 'Hello \'world\'!';
267 // ...
268 $this->assertEqual($this->stringmanager->get_string_from_file(
269 'hello', $this->basedir . 'moodle/lang/en_utf8/test.php', NULL),
270 "Hello 'world'!");
271 }
272
273 public function test_get_string_from_file_simple_interp_with_special_chars() {
274 // From the shared fixture:
275 //.../moodle/lang/en_utf8/test.php:
276 // ...
277 //$string['hellox'] = 'Hello $a!';
278 // ...
279 $this->assertEqual($this->stringmanager->get_string_from_file(
280 'hellox', $this->basedir . 'moodle/lang/en_utf8/test.php', 'Fred. $100 = 100%'),
281 "Hello Fred. $100 = 100%!");
282 }
283
284 public function test_get_string_from_file_complex_interp() {
285 // From the shared fixture:
286 //.../moodle/lang/en_utf8/test.php:
287 // ...
288 //$string['results'] = 'Dear $a->firstname $a->lastname,\n\nOn test \"$a->testname\" you scored $a->grade%% which earns you \$100.';
289 $a = new stdClass;
290 $a->firstname = 'Tim';
291 $a->lastname = 'Hunt';
292 $a->testname = 'The song "\'Right\' said Fred"';
293 $a->grade = 75;
294 $this->assertEqual($this->stringmanager->get_string_from_file(
295 'results', $this->basedir . 'moodle/lang/en_utf8/test.php', $a),
296 "Dear Tim Hunt,\n\nOn test \"The song \"'Right' said Fred\"\" you scored 75% which earns you $100.");
297 }
298
299 public function test_default_lang() {
300 global $SESSION;
301 $SESSION->lang = 'en_utf8';
302 $this->assertEqual($this->stringmanager->get_string('test'), 'Test');
303 $this->assertEqual($this->stringmanager->get_string('hello', 'test'), "Hello 'world'!");
304 $this->assertEqual($this->stringmanager->get_string('hellox', 'test', 'Tim'), 'Hello Tim!');
305 $this->assertEqual($this->stringmanager->get_string('yes', 'block_mrbs'), 'Yes');
306 $this->assertEqual($this->stringmanager->get_string('stringnotdefinedanywhere'), '[[stringnotdefinedanywhere]]');
5c2fec8b 307 $this->assertEqual($this->stringmanager->get_string('emptyen', 'test'), '');
bb45fe62 308 }
309
310 public function test_non_default_no_parent() {
311 global $SESSION;
312 $SESSION->lang = 'fr_utf8';
313 $this->assertEqual($this->stringmanager->get_string('test'), 'Test');
314 $this->assertEqual($this->stringmanager->get_string('hello', 'test'), 'Bonjour tout le monde!');
315 $this->assertEqual($this->stringmanager->get_string('hellox', 'test', 'Jean-Paul'), 'Bonjour Jean-Paul!');
316 $this->assertEqual($this->stringmanager->get_string('yes', 'block_mrbs'), 'Oui');
317 $this->assertEqual($this->stringmanager->get_string('stringnotdefinedanywhere'), '[[stringnotdefinedanywhere]]');
5c2fec8b 318 $this->assertEqual($this->stringmanager->get_string('emptyfr', 'test'), '');
bb45fe62 319 }
320
321 public function test_lang_with_parent() {
322 global $SESSION;
323 $SESSION->lang = 'fr_ca_utf8';
324 $this->assertEqual($this->stringmanager->get_string('test'), 'Test');
325 $this->assertEqual($this->stringmanager->get_string('hello', 'test'), 'Bonjour Québec!');
326 $this->assertEqual($this->stringmanager->get_string('hellox', 'test', 'Jean-Paul'), 'Bonjour Jean-Paul!');
327 $this->assertEqual($this->stringmanager->get_string('yes', 'block_mrbs'), 'Oui');
328 $this->assertEqual($this->stringmanager->get_string('stringnotdefinedanywhere'), '[[stringnotdefinedanywhere]]');
329 }
5c2fec8b 330
bb45fe62 331}
332
333?>