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]

   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  use core_scss;
  20  
  21  /**
  22   * This file contains the unittests for core scss.
  23   *
  24   * @package   core
  25   * @category  phpunit
  26   * @copyright 2016 onwards Ankit Agarwal
  27   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   */
  29  class scss_test extends \advanced_testcase {
  30  
  31      /**
  32       * Data provider for is_valid_file
  33       * @return array
  34       */
  35      public function is_valid_file_provider() {
  36          $themedirectory = \core_component::get_component_directory('theme_boost');
  37          $realroot = realpath($themedirectory);
  38          return [
  39              "File import 1" => [
  40                  "path" => "../test.php",
  41                  "valid" => false
  42              ],
  43              "File import 2" => [
  44                  "path" => "../test.py",
  45                  "valid" => false
  46              ],
  47              "File import 3" => [
  48                  "path" => $realroot . "/scss/moodle.scss",
  49                  "valid" => true
  50              ],
  51              "File import 4" => [
  52                  "path" => $realroot . "/scss/../../../config.php",
  53                  "valid" => false
  54              ],
  55              "File import 5" => [
  56                  "path" => "/../../../../etc/passwd",
  57                  "valid" => false
  58              ],
  59              "File import 6" => [
  60                  "path" => "random",
  61                  "valid" => false
  62              ]
  63          ];
  64      }
  65  
  66      /**
  67       * Test cases for SassC compilation.
  68       */
  69      public function scss_compilation_provider() {
  70          return [
  71              'simple' => [
  72                  'scss' => '$font-stack: Helvetica, sans-serif;
  73                             $primary-color: #333;
  74  
  75                             body {
  76                               font: 100% $font-stack;
  77                               color: $primary-color;
  78                             }',
  79                  'expected' => <<<CSS
  80  body {
  81    font: 100% Helvetica, sans-serif;
  82    color: #333; }
  83  
  84  CSS
  85              ],
  86              'nested' => [
  87                  'scss' => 'nav {
  88                               ul {
  89                                 margin: 0;
  90                                 padding: 0;
  91                                 list-style: none;
  92                               }
  93  
  94                             li { display: inline-block; }
  95  
  96                             a {
  97                               display: block;
  98                               padding: 6px 12px;
  99                               text-decoration: none;
 100                             }
 101                           }',
 102                  'expected' => <<<CSS
 103  nav ul {
 104    margin: 0;
 105    padding: 0;
 106    list-style: none; }
 107  
 108  nav li {
 109    display: inline-block; }
 110  
 111  nav a {
 112    display: block;
 113    padding: 6px 12px;
 114    text-decoration: none; }
 115  
 116  CSS
 117              ]
 118          ];
 119      }
 120  
 121      /**
 122       * @dataProvider is_valid_file_provider
 123       */
 124      public function test_is_valid_file($path, $valid) {
 125          $scss = new \core_scss();
 126          $pathvalid = \phpunit_util::call_internal_method($scss, 'is_valid_file', [$path], \core_scss::class);
 127          $this->assertSame($valid, $pathvalid);
 128      }
 129  
 130      /**
 131       * Test that we can use the SassC compiler if it's provided.
 132       *
 133       * @dataProvider scss_compilation_provider
 134       * @param string $scss The raw scss to compile.
 135       * @param string $expectedcss The expected CSS output.
 136       */
 137      public function test_scss_compilation_with_sassc($scss, $expectedcss) {
 138          if (!defined('PHPUNIT_PATH_TO_SASSC')) {
 139              $this->markTestSkipped('Path to SassC not provided');
 140          }
 141  
 142          $this->resetAfterTest();
 143          set_config('pathtosassc', PHPUNIT_PATH_TO_SASSC);
 144          $compiler = new core_scss();
 145          $this->assertSame($compiler->compile($scss), $expectedcss);
 146      }
 147  }