Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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  namespace core\output;
  18  
  19  use ReflectionMethod;
  20  
  21  /**
  22   * Primary navigation renderable test
  23   *
  24   * @package     core
  25   * @category    output
  26   * @copyright   2021 onwards Peter Dias
  27   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   */
  29  class language_menu_test extends \advanced_testcase {
  30      /**
  31       * Basic setup to make sure the nav objects gets generated without any issues.
  32       */
  33      public function setUp(): void {
  34          global $PAGE;
  35          $this->resetAfterTest();
  36          $PAGE->set_url('/');
  37      }
  38      /**
  39       * Test the get_lang_menu
  40       *
  41       * @dataProvider get_lang_menu_provider
  42       * @param bool $withadditionallangs
  43       * @param string $language
  44       * @param array $expected
  45       */
  46      public function test_get_lang_menu(bool $withadditionallangs, string $language, array $expected) {
  47          global $CFG, $PAGE;
  48  
  49          // Mimic multiple langs installed. To trigger responses 'get_list_of_translations'.
  50          // Note: The text/title of the nodes generated will be 'English(fr), English(de)' but we don't care about this.
  51          // We are testing whether the nodes gets generated when the lang menu is available.
  52          if ($withadditionallangs) {
  53              mkdir("$CFG->dataroot/lang/de", 0777, true);
  54              mkdir("$CFG->dataroot/lang/fr", 0777, true);
  55              // Ensure the new langs are picked up and not taken from the cache.
  56              $stringmanager = get_string_manager();
  57              $stringmanager->reset_caches(true);
  58          }
  59  
  60          force_current_language($language);
  61  
  62          $output = new language_menu($PAGE);
  63          $method = new ReflectionMethod('\core\output\language_menu', 'export_for_template');
  64          $method->setAccessible(true);
  65          $renderer = $PAGE->get_renderer('core');
  66  
  67          $response = $method->invoke($output, $renderer);
  68  
  69          if ($withadditionallangs) { // If there are multiple languages installed.
  70              // Assert that the title of the language menu matches the expected one.
  71              $this->assertEquals($expected['title'], $response['title']);
  72              // Assert that the number of language menu items matches the number of the expected items.
  73              $this->assertEquals(count($expected['items']), count($response['items']));
  74              foreach ($expected['items'] as $expecteditem) {
  75                  $lang = $expecteditem['lang'];
  76                  // We need to manually generate the url key and its value in the expected item array as this cannot
  77                  // be done in the data provider due to the change of the state of $PAGE.
  78                  if ($expecteditem['isactive']) {
  79                      $expecteditem['url'] = new \moodle_url('#');
  80                  } else {
  81                      $expecteditem['url'] = new \moodle_url($PAGE->url, ['lang' => $lang]);
  82                      // When the language menu item is not the current language, it will contain the lang attribute.
  83                      $expecteditem['attributes'][] = [
  84                          'key' => 'lang',
  85                          'value' => $lang
  86                      ];
  87                  }
  88                  // The lang value is only used to generate the url, so this key can be removed.
  89                  unset($expecteditem['lang']);
  90  
  91                  // Assert that the given expected item exists in the returned items.
  92                  $this->assertTrue(in_array($expecteditem, $response['items']));
  93              }
  94          } else { // No multiple languages.
  95              $this->assertEquals($expected, $response);
  96          }
  97      }
  98  
  99      /**
 100       * Provider for test_get_lang_menu
 101       *
 102       * @return array
 103       */
 104      public function get_lang_menu_provider(): array {
 105          return [
 106              'Lang menu with only the current language' => [
 107                  false, 'en', []
 108              ],
 109              'Lang menu with only multiple languages installed' => [
 110                  true, 'en', [
 111                      'title' => 'English ‎(en)‎',
 112                      'items' => [
 113                          [
 114                              'title' => 'English ‎(en)‎',
 115                              'text' => 'English ‎(en)‎',
 116                              'link' => true,
 117                              'isactive' => true,
 118                              'lang' => 'en'
 119                          ],
 120                          [
 121                              'title' => 'English ‎(de)‎',
 122                              'text' => 'English ‎(de)‎',
 123                              'link' => true,
 124                              'isactive' => false,
 125                              'lang' => 'de'
 126                          ],
 127  
 128                          [
 129                              'title' => 'English ‎(fr)‎',
 130                              'text' => 'English ‎(fr)‎',
 131                              'link' => true,
 132                              'isactive' => false,
 133                              'lang' => 'fr'
 134                          ],
 135                      ],
 136                  ],
 137              ],
 138              'Lang menu with only multiple languages installed and other than EN set active.' => [
 139                  true, 'de', [
 140                      'title' => 'English ‎(de)‎',
 141                      'items' => [
 142                          [
 143                              'title' => 'English ‎(en)‎',
 144                              'text' => 'English ‎(en)‎',
 145                              'link' => true,
 146                              'isactive' => false,
 147                              'lang' => 'en'
 148                          ],
 149                          [
 150                              'title' => 'English ‎(de)‎',
 151                              'text' => 'English ‎(de)‎',
 152                              'link' => true,
 153                              'isactive' => true,
 154                              'lang' => 'de'
 155                          ],
 156                          [
 157                              'title' => 'English ‎(fr)‎',
 158                              'text' => 'English ‎(fr)‎',
 159                              'link' => true,
 160                              'isactive' => false,
 161                              'lang' => 'fr'
 162                          ],
 163                      ],
 164                  ],
 165              ],
 166          ];
 167      }
 168  }