Skip to content

Commit e11966b

Browse files
authored
Pg 5208 enable generation by default (#45)
* enable spec generation by default * Clean up code * add install logic * clean code * fix issues * Tests and fixes * Fix licence * phpcs
1 parent 7e39f13 commit e11966b

4 files changed

Lines changed: 173 additions & 3 deletions

File tree

ApiReference.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,16 @@ public function getClientSideTranslationKeys(&$translationKeys): void
4343
$translationKeys[] = 'ApiReference_UserAuthenticationManageTokens';
4444
$translationKeys[] = 'ApiReference_UserAuthenticationUsingTokenAuth';
4545
}
46+
47+
public function install()
48+
{
49+
$config = new Configuration();
50+
$config->install();
51+
}
52+
53+
public function uninstall()
54+
{
55+
$config = new Configuration();
56+
$config->uninstall();
57+
}
4658
}

Configuration.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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\ApiReference;
11+
12+
use Piwik\Config;
13+
14+
class Configuration
15+
{
16+
public const DEFAULT_ENABLE_SPEC_GENERATION = 1;
17+
public const KEY_ENABLE_SPEC_GENERATION = 'enable_spec_generation_task';
18+
19+
public function install()
20+
{
21+
$config = $this->getConfig();
22+
23+
$apiReferenceConfig = $config->ApiReference;
24+
if (empty($apiReferenceConfig)) {
25+
$apiReferenceConfig = array();
26+
}
27+
28+
// we make sure to set a value only if none has been configured yet, eg in common config.
29+
if (!array_key_exists(self::KEY_ENABLE_SPEC_GENERATION, $apiReferenceConfig)) {
30+
$apiReferenceConfig[self::KEY_ENABLE_SPEC_GENERATION] = self::DEFAULT_ENABLE_SPEC_GENERATION;
31+
}
32+
33+
$config->ApiReference = $apiReferenceConfig;
34+
$config->forceSave();
35+
}
36+
37+
public function uninstall()
38+
{
39+
$config = $this->getConfig();
40+
$config->ApiReference = array();
41+
$config->forceSave();
42+
}
43+
44+
45+
public function specGenerationEnabled()
46+
{
47+
$value = $this->getConfigValue(self::KEY_ENABLE_SPEC_GENERATION, self::DEFAULT_ENABLE_SPEC_GENERATION);
48+
49+
if ($value === false || $value === '' || $value === null) {
50+
$value = self::DEFAULT_ENABLE_SPEC_GENERATION;
51+
}
52+
53+
return (bool) $value;
54+
}
55+
56+
private function getConfigValue($name, $default)
57+
{
58+
$config = $this->getConfig();
59+
$attribution = $config->ApiReference;
60+
if (isset($attribution[$name])) {
61+
return $attribution[$name];
62+
}
63+
return $default;
64+
}
65+
66+
private function getConfig()
67+
{
68+
return Config::getInstance();
69+
}
70+
}

Tasks.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Piwik\Plugins\ApiReference;
1313

14-
use Piwik\Config;
1514
use Piwik\Log\LoggerInterface;
1615
use Piwik\Plugins\ApiReference\Generation\PluginListProvider;
1716
use Piwik\Plugins\ApiReference\Generation\SpecGenerationService;
@@ -32,15 +31,21 @@ class Tasks extends \Piwik\Plugin\Tasks
3231
* @var PluginListProvider
3332
*/
3433
private $pluginListProvider;
34+
/**
35+
* @var Configuration
36+
*/
37+
private $config;
3538

3639
public function __construct(
3740
SpecGenerationService $specGenerationService,
3841
LoggerInterface $logger,
39-
?PluginListProvider $pluginListProvider = null
42+
?PluginListProvider $pluginListProvider = null,
43+
?Configuration $config = null
4044
) {
4145
$this->specGenerationService = $specGenerationService;
4246
$this->logger = $logger;
4347
$this->pluginListProvider = $pluginListProvider ?? new PluginListProvider();
48+
$this->config = $config ?? new Configuration();
4449
}
4550

4651
public function schedule()
@@ -77,6 +82,6 @@ public function generateConfiguredPluginSpecs(): void
7782

7883
private function isSpecGenerationEnabled(): bool
7984
{
80-
return (bool) (Config::getInstance()->ApiReference['enable_spec_generation_task'] ?? 0);
85+
return $this->config->specGenerationEnabled();
8186
}
8287
}

tests/Unit/ConfigurationTest.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
declare(strict_types=1);
11+
12+
namespace Piwik\Plugins\ApiReference\tests\Unit;
13+
14+
require_once PIWIK_INCLUDE_PATH . '/plugins/ApiReference/vendor/autoload.php';
15+
16+
use PHPUnit\Framework\TestCase;
17+
use Piwik\Config;
18+
use Piwik\Plugins\ApiReference\Configuration;
19+
20+
/**
21+
* @group ApiReference
22+
* @group ApiReference_Unit
23+
* @group ApiReference_ConfigurationTest
24+
*/
25+
class ConfigurationTest extends TestCase
26+
{
27+
/**
28+
* @var mixed
29+
*/
30+
private $originalConfig;
31+
32+
protected function setUp(): void
33+
{
34+
parent::setUp();
35+
36+
$this->originalConfig = Config::getInstance()->ApiReference ?? null;
37+
}
38+
39+
protected function tearDown(): void
40+
{
41+
Config::getInstance()->ApiReference = $this->originalConfig;
42+
43+
parent::tearDown();
44+
}
45+
46+
public function testInstallSetsDefaultWhenConfigKeyIsMissing(): void
47+
{
48+
Config::getInstance()->ApiReference = [];
49+
50+
$configuration = new Configuration();
51+
$configuration->install();
52+
53+
$this->assertSame(
54+
Configuration::DEFAULT_ENABLE_SPEC_GENERATION,
55+
Config::getInstance()->ApiReference[Configuration::KEY_ENABLE_SPEC_GENERATION]
56+
);
57+
}
58+
59+
public function testInstallPreservesExplicitDisabledValue(): void
60+
{
61+
Config::getInstance()->ApiReference = [
62+
Configuration::KEY_ENABLE_SPEC_GENERATION => 0,
63+
];
64+
65+
$configuration = new Configuration();
66+
$configuration->install();
67+
68+
$this->assertSame(0, Config::getInstance()->ApiReference[Configuration::KEY_ENABLE_SPEC_GENERATION]);
69+
}
70+
71+
public function testUninstallClearsApiReferenceConfig(): void
72+
{
73+
Config::getInstance()->ApiReference = [
74+
Configuration::KEY_ENABLE_SPEC_GENERATION => 1,
75+
'another_key' => 'value',
76+
];
77+
78+
$configuration = new Configuration();
79+
$configuration->uninstall();
80+
81+
$this->assertSame([], Config::getInstance()->ApiReference);
82+
}
83+
}

0 commit comments

Comments
 (0)