Skip to content

Commit

Permalink
add unit tests + docs
Browse files Browse the repository at this point in the history
  • Loading branch information
diosmosis committed Jan 9, 2025
1 parent d3c4128 commit 17247c0
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 7 deletions.
32 changes: 26 additions & 6 deletions classes/WpMatomo/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,12 @@ private function update_components() {
$updater->update();
}

private function is_current_instance_installed() {
// TODO: unit tests
/**
* public for tests
*
* @return bool
*/
public function is_current_instance_installed() {
$installed_components = $this->settings->get_option( Settings::INSTANCE_COMPONENTS_INSTALLED );
if ( empty( $installed_components ) ) {
$installed_components = '[]';
Expand All @@ -413,7 +417,7 @@ private function is_current_instance_installed() {
}

// NOTE: this doesn't handle core plugins, but since they are always present during an install, we
// shouldn't need to
// shouldn't need tob
$plugin_files = isset( $GLOBALS['MATOMO_PLUGIN_FILES'] ) ? $GLOBALS['MATOMO_PLUGIN_FILES'] : [];
$plugin_files = is_array( $plugin_files ) ? $plugin_files : [];

Expand Down Expand Up @@ -446,10 +450,26 @@ private function mark_matomo_installed() {
$this->settings->save();
}

/**
* Install all plugins including core and non-core plugins. Non-core plugins
* are installed one at a time. Uninstalled plugins will not be loaded
* when each non-core plugin is installed.
*
* This works around the core bug where exceptions can be thrown when an
* uninstalled plugin, which is loaded while another plugin is being installed,
* handles the "plugin installed" event.
*
* In a standalone Matomo, this likely won't be an issue, as multiple non-core
* plugins are not usually installed at the same time. In Matomo for WordPress,
* this can happen as a matter of course in Multi Site installs.
*
* If a user creates a new WordPress site with multiple non-core plugins installed,
* by default the Matomo install process will try to install all of them at once,
* causing an error.
*
* @return void
*/
private function install_plugins_one_at_a_time() {
// TODO: docs on why this is needed
// TODO: core bug report

Config::getInstance()->PluginsInstalled = [ 'PluginsInstalled' => [] ];

$non_core_plugins = array_map(
Expand Down
5 changes: 4 additions & 1 deletion classes/WpMatomo/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ class Settings {
const SITE_CURRENCY = 'site_currency';
const NETWORK_CONFIG_OPTIONS = 'config_options';
const DISABLE_ASYNC_ARCHIVING_OPTION_NAME = 'matomo_disable_async_archiving';
const INSTANCE_COMPONENTS_INSTALLED = 'instance-components-installed';

// NOTE: this is not a setting value, but is stored with setting values to avoid
// adding an extra get_option call to every WordPress backoffice request.
const INSTANCE_COMPONENTS_INSTALLED = 'instance-components-installed';

public static $is_doing_action_tracking_related = false;
/**
Expand Down
97 changes: 97 additions & 0 deletions tests/phpunit/wpmatomo/test-install.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,25 @@ class InstallTest extends MatomoAnalytics_TestCase {
*/
private $uninstaller;

/**
* @var array
*/
private $original_plugins;

public function setUp(): void {
parent::setUp();

$this->settings = new Settings();
$this->installer = $this->make_installer();
$this->uninstaller = new Uninstaller();

$this->original_plugins = isset( $GLOBALS['MATOMO_MARKETPLACE_PLUGINS'] ) ? $GLOBALS['MATOMO_MARKETPLACE_PLUGINS'] : [];
}

public function tearDown(): void {
$GLOBALS['MATOMO_MARKETPLACE_PLUGINS'] = $this->original_plugins;

parent::tearDown();
}

private function make_installer() {
Expand Down Expand Up @@ -189,4 +202,88 @@ public function test_get_db_infos_respects_charset_overrides() {
$this->assertEquals( 'dummycharset', $db_config['charset'] );
$this->assertEquals( 'dummycollate', $db_config['collation'] );
}

public function test_is_current_instance_installed_returns_false_if_core_not_installed() {
$GLOBALS['MATOMO_MARKETPLACE_PLUGINS'] = [
ABSPATH . '/wp-content/plugins/matomo/matomo.php',
ABSPATH . '/wp-content/plugins/SomePlugin/SomePlugin.php',
];

$this->settings->set_option( Settings::INSTANCE_COMPONENTS_INSTALLED, wp_json_encode( [ 'SomePlugin' => 1 ] ) );
$this->settings->save();

$is_installed = $this->installer->is_current_instance_installed();
$this->assertFalse( $is_installed );
}

public function test_is_current_instance_installed_returns_false_if_non_core_plugin_not_installed() {
$GLOBALS['MATOMO_PLUGIN_FILES'] = [
ABSPATH . '/wp-content/plugins/matomo/matomo.php',
ABSPATH . '/wp-content/plugins/SomePlugin/SomePlugin.php',
ABSPATH . '/wp-content/plugins/AnotherPlugin/AnotherPlugin.php',
];

$this->settings->set_option( Settings::INSTANCE_COMPONENTS_INSTALLED, wp_json_encode( [ 'core' => 1 ] ) );
$this->settings->save();

$is_installed = $this->installer->is_current_instance_installed();
$this->assertFalse( $is_installed );
}

public function test_is_current_instance_installed_returns_false_if_some_non_core_plugin_not_installed() {
$GLOBALS['MATOMO_PLUGIN_FILES'] = [
ABSPATH . '/wp-content/plugins/matomo/matomo.php',
ABSPATH . '/wp-content/plugins/SomePlugin/SomePlugin.php',
ABSPATH . '/wp-content/plugins/AnotherPlugin/AnotherPlugin.php',
];

$this->settings->set_option(
Settings::INSTANCE_COMPONENTS_INSTALLED,
wp_json_encode(
[
'core' => 1,
'SomePlugin' => 1,
]
)
);
$this->settings->save();

$is_installed = $this->installer->is_current_instance_installed();
$this->assertFalse( $is_installed );
}

public function test_is_current_instance_installed_defaults_installed_components_option_to_empty_array() {
$this->settings->set_option( Settings::INSTANCE_COMPONENTS_INSTALLED, '' );
$this->settings->save();

// sanity check
$existing = $this->settings->get_option( Settings::INSTANCE_COMPONENTS_INSTALLED );
$this->assertEmpty( $existing );

$is_installed = $this->installer->is_current_instance_installed();
$this->assertFalse( $is_installed );
}

public function test_is_current_instance_installed_returns_true_if_core_and_plugins_marked_installed() {
$GLOBALS['MATOMO_PLUGIN_FILES'] = [
ABSPATH . '/wp-content/plugins/matomo/matomo.php',
ABSPATH . '/wp-content/plugins/SomePlugin/SomePlugin.php',
ABSPATH . '/wp-content/plugins/AnotherPlugin/AnotherPlugin.php',
];

$this->settings->set_option(
Settings::INSTANCE_COMPONENTS_INSTALLED,
wp_json_encode(
[
'core' => 1,
'SomePlugin' => 1,
'AnotherPlugin' => 1,
]
)
);
$this->settings->save();

$is_installed = $this->installer->is_current_instance_installed();
$this->assertTrue( $is_installed );
}
}

0 comments on commit 17247c0

Please sign in to comment.