Skip to content

Commit

Permalink
Merge pull request #672 from lucatume/feat-silent-plugin-activation
Browse files Browse the repository at this point in the history
v3 - feat(WPLoader) add support for activatePluginsSilently
  • Loading branch information
lucatume authored Nov 20, 2023
2 parents f032bbc + 1282945 commit 3504d97
Show file tree
Hide file tree
Showing 30 changed files with 746 additions and 182 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
16 changes: 12 additions & 4 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 Down Expand Up @@ -165,8 +169,9 @@ class WPLoader extends Module
'configFile' => '',
'contentFolder' => '',
'pluginsFolder' => '',
'plugins' => '',
'activatePlugins' => '',
'plugins' => [],
'activatePlugins' => [],
'activatePluginsSilently' => [],
'bootstrapActions' => '',
'theme' => '',
];
Expand Down Expand Up @@ -1117,9 +1122,12 @@ public function getContentFolder($path = '')
protected function getInstallationConfiguration()
{
return new Configuration([
'tablesHandling' => isset($this->config['installationTableHandling']) ?
'tablesHandling' => isset($this->config['installationTableHandling']) ?
$this->config['installationTableHandling']
: 'empty'
: '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
32 changes: 32 additions & 0 deletions tests/_bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?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();
Expand All @@ -13,3 +15,33 @@
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;
});
16 changes: 16 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,16 @@
<?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;
});
17 changes: 17 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,17 @@
<?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;
});
12 changes: 6 additions & 6 deletions tests/_support/AcceptanceTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use PHPUnit\Framework\Assert;


/**
* Inherited Methods
* @method void wantToTest( $text )
Expand All @@ -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
*/
}
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 3504d97

Please sign in to comment.