From ec0123208d4b9a6d7bf07bb5ba44354623242cd5 Mon Sep 17 00:00:00 2001 From: nicolasconnault Date: Tue, 10 Feb 2009 15:33:25 +0000 Subject: [PATCH] MDL-18188 Added a new function with matching unit tests, following Sam's comment in the tracker --- lib/moodlelib.php | 33 ++++++++++++++++++++++++++++++++ lib/simpletest/testmoodlelib.php | 30 +++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/lib/moodlelib.php b/lib/moodlelib.php index d13ffe76b1c..dc8fde36dbd 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -6405,6 +6405,39 @@ function check_php_version($version='5.2.4') { return false; } +/** + * Returns one or several CSS class names that match the user's browser. These can be put + * in the body tag of the page to apply browser-specific rules without relying on CSS hacks + */ +function get_browser_version_classes() { + $classes = ''; + if (check_browser_version("MSIE", "0")) { + $classes .= 'ie '; + if (check_browser_version("MSIE", 8)) { + $classes .= 'ie8 '; + } elseif (check_browser_version("MSIE", 7)) { + $classes .= 'ie7 '; + } elseif (check_browser_version("MSIE", 6)) { + $classes .= 'ie6 '; + } + } elseif (check_browser_version("Firefox", 0) || check_browser_version("Gecko", 0) || check_browser_version("Camino", 0)) { + $classes .= 'gecko '; + + if (preg_match('/rv\:([1-2])\.([0-9])/', $_SERVER['HTTP_USER_AGENT'], $matches)) { + $classes .= "gecko{$matches[1]}{$matches[2]} "; + } + + } elseif (check_browser_version("Safari", 0)) { + $classes .= 'safari '; + + } elseif (check_browser_version("Opera", 0)) { + $classes .= 'opera '; + + } + + return $classes; +} + /** * This function makes the return value of ini_get consistent if you are * setting server directives through the .htaccess file in apache. diff --git a/lib/simpletest/testmoodlelib.php b/lib/simpletest/testmoodlelib.php index 3f1f2fffd46..4300698b9af 100644 --- a/lib/simpletest/testmoodlelib.php +++ b/lib/simpletest/testmoodlelib.php @@ -45,14 +45,17 @@ class moodlelib_test extends UnitTestCase { 'MSIE' => array( '5.5' => array('Windows 2000' => 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)'), '6.0' => array('Windows XP SP2' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'), - '7.0' => array('Windows XP SP2' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; YPC 3.0.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)') + '7.0' => array('Windows XP SP2' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; YPC 3.0.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)'), + '8.0' => array('Windows Vista' => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648)'), + ), 'Firefox' => array( '1.0.6' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.10) Gecko/20050716 Firefox/1.0.6'), '1.5' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.8) Gecko/20051107 Firefox/1.5'), '1.5.0.1' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1'), '2.0' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1', - 'Ubuntu Linux AMD64' => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1) Gecko/20060601 Firefox/2.0 (Ubuntu-edgy)') + 'Ubuntu Linux AMD64' => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1) Gecko/20060601 Firefox/2.0 (Ubuntu-edgy)'), + '3.0.6' => array('SUSE' => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.6) Gecko/2009012700 SUSE/3.0.6-1.4 Firefox/3.0.6'), ), 'Safari' => array( '312' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/312.1 (KHTML, like Gecko) Safari/312'), @@ -204,6 +207,29 @@ class moodlelib_test extends UnitTestCase { $this->assertFalse(check_browser_version('Firefox', '3.0')); } + function test_get_browser_version_classes() { + $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X']; + $this->assertEqual('safari ', get_browser_version_classes()); + + $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP']; + $this->assertEqual('opera ', get_browser_version_classes()); + + $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2']; + $this->assertEqual('ie ie6 ', get_browser_version_classes()); + + $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['7.0']['Windows XP SP2']; + $this->assertEqual('ie ie7 ', get_browser_version_classes()); + + $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['8.0']['Windows Vista']; + $this->assertEqual('ie ie8 ', get_browser_version_classes()); + + $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP']; + $this->assertEqual('gecko gecko18 ', get_browser_version_classes()); + + $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['3.0.6']['SUSE']; + $this->assertEqual('gecko gecko19 ', get_browser_version_classes()); + } + function test_optional_param() { $_POST['username'] = 'post_user';