diff --git a/API.php b/API.php index 7f7ecdb..ffbb3e4 100644 --- a/API.php +++ b/API.php @@ -24,6 +24,26 @@ */ class API extends \Piwik\Plugin\API { + /** + * Return the OpenApiDocs plugin whitelist from config/plugins.php. + * + * /index.php?module=API&method=OpenApiDocs.getPluginWhitelist + * + * @return array + * @throws \Exception + */ + public function getPluginWhitelist(): array + { + Piwik::checkUserHasSomeViewAccess(); + + $pluginWhitelist = $this->loadPluginWhitelist(); + if (!is_array($pluginWhitelist)) { + throw new \Exception('OpenApiDocs plugin whitelist config is invalid.'); + } + + return $pluginWhitelist; + } + /** * Get a pre-generated OpenAPI spec file if it exists. This endpoint only reads * the generated JSON file and does not trigger spec generation. @@ -71,6 +91,14 @@ protected function getSpecFilePath(string $pluginName): string return $this->getSpecPathResolver()->getSpecFilePath($pluginName); } + /** + * @return mixed + */ + protected function loadPluginWhitelist() + { + return require __DIR__ . '/config/plugins.php'; + } + protected function isSpecFileReadable(string $filePath): bool { return is_file($filePath) && is_readable($filePath); diff --git a/tests/Unit/APITest.php b/tests/Unit/APITest.php index 124b6b4..1d845d0 100644 --- a/tests/Unit/APITest.php +++ b/tests/Unit/APITest.php @@ -61,6 +61,31 @@ public function testGetOpenApiSpecReturnsDecodedJsonForPlugin() $this->assertSame($expectedSpec, $result); } + public function testGetPluginWhitelistReturnsConfigValuesInOrder() + { + $expectedWhitelist = ['RollUpReporting', 'Login', 'ActivityLog']; + + $api = $this->getMockBuilder(API::class) + ->onlyMethods(['loadPluginWhitelist']) + ->getMock(); + $api->method('loadPluginWhitelist')->willReturn($expectedWhitelist); + + $this->assertSame($expectedWhitelist, $api->getPluginWhitelist()); + } + + public function testGetPluginWhitelistThrowsExceptionWhenConfigIsInvalid() + { + $api = $this->getMockBuilder(API::class) + ->onlyMethods(['loadPluginWhitelist']) + ->getMock(); + $api->method('loadPluginWhitelist')->willReturn('invalid'); + + $this->expectException(\Exception::class); + $this->expectExceptionMessage('OpenApiDocs plugin whitelist config is invalid.'); + + $api->getPluginWhitelist(); + } + public function testGetOpenApiSpecThrowsExceptionWhenFileMissing() { $api = $this->buildApiMock('/tmp/CustomAlerts_openapi_spec_v1.0.0.json', false);