Skip to content

Commit afa8282

Browse files
committed
feat(WPLoader) add support for activatePluginsSilently
1 parent f032bbc commit afa8282

File tree

26 files changed

+562
-31
lines changed

26 files changed

+562
-31
lines changed

codeception.dist.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ extensions:
2929
'tests/_data/themes/dummy': '%WORDPRESS_ROOT_DIR%/wp-content/themes/dummy'
3030
'tests/_data/themes/isolated': '%WORDPRESS_ROOT_DIR%/wp-content/themes/isolated'
3131
'tests/_data/plugins/mu-plugin-1': '%WORDPRESS_ROOT_DIR%/wp-content/plugins/mu-plugin-1'
32+
'tests/_data/plugins/doing-it-right': '%WORDPRESS_ROOT_DIR%/wp-content/plugins/doing-it-right'
33+
'tests/_data/plugins/doing-it-wrong-1': '%WORDPRESS_ROOT_DIR%/wp-content/plugins/doing-it-wrong-1'
34+
'tests/_data/plugins/doing-it-wrong-2': '%WORDPRESS_ROOT_DIR%/wp-content/plugins/doing-it-wrong-2'
3235
'tests/_data/plugins/test': '%WORDPRESS_ROOT_DIR%/wp-content/plugins/test'
3336
'tests/_data/plugins/isolated-test-plugin': '%WORDPRESS_ROOT_DIR%/wp-content/plugins/isolated-test-plugin'
3437
'tests/_data/plugins/isolated-test-plugin-two': '%WORDPRESS_ROOT_DIR%/wp-content/plugins/isolated-test-plugin-two'

src/Codeception/Module/WPLoader.php

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ class WPLoader extends Module
138138
* activated calling the `activate_{$plugin}` before any test case runs and
139139
* after mu-plugins have been loaded; these should be defined in the
140140
* `folder/plugin-file.php` format.
141+
* activatePlugins - array, def. `[]`, a list of plugins that should be
142+
* silently activated calling the `activate_{$plugin}` before any test case runs and
143+
* after mu-plugins have been loaded; these should be defined in the
144+
* `folder/plugin-file.php` format.
141145
* bootstrapActions - array, def. `[]`, a list of actions that should be
142146
* called after before any test case runs.
143147
* skipPluggables - bool, def. `false`, if set to `true` will skip the
@@ -148,27 +152,28 @@ class WPLoader extends Module
148152
*/
149153
protected $config
150154
= [
151-
'loadOnly' => false,
152-
'isolatedInstall' => true,
153-
'installationTableHandling' => 'empty',
154-
'wpDebug' => true,
155-
'multisite' => false,
156-
'skipPluggables' => false,
157-
'dbCharset' => 'utf8',
158-
'dbCollate' => '',
159-
'tablePrefix' => 'wptests_',
160-
'domain' => 'example.org',
161-
'adminEmail' => 'admin@example.org',
162-
'title' => 'Test Blog',
163-
'phpBinary' => 'php',
164-
'language' => '',
165-
'configFile' => '',
166-
'contentFolder' => '',
167-
'pluginsFolder' => '',
168-
'plugins' => '',
169-
'activatePlugins' => '',
170-
'bootstrapActions' => '',
171-
'theme' => '',
155+
'loadOnly' => false,
156+
'isolatedInstall' => true,
157+
'installationTableHandling' => 'empty',
158+
'wpDebug' => true,
159+
'multisite' => false,
160+
'skipPluggables' => false,
161+
'dbCharset' => 'utf8',
162+
'dbCollate' => '',
163+
'tablePrefix' => 'wptests_',
164+
'domain' => 'example.org',
165+
'adminEmail' => 'admin@example.org',
166+
'title' => 'Test Blog',
167+
'phpBinary' => 'php',
168+
'language' => '',
169+
'configFile' => '',
170+
'contentFolder' => '',
171+
'pluginsFolder' => '',
172+
'plugins' => [],
173+
'activatePlugins' => [],
174+
'activatePluginsSilently' => [],
175+
'bootstrapActions' => '',
176+
'theme' => '',
172177
];
173178

174179
/**
@@ -1116,10 +1121,13 @@ public function getContentFolder($path = '')
11161121
*/
11171122
protected function getInstallationConfiguration()
11181123
{
1119-
return new Configuration([
1120-
'tablesHandling' => isset($this->config['installationTableHandling']) ?
1121-
$this->config['installationTableHandling']
1122-
: 'empty'
1123-
]);
1124+
return new Configuration( [
1125+
'tablesHandling' => isset( $this->config['installationTableHandling'] ) ?
1126+
$this->config['installationTableHandling']
1127+
: 'empty',
1128+
'activatePluginsSilently' => isset( $this->config['activatePluginsSilently'] ) ?
1129+
(array) $this->config['activatePluginsSilently']
1130+
: []
1131+
] );
11241132
}
11251133
}

src/includes/bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
'WPLANG' => WPLANG,
115115
],
116116
'tablesHandling' => $installationConfiguration->get('tablesHandling','empty'),
117+
'activatePluginsSilently' => $installationConfiguration->get('activatePluginsSilently', []),
117118
];
118119

119120
$dirConstants = [

src/includes/isolated-install.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,13 @@
192192
$current_site->blog_id = 1;
193193
}
194194

