Skip to content

Commit 489c5df

Browse files
authored
Merge pull request #34 from bordoni/fix/maintain-workos-environment
Fix: ensure workos environment doesn't get set to `staging` on save
2 parents eef1dd0 + bf0f383 commit 489c5df

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

src/WorkOS/Admin/Settings.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,17 @@ private function register_all_options(): void {
513513
'type' => 'string',
514514
'default' => 'staging',
515515
'sanitize_callback' => function ( $value ) {
516-
return in_array( $value, [ 'production', 'staging' ], true ) ? $value : 'staging';
516+
/*
517+
* The settings form does not submit this option; environment
518+
* activation is handled by handle_activate_environment().
519+
* Preserve the existing value when options.php sanitizes the
520+
* missing field during a normal credential/settings save.
521+
*/
522+
if ( ! in_array( $value, [ 'production', 'staging' ], true ) ) {
523+
return Config::get_active_environment();
524+
}
525+
526+
return $value;
517527
},
518528
]
519529
);

tests/wpunit/ConfigTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace WorkOS\Tests\Wpunit;
99

1010
use lucatume\WPBrowser\TestCase\WPTestCase;
11+
use WorkOS\Admin\Settings;
1112
use WorkOS\App;
1213
use WorkOS\Config;
1314
use WorkOS\Database\Schema;
@@ -117,6 +118,52 @@ public function test_migrate_active_environment_moves_legacy_value_to_standalone
117118
$this->assertSame( 3, (int) get_option( 'workos_db_version' ) );
118119
}
119120

121+
/**
122+
* The settings form edits production/staging option rows but does not post
123+
* workos_active_environment. A normal save must not coerce the missing
124+
* active-environment field back to staging.
125+
*/
126+
public function test_settings_save_preserves_active_environment_when_field_is_missing(): void {
127+
if ( ! defined( 'WORKOS_BASENAME' ) ) {
128+
define( 'WORKOS_BASENAME', 'integration-workos/integration-workos.php' );
129+
}
130+
131+
update_option( 'workos_active_environment', 'production' );
132+
133+
$settings = new Settings();
134+
$settings->register_settings();
135+
136+
update_option( 'workos_active_environment', null );
137+
138+
$this->assertSame( 'production', get_option( 'workos_active_environment' ) );
139+
}
140+
141+
/**
142+
* Legacy installs may still have the active environment only in
143+
* workos_global. A settings save must preserve that fallback instead of
144+
* writing the standalone row as staging.
145+
*/
146+
public function test_settings_save_preserves_legacy_active_environment_when_field_is_missing(): void {
147+
if ( ! defined( 'WORKOS_BASENAME' ) ) {
148+
define( 'WORKOS_BASENAME', 'integration-workos/integration-workos.php' );
149+
}
150+
151+
update_option(
152+
'workos_global',
153+
[
154+
'active_environment' => 'production',
155+
]
156+
);
157+
App::container()->get( Global_Options::class )->reset();
158+
159+
$settings = new Settings();
160+
$settings->register_settings();
161+
162+
update_option( 'workos_active_environment', null );
163+
164+
$this->assertSame( 'production', get_option( 'workos_active_environment' ) );
165+
}
166+
120167
/**
121168
* Test mask_secret with a long value.
122169
*/

0 commit comments

Comments
 (0)