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.
136 lines
4.7 KiB
PHP
136 lines
4.7 KiB
PHP
<?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"); |