85 lines
2.9 KiB
PHP
85 lines
2.9 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
$pluginDir = dirname(__DIR__);
|
|
require_once $pluginDir . '/exec/lib/Settings.php';
|
|
require_once $pluginDir . '/exec/lib/DirectAdminUser.php';
|
|
|
|
function fail(string $message): void
|
|
{
|
|
fwrite(STDERR, "FAIL: {$message}\n");
|
|
exit(1);
|
|
}
|
|
|
|
function assert_true(bool $condition, string $message): void
|
|
{
|
|
if (!$condition) {
|
|
fail($message);
|
|
}
|
|
}
|
|
|
|
function assert_same($expected, $actual, string $message): void
|
|
{
|
|
if ($expected !== $actual) {
|
|
fail($message . ' expected=' . var_export($expected, true) . ' actual=' . var_export($actual, true));
|
|
}
|
|
}
|
|
|
|
function load_settings(string $contents): Settings
|
|
{
|
|
$path = tempnam(sys_get_temp_dir(), 'altmysql-settings-');
|
|
if ($path === false) {
|
|
fail('cannot create temp settings file');
|
|
}
|
|
file_put_contents($path, $contents);
|
|
$settings = Settings::load($path);
|
|
unlink($path);
|
|
return $settings;
|
|
}
|
|
|
|
function make_da_user(Settings $settings, array $userConf, array $packageConf): DirectAdminUser
|
|
{
|
|
$class = new ReflectionClass(DirectAdminUser::class);
|
|
/** @var DirectAdminUser $user */
|
|
$user = $class->newInstanceWithoutConstructor();
|
|
foreach ([
|
|
'username' => 'demo',
|
|
'prefix' => 'demo_',
|
|
'userConf' => $userConf,
|
|
'packageConf' => $packageConf,
|
|
'settings' => $settings,
|
|
] as $property => $value) {
|
|
$prop = $class->getProperty($property);
|
|
$prop->setAccessible(true);
|
|
$prop->setValue($user, $value);
|
|
}
|
|
return $user;
|
|
}
|
|
|
|
$alwaysSettings = load_settings("always_enabled=1\nMYSQL_PLUGIN_DEFAULT_DB_LIMIT=5\n");
|
|
assert_true($alwaysSettings->alwaysEnabled(), 'always_enabled=1 should be enabled');
|
|
$alwaysUser = make_da_user($alwaysSettings, [
|
|
'mysql_enabled' => 'no',
|
|
'mysql' => '0',
|
|
'plugins_deny' => 'alt-mysql',
|
|
], []);
|
|
assert_true($alwaysUser->hasPluginAccess(), 'always_enabled=1 should bypass per-user enable and plugin deny rules');
|
|
assert_same(0, $alwaysUser->maxDatabases(), 'always_enabled=1 should make database limit unlimited');
|
|
|
|
$defaultSettings = load_settings("");
|
|
assert_true($defaultSettings->alwaysEnabled(), 'always_enabled should default to enabled');
|
|
$defaultUser = make_da_user($defaultSettings, [], []);
|
|
assert_true($defaultUser->hasPluginAccess(), 'default always_enabled should grant access without custom package items');
|
|
assert_same(0, $defaultUser->maxDatabases(), 'default always_enabled should make database limit unlimited');
|
|
|
|
$legacySettings = load_settings("always_enabled=0\nMYSQL_PLUGIN_DEFAULT_DB_LIMIT=5\n");
|
|
assert_true(!$legacySettings->alwaysEnabled(), 'always_enabled=0 should disable automatic access');
|
|
$legacyUser = make_da_user($legacySettings, [
|
|
'mysql_enabled' => 'no',
|
|
'mysql' => '0',
|
|
], []);
|
|
assert_true(!$legacyUser->hasPluginAccess(), 'always_enabled=0 should preserve per-user access checks');
|
|
assert_same(0, $legacyUser->maxDatabases(), 'explicit mysql=0 remains zero in legacy mode');
|
|
|
|
echo "always_enabled_test: OK\n";
|