Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [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          // Now test the use of the svgicons config setting.
  62          // We need to clone the theme as usesvg property is calculated only once.
  63          $testtheme = clone $theme;
  64          $CFG->svgicons = true;
  65          $imagefile = $testtheme->resolve_image_location('i/test', 'moodle', null);
  66          $this->assertSame('test.svg', basename($imagefile));
  67          $CFG->svgicons = false;
  68          // We need to clone the theme as usesvg property is calculated only once.
  69          $testtheme = clone $theme;
  70          $imagefile = $testtheme->resolve_image_location('i/test', 'moodle', null);
  71          $this->assertSame('test.png', basename($imagefile));
  72          unset($CFG->svgicons);
  73  
  74          // Finally test a few user agents.
  75          $useragents = array(
  76              // IE7 on XP.
  77              'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)' => false,
  78              // IE8 on Vista.
  79              'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)' => false,
  80              // IE8 on Vista in compatibility mode.
  81              'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/4.0)' => false,
  82              // IE8 on Windows 7.
  83              'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)' => false,
  84              // IE9 on Windows 7.
  85              'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)' => true,
  86              // IE9 on Windows 7 in intranet mode.
  87              'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/5.0)' => false,
  88              // IE10 on Windows 8.
  89              'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0; Touch)' => true,
  90              // IE10 on Windows 8 in compatibility mode.
  91              'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; Trident/6.0; Touch; .NET4.0E; .NET4.0C; Tablet PC 2.0)' => true,
  92              // IE11 on Windows 8.
  93              'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0)' => true,
  94              // IE11 on Windows 8 in compatibility mode.
  95              'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.3; Trident/7.0; .NET4.0E; .NET4.0C)' => true,
  96              // Chrome 11 on Windows.
  97              'Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US) AppleWebKit/534.17 (KHTML, like Gecko) Chrome/11.0.652.0 Safari/534.17' => true,
  98              // Chrome 22 on Windows.
  99              'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1' => true,
 100              // Chrome 21 on Ubuntu 12.04.
 101              'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1' => true,
 102              // Firefox 4 on Windows.
 103              'Mozilla/5.0 (Windows NT 6.1; rv:1.9) Gecko/20100101 Firefox/4.0' => true,
 104              // Firefox 15 on Windows.
 105              'Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20120716 Firefox/15.0.1' => true,
 106              // Firefox 15 on Ubuntu.
 107              'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1' => true,
 108              // Opera 12.02 on Ubuntu.
 109              'Opera/9.80 (X11; Linux x86_64; U; en) Presto/2.10.289 Version/12.02' => false,
 110              // Android browser pre 1.0.
 111              'Mozilla/5.0 (Linux; U; Android 0.5; en-us) AppleWebKit/522+ (KHTML, like Gecko) Safari/419.3' => false,
 112              // Android browser 2.3 (HTC).
 113              'Mozilla/5.0 (Linux; U; Android 2.3.5; en-us; HTC Vision Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1' => false,
 114              // Android browser 3.0 (Motorola).
 115              'Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13' => true,
 116              // Android browser 4.3 (Samsung GT-9505).
 117              'Mozilla/5.0 (Linux; Android 4.3; it-it; SAMSUNG GT-I9505/I9505XXUEMJ7 Build/JSS15J) AppleWebKit/537.36 (KHTML, like Gecko) Version/1.5 Chrome/28.0.1500.94 Mobile Safari/537.36' => true
 118          );
 119          foreach ($useragents as $agent => $expected) {
 120              core_useragent::instance(true, $agent);
 121              // We need to clone the theme as usesvg property is calculated only once.
 122              $testtheme = clone $theme;
 123              $imagefile = $testtheme->resolve_image_location('i/test', 'moodle', null);
 124              if ($expected) {
 125                  $this->assertSame('test.svg', basename($imagefile), 'Incorrect image returned for user agent `'.$agent.'`');
 126              } else {
 127                  $this->assertSame('test.png', basename($imagefile), 'Incorrect image returned for user agent `'.$agent.'`');
 128              }
 129          }
 130      }
 131  
 132      /**
 133       * This function will test custom device detection regular expression setting.
 134       *
 135       * @covers \core_useragent
 136       */
 137      public function test_devicedetectregex() {
 138          global $CFG;
 139  
 140          $this->resetAfterTest();
 141  
 142          // Check config currently empty.
 143          $this->assertEmpty(json_decode($CFG->devicedetectregex));
 144          $this->assertTrue(core_useragent::set_user_device_type('tablet'));
 145          $exceptionoccured = false;
 146          try {
 147              core_useragent::set_user_device_type('featurephone');
 148          } catch (moodle_exception $e) {
 149              $exceptionoccured = true;
 150          }
 151          $this->assertTrue($exceptionoccured);
 152  
 153          // Set config and recheck.
 154          $config = array('featurephone' => '(Symbian|MIDP-1.0|Maemo|Windows CE)');
 155          $CFG->devicedetectregex = json_encode($config);
 156          core_useragent::instance(true); // Clears singleton cache.
 157          $this->assertTrue(core_useragent::set_user_device_type('tablet'));
 158          $this->assertTrue(core_useragent::set_user_device_type('featurephone'));
 159      }
 160  
 161      /**
 162       * Confirm that the editor_css_url contains the theme revision and the
 163       * theme subrevision if not in theme designer mode.
 164       */
 165      public function test_editor_css_url_has_revision_and_subrevision() {
 166          global $CFG;
 167  
 168          $this->resetAfterTest();
 169          $theme = theme_config::load(theme_config::DEFAULT_THEME);
 170          $themename = $theme->name;
 171          $themerevision = 1234;
 172          $themesubrevision = 5678;
 173  
 174          $CFG->themedesignermode = false;
 175          $CFG->themerev = $themerevision;
 176  
 177          theme_set_sub_revision_for_theme($themename, $themesubrevision);
 178          $url = $theme->editor_css_url();
 179  
 180          $this->assertMatchesRegularExpression("/{$themerevision}_{$themesubrevision}/", $url->out(false));
 181      }
 182  
 183      /**
 184       * Confirm that editor_scss_to_css is correctly compiling for themes with no parent.
 185       */
 186      public function test_editor_scss_to_css_root_theme() {
 187          global $CFG;
 188  
 189          $this->resetAfterTest();
 190          $theme = theme_config::load('boost');
 191          $editorscss = $CFG->dirroot.'/theme/boost/scss/editor.scss';
 192  
 193          $this->assertTrue(file_exists($editorscss));
 194          $compiler = new core_scss();
 195          $compiler->set_file($editorscss);
 196          $cssexpected = $compiler->to_css();
 197          $cssactual = $theme->editor_scss_to_css();
 198  
 199          $this->assertEquals($cssexpected, $cssactual);
 200      }
 201  
 202      /**
 203       * Confirm that editor_scss_to_css is compiling for a child theme not overriding its parent's editor SCSS.
 204       */
 205      public function test_editor_scss_to_css_child_theme() {
 206          global $CFG;
 207  
 208          $this->resetAfterTest();
 209          $theme = theme_config::load('classic');
 210          $editorscss = $CFG->dirroot.'/theme/boost/scss/editor.scss';
 211  
 212          $this->assertTrue(file_exists($editorscss));
 213          $compiler = new core_scss();
 214          $compiler->set_file($editorscss);
 215          $cssexpected = $compiler->to_css();
 216          $cssactual = $theme->editor_scss_to_css();
 217  
 218          $this->assertEquals($cssexpected, $cssactual);
 219      }
 220  
 221      /**
 222       * Test that {@see theme_config::get_all_block_regions()} returns localised list of region names.
 223       *
 224       * @covers ::get_all_block_regions
 225       */
 226      public function test_get_all_block_regions() {
 227          $this->resetAfterTest();
 228  
 229          $theme = theme_config::load(theme_config::DEFAULT_THEME);
 230          $regions = $theme->get_all_block_regions();
 231  
 232          $this->assertEquals('Right', $regions['side-pre']);
 233      }
 234  }