Skip to content

Commit

Permalink
feat(setup) site support
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed Aug 10, 2023
1 parent a17fc9b commit e25eb24
Show file tree
Hide file tree
Showing 74 changed files with 243,616 additions and 251 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,3 @@ package: update_core_phpunit_includes test

update_sqlite_plugin:
bin/update_sqlite_plugin

1 change: 1 addition & 0 deletions bin/update_sqlite_plugin
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ curl -L "$plugin_file_src" -o "$root_dir/includes/sqlite-database-integration.zi
unzip -o "$root_dir/includes/sqlite-database-integration.zip" -d "$root_dir/includes/"
# Remove the zip file
rm "$root_dir/includes/sqlite-database-integration.zip"
git apply "${root_dir}"/config/patches/sqlite-database-integration/db.copy.patch

22 changes: 22 additions & 0 deletions config/patches/sqlite-database-integration/db.copy.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
diff --git a/includes/sqlite-database-integration/db.copy b/includes/sqlite-database-integration/db.copy
index 0b0797e8..23caff78 100644
--- a/includes/sqlite-database-integration/db.copy
+++ b/includes/sqlite-database-integration/db.copy
@@ -32,6 +32,17 @@ if ( ! defined( 'DATABASE_TYPE' ) ) {
if ( ! defined( 'DB_ENGINE' ) ) {
define( 'DB_ENGINE', 'sqlite' );
}
+// Define SQLite main file constant to avoid having the plugin loaded automatically.
+if ( ! defined( 'SQLITE_MAIN_FILE' ) ) {
+ define( 'SQLITE_MAIN_FILE', '{SQLITE_MAIN_FILE}' );
+}
+// Define DB_DIR and DB_FILE from env, if not already defined.
+if( ! defined( 'DB_DIR' ) && getenv( 'DB_DIR' ) ) {
+ define( 'DB_DIR', realpath( getenv( 'DB_DIR' ) ) );
+}
+if( ! defined( 'DB_FILE' ) && getenv( 'DB_FILE' ) ) {
+ define( 'DB_FILE', getenv( 'DB_FILE' ) );
+}

// Require the implementation from the plugin.
require_once $sqlite_plugin_implementation_folder_path . '/wp-includes/sqlite/db.php';
2 changes: 2 additions & 0 deletions includes/cli-server/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
$root = $_SERVER['DOCUMENT_ROOT'];
$path = '/'. ltrim( parse_url( urldecode( $_SERVER['REQUEST_URI'] ),PHP_URL_PATH ), '/' );

define('DB_ENGINE', getenv('DB_ENGINE') ?: 'mysql');

if ( file_exists( $root.$path ) ) {

// Enforces trailing slash, keeping links tidy in the admin
Expand Down
15 changes: 13 additions & 2 deletions includes/sqlite-database-integration/db.copy
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,22 @@ if ( ! $sqlite_plugin_implementation_folder_path || ! file_exists( $sqlite_plugi

// Constant for backward compatibility.
if ( ! defined( 'DATABASE_TYPE' ) ) {
define( 'DATABASE_TYPE', 'sqlite' );
define( 'DATABASE_TYPE', getenv( 'DATABASE_TYPE' ) ?: 'sqlite' );
}
// Define SQLite constant.
if ( ! defined( 'DB_ENGINE' ) ) {
define( 'DB_ENGINE', 'sqlite' );
define( 'DB_ENGINE', getenv( 'DB_ENGINE' ) ?: 'sqlite' );
}
// Define SQLite main file constant to avoid having the plugin loaded automatically.
if ( ! defined( 'SQLITE_MAIN_FILE' ) ) {
define( 'SQLITE_MAIN_FILE', '{SQLITE_MAIN_FILE}' );
}
// Define DB_DIR and DB_FILE from env, if not already defined.
if( ! defined( 'DB_DIR' ) && getenv( 'DB_DIR' ) ) {
define( 'DB_DIR', realpath( getenv( 'DB_DIR' ) ) );
}
if( ! defined( 'DB_FILE' ) && getenv( 'DB_FILE' ) ) {
define( 'DB_FILE', getenv( 'DB_FILE' ) );
}

// Require the implementation from the plugin.
Expand Down
35 changes: 28 additions & 7 deletions src/Extension/BuiltInServerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ public function start(OutputInterface $output): void
return;
}

[$port, $docRoot, $workers] = $this->parseConfig();
[$port, $docRoot, $workers, $env] = $this->parseConfig();

$output->write("Starting PHP built-in server on port $port to serve $docRoot ...");
$phpBuiltInServer = new PhpBuiltInServer($docRoot, $port, ['PHP_CLI_SERVER_WORKERS' => $workers]);
$env = array_merge([
'PHP_CLI_SERVER_WORKERS' => $workers,
], $env);
$phpBuiltInServer = new PhpBuiltInServer($docRoot, $port, $env);
$phpBuiltInServer->start();
$output->write(' ok', true);
}
Expand Down Expand Up @@ -66,13 +69,15 @@ public function getPrettyName(): string
* port: int,
* docroot: string,
* workers: int,
* pid: int|null
* pid: int|null,
* url: string,
* env: array<string,string|int|float>
* }
* @throws ExtensionException
*/
public function getInfo(): array
{
[$port, $docRoot, $workers] = $this->parseConfig();
[$port, $docRoot, $workers, $env] = $this->parseConfig();
$pidFile = $this->getPidFile();

return [
Expand All @@ -81,15 +86,17 @@ public function getInfo(): array
'port' => $port,
'docroot' => $docRoot,
'workers' => $workers,
'url' => 'http://localhost:' . $port . '/'
'url' => 'http://localhost:' . $port . '/',
'env' => $env
];
}

/**
* @return array{
* 0: int,
* 1: string,
* 2: int
* 2: int,
* 3: array<string,string|int|float>
* }
* @throws ExtensionException
*/
Expand Down Expand Up @@ -122,7 +129,21 @@ private function parseConfig(): array
}
/** @var array{workers?: number} $config */
$workers = (int)($config['workers'] ?? 5);
return array($port, $docRoot, $workers);

