Skip to content

Commit

Permalink
Add test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
nfmohit committed Sep 5, 2024
1 parent a1dfba3 commit 0e9075a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 10 deletions.
26 changes: 16 additions & 10 deletions includes/Core/Util/Uninstallation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 );
}
}
Expand Down
66 changes: 66 additions & 0 deletions tests/phpunit/integration/Core/Util/UninstallationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
}

0 comments on commit 0e9075a

Please sign in to comment.