1 |
efrain |
1 |
<?php
|
|
|
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 mod_customcert lib.
|
|
|
19 |
*
|
|
|
20 |
* @package mod_customcert
|
|
|
21 |
* @category test
|
|
|
22 |
* @copyright 2023 Diego Felipe Monroy <dfelipe.monroyc@gmail.com>
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
namespace mod_customcert;
|
|
|
27 |
|
|
|
28 |
use advanced_testcase;
|
|
|
29 |
|
|
|
30 |
defined('MOODLE_INTERNAL') || die();
|
|
|
31 |
|
|
|
32 |
global $CFG;
|
|
|
33 |
require_once($CFG->dirroot.'/mod/customcert/lib.php');
|
|
|
34 |
require_once($CFG->libdir.'/componentlib.class.php');
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Unit tests for mod_customcert lib.
|
|
|
38 |
*
|
|
|
39 |
* @package mod_customcert
|
|
|
40 |
* @category test
|
|
|
41 |
* @copyright 2023 Diego Felipe Monroy <dfelipe.monroyc@gmail.com>
|
|
|
42 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
43 |
*/
|
|
|
44 |
class lib_test extends advanced_testcase {
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Test set up.
|
|
|
48 |
*/
|
|
|
49 |
public function setUp(): void {
|
|
|
50 |
$this->resetAfterTest();
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Tests force custom language for current session.
|
|
|
55 |
*
|
|
|
56 |
* @covers ::mod_customcert_force_current_language
|
|
|
57 |
*/
|
|
|
58 |
public function test_mod_customcert_force_current_language() {
|
|
|
59 |
global $USER;
|
|
|
60 |
|
|
|
61 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
62 |
$USER = $user1;
|
|
|
63 |
$testlangs = ['es_mx', 'pt_br', 'cs', 'da', 'nb', 'sv'];
|
|
|
64 |
|
|
|
65 |
$activelangs = get_string_manager()->get_list_of_translations();
|
|
|
66 |
// Testing not installed languages.
|
|
|
67 |
foreach ($testlangs as $lang) {
|
|
|
68 |
$forced = mod_customcert_force_current_language($lang);
|
|
|
69 |
$this->assertFalse($forced);
|
|
|
70 |
$this->assertArrayNotHasKey($lang, $activelangs);
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
// Testing english language.
|
|
|
74 |
$forced = mod_customcert_force_current_language('en');
|
|
|
75 |
$this->assertFalse($forced);
|
|
|
76 |
$this->assertArrayHasKey('en', $activelangs);
|
|
|
77 |
|
|
|
78 |
// Install Language packs.
|
|
|
79 |
$this->install_languagues();
|
|
|
80 |
|
|
|
81 |
$activelangs = get_string_manager()->get_list_of_translations();
|
|
|
82 |
foreach ($testlangs as $lang) {
|
|
|
83 |
$forced = mod_customcert_force_current_language($lang);
|
|
|
84 |
$this->assertTrue($forced);
|
|
|
85 |
$this->assertArrayHasKey($lang, $activelangs);
|
|
|
86 |
}
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Install lang packs by lang codes.
|
|
|
91 |
*
|
|
|
92 |
* @return bool
|
|
|
93 |
*/
|
|
|
94 |
private function install_languagues(): bool {
|
|
|
95 |
\core_php_time_limit::raise();
|
|
|
96 |
|
|
|
97 |
$langcodes = [
|
|
|
98 |
'es' => 'es_mx',
|
|
|
99 |
'br' => 'pt_br',
|
|
|
100 |
'cz' => 'cs',
|
|
|
101 |
'dk' => 'da',
|
|
|
102 |
'no' => 'nb',
|
|
|
103 |
'se' => 'sv',
|
|
|
104 |
];
|
|
|
105 |
get_string_manager()->reset_caches();
|
|
|
106 |
|
|
|
107 |
$controller = new \tool_langimport\controller();
|
|
|
108 |
try {
|
|
|
109 |
$updated = $controller->install_languagepacks($langcodes);
|
|
|
110 |
return true;
|
|
|
111 |
} catch (\moodle_exception $e) {
|
|
|
112 |
$this->assertInstanceOf('moodle_exception', $e);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
return false;
|
|
|
116 |
}
|
|
|
117 |
}
|