if (isset($config['env']) && !is_array($config['env'])) {
throw new ExtensionException(
$this,
'The "env" configuration option must be an array.'
);
}
$env = $config['env'] ?? [];
$env = array_map( static function ( $value ): mixed {
return is_string( $value ) ?
str_replace( '%codecept_root_dir%', rtrim( codecept_root_dir(), '\\/' ), $value )
: $value;
}, $env );

return [$port, $docRoot, $workers, $env];
}

private function getPidFile(): string
Expand Down
2 changes: 1 addition & 1 deletion src/Extension/DockerComposeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function stop(OutputInterface $output): void
);
}
$runningFile = $this->getRunningFile();
if (!(is_file($runningFile) && unlink($runningFile))) {
if (is_file($runningFile) && !unlink($runningFile)) {
throw new ExtensionException(
$this,
'Failed to remove Docker Compose running file.'
Expand Down
3 changes: 2 additions & 1 deletion src/ManagedProcess/ChromeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ public function doStart(): void
*/
private function confirmStart(Process $process): void
{
$start = time();
$output = $process->getOutput();
for ($attempts = 0; $attempts < 30; $attempts++) {
while (time() < $start + 30) {
if (str_contains($output, 'ChromeDriver was started successfully.')) {
return;
}
Expand Down
56 changes: 30 additions & 26 deletions src/Module/WPLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ class WPLoader extends Module
private string $bootstrapOutput = '';
private string $installationOutput = '';
private bool $earlyExit = true;
private ?DatabaseInterface $db = null;

public function _getBootstrapOutput(): string
{
Expand Down Expand Up @@ -291,17 +292,10 @@ public function _initialize(): void

// Try and initialize the database connection now.
$db->create();
$db->setEnvVars();
$this->db = $db;

$this->installation = new Installation($config['wpRootFolder'], $db);

$dropInPathname = $this->installation->getContentDir('db.php');
if ($db instanceof SQLiteDatabase && !is_file($dropInPathname)) {
throw new ModuleConfigException(
__CLASS__,
'WPLoader is configured to use a SQLite database, but no db.php drop-in file ' .
"was found in the content folder ($dropInPathname)."
);
}
$this->installation = new Installation( $config['wpRootFolder'], false );

// Update the config to the resolved path.
$config['wpRootFolder'] = $this->installation->getWpRootDir();
Expand All @@ -311,7 +305,14 @@ public function _initialize(): void
if ($installationState instanceof EmptyDir) {
$wpRootDir = $this->installation->getWpRootDir();
Installation::scaffold($wpRootDir);
$this->installation = new Installation($wpRootDir, $db);
$this->installation = new Installation($wpRootDir);
}

if ($db instanceof SqliteDatabase && !is_file($this->installation->getContentDir('db.php'))) {
Installation::placeSqliteMuPlugin(
$this->installation->getMuPluginsDir(),
$this->installation->getContentDir()
);
}

$config['wpRootFolder'] = $this->installation->getWpRootDir();
Expand Down Expand Up @@ -511,17 +512,15 @@ private function installAndBootstrapInstallation(): void
if ($this->config['theme']) {
// Refresh the theme related options.
update_site_option('allowedthemes', [$this->config['theme'] => true]);
$db = $this->installation->getDb();

if ($db === null) {
if ($this->db === null) {
throw new ModuleException(
__CLASS__,
'Could not get database instance from installation.'
);
}

update_option('template', $db->getOption('template'));
update_option('stylesheet', $db->getOption('stylesheet'));
update_option('template', $this->db->getOption('template'));
update_option('stylesheet', $this->db->getOption('stylesheet'));
}

// Format for site-wide active plugins is `[ 'plugin-slug/plugin.php' => timestamp ]`.
Expand All @@ -536,23 +535,22 @@ private function installAndBootstrapInstallation(): void

if ($this->config['theme']) {
// Refresh the theme related options.
$db = $this->installation->getDb();

if ($db === null) {
if ($this->db === null) {
throw new ModuleException(
__CLASS__,
'Could not get database instance from installation.'
);
}

update_option('template', $db->getOption('template'));
update_option('stylesheet', $db->getOption('stylesheet'));
update_option('template', $this->db->getOption('template'));
update_option('stylesheet', $this->db->getOption('stylesheet'));
}

return $plugins;
};
PreloadFilters::addFilter('pre_option_active_plugins', $activate);
}

$this->includeCorePHPUniteSuiteBootstrapFile();

Dispatcher::dispatch(self::EVENT_AFTER_INSTALL, $this);
Expand Down Expand Up @@ -604,15 +602,20 @@ private function activatePluginsSwitchThemeInSeparateProcess(): void
[$type, $name] = explode('::', $key, 2);
$returnValue = $result->getReturnValue();

if ($returnValue instanceof Throwable) {
if ($returnValue instanceof Throwable && !($returnValue instanceof InstallationException)) {
// Not gift-wrapped in a ModuleException to make it easier to debug the issue.
throw $returnValue;
}

if ($result->getExitCode() !== 0) {
$error = $result->getExitCode() !== 0 || $returnValue instanceof InstallationException;

if ($error) {
$reason = $returnValue instanceof InstallationException ?
$returnValue->getMessage()
: $result->getStdoutBuffer();
$message = $type === 'plugin' ?
"Failed to activate plugin $name: {$result->getStdoutBuffer()}"
: "Failed to switch theme $name: {$result->getStdoutBuffer()}";
"Failed to activate plugin $name. $reason"
: "Failed to switch theme $name. $reason";
throw new ModuleException(__CLASS__, $message);
}
}
Expand Down Expand Up @@ -734,6 +737,7 @@ public function getInstallation(): Installation
*/
private function checkInstallationToLoadOnly(): void
{
// The installation must be at least configured: it might be installed by a dump.
if (!$this->installation->isConfigured()) {
$dir = $this->installation->getWpRootDir();
throw new ModuleException(
Expand Down Expand Up @@ -765,7 +769,7 @@ private function disableUpdates(): void
*/
private function importDumps(): void
{
$db = $this->installation->getDb();
$db = $this->db;

if (!$db instanceof DatabaseInterface) {
throw new ModuleException(
Expand Down
4 changes: 4 additions & 0 deletions src/Process/Loop.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Codeception\Exception\ConfigurationException;
use Codeception\Util\Debug;
use lucatume\WPBrowser\Process\Worker\Exited;
use lucatume\WPBrowser\Process\Worker\Result;
use lucatume\WPBrowser\Process\Worker\Running;
Expand Down Expand Up @@ -55,6 +56,9 @@ public function __construct(
private float $timeout = 30,
array $options = []
) {
if (Debug::isEnabled() || getenv('WPBROWSER_LOOP_DEBUG')) {
$this->timeout = 10 ** 10;
}
$this->addWorkers($workers, $options);
}

Expand Down
Loading

0 comments on commit e25eb24

Please sign in to comment.