Skip to content

Commit

Permalink
feat(WPLoader) add support for activatePluginsSilently
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed Nov 20, 2023
1 parent f032bbc commit afa8282
Show file tree
Hide file tree
Showing 26 changed files with 562 additions and 31 deletions.
3 changes: 3 additions & 0 deletions codeception.dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
60 changes: 34 additions & 26 deletions src/Codeception/Module/WPLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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' => '',
];

/**
Expand Down Expand Up @@ -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']
: []
] );
}
}
1 change: 1 addition & 0 deletions src/includes/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
'WPLANG' => WPLANG,
],
'tablesHandling' => $installationConfiguration->get('tablesHandling','empty'),
'activatePluginsSilently' => $installationConfiguration->get('activatePluginsSilently', []),
];

$dirConstants = [
Expand Down
9 changes: 7 additions & 2 deletions src/includes/isolated-install.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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();
Expand Down
38 changes: 35 additions & 3 deletions tests/_bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,47 @@
<?php
// This is global bootstrap for autoloading.
use Codeception\Events;
use Codeception\Util\Autoload;

use function tad\WPBrowser\addListener;
use function tad\WPBrowser\Tests\Support\createTestDatabasesIfNotExist;

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();
//
// 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." );
// }
// }
//} );
13 changes: 13 additions & 0 deletions tests/_data/plugins/doing-it-right/plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
* Plugin Name: Doing It Right
*/

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;
});
15 changes: 15 additions & 0 deletions tests/_data/plugins/doing-it-wrong-1/plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* 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');
}
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;
});
16 changes: 16 additions & 0 deletions tests/_data/plugins/doing-it-wrong-2/plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* 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');
}

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;
});
10 changes: 10 additions & 0 deletions tests/_support/Helper/Wploader_plugin_silent_activation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace Helper;

// here you can define custom actions
// all public methods declared in helper class will be available in $I

class Wploader_plugin_silent_activation extends \Codeception\Module
{

}
26 changes: 26 additions & 0 deletions tests/_support/Wploader_plugin_silent_activationTester.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php


/**
* Inherited Methods
* @method void wantToTest($text)
* @method void wantTo($text)
* @method void execute($callable)
* @method void expectTo($prediction)
* @method void expect($prediction)
* @method void amGoingTo($argumentation)
* @method void am($role)
* @method void lookForwardTo($achieveValue)
* @method void comment($description)
* @method void pause()
*
* @SuppressWarnings(PHPMD)
*/
class Wploader_plugin_silent_activationTester extends \Codeception\Actor
{
use _generated\Wploader_plugin_silent_activationTesterActions;

/**
* Define custom actions here
*/
}
Loading

0 comments on commit afa8282

Please sign in to comment.