From 807c6707656e024024f6c18b80d9f54ac3f89205 Mon Sep 17 00:00:00 2001 From: Jenny Gray Date: Wed, 4 Jun 2014 16:10:18 +0100 Subject: [PATCH 1/2] MDL-45846 Libraries Reinstate custom user agent device detection --- lib/classes/useragent.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/classes/useragent.php b/lib/classes/useragent.php index 7e0d40d0eaa..39c3ed811f6 100644 --- a/lib/classes/useragent.php +++ b/lib/classes/useragent.php @@ -120,7 +120,7 @@ class core_useragent { protected function __construct($forceuseragent = null) { global $CFG; if (!empty($CFG->devicedetectregex)) { - $this->devicetypecustoms = json_decode($CFG->devicedetectregex); + $this->devicetypecustoms = json_decode($CFG->devicedetectregex, true); } if ($forceuseragent !== null) { $this->useragent = $forceuseragent; From 1d20e997a05c88c1ac3b4a7fa21732e247ac486e Mon Sep 17 00:00:00 2001 From: Jenny Gray Date: Thu, 5 Jun 2014 15:47:30 +0100 Subject: [PATCH 2/2] MDL-45846 Libraries custom user agent device detection unit test added --- lib/classes/useragent.php | 5 +++++ lib/tests/theme_config_test.php | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/classes/useragent.php b/lib/classes/useragent.php index 39c3ed811f6..81094ba1ef5 100644 --- a/lib/classes/useragent.php +++ b/lib/classes/useragent.php @@ -122,6 +122,11 @@ class core_useragent { if (!empty($CFG->devicedetectregex)) { $this->devicetypecustoms = json_decode($CFG->devicedetectregex, true); } + if ($this->devicetypecustoms === null) { + // This shouldn't happen unless you're hardcoding the config value. + debugging('Config devicedetectregex is not valid JSON object'); + $this->devicetypecustoms = array(); + } if ($forceuseragent !== null) { $this->useragent = $forceuseragent; } else if (!empty($_SERVER['HTTP_USER_AGENT'])) { diff --git a/lib/tests/theme_config_test.php b/lib/tests/theme_config_test.php index 3f88d227ace..99ab6a98423 100644 --- a/lib/tests/theme_config_test.php +++ b/lib/tests/theme_config_test.php @@ -126,4 +126,31 @@ class core_theme_config_testcase extends advanced_testcase { } } } + + /** + * This function will test custom device detection regular expression setting. + */ + public function test_devicedetectregex() { + global $CFG; + + $this->resetAfterTest(); + + // Check config currently empty. + $this->assertEmpty(json_decode($CFG->devicedetectregex)); + $this->assertTrue(core_useragent::set_user_device_type('tablet')); + $exceptionoccured = false; + try { + core_useragent::set_user_device_type('featurephone'); + } catch (moodle_exception $e) { + $exceptionoccured = true; + } + $this->assertTrue($exceptionoccured); + + // Set config and recheck. + $config = array('featurephone' => '(Symbian|MIDP-1.0|Maemo|Windows CE)'); + $CFG->devicedetectregex = json_encode($config); + core_useragent::instance(true); // Clears singleton cache. + $this->assertTrue(core_useragent::set_user_device_type('tablet')); + $this->assertTrue(core_useragent::set_user_device_type('featurephone')); + } }