From 139d9a642a54b198544bd582191b2b929442b7c2 Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Mon, 30 Mar 2020 17:38:54 +0200 Subject: [PATCH] fix(WPLoader) make sure absolute paths are supported in WPLoader.config.configFile --- CHANGELOG.md | 9 +++- src/Codeception/Module/WPLoader.php | 48 ++++++++++++++----- src/tad/WPBrowser/Filesystem/Utils.php | 4 ++ .../unit/Codeception/Module/WPLoaderTest.php | 38 +++++++++++++++ .../tad/WPBrowser/Filesystem/UtilsTest.php | 12 +++++ 5 files changed, 97 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89b2a1e41..4a71ff348 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/src/Codeception/Module/WPLoader.php b/src/Codeception/Module/WPLoader.php index c8e0b69bb..5ee00f071 100644 --- a/src/Codeception/Module/WPLoader.php +++ b/src/Codeception/Module/WPLoader.php @@ -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; } } @@ -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 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); + } } diff --git a/src/tad/WPBrowser/Filesystem/Utils.php b/src/tad/WPBrowser/Filesystem/Utils.php index ebcd0816a..396c870a1 100644 --- a/src/tad/WPBrowser/Filesystem/Utils.php +++ b/src/tad/WPBrowser/Filesystem/Utils.php @@ -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'); } diff --git a/tests/unit/Codeception/Module/WPLoaderTest.php b/tests/unit/Codeception/Module/WPLoaderTest.php index be32b5247..eb8deded2 100644 --- a/tests/unit/Codeception/Module/WPLoaderTest.php +++ b/tests/unit/Codeception/Module/WPLoaderTest.php @@ -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__)); + } } diff --git a/tests/unit/tad/WPBrowser/Filesystem/UtilsTest.php b/tests/unit/tad/WPBrowser/Filesystem/UtilsTest.php index ba7a6164c..d23df60bd 100644 --- a/tests/unit/tad/WPBrowser/Filesystem/UtilsTest.php +++ b/tests/unit/tad/WPBrowser/Filesystem/UtilsTest.php @@ -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())); + } }