PluginProbe
GetResponse Forms by Optin Cat / 1.3.4
GetResponse Forms by Optin Cat v1.3.4
1.3.4 1.3.5 1.3.6 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.1 1.6.2 1.6.3 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 2.0.0 All 36 releases
getresponse / includes / classes / Mobile-Detect / tests / UserAgentTest.php

UserAgentTest.php in GetResponse Forms by Optin Cat 1.3.4, at includes/classes/Mobile-Detect/tests/UserAgentTest.php

185 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @license MIT License https://github.com/serbanghita/Mobile-Detect/blob/master/LICENSE.txt
4 * @link http://mobiledetect.net
5 */
6 class UserAgentTest extends PHPUnit_Framework_TestCase
7 {
8 protected static $ualist = array();
9 protected static $json;
10
11 public static function generateJson()
12 {
13 //in case this gets run multiple times
14 if (isset(self::$json)) {
15 return self::$json;
16 }
17
18 //the json and PHP formatted files
19 $jsonFile = dirname(__FILE__) . '/ualist.json';
20 $phpFile = dirname(__FILE__) . '/UA_List.inc.php';
21
22 //currently stored as a PHP array
23 $list = include $phpFile;
24
25 //check recency of the file
26 if (file_exists($jsonFile) && is_readable($jsonFile)) {
27 //read the json file
28 $json = json_decode(file_get_contents($jsonFile), true);
29
30 //check that the hash matches
31 $hash = isset($json['hash']) ? $json['hash'] : null;
32
33 if ($hash == sha1(serialize($list))) {
34 //file is up to date, just read the json file
35 self::$json = $json['user_agents'];
36
37 return self::$json;
38 }
39 }
40
41
42 //uses the UA_List.inc.php to generate a json file
43 if (file_exists($jsonFile) && !is_writable($jsonFile)) {
44 throw new RuntimeException("Need to be able to create/update $jsonFile from UA_List.inc.php.");
45 }
46
47 if (!is_writable(dirname($jsonFile))) {
48 throw new RuntimeException("Insufficient permissions to create this file: $jsonFile");
49 }
50
51
52
53 //print_r($list['Acer']); exit;
54
55 $json = array();
56
57 foreach ($list as $vendor => $vendorList) {
58 foreach ($vendorList as $userAgent => $props) {
59 if (is_int($userAgent)) {
60 //this means that the user agent is the props
61 $userAgent = $props;
62 $props = array();
63 }
64
65 $tmp = array(
66 'vendor' => $vendor,
67 'user_agent' => $userAgent
68 );
69
70 if (isset($props['isMobile'])) {
71 $tmp['mobile'] = $props['isMobile'];
72 }
73
74 if (isset($props['isTablet'])) {
75 $tmp['tablet'] = $props['isTablet'];
76 }
77
78 if (isset($props['version'])) {
79 $tmp['version'] = $props['version'];
80 }
81
82 if (isset($props['model'])) {
83 $tmp['model'] = $props['model'];
84 }
85
86 $json[] = $tmp;
87 }
88 }
89
90 //save the hash
91 $hash = sha1(serialize($list));
92 $json = array(
93 'hash' => $hash,
94 'user_agents' => $json
95 );
96
97 if (defined('JSON_PRETTY_PRINT')) {
98 $jsonString = json_encode($json, JSON_PRETTY_PRINT);
99 } else {
100 $jsonString = json_encode($json);
101 }
102
103 file_put_contents($jsonFile, $jsonString);
104 self::$json = $json['user_agents'];
105
106 return self::$json;
107 }
108
109 public static function setUpBeforeClass()
110 {
111 //generate json file first
112 self::generateJson();
113
114 //get the generated JSON data
115 $json = self::$json;
116
117 //make a list that is usable by functions (THE ORDER OF THE KEYS MATTERS!)
118 foreach ($json as $userAgent) {
119 $tmp = array();
120 $tmp[] = isset($userAgent['user_agent']) ? $userAgent['user_agent'] : null;
121 $tmp[] = isset($userAgent['mobile']) ? $userAgent['mobile'] : null;
122 $tmp[] = isset($userAgent['tablet']) ? $userAgent['tablet'] : null;
123 $tmp[] = isset($userAgent['version']) ? $userAgent['version'] : null;
124 $tmp[] = isset($userAgent['model']) ? $userAgent['model'] : null;
125 $tmp[] = isset($userAgent['vendor']) ? $userAgent['vendor'] : null;
126
127 self::$ualist[] = $tmp;
128 }
129 }
130
131 public function userAgentData()
132 {
133 if (!count(self::$ualist)) {
134 self::setUpBeforeClass();
135 }
136
137 return self::$ualist;
138 }
139
140 /**
141 * @medium
142 * @dataProvider userAgentData
143 */
144 public function testUserAgents($userAgent, $isMobile, $isTablet, $version, $model, $vendor)
145 {
146 //make sure we're passed valid data
147 if (!is_string($userAgent) || !is_bool($isMobile) || !is_bool($isTablet)) {
148 $this->markTestIncomplete("The User-Agent $userAgent does not have sufficient information for testing.");
149
150 return;
151 }
152
153 //setup
154 $md = new Mobile_Detect;
155 $md->setUserAgent($userAgent);
156
157 //is mobile?
158 $this->assertEquals($md->isMobile(), $isMobile);
159
160 //is tablet?
161 $this->assertEquals($md->isTablet(), $isTablet, 'FAILED: ' . $userAgent . ' isTablet: ' . $isTablet);
162
163 if (isset($version)) {
164 foreach ($version as $condition => $assertion) {
165 $this->assertEquals($assertion, $md->version($condition), 'FAILED UA (version("'.$condition.'")): '.$userAgent);
166 }
167 }
168
169 //version property tests
170 if (isset($version)) {
171 foreach ($version as $property => $stringVersion) {
172 $v = $md->version($property);
173 $this->assertSame($stringVersion, $v);
174 }
175 }
176
177 //@todo: model test, not sure how exactly yet
178 //@todo: vendor test. The below is theoretical, but fails 50% of the tests...
179 /*if (isset($vendor)) {
180 $method = "is$vendor";
181 $this->assertTrue($md->{$method}(), "Expected Mobile_Detect::{$method}() to be true.");
182 }*/
183 }
184 }
185