From a1dfba35fea835154aab35a7f100af3441250498 Mon Sep 17 00:00:00 2001 From: nfmohit Date: Thu, 5 Sep 2024 08:48:04 +0600 Subject: [PATCH 1/2] Clear all scheduled events on deactivation/reset. --- includes/Core/Util/Uninstallation.php | 42 ++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/includes/Core/Util/Uninstallation.php b/includes/Core/Util/Uninstallation.php index 4d0abfc2cf2..e6323b9a289 100644 --- a/includes/Core/Util/Uninstallation.php +++ b/includes/Core/Util/Uninstallation.php @@ -15,7 +15,12 @@ use Google\Site_Kit\Core\Storage\Encrypted_Options; use Google\Site_Kit\Core\Authentication\Credentials; use Google\Site_Kit\Core\Authentication\Google_Proxy; -use Exception; +use Google\Site_Kit\Core\Authentication\Clients\OAuth_Client; +use Google\Site_Kit\Core\Remote_Features\Remote_Features_Cron; +use Google\Site_Kit\Modules\Analytics_4\Conversion_Reporting\Conversion_Reporting_Cron; +use Google\Site_Kit\Modules\Analytics_4\Synchronize_AdSenseLinked; +use Google\Site_Kit\Modules\Analytics_4\Synchronize_AdsLinked; +use Google\Site_Kit\Modules\Analytics_4\Synchronize_Property; /** * Utility class for handling uninstallation of the plugin. @@ -72,6 +77,21 @@ public function register() { 'googlesitekit_uninstallation', function () { $this->uninstall(); + $this->clear_scheduled_events(); + } + ); + + add_action( + 'googlesitekit_deactivation', + function () { + $this->clear_scheduled_events(); + } + ); + + add_action( + 'googlesitekit_reset', + function () { + $this->clear_scheduled_events(); } ); } @@ -90,4 +110,24 @@ private function uninstall() { $google_proxy->unregister_site( $credentials ); } } + + /** + * Clears all scheduled events. + * + * @since n.e.x.t + */ + private function clear_scheduled_events() { + $events = array( + Conversion_Reporting_Cron::CRON_ACTION, + OAuth_Client::CRON_REFRESH_PROFILE_DATA, + Remote_Features_Cron::CRON_ACTION, + Synchronize_AdSenseLinked::CRON_SYNCHRONIZE_ADSENSE_LINKED, + Synchronize_AdsLinked::CRON_SYNCHRONIZE_ADS_LINKED, + Synchronize_Property::CRON_SYNCHRONIZE_PROPERTY, + ); + + foreach ( $events as $event ) { + wp_clear_scheduled_hook( $event ); + } + } } From 0e9075ae669d35b1c68e6108c50ac5831ca94249 Mon Sep 17 00:00:00 2001 From: nfmohit Date: Thu, 5 Sep 2024 09:22:51 +0600 Subject: [PATCH 2/2] Add test coverage. --- includes/Core/Util/Uninstallation.php | 26 +++++--- .../Core/Util/UninstallationTest.php | 66 +++++++++++++++++++ 2 files changed, 82 insertions(+), 10 deletions(-) diff --git a/includes/Core/Util/Uninstallation.php b/includes/Core/Util/Uninstallation.php index e6323b9a289..fda282bde94 100644 --- a/includes/Core/Util/Uninstallation.php +++ b/includes/Core/Util/Uninstallation.php @@ -47,6 +47,21 @@ class Uninstallation { */ private $options; + /** + * List of scheduled events. + * + * @since n.e.x.t + * @var array + */ + const SCHEDULED_EVENTS = array( + Conversion_Reporting_Cron::CRON_ACTION, + OAuth_Client::CRON_REFRESH_PROFILE_DATA, + Remote_Features_Cron::CRON_ACTION, + Synchronize_AdSenseLinked::CRON_SYNCHRONIZE_ADSENSE_LINKED, + Synchronize_AdsLinked::CRON_SYNCHRONIZE_ADS_LINKED, + Synchronize_Property::CRON_SYNCHRONIZE_PROPERTY, + ); + /** * Constructor. * @@ -117,16 +132,7 @@ private function uninstall() { * @since n.e.x.t */ private function clear_scheduled_events() { - $events = array( - Conversion_Reporting_Cron::CRON_ACTION, - OAuth_Client::CRON_REFRESH_PROFILE_DATA, - Remote_Features_Cron::CRON_ACTION, - Synchronize_AdSenseLinked::CRON_SYNCHRONIZE_ADSENSE_LINKED, - Synchronize_AdsLinked::CRON_SYNCHRONIZE_ADS_LINKED, - Synchronize_Property::CRON_SYNCHRONIZE_PROPERTY, - ); - - foreach ( $events as $event ) { + foreach ( self::SCHEDULED_EVENTS as $event ) { wp_clear_scheduled_hook( $event ); } } diff --git a/tests/phpunit/integration/Core/Util/UninstallationTest.php b/tests/phpunit/integration/Core/Util/UninstallationTest.php index 3d0e07807f4..9e50b631e8b 100644 --- a/tests/phpunit/integration/Core/Util/UninstallationTest.php +++ b/tests/phpunit/integration/Core/Util/UninstallationTest.php @@ -68,10 +68,76 @@ public function test_uninstall_not_using_proxy() { $this->assertFalse( $this->issued_delete_site_request ); } + public function test_clear_scheduled_events__uninstall() { + $this->set_scheduled_events(); + + // Assert scheduled events were set. + foreach ( Uninstallation::SCHEDULED_EVENTS as $event ) { + $this->assertNotEmpty( wp_next_scheduled( $event ) ); + } + + remove_all_actions( 'googlesitekit_uninstallation' ); + + $this->uninstallation->register(); + + do_action( 'googlesitekit_uninstallation' ); + + // Assert scheduled events were cleared. + foreach ( Uninstallation::SCHEDULED_EVENTS as $event ) { + $this->assertEmpty( wp_next_scheduled( $event ) ); + } + } + + public function test_clear_scheduled_events__reset() { + $this->set_scheduled_events(); + + // Assert scheduled events were set. + foreach ( Uninstallation::SCHEDULED_EVENTS as $event ) { + $this->assertNotEmpty( wp_next_scheduled( $event ) ); + } + + remove_all_actions( 'googlesitekit_reset' ); + + $this->uninstallation->register(); + + do_action( 'googlesitekit_reset' ); + + // Assert scheduled events were cleared. + foreach ( Uninstallation::SCHEDULED_EVENTS as $event ) { + $this->assertEmpty( wp_next_scheduled( $event ) ); + } + } + + public function test_clear_scheduled_events__deactivation() { + $this->set_scheduled_events(); + + // Assert scheduled events were set. + foreach ( Uninstallation::SCHEDULED_EVENTS as $event ) { + $this->assertNotEmpty( wp_next_scheduled( $event ) ); + } + + remove_all_actions( 'googlesitekit_deactivation' ); + + $this->uninstallation->register(); + + do_action( 'googlesitekit_deactivation' ); + + // Assert scheduled events were cleared. + foreach ( Uninstallation::SCHEDULED_EVENTS as $event ) { + $this->assertEmpty( wp_next_scheduled( $event ) ); + } + } + public function check_proxy_delete_site_url( $preempt, $args, $url ) { if ( $this->google_proxy->url( Google_Proxy::OAUTH2_DELETE_SITE_URI ) === $url ) { $this->issued_delete_site_request = true; } return $preempt; } + + private function set_scheduled_events() { + foreach ( Uninstallation::SCHEDULED_EVENTS as $event ) { + wp_schedule_event( time(), 'daily', $event ); + } + } }