Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

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