Skip to content

Commit

Permalink
feat(WPLoader) read content locations from config
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed Aug 23, 2024
1 parent d69450b commit 1f8b901
Show file tree
Hide file tree
Showing 7 changed files with 575 additions and 175 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

- Ensure the `WPLoader` module will initialize correctly when used in `loadOnly` mode not using the `EventDispatcherBridge` extension. (thanks @lxbdr)
- Support loading the `wpdb` class from either the `class-wpdb.php` file or the `wp-db.php` one, supporting older versions of WordPress (thanks @BrianHenryIE)
- Read content, plugins and mu-plugins directories paths from the `WPLoader` configuration parameters correctly.

## [4.2.5] 2024-06-26;

Expand Down
2 changes: 2 additions & 0 deletions docs/modules/WPLoader.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## WPLoader module

// @todo update this

A module to load WordPress and make its code available in tests.

Depending on the value of the `loadOnly` configuration parameter, the module will behave differently:
Expand Down
70 changes: 60 additions & 10 deletions src/Module/WPLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,32 @@ public function _didLoadWordPress(): bool
return $this->didLoadWordPress;
}

/**
* Get the absolute path to the mu-plugins directory.
*
* The value will first look at the `WPMU_PLUGIN_DIR` constant, then the `WP_CONTENT_DIR` configuration parameter,
* and will, finally, look in the default path from the WordPress root directory.
*
* @param string $path
*
* @return string
* @since TBD
*/
public function getMuPluginsFolder(string $path = ''): string
{
/** @var array{WPMU_PLUGIN_DIR?: string, WP_CONTENT_DIR?: string} $config */
$config = $this->config;
$candidates = array_filter([
$config['WPMU_PLUGIN_DIR'] ?? null,
isset($config['WP_CONTENT_DIR']) ? rtrim($config['WP_CONTENT_DIR'], '\\/') . '/mu-plugins' : null,
$this->installation->getMuPluginsDir()
]);
/** @var string $muPluginsDir */
$muPluginsDir = reset($candidates);

return rtrim($muPluginsDir, '\\/') . ($path ? ltrim($path, '\\/') : '/');
}

protected function validateConfig(): void
{
// Coming from required fields, the values are now defined.
Expand Down Expand Up @@ -440,10 +466,10 @@ public function _initialize(): void
$this->installation = new Installation($wpRootDir);
}

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

Expand Down Expand Up @@ -610,8 +636,9 @@ public function _loadWordPress(?bool $loadOnly = null): void
/**
* Returns the absolute path to the plugins directory.
*
* The value will first look at the `WP_PLUGIN_DIR` constant, then the `pluginsFolder` configuration parameter
* and will, finally, look in the default path from the WordPress root directory.
* The value will first look at the `WP_PLUGIN_DIR` constant, then the `pluginsFolder` configuration parameter,
* then the `WP_CONTENT_DIR` configuration parameter, and will, finally, look in the default path from the
* WordPress root directory.
*
* @example
* ```php
Expand All @@ -626,7 +653,18 @@ public function _loadWordPress(?bool $loadOnly = null): void
*/
public function getPluginsFolder(string $path = ''): string
{
return $this->installation->getPluginsDir($path);
/** @var array{pluginsFolder?: string, WP_PLUGIN_DIR?: string,WP_CONTENT_DIR?: string} $config */
$config = $this->config;
$candidates = array_filter([
$config['WP_PLUGIN_DIR'] ?? null,
$config['pluginsFolder'] ?? null,
isset($config['WP_CONTENT_DIR']) ? rtrim($config['WP_CONTENT_DIR'], '\\/') . '/plugins' : null,
$this->installation->getPluginsDir()
]);
/** @var string $pluginDir */
$pluginDir = reset($candidates);

return rtrim($pluginDir, '\\/') . ($path ? ltrim($path, '\\/') : '/');
}

/**
Expand Down Expand Up @@ -856,6 +894,9 @@ private function loadConfigFiles(): void
/**
* Returns the absolute path to the WordPress content directory.
*
* The value will first look at the `WP_CONTENT_DIR` configuration parameter, and will, finally, look in the
* default path from the WordPress root directory.
*
* @example
* ```php
* $content = $this->getContentFolder();
Expand All @@ -869,7 +910,16 @@ private function loadConfigFiles(): void
*/
public function getContentFolder(string $path = ''): string
{
return $this->installation->getContentDir($path);
/** @var array{WP_CONTENT_DIR?: string} $config */
$config = $this->config;
$candidates = array_filter([
$config['WP_CONTENT_DIR'] ?? null,
$this->installation->getContentDir()
]);
/** @var string $contentDir */
$contentDir = reset($candidates);

return rtrim($contentDir, '\\/') . ($path ? ltrim($path, '\\/') : '/');
}

