Skip to content

Commit

Permalink
fix Android tablet detection in VERSION_TRUNCATION_MAJOR mode
Browse files Browse the repository at this point in the history
  • Loading branch information
blinkseb committed May 29, 2023
1 parent cc4eb16 commit 0eb5389
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
6 changes: 3 additions & 3 deletions DeviceDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -980,10 +980,10 @@ protected function parseDevice(): void
* Devices running Android 3.X are tablets. Device type of Android 2.X and 4.X+ are unknown
*/
if (null === $this->device && 'Android' === $osName && '' !== $osVersion) {
if (-1 === \version_compare($osVersion, '2.0')) {
if (-1 === \version_compare($osVersion, '2')) {
$this->device = AbstractDeviceParser::DEVICE_TYPE_SMARTPHONE;
} elseif (\version_compare($osVersion, '3.0') >= 0
&& -1 === \version_compare($osVersion, '4.0')
} elseif (\version_compare($osVersion, '3') >= 0
&& -1 === \version_compare($osVersion, '4')
) {
$this->device = AbstractDeviceParser::DEVICE_TYPE_TABLET;
}
Expand Down
59 changes: 58 additions & 1 deletion Tests/DeviceDetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public function testParse(array $fixtureData): void
}

$errorMessage = \sprintf(
"UserAgent: %s\nHeaders: %s",
"UserAgent: %s\nHeaders: %s\nVersion truncation: none",
$ua,
\print_r($fixtureData['headers'] ?? null, true)
);
Expand All @@ -240,6 +240,63 @@ public function testParse(array $fixtureData): void
$this->assertEquals($fixtureData, $uaInfo, $errorMessage);
}

private static function truncateVersion(?string $versionString): ?string
{
if (is_null($versionString)) {
return null;
}

if (\substr_count($versionString, '.') > 0) {
$versionParts = \explode('.', $versionString);
$versionParts = \array_slice($versionParts, 0, 1);
$versionString = \implode('.', $versionParts);
}

return \trim($versionString, ' .');
}

/**
* @dataProvider getFixtures
*/
public function testParseWithVersionTruncationMajor(array $fixtureData): void
{
$ua = $fixtureData['user_agent'];
$clientHints = !empty($fixtureData['headers']) ? ClientHints::factory($fixtureData['headers']) : null;

AbstractDeviceParser::setVersionTruncation(AbstractDeviceParser::VERSION_TRUNCATION_MAJOR);

try {
$uaInfo = DeviceDetector::getInfoFromUserAgent($ua, $clientHints);
} catch (\Exception $exception) {
throw new \Exception(
\sprintf('Error: %s from useragent %s', $exception->getMessage(), $ua),
$exception->getCode(),
$exception
);
}

$errorMessage = \sprintf(
"UserAgent: %s\nHeaders: %s\nVersion truncation: major",
$ua,
\print_r($fixtureData['headers'] ?? null, true)
);

unset($fixtureData['headers']); // ignore headers in result

// truncate versions
if (array_key_exists('version', $fixtureData['os'] ?? [])) {
$fixtureData['os']['version'] = self::truncateVersion($fixtureData['os']['version']);
}

if (array_key_exists('version', $fixtureData['client'] ?? [])) {
$fixtureData['client']['version'] = self::truncateVersion($fixtureData['client']['version']);
}

$this->assertEquals($fixtureData, $uaInfo, $errorMessage);

AbstractDeviceParser::setVersionTruncation(AbstractDeviceParser::VERSION_TRUNCATION_NONE);
}

public function getFixtures(): array
{
$fixtures = [];
Expand Down

0 comments on commit 0eb5389

Please sign in to comment.