File: AIAssistantTest.php

package info (click to toggle)
matomo 5.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 95,068 kB
  • sloc: php: 289,425; xml: 127,249; javascript: 112,130; python: 202; sh: 178; makefile: 20; sql: 10
file content (120 lines) | stat: -rw-r--r-- 3,379 bytes parent folder | download
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
<?php

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

namespace Piwik\Plugins\Referrers\tests\Unit;

use Piwik\Plugins\Referrers\AIAssistant;

/**
 * @group AIAssistant
 * @group Plugins
 */
class AIAssistantTest extends \PHPUnit\Framework\TestCase
{
    public static function setUpBeforeClass(): void
    {
        // inject definitions to avoid database usage
        $yml = file_get_contents(PIWIK_PATH_TEST_TO_ROOT . AIAssistant::DEFINITION_FILE);
        AIAssistant::getInstance()->loadYmlData($yml);
        parent::setUpBeforeClass();
    }

    public function isAIAssistantUrlTestData(): iterable
    {
        yield 'chatgpt.com with correct assumed assistant name' => [
            'https://chatgpt.com',
            'ChatGPT',
            true,
        ];

        yield 'chatgpt.com with incorrect assumed assistant name' => [
            'https://chatgpt.com',
            'Copilot',
            false,
        ];

        yield 'chatgpt.com without assumed assistant name' => [
            'https://chatgpt.com',
            null,
            true,
        ];

        yield 'copilot.microsoft.com with correct assumed assistant name' => [
            'https://copilot.microsoft.com',
            'Copilot',
            true,
        ];
    }

    /**
     * @dataProvider isAIAssistantUrlTestData
     */
    public function testIsAIAssistantUrl($url, $assumedSocial, $expected)
    {
        $this->assertEquals($expected, AIAssistant::getInstance()->isAIAssistantUrl($url, $assumedSocial));
    }

    public function getAIAssistantFromDomainTestData(): iterable
    {
        yield 'ChatGPT url is correctly detected' => [
            'https://chatgpt.com',
            'ChatGPT',
        ];

        yield 'Grok url with path is correctly detected' => [
            'https://x.com/i/grok',
            'Grok',
        ];

        yield 'ChatGPT url with path is correctly detected' => [
            'http://chatgpt.com/i/16564-4345-5sdff-333',
            'ChatGPT',
        ];

        yield 'ChatGPT subdomain url is correctly detected' => [
            'https://custom.chatgpt.com/i/16564-4345-5sdff-333',
            'ChatGPT',
        ];

        yield 'ChatGPT look-a-url is detected as Unknown' => [
            'https://chatgpt.com.custom.org/i/16564-4345-5sdff-333',
            \Piwik\Piwik::translate('General_Unknown'),
        ];

        yield 'Unknown url is detected as Unknown' => [
            'https://xwayn.com',
            \Piwik\Piwik::translate('General_Unknown'),
        ];
    }

    /**
     * @dataProvider getAIAssistantFromDomainTestData
     */
    public function testGetSocialNetworkFromDomain($url, $expected)
    {
        $this->assertEquals($expected, AIAssistant::getInstance()->getAIAssistantFromDomain($url));
    }

    public function getLogoFromUrlTestData()
    {
        return array(
            array('https://chatgpt.com', 'chatgpt.com.png'),
            array('www.chatgpt.com', 'chatgpt.com.png',),
            array('grok.com', 'grok.com.png',),
        );
    }

    /**
     * @dataProvider getLogoFromUrlTestData
     */
    public function testGetLogoFromUrl($url, $expected)
    {
        self::assertStringContainsString($expected, AIAssistant::getInstance()->getLogoFromUrl($url));
    }
}