Skip to content

Check is_dir(WP_CONTENT_DIR) or codecept_root_dir(WP_CONTENT_DIR) #772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1,151 changes: 991 additions & 160 deletions CHANGELOG.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/modules/WPLoader.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ When used in this mode, the module supports the following configuration paramete
!!! note

**The order of the modules matters.**
In your suite configuratin file place the `WPDb` module **before** the `WPLoader` one to make sure the `WPDb` module will correctly set up the database fixture before the `WPLoader` modules attempts to load WordPress from it.
In your suite configuration file place the `WPDb` module **before** the `WPLoader` one to make sure the `WPDb` module will correctly set up the database fixture before the `WPLoader` modules attempts to load WordPress from it.

!!! warning

Expand Down
9 changes: 9 additions & 0 deletions src/Module/WPLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,15 @@ protected function validateConfig(): void
);
}

foreach (['WP_CONTENT_DIR', 'WP_PLUGIN_DIR', 'WPMU_PLUGIN_DIR', 'pluginsFolder'] as $pathConst) {
if (!empty($this->config[$pathConst])
&& !is_dir($this->config[$pathConst])
&& is_dir(codecept_root_dir($this->config[$pathConst]))
) {
$this->config[$pathConst] = codecept_root_dir($this->config[$pathConst]);
}
}

parent::validateConfig();
}

Expand Down
Empty file added tests/_data/_content/.gitkeep
Empty file.
Empty file.
Empty file added tests/_data/_plugins/.gitkeep
Empty file.
Empty file.
151 changes: 151 additions & 0 deletions tests/unit/lucatume/WPBrowser/Module/WPLoaderRelativeConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

namespace lucatume\WPBrowser\Module;

use Codeception\Lib\Di;
use Codeception\Lib\ModuleContainer;
use Codeception\Test\Unit;
use lucatume\WPBrowser\Utils\Env;

class WPLoaderRelativeConfigTest extends Unit
{
private string $originalWorkingDirectory;

protected function setUp(): void
{
parent::setUp();
$this->originalWorkingDirectory = getcwd();
}

protected function tearDown(): void
{
chdir($this->originalWorkingDirectory);
parent::tearDown();
}

public function testRelativeContentDir(): void
{
$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');
$dbUrl = sprintf(
'mysql://%s:%s@127.0.0.1:%d/%s',
$dbUser,
$dbPassword,
$dbLocalhostPort,
$dbName
);
$moduleContainer = new ModuleContainer(new Di(), []);

// Change the root to another directory that is not the Codeception root.
chdir(codecept_data_dir('root-dirs/some/nested/dir'));

$module = new WPLoader($moduleContainer, [
'wpRootFolder' => codecept_root_dir('var/wordpress'),
'dbUrl' => $dbUrl,
// Set the content dir to a path that will not resolve relative to the current working directory.
'WP_CONTENT_DIR' => 'tests/_data/_content',
]);

$this->assertEquals(
codecept_root_dir('tests/_data/_content'),
$module->_getConfig('WP_CONTENT_DIR')
);
}

public function testRelativePluginDir(): void
{
$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');
$dbUrl = sprintf(
'mysql://%s:%s@127.0.0.1:%d/%s',
$dbUser,
$dbPassword,
$dbLocalhostPort,
$dbName
);
$moduleContainer = new ModuleContainer(new Di(), []);

// Change the root to another directory that is not the Codeception root.
chdir(codecept_data_dir('root-dirs/some/nested/dir'));

$module = new WPLoader($moduleContainer, [
'wpRootFolder' => codecept_root_dir('var/wordpress'),
'dbUrl' => $dbUrl,
// Set the plugin dir to a path that will not resolve relative to the current working directory.
'WP_PLUGIN_DIR' => 'tests/_data/_plugins',
]);

$this->assertEquals(
codecept_root_dir('tests/_data/_plugins'),
$module->_getConfig('WP_PLUGIN_DIR')
);
}



public function testRelativeMuPluginDir(): void
{
$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');
$dbUrl = sprintf(
'mysql://%s:%s@127.0.0.1:%d/%s',
$dbUser,
$dbPassword,
$dbLocalhostPort,
$dbName
);
$moduleContainer = new ModuleContainer(new Di(), []);

// Change the root to another directory that is not the Codeception root.
chdir(codecept_data_dir('root-dirs/some/nested/dir'));

$module = new WPLoader($moduleContainer, [
'wpRootFolder' => codecept_root_dir('var/wordpress'),
'dbUrl' => $dbUrl,
// Set the mu-plugin dir to a path that will not resolve relative to the current working directory.
'WPMU_PLUGIN_DIR' => 'tests/_data/_mu-plugins',
]);

$this->assertEquals(
codecept_root_dir('tests/_data/_mu-plugins'),
$module->_getConfig('WPMU_PLUGIN_DIR')
);
}

public function testRelativePluginsFolder(): void
{
$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');
$dbUrl = sprintf(
'mysql://%s:%s@127.0.0.1:%d/%s',
$dbUser,
$dbPassword,
$dbLocalhostPort,
$dbName
);
$moduleContainer = new ModuleContainer(new Di(), []);

// Change the root to another directory that is not the Codeception root.
chdir(codecept_data_dir('root-dirs/some/nested/dir'));

$module = new WPLoader($moduleContainer, [
'wpRootFolder' => codecept_root_dir('var/wordpress'),
'dbUrl' => $dbUrl,
// Set the plugin dir to a path that will not resolve relative to the current working directory.
'pluginsFolder' => 'tests/_data/_plugins',
]);

$this->assertEquals(
codecept_root_dir('tests/_data/_plugins'),
$module->_getConfig('pluginsFolder')
);
}
}