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