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 * User agent test suite.
21 * @copyright 2013 Sam Hemelryk
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @covers \core_useragent
25 class useragent_test
extends advanced_testcase
{
28 * Restores the user agent to the default one.
30 public function tearDown(): void
{
31 core_useragent
::instance(true);
36 * Data provider for known user agents.
40 public function user_agents_providers() {
41 // Note: When adding new entries to this list, please ensure that any new browser versions are added to the corresponding list.
42 // This ensures that regression tests are applied to all known user agents.
44 'Microsoft Edge for Windows 10 Desktop' => array(
45 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10136',
48 'check_edge_version' => array(
52 // Edge pretends to be WebKit.
55 // Edge pretends to be Chrome.
56 // Note: Because Edge pretends to be Chrome, it will not be picked up as a Safari browser.
58 'check_chrome_version' => array(
65 'versionclasses' => array(
70 'Microsoft Edge for Windows 10 Mobile' => array(
71 'Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; DEVICE INFO) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Mobile Safari/537.36 Edge/12.10136',
74 'check_edge_version' => array(
78 // Edge pretends to be WebKit.
81 // Mobile Edge pretends to be Android.
82 'is_webkit_android' => true,
83 'check_webkit_android_version' => array(
88 // Edge pretends to be Chrome.
89 // Note: Because Edge pretends to be Chrome, it will not be picked up as a Safari browser.
91 'check_chrome_version' => array(
98 'versionclasses' => array(
102 'devicetype' => 'mobile',
106 // Windows XP; Firefox 1.0.6.
108 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.10) Gecko/20050716 Firefox/1.0.6',
110 'is_firefox' => true,
113 'check_gecko_version' => array(
117 'versionclasses' => array(
124 // Windows XP; Firefox 1.0.6.
126 'Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.8) Gecko/20051107 Firefox/1.5',
128 'is_firefox' => true,
129 'check_firefox_version' => array(
134 'check_gecko_version' => array(
140 'versionclasses' => array(
147 // Windows XP; Firefox 1.5.0.1.
149 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1',
151 'is_firefox' => true,
152 'check_firefox_version' => array(
157 'check_gecko_version' => array(
163 'versionclasses' => array(
170 // Windows XP; Firefox 2.0.
172 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1',
174 'is_firefox' => true,
175 'check_firefox_version' => array(
180 'check_gecko_version' => array(
185 '2006010100' => true,
188 'versionclasses' => array(
195 // Ubuntu Linux amd64; Firefox 2.0.
197 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1) Gecko/20060601 Firefox/2.0 (Ubuntu-edgy)',
199 'is_firefox' => true,
200 'check_firefox_version' => array(
205 'check_gecko_version' => array(
210 '2006010100' => true,
213 'versionclasses' => array(
220 // SUSE; Firefox 3.0.6.
222 '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',
224 'is_firefox' => true,
225 'check_firefox_version' => array(
231 'check_gecko_version' => array(
236 '2006010100' => true,
239 'versionclasses' => array(
246 // Linux i686; Firefox 3.6.
248 'Mozilla/5.0 (X11; Linux i686; rv:2.0) Gecko/20100101 Firefox/3.6',
250 'is_firefox' => true,
251 'check_firefox_version' => array(
257 'check_gecko_version' => array(
262 '2006010100' => true,
267 'versionclasses' => array(
274 // Windows; Firefox 11.0.
276 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko Firefox/11.0',
278 'is_firefox' => true,
279 'check_firefox_version' => array(
287 'check_gecko_version' => array(
292 '2006010100' => true,
298 'versionclasses' => array(
304 // Windows; Firefox 15.0a2.
306 'Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20120716 Firefox/15.0a2',
308 'is_firefox' => true,
309 'check_firefox_version' => array(
318 'check_gecko_version' => array(
323 '2006010100' => true,
330 'versionclasses' => array(
336 // Firefox 18; Mac OS X 10.
338 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:18.0) Gecko/18.0 Firefox/18.0',
340 'is_firefox' => true,
341 'check_firefox_version' => array(
351 'check_gecko_version' => array(
356 '2006010100' => true,
364 'versionclasses' => array(
370 // Firefox 33; Mac OS X 10.10.
372 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:33.0) Gecko/20100101 Firefox/33.0',
374 'is_firefox' => true,
375 'check_firefox_version' => array(
387 'check_gecko_version' => array(
392 '2006010100' => true,
401 'versionclasses' => array(
407 // SeaMonkey 2.0; Windows.
409 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1b3pre) Gecko/20081208 SeaMonkey/2.0',
412 'check_gecko_version' => array(
418 '2006010100' => true,
421 'versionclasses' => array(
428 // SeaMonkey 2.1; Linux.
430 'Mozilla/5.0 (X11; Linux x86_64; rv:2.0.1) Gecko/20110609 Firefox/4.0.1 SeaMonkey/2.1',
433 'check_gecko_version' => array(
438 '2006010100' => true,
444 'is_firefox' => true,
445 'check_firefox_version' => array(
451 'versionclasses' => array(
458 // SeaMonkey 2.3; FreeBSD.
460 'Mozilla/5.0 (X11; FreeBSD amd64; rv:6.0) Gecko/20110818 Firefox/6.0 SeaMonkey/2.3',
463 'check_gecko_version' => array(
468 '2006010100' => true,
474 'is_firefox' => true,
475 'check_firefox_version' => array(
481 'versionclasses' => array(
487 // Mac OS X; MS Word 14.
489 'Mozilla/5.0 (Macintosh; Intel Mac OS X) Word/14.38.0',
491 'versionclasses' => array(
498 // Safari 312; Max OS X.
500 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/312.1 (KHTML, like Gecko) Safari/312',
503 'check_safari_version' => array(
510 'versionclasses' => array(
516 // Safari 412; Max OS X.
518 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/412 (KHTML, like Gecko) Safari/412',
521 'check_safari_version' => array(
528 'versionclasses' => array(
534 // Safari 2.0; Max OS X.
536 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/412 (KHTML, like Gecko) Safari/412',
539 'check_safari_version' => array(
546 'versionclasses' => array(
552 // iOS Safari 528; iPhone.
554 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; cs-cz) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11 Safari/528.16',
556 // Note: We do *not* identify mobile Safari as Safari.
557 'is_safari_ios' => true,
559 'check_safari_ios_version' => array(
565 'versionclasses' => array(
569 'devicetype' => 'mobile',
573 // Safari; iPhone 6 Plus; iOS 8.1; Build 12B411.
575 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_10 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12B411 Safari/600.1.4',
577 // Note: We do *not* identify mobile Safari as Safari.
578 'is_safari_ios' => true,
580 'check_safari_ios_version' => array(
588 'versionclasses' => array(
592 'devicetype' => 'mobile',
596 // iOS Safari 533; iPad.
598 'Mozilla/5.0 (iPad; U; CPU OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5',
600 // Note: We do *not* identify mobile Safari as Safari.
601 'is_safari_ios' => true,
603 'check_safari_ios_version' => array(
609 'versionclasses' => array(
613 'devicetype' => 'tablet',
617 // Android WebKit 525; G1 Phone.
618 'Android WebKit 525; G1 Phone' => array(
619 'Mozilla/5.0 (Linux; U; Android 1.1; en-gb; dream) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2 – G1 Phone',
621 'is_webkit_android' => true,
622 'check_webkit_android_version' => array(
628 'versionclasses' => array(
632 'devicetype' => 'mobile',
634 'supports_svg' => false,
638 // Android WebKit 530; Nexus.
639 'Android WebKit 530; Nexus' => array(
640 'Mozilla/5.0 (Linux; U; Android 2.1; en-us; Nexus One Build/ERD62) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17 –Nexus',
642 'is_webkit_android' => true,
643 'check_webkit_android_version' => array(
650 'versionclasses' => array(
654 'devicetype' => 'mobile',
656 'supports_svg' => false,
660 // Android WebKit 537; Samsung GT-9505.
662 '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',
664 'is_webkit_android' => true,
665 'check_webkit_android_version' => array(
673 'check_chrome_version' => array(
679 'versionclasses' => array(
684 'devicetype' => 'mobile',
688 // Android WebKit 537; Nexus 5.
690 'Mozilla/5.0 (Linux; Android 5.0; Nexus 5 Build/LPX13D) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.102 Mobile Safari/537.36',
692 'is_webkit_android' => true,
693 'check_webkit_android_version' => array(
701 'check_chrome_version' => array(
707 'versionclasses' => array(
712 'devicetype' => 'mobile',
716 // Chrome 8; Mac OS X.
718 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_5; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.215 Safari/534.10',
721 'check_chrome_version' => array(
728 'versionclasses' => array(
734 // Chrome 39; Mac OS X.
736 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36',
739 'check_chrome_version' => array(
748 'versionclasses' => array(
754 // Opera 12.15 (Build 1748); Mac OS X.
756 'Opera/9.80 (Macintosh; Intel Mac OS X 10.10.0; Edition MAS) Presto/2.12.388 Version/12.15',
759 'check_opera_version' => array(
766 'versionclasses' => array(
772 // Google web crawlers.
774 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
776 'is_web_crawler' => true,
777 'versionclasses' => array(
782 'Googlebot/2.1 (+http://www.googlebot.com/bot.html)',
784 'is_web_crawler' => true,
785 'versionclasses' => array(
790 'Googlebot-Image/1.0',
792 'is_web_crawler' => true,
793 'versionclasses' => array(
799 // See https://help.yahoo.com/kb/slurp-crawling-page-sln22600.html.
801 'Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)',
803 'is_web_crawler' => true,
804 'versionclasses' => array(
809 // Bing / MSN / AdIdx crawlers.
810 // See http://www.bing.com/webmaster/help/which-crawlers-does-bing-use-8c184ec0.
812 'Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)',
814 'is_web_crawler' => true,
815 'versionclasses' => array(
820 'Mozilla/5.0 (compatible; bingbot/2.0 +http://www.bing.com/bingbot.htm)',
822 'is_web_crawler' => true,
823 'versionclasses' => array(
828 'Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)',
830 'is_web_crawler' => true,
832 'is_safari_ios' => true,
834 'check_safari_ios_version' => array(
838 'versionclasses' => array(
842 'devicetype' => 'mobile',
846 'Mozilla/5.0 (Windows Phone 8.1; ARM; Trident/7.0; Touch; rv:11.0; IEMobile/11.0; NOKIA; Lumia 530) like Gecko (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)',
848 'is_web_crawler' => true,
850 'check_ie_version' => array(
861 'versionclasses' => array(
865 'devicetype' => 'mobile',
870 'msnbot/2.0b (+http://search.msn.com/msnbot.htm)',
872 'is_web_crawler' => true,
873 'versionclasses' => array(
880 'is_web_crawler' => true,
881 'versionclasses' => array(
886 'msnbot-media/1.1 (+http://search.msn.com/msnbot.htm)',
888 'is_web_crawler' => true,
889 'versionclasses' => array(
894 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534+ (KHTML, like Gecko) BingPreview/1.0b',
896 'is_web_crawler' => true,
899 'check_safari_version' => array(
905 'versionclasses' => array(
911 'Mozilla/5.0 (Windows Phone 8.1; ARM; Trident/7.0; Touch; rv:11.0; IEMobile/11.0; NOKIA; Lumia 530) like Gecko BingPreview/1.0b',
913 'is_web_crawler' => true,
915 'check_ie_version' => array(
926 'versionclasses' => array(
930 'devicetype' => 'mobile',
935 // See http://help.yandex.com/search/robots/agent.xml.
937 'Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)',
939 'is_web_crawler' => true,
940 'versionclasses' => array(
945 'Mozilla/5.0 (compatible; YandexImages/3.0; +http://yandex.com/bots)',
947 'is_web_crawler' => true,
948 'versionclasses' => array(
955 'AltaVista V2.0B crawler@evreka.com',
957 'is_web_crawler' => true,
958 'versionclasses' => array(
965 'ZoomSpider - wrensoft.com [ZSEBOT]',
967 'is_web_crawler' => true,
968 'versionclasses' => array(
975 'Baiduspider+(+http://www.baidu.com/search/spider_jp.html)',
977 'is_web_crawler' => true,
978 'versionclasses' => array(
983 'Baiduspider+(+http://www.baidu.com/search/spider.htm)',
985 'is_web_crawler' => true,
986 'versionclasses' => array(
993 'User-Agent: Mozilla/2.0 (compatible; Ask Jeeves/Teoma)',
995 'is_web_crawler' => true,
996 'versionclasses' => array(
1003 'User-Agent: MoodleBot/3.8 (+https://moodle.org)',
1005 'is_web_crawler' => true,
1006 'versionclasses' => array(
1011 // Macos Desktop app.
1013 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) moodlemobile/3.6.0 Chrome/69.0.3497.106 Electron/4.0.1 Safari/537.36 MoodleMobile',
1015 'is_moodle_app' => true,
1016 'is_webkit' => true,
1017 'is_chrome' => true,
1018 'check_chrome_version' => array(
1024 'versionclasses' => array(
1030 // Linux Desktop app.
1032 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) moodledesktop/3.6.0 Chrome/69.0.3497.106 Electron/4.0.1 Safari/537.36 MoodleMobile',
1034 'is_moodle_app' => true,
1035 'is_webkit' => true,
1036 'is_chrome' => true,
1037 'check_chrome_version' => array(
1043 'versionclasses' => array(
1049 // Windows Desktop app.
1051 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) moodledesktop/3.6.0 Chrome/69.0.3497.106 Electron/4.0.1 Safari/537.36 MoodleMobile',
1053 'is_moodle_app' => true,
1054 'is_webkit' => true,
1055 'is_chrome' => true,
1056 'check_chrome_version' => array(
1062 'versionclasses' => array(
1070 'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.99 Mobile Safari/537.36 MoodleMobile',
1072 'is_moodle_app' => true,
1073 'is_webkit' => true,
1074 'is_webkit_android' => true,
1075 'is_chrome' => true,
1076 'check_chrome_version' => array(
1082 'devicetype' => 'mobile',
1083 'check_webkit_android_version' => array(
1087 'versionclasses' => array(
1094 // Android app, user agent lower case.
1096 'Mozilla/5.0 (Linux; Android 7.1.1; Moto G Play Build/NPIS26.48-43-2; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.99 Mobile Safari/537.36 moodlemobile',
1098 'is_moodle_app' => true,
1099 'is_webkit' => true,
1100 'is_webkit_android' => true,
1101 'is_chrome' => true,
1102 'check_chrome_version' => array(
1108 'devicetype' => 'mobile',
1109 'check_webkit_android_version' => array(
1113 'versionclasses' => array(
1120 // iOS (iPhone) app.
1122 'Mozilla/5.0 (iPhone; CPU OS 13_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Mobile/14G60 MoodleMobile',
1124 'is_moodle_app' => true,
1126 'is_webkit' => true,
1127 'devicetype' => 'mobile',
1128 'versionclasses' => array(
1135 'Mozilla/5.0 (iPad; CPU OS 12_1_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16D39 MoodleMobile',
1137 'is_moodle_app' => true,
1139 'is_webkit' => true,
1140 'devicetype' => 'tablet',
1141 'versionclasses' => array(
1149 * Test instance generation.
1151 public function test_instance(): void
{
1152 $this->assertInstanceOf('core_useragent', core_useragent
::instance());
1153 $this->assertInstanceOf('core_useragent', core_useragent
::instance(true));
1157 * @dataProvider user_agents_providers
1159 public function test_useragent_edge($useragent, $tests): void
{
1160 // Setup the core_useragent instance.
1161 core_useragent
::instance(true, $useragent);
1164 if (isset($tests['is_edge']) && $tests['is_edge']) {
1165 $this->assertTrue(core_useragent
::is_edge());
1167 $this->assertFalse(core_useragent
::is_edge());
1171 // New versions of should be added here.
1175 if (isset($tests['check_edge_version'])) {
1176 // The test provider has overwritten some of the above checks.
1177 // Must use the '+' operator, because array_merge will incorrectly rewrite the array keys for integer-based indexes.
1178 $versions = $tests['check_edge_version'] +
$versions;
1181 foreach ($versions as $version => $result) {
1182 $this->assertEquals($result, core_useragent
::check_edge_version($version),
1183 "Version incorrectly determined for Edge version '{$version}'");
1188 * @dataProvider user_agents_providers
1190 public function test_useragent_ie($useragent, $tests): void
{
1191 // Setup the core_useragent instance.
1192 core_useragent
::instance(true, $useragent);
1195 if (isset($tests['is_ie']) && $tests['is_ie']) {
1196 $this->assertTrue(core_useragent
::is_ie());
1198 $this->assertFalse(core_useragent
::is_ie());
1202 // New versions of should be added here.
1217 if (isset($tests['check_ie_version'])) {
1218 // The test provider has overwritten some of the above checks.
1219 // Must use the '+' operator, because array_merge will incorrectly rewrite the array keys for integer-based indexes.
1220 $versions = $tests['check_ie_version'] +
$versions;
1223 foreach ($versions as $version => $result) {
1224 $this->assertEquals($result, core_useragent
::check_ie_version($version),
1225 "Version incorrectly determined for IE version '{$version}'");
1228 // IE Compatibility mode.
1229 if (isset($tests['iecompatibility']) && $tests['iecompatibility']) {
1230 $this->assertTrue(core_useragent
::check_ie_compatibility_view(), "IE Compability false negative");
1232 $this->assertFalse(core_useragent
::check_ie_compatibility_view(), "IE Compability false positive");
1238 * @dataProvider user_agents_providers
1240 public function test_useragent_msword($useragent, $tests): void
{
1241 // Setup the core_useragent instance.
1242 core_useragent
::instance(true, $useragent);
1245 if (isset($tests['is_msword']) && $tests['is_msword']) {
1246 $this->assertTrue(core_useragent
::is_msword());
1248 $this->assertFalse(core_useragent
::is_msword());
1253 * @dataProvider user_agents_providers
1255 public function test_useragent_supports($useragent, $tests): void
{
1256 // Setup the core_useragent instance.
1257 core_useragent
::instance(true, $useragent);
1260 if (!isset($tests['supports_svg']) ||
$tests['supports_svg']) {
1261 $this->assertTrue(core_useragent
::supports_svg(),
1262 "SVG Support was not reported (and should have been)");
1264 $this->assertFalse(core_useragent
::supports_svg(),
1265 "SVG Support was reported (and should not have been)");
1268 // Supports JSON ContentType.
1269 if (!isset($tests['supports_json_contenttype']) ||
$tests['supports_json_contenttype']) {
1270 $this->assertTrue(core_useragent
::supports_json_contenttype(),
1271 "JSON ContentType Support was not reported (and should have been)");
1273 $this->assertFalse(core_useragent
::supports_json_contenttype(),
1274 "JSON ContentType Support was reported (and should not have been)");
1279 * @dataProvider user_agents_providers
1281 public function test_useragent_webkit($useragent, $tests): void
{
1282 // Setup the core_useragent instance.
1283 core_useragent
::instance(true, $useragent);
1285 if (isset($tests['is_webkit']) && $tests['is_webkit']) {
1286 $this->assertTrue(core_useragent
::is_webkit(),
1287 "Browser was not identified as a webkit browser");
1288 $this->assertTrue(core_useragent
::check_webkit_version());
1290 $this->assertFalse(core_useragent
::is_webkit(),
1291 "Browser was incorrectly identified as a webkit browser");
1292 $this->assertFalse(core_useragent
::check_webkit_version());
1296 // New versions should be added here.
1299 if (isset($tests['check_webkit_version'])) {
1300 // The test provider has overwritten some of the above checks.
1301 // Must use the '+' operator, because array_merge will incorrectly rewrite the array keys for integer-based indexes.
1302 $versions = $tests['check_webkit_version'] +
$versions;
1305 foreach ($versions as $version => $result) {
1306 $this->assertEquals($result, core_useragent
::check_webkit_version($version),
1307 "Version incorrectly determined for Webkit version '{$version}'");
1312 * @dataProvider user_agents_providers
1314 public function test_useragent_webkit_android($useragent, $tests): void
{
1315 // Setup the core_useragent instance.
1316 core_useragent
::instance(true, $useragent);
1318 if (isset($tests['is_webkit_android']) && $tests['is_webkit_android']) {
1319 $this->assertTrue(core_useragent
::is_webkit_android(),
1320 "Browser was not identified as an Android webkit browser");
1321 $this->assertTrue(core_useragent
::check_webkit_android_version());
1323 $this->assertFalse(core_useragent
::is_webkit_android(),
1324 "Browser was incorrectly identified as an Android webkit browser");
1325 $this->assertFalse(core_useragent
::check_webkit_android_version());
1329 // New versions should be added here.
1335 if (isset($tests['check_webkit_android_version'])) {
1336 // The test provider has overwritten some of the above checks.
1337 // Must use the '+' operator, because array_merge will incorrectly rewrite the array keys for integer-based indexes.
1338 $versions = $tests['check_webkit_android_version'] +
$versions;
1341 foreach ($versions as $version => $result) {
1342 $this->assertEquals($result, core_useragent
::check_webkit_android_version($version),
1343 "Version incorrectly determined for Android webkit version '{$version}'");
1348 * @dataProvider user_agents_providers
1350 public function test_useragent_chrome($useragent, $tests): void
{
1351 // Setup the core_useragent instance.
1352 core_useragent
::instance(true, $useragent);
1354 if (isset($tests['is_chrome']) && $tests['is_chrome']) {
1355 $this->assertTrue(core_useragent
::is_chrome(),
1356 "Browser was not identified as a chrome browser");
1357 $this->assertTrue(core_useragent
::check_chrome_version());
1359 $this->assertFalse(core_useragent
::is_chrome(),
1360 "Browser was incorrectly identified as a chrome browser");
1361 $this->assertFalse(core_useragent
::check_chrome_version());
1365 // New versions should be added here.
1372 if (isset($tests['check_chrome_version'])) {
1373 // The test provider has overwritten some of the above checks.
1374 // Must use the '+' operator, because array_merge will incorrectly rewrite the array keys for integer-based indexes.
1375 $versions = $tests['check_chrome_version'] +
$versions;
1378 foreach ($versions as $version => $result) {
1379 $this->assertEquals($result, core_useragent
::check_chrome_version($version),
1380 "Version incorrectly determined for Chrome version '{$version}'");
1385 * @dataProvider user_agents_providers
1387 public function test_useragent_safari($useragent, $tests): void
{
1388 // Setup the core_useragent instance.
1389 core_useragent
::instance(true, $useragent);
1391 if (isset($tests['is_safari']) && $tests['is_safari']) {
1392 $this->assertTrue(core_useragent
::is_safari(),
1393 "Browser was not identified as a safari browser");
1394 $this->assertTrue(core_useragent
::check_safari_version());
1396 $this->assertFalse(core_useragent
::is_safari(),
1397 "Browser was incorrectly identified as a safari browser");
1398 $this->assertFalse(core_useragent
::check_safari_version());
1401 // Check Safari (generic).
1403 // New versions should be added here.
1409 if (isset($tests['check_safari_version'])) {
1410 // The test provider has overwritten some of the above checks.
1411 // Must use the '+' operator, because array_merge will incorrectly rewrite the array keys for integer-based indexes.
1412 $versions = $tests['check_safari_version'] +
$versions;
1415 foreach ($versions as $version => $result) {
1416 $this->assertEquals($result, core_useragent
::check_safari_version($version),
1417 "Version incorrectly determined for Safari (generic) version '{$version}'");
1422 * @dataProvider user_agents_providers
1424 public function test_useragent_ios_safari($useragent, $tests): void
{
1425 // Setup the core_useragent instance.
1426 core_useragent
::instance(true, $useragent);
1428 if (isset($tests['is_safari_ios']) && $tests['is_safari_ios']) {
1429 $this->assertTrue(core_useragent
::is_safari_ios(),
1430 "Browser was not identified as an iOS safari browser");
1431 $this->assertTrue(core_useragent
::check_safari_ios_version());
1433 $this->assertFalse(core_useragent
::is_safari_ios(),
1434 "Browser was incorrectly identified as an iOS safari browser");
1435 $this->assertFalse(core_useragent
::check_safari_ios_version());
1438 // Check iOS Safari.
1440 // New versions should be added here.
1446 if (isset($tests['check_safari_ios_version'])) {
1447 // The test provider has overwritten some of the above checks.
1448 // Must use the '+' operator, because array_merge will incorrectly rewrite the array keys for integer-based indexes.
1449 $versions = $tests['check_safari_ios_version'] +
$versions;
1452 foreach ($versions as $version => $result) {
1453 $this->assertEquals($result, core_useragent
::check_safari_ios_version($version),
1454 "Version incorrectly determined for iOS Safari version '{$version}'");
1459 * @dataProvider user_agents_providers
1461 public function test_useragent_ios($useragent, $tests): void
{
1462 // Setup the core_useragent instance.
1463 core_useragent
::instance(true, $useragent);
1465 if (isset($tests['is_ios']) && $tests['is_ios']) {
1466 $this->assertTrue(core_useragent
::is_ios(),
1467 "Browser was not identified as an iOS device browser");
1468 // The iOS app is not Safari based.
1469 if (!isset($tests['is_moodle_app']) ||
!$tests['is_moodle_app']) {
1470 $this->assertTrue(core_useragent
::check_safari_ios_version());
1473 $this->assertFalse(core_useragent
::is_ios(),
1474 "Browser was incorrectly identified as an iOS device browser");
1475 $this->assertFalse(core_useragent
::check_safari_ios_version());
1480 * @dataProvider user_agents_providers
1482 public function test_useragent_gecko($useragent, $tests): void
{
1483 // Setup the core_useragent instance.
1484 core_useragent
::instance(true, $useragent);
1486 if (isset($tests['is_gecko']) && $tests['is_gecko']) {
1487 $this->assertTrue(core_useragent
::is_gecko(),
1488 "Browser was not identified as a gecko browser");
1489 $this->assertTrue(core_useragent
::check_gecko_version());
1491 $this->assertFalse(core_useragent
::is_gecko(),
1492 "Browser was incorrectly identified as a gecko browser");
1493 $this->assertFalse(core_useragent
::check_gecko_version());
1497 // New versions should be added here.
1502 '20030516' => false,
1503 '20051116' => false,
1504 '2006010100' => false,
1505 '20100101' => false,
1511 if (isset($tests['check_gecko_version'])) {
1512 // The test provider has overwritten some of the above checks.
1513 // Must use the '+' operator, because array_merge will incorrectly rewrite the array keys for integer-based indexes.
1514 $versions = $tests['check_gecko_version'] +
$versions;
1517 foreach ($versions as $version => $result) {
1518 $this->assertEquals($result, core_useragent
::check_gecko_version($version),
1519 "Version incorrectly determined for Gecko version '{$version}'");
1524 * @dataProvider user_agents_providers
1526 public function test_useragent_firefox($useragent, $tests): void
{
1527 // Setup the core_useragent instance.
1528 core_useragent
::instance(true, $useragent);
1530 if (isset($tests['is_firefox']) && $tests['is_firefox']) {
1531 $this->assertTrue(core_useragent
::is_firefox(),
1532 "Browser was not identified as a firefox browser");
1533 $this->assertTrue(core_useragent
::check_firefox_version());
1535 $this->assertFalse(core_useragent
::is_firefox(),
1536 "Browser was incorrectly identified as a firefox browser");
1537 $this->assertFalse(core_useragent
::check_firefox_version());
1541 // New versions should be added here.
1552 if (isset($tests['check_firefox_version'])) {
1553 // The test provider has overwritten some of the above checks.
1554 // Must use the '+' operator, because array_merge will incorrectly rewrite the array keys for integer-based indexes.
1555 $versions = $tests['check_firefox_version'] +
$versions;
1558 foreach ($versions as $version => $result) {
1559 $this->assertEquals($result, core_useragent
::check_firefox_version($version),
1560 "Version incorrectly determined for Firefox version '{$version}'");
1565 * @dataProvider user_agents_providers
1567 public function test_useragent_opera($useragent, $tests): void
{
1568 // Setup the core_useragent instance.
1569 core_useragent
::instance(true, $useragent);
1571 if (isset($tests['is_opera']) && $tests['is_opera']) {
1572 $this->assertTrue(core_useragent
::is_opera(),
1573 "Browser was not identified as a opera browser");
1574 $this->assertTrue(core_useragent
::check_opera_version());
1576 $this->assertFalse(core_useragent
::is_opera(),
1577 "Browser was incorrectly identified as a opera browser");
1578 $this->assertFalse(core_useragent
::check_opera_version());
1582 // New versions should be added here.
1589 if (isset($tests['check_opera_version'])) {
1590 // The test provider has overwritten some of the above checks.
1591 // Must use the '+' operator, because array_merge will incorrectly rewrite the array keys for integer-based indexes.
1592 $versions = $tests['check_opera_version'] +
$versions;
1595 foreach ($versions as $version => $result) {
1596 $this->assertEquals($result, core_useragent
::check_opera_version($version),
1597 "Version incorrectly determined for Opera version '{$version}'");
1602 * @dataProvider user_agents_providers
1604 public function test_get_device_type($useragent, $tests): void
{
1605 // Setup the core_useragent instance.
1606 core_useragent
::instance(true, $useragent);
1608 $expected = 'default';
1609 if (isset($tests['devicetype'])) {
1610 $expected = $tests['devicetype'];
1613 $this->assertEquals($expected, core_useragent
::get_device_type(),
1614 "Device Type was not correctly identified");
1618 * @dataProvider user_agents_providers
1620 public function test_get_browser_version_classes($useragent, $tests): void
{
1621 // Setup the core_useragent instance.
1622 core_useragent
::instance(true, $useragent);
1624 $actual = core_useragent
::get_browser_version_classes();
1625 foreach ($tests['versionclasses'] as $expectedclass) {
1626 $this->assertContains($expectedclass, $actual);
1628 $this->assertCount(count($tests['versionclasses']), $actual);
1632 * @dataProvider user_agents_providers
1634 public function test_useragent_web_crawler($useragent, $tests): void
{
1635 // Setup the core_useragent instance.
1636 core_useragent
::instance(true, $useragent);
1638 $expectation = isset($tests['is_web_crawler']) ?
$tests['is_web_crawler'] : false;
1639 $this->assertSame($expectation, core_useragent
::is_web_crawler());
1643 * @dataProvider user_agents_providers
1645 public function test_useragent_moodle_app($useragent, $tests): void
{
1646 // Setup the core_useragent instance.
1647 core_useragent
::instance(true, $useragent);
1649 $expectation = isset($tests['is_moodle_app']) ?
$tests['is_moodle_app'] : false;
1650 $this->assertSame($expectation, core_useragent
::is_moodle_app());