Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]
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. 19 * 20 * @package filter_multilang 21 * @category test 22 * @copyright 2019 The Open University 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 29 /** 30 * Tests for filter_multilang. 31 * 32 * @copyright 2019 The Open University 33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 34 */ 35 class filter_multilang_filter_testcase extends advanced_testcase { 36 37 public function setUp(): void { 38 parent::setUp(); 39 40 $this->resetAfterTest(true); 41 42 // Enable glossary filter at top level. 43 filter_set_global_state('multilang', TEXTFILTER_ON); 44 } 45 46 /** 47 * Setup parent language relationship. 48 * 49 * @param string $child the child language, e.g. 'fr_ca'. 50 * @param string $parent the parent language, e.g. 'fr'. 51 */ 52 protected function setup_parent_language(string $child, string $parent) { 53 global $CFG; 54 55 $langfolder = $CFG->dataroot . '/lang/' . $child; 56 check_dir_exists($langfolder); 57 $langconfig = "<?php\n\$string['parentlanguage'] = '$parent';"; 58 file_put_contents($langfolder . '/langconfig.php', $langconfig); 59 } 60 61 /** 62 * Data provider for multi-language filtering tests. 63 */ 64 public function multilang_testcases() { 65 return [ 66 'Basic case EN' => [ 67 'English', 68 '<span lang="en" class="multilang">English</span><span lang="fr" class="multilang">Français</span>', 69 'en', 70 ], 71 'Basic case FR' => [ 72 'Français', 73 '<span lang="en" class="multilang">English</span><span lang="fr" class="multilang">Français</span>', 74 'fr', 75 ], 76 'Reversed input order EN' => [ 77 'English', 78 '<span lang="fr" class="multilang">Français</span><span class="multilang" lang="en">English</span>', 79 'en', 80 ], 81 'Reversed input order EN' => [ 82 'Français', 83 '<span lang="fr" class="multilang">Français</span><span class="multilang" lang="en">English</span>', 84 'fr', 85 ], 86 'Fallback to parent when child not present' => [ 87 'Français', 88 '<span lang="en" class="multilang">English</span><span lang="fr" class="multilang">Français</span>', 89 'fr_ca', ['fr_ca' => 'fr'] 90 ], 91 'Both parent and child language present, using child' => [ 92 'Québécois', 93 '<span lang="fr_ca" class="multilang">Québécois</span> 94 <span lang="fr" class="multilang">Français</span> 95 <span lang="en" class="multilang">English</span>', 96 'fr_ca', ['fr_ca' => 'fr'], 97 ], 98 'Both parent and child language present, using parent' => [ 99 'Français', 100 '<span lang="fr_ca" class="multilang">Québécois</span> 101 <span lang="fr" class="multilang">Français</span> 102 <span lang="en" class="multilang">English</span>', 103 'fr', ['fr_ca' => 'fr'], 104 ], 105 'Both parent and child language present - reverse order, using child' => [ 106 'Québécois', 107 '<span lang="en" class="multilang">English</span> 108 <span lang="fr" class="multilang">Français</span> 109 <span lang="fr_ca" class="multilang">Québécois</span>', 110 'fr_ca', ['fr_ca' => 'fr'], 111 ], 112 'Both parent and child language present - reverse order, using parent' => [ 113 'Français', 114 '<span lang="en" class="multilang">English</span> 115 <span lang="fr" class="multilang">Français</span> 116 <span lang="fr_ca" class="multilang">Québécois</span>', 117 'fr', ['fr_ca' => 'fr'], 118 ], 119 'Fallback to parent when child not present when parent is en' => [ 120 'English', 121 '<span lang="de" class="multilang">Deutsch</span><span lang="en" class="multilang">English</span>', 122 'en_us', 123 ], 124 ]; 125 } 126 127 /** 128 * Tests the filtering of multi-language strings. 129 * 130 * @dataProvider multilang_testcases 131 * 132 * @param string $expectedoutput The expected filter output. 133 * @param string $input the input that is filtererd. 134 * @param string $targetlang the laguage to set as the current languge . 135 * @param array $parentlangs Array child lang => parent lang. E.g. ['es_co' => 'es', 'es_mx' => 'es']. 136 */ 137 public function test_filtering($expectedoutput, $input, $targetlang, $parentlangs = []) { 138 global $SESSION; 139 $SESSION->forcelang = $targetlang; 140 141 foreach ($parentlangs as $child => $parent) { 142 $this->setup_parent_language($child, $parent); 143 } 144 145 $filtered = format_text($input, FORMAT_HTML, array('context' => context_system::instance())); 146 $this->assertEquals($expectedoutput, $filtered); 147 } 148 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body