Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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   */
  37  class core_theme_config_testcase extends advanced_testcase {
  38      /**
  39       * This function will test directives used to serve SVG images to make sure
  40       * this are making the right decisions.
  41       */
  42      public function test_svg_image_use() {
  43          global $CFG;
  44  
  45          $this->resetAfterTest();
  46  
  47          // The two required tests.
  48          $this->assertTrue(file_exists($CFG->dirroot.'/pix/i/test.svg'));
  49          $this->assertTrue(file_exists($CFG->dirroot.'/pix/i/test.png'));
  50  
  51          $theme = theme_config::load(theme_config::DEFAULT_THEME);
  52  
  53          // First up test the forced setting.
  54          $imagefile = $theme->resolve_image_location('i/test', 'moodle', true);
  55          $this->assertSame('test.svg', basename($imagefile));
  56          $imagefile = $theme->resolve_image_location('i/test', 'moodle', false);
  57          $this->assertSame('test.png', basename($imagefile));
  58  
  59          // Now test the use of the svgicons config setting.
  60          // We need to clone the theme as usesvg property is calculated only once.
  61          $testtheme = clone $theme;
  62          $CFG->svgicons = true;
  63          $imagefile = $testtheme->resolve_image_location('i/test', 'moodle', null);
  64          $this->assertSame('test.svg', basename($imagefile));
  65          $CFG->svgicons = false;
  66          // We need to clone the theme as usesvg property is calculated only once.
  67          $testtheme = clone $theme;
  68          $imagefile = $testtheme->resolve_image_location('i/test', 'moodle', null);
  69          $this->assertSame('test.png', basename($imagefile));
  70          unset($CFG->svgicons);
  71  
  72          // Finally test a few user agents.
  73          $useragents = array(
  74              // IE7 on XP.
  75              'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)' => false,
  76              // IE8 on Vista.
  77              'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)' => false,
  78              // IE8 on Vista in compatibility mode.
  79              'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/4.0)' => false,
  80              // IE8 on Windows 7.
  81              'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)' => false,
  82              // IE9 on Windows 7.
  83              'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)' => true,
  84              // IE9 on Windows 7 in intranet mode.
  85              'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/5.0)' => false,
  86              // IE10 on Windows 8.
  87              'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0; Touch)' => true,
  88              // IE10 on Windows 8 in compatibility mode.
  89              'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; Trident/6.0; Touch; .NET4.0E; .NET4.0C; Tablet PC 2.0)' => true,
  90              // IE11 on Windows 8.
  91              'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0)' => true,
  92              // IE11 on Windows 8 in compatibility mode.
  93              'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.3; Trident/7.0; .NET4.0E; .NET4.0C)' => true,
  94              // Chrome 11 on Windows.
  95              '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,
  96              // Chrome 22 on Windows.
  97              'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1' => true,
  98              // Chrome 21 on Ubuntu 12.04.
  99              'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1' => true,
 100              // Firefox 4 on Windows.
 101              'Mozilla/5.0 (Windows NT 6.1; rv:1.9) Gecko/20100101 Firefox/4.0' => true,
 102              // Firefox 15 on Windows.
 103              'Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20120716 Firefox/15.0.1' => true,
 104              // Firefox 15 on Ubuntu.
 105              'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1' => true,
 106              // Opera 12.02 on Ubuntu.
 107              'Opera/9.80 (X11; Linux x86_64; U; en) Presto/2.10.289 Version/12.02' => false,
 108              // Android browser pre 1.0.
 109              'Mozilla/5.0 (Linux; U; Android 0.5; en-us) AppleWebKit/522+ (KHTML, like Gecko) Safari/419.3' => false,
 110              // Android browser 2.3 (HTC).
 111              '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,
 112              // Android browser 3.0 (Motorola).
 113              '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,
 114              // Android browser 4.3 (Samsung GT-9505).
 115              '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
 116          );
 117          foreach ($useragents as $agent => $expected) {
 118              core_useragent::instance(true, $agent);
 119              // We need to clone the theme as usesvg property is calculated only once.
 120              $testtheme = clone $theme;
 121              $imagefile = $testtheme->resolve_image_location('i/test', 'moodle', null);
 122              if ($expected) {
 123                  $this->assertSame('test.svg', basename($imagefile), 'Incorrect image returned for user agent `'.$agent.'`');
 124              } else {
 125                  $this->assertSame('test.png', basename($imagefile), 'Incorrect image returned for user agent `'.$agent.'`');
 126              }
 127          }
 128      }
 129  
 130      /**
 131       * This function will test custom device detection regular expression setting.
 132       */
 133      public function test_devicedetectregex() {
 134          global $CFG;
 135  
 136          $this->resetAfterTest();
 137  
 138          // Check config currently empty.
 139          $this->assertEmpty(json_decode($CFG->devicedetectregex));
 140          $this->assertTrue(core_useragent::set_user_device_type('tablet'));
 141          $exceptionoccured = false;
 142          try {
 143              core_useragent::set_user_device_type('featurephone');
 144          } catch (moodle_exception $e) {
 145              $exceptionoccured = true;
 146          }
 147          $this->assertTrue($exceptionoccured);
 148  
 149          // Set config and recheck.
 150          $config = array('featurephone' => '(Symbian|MIDP-1.0|Maemo|Windows CE)');
 151          $CFG->devicedetectregex = json_encode($config);
 152          core_useragent::instance(true); // Clears singleton cache.
 153          $this->assertTrue(core_useragent::set_user_device_type('tablet'));
 154          $this->assertTrue(core_useragent::set_user_device_type('featurephone'));
 155      }
 156  
 157      /**
 158       * Confirm that the editor_css_url contains the theme revision and the
 159       * theme subrevision if not in theme designer mode.
 160       */
 161      public function test_editor_css_url_has_revision_and_subrevision() {
 162          global $CFG;
 163  
 164          $this->resetAfterTest();
 165          $theme = theme_config::load(theme_config::DEFAULT_THEME);
 166          $themename = $theme->name;
 167          $themerevision = 1234;
 168          $themesubrevision = 5678;
 169  
 170          $CFG->themedesignermode = false;
 171          $CFG->themerev = $themerevision;
 172  
 173          theme_set_sub_revision_for_theme($themename, $themesubrevision);
 174          $url = $theme->editor_css_url();
 175  
 176          $this->assertRegExp("/{$themerevision}_{$themesubrevision}/", $url->out(false));
 177      }
 178  
 179      /**
 180       * Confirm that editor_scss_to_css is correctly compiling for themes with no parent.
 181       */
 182      public function test_editor_scss_to_css_root_theme() {
 183          global $CFG;
 184  
 185          $this->resetAfterTest();
 186          $theme = theme_config::load('boost');
 187          $editorscss = $CFG->dirroot.'/theme/boost/scss/editor.scss';
 188  
 189          $this->assertTrue(file_exists($editorscss));
 190          $compiler = new core_scss();
 191          $compiler->set_file($editorscss);
 192          $cssexpected = $compiler->to_css();
 193          $cssactual = $theme->editor_scss_to_css();
 194  
 195          $this->assertEquals($cssexpected, $cssactual);
 196      }
 197  
 198      /**
 199       * Confirm that editor_scss_to_css is compiling for a child theme not overriding its parent's editor SCSS.
 200       */
 201      public function test_editor_scss_to_css_child_theme() {
 202          global $CFG;
 203  
 204          $this->resetAfterTest();
 205          $theme = theme_config::load('classic');
 206          $editorscss = $CFG->dirroot.'/theme/boost/scss/editor.scss';
 207  
 208          $this->assertTrue(file_exists($editorscss));
 209          $compiler = new core_scss();
 210          $compiler->set_file($editorscss);
 211          $cssexpected = $compiler->to_css();
 212          $cssactual = $theme->editor_scss_to_css();
 213  
 214          $this->assertEquals($cssexpected, $cssactual);
 215      }
 216  }