2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Tests the theme config class.
22 * @copyright 2012 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5)
26 defined('MOODLE_INTERNAL') ||
die();
29 require_once($CFG->libdir
. '/outputlib.php');
32 * Tests the theme config class.
34 * @copyright 2012 Sam Hemelryk
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class core_theme_config_testcase
extends advanced_testcase
{
39 * This function will test directives used to serve SVG images to make sure
40 * this are making the right decisions.
42 public function test_svg_image_use() {
45 $this->resetAfterTest();
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'));
51 $theme = theme_config
::load(theme_config
::DEFAULT_THEME
);
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));
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
);
72 // Finally test a few user agents.
75 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)' => false,
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,
81 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)' => false,
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,
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,
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
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);
123 $this->assertSame('test.svg', basename($imagefile), 'Incorrect image returned for user agent `'.$agent.'`');
125 $this->assertSame('test.png', basename($imagefile), 'Incorrect image returned for user agent `'.$agent.'`');
131 * This function will test custom device detection regular expression setting.
133 public function test_devicedetectregex() {
136 $this->resetAfterTest();
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;
143 core_useragent
::set_user_device_type('featurephone');
144 } catch (moodle_exception
$e) {
145 $exceptionoccured = true;
147 $this->assertTrue($exceptionoccured);
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'));
158 * Confirm that the editor_css_url contains the theme revision and the
159 * theme subrevision if not in theme designer mode.
161 public function test_editor_css_url_has_revision_and_subrevision() {
164 $this->resetAfterTest();
165 $theme = theme_config
::load(theme_config
::DEFAULT_THEME
);
166 $themename = $theme->name
;
167 $themerevision = 1234;
168 $themesubrevision = 5678;
170 $CFG->themedesignermode
= false;
171 $CFG->themerev
= $themerevision;
173 theme_set_sub_revision_for_theme($themename, $themesubrevision);
174 $url = $theme->editor_css_url();
176 $this->assertRegExp("/{$themerevision}_{$themesubrevision}/", $url->out(false));
180 * Confirm that editor_scss_to_css is correctly compiling for themes with no parent.
182 public function test_editor_scss_to_css_root_theme() {
185 $this->resetAfterTest();
186 $theme = theme_config
::load('boost');
187 $editorscss = $CFG->dirroot
.'/theme/boost/scss/editor.scss';
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();
195 $this->assertEquals($cssexpected, $cssactual);
199 * Confirm that editor_scss_to_css is compiling for a child theme not overriding its parent's editor SCSS.
201 public function test_editor_scss_to_css_child_theme() {
204 $this->resetAfterTest();
205 $theme = theme_config
::load('classic');
206 $editorscss = $CFG->dirroot
.'/theme/boost/scss/editor.scss';
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();
214 $this->assertEquals($cssexpected, $cssactual);