get_string: Refactoring, performance improvements, bug fixes and unit tests
[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.';
78
79.../moodle/lang/en_utf8_local/moodle.php:
80$string['locallyoverridden'] = 'Should see this';
81
82.../moodledata/lang/fr_ca_utf8/langconfig.php
83$string['parentlanguage'] = 'fr_utf8';
84
85.../moodledata/lang/es_ar_utf8/langconfig.php
86$string['parentlanguage'] = 'es_utf8';
87
88.../moodle/lang/es_ar_utf8_local/langconfig.php
89$string['parentlanguage'] = 'es_mx_utf8';
90
91.../moodledata/lang/fr_utf8/test.php
92$string['hello'] = 'Bonjour tout le monde!';
93$string['hellox'] = 'Bonjour $a!';
94
95.../moodledata/lang/fr_ca_utf8/test.php
96$string['hello'] = 'Bonjour Québec!';
97
98.../moodle/blocks/mrbs/lang/en_utf8/block_mrbs.php:
99$string['yes'] = 'Yes';
100
101.../moodle/blocks/mrbs/lang/fr_utf8/block_mrbs.php:
102$string['yes'] = 'Oui';
103
104*/
105
106class string_manager_test extends UnitTestCase {
107 protected $originallang;
108 protected $basedir;
109 protected $stringmanager;
110
111 public function setUp() {
112 global $CFG, $SESSION;
113 if (isset($SESSION->lang)) {
114 $this->originallang = $SESSION->lang;
115 } else {
116 $this->originallang = null;
117 }
118 $this->basedir = $CFG->libdir . '/simpletest/get_string_fixtures/';
119 $this->stringmanager = new testable_string_manager($this->basedir . 'moodle',
120 $this->basedir . 'moodledata', 'adminpath', false);
121 }
122
123 public function tearDown() {
124 global $SESSION;
125 if (is_null($this->originallang)) {
126 unset($SESSION->lang);
127 } else {
128 $SESSION->lang = $this->originallang;
129 }
130 }
131
132 public function test_locations_to_search_moodle() {
133 $this->assertEqual($this->stringmanager->locations_to_search('moodle'), array(
134 $this->basedir . 'moodle/lang/' => '',
135 $this->basedir . 'moodledata/lang/' => '',
136 ));
137 }
138
139 public function test_locations_to_search_langconfig() {
140 $this->assertEqual($this->stringmanager->locations_to_search('langconfig'), array(
141 $this->basedir . 'moodle/lang/' => '',
142 $this->basedir . 'moodledata/lang/' => '',
143 ));
144 }
145
146 public function test_locations_to_search_module() {
147 $this->assertEqual($this->stringmanager->locations_to_search('forum'), array(
148 $this->basedir . 'moodle/lang/' => 'forum/',
149 $this->basedir . 'moodledata/lang/' => 'forum/',
150 $this->basedir . 'moodle/mod/forum/lang/' => 'forum/',
151 ));
152 }
153
154 public function test_locations_to_search_question_type() {
155 $this->assertEqual($this->stringmanager->locations_to_search('qtype_matrix'), array(
156 $this->basedir . 'moodle/lang/' => 'qtype_matrix/',
157 $this->basedir . 'moodledata/lang/' => 'qtype_matrix/',
158 $this->basedir . 'moodle/question/type/matrix/lang/' => 'matrix/',
159 ));
160 }
161
162 public function test_locations_to_search_local() {
163 $this->assertEqual($this->stringmanager->locations_to_search('local'), array(
164 $this->basedir . 'moodle/lang/' => 'local/',
165 $this->basedir . 'moodledata/lang/' => 'local/',
166 $this->basedir . 'moodle/local/lang/' => 'local/',
167 ));
168 }
169
170 public function test_locations_to_search_report() {
171 $this->assertEqual($this->stringmanager->locations_to_search('report_super'), array(
172 $this->basedir . 'moodle/lang/' => 'report_super/',
173 $this->basedir . 'moodledata/lang/' => 'report_super/',
174 $this->basedir . 'moodle/adminpath/report/super/lang/' => 'super/',
175 $this->basedir . 'moodle/course/report/super/lang/' => 'super/',
176 ));
177 }
178
179 public function test_parse_module_name_module() {
180 $this->assertEqual($this->stringmanager->parse_module_name('forum'),
181 array('', 'forum'));
182 }
183
184 public function test_parse_module_name_grade_report() {
185 $this->assertEqual($this->stringmanager->parse_module_name('gradereport_magic'),
186 array('gradereport_', 'magic'));
187 }
188
189 public function test_get_parent_language_normal() {
190 // This is a standard case with parent language defined in
191 // moodledata/lang/fr_ca_utf8/langconfig.php. From the shared fixture:
192 //
193 //.../moodledata/lang/fr_ca_utf8/langconfig.php
194 //$string['parentlanguage'] = 'fr_utf8';
195 $this->assertEqual($this->stringmanager->get_parent_language('fr_ca_utf8'), 'fr_utf8');
196 }
197
198 public function test_get_parent_language_local_override() {
199 // This is an artificial case where the parent from moodledata/lang/es_ar_utf8 is overridden by
200 // a custom file in moodle/lang/es_ar_utf8_local. From the shared fixture:
201 //
202 //.../moodledata/lang/es_ar_utf8/langconfig.php
203 //$string['parentlanguage'] = 'es_utf8';
204 //
205 //.../moodle/lang/es_ar_utf8_local/langconfig.php
206 //$string['parentlanguage'] = 'es_mx_utf8';
207 $this->assertEqual($this->stringmanager->get_parent_language('es_ar_utf8'), 'es_mx_utf8');
208 }
209
210 public function test_load_lang_file() {
211 // From, the shared fixture:
212 //
213 //.../moodle/lang/en_utf8/test.php:
214 //$string['hello'] = 'Hello \'world\'!';
215 //$string['hellox'] = 'Hello $a!';
216 //$string['results'] = 'Dear $a->firstname $a->lastname,\n\nOn test \"$a->testname\" you scored $a->grade%% which earns you \$100.';
217 $this->assertEqual($this->stringmanager->load_lang_file($this->basedir . 'moodle/lang/en_utf8/test.php'), array(
218 'hello' => "Hello 'world'!",
219 'hellox' => 'Hello $a!',
220 'results' => 'Dear $a->firstname $a->lastname,\n\nOn test \"$a->testname\" you scored $a->grade%% which earns you \$100.',
221 ));
222 }
223
224 public function test_get_string_from_file_simple() {
225 // From the shared fixture:
226 //.../moodle/lang/en_utf8/test.php:
227 //$string['hello'] = 'Hello \'world\'!';
228 // ...
229 $this->assertEqual($this->stringmanager->get_string_from_file(
230 'hello', $this->basedir . 'moodle/lang/en_utf8/test.php', NULL),
231 "Hello 'world'!");
232 }
233
234 public function test_get_string_from_file_simple_interp_with_special_chars() {
235 // From the shared fixture:
236 //.../moodle/lang/en_utf8/test.php:
237 // ...
238 //$string['hellox'] = 'Hello $a!';
239 // ...
240 $this->assertEqual($this->stringmanager->get_string_from_file(
241 'hellox', $this->basedir . 'moodle/lang/en_utf8/test.php', 'Fred. $100 = 100%'),
242 "Hello Fred. $100 = 100%!");
243 }
244
245 public function test_get_string_from_file_complex_interp() {
246 // From the shared fixture:
247 //.../moodle/lang/en_utf8/test.php:
248 // ...
249 //$string['results'] = 'Dear $a->firstname $a->lastname,\n\nOn test \"$a->testname\" you scored $a->grade%% which earns you \$100.';
250 $a = new stdClass;
251 $a->firstname = 'Tim';
252 $a->lastname = 'Hunt';
253 $a->testname = 'The song "\'Right\' said Fred"';
254 $a->grade = 75;
255 $this->assertEqual($this->stringmanager->get_string_from_file(
256 'results', $this->basedir . 'moodle/lang/en_utf8/test.php', $a),
257 "Dear Tim Hunt,\n\nOn test \"The song \"'Right' said Fred\"\" you scored 75% which earns you $100.");
258 }
259
260 public function test_default_lang() {
261 global $SESSION;
262 $SESSION->lang = 'en_utf8';
263 $this->assertEqual($this->stringmanager->get_string('test'), 'Test');
264 $this->assertEqual($this->stringmanager->get_string('hello', 'test'), "Hello 'world'!");
265 $this->assertEqual($this->stringmanager->get_string('hellox', 'test', 'Tim'), 'Hello Tim!');
266 $this->assertEqual($this->stringmanager->get_string('yes', 'block_mrbs'), 'Yes');
267 $this->assertEqual($this->stringmanager->get_string('stringnotdefinedanywhere'), '[[stringnotdefinedanywhere]]');
268 }
269
270 public function test_non_default_no_parent() {
271 global $SESSION;
272 $SESSION->lang = 'fr_utf8';
273 $this->assertEqual($this->stringmanager->get_string('test'), 'Test');
274 $this->assertEqual($this->stringmanager->get_string('hello', 'test'), 'Bonjour tout le monde!');
275 $this->assertEqual($this->stringmanager->get_string('hellox', 'test', 'Jean-Paul'), 'Bonjour Jean-Paul!');
276 $this->assertEqual($this->stringmanager->get_string('yes', 'block_mrbs'), 'Oui');
277 $this->assertEqual($this->stringmanager->get_string('stringnotdefinedanywhere'), '[[stringnotdefinedanywhere]]');
278 }
279
280 public function test_lang_with_parent() {
281 global $SESSION;
282 $SESSION->lang = 'fr_ca_utf8';
283 $this->assertEqual($this->stringmanager->get_string('test'), 'Test');
284 $this->assertEqual($this->stringmanager->get_string('hello', 'test'), 'Bonjour Québec!');
285 $this->assertEqual($this->stringmanager->get_string('hellox', 'test', 'Jean-Paul'), 'Bonjour Jean-Paul!');
286 $this->assertEqual($this->stringmanager->get_string('yes', 'block_mrbs'), 'Oui');
287 $this->assertEqual($this->stringmanager->get_string('stringnotdefinedanywhere'), '[[stringnotdefinedanywhere]]');
288 }
289}
290
291?>