Skip to content

Commit fcc9f8a

Browse files
committed
Removed matomo case and added plugin validation logic
1 parent cdbffb5 commit fcc9f8a

2 files changed

Lines changed: 18 additions & 30 deletions

File tree

API.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,26 @@ class API extends \Piwik\Plugin\API
2727
* Get a pre-generated OpenAPI spec file if it exists. This endpoint only reads
2828
* the generated JSON file and does not trigger spec generation.
2929
*
30-
* /index.php?module=API&method=OpenApiDocs.getOpenApiSpec&spec=matomo
30+
* /index.php?module=API&method=OpenApiDocs.getOpenApiSpec&spec=CustomAlerts
3131
*
32-
* @param string $spec Spec identifier used in the generated filename. Use `matomo`
33-
* for the aggregate spec or a plugin name for a specific spec.
32+
* @param string $spec Plugin name used in the generated filename.
3433
* @param string $format Output format. Only `json` is supported.
3534
* @return array<string, mixed> The decoded OpenAPI specification payload.
3635
* @throws \Exception If the file is missing, unreadable, or contains invalid JSON.
3736
*/
38-
public function getOpenApiSpec(string $spec = 'matomo', string $format = 'json'): array
37+
public function getOpenApiSpec(string $spec, string $format = 'json'): array
3938
{
4039
Piwik::checkUserHasSomeViewAccess();
4140

4241
$this->validateJsonFormat($format);
4342

43+
if (
44+
!Manager::getInstance()->isValidPluginName($spec)
45+
|| !Manager::getInstance()->isPluginInFilesystem($spec)
46+
) {
47+
throw new \Piwik\Exception\PluginNotFoundException($spec);
48+
}
49+
4450
$filePath = $this->getSpecFilePath($spec);
4551
if (!$this->isSpecFileReadable($filePath)) {
4652
throw new \Exception('OpenAPI spec file was not found. Generate it first via openapidocs:generate-spec-file.');

tests/Unit/APITest.php

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,6 @@ protected function tearDown(): void
4343
parent::tearDown();
4444
}
4545

46-
public function testGetOpenApiSpecReturnsDecodedJsonForMatomo()
47-
{
48-
$expectedSpec = [
49-
'openapi' => '3.1.0',
50-
'info' => [
51-
'title' => 'Matomo Reporting API',
52-
'version' => '1.0.0',
53-
],
54-
];
55-
56-
$api = $this->buildApiMock('/tmp/matomo_openapi_spec_v1.0.0.json', true, json_encode($expectedSpec));
57-
58-
$result = $api->getOpenApiSpec();
59-
60-
$this->assertSame($expectedSpec, $result);
61-
}
62-
6346
public function testGetOpenApiSpecReturnsDecodedJsonForPlugin()
6447
{
6548
$expectedSpec = [
@@ -79,22 +62,22 @@ public function testGetOpenApiSpecReturnsDecodedJsonForPlugin()
7962

8063
public function testGetOpenApiSpecThrowsExceptionWhenFileMissing()
8164
{
82-
$api = $this->buildApiMock('/tmp/matomo_openapi_spec_v1.0.0.json', false);
65+
$api = $this->buildApiMock('/tmp/CustomAlerts_openapi_spec_v1.0.0.json', false);
8366

8467
$this->expectException(\Exception::class);
8568
$this->expectExceptionMessage('OpenAPI spec file was not found');
8669

87-
$api->getOpenApiSpec();
70+
$api->getOpenApiSpec('CustomAlerts');
8871
}
8972

9073
public function testGetOpenApiSpecThrowsExceptionWhenJsonIsInvalid()
9174
{
92-
$api = $this->buildApiMock('/tmp/matomo_openapi_spec_v1.0.0.json', true, '{invalid json}');
75+
$api = $this->buildApiMock('/tmp/CustomAlerts_openapi_spec_v1.0.0.json', true, '{invalid json}');
9376

9477
$this->expectException(\Exception::class);
9578
$this->expectExceptionMessage('OpenAPI spec file contains invalid JSON');
9679

97-
$api->getOpenApiSpec();
80+
$api->getOpenApiSpec('CustomAlerts');
9881
}
9982

10083
public function testGetOpenApiSpecThrowsExceptionWhenFormatIsInvalid()
@@ -107,14 +90,13 @@ public function testGetOpenApiSpecThrowsExceptionWhenFormatIsInvalid()
10790
$api->getOpenApiSpec('CustomAlerts', 'yaml');
10891
}
10992

110-
public function testGetSpecFilePathUsesMatomoFileNameByDefault()
93+
public function testGetOpenApiSpecThrowsExceptionWhenSpecIsNotAValidPlugin()
11194
{
11295
$api = new API();
11396

114-
$this->assertSame(
115-
PIWIK_INCLUDE_PATH . '/plugins/OpenApiDocs/tmp/specs/matomo_openapi_spec_v1.0.0.json',
116-
$this->callProtectedMethod($api, 'getSpecFilePath', ['matomo'])
117-
);
97+
$this->expectException(\Piwik\Exception\PluginNotFoundException::class);
98+
99+
$api->getOpenApiSpec('DefinitelyNotARealPlugin');
118100
}
119101

120102
public function testGetSpecFilePathUsesPluginSpecificFileName()

0 commit comments

Comments
 (0)