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");