Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [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 for lib/classes/output/mustache_template_finder.php
  19   *
  20   * @package   core
  21   * @category  phpunit
  22   * @copyright 2015 Damyon Wiese
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use core\output\mustache_template_finder;
  29  
  30  /**
  31   * Unit tests for the Mustache template finder class (contains logic about
  32   * resolving mustache template locations.
  33   */
  34  class core_output_mustache_template_finder_testcase extends advanced_testcase {
  35  
  36      /**
  37       * Data provider which reutrns a set of valid template directories to be used when testing
  38       * get_template_directories_for_component.
  39       *
  40       * @return array
  41       */
  42      public function valid_template_directories_provider(): array {
  43          return [
  44              'plugin: mod_assign' => [
  45                  'component' => 'mod_assign',
  46                  'theme' => '',
  47                  'paths' => [
  48                      'theme/boost/templates/mod_assign/',
  49                      'mod/assign/templates/'
  50                  ],
  51              ],
  52              'plugin: mod_assign with classic' => [
  53                  'component' => 'mod_assign',
  54                  'theme' => 'classic',
  55                  'paths' => [
  56                      'theme/classic/templates/mod_assign/',
  57                      'theme/boost/templates/mod_assign/',
  58                      'mod/assign/templates/'
  59                  ],
  60              ],
  61              'subsystem: core_user' => [
  62                  'component' => 'core_user',
  63                  'theme' => 'classic',
  64                  'paths' => [
  65                      'theme/classic/templates/core_user/',
  66                      'theme/boost/templates/core_user/',
  67                      'user/templates/'
  68                  ],
  69              ],
  70              'core' => [
  71                  'component' => 'core',
  72                  'theme' => 'classic',
  73                  'paths' => [
  74                      'theme/classic/templates/core/',
  75                      'theme/boost/templates/core/',
  76                      'lib/templates/'
  77                  ],
  78              ],
  79          ];
  80      }
  81  
  82      /**
  83       * Tests for get_template_directories_for_component.
  84       *
  85       * @dataProvider valid_template_directories_provider
  86       * @param   string $component
  87       * @param   string $theme
  88       * @param   array $paths
  89       */
  90      public function test_get_template_directories_for_component(string $component, string $theme, array $paths): void {
  91          global $CFG;
  92  
  93          // Test a plugin.
  94          $dirs = mustache_template_finder::get_template_directories_for_component($component, $theme, $paths);
  95  
  96          $correct = array_map(function($path) use ($CFG) {
  97              return implode('/', [$CFG->dirroot, $path]);
  98          }, $paths);
  99  
 100          $this->assertEquals($correct, $dirs);
 101      }
 102  
 103      /**
 104       * Tests for get_template_directories_for_component when dealing with an invalid component.
 105       */
 106      public function test_invalid_component_get_template_directories_for_component() {
 107          // Test something invalid.
 108          $this->expectException(coding_exception::class);
 109          mustache_template_finder::get_template_directories_for_component('octopus', 'classic');
 110      }
 111  
 112      /**
 113       * Data provider which reutrns a set of valid template directories to be used when testing
 114       * get_template_directories_for_component.
 115       *
 116       * @return array
 117       */
 118      public function valid_template_filepath_provider(): array {
 119          return [
 120              'Standard core template' => [
 121                  'template' => 'core/modal',
 122                  'theme' => '',
 123                  'location' => 'lib/templates/modal.mustache',
 124              ],
 125              'Template overridden by theme' => [
 126                  'template' => 'core_form/element-float-inline',
 127                  'theme' => '',
 128                  'location' => 'theme/boost/templates/core_form/element-float-inline.mustache',
 129              ],
 130              'Template overridden by theme but child theme selected' => [
 131                  'template' => 'core_form/element-float-inline',
 132                  'theme' => 'classic',
 133                  'location' => 'theme/boost/templates/core_form/element-float-inline.mustache',
 134              ],
 135              'Template overridden by child theme' => [
 136                  'template' => 'core/full_header',
 137                  'theme' => 'classic',
 138                  'location' => 'theme/classic/templates/core/full_header.mustache',
 139              ],
 140              'Template overridden by child theme but tested against defualt theme' => [
 141                  'template' => 'core/full_header',
 142                  'theme' => '',
 143                  'location' => 'lib/templates/full_header.mustache',
 144              ],
 145              'Standard plugin template' => [
 146                  'template' => 'mod_assign/grading_panel',
 147                  'theme' => '',
 148                  'location' => 'mod/assign/templates/grading_panel.mustache',
 149              ],
 150              'Subsystem template' => [
 151                  'template' => 'core_user/status_details',
 152                  'theme' => '',
 153                  'location' => 'user/templates/status_details.mustache',
 154              ],
 155              'Theme own template' => [
 156                  'template' => 'theme_classic/columns',
 157                  'theme' => '',
 158                  'location' => 'theme/classic/templates/columns.mustache',
 159              ],
 160              'Theme overridden template against that theme' => [
 161                  'template' => 'theme_classic/navbar',
 162                  'theme' => 'classic',
 163                  'location' => 'theme/classic/templates/navbar.mustache',
 164              ],
 165              // Note: This one looks strange but is correct. It is legitimate to request theme's component template in
 166              // the context of another theme. For example, this is used by child themes making use of parent theme
 167              // templates.
 168              'Theme overridden template against the default theme' => [
 169                  'template' => 'theme_classic/navbar',
 170                  'theme' => '',
 171                  'location' => 'theme/classic/templates/navbar.mustache',
 172              ],
 173          ];
 174      }
 175  
 176      /**
 177       * Tests for get_template_filepath.
 178       *
 179       * @dataProvider valid_template_filepath_provider
 180       * @param   string $template
 181       * @param   string $theme
 182       * @param   string $location
 183       */
 184      public function test_get_template_filepath(string $template, string $theme, string $location) {
 185          global $CFG;
 186  
 187          $filename = mustache_template_finder::get_template_filepath($template, $theme);
 188          $this->assertEquals("{$CFG->dirroot}/{$location}", $filename);
 189      }
 190  
 191      /**
 192       * Tests for get_template_filepath when dealing with an invalid component.
 193       */
 194      public function test_invalid_component_get_template_filepath() {
 195          $this->expectException(moodle_exception::class);
 196          mustache_template_finder::get_template_filepath('core/octopus', 'classic');
 197      }
 198  }