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.

Differences Between: [Versions 310 and 401] [Versions 39 and 401] [Versions 401 and 402] [Versions 401 and 403]

   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;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->libdir . '/outputrequirementslib.php');
  23  
  24  
  25  /**
  26   * Unit tests for lib/outputrequirementslibphp.
  27   *
  28   * @package   core
  29   * @category  test
  30   * @copyright 2012 Petr Škoda
  31   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class outputrequirementslib_test extends \advanced_testcase {
  34      public function test_string_for_js() {
  35          $this->resetAfterTest();
  36  
  37          $page = new \moodle_page();
  38          $page->requires->string_for_js('course', 'moodle', 1);
  39          $page->requires->string_for_js('course', 'moodle', 1);
  40          $this->expectException('coding_exception');
  41          $page->requires->string_for_js('course', 'moodle', 2);
  42  
  43          // Note: we can not switch languages in phpunit yet,
  44          //       it would be nice to test that the strings are actually fetched in the footer.
  45      }
  46  
  47      public function test_one_time_output_normal_case() {
  48          $page = new \moodle_page();
  49          $this->assertTrue($page->requires->should_create_one_time_item_now('test_item'));
  50          $this->assertFalse($page->requires->should_create_one_time_item_now('test_item'));
  51      }
  52  
  53      public function test_one_time_output_repeat_output_throws() {
  54          $page = new \moodle_page();
  55          $page->requires->set_one_time_item_created('test_item');
  56          $this->expectException('coding_exception');
  57          $page->requires->set_one_time_item_created('test_item');
  58      }
  59  
  60      public function test_one_time_output_different_pages_independent() {
  61          $firstpage = new \moodle_page();
  62          $secondpage = new \moodle_page();
  63          $this->assertTrue($firstpage->requires->should_create_one_time_item_now('test_item'));
  64          $this->assertTrue($secondpage->requires->should_create_one_time_item_now('test_item'));
  65      }
  66  
  67      /**
  68       * Test for the jquery_plugin method.
  69       *
  70       * Test to make sure that backslashes are not generated with either slasharguments set to on or off.
  71       */
  72      public function test_jquery_plugin() {
  73          global $CFG, $PAGE;
  74  
  75          $this->resetAfterTest();
  76  
  77          // With slasharguments on.
  78          $CFG->slasharguments = 1;
  79  
  80          $page = new \moodle_page();
  81          $requirements = $page->requires;
  82          // Assert successful method call.
  83          $this->assertTrue($requirements->jquery_plugin('jquery'));
  84          $this->assertTrue($requirements->jquery_plugin('ui'));
  85  
  86          // Get the code containing the required jquery plugins.
  87          $renderer = $PAGE->get_renderer('core', null, RENDERER_TARGET_MAINTENANCE);
  88          $requirecode = $requirements->get_top_of_body_code($renderer);
  89          // Make sure that the generated code does not contain backslashes.
  90          $this->assertFalse(strpos($requirecode, '\\'), "Output contains backslashes: " . $requirecode);
  91  
  92          // With slasharguments off.
  93          $CFG->slasharguments = 0;
  94  
  95          $page = new \moodle_page();
  96          $requirements = $page->requires;
  97          // Assert successful method call.
  98          $this->assertTrue($requirements->jquery_plugin('jquery'));
  99          $this->assertTrue($requirements->jquery_plugin('ui'));
 100  
 101          // Get the code containing the required jquery plugins.
 102          $requirecode = $requirements->get_top_of_body_code($renderer);
 103          // Make sure that the generated code does not contain backslashes.
 104          $this->assertFalse(strpos($requirecode, '\\'), "Output contains backslashes: " . $requirecode);
 105      }
 106  
 107      /**
 108       * Test AMD modules loading.
 109       */
 110      public function test_js_call_amd() {
 111  
 112          $page = new \moodle_page();
 113  
 114          // Load an AMD module without a function call.
 115          $page->requires->js_call_amd('theme_foobar/lightbox');
 116  
 117          // Load an AMD module and call its function without parameters.
 118          $page->requires->js_call_amd('theme_foobar/demo_one', 'init');
 119  
 120          // Load an AMD module and call its function with some parameters.
 121          $page->requires->js_call_amd('theme_foobar/demo_two', 'init', [
 122              'foo',
 123              'keyWillIgnored' => 'baz',
 124              [42, 'xyz'],
 125          ]);
 126  
 127          $html = $page->requires->get_end_code();
 128  
 129          $modname = 'theme_foobar/lightbox';
 130          $this->assertStringContainsString("M.util.js_pending('{$modname}'); require(['{$modname}'], function(amd) {M.util.js_complete('{$modname}');});", $html);
 131  
 132          $modname = 'theme_foobar/demo_one';
 133          $this->assertStringContainsString("M.util.js_pending('{$modname}'); require(['{$modname}'], function(amd) {amd.init(); M.util.js_complete('{$modname}');});", $html);
 134  
 135          $modname = 'theme_foobar/demo_two';
 136          $this->assertStringContainsString("M.util.js_pending('{$modname}'); require(['{$modname}'], function(amd) {amd.init(\"foo\", \"baz\", [42,\"xyz\"]); M.util.js_complete('{$modname}');});", $html);
 137      }
 138  }