feat(extensions): Add extension enable/disable toggle and loading logic
The settings view now renders a toggle switch for each extension to enable or disable it. The `Plugi` class includes a new method to load extensions based on their enabled state. Tests verify the activation logic and settings persistence.
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
use Plugi\Core\DB;
|
||||
use Plugi\Extensions;
|
||||
use Plugi\Plugi;
|
||||
use Plugi\Setting;
|
||||
|
||||
chdir(dirname(__DIR__, 2));
|
||||
|
||||
require_once __DIR__ . '/../src/Extensions.php';
|
||||
require_once __DIR__ . '/../src/Contracts/SettingContract.php';
|
||||
require_once __DIR__ . '/../src/Contracts/SettingRepositoryContract.php';
|
||||
require_once __DIR__ . '/../src/Setting.php';
|
||||
require_once __DIR__ . '/../src/Core/DB.php';
|
||||
require_once __DIR__ . '/../src/Repositories/BaseRepository.php';
|
||||
require_once __DIR__ . '/../src/Repositories/SettingRepository.php';
|
||||
require_once __DIR__ . '/../src/Plugi.php';
|
||||
|
||||
function phpb_config(string $key)
|
||||
{
|
||||
$value = $GLOBALS['phpb_config'];
|
||||
foreach (explode('.', $key) as $part) {
|
||||
if (! is_array($value) || ! array_key_exists($part, $value)) {
|
||||
return null;
|
||||
}
|
||||
$value = $value[$part];
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
function phpb_instance(string $name, array $params = []): ?string
|
||||
{
|
||||
return $name === 'setting' ? Setting::class : null;
|
||||
}
|
||||
|
||||
function phpb_current_language(): string
|
||||
{
|
||||
return 'en';
|
||||
}
|
||||
|
||||
function phpb_remove_non_alpha_numeric(string $value): string
|
||||
{
|
||||
return preg_replace('/[^a-zA-Z0-9]/', '', $value);
|
||||
}
|
||||
|
||||
class ExtensionActivationTestDatabase extends DB
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->pdo = new PDO('sqlite::memory:');
|
||||
$this->pdo->exec('CREATE TABLE settings (id INTEGER PRIMARY KEY AUTOINCREMENT, setting TEXT UNIQUE, value TEXT, is_array INTEGER)');
|
||||
$this->databaseName = 'extension_activation_test';
|
||||
}
|
||||
|
||||
public function setActivation(?string $value): void
|
||||
{
|
||||
if ($value === null) {
|
||||
$statement = $this->pdo->prepare('DELETE FROM settings WHERE setting = ?');
|
||||
$statement->execute(['extensions--form--enabled']);
|
||||
return;
|
||||
}
|
||||
|
||||
$statement = $this->pdo->prepare('INSERT OR REPLACE INTO settings (setting, value, is_array) VALUES (?, ?, 0)');
|
||||
$statement->execute(['extensions--form--enabled', $value]);
|
||||
}
|
||||
}
|
||||
|
||||
class ExtensionActivationTestPlugi extends Plugi
|
||||
{
|
||||
public function setDatabaseConnection()
|
||||
{
|
||||
global $phpb_db;
|
||||
$phpb_db = $GLOBALS['extensionActivationTestDatabase'];
|
||||
}
|
||||
|
||||
public function checkDatabaseDefinition()
|
||||
{
|
||||
}
|
||||
|
||||
public function loadTranslations($language)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
$database = new ExtensionActivationTestDatabase();
|
||||
$GLOBALS['extensionActivationTestDatabase'] = $database;
|
||||
$_ENV['DATABASE_PREFIX'] = '';
|
||||
$settingValues = new ReflectionProperty(Setting::class, 'settings');
|
||||
$registryDefaults = [
|
||||
'settings' => [],
|
||||
'configs' => [],
|
||||
'backends' => [],
|
||||
'routes' => [],
|
||||
'blocks' => [],
|
||||
'layouts' => [],
|
||||
'assets' => ['header' => [], 'footer' => [], 'admin-header' => [], 'admin-footer' => []],
|
||||
'htmlBlocks' => ['head' => [], 'body-start' => [], 'body-end' => [], 'admin-head' => [], 'admin-body-start' => [], 'admin-body-end' => []],
|
||||
];
|
||||
$config = [
|
||||
'general' => ['language' => 'en'],
|
||||
'storage' => ['use_database' => true],
|
||||
'auth' => ['use_login' => false],
|
||||
'website_manager' => ['use_website_manager' => false],
|
||||
'theme' => ['folder' => dirname(__DIR__, 2) . '/themes', 'active_theme' => 'default'],
|
||||
];
|
||||
|
||||
$boot = static function (?string $activationState) use ($database, $registryDefaults, $settingValues, $config): void {
|
||||
$database->setActivation($activationState);
|
||||
$settingValues->setValue(null, null);
|
||||
foreach ($registryDefaults as $property => $value) {
|
||||
(new ReflectionProperty(Extensions::class, $property))->setValue(null, $value);
|
||||
}
|
||||
new ExtensionActivationTestPlugi($config);
|
||||
};
|
||||
|
||||
$boot('1');
|
||||
if (! isset(Extensions::getRoutes()['/ext--form/submit'])
|
||||
|| ! isset(Extensions::getBackends()['ext--form'])
|
||||
|| ! isset(Extensions::getBlocks()['form-wrapper'])) {
|
||||
throw new RuntimeException('A persisted enabled state should register the extension during bootstrap.');
|
||||
}
|
||||
if (! str_contains(Extensions::getHeaderAssets(), '/extensions/plugi-ext-form/assets/dist.css')
|
||||
|| ! str_contains(Extensions::getFooterAssets(), '/extensions/plugi-ext-form/assets/dist.js')) {
|
||||
throw new RuntimeException('A persisted enabled state should register config-declared assets during bootstrap.');
|
||||
}
|
||||
|
||||
$boot('0');
|
||||
if (Extensions::getRoutes() !== [] || Extensions::getBackends() !== [] || Extensions::getBlocks() !== []) {
|
||||
throw new RuntimeException('A persisted disabled state should not register runtime features during bootstrap.');
|
||||
}
|
||||
if (Extensions::getHeaderAssets() !== '' || Extensions::getFooterAssets() !== '' || Extensions::getHtmlBlocks('head') !== '') {
|
||||
throw new RuntimeException('A persisted disabled state should not register assets or HTML hooks during bootstrap.');
|
||||
}
|
||||
$configs = Extensions::getConfigs();
|
||||
if (! isset($configs['form']['settings'][0]['name']) || $configs['form']['settings'][0]['name'] !== 'email') {
|
||||
throw new RuntimeException('Disabled extensions should retain their settings metadata.');
|
||||
}
|
||||
$db = (new ReflectionClass(DB::class))->newInstanceWithoutConstructor();
|
||||
if (! isset($db->getDBDefinition()['ext__form_entries'])) {
|
||||
throw new RuntimeException('Disabled extensions should retain their schema definitions.');
|
||||
}
|
||||
|
||||
$boot(null);
|
||||
if (Extensions::getRoutes() !== [] || Extensions::getBackends() !== [] || Extensions::getBlocks() !== []) {
|
||||
throw new RuntimeException('An absent activation state should remain disabled during bootstrap.');
|
||||
}
|
||||
|
||||
fwrite(STDOUT, "PASS: bootstrap loads only extensions with a persisted enabled state.\n");
|
||||
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
use Plugi\Core\DB;
|
||||
use Plugi\Extensions;
|
||||
use Plugi\Plugi;
|
||||
use Plugi\Repositories\SettingRepository;
|
||||
use Plugi\Setting;
|
||||
|
||||
chdir(dirname(__DIR__, 2));
|
||||
|
||||
require_once __DIR__ . '/../src/Extensions.php';
|
||||
require_once __DIR__ . '/../src/Contracts/SettingContract.php';
|
||||
require_once __DIR__ . '/../src/Contracts/SettingRepositoryContract.php';
|
||||
require_once __DIR__ . '/../src/Setting.php';
|
||||
require_once __DIR__ . '/../src/Core/DB.php';
|
||||
require_once __DIR__ . '/../src/Repositories/BaseRepository.php';
|
||||
require_once __DIR__ . '/../src/Repositories/SettingRepository.php';
|
||||
require_once __DIR__ . '/../src/Plugi.php';
|
||||
|
||||
function phpb_active_languages(): array
|
||||
{
|
||||
return ['en' => []];
|
||||
}
|
||||
|
||||
function phpb_config(string $key): ?string
|
||||
{
|
||||
return match ($key) {
|
||||
'general.language' => 'en',
|
||||
'theme.active_theme' => 'default',
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
|
||||
function phpb_csrf_field(): void
|
||||
{
|
||||
echo '<input type="hidden" name="_csrf_token" value="test-token">';
|
||||
}
|
||||
|
||||
function phpb_e($value): string
|
||||
{
|
||||
return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
|
||||
}
|
||||
|
||||
function phpb_instance(string $name): string
|
||||
{
|
||||
return Setting::class;
|
||||
}
|
||||
|
||||
function phpb_themes(): array
|
||||
{
|
||||
return ['default' => ['title' => 'Default']];
|
||||
}
|
||||
|
||||
function phpb_trans(string $key): array|string
|
||||
{
|
||||
return $key === 'languages' ? ['en' => 'English'] : $key;
|
||||
}
|
||||
|
||||
function phpb_url(string $module, array $parameters = []): string
|
||||
{
|
||||
return '/' . $module;
|
||||
}
|
||||
|
||||
function phpb_remove_non_alpha_numeric(string $value): string
|
||||
{
|
||||
return preg_replace('/[^a-zA-Z0-9]/', '', $value);
|
||||
}
|
||||
|
||||
class ExtensionSettingsTestDatabase extends DB
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->pdo = new PDO('sqlite::memory:');
|
||||
$this->pdo->exec('CREATE TABLE settings (id INTEGER PRIMARY KEY AUTOINCREMENT, setting TEXT, value TEXT, is_array INTEGER)');
|
||||
$this->databaseName = 'extension_settings_test';
|
||||
}
|
||||
}
|
||||
|
||||
$settingValues = new ReflectionProperty(Setting::class, 'settings');
|
||||
$settingValues->setValue(null, []);
|
||||
|
||||
$plugi = new Plugi();
|
||||
$plugi->loadExtensions();
|
||||
Extensions::addSettings([[
|
||||
'name' => 'enabled',
|
||||
'key' => 'ext--form--enabled',
|
||||
'type' => 'text',
|
||||
'label' => 'Extension field named enabled',
|
||||
'default' => '',
|
||||
]]);
|
||||
|
||||
$renderSettings = static function (): string {
|
||||
ob_start();
|
||||
require __DIR__ . '/../src/Modules/WebsiteManager/resources/views/tabs/settings.php';
|
||||
return ob_get_clean();
|
||||
};
|
||||
|
||||
$uncheckedSettings = $renderSettings();
|
||||
foreach (Extensions::getConfigs() as $extKey => $config) {
|
||||
if (substr_count($uncheckedSettings, 'name="extensions--' . $extKey . '--enabled"') !== 2) {
|
||||
throw new RuntimeException('Each discovered extension should submit both enabled and disabled values.');
|
||||
}
|
||||
}
|
||||
if (preg_match('/<input type="checkbox" name="extensions--sitemap--enabled" value="1"[^>]*checked/', $uncheckedSettings)) {
|
||||
throw new RuntimeException('An extension without a saved state should render disabled.');
|
||||
}
|
||||
if (strpos($uncheckedSettings, 'name="extensions--form--enabled" value="0"') > strpos($uncheckedSettings, 'name="extensions--form--enabled" value="1"')) {
|
||||
throw new RuntimeException('The disabled value must precede the checkbox so checked values override it.');
|
||||
}
|
||||
if (substr_count($uncheckedSettings, 'name="ext--form--enabled"') !== 1) {
|
||||
throw new RuntimeException('An extension setting named enabled should not share the activation control name.');
|
||||
}
|
||||
|
||||
$settingValues->setValue(null, ['extensions--form--enabled' => '1', 'ext--form--enabled' => 'kept']);
|
||||
$checkedSettings = $renderSettings();
|
||||
if (! preg_match('/<input type="checkbox" name="extensions--form--enabled" value="1"[^>]*checked/', $checkedSettings)) {
|
||||
throw new RuntimeException('An enabled extension should render its switch as checked.');
|
||||
}
|
||||
if (! preg_match('/<input type="text" name="ext--form--enabled" value="kept"/', $checkedSettings)) {
|
||||
throw new RuntimeException('An extension setting named enabled should retain its submitted value.');
|
||||
}
|
||||
|
||||
$_ENV['DATABASE_PREFIX'] = '';
|
||||
$database = new ExtensionSettingsTestDatabase();
|
||||
$phpb_db = $database;
|
||||
$settingRepository = new SettingRepository();
|
||||
if (! $settingRepository->updateSettings(['extensions--form--enabled' => '1'])
|
||||
|| ! $settingRepository->updateSettings(['extensions--form--enabled' => '0'])) {
|
||||
throw new RuntimeException('The settings repository should persist both enabled and disabled values.');
|
||||
}
|
||||
$saved = $database->select('SELECT value FROM settings WHERE setting = ?', ['extensions--form--enabled']);
|
||||
if (($saved[0]['value'] ?? null) !== '0') {
|
||||
throw new RuntimeException('Saving an unchecked extension switch should replace its earlier enabled state.');
|
||||
}
|
||||
|
||||
fwrite(STDOUT, "PASS: extension switches render and persist explicit on/off values.\n");
|
||||
Reference in New Issue
Block a user