File: NoRecentRequestsMessageTest.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 (115 lines) | stat: -rw-r--r-- 4,965 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
<?php

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

declare(strict_types=1);

namespace Piwik\Plugins\BotTracking\tests\Integration;

use Piwik\Date;
use Piwik\Plugins\BotTracking\BotDetector;
use Piwik\Plugins\BotTracking\Dao\BotRequestsDao;
use Piwik\Plugins\BotTracking\NoRecentRequestsMessage;
use Piwik\Site;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

/**
 * @group BotTracking
 * @group BotTrackingNoRecentRequestsMessage
 * @group Plugins
 */
class NoRecentRequestsMessageTest extends IntegrationTestCase
{
    /**
     * @var int
     */
    private $idSite;

    public function setUp(): void
    {
        parent::setUp();

        $this->idSite = Fixture::createWebsite(
            '2024-01-01 00:00:00',
            0,
            'Test Site',
            'https://example.com',
            false,
            null,
            null,
            'America/Los_Angeles'
        );
    }

    /**
     * @dataProvider getShouldShowTestData
     */
    public function testShouldShowNoRecentRequestsMessage(
        string $testUtcNow,
        string $testPeriod,
        string $testDate,
        string $requestServerTime,
        string $siteTimezone,
        bool $expectedShowShould
    ): void {
        Date::$now = strtotime($testUtcNow);
        Site::setSiteFromArray($this->idSite, ['timezone' => $siteTimezone]);

        $dao = new BotRequestsDao();
        $dao->insert([
            'idsite'      => $this->idSite,
            'server_time' => $requestServerTime,
            'bot_name'    => 'ChatGPT-User',
            'bot_type'    => BotDetector::BOT_TYPE_AI_CHATBOT,
        ]);

        $shouldShow = NoRecentRequestsMessage::shouldShow($this->idSite, $testPeriod, $testDate);

        self::assertSame($expectedShowShould, $shouldShow);
    }

    public function testShouldShowWhenNoRequestsExist(): void
    {
        Date::$now = strtotime('2024-01-01 12:00:00');
        Site::setSiteFromArray($this->idSite, ['timezone' => 'UTC']);

        $shouldShow = NoRecentRequestsMessage::shouldShow($this->idSite, 'day', '2024-01-01');

        self::assertTrue($shouldShow);
    }

    /**
     * @return iterable<string, array{string, string, string, string, string, bool}>
     */
    public function getShouldShowTestData(): iterable
    {
        yield 'today + request yesterday' => ['2024-01-01 12:00:00', 'day', '2024-01-01', '2023-12-31 12:00:00', 'UTC', false];
        yield 'today + request within last 7 days' => ['2024-01-07 12:00:00', 'day', '2024-01-08', '2024-01-01 12:00:00', 'UTC', false];
        yield 'today + request 1 week ago' => ['2024-01-08 12:00:00', 'day', '2024-01-08', '2024-01-01 12:00:00', 'UTC', true];
        yield 'today + request 1 month ago' => ['2024-01-01 12:00:00', 'day', '2024-01-01', '2023-12-01 12:00:00', 'UTC', true];

        yield 'old day + request today' => ['2024-01-01 12:00:00', 'day', '2023-01-01', '2024-01-01 00:00:00', 'UTC', false];
        yield 'old day + old request' => ['2024-01-01 12:00:00', 'day', '2023-01-01', '2023-01-01 00:00:00', 'UTC', false];

        yield 'last month within reach + request today' => ['2024-01-01 12:00:00', 'month', '2023-12-01', '2024-01-01 00:00:00', 'UTC', false];
        yield 'last month within reach + old request' => ['2024-01-01 12:00:00', 'month', '2023-12-01', '2023-01-01 00:00:00', 'UTC', true];
        yield 'last month out of reach + request today' => ['2024-01-21 12:00:00', 'month', '2023-12-01', '2024-01-21 00:00:00', 'UTC', false];
        yield 'last month out of reach + old request' => ['2024-01-21 12:00:00', 'month', '2023-12-01', '2023-01-01 00:00:00', 'UTC', false];

        yield 'range within reach + request today' => ['2024-01-01 12:00:00', 'range', '2023-12-01,2023-12-30', '2024-01-01 00:00:00', 'UTC', false];
        yield 'range within reach + old request' => ['2024-01-01 12:00:00', 'range', '2023-12-01,2023-12-30', '2023-01-01 00:00:00', 'UTC', true];
        yield 'range out of reach + request today' => ['2024-01-21 12:00:00', 'range', '2023-12-01,2023-12-30', '2024-01-21 00:00:00', 'UTC', false];
        yield 'range out of reach + old request' => ['2024-01-21 12:00:00', 'range', '2023-12-01,2023-12-30', '2023-01-01 00:00:00', 'UTC', false];

        yield 'site timezone positive UTC + recent request' => ['2023-12-31 16:00:00', 'day', '2024-01-01', '2023-12-25 16:00:00', 'UTC+8', false];
        yield 'site timezone positive UTC + old request' => ['2023-12-31 16:00:00', 'day', '2024-01-01', '2023-12-25 15:59:59', 'UTC+8', true];
        yield 'site timezone negative UTC + recent request' => ['2024-01-01 08:00:00', 'day', '2024-01-01', '2023-12-26 08:00:00', 'UTC-8', false];
        yield 'site timezone negative UTC + old request' => ['2024-01-01 08:00:00', 'day', '2024-01-01', '2023-12-26 07:59:59', 'UTC-8', true];
    }
}