File size: 7,018 Bytes
c938f2d | 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 196 197 198 199 200 201 202 203 204 205 206 207 208 | <?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\Config;
use Piwik\Date;
use Piwik\Piwik;
use Piwik\Plugin\Manager;
use Piwik\Plugins\API\API as MetadataApi;
use Piwik\Plugins\BotTracking\API;
use Piwik\Plugins\BotTracking\BotDetector;
use Piwik\Plugins\BotTracking\Dao\BotRequestsDao;
use Piwik\Plugins\BotTracking\Metrics;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Tracker\Action;
use Piwik\Tracker\TableLogAction;
use Piwik\ViewDataTable\Factory as ViewDataTableFactory;
/**
* @group BotTracking
* @group Plugins
*/
class RealTimeApiTest extends IntegrationTestCase
{
/**
* @var int
*/
private $idSite;
public function setUp(): void
{
parent::setUp();
Date::$now = (int) strtotime('2026-01-01 12:00:00');
$this->idSite = Fixture::createWebsite('2026-01-01 00:00:00');
}
public function tearDown(): void
{
Date::$now = null;
parent::tearDown();
}
public function testRealTimeApiRejectsLookbacksLongerThanTwelveHours(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('lastMinutes only accepts values between 1 and 720');
API::getInstance()->getAIChatbotsRealTime($this->idSite, 721);
}
public function testRealTimeApiRejectsNonPositiveLookbacks(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('lastMinutes only accepts values between 1 and 720');
API::getInstance()->getTopPageUrlsRealTime($this->idSite, 0);
}
public function testRealTimeApiReturnsDecoratedChatbotLabels(): void
{
$dao = new BotRequestsDao();
$dao->insert([
'idsite' => $this->idSite,
'server_time' => '2026-01-01 11:45:00',
'idaction_url' => $this->createAction('example.com/realtime/a', Action::TYPE_PAGE_URL),
'bot_name' => 'ChatGPT-User',
'bot_type' => BotDetector::BOT_TYPE_AI_CHATBOT,
'http_status_code' => 200,
]);
$table = API::getInstance()->getAIChatbotsRealTime($this->idSite, '30');
$rows = $table->getRows();
self::assertCount(1, $rows);
self::assertSame('ChatGPT', $rows[0]->getColumn('label'));
self::assertSame(1, $rows[0]->getColumn(Metrics::COLUMN_REQUESTS));
}
public function testRealTimeReportsAreHiddenFromReportMetadata(): void
{
$metadata = MetadataApi::getInstance()->getReportMetadata($this->idSite);
$uniqueIds = array_column($metadata, 'uniqueId');
self::assertNotContains('BotTracking_getAIChatbotsRealTime', $uniqueIds);
self::assertNotContains('BotTracking_getTopPageUrlsRealTime', $uniqueIds);
}
public function testRealTimeReportFooterShowsConfiguredLimit(): void
{
$config = Config::getInstance();
$previousConfig = $config->General['live_ai_chatbots_top_page_urls_maximum_rows'] ?? null;
$hadPreviousConfig = array_key_exists('live_ai_chatbots_top_page_urls_maximum_rows', $config->General);
$config->General['live_ai_chatbots_top_page_urls_maximum_rows'] = 37;
try {
Manager::getInstance()->loadPluginTranslations();
$_GET['idSite'] = (string) $this->idSite;
$_GET['date'] = 'today';
$_GET['period'] = 'day';
$view = ViewDataTableFactory::build(
$defaultType = null,
'BotTracking.getTopPageUrlsRealTime',
$controllerAction = 'BotTracking.getTopPageUrlsRealTime',
$forceDefault = false,
$loadViewDataTableParametersForUser = false
);
self::assertStringContainsString('37', $view->config->show_footer_message);
self::assertNotSame(
'BotTracking_AIChatbotsRealTimeReportLimitFooter',
$view->config->show_footer_message
);
} finally {
unset($_GET['idSite'], $_GET['date'], $_GET['period']);
if ($hadPreviousConfig) {
$config->General['live_ai_chatbots_top_page_urls_maximum_rows'] = $previousConfig;
} else {
unset($config->General['live_ai_chatbots_top_page_urls_maximum_rows']);
}
}
}
public function testHiddenRealTimeReportsAreAddedToGlossaryPageItems(): void
{
$glossaryItems = [
'metrics' => [
'title' => Piwik::translate('General_Metrics'),
'entries' => [],
],
'reports' => [
'title' => Piwik::translate('General_Reports'),
'entries' => [],
],
];
Piwik::postEvent('API.addGlossaryItems', array(&$glossaryItems));
$reportNames = array_column($glossaryItems['reports']['entries'], 'name');
$category = Piwik::translate('General_AIAssistants');
self::assertContains(
sprintf('%s (%s)', Piwik::translate('BotTracking_AIChatbotsLast30MinutesTitle'), $category),
$reportNames
);
self::assertContains(
sprintf('%s (%s)', Piwik::translate('BotTracking_AIChatbotsLast8HoursTitle'), $category),
$reportNames
);
self::assertContains(
sprintf('%s (%s)', Piwik::translate('BotTracking_TopPageUrlsLast30MinutesTitle'), $category),
$reportNames
);
self::assertContains(
sprintf('%s (%s)', Piwik::translate('BotTracking_TopPageUrlsLast8HoursTitle'), $category),
$reportNames
);
}
public function testRequestsMetricAppearsInGlossaryMetrics(): void
{
$metrics = MetadataApi::getInstance()->getGlossaryMetrics($this->idSite);
$metric = $this->findMetric($metrics, Metrics::COLUMN_REQUESTS);
self::assertNotNull($metric);
self::assertSame(Piwik::translate('BotTracking_ColumnRequests'), $metric['name']);
self::assertSame(
Piwik::translate('BotTracking_ColumnRequestsDocumentation'),
$metric['documentation']
);
}
private function createAction(string $name, int $type): int
{
$actionIds = TableLogAction::loadIdsAction([[$name, $type]]);
return (int) reset($actionIds);
}
/**
* @param array<int, array<string, mixed>> $metrics
* @return array<string, mixed>|null
*/
private function findMetric(array $metrics, string $metricId): ?array
{
foreach ($metrics as $metric) {
if (($metric['id'] ?? null) === $metricId) {
return $metric;
}
}
return null;
}
}
|