Skip to content

Commit

Permalink
Add tests for Admin_Settings::reset_settings() (#231)
Browse files Browse the repository at this point in the history
* Add tests for `Admin_Settings::reset_settings()`.

* Do not exit during test runs.
  • Loading branch information
costdev authored Dec 4, 2024
1 parent 8d73e5b commit a324757
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 1 deletion.
2 changes: 1 addition & 1 deletion includes/class-admin-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function reset_settings() {
network_admin_url( 'index.php?page=aspireupdate-settings' )
)
);
exit;
! defined( 'AP_RUN_TESTS' ) && exit;
}
}

Expand Down
183 changes: 183 additions & 0 deletions tests/phpunit/tests/AdminSettings/AdminSettings_ResetSettingsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
<?php
/**
* Class AdminSettings_ResetSettingsTest
*
* @package AspireUpdate
*/

/**
* Tests for Admin_Settings::reset_settings()
*
* @covers \AspireUpdate\Admin_Settings::reset_settings
*/
class AdminSettings_ResetSettingsTest extends AdminSettings_UnitTestCase {
/**
* Test that settings are not reset when $_GET['reset'] is not set.
*/
public function test_should_not_reset_settings_when_get_reset_is_not_set() {
unset( $_GET['reset'] );

$settings = [ 'api_host' => 'the.option.value' ];
update_site_option( self::$option_name, $settings );

$admin_settings = new \AspireUpdate\Admin_Settings();
$admin_settings->reset_settings();

$this->assertSame( $settings, get_site_option( self::$option_name ) );
}

/**
* Test that a redirect is not performed when $_GET['reset'] is not set.
*/
public function test_should_not_redirect_when_get_reset_is_not_set() {
unset( $_GET['reset'] );

$settings = [ 'api_host' => 'the.option.value' ];
update_site_option( self::$option_name, $settings );

$redirect = new MockAction();
add_filter( 'wp_redirect', [ $redirect, 'filter' ] );

$admin_settings = new \AspireUpdate\Admin_Settings();
$admin_settings->reset_settings();

$this->assertSame( 0, $redirect->get_call_count() );
}

/**
* Test that settings are not reset when $_GET['reset'] is set to an incorrect value.
*/
public function test_should_not_reset_settings_when_get_reset_is_set_to_an_incorrect_value() {
$_GET['reset'] = 'incorrect_value';
$settings = [ 'api_host' => 'the.option.value' ];
update_site_option( self::$option_name, $settings );

$admin_settings = new \AspireUpdate\Admin_Settings();
$admin_settings->reset_settings();

unset( $_GET['reset'] );

$this->assertSame( $settings, get_site_option( self::$option_name ) );
}

/**
* Test that a redirect is not performed when $_GET['reset'] is set to an incorrect value.
*/
public function test_should_not_redirect_when_get_reset_is_set_to_an_incorrect_value() {
$_GET['reset'] = 'incorrect_value';
$settings = [ 'api_host' => 'the.option.value' ];
update_site_option( self::$option_name, $settings );

$redirect = new MockAction();
add_filter( 'wp_redirect', [ $redirect, 'filter' ] );

$admin_settings = new \AspireUpdate\Admin_Settings();
$admin_settings->reset_settings();

unset( $_GET['reset'] );

$this->assertSame( 0, $redirect->get_call_count() );
}

/**
* Test that settings are not reset when $_GET['reset-nonce'] is not set.
*/
public function test_should_not_reset_settings_when_get_resetnonce_is_not_set() {
unset( $_GET['reset-nonce'] );

$_GET['reset'] = 'reset';
$settings = [ 'api_host' => 'the.option.value' ];
update_site_option( self::$option_name, $settings );

$admin_settings = new \AspireUpdate\Admin_Settings();
$admin_settings->reset_settings();

unset( $_GET['reset'] );

$this->assertSame( $settings, get_site_option( self::$option_name ) );
}

/**
* Test that settings are not reset when nonce verification fails.
*/
public function test_should_not_reset_settings_when_nonce_verification_fails() {
$_GET['reset'] = 'reset';
$_GET['reset-nonce'] = 'an_invalid_value';

$settings = [ 'api_host' => 'the.option.value' ];
update_site_option( self::$option_name, $settings );

$admin_settings = new \AspireUpdate\Admin_Settings();
$admin_settings->reset_settings();

unset( $_GET['reset'], $_GET['reset-nonce'] );

$this->assertSame( $settings, get_site_option( self::$option_name ) );
}

/**
* Test that a redirect is not performed when nonce verification fails.
*/
public function test_should_not_redirect_settings_when_nonce_verification_fails() {
$_GET['reset'] = 'reset';
$_GET['reset-nonce'] = 'an_invalid_value';

$settings = [ 'api_host' => 'the.option.value' ];
update_site_option( self::$option_name, $settings );

$redirect = new MockAction();
add_filter( 'wp_redirect', [ $redirect, 'filter' ] );

$admin_settings = new \AspireUpdate\Admin_Settings();
$admin_settings->reset_settings();

unset( $_GET['reset'], $_GET['reset-nonce'] );

$this->assertSame( 0, $redirect->get_call_count() );
}

/**
* Test that settings are reset when reset requirements are met.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_should_reset_settings_when_reset_requirements_are_met() {
$_GET['reset'] = 'reset';
$_GET['reset-nonce'] = wp_create_nonce( 'aspireupdate-reset-nonce' );

$settings = [ 'api_host' => 'the.option.value' ];
update_site_option( self::$option_name, $settings );

$admin_settings = new \AspireUpdate\Admin_Settings();
$admin_settings->reset_settings();

unset( $_GET['reset'], $_GET['reset-nonce'] );

$this->assertNotSame( $settings, get_site_option( self::$option_name ) );
}

/**
* Test that a redirect is performed when reset requirements are met.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_should_redirect_when_reset_requirements_are_met() {
$_GET['reset'] = 'reset';
$_GET['reset-nonce'] = wp_create_nonce( 'aspireupdate-reset-nonce' );

$settings = [ 'api_host' => 'the.option.value' ];
update_site_option( self::$option_name, $settings );

$redirect = new MockAction();
add_filter( 'wp_redirect', [ $redirect, 'filter' ] );

$admin_settings = new \AspireUpdate\Admin_Settings();
$admin_settings->reset_settings();

unset( $_GET['reset'], $_GET['reset-nonce'] );

$this->assertSame( 1, $redirect->get_call_count() );
}
}

0 comments on commit a324757

Please sign in to comment.