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 32692d0
Show file tree
Hide file tree
Showing 5 changed files with 324 additions and 64 deletions.
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
66 changes: 58 additions & 8 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 @@ -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];
}

}
60 changes: 4 additions & 56 deletions tests/unit/lucatume/WPBrowser/Module/WPLoaderLoadOnlyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,15 @@
use Codeception\Lib\ModuleContainer;
use Codeception\Test\Unit;
use lucatume\WPBrowser\Tests\Traits\Fork;
use lucatume\WPBrowser\Tests\Traits\TmpFilesCleanup;
use lucatume\WPBrowser\Utils\Env;
use lucatume\WPBrowser\Utils\Filesystem as FS;
use lucatume\WPBrowser\Tests\Traits\InstallationMocks;

class WPLoaderLoadOnlyTest extends Unit
{
use TmpFilesCleanup;

private function makeMockWordPressInstallation(): 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];
}
use InstallationMocks;

public function testWillLoadWordPressInBeforeSuiteWhenLoadOnlyIsTrue(): void
{
[$wpRootFolder, $dbUrl] = $this->makeMockWordPressInstallation();
[$wpRootFolder, $dbUrl] = $this->makeMockConfiguredInstallation();
$moduleContainer = new ModuleContainer(new Di(), []);
$module = new WPLoader($moduleContainer, [
'dbUrl' => $dbUrl,
Expand Down Expand Up @@ -100,7 +48,7 @@ public function testWillLoadWordPressInBeforeSuiteWhenLoadOnlyIsTrue(): void

public function testWillLoadWordPressInInitializeWhenLoadOnlyIsFalse(): void
{
[$wpRootFolder, $dbUrl] = $this->makeMockWordPressInstallation();
[$wpRootFolder, $dbUrl] = $this->makeMockConfiguredInstallation();
$moduleContainer = new ModuleContainer(new Di(), []);
$module = new WPLoader($moduleContainer, [
'dbUrl' => $dbUrl,
Expand Down
Loading

0 comments on commit 32692d0

Please sign in to comment.