PageRenderTime 36ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/tests/theme_config_test.php

http://github.com/moodle/moodle
PHP | 216 lines | 114 code | 27 blank | 75 comment | 3 complexity | 8ff8cb7e85ea39df89ef421ba5cdfb4c MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. * Tests the theme config class.
  18. *
  19. * @package core
  20. * @category phpunit
  21. * @copyright 2012 Sam Hemelryk
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5)
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. global $CFG;
  26. require_once($CFG->libdir . '/outputlib.php');
  27. /**
  28. * Tests the theme config class.
  29. *
  30. * @copyright 2012 Sam Hemelryk
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. class core_theme_config_testcase extends advanced_testcase {
  34. /**
  35. * This function will test directives used to serve SVG images to make sure
  36. * this are making the right decisions.
  37. */
  38. public function test_svg_image_use() {
  39. global $CFG;
  40. $this->resetAfterTest();
  41. // The two required tests.
  42. $this->assertTrue(file_exists($CFG->dirroot.'/pix/i/test.svg'));
  43. $this->assertTrue(file_exists($CFG->dirroot.'/pix/i/test.png'));
  44. $theme = theme_config::load(theme_config::DEFAULT_THEME);
  45. // First up test the forced setting.
  46. $imagefile = $theme->resolve_image_location('i/test', 'moodle', true);
  47. $this->assertSame('test.svg', basename($imagefile));
  48. $imagefile = $theme->resolve_image_location('i/test', 'moodle', false);
  49. $this->assertSame('test.png', basename($imagefile));
  50. // Now test the use of the svgicons config setting.
  51. // We need to clone the theme as usesvg property is calculated only once.
  52. $testtheme = clone $theme;
  53. $CFG->svgicons = true;
  54. $imagefile = $testtheme->resolve_image_location('i/test', 'moodle', null);
  55. $this->assertSame('test.svg', basename($imagefile));
  56. $CFG->svgicons = false;
  57. // We need to clone the theme as usesvg property is calculated only once.
  58. $testtheme = clone $theme;
  59. $imagefile = $testtheme->resolve_image_location('i/test', 'moodle', null);
  60. $this->assertSame('test.png', basename($imagefile));
  61. unset($CFG->svgicons);
  62. // Finally test a few user agents.
  63. $useragents = array(
  64. // IE7 on XP.
  65. 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)' => false,
  66. // IE8 on Vista.
  67. 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)' => false,
  68. // IE8 on Vista in compatibility mode.
  69. 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/4.0)' => false,
  70. // IE8 on Windows 7.
  71. 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)' => false,
  72. // IE9 on Windows 7.
  73. 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)' => true,
  74. // IE9 on Windows 7 in intranet mode.
  75. 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/5.0)' => false,
  76. // IE10 on Windows 8.
  77. 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0; Touch)' => true,
  78. // IE10 on Windows 8 in compatibility mode.
  79. 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; Trident/6.0; Touch; .NET4.0E; .NET4.0C; Tablet PC 2.0)' => true,
  80. // IE11 on Windows 8.
  81. 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0)' => true,
  82. // IE11 on Windows 8 in compatibility mode.
  83. 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.3; Trident/7.0; .NET4.0E; .NET4.0C)' => true,
  84. // Chrome 11 on Windows.
  85. '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,
  86. // Chrome 22 on Windows.
  87. 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1' => true,
  88. // Chrome 21 on Ubuntu 12.04.
  89. 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1' => true,
  90. // Firefox 4 on Windows.
  91. 'Mozilla/5.0 (Windows NT 6.1; rv:1.9) Gecko/20100101 Firefox/4.0' => true,
  92. // Firefox 15 on Windows.
  93. 'Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20120716 Firefox/15.0.1' => true,
  94. // Firefox 15 on Ubuntu.
  95. 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1' => true,
  96. // Opera 12.02 on Ubuntu.
  97. 'Opera/9.80 (X11; Linux x86_64; U; en) Presto/2.10.289 Version/12.02' => false,
  98. // Android browser pre 1.0.
  99. 'Mozilla/5.0 (Linux; U; Android 0.5; en-us) AppleWebKit/522+ (KHTML, like Gecko) Safari/419.3' => false,
  100. // Android browser 2.3 (HTC).
  101. '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,
  102. // Android browser 3.0 (Motorola).
  103. '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,
  104. // Android browser 4.3 (Samsung GT-9505).
  105. '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
  106. );
  107. foreach ($useragents as $agent => $expected) {
  108. core_useragent::instance(true, $agent);
  109. // We need to clone the theme as usesvg property is calculated only once.
  110. $testtheme = clone $theme;
  111. $imagefile = $testtheme->resolve_image_location('i/test', 'moodle', null);
  112. if ($expected) {
  113. $this->assertSame('test.svg', basename($imagefile), 'Incorrect image returned for user agent `'.$agent.'`');
  114. } else {
  115. $this->assertSame('test.png', basename($imagefile), 'Incorrect image returned for user agent `'.$agent.'`');
  116. }
  117. }
  118. }
  119. /**
  120. * This function will test custom device detection regular expression setting.
  121. */
  122. public function test_devicedetectregex() {
  123. global $CFG;
  124. $this->resetAfterTest();
  125. // Check config currently empty.
  126. $this->assertEmpty(json_decode($CFG->devicedetectregex));
  127. $this->assertTrue(core_useragent::set_user_device_type('tablet'));
  128. $exceptionoccured = false;
  129. try {
  130. core_useragent::set_user_device_type('featurephone');
  131. } catch (moodle_exception $e) {
  132. $exceptionoccured = true;
  133. }
  134. $this->assertTrue($exceptionoccured);
  135. // Set config and recheck.
  136. $config = array('featurephone' => '(Symbian|MIDP-1.0|Maemo|Windows CE)');
  137. $CFG->devicedetectregex = json_encode($config);
  138. core_useragent::instance(true); // Clears singleton cache.
  139. $this->assertTrue(core_useragent::set_user_device_type('tablet'));
  140. $this->assertTrue(core_useragent::set_user_device_type('featurephone'));
  141. }
  142. /**
  143. * Confirm that the editor_css_url contains the theme revision and the
  144. * theme subrevision if not in theme designer mode.
  145. */
  146. public function test_editor_css_url_has_revision_and_subrevision() {
  147. global $CFG;
  148. $this->resetAfterTest();
  149. $theme = theme_config::load(theme_config::DEFAULT_THEME);
  150. $themename = $theme->name;
  151. $themerevision = 1234;
  152. $themesubrevision = 5678;
  153. $CFG->themedesignermode = false;
  154. $CFG->themerev = $themerevision;
  155. theme_set_sub_revision_for_theme($themename, $themesubrevision);
  156. $url = $theme->editor_css_url();
  157. $this->assertRegExp("/{$themerevision}_{$themesubrevision}/", $url->out(false));
  158. }
  159. /**
  160. * Confirm that editor_scss_to_css is correctly compiling for themes with no parent.
  161. */
  162. public function test_editor_scss_to_css_root_theme() {
  163. global $CFG;
  164. $this->resetAfterTest();
  165. $theme = theme_config::load('boost');
  166. $editorscss = $CFG->dirroot.'/theme/boost/scss/editor.scss';
  167. $this->assertTrue(file_exists($editorscss));
  168. $compiler = new core_scss();
  169. $compiler->set_file($editorscss);
  170. $cssexpected = $compiler->to_css();
  171. $cssactual = $theme->editor_scss_to_css();
  172. $this->assertEquals($cssexpected, $cssactual);
  173. }
  174. /**
  175. * Confirm that editor_scss_to_css is compiling for a child theme not overriding its parent's editor SCSS.
  176. */
  177. public function test_editor_scss_to_css_child_theme() {
  178. global $CFG;
  179. $this->resetAfterTest();
  180. $theme = theme_config::load('classic');
  181. $editorscss = $CFG->dirroot.'/theme/boost/scss/editor.scss';
  182. $this->assertTrue(file_exists($editorscss));
  183. $compiler = new core_scss();
  184. $compiler->set_file($editorscss);
  185. $cssexpected = $compiler->to_css();
  186. $cssactual = $theme->editor_scss_to_css();
  187. $this->assertEquals($cssexpected, $cssactual);
  188. }
  189. }