From afa8282029049ad84ba26f9d81be1f062ffffa13 Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Mon, 20 Nov 2023 15:08:28 +0100 Subject: [PATCH 1/3] feat(WPLoader) add support for activatePluginsSilently --- codeception.dist.yml | 3 + src/Codeception/Module/WPLoader.php | 60 +++-- src/includes/bootstrap.php | 1 + src/includes/isolated-install.php | 9 +- tests/_bootstrap.php | 38 ++- tests/_data/plugins/doing-it-right/plugin.php | 13 + .../_data/plugins/doing-it-wrong-1/plugin.php | 15 ++ .../_data/plugins/doing-it-wrong-2/plugin.php | 16 ++ .../Wploader_plugin_silent_activation.php | 10 + ...ploader_plugin_silent_activationTester.php | 26 ++ ..._plugin_silent_activationTesterActions.php | 254 ++++++++++++++++++ ...sing_blogs_entry_for_site__0.snapshot.json | 3 + ...ith_available_wpdb_global__0.snapshot.json | 3 + ...pecified_from_wpdb_global__0.snapshot.json | 3 + ...late_and_stylesheet_files__0.snapshot.json | 3 + ...est_with_inactive_plugins__0.snapshot.json | 3 + ...th_missing_siteurl_option__0.snapshot.json | 3 + ..._plugins_dir_constant_set__0.snapshot.json | 3 + ..._in_blogs_for_blog_domain__0.snapshot.json | 3 + ...with_working_installation__0.snapshot.json | 3 + ...isite_subdir_installation__0.snapshot.json | 3 + ...te_subdomain_installation__0.snapshot.json | 3 + ...er_plugin_silent_activation.suite.dist.yml | 30 +++ .../DoingItRightTest.php | 23 ++ .../DoingItWrongPluginOneTest.php | 31 +++ .../DoingItWrongPluginTwoTest.php | 31 +++ 26 files changed, 562 insertions(+), 31 deletions(-) create mode 100644 tests/_data/plugins/doing-it-right/plugin.php create mode 100644 tests/_data/plugins/doing-it-wrong-1/plugin.php create mode 100644 tests/_data/plugins/doing-it-wrong-2/plugin.php create mode 100644 tests/_support/Helper/Wploader_plugin_silent_activation.php create mode 100644 tests/_support/Wploader_plugin_silent_activationTester.php create mode 100644 tests/_support/_generated/Wploader_plugin_silent_activationTesterActions.php create mode 100644 tests/wploader_plugin_silent_activation.suite.dist.yml create mode 100644 tests/wploader_plugin_silent_activation/DoingItRightTest.php create mode 100644 tests/wploader_plugin_silent_activation/DoingItWrongPluginOneTest.php create mode 100644 tests/wploader_plugin_silent_activation/DoingItWrongPluginTwoTest.php diff --git a/codeception.dist.yml b/codeception.dist.yml index 2ade96668..7fc117f9d 100644 --- a/codeception.dist.yml +++ b/codeception.dist.yml @@ -29,6 +29,9 @@ extensions: 'tests/_data/themes/dummy': '%WORDPRESS_ROOT_DIR%/wp-content/themes/dummy' 'tests/_data/themes/isolated': '%WORDPRESS_ROOT_DIR%/wp-content/themes/isolated' 'tests/_data/plugins/mu-plugin-1': '%WORDPRESS_ROOT_DIR%/wp-content/plugins/mu-plugin-1' + 'tests/_data/plugins/doing-it-right': '%WORDPRESS_ROOT_DIR%/wp-content/plugins/doing-it-right' + 'tests/_data/plugins/doing-it-wrong-1': '%WORDPRESS_ROOT_DIR%/wp-content/plugins/doing-it-wrong-1' + 'tests/_data/plugins/doing-it-wrong-2': '%WORDPRESS_ROOT_DIR%/wp-content/plugins/doing-it-wrong-2' 'tests/_data/plugins/test': '%WORDPRESS_ROOT_DIR%/wp-content/plugins/test' 'tests/_data/plugins/isolated-test-plugin': '%WORDPRESS_ROOT_DIR%/wp-content/plugins/isolated-test-plugin' 'tests/_data/plugins/isolated-test-plugin-two': '%WORDPRESS_ROOT_DIR%/wp-content/plugins/isolated-test-plugin-two' diff --git a/src/Codeception/Module/WPLoader.php b/src/Codeception/Module/WPLoader.php index 13566b098..047df309c 100644 --- a/src/Codeception/Module/WPLoader.php +++ b/src/Codeception/Module/WPLoader.php @@ -138,6 +138,10 @@ class WPLoader extends Module * activated calling the `activate_{$plugin}` before any test case runs and * after mu-plugins have been loaded; these should be defined in the * `folder/plugin-file.php` format. + * activatePlugins - array, def. `[]`, a list of plugins that should be + * silently activated calling the `activate_{$plugin}` before any test case runs and + * after mu-plugins have been loaded; these should be defined in the + * `folder/plugin-file.php` format. * bootstrapActions - array, def. `[]`, a list of actions that should be * called after before any test case runs. * skipPluggables - bool, def. `false`, if set to `true` will skip the @@ -148,27 +152,28 @@ class WPLoader extends Module */ protected $config = [ - 'loadOnly' => false, - 'isolatedInstall' => true, - 'installationTableHandling' => 'empty', - 'wpDebug' => true, - 'multisite' => false, - 'skipPluggables' => false, - 'dbCharset' => 'utf8', - 'dbCollate' => '', - 'tablePrefix' => 'wptests_', - 'domain' => 'example.org', - 'adminEmail' => 'admin@example.org', - 'title' => 'Test Blog', - 'phpBinary' => 'php', - 'language' => '', - 'configFile' => '', - 'contentFolder' => '', - 'pluginsFolder' => '', - 'plugins' => '', - 'activatePlugins' => '', - 'bootstrapActions' => '', - 'theme' => '', + 'loadOnly' => false, + 'isolatedInstall' => true, + 'installationTableHandling' => 'empty', + 'wpDebug' => true, + 'multisite' => false, + 'skipPluggables' => false, + 'dbCharset' => 'utf8', + 'dbCollate' => '', + 'tablePrefix' => 'wptests_', + 'domain' => 'example.org', + 'adminEmail' => 'admin@example.org', + 'title' => 'Test Blog', + 'phpBinary' => 'php', + 'language' => '', + 'configFile' => '', + 'contentFolder' => '', + 'pluginsFolder' => '', + 'plugins' => [], + 'activatePlugins' => [], + 'activatePluginsSilently' => [], + 'bootstrapActions' => '', + 'theme' => '', ]; /** @@ -1116,10 +1121,13 @@ public function getContentFolder($path = '') */ protected function getInstallationConfiguration() { - return new Configuration([ - 'tablesHandling' => isset($this->config['installationTableHandling']) ? - $this->config['installationTableHandling'] - : 'empty' - ]); + return new Configuration( [ + 'tablesHandling' => isset( $this->config['installationTableHandling'] ) ? + $this->config['installationTableHandling'] + : 'empty', + 'activatePluginsSilently' => isset( $this->config['activatePluginsSilently'] ) ? + (array) $this->config['activatePluginsSilently'] + : [] + ] ); } } diff --git a/src/includes/bootstrap.php b/src/includes/bootstrap.php index e9b8ab2fa..58646812e 100644 --- a/src/includes/bootstrap.php +++ b/src/includes/bootstrap.php @@ -114,6 +114,7 @@ 'WPLANG' => WPLANG, ], 'tablesHandling' => $installationConfiguration->get('tablesHandling','empty'), + 'activatePluginsSilently' => $installationConfiguration->get('activatePluginsSilently', []), ]; $dirConstants = [ diff --git a/src/includes/isolated-install.php b/src/includes/isolated-install.php index aa9ab553d..0d4d7c070 100644 --- a/src/includes/isolated-install.php +++ b/src/includes/isolated-install.php @@ -192,9 +192,13 @@ $current_site->blog_id = 1; } +$activatePluginsSilently = isset( $configuration['activatePluginsSilently'] ) ? + $configuration['activatePluginsSilently'] + : []; + // finally activate the plugins that should be activated if (!empty($activePlugins)) { - $activePlugins = array_unique($activePlugins); + $activePlugins = array_values( array_unique( array_merge( $activePlugins, $activatePluginsSilently ) ) ); if ($multisite) { require(ABSPATH . WPINC . '/class-wp-site-query.php'); @@ -204,8 +208,9 @@ } foreach ($activePlugins as $plugin) { + $silent = in_array($plugin, $activatePluginsSilently,true); printf("\n%sctivating plugin [%s]...", $multisite ? 'Network a' : 'A', $plugin); - $activated = activate_plugin($plugin, null, $multisite, false); + $activated = activate_plugin($plugin, null, $multisite, $silent); if (is_wp_error($activated)) { echo $activated->get_error_message(); diff --git a/tests/_bootstrap.php b/tests/_bootstrap.php index 0e7e3ed85..218a63156 100644 --- a/tests/_bootstrap.php +++ b/tests/_bootstrap.php @@ -1,15 +1,47 @@ getSuite()->getName(); +// +// if ( $suiteName !== 'wploader_plugin_silent_activation' ) { +// return; +// } +// +// $wpRootDir = realpath( getenv( 'WORDPRESS_ROOT_DIR' ) ); +// +// if ( ! is_dir( $wpRootDir ) ) { +// throw new \RuntimeException( "The WORDPRESS_ROOT_DIR is not a valid directory." ); +// } +// +// $wpRootDir = rtrim( $wpRootDir, '/\\' ); +// +// foreach ( +// [ +// codecept_data_dir( 'plugins/doing-it-wrong-1' ) => $wpRootDir . '/wp-content/plugins/doing-it-wrong-1', +// codecept_data_dir( 'plugins/doing-it-wrong-2' ) => $wpRootDir . '/wp-content/plugins/doing-it-wrong-2', +// ] as $source => $destination +// ) { +// if ( ! copy( +// $source, +// $destination +// ) ) { +// throw new \RuntimeException( "Could not copy the plugin to the WordPress plugins directory." ); +// } +// } +//} ); diff --git a/tests/_data/plugins/doing-it-right/plugin.php b/tests/_data/plugins/doing-it-right/plugin.php new file mode 100644 index 000000000..1dae79ca4 --- /dev/null +++ b/tests/_data/plugins/doing-it-right/plugin.php @@ -0,0 +1,13 @@ +getPluginsFolder(); + * $hello = $this->getPluginsFolder('hello.php'); + * ``` + * + * @param string $path A relative path to append to te plugins directory absolute path. + * + * @return string The absolute path to the `pluginsFolder` path or the same with a relative path appended if `$path` + * is provided. + * + * @throws ModuleConfigException If the path to the plugins folder does not exist. + * @see \Codeception\Module\WPLoader::getPluginsFolder() + */ + public function getPluginsFolder($path = "") { + return $this->getScenario()->runStep(new \Codeception\Step\Action('getPluginsFolder', func_get_args())); + } + + + /** + * [!] Method is generated. Documentation taken from corresponding module. + * + * Accessor method to get the object storing the factories for things. + * This methods gives access to the same factories provided by the + * [Core test suite](https://make.wordpress.org/core/handbook/testing/automated-testing/writing-phpunit-tests/). + * + * @return FactoryStore A factory store, proxy to get hold of the Core suite object + * factories. + * + * @example + * ```php + * $postId = $I->factory()->post->create(); + * $userId = $I->factory()->user->create(['role' => 'administrator']); + * ``` + * + * @link https://make.wordpress.org/core/handbook/testing/automated-testing/writing-phpunit-tests/ + * @see \Codeception\Module\WPLoader::factory() + */ + public function factory() { + return $this->getScenario()->runStep(new \Codeception\Step\Action('factory', func_get_args())); + } + + + /** + * [!] Method is generated. Documentation taken from corresponding module. + * + * Returns the absolute path to the WordPress content directory. + * + * @example + * ```php + * $content = $this->getContentFolder(); + * $themes = $this->getContentFolder('themes'); + * $twentytwenty = $this->getContentFolder('themes/twentytwenty'); + * ``` + * + * @param string $path An optional path to append to the content directory absolute path. + * + * @return string The content directory absolute path, or a path in it. + * + * @throws ModuleConfigException If the path to the content directory cannot be resolved. + * @see \Codeception\Module\WPLoader::getContentFolder() + */ + public function getContentFolder($path = "") { + return $this->getScenario()->runStep(new \Codeception\Step\Action('getContentFolder', func_get_args())); + } + + + /** + * [!] Method is generated. Documentation taken from corresponding module. + * + * Starts the debug of all WordPress filters and actions. + * + * The method hook on `all` filters and actions to debug their value. + * + * @example + * ```php + * // Start debugging all WordPress filters and action final and initial values. + * $this->startWpFiltersDebug(); + * + * // Run some code firing filters and debug them. + * + * // Stop debugging all WordPress filters and action final and initial values. + * $this->stopWpFiltersDebug(); + * ``` + * + * @param callable|null $format A callback function to format the arguments debug output; the callback will receive + * the array of arguments as input. + * + * @return void + * @see \Codeception\Module\WPLoader::startWpFiltersDebug() + */ + public function startWpFiltersDebug(?callable $format = NULL) { + return $this->getScenario()->runStep(new \Codeception\Step\Action('startWpFiltersDebug', func_get_args())); + } + + + /** + * [!] Method is generated. Documentation taken from corresponding module. + * + * Stops the debug of all WordPress filters and actions. + * + * @example + * ```php + * // Start debugging all WordPress filters and action final and initial values. + * $this->startWpFiltersDebug(); + * + * // Run some code firing filters and debug them. + * + * // Stop debugging all WordPress filters and action final and initial values. + * $this->stopWpFiltersDebug(); + * ``` + * + * @return void + * @see \Codeception\Module\WPLoader::stopWpFiltersDebug() + */ + public function stopWpFiltersDebug() { + return $this->getScenario()->runStep(new \Codeception\Step\Action('stopWpFiltersDebug', func_get_args())); + } + + + /** + * [!] Method is generated. Documentation taken from corresponding module. + * + * Debugs a single WordPress filter initial call using Codeception debug functions. + * + * The output will show following the selected output verbosity (`--debug` and `-vvv` CLI options). + * + * @example + * ```php + * // Start debugging all WordPress filters initial value. + * add_filter('all', [$this,'debugWpFilterInitial']); + * + * // Run some code firing filters and debug them. + * + * // Stop debugging all WordPress filters initial value. + * remove_filter('all', [$this,'debugWpFilterInitial']); + * ``` + * + * @param mixed ...$args The filter call arguments. + * + * @return mixed The filter input value, unchanged. + * @see \Codeception\Module\WPLoader::debugWpFilterInitial() + */ + public function debugWpFilterInitial($args = null) { + return $this->getScenario()->runStep(new \Codeception\Step\Action('debugWpFilterInitial', func_get_args())); + } + + + /** + * [!] Method is generated. Documentation taken from corresponding module. + * + * Debugs a single WordPress filter final call using Codeception debug functions. + * + * The output will show following the selected output verbosity (`--debug` and `-vvv` CLI options). + * + * @example + * ```php + * // Start debugging all WordPress filters final value. + * add_filter('all', [$this,'debugWpFilterFinal']); + * + * // Run some code firing filters and debug them. + * + * // Stop debugging all WordPress filters final value. + * remove_filter('all', [$this,'debugWpFilterFinal']); + * ``` + * + * @param mixed ...$args The filter call arguments. + * + * @return mixed The filter input value, unchanged. + * @see \Codeception\Module\WPLoader::debugWpFilterFinal() + */ + public function debugWpFilterFinal($args = null) { + return $this->getScenario()->runStep(new \Codeception\Step\Action('debugWpFilterFinal', func_get_args())); + } + + + /** + * [!] Method is generated. Documentation taken from corresponding module. + * + * Debugs a single WordPress action initial call using Codeception debug functions. + * + * The output will show following the selected output verbosity (`--debug` and `-vvv` CLI options). + * + * @example + * ```php + * // Start debugging all WordPress actions initial value. + * add_action('all', [$this,'debugWpActionInitial']); + * + * // Run some code firing actions and debug them. + * + * // Stop debugging all WordPress actions initial value. + * remove_action('all', [$this,'debugWpActionInitial']); + * ``` + * + * @param mixed ...$args The action call arguments. + * + * @return void + * @see \Codeception\Module\WPLoader::debugWpActionInitial() + */ + public function debugWpActionInitial($args = null) { + return $this->getScenario()->runStep(new \Codeception\Step\Action('debugWpActionInitial', func_get_args())); + } + + + /** + * [!] Method is generated. Documentation taken from corresponding module. + * + * Debugs a single WordPress action final call using Codeception debug functions. + * + * The output will show following the selected output verbosity (`--debug` and `-vvv` CLI options). + * + * @example + * ```php + * // Start debugging all WordPress actions final value. + * add_action('all', [$this,'debugWpActionFinal']); + * + * // Run some code firing actions and debug them. + * + * // Stop debugging all WordPress actions final value. + * remove_action('all', [$this,'debugWpActionFinal']); + * ``` + * + * @param mixed ...$args The action call arguments. + * + * @return void + * @see \Codeception\Module\WPLoader::debugWpActionFinal() + */ + public function debugWpActionFinal($args = null) { + return $this->getScenario()->runStep(new \Codeception\Step\Action('debugWpActionFinal', func_get_args())); + } +} diff --git a/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_multisite_with_missing_blogs_entry_for_site__0.snapshot.json b/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_multisite_with_missing_blogs_entry_for_site__0.snapshot.json index 927792c61..097a74cfd 100644 --- a/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_multisite_with_missing_blogs_entry_for_site__0.snapshot.json +++ b/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_multisite_with_missing_blogs_entry_for_site__0.snapshot.json @@ -32,6 +32,9 @@ "Must-use plugins health-check:": "mu-plugins directory (wp-content/mu-plugins) does not exist.", "Plugins health-check:": { "Inactive plugin [akismet]": "wp-content/plugins/akismet", + "Inactive plugin [doing-it-right]": "wp-content/plugins/doing-it-right", + "Inactive plugin [doing-it-wrong-1]": "wp-content/plugins/doing-it-wrong-1", + "Inactive plugin [doing-it-wrong-2]": "wp-content/plugins/doing-it-wrong-2", "Inactive plugin [isolated-test-plugin-two]": "wp-content/plugins/isolated-test-plugin-two", "Inactive plugin [isolated-test-plugin]": "wp-content/plugins/isolated-test-plugin", "Inactive plugin [mu-plugin-1]": "wp-content/plugins/mu-plugin-1", diff --git a/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_available_wpdb_global__0.snapshot.json b/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_available_wpdb_global__0.snapshot.json index 867c3ac5a..2bbb81b42 100644 --- a/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_available_wpdb_global__0.snapshot.json +++ b/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_available_wpdb_global__0.snapshot.json @@ -33,6 +33,9 @@ "Plugins health-check:": { "Active plugin [airplane-mode/airplane-mode.php]": "file [wp-content/plugins/airplane-mode/airplane-mode.php] does not exist.", "Inactive plugin [akismet]": "wp-content/plugins/akismet", + "Inactive plugin [doing-it-right]": "wp-content/plugins/doing-it-right", + "Inactive plugin [doing-it-wrong-1]": "wp-content/plugins/doing-it-wrong-1", + "Inactive plugin [doing-it-wrong-2]": "wp-content/plugins/doing-it-wrong-2", "Inactive plugin [isolated-test-plugin-two]": "wp-content/plugins/isolated-test-plugin-two", "Inactive plugin [isolated-test-plugin]": "wp-content/plugins/isolated-test-plugin", "Inactive plugin [mu-plugin-1]": "wp-content/plugins/mu-plugin-1", diff --git a/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_diff_set_of_tables_specified_from_wpdb_global__0.snapshot.json b/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_diff_set_of_tables_specified_from_wpdb_global__0.snapshot.json index dc9f464b8..5a539f733 100644 --- a/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_diff_set_of_tables_specified_from_wpdb_global__0.snapshot.json +++ b/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_diff_set_of_tables_specified_from_wpdb_global__0.snapshot.json @@ -33,6 +33,9 @@ "Plugins health-check:": { "Active plugin [airplane-mode/airplane-mode.php]": "file [wp-content/plugins/airplane-mode/airplane-mode.php] does not exist.", "Inactive plugin [akismet]": "wp-content/plugins/akismet", + "Inactive plugin [doing-it-right]": "wp-content/plugins/doing-it-right", + "Inactive plugin [doing-it-wrong-1]": "wp-content/plugins/doing-it-wrong-1", + "Inactive plugin [doing-it-wrong-2]": "wp-content/plugins/doing-it-wrong-2", "Inactive plugin [isolated-test-plugin-two]": "wp-content/plugins/isolated-test-plugin-two", "Inactive plugin [isolated-test-plugin]": "wp-content/plugins/isolated-test-plugin", "Inactive plugin [mu-plugin-1]": "wp-content/plugins/mu-plugin-1", diff --git a/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_empty_and_missing_template_and_stylesheet_files__0.snapshot.json b/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_empty_and_missing_template_and_stylesheet_files__0.snapshot.json index dcf14bb64..d7b1e5272 100644 --- a/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_empty_and_missing_template_and_stylesheet_files__0.snapshot.json +++ b/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_empty_and_missing_template_and_stylesheet_files__0.snapshot.json @@ -34,6 +34,9 @@ "Active plugin [one/one.php]": "file [wp-content/plugins/one/one.php] does not exist.", "Active plugin [two/two.php]": "file [wp-content/plugins/two/two.php] does not exist.", "Inactive plugin [akismet]": "wp-content/plugins/akismet", + "Inactive plugin [doing-it-right]": "wp-content/plugins/doing-it-right", + "Inactive plugin [doing-it-wrong-1]": "wp-content/plugins/doing-it-wrong-1", + "Inactive plugin [doing-it-wrong-2]": "wp-content/plugins/doing-it-wrong-2", "Inactive plugin [isolated-test-plugin-two]": "wp-content/plugins/isolated-test-plugin-two", "Inactive plugin [isolated-test-plugin]": "wp-content/plugins/isolated-test-plugin", "Inactive plugin [mu-plugin-1]": "wp-content/plugins/mu-plugin-1", diff --git a/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_inactive_plugins__0.snapshot.json b/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_inactive_plugins__0.snapshot.json index 79b35a6d0..2fccaab9c 100644 --- a/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_inactive_plugins__0.snapshot.json +++ b/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_inactive_plugins__0.snapshot.json @@ -31,6 +31,9 @@ }, "Must-use plugins health-check:": "mu-plugins directory (wp-content/mu-plugins) does not exist.", "Plugins health-check:": { + "Inactive plugin [doing-it-right]": "../../tests/_data/plugins/doing-it-right", + "Inactive plugin [doing-it-wrong-1]": "../../tests/_data/plugins/doing-it-wrong-1", + "Inactive plugin [doing-it-wrong-2]": "../../tests/_data/plugins/doing-it-wrong-2", "Inactive plugin [isolated-test-plugin-two]": "../../tests/_data/plugins/isolated-test-plugin-two", "Inactive plugin [isolated-test-plugin]": "../../tests/_data/plugins/isolated-test-plugin", "Inactive plugin [mu-plugin-1]": "../../tests/_data/plugins/mu-plugin-1", diff --git a/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_missing_siteurl_option__0.snapshot.json b/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_missing_siteurl_option__0.snapshot.json index 6fce6a284..41fcdae51 100644 --- a/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_missing_siteurl_option__0.snapshot.json +++ b/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_missing_siteurl_option__0.snapshot.json @@ -32,6 +32,9 @@ "Must-use plugins health-check:": "mu-plugins directory (wp-content/mu-plugins) does not exist.", "Plugins health-check:": { "Inactive plugin [akismet]": "wp-content/plugins/akismet", + "Inactive plugin [doing-it-right]": "wp-content/plugins/doing-it-right", + "Inactive plugin [doing-it-wrong-1]": "wp-content/plugins/doing-it-wrong-1", + "Inactive plugin [doing-it-wrong-2]": "wp-content/plugins/doing-it-wrong-2", "Inactive plugin [isolated-test-plugin-two]": "wp-content/plugins/isolated-test-plugin-two", "Inactive plugin [isolated-test-plugin]": "wp-content/plugins/isolated-test-plugin", "Inactive plugin [mu-plugin-1]": "wp-content/plugins/mu-plugin-1", diff --git a/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_mu_plugins_dir_constant_set__0.snapshot.json b/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_mu_plugins_dir_constant_set__0.snapshot.json index 94cf71efc..4448e870f 100644 --- a/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_mu_plugins_dir_constant_set__0.snapshot.json +++ b/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_mu_plugins_dir_constant_set__0.snapshot.json @@ -33,6 +33,9 @@ "Plugins health-check:": { "Active plugin [airplane-mode/airplane-mode.php]": "file [wp-content/plugins/airplane-mode/airplane-mode.php] does not exist.", "Inactive plugin [akismet]": "wp-content/plugins/akismet", + "Inactive plugin [doing-it-right]": "wp-content/plugins/doing-it-right", + "Inactive plugin [doing-it-wrong-1]": "wp-content/plugins/doing-it-wrong-1", + "Inactive plugin [doing-it-wrong-2]": "wp-content/plugins/doing-it-wrong-2", "Inactive plugin [isolated-test-plugin-two]": "wp-content/plugins/isolated-test-plugin-two", "Inactive plugin [isolated-test-plugin]": "wp-content/plugins/isolated-test-plugin", "Inactive plugin [mu-plugin-1]": "wp-content/plugins/mu-plugin-1", diff --git a/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_no_results_in_blogs_for_blog_domain__0.snapshot.json b/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_no_results_in_blogs_for_blog_domain__0.snapshot.json index 54054dbc2..936a2d86b 100644 --- a/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_no_results_in_blogs_for_blog_domain__0.snapshot.json +++ b/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_no_results_in_blogs_for_blog_domain__0.snapshot.json @@ -32,6 +32,9 @@ "Must-use plugins health-check:": "mu-plugins directory (wp-content/mu-plugins) does not exist.", "Plugins health-check:": { "Inactive plugin [akismet]": "wp-content/plugins/akismet", + "Inactive plugin [doing-it-right]": "wp-content/plugins/doing-it-right", + "Inactive plugin [doing-it-wrong-1]": "wp-content/plugins/doing-it-wrong-1", + "Inactive plugin [doing-it-wrong-2]": "wp-content/plugins/doing-it-wrong-2", "Inactive plugin [isolated-test-plugin-two]": "wp-content/plugins/isolated-test-plugin-two", "Inactive plugin [isolated-test-plugin]": "wp-content/plugins/isolated-test-plugin", "Inactive plugin [mu-plugin-1]": "wp-content/plugins/mu-plugin-1", diff --git a/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_working_installation__0.snapshot.json b/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_working_installation__0.snapshot.json index 7013031a9..28450ae6b 100644 --- a/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_working_installation__0.snapshot.json +++ b/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_working_installation__0.snapshot.json @@ -33,6 +33,9 @@ "Plugins health-check:": { "Active plugin [airplane-mode/airplane-mode.php]": "file [wp-content/plugins/airplane-mode/airplane-mode.php] does not exist.", "Inactive plugin [akismet]": "wp-content/plugins/akismet", + "Inactive plugin [doing-it-right]": "wp-content/plugins/doing-it-right", + "Inactive plugin [doing-it-wrong-1]": "wp-content/plugins/doing-it-wrong-1", + "Inactive plugin [doing-it-wrong-2]": "wp-content/plugins/doing-it-wrong-2", "Inactive plugin [isolated-test-plugin-two]": "wp-content/plugins/isolated-test-plugin-two", "Inactive plugin [isolated-test-plugin]": "wp-content/plugins/isolated-test-plugin", "Inactive plugin [mu-plugin-1]": "wp-content/plugins/mu-plugin-1", diff --git a/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_working_multisite_subdir_installation__0.snapshot.json b/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_working_multisite_subdir_installation__0.snapshot.json index ace79169f..a9e64499c 100644 --- a/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_working_multisite_subdir_installation__0.snapshot.json +++ b/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_working_multisite_subdir_installation__0.snapshot.json @@ -32,6 +32,9 @@ "Must-use plugins health-check:": "mu-plugins directory (wp-content/mu-plugins) does not exist.", "Plugins health-check:": { "Inactive plugin [akismet]": "wp-content/plugins/akismet", + "Inactive plugin [doing-it-right]": "wp-content/plugins/doing-it-right", + "Inactive plugin [doing-it-wrong-1]": "wp-content/plugins/doing-it-wrong-1", + "Inactive plugin [doing-it-wrong-2]": "wp-content/plugins/doing-it-wrong-2", "Inactive plugin [isolated-test-plugin-two]": "wp-content/plugins/isolated-test-plugin-two", "Inactive plugin [isolated-test-plugin]": "wp-content/plugins/isolated-test-plugin", "Inactive plugin [mu-plugin-1]": "wp-content/plugins/mu-plugin-1", diff --git a/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_working_multisite_subdomain_installation__0.snapshot.json b/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_working_multisite_subdomain_installation__0.snapshot.json index b35bbd620..d48719d01 100644 --- a/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_working_multisite_subdomain_installation__0.snapshot.json +++ b/tests/dbunit/tad/WPBrowser/Module/Support/__snapshots__/WPHealthcheckTest__test_with_working_multisite_subdomain_installation__0.snapshot.json @@ -32,6 +32,9 @@ "Must-use plugins health-check:": "mu-plugins directory (wp-content/mu-plugins) does not exist.", "Plugins health-check:": { "Inactive plugin [akismet]": "wp-content/plugins/akismet", + "Inactive plugin [doing-it-right]": "wp-content/plugins/doing-it-right", + "Inactive plugin [doing-it-wrong-1]": "wp-content/plugins/doing-it-wrong-1", + "Inactive plugin [doing-it-wrong-2]": "wp-content/plugins/doing-it-wrong-2", "Inactive plugin [isolated-test-plugin-two]": "wp-content/plugins/isolated-test-plugin-two", "Inactive plugin [isolated-test-plugin]": "wp-content/plugins/isolated-test-plugin", "Inactive plugin [mu-plugin-1]": "wp-content/plugins/mu-plugin-1", diff --git a/tests/wploader_plugin_silent_activation.suite.dist.yml b/tests/wploader_plugin_silent_activation.suite.dist.yml new file mode 100644 index 000000000..57ab670a0 --- /dev/null +++ b/tests/wploader_plugin_silent_activation.suite.dist.yml @@ -0,0 +1,30 @@ +actor: Wploader_plugin_silent_activationTester +modules: + enabled: + - WPLoader + config: + WPLoader: + wpRootFolder: '%WORDPRESS_ROOT_DIR%' + dbName: '%WORDPRESS_DB_NAME%' + dbHost: '%WORDPRESS_DB_HOST%' + dbUser: '%WORDPRESS_DB_USER%' + dbPassword: '%WORDPRESS_DB_PASSWORD%' + wpDebug: true + tablePrefix: '%WORDPRESS_TABLE_PREFIX%' + domain: '%WP_DOMAIN%' + adminEmail: 'admin@%WP_DOMAIN%' + title: Test + configFile: '' + theme: dummy + plugins: + - doing-it-right/plugin.php + - doing-it-wrong-1/plugin.php + - doing-it-wrong-2/plugin.php + activateplugins: + - doing-it-right/plugin.php + - doing-it-wrong-1/plugin.php + activatePluginsSilently: + - doing-it-wrong-1/plugin.php + - doing-it-wrong-2/plugin.php + installationTableHandling: drop + booststrapActions: [] diff --git a/tests/wploader_plugin_silent_activation/DoingItRightTest.php b/tests/wploader_plugin_silent_activation/DoingItRightTest.php new file mode 100644 index 000000000..bab65c350 --- /dev/null +++ b/tests/wploader_plugin_silent_activation/DoingItRightTest.php @@ -0,0 +1,23 @@ +assertTrue( is_plugin_active( 'doing-it-right/plugin.php' ) ); + $this->assertEquals( 'activated', get_option( 'doing_it_right_activation' ) ); + } + + /** + * It should correctly load the plugin + * + * @test + */ + public function should_correctly_load_the_plugin() { + global $doing_it_right_plugin_loaded; + $this->assertTrue( $doing_it_right_plugin_loaded ); + } +} diff --git a/tests/wploader_plugin_silent_activation/DoingItWrongPluginOneTest.php b/tests/wploader_plugin_silent_activation/DoingItWrongPluginOneTest.php new file mode 100644 index 000000000..671d64e67 --- /dev/null +++ b/tests/wploader_plugin_silent_activation/DoingItWrongPluginOneTest.php @@ -0,0 +1,31 @@ +assertTrue( is_plugin_active( 'doing-it-wrong-1/plugin.php' ) ); + } + + /** + * It should not have set the option during the plugin activation + * + * @test + */ + public function should_have_not_set_the_option_during_the_plugin_activation(){ + $this->assertEquals( '', get_option( 'doing_it_wrong_1_activation' ) ); + } + + /** + * It should correctly load the plugin + * + * @test + */ + public function should_correctly_load_the_plugin() { + global $doing_it_wrong_1_plugin_loaded; + $this->assertTrue( $doing_it_wrong_1_plugin_loaded ); + } +} diff --git a/tests/wploader_plugin_silent_activation/DoingItWrongPluginTwoTest.php b/tests/wploader_plugin_silent_activation/DoingItWrongPluginTwoTest.php new file mode 100644 index 000000000..bc4e0dc5b --- /dev/null +++ b/tests/wploader_plugin_silent_activation/DoingItWrongPluginTwoTest.php @@ -0,0 +1,31 @@ +assertTrue( is_plugin_active( 'doing-it-wrong-2/plugin.php' ) ); + } + + /** + * It should have not set the option during the plugin activation + * + * @test + */ + public function should_have_set_not_the_option_during_the_plugin_activation() { + $this->assertEquals( '', get_option( 'doing_it_wrong_2_activation' ) ); + } + + /** + * It should correctly load the plugin + * + * @test + */ + public function should_correctly_load_the_plugin() { + global $doing_it_wrong_2_plugin_loaded; + $this->assertTrue( $doing_it_wrong_2_plugin_loaded ); + } +} From 66bc3f2d5c1011f63f70c5489e4c30cf7067afd3 Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Mon, 20 Nov 2023 15:15:26 +0100 Subject: [PATCH 2/3] style(src) cs fixes --- src/Codeception/Module/WPLoader.php | 60 ++--- tests/_bootstrap.php | 50 ++-- tests/_data/plugins/doing-it-right/plugin.php | 12 +- .../_data/plugins/doing-it-wrong-1/plugin.php | 15 +- .../_data/plugins/doing-it-wrong-2/plugin.php | 15 +- tests/_support/AcceptanceTester.php | 12 +- tests/acceptance/AjaxCest.php | 97 ++++---- .../Codeception/Lib/Generator/WPUnitTest.php | 228 +++++++++--------- .../DoingItRightTest.php | 41 ++-- .../DoingItWrongPluginOneTest.php | 56 +++-- .../DoingItWrongPluginTwoTest.php | 56 +++-- .../wploadersuite/BeforeAfterMethodsTest.php | 35 +-- 12 files changed, 355 insertions(+), 322 deletions(-) diff --git a/src/Codeception/Module/WPLoader.php b/src/Codeception/Module/WPLoader.php index 047df309c..e78128787 100644 --- a/src/Codeception/Module/WPLoader.php +++ b/src/Codeception/Module/WPLoader.php @@ -152,28 +152,28 @@ class WPLoader extends Module */ protected $config = [ - 'loadOnly' => false, - 'isolatedInstall' => true, - 'installationTableHandling' => 'empty', - 'wpDebug' => true, - 'multisite' => false, - 'skipPluggables' => false, - 'dbCharset' => 'utf8', - 'dbCollate' => '', - 'tablePrefix' => 'wptests_', - 'domain' => 'example.org', - 'adminEmail' => 'admin@example.org', - 'title' => 'Test Blog', - 'phpBinary' => 'php', - 'language' => '', - 'configFile' => '', - 'contentFolder' => '', - 'pluginsFolder' => '', - 'plugins' => [], - 'activatePlugins' => [], - 'activatePluginsSilently' => [], - 'bootstrapActions' => '', - 'theme' => '', + 'loadOnly' => false, + 'isolatedInstall' => true, + 'installationTableHandling' => 'empty', + 'wpDebug' => true, + 'multisite' => false, + 'skipPluggables' => false, + 'dbCharset' => 'utf8', + 'dbCollate' => '', + 'tablePrefix' => 'wptests_', + 'domain' => 'example.org', + 'adminEmail' => 'admin@example.org', + 'title' => 'Test Blog', + 'phpBinary' => 'php', + 'language' => '', + 'configFile' => '', + 'contentFolder' => '', + 'pluginsFolder' => '', + 'plugins' => [], + 'activatePlugins' => [], + 'activatePluginsSilently' => [], + 'bootstrapActions' => '', + 'theme' => '', ]; /** @@ -1121,13 +1121,13 @@ public function getContentFolder($path = '') */ protected function getInstallationConfiguration() { - return new Configuration( [ - 'tablesHandling' => isset( $this->config['installationTableHandling'] ) ? - $this->config['installationTableHandling'] - : 'empty', - 'activatePluginsSilently' => isset( $this->config['activatePluginsSilently'] ) ? - (array) $this->config['activatePluginsSilently'] - : [] - ] ); + return new Configuration([ + 'tablesHandling' => isset($this->config['installationTableHandling']) ? + $this->config['installationTableHandling'] + : 'empty', + 'activatePluginsSilently' => isset($this->config['activatePluginsSilently']) ? + (array) $this->config['activatePluginsSilently'] + : [] + ]); } } diff --git a/tests/_bootstrap.php b/tests/_bootstrap.php index 218a63156..1fe618560 100644 --- a/tests/_bootstrap.php +++ b/tests/_bootstrap.php @@ -9,39 +9,39 @@ createTestDatabasesIfNotExist(); // Make sure traits can be autoloaded from tests/_support/Traits -Autoload::addNamespace( '\lucatume\WPBrowser\Tests\Traits', codecept_root_dir( 'tests/_support/Traits' ) ); +Autoload::addNamespace('\lucatume\WPBrowser\Tests\Traits', codecept_root_dir('tests/_support/Traits')); // If the `uopz` extension is installed, then ensure `exit` and `die` to work normally. -if ( function_exists( 'uopz_allow_exit' ) ) { - uopz_allow_exit( true ); +if (function_exists('uopz_allow_exit')) { + uopz_allow_exit(true); } //addListener( Events::SUITE_BEFORE, function ( Codeception\Event\SuiteEvent $event ) { -// $suiteName = $event->getSuite()->getName(); +// $suiteName = $event->getSuite()->getName(); // -// if ( $suiteName !== 'wploader_plugin_silent_activation' ) { -// return; -// } +// if ( $suiteName !== 'wploader_plugin_silent_activation' ) { +// return; +// } // -// $wpRootDir = realpath( getenv( 'WORDPRESS_ROOT_DIR' ) ); +// $wpRootDir = realpath( getenv( 'WORDPRESS_ROOT_DIR' ) ); // -// if ( ! is_dir( $wpRootDir ) ) { -// throw new \RuntimeException( "The WORDPRESS_ROOT_DIR is not a valid directory." ); -// } +// if ( ! is_dir( $wpRootDir ) ) { +// throw new \RuntimeException( "The WORDPRESS_ROOT_DIR is not a valid directory." ); +// } // -// $wpRootDir = rtrim( $wpRootDir, '/\\' ); +// $wpRootDir = rtrim( $wpRootDir, '/\\' ); // -// foreach ( -// [ -// codecept_data_dir( 'plugins/doing-it-wrong-1' ) => $wpRootDir . '/wp-content/plugins/doing-it-wrong-1', -// codecept_data_dir( 'plugins/doing-it-wrong-2' ) => $wpRootDir . '/wp-content/plugins/doing-it-wrong-2', -// ] as $source => $destination -// ) { -// if ( ! copy( -// $source, -// $destination -// ) ) { -// throw new \RuntimeException( "Could not copy the plugin to the WordPress plugins directory." ); -// } -// } +// foreach ( +// [ +// codecept_data_dir( 'plugins/doing-it-wrong-1' ) => $wpRootDir . '/wp-content/plugins/doing-it-wrong-1', +// codecept_data_dir( 'plugins/doing-it-wrong-2' ) => $wpRootDir . '/wp-content/plugins/doing-it-wrong-2', +// ] as $source => $destination +// ) { +// if ( ! copy( +// $source, +// $destination +// ) ) { +// throw new \RuntimeException( "Could not copy the plugin to the WordPress plugins directory." ); +// } +// } //} ); diff --git a/tests/_data/plugins/doing-it-right/plugin.php b/tests/_data/plugins/doing-it-right/plugin.php index 1dae79ca4..8467cfd1c 100644 --- a/tests/_data/plugins/doing-it-right/plugin.php +++ b/tests/_data/plugins/doing-it-right/plugin.php @@ -3,11 +3,11 @@ * Plugin Name: Doing It Right */ -register_activation_hook( __FILE__, function () { - update_option( 'doing_it_right_activation', 'activated' ); -} ); +register_activation_hook(__FILE__, function () { + update_option('doing_it_right_activation', 'activated'); +}); -add_action('plugins_loaded', function(){ - global $doing_it_right_plugin_loaded; - $doing_it_right_plugin_loaded = true; +add_action('plugins_loaded', function () { + global $doing_it_right_plugin_loaded; + $doing_it_right_plugin_loaded = true; }); diff --git a/tests/_data/plugins/doing-it-wrong-1/plugin.php b/tests/_data/plugins/doing-it-wrong-1/plugin.php index de849f8da..344c78951 100644 --- a/tests/_data/plugins/doing-it-wrong-1/plugin.php +++ b/tests/_data/plugins/doing-it-wrong-1/plugin.php @@ -3,13 +3,14 @@ * Plugin Name: Doing It Wrong 1 */ -function doing_it_wrong_1_activation(){ - _doing_it_wrong( __FUNCTION__, 'This is a test', '1.0.0' ); - update_option('doing_it_wrong_1j_activation', 'activated'); +function doing_it_wrong_1_activation() +{ + _doing_it_wrong(__FUNCTION__, 'This is a test', '1.0.0'); + update_option('doing_it_wrong_1j_activation', 'activated'); } -register_activation_hook(__FILE__,'doing_it_wrong_1_activation'); +register_activation_hook(__FILE__, 'doing_it_wrong_1_activation'); -add_action('plugins_loaded', function(){ - global $doing_it_wrong_1_plugin_loaded; - $doing_it_wrong_1_plugin_loaded = true; +add_action('plugins_loaded', function () { + global $doing_it_wrong_1_plugin_loaded; + $doing_it_wrong_1_plugin_loaded = true; }); diff --git a/tests/_data/plugins/doing-it-wrong-2/plugin.php b/tests/_data/plugins/doing-it-wrong-2/plugin.php index a193c3360..dfc8502b3 100644 --- a/tests/_data/plugins/doing-it-wrong-2/plugin.php +++ b/tests/_data/plugins/doing-it-wrong-2/plugin.php @@ -3,14 +3,15 @@ * Plugin Name: Doing It Wrong 2 */ -function doing_it_wrong_2_activation() { - _doing_it_wrong( __FUNCTION__, 'This is a test', '1.0.0' ); - update_option('doing_it_wrong_2_activation', 'activated'); +function doing_it_wrong_2_activation() +{ + _doing_it_wrong(__FUNCTION__, 'This is a test', '1.0.0'); + update_option('doing_it_wrong_2_activation', 'activated'); } -register_activation_hook( __FILE__, 'doing_it_wrong_2_activation' ); +register_activation_hook(__FILE__, 'doing_it_wrong_2_activation'); -add_action('plugins_loaded', function(){ - global $doing_it_wrong_2_plugin_loaded; - $doing_it_wrong_2_plugin_loaded = true; +add_action('plugins_loaded', function () { + global $doing_it_wrong_2_plugin_loaded; + $doing_it_wrong_2_plugin_loaded = true; }); diff --git a/tests/_support/AcceptanceTester.php b/tests/_support/AcceptanceTester.php index 8c45540a3..0d1cd504b 100644 --- a/tests/_support/AcceptanceTester.php +++ b/tests/_support/AcceptanceTester.php @@ -2,7 +2,6 @@ use PHPUnit\Framework\Assert; - /** * Inherited Methods * @method void wantToTest( $text ) @@ -18,10 +17,11 @@ * * @SuppressWarnings(PHPMD) */ -class AcceptanceTester extends \Codeception\Actor { - use _generated\AcceptanceTesterActions; +class AcceptanceTester extends \Codeception\Actor +{ + use _generated\AcceptanceTesterActions; - /** - * Define custom actions here - */ + /** + * Define custom actions here + */ } diff --git a/tests/acceptance/AjaxCest.php b/tests/acceptance/AjaxCest.php index 3569adefc..88e41dc46 100644 --- a/tests/acceptance/AjaxCest.php +++ b/tests/acceptance/AjaxCest.php @@ -1,8 +1,10 @@ haveMuPlugin( 'test-ajax-plugin', $code ); - } + $I->haveMuPlugin('test-ajax-plugin', $code); + } - public function test_invalid_action_returns_400( AcceptanceTester $I ) { - $I->amOnAdminAjaxPage( [ - 'action' => 'invalid_action' - ] ); - $I->seeResponseCodeIs( 400 ); - } + public function test_invalid_action_returns_400(AcceptanceTester $I) + { + $I->amOnAdminAjaxPage([ + 'action' => 'invalid_action' + ]); + $I->seeResponseCodeIs(400); + } - public function test_missing_nonce_returns_403( AcceptanceTester $I ) { - $I->amOnAdminAjaxPage( [ - 'action' => 'test_ajax_action' - ] ); - $I->seeResponseCodeIs( 403 ); - } + public function test_missing_nonce_returns_403(AcceptanceTester $I) + { + $I->amOnAdminAjaxPage([ + 'action' => 'test_ajax_action' + ]); + $I->seeResponseCodeIs(403); + } - public function test_wrong_nonce_returns_403( AcceptanceTester $I ) { - $I->amOnAdminAjaxPage( [ - 'action' => 'test_ajax_action', - '_ajax_nonce' => 'wrong_nonce' - ] ); - $I->seeResponseCodeIs( 403 ); - } + public function test_wrong_nonce_returns_403(AcceptanceTester $I) + { + $I->amOnAdminAjaxPage([ + 'action' => 'test_ajax_action', + '_ajax_nonce' => 'wrong_nonce' + ]); + $I->seeResponseCodeIs(403); + } - public function test_valid_nonce_returns_200_for_visitor( AcceptanceTester $I ) { - $I->amOnPage( '/' ); - $nonce = $I->grabValueFrom( '#test-ajax-nonce' ); + public function test_valid_nonce_returns_200_for_visitor(AcceptanceTester $I) + { + $I->amOnPage('/'); + $nonce = $I->grabValueFrom('#test-ajax-nonce'); - $I->amOnAdminAjaxPage( [ - 'action' => 'test_ajax_action', - '_ajax_nonce' => $nonce - ] ); - $I->seeResponseCodeIs( 200 ); - $I->canSee( json_encode( [ 'type' => 'no-priv' ] ) ); - } + $I->amOnAdminAjaxPage([ + 'action' => 'test_ajax_action', + '_ajax_nonce' => $nonce + ]); + $I->seeResponseCodeIs(200); + $I->canSee(json_encode([ 'type' => 'no-priv' ])); + } - public function test_valid_nonce_returns_200_for_admin( AcceptanceTester $I ) { - $I->loginAsAdmin(); - $I->amOnPage( '/' ); - $nonce = $I->grabValueFrom( '#test-ajax-nonce' ); + public function test_valid_nonce_returns_200_for_admin(AcceptanceTester $I) + { + $I->loginAsAdmin(); + $I->amOnPage('/'); + $nonce = $I->grabValueFrom('#test-ajax-nonce'); - $I->amOnAdminAjaxPage( [ - 'action' => 'test_ajax_action', - '_ajax_nonce' => $nonce - ] ); - $I->seeResponseCodeIs( 200 ); - $I->canSee( json_encode( [ 'type' => 'priv' ] ) ); - } + $I->amOnAdminAjaxPage([ + 'action' => 'test_ajax_action', + '_ajax_nonce' => $nonce + ]); + $I->seeResponseCodeIs(200); + $I->canSee(json_encode([ 'type' => 'priv' ])); + } } diff --git a/tests/unit/Codeception/Lib/Generator/WPUnitTest.php b/tests/unit/Codeception/Lib/Generator/WPUnitTest.php index a3d1e2c72..ac9f162ec 100644 --- a/tests/unit/Codeception/Lib/Generator/WPUnitTest.php +++ b/tests/unit/Codeception/Lib/Generator/WPUnitTest.php @@ -7,114 +7,122 @@ use tad\Codeception\SnapshotAssertions\SnapshotAssertions; use tad\WPBrowser\Compat\Compatibility; -class WPUnitTest extends \Codeception\Test\Unit { - use WithUopz; - use SnapshotAssertions; - - /** - * A backup of the current PHPUnit Series env var. - * @var array|false|string - */ - protected $phpunitSeriesEnv; - /** - * @var \UnitTester - */ - protected $tester; - - /** - * @var string - */ - private $phpunitVersionPropertyBackup = null; - - private function setPhpUnitSeriesTo( $series ) { - $reflectionClass = new ReflectionClass( Version::class ); - $versionProp = $reflectionClass->getProperty( 'version' ); - $versionProp->setAccessible( true ); - $this->phpunitVersionPropertyBackup = $versionProp->getValue(); - $versionProp->setValue( $series ); - } - - /** - * @after - */ - public function restorePhpunitVersionProperty() { - if ( null === $this->phpunitVersionPropertyBackup ) { - return; - } - $reflectionClass = new ReflectionClass( Version::class ); - $versionProp = $reflectionClass->getProperty( 'version' ); - $versionProp->setAccessible( true ); - $versionProp->setValue( $this->phpunitVersionPropertyBackup ); - $this->phpunitVersionPropertyBackup = null; - } - - /** - * It should scaffold PHPUnit v8 compatible code on series 8 - * - * @test - * @dataProvider phpUnitEq8Series - */ - public function should_scaffold_php_unit_v_8_code_on_series_8( $series ) { - $this->setPhpUnitSeriesTo( '8.0' ); - $settings = [ 'namespace' => 'Acme' ]; - $name = 'SomeClass'; - - $generator = new WPUnit( $settings, $name, WPTestCase::class ); - $code = $generator->produce(); - - $this->assertMatchesCodeSnapshot( $code, 'php' ); - } - - public function phpUnitLt8Series() { - return [ - '5.5' => [ '5.5' ], - '5.5.5' => [ '5.5' ], - '6.2' => [ '6.2' ], - '6.2.3' => [ '6.2' ], - '7.5' => [ '7.5' ], - '7.5.6' => [ '7.5' ], - ]; - } - - /** - * It should scaffold PHPUnit lt 8.0 compatible code on series lt 8 - * - * @test - * @dataProvider phpUnitLt8Series - */ - public function should_scaffold_php_unit_lt_8_0_compatible_code_on_series_lt_8( $series ) { - $this->setPhpUnitSeriesTo( $series ); - $settings = [ 'namespace' => 'Acme' ]; - $name = 'SomeClass'; - - $generator = new WPUnit( $settings, $name, WPTestCase::class ); - $code = $generator->produce(); - - $this->assertMatchesCodeSnapshot( $code, 'php' ); - } - - /** - * It should correctly add the tester property if actor is set in the settings - * - * @test - */ - public function should_correctly_add_the_tester_property_if_actor_is_set_in_the_settings() { - $this->setPhpUnitSeriesTo( '6.0' ); - $settings = [ 'namespace' => 'Acme', 'actor' => 'Fixer' ]; - $name = 'SomeClass'; - - $generator = new WPUnit( $settings, $name, WPTestCase::class ); - $code = $generator->produce(); - - $this->assertMatchesCodeSnapshot( $code, 'php' ); - } - - public function phpUnitEq8Series() { - return [ - '8.0' => [ '8.0' ], - '8.0.4' => [ '8.0.4' ], - '8.1' => [ '8.1' ], - '8.1.6' => [ '8.1.6' ], - ]; - } +class WPUnitTest extends \Codeception\Test\Unit +{ + use WithUopz; + use SnapshotAssertions; + + /** + * A backup of the current PHPUnit Series env var. + * @var array|false|string + */ + protected $phpunitSeriesEnv; + /** + * @var \UnitTester + */ + protected $tester; + + /** + * @var string + */ + private $phpunitVersionPropertyBackup = null; + + private function setPhpUnitSeriesTo($series) + { + $reflectionClass = new ReflectionClass(Version::class); + $versionProp = $reflectionClass->getProperty('version'); + $versionProp->setAccessible(true); + $this->phpunitVersionPropertyBackup = $versionProp->getValue(); + $versionProp->setValue($series); + } + + /** + * @after + */ + public function restorePhpunitVersionProperty() + { + if (null === $this->phpunitVersionPropertyBackup) { + return; + } + $reflectionClass = new ReflectionClass(Version::class); + $versionProp = $reflectionClass->getProperty('version'); + $versionProp->setAccessible(true); + $versionProp->setValue($this->phpunitVersionPropertyBackup); + $this->phpunitVersionPropertyBackup = null; + } + + /** + * It should scaffold PHPUnit v8 compatible code on series 8 + * + * @test + * @dataProvider phpUnitEq8Series + */ + public function should_scaffold_php_unit_v_8_code_on_series_8($series) + { + $this->setPhpUnitSeriesTo('8.0'); + $settings = [ 'namespace' => 'Acme' ]; + $name = 'SomeClass'; + + $generator = new WPUnit($settings, $name, WPTestCase::class); + $code = $generator->produce(); + + $this->assertMatchesCodeSnapshot($code, 'php'); + } + + public function phpUnitLt8Series() + { + return [ + '5.5' => [ '5.5' ], + '5.5.5' => [ '5.5' ], + '6.2' => [ '6.2' ], + '6.2.3' => [ '6.2' ], + '7.5' => [ '7.5' ], + '7.5.6' => [ '7.5' ], + ]; + } + + /** + * It should scaffold PHPUnit lt 8.0 compatible code on series lt 8 + * + * @test + * @dataProvider phpUnitLt8Series + */ + public function should_scaffold_php_unit_lt_8_0_compatible_code_on_series_lt_8($series) + { + $this->setPhpUnitSeriesTo($series); + $settings = [ 'namespace' => 'Acme' ]; + $name = 'SomeClass'; + + $generator = new WPUnit($settings, $name, WPTestCase::class); + $code = $generator->produce(); + + $this->assertMatchesCodeSnapshot($code, 'php'); + } + + /** + * It should correctly add the tester property if actor is set in the settings + * + * @test + */ + public function should_correctly_add_the_tester_property_if_actor_is_set_in_the_settings() + { + $this->setPhpUnitSeriesTo('6.0'); + $settings = [ 'namespace' => 'Acme', 'actor' => 'Fixer' ]; + $name = 'SomeClass'; + + $generator = new WPUnit($settings, $name, WPTestCase::class); + $code = $generator->produce(); + + $this->assertMatchesCodeSnapshot($code, 'php'); + } + + public function phpUnitEq8Series() + { + return [ + '8.0' => [ '8.0' ], + '8.0.4' => [ '8.0.4' ], + '8.1' => [ '8.1' ], + '8.1.6' => [ '8.1.6' ], + ]; + } } diff --git a/tests/wploader_plugin_silent_activation/DoingItRightTest.php b/tests/wploader_plugin_silent_activation/DoingItRightTest.php index bab65c350..53c321932 100644 --- a/tests/wploader_plugin_silent_activation/DoingItRightTest.php +++ b/tests/wploader_plugin_silent_activation/DoingItRightTest.php @@ -1,23 +1,26 @@ assertTrue( is_plugin_active( 'doing-it-right/plugin.php' ) ); - $this->assertEquals( 'activated', get_option( 'doing_it_right_activation' ) ); - } +class DoingItRightTest extends \Codeception\TestCase\WPTestCase +{ + /** + * It should have correctly activated the plugin + * + * @test + */ + public function should_have_correctly_activated_the_plugin() + { + $this->assertTrue(is_plugin_active('doing-it-right/plugin.php')); + $this->assertEquals('activated', get_option('doing_it_right_activation')); + } - /** - * It should correctly load the plugin - * - * @test - */ - public function should_correctly_load_the_plugin() { - global $doing_it_right_plugin_loaded; - $this->assertTrue( $doing_it_right_plugin_loaded ); - } + /** + * It should correctly load the plugin + * + * @test + */ + public function should_correctly_load_the_plugin() + { + global $doing_it_right_plugin_loaded; + $this->assertTrue($doing_it_right_plugin_loaded); + } } diff --git a/tests/wploader_plugin_silent_activation/DoingItWrongPluginOneTest.php b/tests/wploader_plugin_silent_activation/DoingItWrongPluginOneTest.php index 671d64e67..6af5330de 100644 --- a/tests/wploader_plugin_silent_activation/DoingItWrongPluginOneTest.php +++ b/tests/wploader_plugin_silent_activation/DoingItWrongPluginOneTest.php @@ -1,31 +1,35 @@ assertTrue( is_plugin_active( 'doing-it-wrong-1/plugin.php' ) ); - } +class DoingItWrongPluginOneTest extends \Codeception\TestCase\WPTestCase +{ + /** + * It should have activated plugin one + * + * @test + */ + public function should_have_activated_plugin_one() + { + $this->assertTrue(is_plugin_active('doing-it-wrong-1/plugin.php')); + } - /** - * It should not have set the option during the plugin activation - * - * @test - */ - public function should_have_not_set_the_option_during_the_plugin_activation(){ - $this->assertEquals( '', get_option( 'doing_it_wrong_1_activation' ) ); - } + /** + * It should not have set the option during the plugin activation + * + * @test + */ + public function should_have_not_set_the_option_during_the_plugin_activation() + { + $this->assertEquals('', get_option('doing_it_wrong_1_activation')); + } - /** - * It should correctly load the plugin - * - * @test - */ - public function should_correctly_load_the_plugin() { - global $doing_it_wrong_1_plugin_loaded; - $this->assertTrue( $doing_it_wrong_1_plugin_loaded ); - } + /** + * It should correctly load the plugin + * + * @test + */ + public function should_correctly_load_the_plugin() + { + global $doing_it_wrong_1_plugin_loaded; + $this->assertTrue($doing_it_wrong_1_plugin_loaded); + } } diff --git a/tests/wploader_plugin_silent_activation/DoingItWrongPluginTwoTest.php b/tests/wploader_plugin_silent_activation/DoingItWrongPluginTwoTest.php index bc4e0dc5b..f473f007d 100644 --- a/tests/wploader_plugin_silent_activation/DoingItWrongPluginTwoTest.php +++ b/tests/wploader_plugin_silent_activation/DoingItWrongPluginTwoTest.php @@ -1,31 +1,35 @@ assertTrue( is_plugin_active( 'doing-it-wrong-2/plugin.php' ) ); - } +class DoingItWrongPluginTwoTest extends \Codeception\TestCase\WPTestCase +{ + /** + * It should have activated plugin two + * + * @test + */ + public function should_have_activated_plugin_two() + { + $this->assertTrue(is_plugin_active('doing-it-wrong-2/plugin.php')); + } - /** - * It should have not set the option during the plugin activation - * - * @test - */ - public function should_have_set_not_the_option_during_the_plugin_activation() { - $this->assertEquals( '', get_option( 'doing_it_wrong_2_activation' ) ); - } + /** + * It should have not set the option during the plugin activation + * + * @test + */ + public function should_have_set_not_the_option_during_the_plugin_activation() + { + $this->assertEquals('', get_option('doing_it_wrong_2_activation')); + } - /** - * It should correctly load the plugin - * - * @test - */ - public function should_correctly_load_the_plugin() { - global $doing_it_wrong_2_plugin_loaded; - $this->assertTrue( $doing_it_wrong_2_plugin_loaded ); - } + /** + * It should correctly load the plugin + * + * @test + */ + public function should_correctly_load_the_plugin() + { + global $doing_it_wrong_2_plugin_loaded; + $this->assertTrue($doing_it_wrong_2_plugin_loaded); + } } diff --git a/tests/wploadersuite/BeforeAfterMethodsTest.php b/tests/wploadersuite/BeforeAfterMethodsTest.php index d83b24d73..95d9380a1 100644 --- a/tests/wploadersuite/BeforeAfterMethodsTest.php +++ b/tests/wploadersuite/BeforeAfterMethodsTest.php @@ -2,23 +2,28 @@ use PHPUnit\Framework\Assert; -class BeforeAfterMethodsTest extends \Codeception\TestCase\WPTestCase { - private static $staticCanary = true; - private $canary = false; +class BeforeAfterMethodsTest extends \Codeception\TestCase\WPTestCase +{ + private static $staticCanary = true; + private $canary = false; - public function _before() { - $this->canary = true; - } + public function _before() + { + $this->canary = true; + } - public function _after() { - self::$staticCanary = false; - } + public function _after() + { + self::$staticCanary = false; + } - public static function wpTearDownAfterClass() { - Assert::assertFalse( self::$staticCanary ); - } + public static function wpTearDownAfterClass() + { + Assert::assertFalse(self::$staticCanary); + } - public function test_before_method_is_loaded() { - $this->assertTrue( $this->canary ); - } + public function test_before_method_is_loaded() + { + $this->assertTrue($this->canary); + } } From 12829451bbe910ed7a4e91885950687ff0d2984b Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Mon, 20 Nov 2023 15:48:39 +0100 Subject: [PATCH 3/3] test(_support/_generated) rebuild actions on PHP 5.6 --- .../Wploader_plugin_silent_activationTesterActions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/_support/_generated/Wploader_plugin_silent_activationTesterActions.php b/tests/_support/_generated/Wploader_plugin_silent_activationTesterActions.php index 2ec859b90..1d2bb444b 100644 --- a/tests/_support/_generated/Wploader_plugin_silent_activationTesterActions.php +++ b/tests/_support/_generated/Wploader_plugin_silent_activationTesterActions.php @@ -112,7 +112,7 @@ public function getContentFolder($path = "") { * @return void * @see \Codeception\Module\WPLoader::startWpFiltersDebug() */ - public function startWpFiltersDebug(?callable $format = NULL) { + public function startWpFiltersDebug($format = NULL) { return $this->getScenario()->runStep(new \Codeception\Step\Action('startWpFiltersDebug', func_get_args())); }