195+
$activatePluginsSilently = isset( $configuration['activatePluginsSilently'] ) ?
196+
$configuration['activatePluginsSilently']
197+
: [];
198+
195199
// finally activate the plugins that should be activated
196200
if (!empty($activePlugins)) {
197-
$activePlugins = array_unique($activePlugins);
201+
$activePlugins = array_values( array_unique( array_merge( $activePlugins, $activatePluginsSilently ) ) );
198202

199203
if ($multisite) {
200204
require(ABSPATH . WPINC . '/class-wp-site-query.php');
@@ -204,8 +208,9 @@
204208
}
205209

206210
foreach ($activePlugins as $plugin) {
211+
$silent = in_array($plugin, $activatePluginsSilently,true);
207212
printf("\n%sctivating plugin [%s]...", $multisite ? 'Network a' : 'A', $plugin);
208-
$activated = activate_plugin($plugin, null, $multisite, false);
213+
$activated = activate_plugin($plugin, null, $multisite, $silent);
209214

210215
if (is_wp_error($activated)) {
211216
echo $activated->get_error_message();

tests/_bootstrap.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,47 @@
11
<?php
22
// This is global bootstrap for autoloading.
3+
use Codeception\Events;
34
use Codeception\Util\Autoload;
45

6+
use function tad\WPBrowser\addListener;
57
use function tad\WPBrowser\Tests\Support\createTestDatabasesIfNotExist;
68

79
createTestDatabasesIfNotExist();
810

911
// Make sure traits can be autoloaded from tests/_support/Traits
10-
Autoload::addNamespace('\lucatume\WPBrowser\Tests\Traits', codecept_root_dir('tests/_support/Traits'));
12+
Autoload::addNamespace( '\lucatume\WPBrowser\Tests\Traits', codecept_root_dir( 'tests/_support/Traits' ) );
1113

1214
// If the `uopz` extension is installed, then ensure `exit` and `die` to work normally.
13-
if (function_exists('uopz_allow_exit')) {
14-
uopz_allow_exit(true);
15+
if ( function_exists( 'uopz_allow_exit' ) ) {
16+
uopz_allow_exit( true );
1517
}
18+
19+
//addListener( Events::SUITE_BEFORE, function ( Codeception\Event\SuiteEvent $event ) {
20+
// $suiteName = $event->getSuite()->getName();
21+
//
22+
// if ( $suiteName !== 'wploader_plugin_silent_activation' ) {
23+
// return;
24+
// }
25+
//
26+
// $wpRootDir = realpath( getenv( 'WORDPRESS_ROOT_DIR' ) );
27+
//
28+
// if ( ! is_dir( $wpRootDir ) ) {
29+
// throw new \RuntimeException( "The WORDPRESS_ROOT_DIR is not a valid directory." );
30+
// }
31+
//
32+
// $wpRootDir = rtrim( $wpRootDir, '/\\' );
33+
//
34+
// foreach (
35+
// [
36+
// codecept_data_dir( 'plugins/doing-it-wrong-1' ) => $wpRootDir . '/wp-content/plugins/doing-it-wrong-1',
37+
// codecept_data_dir( 'plugins/doing-it-wrong-2' ) => $wpRootDir . '/wp-content/plugins/doing-it-wrong-2',
38+
// ] as $source => $destination
39+
// ) {
40+
// if ( ! copy(
41+
// $source,
42+
// $destination
43+
// ) ) {
44+
// throw new \RuntimeException( "Could not copy the plugin to the WordPress plugins directory." );
45+
// }
46+
// }
47+
//} );
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* Plugin Name: Doing It Right
4+
*/
5+
6+
register_activation_hook( __FILE__, function () {
7+
update_option( 'doing_it_right_activation', 'activated' );
8+
} );
9+
10+
add_action('plugins_loaded', function(){
11+
global $doing_it_right_plugin_loaded;
12+
$doing_it_right_plugin_loaded = true;
13+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Plugin Name: Doing It Wrong 1
4+
*/
5+
6+
function doing_it_wrong_1_activation(){
7+
_doing_it_wrong( __FUNCTION__, 'This is a test', '1.0.0' );
8+
update_option('doing_it_wrong_1j_activation', 'activated');
9+
}
10+
register_activation_hook(__FILE__,'doing_it_wrong_1_activation');
11+
12+
add_action('plugins_loaded', function(){
13+
global $doing_it_wrong_1_plugin_loaded;
14+
$doing_it_wrong_1_plugin_loaded = true;
15+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* Plugin Name: Doing It Wrong 2
4+
*/
5+
6+
function doing_it_wrong_2_activation() {
7+
_doing_it_wrong( __FUNCTION__, 'This is a test', '1.0.0' );
8+
update_option('doing_it_wrong_2_activation', 'activated');
9+
}
10+
11+
register_activation_hook( __FILE__, 'doing_it_wrong_2_activation' );
12+
13+
add_action('plugins_loaded', function(){
14+
global $doing_it_wrong_2_plugin_loaded;
15+
$doing_it_wrong_2_plugin_loaded = true;
16+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace Helper;
3+
4+
// here you can define custom actions
5+
// all public methods declared in helper class will be available in $I
6+
7+
class Wploader_plugin_silent_activation extends \Codeception\Module
8+
{
9+
10+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
4+
/**
5+
* Inherited Methods
6+
* @method void wantToTest($text)
7+
* @method void wantTo($text)
8+
* @method void execute($callable)
9+
* @method void expectTo($prediction)
10+
* @method void expect($prediction)
11+
* @method void amGoingTo($argumentation)
12+
* @method void am($role)
13+
* @method void lookForwardTo($achieveValue)
14+
* @method void comment($description)
15+
* @method void pause()
16+
*
17+
* @SuppressWarnings(PHPMD)
18+
*/
19+
class Wploader_plugin_silent_activationTester extends \Codeception\Actor
20+
{
21+
use _generated\Wploader_plugin_silent_activationTesterActions;
22+
23+
/**
24+
* Define custom actions here
25+
*/
26+
}

0 commit comments

Comments
 (0)