MDL-10221 Simulating the HTTP DELETE method by using a URL param (action), because...
[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 {
64208b5d 43 public function __construct($dirroot, $dataroot, $runninginstaller) {
44 parent::__construct($dirroot, $dataroot, $runninginstaller);
bb45fe62 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
bb45fe62 63class string_manager_test extends UnitTestCase {
081a63a9 64
65 public static $includecoverage = array('lib/moodlelib.php');
bb45fe62 66 protected $originallang;
7e3a668c 67 protected $workspace = 'temp/get_string_fixtures'; // Path inside $CFG->dataroot where we work.
bb45fe62 68 protected $basedir;
69 protected $stringmanager;
7e3a668c 70 protected $fileswritten = array();
bb45fe62 71
72 public function setUp() {
73 global $CFG, $SESSION;
74 if (isset($SESSION->lang)) {
75 $this->originallang = $SESSION->lang;
76 } else {
77 $this->originallang = null;
78 }
7e3a668c 79 $this->basedir = $CFG->dataroot . '/' . $this->workspace . '/';
bb45fe62 80 $this->stringmanager = new testable_string_manager($this->basedir . 'moodle',
64208b5d 81 $this->basedir . 'moodledata', false);
7e3a668c 82 make_upload_directory($this->workspace . '/' . 'moodle');
83 make_upload_directory($this->workspace . '/' . 'moodledata');
bb45fe62 84 }
85
86 public function tearDown() {
87 global $SESSION;
88 if (is_null($this->originallang)) {
89 unset($SESSION->lang);
90 } else {
91 $SESSION->lang = $this->originallang;
92 }
7e3a668c 93 foreach ($this->fileswritten as $path) {
94 unlink($path);
95 }
96 $this->fileswritten = array();
97 }
98
99 /**
100 * Write a teest language file under $this->basedir
101 * @param string $path e.g. 'moodle/lang/en_htf8/test.php' or 'moodledata/lang/fr_utf8/test/php'.
102 * @param array $strings the strings to store in the file.
103 */
104 protected function write_lang_file($path, $strings) {
105 $contents = "<?php\n";
106 foreach ($strings as $key => $string) {
107 $string = str_replace("'", "\'", $string);
108 if (substr($string, -1) == '\\') {
109 $string .= '\\';
110 }
111 $contents .= "\$string['$key'] = '" . $string . "';\n";
112 }
113 $contents .= "?>\n";
114 make_upload_directory($this->workspace . '/' . dirname($path));
115 $path = $this->basedir . $path;
116 file_put_contents($path, $contents);
117 $this->fileswritten[] = $path;
bb45fe62 118 }
119
120 public function test_locations_to_search_moodle() {
121 $this->assertEqual($this->stringmanager->locations_to_search('moodle'), array(
122 $this->basedir . 'moodle/lang/' => '',
123 $this->basedir . 'moodledata/lang/' => '',
124 ));
125 }
126
127 public function test_locations_to_search_langconfig() {
128 $this->assertEqual($this->stringmanager->locations_to_search('langconfig'), array(
129 $this->basedir . 'moodle/lang/' => '',
130 $this->basedir . 'moodledata/lang/' => '',
131 ));
132 }
133
134 public function test_locations_to_search_module() {
135 $this->assertEqual($this->stringmanager->locations_to_search('forum'), array(
136 $this->basedir . 'moodle/lang/' => 'forum/',
137 $this->basedir . 'moodledata/lang/' => 'forum/',
138 $this->basedir . 'moodle/mod/forum/lang/' => 'forum/',
139 ));
140 }
141
142 public function test_locations_to_search_question_type() {
143 $this->assertEqual($this->stringmanager->locations_to_search('qtype_matrix'), array(
144 $this->basedir . 'moodle/lang/' => 'qtype_matrix/',
145 $this->basedir . 'moodledata/lang/' => 'qtype_matrix/',
146 $this->basedir . 'moodle/question/type/matrix/lang/' => 'matrix/',
147 ));
148 }
149
150 public function test_locations_to_search_local() {
151 $this->assertEqual($this->stringmanager->locations_to_search('local'), array(
152 $this->basedir . 'moodle/lang/' => 'local/',
153 $this->basedir . 'moodledata/lang/' => 'local/',
64208b5d 154 $this->basedir . 'moodle/mod/local/lang/' => 'local/',
bb45fe62 155 ));
156 }
157
158 public function test_locations_to_search_report() {
64208b5d 159 global $CFG;
bb45fe62 160 $this->assertEqual($this->stringmanager->locations_to_search('report_super'), array(
161 $this->basedir . 'moodle/lang/' => 'report_super/',
162 $this->basedir . 'moodledata/lang/' => 'report_super/',
64208b5d 163 $this->basedir . 'moodle/' . $CFG->admin . '/report/super/lang/' => 'super/',
bb45fe62 164 ));
165 }
166
167 public function test_parse_module_name_module() {
168 $this->assertEqual($this->stringmanager->parse_module_name('forum'),
169 array('', 'forum'));
170 }
171
172 public function test_parse_module_name_grade_report() {
173 $this->assertEqual($this->stringmanager->parse_module_name('gradereport_magic'),
174 array('gradereport_', 'magic'));
175 }
176
177 public function test_get_parent_language_normal() {
7e3a668c 178 // Setup fixture.
179 $this->write_lang_file('moodledata/lang/fr_ca_utf8/langconfig.php', array(
180 'parentlanguage' => 'fr_utf8',
181 ));
182 // Exercise SUT.
bb45fe62 183 $this->assertEqual($this->stringmanager->get_parent_language('fr_ca_utf8'), 'fr_utf8');
184 }
185
186 public function test_get_parent_language_local_override() {
7e3a668c 187 // Setup fixture.
188 $this->write_lang_file('moodledata/lang/es_ar_utf8/langconfig.php', array(
189 'parentlanguage' => 'es_utf8',
190 ));
191 $this->write_lang_file('moodle/lang/es_ar_utf8_local/langconfig.php', array(
192 'parentlanguage' => 'es_mx_utf8',
193 ));
194 // Exercise SUT.
bb45fe62 195 $this->assertEqual($this->stringmanager->get_parent_language('es_ar_utf8'), 'es_mx_utf8');
196 }
197
198 public function test_load_lang_file() {
7e3a668c 199 // Setup fixture.
200 $this->write_lang_file('moodle/lang/en_utf8/test.php', array(
201 'hello' => 'Hello \'world\'!',
202 'hellox' => 'Hello $a!',
203 'results' => 'Dear $a->firstname $a->lastname,\n\nOn test \"$a->testname\" you scored $a->grade%% which earns you \$100.',
204 ));
205 // Exercise SUT.
bb45fe62 206 $this->assertEqual($this->stringmanager->load_lang_file($this->basedir . 'moodle/lang/en_utf8/test.php'), array(
207 'hello' => "Hello 'world'!",
208 'hellox' => 'Hello $a!',
209 'results' => 'Dear $a->firstname $a->lastname,\n\nOn test \"$a->testname\" you scored $a->grade%% which earns you \$100.',
210 ));
211 }
212
5c2fec8b 213 public function test_get_string_from_file_empty() {
7e3a668c 214 // Setup fixture.
215 $this->write_lang_file('moodle/lang/en_utf8/test.php', array(
216 'emptyen' => '',
217 ));
218 // Exercise SUT.
5c2fec8b 219 $this->assertIdentical($this->stringmanager->get_string_from_file(
220 'emptyen', $this->basedir . 'moodle/lang/en_utf8/test.php', NULL),
221 '');
222 }
223
bb45fe62 224 public function test_get_string_from_file_simple() {
7e3a668c 225 // Setup fixture.
226 $this->write_lang_file('moodle/lang/en_utf8/test.php', array(
227 'hello' => 'Hello \'world\'!',
228 ));
229 // Exercise SUT.
bb45fe62 230 $this->assertEqual($this->stringmanager->get_string_from_file(
231 'hello', $this->basedir . 'moodle/lang/en_utf8/test.php', NULL),
232 "Hello 'world'!");
233 }
234
235 public function test_get_string_from_file_simple_interp_with_special_chars() {
7e3a668c 236 // Setup fixture.
237 $this->write_lang_file('moodle/lang/en_utf8/test.php', array(
238 'hellox' => 'Hello $a!',
239 ));
240 // Exercise SUT.
bb45fe62 241 $this->assertEqual($this->stringmanager->get_string_from_file(
242 'hellox', $this->basedir . 'moodle/lang/en_utf8/test.php', 'Fred. $100 = 100%'),
243 "Hello Fred. $100 = 100%!");
244 }
245
246 public function test_get_string_from_file_complex_interp() {
7e3a668c 247 // Setup fixture.
248 $this->write_lang_file('moodle/lang/en_utf8/test.php', array(
249 'results' => 'Dear $a->firstname $a->lastname,\n\nOn test \"$a->testname\" you scored $a->grade%% which earns you \$100.',
250 ));
251 // Exercise SUT.
bb45fe62 252 $a = new stdClass;
253 $a->firstname = 'Tim';
254 $a->lastname = 'Hunt';
255 $a->testname = 'The song "\'Right\' said Fred"';
256 $a->grade = 75;
257 $this->assertEqual($this->stringmanager->get_string_from_file(
258 'results', $this->basedir . 'moodle/lang/en_utf8/test.php', $a),
259 "Dear Tim Hunt,\n\nOn test \"The song \"'Right' said Fred\"\" you scored 75% which earns you $100.");
260 }
261
262 public function test_default_lang() {
7e3a668c 263 // Setup fixture.
264 $this->write_lang_file('moodle/lang/en_utf8/moodle.php', array(
265 'test' => 'Test',
266 ));
267 $this->write_lang_file('moodle/lang/en_utf8/test.php', array(
268 'hello' => 'Hello \'world\'!',
269 'hellox' => 'Hello $a!',
270 'results' => 'Dear $a->firstname $a->lastname,\n\nOn test \"$a->testname\" you scored $a->grade%% which earns you \$100.',
271 'emptyen' => '',
272 ));
273 $this->write_lang_file('moodle/blocks/mrbs/lang/en_utf8/block_mrbs.php', array(
274 'yes' => 'Yes',
275 ));
bb45fe62 276 global $SESSION;
277 $SESSION->lang = 'en_utf8';
7e3a668c 278 // Exercise SUT.
bb45fe62 279 $this->assertEqual($this->stringmanager->get_string('test'), 'Test');
280 $this->assertEqual($this->stringmanager->get_string('hello', 'test'), "Hello 'world'!");
281 $this->assertEqual($this->stringmanager->get_string('hellox', 'test', 'Tim'), 'Hello Tim!');
282 $this->assertEqual($this->stringmanager->get_string('yes', 'block_mrbs'), 'Yes');
283 $this->assertEqual($this->stringmanager->get_string('stringnotdefinedanywhere'), '[[stringnotdefinedanywhere]]');
5c2fec8b 284 $this->assertEqual($this->stringmanager->get_string('emptyen', 'test'), '');
bb45fe62 285 }
286
287 public function test_non_default_no_parent() {
7e3a668c 288 // Setup fixture.
289 $this->write_lang_file('moodle/lang/en_utf8/moodle.php', array(
290 'test' => 'Test',
291 ));
292 $this->write_lang_file('moodle/lang/fr_utf8/test.php', array(
293 'hello' => 'Bonjour tout le monde!',
294 'hellox' => 'Bonjour $a!',
295 'emptyfr' => '',
296 ));
297 $this->write_lang_file('moodle/blocks/mrbs/lang/fr_utf8/block_mrbs.php', array(
298 'yes' => 'Oui',
299 ));
bb45fe62 300 global $SESSION;
301 $SESSION->lang = 'fr_utf8';
7e3a668c 302 // Exercise SUT.
bb45fe62 303 $this->assertEqual($this->stringmanager->get_string('test'), 'Test');
304 $this->assertEqual($this->stringmanager->get_string('hello', 'test'), 'Bonjour tout le monde!');
305 $this->assertEqual($this->stringmanager->get_string('hellox', 'test', 'Jean-Paul'), 'Bonjour Jean-Paul!');
306 $this->assertEqual($this->stringmanager->get_string('yes', 'block_mrbs'), 'Oui');
307 $this->assertEqual($this->stringmanager->get_string('stringnotdefinedanywhere'), '[[stringnotdefinedanywhere]]');
5c2fec8b 308 $this->assertEqual($this->stringmanager->get_string('emptyfr', 'test'), '');
bb45fe62 309 }
310
311 public function test_lang_with_parent() {
7e3a668c 312 // Setup fixture.
313 $this->write_lang_file('moodledata/lang/fr_ca_utf8/langconfig.php', array(
314 'parentlanguage' => 'fr_utf8',
315 ));
316 $this->write_lang_file('moodle/lang/en_utf8/moodle.php', array(
317 'test' => 'Test',
318 ));
319 $this->write_lang_file('moodle/lang/fr_utf8/test.php', array(
320 'hello' => 'Bonjour tout le monde!',
321 'hellox' => 'Bonjour $a!',
322 'emptyfr' => '',
323 ));
324 $this->write_lang_file('moodle/blocks/mrbs/lang/fr_utf8/block_mrbs.php', array(
325 'yes' => 'Oui',
326 ));
327 $this->write_lang_file('moodledata/lang/fr_ca_utf8/test.php', array(
328 'hello' => 'Bonjour Québec!',
329 ));
bb45fe62 330 global $SESSION;
331 $SESSION->lang = 'fr_ca_utf8';
7e3a668c 332 // Exercise SUT.
bb45fe62 333 $this->assertEqual($this->stringmanager->get_string('test'), 'Test');
334 $this->assertEqual($this->stringmanager->get_string('hello', 'test'), 'Bonjour Québec!');
335 $this->assertEqual($this->stringmanager->get_string('hellox', 'test', 'Jean-Paul'), 'Bonjour Jean-Paul!');
336 $this->assertEqual($this->stringmanager->get_string('yes', 'block_mrbs'), 'Oui');
337 $this->assertEqual($this->stringmanager->get_string('stringnotdefinedanywhere'), '[[stringnotdefinedanywhere]]');
338 }
5c2fec8b 339
bb45fe62 340}
341
342?>