moodle/lib/tests/scss_test.php
Eloy Lafuente (stronk7) 1093256560
MDL-81522 phpunit: Add missing void return type to all tests
While this change is not 100% required now, it's good habit
and we are checking for it since Moodle 4.4.

All the changes in this commit have been applied automatically
using the moodle.PHPUnit.TestReturnType sniff and are, exclusively
adding the ": void" return types when missing.
2024-06-11 12:18:04 +02:00

148 lines
4.3 KiB
PHP

<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core;
use core_scss;
/**
* This file contains the unittests for core scss.
*
* @package core
* @category phpunit
* @copyright 2016 onwards Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class scss_test extends \advanced_testcase {
/**
* Data provider for is_valid_file
* @return array
*/
public function is_valid_file_provider() {
$themedirectory = \core_component::get_component_directory('theme_boost');
$realroot = realpath($themedirectory);
return [
"File import 1" => [
"path" => "../test.php",
"valid" => false
],
"File import 2" => [
"path" => "../test.py",
"valid" => false
],
"File import 3" => [
"path" => $realroot . "/scss/moodle.scss",
"valid" => true
],
"File import 4" => [
"path" => $realroot . "/scss/../../../config.php",
"valid" => false
],
"File import 5" => [
"path" => "/../../../../etc/passwd",
"valid" => false
],
"File import 6" => [
"path" => "random",
"valid" => false
]
];
}
/**
* Test cases for SassC compilation.
*/
public function scss_compilation_provider() {
return [
'simple' => [
'scss' => '$font-stack: Helvetica, sans-serif;
$primary-color: #333;
body {
font: 100% $font-stack;
color: $primary-color;
}',
'expected' => <<<CSS
body {
font: 100% Helvetica, sans-serif;
color: #333; }
CSS
],
'nested' => [
'scss' => 'nav {
ul {
margin: 0;
padding: 0;
list-style: none;
}
li { display: inline-block; }
a {
display: block;
padding: 6px 12px;
text-decoration: none;
}
}',
'expected' => <<<CSS
nav ul {
margin: 0;
padding: 0;
list-style: none; }
nav li {
display: inline-block; }
nav a {
display: block;
padding: 6px 12px;
text-decoration: none; }
CSS
]
];
}
/**
* @dataProvider is_valid_file_provider
*/
public function test_is_valid_file($path, $valid): void {
$scss = new \core_scss();
$pathvalid = \phpunit_util::call_internal_method($scss, 'is_valid_file', [$path], \core_scss::class);
$this->assertSame($valid, $pathvalid);
}
/**
* Test that we can use the SassC compiler if it's provided.
*
* @dataProvider scss_compilation_provider
* @param string $scss The raw scss to compile.
* @param string $expectedcss The expected CSS output.
*/
public function test_scss_compilation_with_sassc($scss, $expectedcss): void {
if (!defined('PHPUNIT_PATH_TO_SASSC')) {
$this->markTestSkipped('Path to SassC not provided');
}
$this->resetAfterTest();
set_config('pathtosassc', PHPUNIT_PATH_TO_SASSC);
$compiler = new core_scss();
$this->assertSame($compiler->compile($scss), $expectedcss);
}
}