Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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  /**
  18   * Tests the theme config class.
  19   *
  20   * @package   core
  21   * @category  phpunit
  22   * @copyright 2012 Sam Hemelryk
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5)
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->libdir . '/outputlib.php');
  30  
  31  /**
  32   * Tests the theme config class.
  33   *
  34   * @copyright 2012 Sam Hemelryk
  35   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   * @covers \theme_config
  37   * @coversDefaultClass \theme_config
  38   */
  39  class theme_config_test extends advanced_testcase {
  40      /**
  41       * This function will test directives used to serve SVG images to make sure
  42       * this are making the right decisions.
  43       */
  44      public function test_svg_image_use() {
  45          global $CFG;
  46  
  47          $this->resetAfterTest();
  48  
  49          // The two required tests.
  50          $this->assertTrue(file_exists($CFG->dirroot.'/pix/i/test.svg'));
  51          $this->assertTrue(file_exists($CFG->dirroot.'/pix/i/test.png'));
  52  
  53          $theme = theme_config::load(theme_config::DEFAULT_THEME);
  54  
  55          // First up test the forced setting.
  56          $imagefile = $theme->resolve_image_location('i/test', 'moodle', true);
  57          $this->assertSame('test.svg', basename($imagefile));
  58          $imagefile = $theme->resolve_image_location('i/test', 'moodle', false);
  59          $this->assertSame('test.png', basename($imagefile));
  60  
  61          // Finally test a few user agents.
  62          $useragents = [
  63              'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.3; Trident/7.0; .NET4.0E; .NET4.0C)' => true,
  64              // Chrome 11 on Windows.
  65              'Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US) AppleWebKit/534.17 (KHTML, like Gecko) ' .
  66                     'Chrome/11.0.652.0 Safari/534.17' => true,
  67              // Chrome 22 on Windows.
  68              'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1' => true,
  69              // Chrome 21 on Ubuntu 12.04.
  70              'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1' => true,
  71              // Firefox 4 on Windows.
  72              'Mozilla/5.0 (Windows NT 6.1; rv:1.9) Gecko/20100101 Firefox/4.0' => true,
  73              // Firefox 15 on Windows.
  74              'Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20120716 Firefox/15.0.1' => true,
  75              // Firefox 15 on Ubuntu.
  76              'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1' => true,
  77              // Opera 15 on MacOS.
  78              'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 ' .
  79                      'Safari/537.36 OPR/15.0.1147.153' => true,
  80              // Android browser pre 1.0.
  81              'Mozilla/5.0 (Linux; U; Android 0.5; en-us) AppleWebKit/522+ (KHTML, like Gecko) Safari/419.3' => false,
  82              // Android browser 2.3 (HTC).
  83              'Mozilla/5.0 (Linux; U; Android 2.3.5; en-us; HTC Vision Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) ' .
  84                      'Version/4.0 Mobile Safari/533.1' => false,
  85              // Android browser 3.0 (Motorola).
  86              'Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 ' .
  87                      'Safari/534.13' => true,
  88              // Android browser 4.3 (Samsung GT-9505).
  89              'Mozilla/5.0 (Linux; Android 4.3; it-it; SAMSUNG GT-I9505/I9505XXUEMJ7 Build/JSS15J) AppleWebKit/537.36 '.
  90                      '(KHTML, like Gecko) Version/1.5 Chrome/28.0.1500.94 Mobile Safari/537.36' => true,
  91          ];
  92          foreach ($useragents as $agent => $expected) {
  93              core_useragent::instance(true, $agent);
  94              // We need to clone the theme as usesvg property is calculated only once.
  95              $testtheme = clone $theme;
  96              $imagefile = $testtheme->resolve_image_location('i/test', 'moodle', null);
  97              if ($expected) {
  98                  $this->assertSame('test.svg', basename($imagefile), 'Incorrect image returned for user agent `'.$agent.'`');
  99              } else {
 100                  $this->assertSame('test.png', basename($imagefile), 'Incorrect image returned for user agent `'.$agent.'`');
 101              }
 102          }
 103      }
 104  
 105      /**
 106       * Confirm that the editor_css_url contains the theme revision and the
 107       * theme subrevision if not in theme designer mode.
 108       */
 109      public function test_editor_css_url_has_revision_and_subrevision() {
 110          global $CFG;
 111  
 112          $this->resetAfterTest();
 113          $theme = theme_config::load(theme_config::DEFAULT_THEME);
 114          $themename = $theme->name;
 115          $themerevision = 1234;
 116          $themesubrevision = 5678;
 117  
 118          $CFG->themedesignermode = false;
 119          $CFG->themerev = $themerevision;
 120  
 121          theme_set_sub_revision_for_theme($themename, $themesubrevision);
 122          $url = $theme->editor_css_url();
 123  
 124          $this->assertMatchesRegularExpression("/{$themerevision}_{$themesubrevision}/", $url->out(false));
 125      }
 126  
 127      /**
 128       * Confirm that editor_scss_to_css is correctly compiling for themes with no parent.
 129       */
 130      public function test_editor_scss_to_css_root_theme() {
 131          global $CFG;
 132  
 133          $this->resetAfterTest();
 134          $theme = theme_config::load('boost');
 135          $editorscss = $CFG->dirroot.'/theme/boost/scss/editor.scss';
 136  
 137          $this->assertTrue(file_exists($editorscss));
 138          $compiler = new core_scss();
 139          $compiler->set_file($editorscss);
 140          $cssexpected = $compiler->to_css();
 141          $cssactual = $theme->editor_scss_to_css();
 142  
 143          $this->assertEquals($cssexpected, $cssactual);
 144      }
 145  
 146      /**
 147       * Confirm that editor_scss_to_css is compiling for a child theme not overriding its parent's editor SCSS.
 148       */
 149      public function test_editor_scss_to_css_child_theme() {
 150          global $CFG;
 151  
 152          $this->resetAfterTest();
 153          $theme = theme_config::load('classic');
 154          $editorscss = $CFG->dirroot.'/theme/boost/scss/editor.scss';
 155  
 156          $this->assertTrue(file_exists($editorscss));
 157          $compiler = new core_scss();
 158          $compiler->set_file($editorscss);
 159          $cssexpected = $compiler->to_css();
 160          $cssactual = $theme->editor_scss_to_css();
 161  
 162          $this->assertEquals($cssexpected, $cssactual);
 163      }
 164  
 165      /**
 166       * Test that {@see theme_config::get_all_block_regions()} returns localised list of region names.
 167       *
 168       * @covers ::get_all_block_regions
 169       */
 170      public function test_get_all_block_regions() {
 171          $this->resetAfterTest();
 172  
 173          $theme = theme_config::load(theme_config::DEFAULT_THEME);
 174          $regions = $theme->get_all_block_regions();
 175  
 176          $this->assertEquals('Right', $regions['side-pre']);
 177      }
 178  }