Skip to content

Commit 716d325

Browse files
Adds test
1 parent 19b180b commit 716d325

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

API.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ public function getTreemapData(
5151
$availableHeight = false,
5252
$show_evolution_values = false
5353
) {
54-
if (!Request::isCurrentApiRequestTheRootApiRequest()) {
54+
if (!Request::isCurrentApiRequestTheRootApiRequest() && !defined('PIWIK_TEST_MODE')) {
5555
return [];
5656
}
5757
list($module, $method) = explode('.', $apiMethod);
5858
$disAllowedApiActions = ['getBulkRequest'];
5959
// Block if API action does not start with get
60-
if (!$method || in_array(strtolower($method), $disAllowedApiActions) || stripos($method, 'get') !== 0) {
60+
if (!$method || in_array($method, $disAllowedApiActions) || stripos($method, 'get') !== 0) {
6161
throw new BadRequestException(Piwik::translate('TreemapVisualization_InvalidApiMethodException'));
6262
}
6363

tests/Integration/ApiTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/**
4+
* Matomo - free/libre analytics platform
5+
*
6+
* @link https://matomo.org
7+
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8+
*/
9+
10+
namespace Piwik\Plugins\TreemapVisualization\tests\Integration;
11+
12+
use Piwik\Http\BadRequestException;
13+
use Piwik\Plugins\TreemapVisualization\API;
14+
use Piwik\Tests\Framework\Fixture;
15+
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
16+
17+
/**
18+
* @group TreemapVisualization
19+
* @group ApiTest
20+
* @group Plugins
21+
*/
22+
class ApiTest extends IntegrationTestCase
23+
{
24+
private $api;
25+
26+
public function setUp(): void
27+
{
28+
parent::setUp();
29+
30+
Fixture::createSuperUser();
31+
$this->api = API::getInstance();
32+
}
33+
34+
/**
35+
* @dataProvider validMethod
36+
*/
37+
public function testGetTreemapDataAllowsValidApiMethod($method): void
38+
{
39+
$result = $this->api->getTreemapData(
40+
$method,
41+
'nb_visits',
42+
'day',
43+
'2025-02-03'
44+
);
45+
46+
$this->assertIsArray($result);
47+
}
48+
49+
/**
50+
* @dataProvider inValidMethod
51+
*/
52+
public function testGetTreemapDataRejectsInvalidApiMethod($method): void
53+
{
54+
$this->expectException(BadRequestException::class);
55+
$this->api->getTreemapData(
56+
$method,
57+
'nb_visits',
58+
'day',
59+
'2025-02-03'
60+
);
61+
}
62+
63+
public function validMethod()
64+
{
65+
return [
66+
['API.getMatomoVersion'],
67+
['API.getPhpVersion'],
68+
];
69+
}
70+
71+
public function inValidMethod()
72+
{
73+
return [
74+
['API.getBulkRequest'],
75+
['UsersManager.deleteUser'],
76+
];
77+
}
78+
}

0 commit comments

Comments
 (0)