Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/WorkOS/Admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,17 @@ private function register_all_options(): void {
'type' => 'string',
'default' => 'staging',
'sanitize_callback' => function ( $value ) {
return in_array( $value, [ 'production', 'staging' ], true ) ? $value : 'staging';
/*
* The settings form does not submit this option; environment
* activation is handled by handle_activate_environment().
* Preserve the existing value when options.php sanitizes the
* missing field during a normal credential/settings save.
*/
if ( ! in_array( $value, [ 'production', 'staging' ], true ) ) {
return Config::get_active_environment();
}

return $value;
},
]
);
Expand Down
47 changes: 47 additions & 0 deletions tests/wpunit/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace WorkOS\Tests\Wpunit;

use lucatume\WPBrowser\TestCase\WPTestCase;
use WorkOS\Admin\Settings;
use WorkOS\App;
use WorkOS\Config;
use WorkOS\Database\Schema;
Expand Down Expand Up @@ -117,6 +118,52 @@ public function test_migrate_active_environment_moves_legacy_value_to_standalone
$this->assertSame( 3, (int) get_option( 'workos_db_version' ) );
}

/**
* The settings form edits production/staging option rows but does not post
* workos_active_environment. A normal save must not coerce the missing
* active-environment field back to staging.
*/
public function test_settings_save_preserves_active_environment_when_field_is_missing(): void {
if ( ! defined( 'WORKOS_BASENAME' ) ) {
define( 'WORKOS_BASENAME', 'integration-workos/integration-workos.php' );
}

update_option( 'workos_active_environment', 'production' );

$settings = new Settings();
$settings->register_settings();

update_option( 'workos_active_environment', null );

$this->assertSame( 'production', get_option( 'workos_active_environment' ) );
}

/**
* Legacy installs may still have the active environment only in
* workos_global. A settings save must preserve that fallback instead of
* writing the standalone row as staging.
*/
public function test_settings_save_preserves_legacy_active_environment_when_field_is_missing(): void {
if ( ! defined( 'WORKOS_BASENAME' ) ) {
define( 'WORKOS_BASENAME', 'integration-workos/integration-workos.php' );
}

update_option(
'workos_global',
[
'active_environment' => 'production',
]
);
App::container()->get( Global_Options::class )->reset();

$settings = new Settings();
$settings->register_settings();

update_option( 'workos_active_environment', null );

$this->assertSame( 'production', get_option( 'workos_active_environment' ) );
}

/**
* Test mask_secret with a long value.
*/
Expand Down