/lib/tests/mustache_template_finder_test.php

https://github.com/markn86/moodle · PHP · 198 lines · 119 code · 14 blank · 65 comment · 1 complexity · 9aeab26463702dfb7dd8155944e3ef24 MD5 · raw file

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