Skip to content

Commit

Permalink
Merge pull request #717 from lucatume/v35-fix-716
Browse files Browse the repository at this point in the history
fix #716
  • Loading branch information
lucatume authored Apr 12, 2024
2 parents d87aebd + 9ee6a00 commit 23edc34
Show file tree
Hide file tree
Showing 35 changed files with 613 additions and 164 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [unreleased] Unreleased

### Changed

- Fix non standard installation handling, hello Bedrock! (#716)

## [3.5.6] 2024-04-07;

### Changed
Expand Down
65 changes: 41 additions & 24 deletions includes/cli-server/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,57 @@
*/

$root = $_SERVER['DOCUMENT_ROOT'];
$path = '/'. ltrim( parse_url( urldecode( $_SERVER['REQUEST_URI'] ),PHP_URL_PATH ), '/' );
$path = '/' . ltrim(parse_url(urldecode($_SERVER['REQUEST_URI']), PHP_URL_PATH), '/');

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

// Add a unique request ID to the headers.
$requestId = md5( microtime() );
$requestId = md5(microtime());
$_SERVER['REQUEST_ID'] = $requestId;
header( 'X-Request-ID: ' . $requestId );
header('X-Request-ID: ' . $requestId);

// Disable the MU upgrade routine.
global $wp_filter;
$wp_filter['do_mu_upgrade'][10][] = [
'accepted_args' => 0,
'function' => '__return_false'
'function' => '__return_false'
];

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

// Enforces trailing slash, keeping links tidy in the admin
if ( is_dir( $root.$path ) && substr_compare($path, '/', -strlen('/')) !== 0 ) {
header( "Location: $path/" );
exit;
}

// Runs PHP file if it exists
if ( strpos($path, '.php') !== false ) {
chdir( dirname( $root.$path ) );
require_once $root.$path;
} else {
return false;
}
$wpbrowserSiteurl = getenv('WPBROWSER_SITEURL');
$wpbrowserHomeUrl = getenv('WPBROWSER_HOMEURL');
if ($wpbrowserSiteurl) {
$wp_filter['option_siteurl'][1000][] = [
'accepted_args' => 0,
'function' => function () use ($wpbrowserSiteurl) {
return $wpbrowserSiteurl;
}
];
}
if (getenv('WPBROWSER_HOMEURL')) {
$wp_filter['option_home'][1000][] = [
'accepted_args' => 0,
'function' => function () use ($wpbrowserHomeUrl) {
return $wpbrowserHomeUrl;
}
];
}
unset($wpbrowserSiteurl, $wpbrowserHomeUrl);

if (file_exists($root . $path)) {
// Enforces trailing slash, keeping links tidy in the admin
if (is_dir($root . $path) && substr_compare($path, '/', -strlen('/')) !== 0) {
header("Location: $path/");
exit;
}

// Runs PHP file if it exists
if (strpos($path, '.php') !== false) {
chdir(dirname($root . $path));
require_once $root . $path;
} else {
return false;
}
} else {

// Otherwise, run `index.php`
chdir( $root );
require_once 'index.php';
// Otherwise, run `index.php`
chdir($root);
require_once 'index.php';
}
4 changes: 3 additions & 1 deletion includes/sqlite-database-integration/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
* @package wp-sqlite-integration
*/

define( 'SQLITE_MAIN_FILE', __FILE__ );
if (!defined('SQLITE_MAIN_FILE')) {
define('SQLITE_MAIN_FILE', __FILE__);
}

require_once __DIR__ . '/admin-page.php';
require_once __DIR__ . '/activate.php';
Expand Down
33 changes: 27 additions & 6 deletions src/Project/SiteProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use lucatume\WPBrowser\Exceptions\RuntimeException;
use lucatume\WPBrowser\Extension\BuiltInServerController;
use lucatume\WPBrowser\Extension\ChromeDriverController;
use lucatume\WPBrowser\Template\Wpbrowser;
use lucatume\WPBrowser\Utils\ChromedriverInstaller;
use lucatume\WPBrowser\Utils\Codeception;
use lucatume\WPBrowser\Utils\Filesystem as FS;
Expand Down Expand Up @@ -58,7 +59,7 @@ public function __construct(InputInterface $input, OutputInterface $output, stri
);
}

$suggest = "Make sure you're initializing wp-browser at the root of your site project,".
$suggest = "Make sure you're initializing wp-browser at the root of your site project," .
" and that the directory contains the WordPress files and a wp-config.php file.";

if ($installationState instanceof EmptyDir) {
Expand Down Expand Up @@ -106,7 +107,8 @@ public function setup(): void
}

$contentDir = $this->installation->getContentDir();
Installation::placeSqliteMuPlugin($this->installation->getMuPluginsDir(), $contentDir);
$dropInPath = Installation::placeSqliteMuPlugin($this->installation->getMuPluginsDir(), $contentDir);
Wpbrowser::setDropInPath($dropInPath);

$this->sayInfo('SQLite drop-in placed, installing WordPress ...');
$serverLocalhostPort = Random::openLocalhostPort();
Expand Down Expand Up @@ -152,19 +154,37 @@ public function setup(): void
EOT;

