diff options
author | Ozh <ozh@ozh.org> | 2022-03-28 19:52:13 +0300 |
---|---|---|
committer | Ozh <ozh@ozh.org> | 2022-03-28 19:52:13 +0300 |
commit | 30375951d5f564ddcc7c648361117dee6dc14735 (patch) | |
tree | 94a7ecaba24a9e8d050744e4a139ec6280b9543a | |
parent | 159b86deaf96150aef828a940052fb74aabf2b3c (diff) |
Uninstall script and testspluginstuff
-rw-r--r-- | includes/functions-plugins.php | 10 | ||||
-rw-r--r-- | tests/data/plugins/test-plugin/uninstall.php | 12 | ||||
-rw-r--r-- | tests/data/plugins/test-plugin2/uninstall.php | 12 | ||||
-rw-r--r-- | tests/tests/plugins/files.php | 34 |
4 files changed, 68 insertions, 0 deletions
diff --git a/includes/functions-plugins.php b/includes/functions-plugins.php index 961d3cf8..b506a999 100644 --- a/includes/functions-plugins.php +++ b/includes/functions-plugins.php @@ -684,6 +684,16 @@ function yourls_deactivate_plugin( $plugin ) { return yourls__( 'Plugin not active' ); } + // Check if we have an uninstall file - load if so + $uninst_file = YOURLS_PLUGINDIR . '/' . dirname($plugin) . '/uninstall.php'; + if ( file_exists($uninst_file) ) { + define('YOURLS_UNINSTALL_PLUGIN', true); + $attempt = yourls_activate_plugin_sandbox( $uninst_file ); + if( $attempt !== true ) { + return yourls_s( 'Plugin generated unexpected output. Error was: <br/><pre>%s</pre>', $attempt ); + } + } + // Deactivate the plugin $ydb = yourls_get_db(); $plugins = $ydb->get_plugins(); diff --git a/tests/data/plugins/test-plugin/uninstall.php b/tests/data/plugins/test-plugin/uninstall.php new file mode 100644 index 00000000..5cab13eb --- /dev/null +++ b/tests/data/plugins/test-plugin/uninstall.php @@ -0,0 +1,12 @@ +<?php +/** + * Uninstall script + * This file is executed when the plugin is uninstalled on YOURLS 1.8.3 or later. + */ + +// No direct call. +if( !defined( 'YOURLS_UNINSTALL_PLUGIN' ) ) die(); + +// The uninstallation process itself + +// Nothing. diff --git a/tests/data/plugins/test-plugin2/uninstall.php b/tests/data/plugins/test-plugin2/uninstall.php new file mode 100644 index 00000000..5cab13eb --- /dev/null +++ b/tests/data/plugins/test-plugin2/uninstall.php @@ -0,0 +1,12 @@ +<?php +/** + * Uninstall script + * This file is executed when the plugin is uninstalled on YOURLS 1.8.3 or later. + */ + +// No direct call. +if( !defined( 'YOURLS_UNINSTALL_PLUGIN' ) ) die(); + +// The uninstallation process itself + +// Nothing. diff --git a/tests/tests/plugins/files.php b/tests/tests/plugins/files.php index 03f2c47f..6062c762 100644 --- a/tests/tests/plugins/files.php +++ b/tests/tests/plugins/files.php @@ -76,9 +76,27 @@ class Plugin_Files_Tests extends PHPUnit\Framework\TestCase { */ public function test_plugin_activate() { $plugin = $this->pick_a_plugin(); + + // Make sure the plugin.php is NOT present in get_included_files() + // We sanitize the array to deal with different platforms (D:\hello\Windows vs /home/user/hello/Linux) + $this->assertFalse(in_array(yourls_sanitize_filename(YOURLS_PLUGINDIR.'/'.$plugin), + array_map('yourls_sanitize_filename', get_included_files()))); + + // Activate the plugin $this->assertTrue( yourls_activate_plugin( $plugin ) ); $this->assertGreaterThan( 0, yourls_has_active_plugins() ); $this->assertTrue( yourls_is_active_plugin( $plugin ) ); + + // Make sure the plugin.php is now present in get_included_files() + $included_files = array_map('yourls_sanitize_filename', get_included_files()) ; + $this->assertTrue(in_array(yourls_sanitize_filename(YOURLS_PLUGINDIR.'/'.$plugin), $included_files)); + + // Make sure the plugin's uninstall.php is NOT present in get_included_files() + $this->assertFalse(in_array(yourls_sanitize_filename(YOURLS_PLUGINDIR.'/'.dirname($plugin).'/uninstall.php'),$included_files)); + + // We should NOT have YOURLS_UNINSTALL_PLUGIN defined + $this->assertFalse(defined('YOURLS_UNINSTALL_PLUGIN')); + return $plugin; } @@ -89,6 +107,7 @@ class Plugin_Files_Tests extends PHPUnit\Framework\TestCase { */ public function test_plugin_activate_twice( $plugin ) { $this->assertNotSame( true, yourls_activate_plugin( $plugin ) ); + // Note: we assertNotSame() with true because the function either returns true or a string return $plugin; } @@ -132,8 +151,23 @@ class Plugin_Files_Tests extends PHPUnit\Framework\TestCase { $this->assertTrue( yourls_deactivate_plugin($plugin) ); $this->assertSame( 0, yourls_has_active_plugins() ); $this->assertFalse( yourls_is_active_plugin($plugin) ); + return $plugin; } + /** + * Check that deactivating a plugin correctly ran the uninstall script + * + * @depends test_plugin_deactivate + */ + public function test_plugin_uninstall( $plugin ) { + // Make sure uninstall.php is NOW present in get_included_files() + $this->assertTrue( in_array( yourls_sanitize_filename(YOURLS_PLUGINDIR . '/' . dirname($plugin) . '/uninstall.php'), + array_map('yourls_sanitize_filename', get_included_files())) ); + + // we should now have YOURLS_UNINSTALL_PLUGIN set to true + $this->assertTrue( defined('YOURLS_UNINSTALL_PLUGIN') && YOURLS_UNINSTALL_PLUGIN ); + } + /** * Check that an missing plugin does not activate */ |