| 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 BasicTest extends PHPUnit_Framework_TestCase |
| 7 |
{ |
| 8 |
/** |
| 9 |
* @var Mobile_Detect |
| 10 |
*/ |
| 11 |
protected $detect; |
| 12 |
|
| 13 |
public function testClassExists() |
| 14 |
{ |
| 15 |
$this->assertTrue(class_exists('Mobile_Detect')); |
| 16 |
} |
| 17 |
|
| 18 |
public function setUp() |
| 19 |
{ |
| 20 |
$this->detect = new Mobile_Detect; |
| 21 |
} |
| 22 |
|
| 23 |
public function testBasicMethods() |
| 24 |
{ |
| 25 |
$this->assertNotEmpty( $this->detect->getScriptVersion() ); |
| 26 |
|
| 27 |
$this->detect->setHttpHeaders(array( |
| 28 |
'SERVER_SOFTWARE' => 'Apache/2.2.15 (Linux) Whatever/4.0 PHP/5.2.13', |
| 29 |
'REQUEST_METHOD' => 'POST', |
| 30 |
'HTTP_HOST' => 'home.ghita.org', |
| 31 |
'HTTP_X_REAL_IP' => '1.2.3.4', |
| 32 |
'HTTP_X_FORWARDED_FOR' => '1.2.3.5', |
| 33 |
'HTTP_CONNECTION' => 'close', |
| 34 |
'HTTP_USER_AGENT' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0_1 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A523 Safari/8536.25', |
| 35 |
'HTTP_ACCEPT' => 'text/vnd.wap.wml, application/json, text/javascript, */*; q=0.01', |
| 36 |
'HTTP_ACCEPT_LANGUAGE' => 'en-us,en;q=0.5', |
| 37 |
'HTTP_ACCEPT_ENCODING' => 'gzip, deflate', |
| 38 |
'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest', |
| 39 |
'HTTP_REFERER' => 'http://mobiledetect.net', |
| 40 |
'HTTP_PRAGMA' => 'no-cache', |
| 41 |
'HTTP_CACHE_CONTROL' => 'no-cache', |
| 42 |
'REMOTE_ADDR' => '11.22.33.44', |
| 43 |
'REQUEST_TIME' => '01-10-2012 07:57' |
| 44 |
)); |
| 45 |
|
| 46 |
//12 because only 12 start with HTTP_ |
| 47 |
$this->assertCount( 12, $this->detect->getHttpHeaders() ); |
| 48 |
$this->assertTrue( $this->detect->checkHttpHeadersForMobile() ); |
| 49 |
|
| 50 |
$this->detect->setUserAgent('Mozilla/5.0 (iPhone; CPU iPhone OS 6_0_1 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A523 Safari/8536.25'); |
| 51 |
$this->assertNotEmpty( $this->detect->getUserAgent() ); |
| 52 |
|
| 53 |
$this->assertTrue( $this->detect->isMobile() ); |
| 54 |
$this->assertFalse( $this->detect->isTablet() ); |
| 55 |
|
| 56 |
$this->assertTrue( $this->detect->isIphone() ); |
| 57 |
$this->assertTrue( $this->detect->isiphone() ); |
| 58 |
$this->assertTrue( $this->detect->isiOS() ); |
| 59 |
$this->assertTrue( $this->detect->isios() ); |
| 60 |
$this->assertTrue( $this->detect->is('iphone') ); |
| 61 |
$this->assertTrue( $this->detect->is('ios') ); |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
public function headersProvider() |
| 66 |
{ |
| 67 |
return array( |
| 68 |
array(array( |
| 69 |
'SERVER_SOFTWARE' => 'Apache/2.2.15 (Linux) Whatever/4.0 PHP/5.2.13', |
| 70 |
'REQUEST_METHOD' => 'POST', |
| 71 |
'HTTP_HOST' => 'home.ghita.org', |
| 72 |
'HTTP_X_REAL_IP' => '1.2.3.4', |
| 73 |
'HTTP_X_FORWARDED_FOR' => '1.2.3.5', |
| 74 |
'HTTP_CONNECTION' => 'close', |
| 75 |
'HTTP_USER_AGENT' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0_1 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A523 Safari/8536.25', |
| 76 |
'HTTP_ACCEPT' => 'text/vnd.wap.wml, application/json, text/javascript, */*; q=0.01', |
| 77 |
'HTTP_ACCEPT_LANGUAGE' => 'en-us,en;q=0.5', |
| 78 |
'HTTP_ACCEPT_ENCODING' => 'gzip, deflate', |
| 79 |
'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest', |
| 80 |
'HTTP_REFERER' => 'http://mobiledetect.net', |
| 81 |
'HTTP_PRAGMA' => 'no-cache', |
| 82 |
'HTTP_CACHE_CONTROL' => 'no-cache', |
| 83 |
'REMOTE_ADDR' => '11.22.33.44', |
| 84 |
'REQUEST_TIME' => '01-10-2012 07:57' |
| 85 |
)), |
| 86 |
array(array( |
| 87 |
'SERVER_SOFTWARE' => 'Rogue software', |
| 88 |
'REQUEST_METHOD' => 'GET', |
| 89 |
'REMOTE_ADDR' => '8.8.8.8', |
| 90 |
'REQUEST_TIME' => '07-10-2013 23:56', |
| 91 |
'HTTP_USER_AGENT' => "garbage/1.0" |
| 92 |
)), |
| 93 |
array(array( |
| 94 |
'SERVER_SOFTWARE' => 'Apache/1.3.17 (Linux) PHP/5.5.2', |
| 95 |
'REQUEST_METHOD' => 'HEAD', |
| 96 |
'HTTP_USER_AGENT' => 'Mozilla/5.0 (Linux; U; Android 1.5; en-us; ADR6200 Build/CUPCAKE) AppleWebKit/528.5+ (KHTML, like Gecko) Version/3.1.2 Mobile Safari/525.20.1', |
| 97 |
'REMOTE_ADDR' => '1.250.250.0', |
| 98 |
'REQUEST_TIME' => '06-12-2006 11:06' |
| 99 |
)), |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* @dataProvider headersProvider |
| 105 |
* @covers Mobile_Detect::getHttpHeader |
| 106 |
*/ |
| 107 |
public function testConstructorInjection(array $headers) |
| 108 |
{ |
| 109 |
$md = new Mobile_Detect($headers); |
| 110 |
|
| 111 |
foreach ($headers as $header => $value) { |
| 112 |
if (substr($header, 0, 5) !== 'HTTP_') { |
| 113 |
//make sure it wasn't set |
| 114 |
$this->assertNull($md->getHttpHeader($value)); |
| 115 |
} else { |
| 116 |
//make sure it's equal |
| 117 |
$this->assertEquals($value, $md->getHttpHeader($header)); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
//verify some of the headers work with the translated getter |
| 122 |
$this->assertNull($md->getHttpHeader('Remote-Addr')); |
| 123 |
$this->assertNull($md->getHttpHeader('Server-Software')); |
| 124 |
$this->assertEquals($headers['HTTP_USER_AGENT'], $md->getHttpHeader('User-Agent')); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* @dataProvider headersProvider |
| 129 |
* @covers Mobile_Detect::getHttpHeader |
| 130 |
*/ |
| 131 |
public function testInvalidHeader($headers) |
| 132 |
{ |
| 133 |
$md = new Mobile_Detect($headers); |
| 134 |
$this->assertNull($md->getHttpHeader('garbage_is_Garbage')); |
| 135 |
} |
| 136 |
|
| 137 |
public function userAgentProvider() |
| 138 |
{ |
| 139 |
return array( |
| 140 |
array(array( |
| 141 |
'HTTP_USER_AGENT' => 'blah' |
| 142 |
), 'blah'), |
| 143 |
array(array( |
| 144 |
'HTTP_USER_AGENT' => 'iphone', |
| 145 |
'HTTP_X_OPERAMINI_PHONE_UA' => 'some other stuff' |
| 146 |
), 'iphone some other stuff'), |
| 147 |
array(array( |
| 148 |
'HTTP_X_DEVICE_USER_AGENT' => 'hello world' |
| 149 |
), 'hello world'), |
| 150 |
array(array(), null) |
| 151 |
); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* @dataProvider userAgentProvider |
| 156 |
* @covers Mobile_Detect::setUserAgent |
| 157 |
* @covers Mobile_Detect::getUserAgent |
| 158 |
*/ |
| 159 |
public function testGetUserAgent($headers, $expectedUserAgent) |
| 160 |
{ |
| 161 |
$md = new Mobile_Detect($headers); |
| 162 |
$md->setUserAgent(); |
| 163 |
$this->assertSame($expectedUserAgent, $md->getUserAgent()); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Headers should be reset when you use setHttpHeaders. |
| 168 |
* @covers Mobile_Detect::setHttpHeaders |
| 169 |
* @issue #144 |
| 170 |
*/ |
| 171 |
public function testSetHttpHeaders() |
| 172 |
{ |
| 173 |
$header1 = array('HTTP_PINK_PONY' => 'I secretly love ponies >_>'); |
| 174 |
$md = new Mobile_Detect($header1); |
| 175 |
$this->assertSame($md->getHttpHeaders(), $header1); |
| 176 |
|
| 177 |
$header2 = array('HTTP_FIRE_BREATHING_DRAGON' => 'yeah!'); |
| 178 |
$md->setHttpHeaders($header2); |
| 179 |
$this->assertSame($md->getHttpHeaders(), $header2); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* @covers Mobile_Detect::setUserAgent |
| 184 |
* @covers Mobile_Detect::getUserAgent |
| 185 |
*/ |
| 186 |
public function testSetUserAgent() |
| 187 |
{ |
| 188 |
$md = new Mobile_Detect(array()); |
| 189 |
$md->setUserAgent('hello world'); |
| 190 |
$this->assertSame('hello world', $md->getUserAgent()); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* @covers Mobile_Detect::setDetectionType |
| 195 |
*/ |
| 196 |
public function testSetDetectionType() |
| 197 |
{ |
| 198 |
$md = new Mobile_Detect(array()); |
| 199 |
|
| 200 |
$md->setDetectionType('bskdfjhs'); |
| 201 |
$this->assertAttributeEquals( |
| 202 |
Mobile_Detect::DETECTION_TYPE_MOBILE, |
| 203 |
'detectionType', |
| 204 |
$md |
| 205 |
); |
| 206 |
|
| 207 |
$md->setDetectionType(); |
| 208 |
$this->assertAttributeEquals( |
| 209 |
Mobile_Detect::DETECTION_TYPE_MOBILE, |
| 210 |
'detectionType', |
| 211 |
$md |
| 212 |
); |
| 213 |
|
| 214 |
$md->setDetectionType(Mobile_Detect::DETECTION_TYPE_MOBILE); |
| 215 |
$this->assertAttributeEquals( |
| 216 |
Mobile_Detect::DETECTION_TYPE_MOBILE, |
| 217 |
'detectionType', |
| 218 |
$md |
| 219 |
); |
| 220 |
|
| 221 |
$md->setDetectionType(Mobile_Detect::DETECTION_TYPE_EXTENDED); |
| 222 |
$this->assertAttributeEquals( |
| 223 |
Mobile_Detect::DETECTION_TYPE_EXTENDED, |
| 224 |
'detectionType', |
| 225 |
$md |
| 226 |
); |
| 227 |
} |
| 228 |
|
| 229 |
//special headers that give 'quick' indication that a device is mobile |
| 230 |
public function quickHeadersData() |
| 231 |
{ |
| 232 |
return array( |
| 233 |
array(array( |
| 234 |
'HTTP_ACCEPT' => 'application/json; q=0.2, application/x-obml2d; q=0.8, image/gif; q=0.99, */*' |
| 235 |
)), |
| 236 |
array(array( |
| 237 |
'HTTP_ACCEPT' => 'text/*; q=0.1, application/vnd.rim.html' |
| 238 |
)), |
| 239 |
array(array( |
| 240 |
'HTTP_ACCEPT' => 'text/vnd.wap.wml', |
| 241 |
)), |
| 242 |
array(array( |
| 243 |
'HTTP_ACCEPT' => 'application/vnd.wap.xhtml+xml', |
| 244 |
)), |
| 245 |
array(array( |
| 246 |
'HTTP_X_WAP_PROFILE' => 'hello', |
| 247 |
)), |
| 248 |
array(array( |
| 249 |
'HTTP_X_WAP_CLIENTID' => '' |
| 250 |
)), |
| 251 |
array(array( |
| 252 |
'HTTP_WAP_CONNECTION' => '' |
| 253 |
)), |
| 254 |
array(array( |
| 255 |
'HTTP_PROFILE' => '' |
| 256 |
)), |
| 257 |
array(array( |
| 258 |
'HTTP_X_OPERAMINI_PHONE_UA' => '' |
| 259 |
)), |
| 260 |
array(array( |
| 261 |
'HTTP_X_NOKIA_GATEWAY_ID' => '' |
| 262 |
)), |
| 263 |
array(array( |
| 264 |
'HTTP_X_ORANGE_ID' => '' |
| 265 |
)), |
| 266 |
array(array( |
| 267 |
'HTTP_X_VODAFONE_3GPDPCONTEXT' => '' |
| 268 |
)), |
| 269 |
array(array( |
| 270 |
'HTTP_X_HUAWEI_USERID' => '' |
| 271 |
)), |
| 272 |
array(array( |
| 273 |
'HTTP_UA_OS' => '' |
| 274 |
)), |
| 275 |
array(array( |
| 276 |
'HTTP_X_MOBILE_GATEWAY' => '' |
| 277 |
)), |
| 278 |
array(array( |
| 279 |
'HTTP_X_ATT_DEVICEID' => '' |
| 280 |
)), |
| 281 |
array(array( |
| 282 |
'HTTP_UA_CPU' => 'ARM' |
| 283 |
)) |
| 284 |
); |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* @dataProvider quickHeadersData |
| 289 |
* @covers Mobile_Detect::checkHttpHeadersForMobile |
| 290 |
*/ |
| 291 |
public function testQuickHeaders($headers) |
| 292 |
{ |
| 293 |
$md = new Mobile_Detect($headers); |
| 294 |
$this->assertTrue($md->checkHttpHeadersForMobile()); |
| 295 |
} |
| 296 |
|
| 297 |
// Headers that are not mobile. |
| 298 |
public function quickNonMobileHeadersData() |
| 299 |
{ |
| 300 |
|
| 301 |
return array( |
| 302 |
array(array( |
| 303 |
'HTTP_UA_CPU' => 'AMD64' |
| 304 |
)), |
| 305 |
array(array( |
| 306 |
'HTTP_UA_CPU' => 'X86' |
| 307 |
)), |
| 308 |
array(array( |
| 309 |
'HTTP_ACCEPT' => 'text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01' |
| 310 |
)), |
| 311 |
array(array( |
| 312 |
'HTTP_REQUEST_METHOD' => 'DELETE' |
| 313 |
)), |
| 314 |
array(array( |
| 315 |
'HTTP_VIA' => '1.1 ws-proxy.stuff.co.il C0A800FA' |
| 316 |
)), |
| 317 |
); |
| 318 |
|
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* @dataProvider quickNonMobileHeadersData |
| 323 |
* @covers Mobile_Detect::checkHttpHeadersForMobile |
| 324 |
*/ |
| 325 |
public function testNonMobileQuickHeaders($headers) |
| 326 |
{ |
| 327 |
$md = new Mobile_Detect($headers); |
| 328 |
$this->assertFalse($md->checkHttpHeadersForMobile()); |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* @expectedException BadMethodCallException |
| 333 |
* @coversNothing |
| 334 |
*/ |
| 335 |
public function testBadMethodCall() |
| 336 |
{ |
| 337 |
$md = new Mobile_Detect(array()); |
| 338 |
$md->badmethodthatdoesntexistatall(); |
| 339 |
} |
| 340 |
|
| 341 |
public function versionDataProvider() |
| 342 |
{ |
| 343 |
return array( |
| 344 |
array( |
| 345 |
'Mozilla/5.0 (Linux; Android 4.0.4; ARCHOS 80G9 Build/IMM76D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19', |
| 346 |
'Android', |
| 347 |
'4.0.4', |
| 348 |
4.04 |
| 349 |
), |
| 350 |
array( |
| 351 |
'Mozilla/5.0 (Linux; Android 4.0.4; ARCHOS 80G9 Build/IMM76D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19', |
| 352 |
'Webkit', |
| 353 |
'535.19', |
| 354 |
535.19 |
| 355 |
), |
| 356 |
array( |
| 357 |
'Mozilla/5.0 (Linux; Android 4.0.4; ARCHOS 80G9 Build/IMM76D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19', |
| 358 |
'Chrome', |
| 359 |
'18.0.1025.166', |
| 360 |
18.01025166 |
| 361 |
), |
| 362 |
array( |
| 363 |
'Mozilla/5.0 (BlackBerry; U; BlackBerry 9700; en-US) AppleWebKit/534.8 (KHTML, like Gecko) Version/6.0.0.448 Mobile Safari/534.8', |
| 364 |
'BlackBerry', |
| 365 |
'6.0.0.448', |
| 366 |
6.00448 |
| 367 |
), |
| 368 |
array( |
| 369 |
'Mozilla/5.0 (BlackBerry; U; BlackBerry 9700; en-US) AppleWebKit/534.8 (KHTML, like Gecko) Version/6.0.0.448 Mobile Safari/534.8', |
| 370 |
'Webkit', |
| 371 |
'534.8', |
| 372 |
534.8 |
| 373 |
), |
| 374 |
array( |
| 375 |
'Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en-GB) AppleWebKit/534.8+ (KHTML, like Gecko) Version/6.0.0.546 Mobile Safari/534.8+', |
| 376 |
'BlackBerry', |
| 377 |
'6.0.0.546', |
| 378 |
6.00546 |
| 379 |
) |
| 380 |
); |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* @dataProvider versionDataProvider |
| 385 |
* @covers Mobile_Detect::version |
| 386 |
*/ |
| 387 |
public function testVersionExtraction($userAgent, $property, $stringVersion, $floatVersion) |
| 388 |
{ |
| 389 |
$md = new Mobile_Detect(array('HTTP_USER_AGENT' => $userAgent)); |
| 390 |
$prop = $md->version($property); |
| 391 |
|
| 392 |
$this->assertSame($stringVersion, $prop); |
| 393 |
|
| 394 |
$prop = $md->version($property, 'float'); |
| 395 |
$this->assertSame($floatVersion, $prop); |
| 396 |
|
| 397 |
//assert that garbage data is always === false |
| 398 |
$prop = $md->version('garbage input is always garbage'); |
| 399 |
$this->assertFalse($prop); |
| 400 |
} |
| 401 |
|
| 402 |
/** |
| 403 |
* @covers Mobile_Detect::getMobileDetectionRules |
| 404 |
*/ |
| 405 |
public function testRules() |
| 406 |
{ |
| 407 |
$md = new Mobile_Detect; |
| 408 |
$count = array_sum(array( |
| 409 |
count(Mobile_Detect::getPhoneDevices()), |
| 410 |
count(Mobile_Detect::getTabletDevices()), |
| 411 |
count(Mobile_Detect::getOperatingSystems()), |
| 412 |
count(Mobile_Detect::getBrowsers()) |
| 413 |
)); |
| 414 |
$rules = $md->getRules(); |
| 415 |
$this->assertEquals($count, count($rules)); |
| 416 |
} |
| 417 |
|
| 418 |
/** |
| 419 |
* @covers Mobile_Detect::getMobileDetectionRulesExtended |
| 420 |
*/ |
| 421 |
public function testRulesExtended() |
| 422 |
{ |
| 423 |
$md = new Mobile_Detect; |
| 424 |
$count = array_sum(array( |
| 425 |
count(Mobile_Detect::getPhoneDevices()), |
| 426 |
count(Mobile_Detect::getTabletDevices()), |
| 427 |
count(Mobile_Detect::getOperatingSystems()), |
| 428 |
count(Mobile_Detect::getBrowsers()), |
| 429 |
count(Mobile_Detect::getUtilities()) |
| 430 |
)); |
| 431 |
$md->setDetectionType(Mobile_Detect::DETECTION_TYPE_EXTENDED); |
| 432 |
$rules = $md->getRules(); |
| 433 |
$this->assertEquals($count, count($rules)); |
| 434 |
} |
| 435 |
|
| 436 |
/** |
| 437 |
* @covers Mobile_Detect::getScriptVersion |
| 438 |
*/ |
| 439 |
public function testScriptVersion() |
| 440 |
{ |
| 441 |
$v = Mobile_Detect::getScriptVersion(); |
| 442 |
$formatCheck = (bool)preg_match('/^[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9])?$/', $v); |
| 443 |
|
| 444 |
$this->assertTrue($formatCheck, "Fails the semantic version test. The version " . var_export($v, true) |
| 445 |
. ' does not match X.Y.Z pattern'); |
| 446 |
} |
| 447 |
|
| 448 |
public function crazyVersionNumbers() |
| 449 |
{ |
| 450 |
return array( |
| 451 |
array('2.5.6', 2.56), |
| 452 |
array('12142.2142.412521.24.152', 12142.214241252124152), |
| 453 |
array('6_3', 6.3), |
| 454 |
array('4_7 /7 7 12_9', 4.777129), |
| 455 |
array('49', 49.0), |
| 456 |
array('2.6.x', 2.6), |
| 457 |
array('45.6.1.x.12', 45.61) |
| 458 |
); |
| 459 |
} |
| 460 |
|
| 461 |
/** |
| 462 |
* @dataProvider crazyVersionNumbers |
| 463 |
* @covers Mobile_Detect::prepareVersionNo |
| 464 |
*/ |
| 465 |
public function testPrepareVersionNo($raw, $expected) |
| 466 |
{ |
| 467 |
$md = new Mobile_Detect; |
| 468 |
$actual = $md->prepareVersionNo($raw); |
| 469 |
$this->assertSame($expected, $actual, "We expected " . var_export($raw, true) . " to convert to " |
| 470 |
. var_export($expected, true) . ', but got ' . var_export($actual, true) . ' instead'); |
| 471 |
} |
| 472 |
} |
| 473 |
|