private function getCodeExecutionFactory(): CodeExecutionFactory
Expand Down Expand Up @@ -1083,7 +1133,7 @@ private function activatePluginsTheme(array $plugins): array
wp_cache_delete('alloptions', 'options');

// Do not include external plugins, it would create issues at this stage.
$pluginsDir = $this->installation->getPluginsDir();
$pluginsDir = $this->getPluginsFolder();

return array_values(
array_filter(
Expand Down Expand Up @@ -1125,7 +1175,7 @@ private function muActivatePluginsTheme(array $plugins): array
wp_cache_delete("1::active_sitewide_plugins", 'site-options');

// Do not include external plugins, it would create issues at this stage.
$pluginsDir = $this->installation->getPluginsDir();
$pluginsDir = $this->getPluginsFolder();
$validPlugins = array_values(
array_filter(
$plugins,
Expand Down Expand Up @@ -1168,7 +1218,7 @@ private function includeAllPlugins(array $plugins, bool $isMultisite): void
$activePlugins = [];
}

$pluginsDir = $this->installation->getPluginsDir();
$pluginsDir = $this->getPluginsFolder();

foreach ($plugins as $plugin) {
if (!is_file($pluginsDir . "/$plugin")) {
Expand Down
99 changes: 99 additions & 0 deletions tests/_support/Traits/InstallationMocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace lucatume\WPBrowser\Tests\Traits;

use lucatume\WPBrowser\Utils\Env;
use lucatume\WPBrowser\Utils\Filesystem as FS;

trait InstallationMocks
{
use TmpFilesCleanup;

/**
* @return array{0: string, 1: string}
*/
private function makeMockConfiguredInstallation(): array
{
$dbUser = Env::get('WORDPRESS_DB_USER');
$dbPassword = Env::get('WORDPRESS_DB_PASSWORD');
$dbLocalhostPort = Env::get('WORDPRESS_DB_LOCALHOST_PORT');
$dbName = Env::get('WORDPRESS_DB_NAME');
$wpRootFolder = FS::tmpDir('wploader_', [
'wp-includes' => [
'version.php' => <<< PHP
<?php
\$wp_version = '6.5';
\$wp_db_version = 57155;
\$tinymce_version = '49110-20201110';
\$required_php_version = '7.0.0';
\$required_mysql_version = '5.5.5';
PHP
],
'wp-config.php' => <<< PHP
<?php
define('DB_NAME', '$dbName');
define('DB_USER', '$dbUser');
define('DB_PASSWORD', '$dbPassword');
define('DB_HOST', '127.0.0.1:$dbLocalhostPort');
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
global \$table_prefix;
\$table_prefix = 'wp_';
define('AUTH_KEY', 'auth-key-salt');
define('SECURE_AUTH_KEY', 'secure-auth-key-salt');
define('LOGGED_IN_KEY', 'logged-in-key-salt');
define('NONCE_KEY', 'nonce-key-salt');
define('AUTH_SALT', 'auth-salt');
define('SECURE_AUTH_SALT', 'secure-auth-salt');
define('LOGGED_IN_SALT', 'logged-in-salt');
define('NONCE_SALT', 'nonce-salt');
PHP,
'wp-settings.php' => '<?php',
'wp-load.php' => '<?php do_action("wp_loaded");',
]);
$dbUrl = sprintf(
'mysql://%s:%s@127.0.0.1:%d/%s',
$dbUser,
$dbPassword,
$dbLocalhostPort,
$dbName
);

return [$wpRootFolder, $dbUrl];
}

/**
* @return array{0: string, 1: string}
*/
private function makeMockScaffoldedInstallation(): array
{
$dbUser = Env::get('WORDPRESS_DB_USER');
$dbPassword = Env::get('WORDPRESS_DB_PASSWORD');
$dbLocalhostPort = Env::get('WORDPRESS_DB_LOCALHOST_PORT');
$dbName = Env::get('WORDPRESS_DB_NAME');
$wpRootFolder = FS::tmpDir('wploader_', [
'wp-includes' => [
'version.php' => <<< PHP
<?php
\$wp_version = '6.5';
\$wp_db_version = 57155;
\$tinymce_version = '49110-20201110';
\$required_php_version = '7.0.0';
\$required_mysql_version = '5.5.5';
PHP
],
'wp-settings.php' => '<?php',
'wp-load.php' => '<?php do_action("wp_loaded");',
]);
$dbUrl = sprintf(
'mysql://%s:%s@127.0.0.1:%d/%s',
$dbUser,
$dbPassword,
$dbLocalhostPort,
$dbName
);

return [$wpRootFolder, $dbUrl];
}

}
Loading

0 comments on commit 1f8b901

Please sign in to comment.