$docRootEnvVar = "%WORDPRESS_ROOT_DIR%";
$docRoot = dirname($this->installation->getWpConfigFilePath());
$siteUrlRelativePath = '';
$wpRootDir = $this->installation->getWpRootDir();
if ($docRoot !== rtrim($wpRootDir, '/')) {
$docRootRelativePath = FS::relativePath($this->workDir, $docRoot);
$docRootEnvVar = '%WORDPRESS_DOCROOT%';
$siteUrlRelativePath = rtrim('/' . ltrim(FS::relativePath($docRoot, $wpRootDir), '/'), '/');
$this->testEnvironment->extraEnvFileContents .= <<<EOT
# The path to the directory that should be served on localhost, the one containing the wp-config.php file.
WORDPRESS_DOCROOT=$docRootRelativePath
EOT;
}

$this->testEnvironment->extensionsEnabled = [
ChromeDriverController::class => [
'port' => "%CHROMEDRIVER_PORT%",
],
BuiltInServerController::class => [
'workers' => 5,
'port' => "%BUILTIN_SERVER_PORT%",
'docroot' => "%WORDPRESS_ROOT_DIR%",
'docroot' => $docRootEnvVar,
'env' => [
'DATABASE_TYPE' => 'sqlite',
'DB_ENGINE' => 'sqlite',
'DB_DIR' => '%codecept_root_dir%' . DIRECTORY_SEPARATOR . $dataDirRelativePath,
'DB_FILE' => 'db.sqlite'
'DB_FILE' => 'db.sqlite',
'WPBROWSER_SITEURL' => '%WORDPRESS_URL%' . $siteUrlRelativePath,
'WPBROWSER_HOMEURL' => '%WORDPRESS_URL%'
]
]

Expand All @@ -174,7 +194,8 @@ public function setup(): void
$this->testEnvironment->customCommands[] = DevInfo::class;
$this->testEnvironment->customCommands[] = DevRestart::class;
$this->testEnvironment->customCommands[] = ChromedriverUpdate::class;
$this->testEnvironment->wpRootDir = '.';
$this->testEnvironment->wpRootDir = FS::relativePath($this->workDir, $wpRootDir) ?: '.';
$this->testEnvironment->wpAdminPath = '/' . ltrim(FS::relativePath($docRoot, $wpRootDir . '/wp-admin'), '/');
$this->testEnvironment->dbUrl = 'sqlite://' . implode(
DIRECTORY_SEPARATOR,
['%codecept_root_dir%', $dataDirRelativePath, 'db.sqlite']
Expand All @@ -193,7 +214,7 @@ public function getTestEnv(): ?TestEnvironment

private function getName(): string
{
return basename(dirname($this->workDir));
return basename($this->workDir);
}

private function scaffoldEndToEndActivationCest(): void
Expand Down
4 changes: 4 additions & 0 deletions src/Project/TestEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class TestEnvironment
* @var string
*/
public $wpDomain = 'wordpress.test';
/**
* @var string
*/
public $wpAdminPath = '/wp-admin';
/**
* @var string
*/
Expand Down
20 changes: 17 additions & 3 deletions src/Template/Wpbrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class Wpbrowser extends Bootstrap
* @var \lucatume\WPBrowser\Project\TestEnvironment|null
*/
private $testEnvironment;
/**
* @var string
*/
private static $dropInPath = '';

/**
* @throws RuntimeException
Expand Down Expand Up @@ -199,6 +203,7 @@ private function createEnvFile(): void
# The URL and domain of the WordPress site used in end-to-end tests.
WORDPRESS_URL={$testEnv->wpUrl}
WORDPRESS_DOMAIN={$testEnv->wpDomain}
WORDPRESS_ADMIN_PATH={$testEnv->wpAdminPath}
# The username and password of the administrator user of the WordPress site used in end-to-end tests.
WORDPRESS_ADMIN_USER={$testEnv->wpAdminUser}
Expand Down Expand Up @@ -241,7 +246,7 @@ private function createEndToEndSuite(ProjectInterface $project): void
url: '%WORDPRESS_URL%'
adminUsername: '%WORDPRESS_ADMIN_USER%'
adminPassword: '%WORDPRESS_ADMIN_PASSWORD%'
adminPath: '/wp-admin'
adminPath: '%WORDPRESS_ADMIN_PATH%'
browser: chrome
host: '%CHROMEDRIVER_HOST%'
port: '%CHROMEDRIVER_PORT%'
Expand Down Expand Up @@ -269,10 +274,10 @@ private function createEndToEndSuite(ProjectInterface $project): void
wpRootFolder: '%WORDPRESS_ROOT_DIR%'
lucatume\WPBrowser\Module\WPLoader:
loadOnly: true
wpRootFolder: "%WORDPRESS_ROOT_DIR%"
wpRootFolder: '%WORDPRESS_ROOT_DIR%'
dbUrl: '%WORDPRESS_DB_URL%'
domain: '%WORDPRESS_DOMAIN%'
EOF;
$this->createSuite('EndToEnd', 'EndToEnd', $suiteConfig);
$bootstrapContents = <<<EOF
Expand Down Expand Up @@ -319,11 +324,20 @@ private function buildProjectFromWorkDir(string $workDir): ProjectInterface
return new SiteProject($this->input, $this->output, $workDir);
}

public static function setDropInPath(string $path): void
{
self::$dropInPath = $path;
}

protected function cleanUpOnFail(): void
{
FS::rrmdir($this->workDir . '/tests');
if (is_file($this->workDir . '/codeception.yml')) {
@unlink($this->workDir . '/codeception.yml');
}

if (self::$dropInPath && file_exists(self::$dropInPath)) {
@unlink(self::$dropInPath);
}
}
}
21 changes: 16 additions & 5 deletions src/WordPress/CodeExecution/InstallAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

use Closure;
use lucatume\WPBrowser\WordPress\FileRequests\FileRequest;
use lucatume\WPBrowser\WordPress\Traits\WordPressChecks;
use lucatume\WPBrowser\WordPress\WPConfigFile;

use function wp_slash;

class InstallAction implements CodeExecutionActionInterface
{
use WordPressChecks;

/**
* @var \lucatume\WPBrowser\WordPress\FileRequests\FileRequest
*/
Expand All @@ -27,7 +32,7 @@ public function __construct(
->setTargetFile($wpRootDir . '/wp-load.php')
->defineConstant('WP_INSTALLING', true)
->defineConstant('MULTISITE', false)
->addPreloadClosure(function () use ($url): void {
->addPreloadClosure(function (): void {
// The `MULTISITE` const might be already defined in the `wp-config.php` file.
// If that is the case, silence the error.
set_error_handler(static function ($errno, $errstr): bool {
Expand All @@ -42,14 +47,20 @@ public function __construct(

// Plug the `wp_mail` function to avoid the sending of emails.
require_once dirname(__DIR__, 3) . '/includes/pluggables/function-wp-mail.php';

if (!defined('WP_SITEURL')) {
define('WP_SITEURL', $url);
}
})
->addAfterLoadClosure(function () use ($title, $adminUser, $adminPassword, $adminEmail) {
return $this->installWordPress($title, $adminUser, $adminPassword, $adminEmail);
});

// Define the `WP_SITEURL` constant if not already defined in the wp-config.php file.
$wpConfigFilePath = $this->findWpConfigFilePath($wpRootDir);
if ($wpConfigFilePath) {
$wpConfigFile = new WPConfigFile($wpRootDir, $wpConfigFilePath);
if (!$wpConfigFile->getConstant('WP_SITEURL')) {
$request->defineConstant('WP_SITEURL', $url);
}
}

$this->request = $request;
}

Expand Down
6 changes: 4 additions & 2 deletions src/WordPress/FileRequests/FileRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,10 @@ public function execute(): array
PreloadFilters::addFilter($hookName, $callback, $priority, $acceptedArgs);
}

// Reveal the errors.
define('WP_DISABLE_FATAL_ERROR_HANDLER', true);
// Reveal the errors: disable the fatal error handler.
PreloadFilters::addFilter('wp_fatal_error_handler_enabled', function () {
return false;
}, 1000);

// Set up the cookie jar.
foreach ($this->cookieJar as $key => $value) {
Expand Down
13 changes: 8 additions & 5 deletions src/WordPress/Installation.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,14 @@ public static function getCleanScaffoldedInstallations(): array
/**
* @throws InstallationException
*/
public static function placeSqliteMuPlugin(string $muPluginsDir, string $contentDir): void
public static function placeSqliteMuPlugin(string $muPluginsDir, string $contentDir): string
{
if (self::hasSqliteDropin($contentDir . '/db.php')) {
return;
$dropinPathname = $contentDir . '/db.php';
if (self::hasSqliteDropin($dropinPathname)) {
return $dropinPathname;
}

if (is_file($contentDir . '/db.php')) {
if (is_file($dropinPathname)) {
throw new InstallationException(
"The db.php file already exists in the $contentDir directory and it's not a SQLite drop-in.",
InstallationException::DB_DROPIN_ALREADY_EXISTS
Expand Down Expand Up @@ -137,7 +138,7 @@ public static function placeSqliteMuPlugin(string $muPluginsDir, string $content
$dbCopyContents
);

if (!file_put_contents($contentDir . '/db.php', $updatedContents, LOCK_EX)) {
if (!file_put_contents($dropinPathname, $updatedContents, LOCK_EX)) {
throw new InstallationException(
"Could not write the SQLite db.php file at $contentDir.",
InstallationException::SQLITE_DROPIN_COPY_FAILED
Expand All @@ -153,6 +154,8 @@ public static function placeSqliteMuPlugin(string $muPluginsDir, string $content
InstallationException::SQLITE_DROPIN_COPY_FAILED
);
}

return $dropinPathname;
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/WordPress/Traits/WordPressChecks.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ private function checkWPRootDir(string $wpRootDir): string
return $wpRootDir;
}


/**
* @return string|false
*/
Expand Down
Loading

0 comments on commit 23edc34

Please sign in to comment.