Merge branch 'MDL-48467-28' of git://github.com/merrill-oakland/moodle into MOODLE_28...
[moodle.git] / lib / tests / theme_config_test.php
blob99ab6a9842378ffc3dc868d00934de9292b595ff
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.
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/>.
17 /**
18 * Tests the theme config class.
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)
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->libdir . '/outputlib.php');
31 /**
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 {
38 /**
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() {
43 global $CFG;
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.
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
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.'`');
131 * This function will test custom device detection regular expression setting.
133 public function test_devicedetectregex() {
134 global $CFG;
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;
142 try {
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'));