File size: 7,648 Bytes
a3a72f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\DeviceDetectorCache\tests\Unit;

use PHPUnit\Framework\TestCase;
use Piwik\DeviceDetector\DeviceDetectorFactory;
use Piwik\Filesystem;
use Piwik\Plugins\DeviceDetectorCache\CachedEntry;
use Piwik\Plugins\DeviceDetectorCache\Factory;
use Piwik\Version;

/**
 * @group DeviceDetectorCache
 * @group DeviceDetectorCacheFactoryTest
 * @group Plugins
 */
class DeviceDetectorCacheFactoryTest extends TestCase
{
    public function setUp(): void
    {
        DeviceDetectorFactory::clearInstancesCache();
        CachedEntry::setCacheDir(PIWIK_DOCUMENT_ROOT . '/tmp/devicecachetests/');
    }

    public function tearDown(): void
    {
        CachedEntry::clearCacheDir();
        Filesystem::unlinkRecursive(CachedEntry::getCacheDir(), true);
    }

    public function testGetInstanceFromCache()
    {
        $userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10; rv:33.0) Gecko/20100101 Firefox/33.0";
        $expected  = [
            'bot'    => null,
            'brand'  => 'Cooper',
            'client' => [
                'type' => 'browser',
                'name' => 'Microsoft Edge',
            ],
            'device' => 1,
            'model'  => 'iPhone',
            'os'     => [
                'name' => 'Linux',
            ],
        ];

        self::writeFakeFile($expected, $userAgent);

        $factory         = new Factory();
        $deviceDetection = $factory->makeInstance($userAgent, []);
        $this->assertInstanceOf(CachedEntry::class, $deviceDetection);
        $this->assertEquals(null, $deviceDetection->getBot());
        $this->assertEquals('Cooper', $deviceDetection->getBrandName());
        $this->assertEquals($expected['client'], $deviceDetection->getClient());
        $this->assertEquals(1, $deviceDetection->getDevice());
        $this->assertEquals('iPhone', $deviceDetection->getModel());
        $this->assertEquals($expected['os'], $deviceDetection->getOs());
    }

    public function testGetInstanceFromCacheWithClientHints()
    {
        $userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.66";
        $clientHints = [
            'HTTP_SEC_CH_UA_PLATFORM' => '"Windows"',
            'HTTP_SEC_CH_UA' => '" Not A;Brand";v="99", "Chromium";v="95", "Microsoft Edge";v="95"',
            'HTTP_SEC_CH_UA_MOBILE' => "?0",
            'HTTP_SEC_CH_UA_FULL_VERSION' => '"98.0.0.1"',
            'HTTP_SEC_CH_UA_PLATFORM_VERSION' => '"14.0.0"',
            'HTTP_SEC_CH_UA_ARCH' => "x86",
            'HTTP_SEC_CH_UA_BITNESS' => "64",
            'HTTP_SEC_CH_UA_MODEL' => ""
        ];
        $expected  = [
            'bot'    => null,
            'brand'  => '',
            'client' => [
                'type' => 'browser',
                'name' => 'Microsoft Edge',
                'version' => '97.0.1072.66'
            ],
            'device' => 1,
            'model'  => '',
            'os'     => [
                'name' => 'Windows',
                'version' => '10',
            ],
        ];

        self::writeFakeFile($expected, $userAgent);

        $factory = new Factory();

        // not using client hints will return the cached entry
        $deviceDetection = $factory->makeInstance($userAgent, []);
        $this->assertInstanceOf(CachedEntry::class, $deviceDetection);
        $this->assertEquals(null, $deviceDetection->getBot());
        $this->assertEquals($expected['brand'], $deviceDetection->getBrandName());
        $this->assertEquals($expected['client'], $deviceDetection->getClient());
        $this->assertEquals(1, $deviceDetection->getDevice());
        $this->assertEquals($expected['model'], $deviceDetection->getModel());
        $this->assertEquals($expected['os'], $deviceDetection->getOs());

        // using client hints will overwrite some value if needed
        $deviceDetection = $factory->makeInstance($userAgent, $clientHints);
        $expected['os'] = [
            'name' => 'Windows',
            'version' => '11',
            'short_name' => 'WIN',
            'platform' => 'x64',
            'family' => 'Windows',
        ];
        $expected['client'] = [
            'type' => 'browser',
            'name' => 'Microsoft Edge',
            'version' => '98.0',
            'short_name' => 'PS',
            // empty because cached result doesn't contain it and engine won't be detected through client hints only
            'engine' => '',
            'engine_version' => '',
            'family' => 'Internet Explorer',
        ];

        if (version_compare('4.11.0', Version::VERSION, '>=')) {
            // On older versions client hints were not supported and thus the result differed.
            $expected['os'] = [
                'name' => 'Windows',
                'version' => '10',
            ];
            $expected['client'] = [
                'type' => 'browser',
                'name' => 'Microsoft Edge',
                'version' => '97.0.1072.66'
            ];
        }

        $this->assertInstanceOf(CachedEntry::class, $deviceDetection);
        $this->assertEquals(null, $deviceDetection->getBot());
        $this->assertEquals($expected['brand'], $deviceDetection->getBrandName());
        $this->assertEquals($expected['client'], $deviceDetection->getClient());
        $this->assertEquals(1, $deviceDetection->getDevice());
        $this->assertEquals($expected['model'], $deviceDetection->getModel());
        $this->assertEquals($expected['os'], $deviceDetection->getOs());
    }

    public function testGetInstanceFromDeviceDetector()
    {
        $userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10; rv:33.0) Gecko/20100101 Firefox/33.0";
        $expected  = [
            'client' => [
                'type'           => 'browser',
                'name'           => 'Firefox',
                'short_name'     => 'FF',
                'version'        => '33.0',
                'engine'         => 'Gecko',
                'engine_version' => '33.0',
            ],
            'os'     => [
                'name'       => 'Mac',
                'short_name' => 'MAC',
                'version'    => '10.10',
                'platform'   => '',
            ],
        ];

        $factory         = new Factory();
        $deviceDetection = $factory->makeInstance($userAgent, []);
        $this->assertInstanceOf("\DeviceDetector\DeviceDetector", $deviceDetection);
        $this->assertEquals(null, $deviceDetection->getBot());
        $this->assertEquals('AP', $deviceDetection->getBrand());
        $client = $deviceDetection->getClient();
        unset($client['family']); // newer version of DD might return the family. We ignore it here, to allow tests with older Matomo versions
        $this->assertEquals($expected['client'], $client);
        $this->assertEquals(0, $deviceDetection->getDevice());
        $this->assertEquals('', $deviceDetection->getModel());
        $os = $deviceDetection->getOs();
        unset($os['family']); // newer version of DD might return the family. We ignore it here, to allow tests with older Matomo versions
        $this->assertEquals($expected['os'], $os);
    }

    public static function writeFakeFile($expected, $userAgent)
    {
        $filePath = CachedEntry::getCachePath($userAgent, true);
        $content  = "<?php return " . var_export($expected, true) . ";";
        file_put_contents($filePath, $content, LOCK_EX);
    }
}