Handling network activation of plugins in WordPress integration tests.
A missing piece
The WPLoader module part of wp-browser is a wrapper around WordPress PHPUnit based automated testing suite and as such it inherits its inner workings.
As the underlying suite the module allows tests to run in the context of a multisite installation setting its multisite
option to true
:
class_name: MuloaderTester
modules:
enabled:
- \Helper\Muloader
- WPLoader
config:
WPLoader:
multisite: true
wpRootFolder: "/Users/Luca/Sites/wordpress"
dbName: "codeception-tests"
dbHost: "127.0.0.1"
dbUser: "root"
dbPassword: "root"
tablePrefix: "wp_"
domain: "codeception-tests.dev"
adminEmail: "admin@codeception-tests.dev"
title: "Codeception WPLoader Blog"
theme: dummy
plugins: ['acme-plugin/plugin.php']
activatePlugins: ['acme-plugin/plugin.php']
In this case any plugin specified in the activatePlugins
option would then be network activated.
The missing piece is that, in the context of that activation that it’s happening right after WordPress installation, any check on the is_multisite
function would always return false
as WordPress installation of a multisite network requires it.
A code example
The below example plugin registers a method to run at the plugin activation and in that method will check for the multisite context to update an option;
<?php
/**
* Plugin Name: Acme Plugin 1
* Plugin URI: http://theAverageDev.com
* Description: Acme Plugin 1
* Version: 1.0
* Author: theAverageDev
* Author URI: http://theAverageDev.com
* License: GPL 2.0
*/
class Acme_Plugin1
{
public static function activate()
{
if (is_multisite()) {
update_network_option(null, 'acme-plugin-was', 'mu-activated');
} else {
update_option('acme-plugin-was', 'not-mu-activated');
}
}
}
register_activation_hook(__FILE__, [Acme_Plugin1::class, 'activate']);
Given the WPLoader
module configuration above this test would, counter-intuitively, fail:
<?php
namespace tad\WPBrowser\Tests;
class MuPluginActivationCestTest extends \Codeception\TestCase\WPTestCase
{
/**
* @test
* it should network activate network plugins on mu installation
*/
public function it_should_network_activate_network_plugins_on_mu_installation()
{
$this->assertFalse(get_option('acme-plugin-was'));
$this->assertEquals('mu-activated', get_network_option(null, 'acme-plugin-was'));
}
}
The reason being that the particular nature of the WordPress installation and setup process would see the MULTISITE
constant set to false
hence leading to a false
return value for the is_multisite()
function.
To get around that the latest version of wp-browser uses some “monkey-patching” based on Patchwork capabilities to make some multisite functions work as expected.
On GitHub
Version 1.19.0
of WPBrowser is available on GitHub.