From bf0f383d20035eb9bc1d59ab4dd157779c943065 Mon Sep 17 00:00:00 2001 From: Justin Frydman Date: Tue, 23 Jun 2026 15:15:00 -0600 Subject: [PATCH] Fix: ensure workos environment doesn't get set to `staging` on save every time --- src/WorkOS/Admin/Settings.php | 12 ++++++++- tests/wpunit/ConfigTest.php | 47 +++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/WorkOS/Admin/Settings.php b/src/WorkOS/Admin/Settings.php index c30f044..6f5df50 100644 --- a/src/WorkOS/Admin/Settings.php +++ b/src/WorkOS/Admin/Settings.php @@ -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; }, ] ); diff --git a/tests/wpunit/ConfigTest.php b/tests/wpunit/ConfigTest.php index 426b34e..066d24c 100644 --- a/tests/wpunit/ConfigTest.php +++ b/tests/wpunit/ConfigTest.php @@ -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; @@ -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. */