Skip to content

Commit

Permalink
fix(WPLoader) make sure absolute paths are supported in WPLoader.conf…
Browse files Browse the repository at this point in the history
…ig.configFile
  • Loading branch information
lucatume committed Mar 30, 2020
1 parent a37b586 commit 139d9a6
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 14 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [unreleased] Unreleased

## [2.3.2] 2020-03-29;

### Fixed

- absolute paths handling in the `configFile` parameter of `WPLoader` configuration

## [2.3.1] 2020-03-29;

### Fixed
Expand Down Expand Up @@ -1241,4 +1247,5 @@ This project adheres to [Semantic Versioning](http://semver.org/).
[2.2.37]: https://github.com/lucatume/wp-browser/compare/2.2.36...2.2.37
[2.3.0]: https://github.com/lucatume/wp-browser/compare/2.2.37...2.3.0
[2.3.1]: https://github.com/lucatume/wp-browser/compare/2.3.0...2.3.1
[unreleased]: https://github.com/lucatume/wp-browser/compare/2.3.1...HEAD
[2.3.2]: https://github.com/lucatume/wp-browser/compare/2.3.1...2.3.2
[unreleased]: https://github.com/lucatume/wp-browser/compare/2.3.2...HEAD
48 changes: 35 additions & 13 deletions src/Codeception/Module/WPLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,19 +390,8 @@ protected function defineGlobals()
*/
protected function loadConfigFile($folder = null)
{
$folder = $folder ?: codecept_root_dir();
$frags = $this->config['configFile'];
foreach ((array)$frags as $frag) {
if (! empty($frag)) {
$configFile = Utils::findHereOrInParent($frag, $folder);
if (! file_exists($configFile)) {
throw new ModuleConfigException(
__CLASS__,
"\nConfig file `{$frag}` could not be found in WordPress root folder or above."
);
}
require_once $configFile;
}
foreach ($this->_getConfigFiles($folder) as $configFile) {
require_once $configFile;
}
}

Expand Down Expand Up @@ -800,4 +789,37 @@ protected function setupFactoryStore()
{
$this->factoryStore = new FactoryStore();
}

/**
* Returns an array of the configuration files specified with the `configFile` parameter of the module configuarion.
*
* @param string|null $folder The start directory to search for configuration files. If not found in the starting
* directory, then files will be searched in the directory parents.
*
* @return array<string> An array of configuration files absolute paths.
*
* @throws ModuleConfigException If a specified configuration file does not exist.
*/
public function _getConfigFiles($folder = null)
{
$folder = $folder ?: codecept_root_dir();

$frags = $this->config['configFile'];
$configFiles = [];

foreach ((array)$frags as $frag) {
if (! empty($frag)) {
$configFile = Utils::findHereOrInParent($frag, $folder);
if (! file_exists($configFile)) {
throw new ModuleConfigException(
__CLASS__,
"\nConfig file `{$frag}` could not be found in WordPress root folder or above."
);
}
$configFiles[] = $configFile;
}
}

return array_unique($configFiles);
}
}
4 changes: 4 additions & 0 deletions src/tad/WPBrowser/Filesystem/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public static function findHereOrInParent(
throw new \InvalidArgumentException('Frag must be a string');
}

if (file_exists($frag)) {
return $frag;
}

if (!is_string($start)) {
throw new \InvalidArgumentException('Start must be a string');
}
Expand Down
38 changes: 38 additions & 0 deletions tests/unit/Codeception/Module/WPLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,42 @@ public function should_correctly_build_paths_when_the_wp_plugin_dir_const_is_def
$this->assertEquals($pluginsRoot->url() . '/plugins', $wpLoader->getPluginsFolder());
$this->assertEquals($pluginsRoot->url() . '/plugins/foo/bar', $wpLoader->getPluginsFolder('foo/bar'));
}

/**
* It should handle absolute path for configFile parameter
*
* @test
*/
public function should_handle_absolute_path_for_config_file_parameter()
{
$configFile = __FILE__;
$this->config['configFile'] = $configFile;

$wpLoader = $this->make_instance();

$this->assertEquals([__FILE__], $wpLoader->_getConfigFiles());
}

/**
* It should handle multiple absolute and relative paths for config files
*
* @test
*/
public function should_handle_multiple_absolute_and_relative_paths_for_config_files()
{
$filesHere = glob(__DIR__ . '/*.php');
$configFiles = [
basename(__FILE__),
reset($filesHere),
__FILE__
];
$this->config['configFile'] = $configFiles;

$wpLoader = $this->make_instance();

$this->assertEquals([
__FILE__,
reset($filesHere)
], $wpLoader->_getConfigFiles(__DIR__));
}
}
12 changes: 12 additions & 0 deletions tests/unit/tad/WPBrowser/Filesystem/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,16 @@ public function it_should_find_files_with_relative_dirnname()
$this->assertEquals($path, Utils::findHereOrInParent('../../foo.php', dirname(__FILE__)));
unlink($path);
}

/**
* It should find absolute file here or in parent
*
* @test
*/
public function should_find_absolute_file_here_or_in_parent()
{
$file = __FILE__;

$this->assertEquals(__FILE__, Utils::findHereOrInParent($file, codecept_output_dir()));
}
}

0 comments on commit 139d9a6

Please sign in to comment.