MDL-46585 lang: display debugging message when deprecated string is used
[moodle.git] / lib / tests / string_manager_standard_test.php
CommitLineData
b7acd9be 1<?php
b7acd9be
DM
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Unit tests for localization support in lib/moodlelib.php
19 *
20 * @package core
ad6df1bf 21 * @category phpunit
b7acd9be
DM
22 * @copyright 2013 David Mudrak <david@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->libdir.'/moodlelib.php');
30
31/**
ad6df1bf 32 * Tests for the API of the string_manager.
b7acd9be
DM
33 *
34 * @copyright 2013 David Mudrak <david@moodle.com>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
99a9d8d9 37class core_string_manager_standard_testcase extends advanced_testcase {
b7acd9be
DM
38
39 public function test_string_manager_instance() {
b7acd9be
DM
40 $this->resetAfterTest();
41
42 $otherroot = dirname(__FILE__).'/fixtures/langtest';
43 $stringman = testable_core_string_manager::instance($otherroot);
99a9d8d9 44 $this->assertInstanceOf('core_string_manager', $stringman);
b7acd9be
DM
45 }
46
47 public function test_get_language_dependencies() {
b7acd9be
DM
48 $this->resetAfterTest();
49
50 $otherroot = dirname(__FILE__).'/fixtures/langtest';
51 $stringman = testable_core_string_manager::instance($otherroot);
52
53 // There is no parent language for 'en'.
54 $this->assertSame(array(), $stringman->get_language_dependencies('en'));
55 // Language with no parent language declared.
56 $this->assertSame(array('aa'), $stringman->get_language_dependencies('aa'));
57 // Language with parent language explicitly set to English (en < de).
58 $this->assertSame(array('de'), $stringman->get_language_dependencies('de'));
59 // Language dependency hierarchy (de < de_du < de_kids).
60 $this->assertSame(array('de', 'de_du', 'de_kids'), $stringman->get_language_dependencies('de_kids'));
61 // Language with the parent language misconfigured to itself (sd < sd).
62 $this->assertSame(array('sd'), $stringman->get_language_dependencies('sd'));
63 // Language with circular dependency (cda < cdb < cdc < cda).
64 $this->assertSame(array('cda', 'cdb', 'cdc'), $stringman->get_language_dependencies('cdc'));
65 // Orphaned language (N/A < bb).
66 $this->assertSame(array('bb'), $stringman->get_language_dependencies('bb'));
67 // Descendant of an orphaned language (N/A < bb < bc).
68 $this->assertSame(array('bb', 'bc'), $stringman->get_language_dependencies('bc'));
69 }
4b867a06
MG
70
71 public function test_deprecated_strings() {
72 $stringman = get_string_manager();
73
74 // Check non-deprecated string.
75 $this->assertFalse($stringman->string_deprecated('hidden', 'grades'));
76
77 // Check deprecated string.
78 $this->assertTrue($stringman->string_deprecated('hidden', 'repository'));
79 $this->assertTrue($stringman->string_exists('hidden', 'repository'));
80 $this->assertDebuggingNotCalled();
81 $this->assertEquals('Hidden', get_string('hidden', 'repository'));
82 $this->assertDebuggingCalled('String [hidden,core_repository] is deprecated. '.
83 'Either you should no longer be using that string, or the string has been incorrectly deprecated, in which case you should report this as a bug. '.
84 'Please refer to https://docs.moodle.org/dev/String_deprecation');
85 }
86
87 /**
88 * This test is a built-in validation of deprecated.txt files in lang locations.
89 *
90 * It will fail if the string in the wrong format or non-existing (mistyped) string was deprecated.
91 */
92 public function test_validate_deprecated_strings_files() {
93 global $CFG;
94 $stringman = get_string_manager();
95 $teststringman = testable_core_string_manager::instance($CFG->langotherroot, $CFG->langlocalroot, array());
96 $allstrings = $teststringman->get_all_deprecated_strings();
97
98 foreach ($allstrings as $string) {
99 if (!preg_match('/^(.*),(.*)$/', $string, $matches) ||
100 clean_param($matches[2], PARAM_COMPONENT) !== $matches[2]) {
101 $this->fail('String "'.$string.'" appearing in one of the lang/en/deprecated.txt files does not have correct syntax');
102 }
103 list($pluginttype, $pluginname) = core_component::normalize_component($matches[2]);
104 if ($matches[2] !== $pluginttype . '_' . $pluginname) {
105 $this->fail('String "'.$string.'" appearing in one of the lang/en/deprecated.txt files does not have normalised component name');
106 }
107 if (!$stringman->string_exists($matches[1], $matches[2])) {
108 $this->fail('String "'.$string.'" appearing in one of the lang/en/deprecated.txt files does not exist');
109 }
110 }
111 }
b7acd9be
DM
112}
113
114
115/**
116 * Helper class providing testable string_manager
117 *
118 * @copyright 2013 David Mudrak <david@moodle.com>
119 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
120 */
99a9d8d9 121class testable_core_string_manager extends core_string_manager_standard {
b7acd9be
DM
122
123 /**
124 * Factory method
125 *
126 * @param string $otherroot full path to the location of installed upstream language packs
127 * @param string $localroot full path to the location of locally customized language packs, defaults to $otherroot
128 * @param bool $usecache use application permanent cache
129 * @param array $translist explicit list of visible translations
130 * @param string $menucache the location of a file that caches the list of available translations
131 * @return testable_core_string_manager
132 */
133 public static function instance($otherroot, $localroot = null, $usecache = false, array $translist = array(), $menucache = null) {
134 global $CFG;
135
136 if (is_null($localroot)) {
137 $localroot = $otherroot;
138 }
139
140 if (is_null($menucache)) {
141 $menucache = $CFG->cachedir.'/languages';
142 }
143
144 return new testable_core_string_manager($otherroot, $localroot, $usecache, $translist, $menucache);
145 }
4b867a06
MG
146
147 public function get_all_deprecated_strings() {
148 return array_flip($this->load_deprecated_strings());
149 }
b7acd9be 150}