Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Allow upcoming changes to control site setting defaults (#38815)

Martin Brennandd8c16cbd615
diff --git a/.skills/discourse-upcoming-changes-authoring/SKILL.md b/.skills/discourse-upcoming-changes-authoring/SKILL.md
index 89b8b48b0ac..0fe38cbb178 100644
--- a/.skills/discourse-upcoming-changes-authoring/SKILL.md
+++ b/.skills/discourse-upcoming-changes-authoring/SKILL.md
@@ -24,7 +24,9 @@ Key methods to understand:
**`app/models/upcoming_change_event.rb`** — Audit trail. Every lifecycle event (added, removed, status change, manual toggle, admin notification) is recorded here. Has unique indexes to prevent duplicate events of specific types per change.
-**`lib/site_setting_extension.rb`** — Where `upcoming_change:` metadata in `site_settings.yml` gets parsed. When a setting is registered with this metadata, it stores the parsed result in `@upcoming_change_metadata` and defines a `{name}_groups_map` method. The `impact` string is split into `impact_type` and `impact_role`.
+**`lib/site_setting_extension.rb`** — Where `upcoming_change:` metadata in `site_settings.yml` gets parsed. When a setting is registered with this metadata, it stores the parsed result in `@upcoming_change_metadata` and defines a `{name}_groups_map` method. The `impact` string is split into `impact_type` and `impact_role`. Also handles `upcoming_change_default_override:` metadata — see [Default Overrides](#default-overrides) below.
+
+**`lib/site_settings/defaults_provider.rb`** — Manages the default values for all settings, including upcoming change default overrides. Tracks which overrides are active via `@active_upcoming_change_overrides` and applies them when resolving defaults. Provides `upcoming_change_override_metadata` for the frontend to display warnings about changed defaults.
**`app/models/site_setting_group.rb`** — Stores group restrictions for settings. Group IDs are pipe-separated strings (`"1|2|3"`). The `setting_group_ids` class method returns a hash used for in-memory caching.
@@ -82,6 +84,51 @@ All services use `Service::Base`. They're organized under `app/services/upcoming
**`app/services/problem_check/upcoming_change_stable_opted_out.rb`** — Warns admins hourly if they've opted out of a stable/permanent change.
+### Default Overrides
+
+Upcoming changes can override the default value of a *different* site setting when enabled. This allows feature rollouts to change related setting defaults without breaking admin customizations.
+
+#### Metadata Format
+
+A setting declares a default override with the `upcoming_change_default_override` key in `config/site_settings.yml`:
+
+```yaml
+# The upcoming change setting (the "trigger")
+increase_suggested_topics_max_days_old_default:
+ default: false
+ type: bool
+ upcoming_change:
+ status: experimental
+ impact: "site_setting_default,all_members"
+
+# The setting whose default changes (the "target")
+suggested_topics_max_days_old:
+ default: 365
+ type: integer
+ upcoming_change_default_override:
+ upcoming_change: increase_suggested_topics_max_days_old_default
+ new_default: 1000
+```
+
+When `increase_suggested_topics_max_days_old_default` is enabled (either manually by admin or via auto-promotion), the default value of `suggested_topics_max_days_old` changes from `365` to `1000`. The `impact` field on the trigger setting should include `site_setting_default` as its `impact_type`.
+
+#### How It Works
+
+1. **Registration** — `lib/site_setting_extension.rb` parses `upcoming_change_default_override` during setting registration and stores it in `upcoming_change_default_overrides` (a hash keyed by setting name).
+
+2. **Activation** — During `SiteSetting.refresh!`, each override is checked: if `UpcomingChanges.enabled?(override[:upcoming_change])` returns true, the override is activated via `defaults.activate_upcoming_change_override`. The setting's current value is updated to `new_default` **only if the admin has not manually modified it**.
+
+3. **Default resolution** — `DefaultsProvider#all` applies active overrides when resolving defaults, so code reading `SiteSetting.defaults[:setting_name]` gets the overridden value.
+
+4. **Frontend display** — `DefaultsProvider#upcoming_change_override_metadata` returns `{ old_default:, new_default:, change_setting_name: }` for active overrides. The site settings UI (`admin/components/site-setting.gjs`) shows a warning linking to the upcoming changes page.
+
+#### Key Behaviors
+
+- **Non-destructive**: If an admin has manually set a custom value for the target setting, the override does not apply — it only affects the default.
+- **Reversible**: Disabling the upcoming change deactivates the override and restores the original default.
+- **Default-locale only**: Overrides currently only apply on the default locale.
+- **Related setting link**: `UpcomingChanges.find_related_default_override_for_change` finds the target setting for a given upcoming change, used to show cross-links in the UI.
+
## Key Design Decisions
### Caching Strategy
@@ -133,6 +180,22 @@ The three main components to know:
State is managed via `trackedObject` for reactivity. API calls go through `ajax()` directly in the item component.
+### Adding a Default Override
+
+To make an upcoming change control the default of another setting:
+
+1. Add `upcoming_change_default_override` metadata to the **target** setting in `config/site_settings.yml`:
+ ```yaml
+ target_setting:
+ default: original_value
+ upcoming_change_default_override:
+ upcoming_change: trigger_setting_name
+ new_default: new_value
+ ```
+2. Ensure the **trigger** setting has `impact: "site_setting_default,..."` in its `upcoming_change:` metadata
+3. The override activates automatically when the trigger is enabled — no additional code needed
+4. Test with `mock_upcoming_change_default_overrides` — see [Mocking Default Overrides](#mocking-default-overrides)
+
### Changing Resolution Logic
All value resolution goes through `resolved_value` in `lib/upcoming_changes.rb`. If you need to change how settings are evaluated (e.g., adding a new override condition), this is the single place to modify. The method checks in order: permanent status, admin manual override, auto-promotion threshold.
@@ -158,6 +221,39 @@ mock_upcoming_change_metadata(
Always clean up with `clear_mocked_upcoming_change_metadata` in an `after` block (or the helper handles it automatically depending on context). The helper is defined in `spec/support/helpers.rb`.
+### Mocking Default Overrides
+
+Use `mock_upcoming_change_default_overrides` to set up override metadata in tests — never modify `site_settings.yml`:
+
+```ruby
+mock_upcoming_change_default_overrides(
+ {
+ suggested_topics_max_days_old: {
+ upcoming_change: :increase_suggested_topics_max_days_old_default,
+ new_default: 1000,
+ },
+ },
+)
+
+# Enable the trigger setting and refresh to activate the override
+SiteSetting.increase_suggested_topics_max_days_old_default = true
+SiteSetting.refresh!
+
+# Now SiteSetting.suggested_topics_max_days_old returns 1000 (the overridden default)
+```
+
+To test that the override does NOT apply when the admin has customized the target setting:
+
+```ruby
+# Admin sets a custom value before the override activates
+SiteSetting.suggested_topics_max_days_old = 730
+SiteSetting.increase_suggested_topics_max_days_old_default = true
+SiteSetting.refresh!
+
+# Override is not applied — admin's explicit choice is preserved
+expect(SiteSetting.suggested_topics_max_days_old).to eq(730)
+```
+
### Cache Clearing in Tests
After modifying metadata or settings, call `UpcomingChanges.clear_caches!` to ensure tests see fresh data. The caches are keyed by git version, so they persist across test examples unless explicitly cleared.
@@ -210,6 +306,7 @@ Notification type tests create notifications with `Notification.create()` and ve
| Event model | `app/models/upcoming_change_event.rb` |
| Group model | `app/models/site_setting_group.rb` |
| Settings integration | `lib/site_setting_extension.rb` (search for `upcoming_change`) |
+| Defaults provider | `lib/site_settings/defaults_provider.rb` (default override activation/resolution) |
| Services | `app/services/upcoming_changes/*.rb` |
| Group upsert | `app/services/site_setting/upsert_groups.rb` |
| Controller | `admin/config/upcoming_changes_controller.rb` |
diff --git a/app/assets/stylesheets/admin/settings.scss b/app/assets/stylesheets/admin/settings.scss
index 7b43301911a..03d5cf3de67 100644
--- a/app/assets/stylesheets/admin/settings.scss
+++ b/app/assets/stylesheets/admin/settings.scss
@@ -64,7 +64,7 @@
}
}
- .setting-theme-warning {
+ .setting-override-warning {
font-size: var(--font-down-1);
color: var(--primary-medium);
diff --git a/app/assets/stylesheets/admin/upcoming-changes.scss b/app/assets/stylesheets/admin/upcoming-changes.scss
index d52a5da949c..ff9429c0f3c 100644
--- a/app/assets/stylesheets/admin/upcoming-changes.scss
+++ b/app/assets/stylesheets/admin/upcoming-changes.scss
@@ -21,7 +21,8 @@
width: 35%;
}
- &__dependents {
+ &__dependents,
+ &__related {
margin-top: var(--space-2);
}
diff --git a/app/services/upcoming_changes/list.rb b/app/services/upcoming_changes/list.rb
index d7cae3a5dee..484dd312978 100644
--- a/app/services/upcoming_changes/list.rb
+++ b/app/services/upcoming_changes/list.rb
@@ -60,7 +60,8 @@ class UpcomingChanges::List
:upcoming_change,
:plugin,
).merge(
- dependents: SiteSetting.type_supervisor.dependencies.dependents(setting[:setting].to_s),
+ dependents: UpcomingChanges.find_dependents_for_change(setting[:setting]),
+ related: UpcomingChanges.find_related_default_override_for_change(setting[:setting]),
)
end
end
diff --git a/app/services/upcoming_changes/notify_promotion.rb b/app/services/upcoming_changes/notify_promotion.rb
index 1800666b6c6..d044915241a 100644
--- a/app/services/upcoming_changes/notify_promotion.rb
+++ b/app/services/upcoming_changes/notify_promotion.rb
@@ -58,7 +58,7 @@ class UpcomingChanges::NotifyPromotion
end
def admin_has_not_manually_toggled(params:)
- !SiteSetting.modified.key?(params.setting_name)
+ !SiteSetting.setting_modified_from_default?(params.setting_name)
end
def should_notify_admins(params:)
diff --git a/app/services/upcoming_changes/toggle.rb b/app/services/upcoming_changes/toggle.rb
index bb4cffc77c8..678bdc083a2 100644
--- a/app/services/upcoming_changes/toggle.rb
+++ b/app/services/upcoming_changes/toggle.rb
@@ -8,7 +8,7 @@ class UpcomingChanges::Toggle
options { attribute :log_change, default: true }
params do
- attribute :setting_name, :string
+ attribute :setting_name, :symbol
attribute :enabled, :boolean
validates :setting_name, presence: true
validates :enabled, inclusion: [true, false]
@@ -43,12 +43,11 @@ class UpcomingChanges::Toggle
def toggle(params:, guardian:, options:)
context[:previous_value] = SiteSetting.public_send(params.setting_name)
-
SiteSetting.send("#{params.setting_name}=", params.enabled)
end
def clear_groups_if_disallowed(params:)
- metadata = SiteSetting.upcoming_change_metadata[params.setting_name.to_sym]
+ metadata = SiteSetting.upcoming_change_metadata[params.setting_name]
return if !metadata || !metadata[:disallow_enabled_for_groups]
SiteSettingGroup.find_by(name: params.setting_name)&.destroy!
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 148026a57c4..a43725c9c17 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -6114,6 +6114,8 @@ en:
permanent_no_group_selection: "N/A, permanent changes apply to all groups."
no_changes: "There are no upcoming changes at this time."
preview: "Preview"
+ default_warning: "The default for this setting has changed from %{oldDefaultValue} to %{newDefaultValue} due to an <a href='%{basePath}/admin/config/upcoming-changes?changeNamesFilter=%{changeNamesFilter}'>upcoming change</a>."
+ default_warning_short: "The default for this setting has changed, read more about this in the <a href='%{basePath}/admin/config/upcoming-changes?changeNamesFilter=%{changeNamesFilter}'>upcoming changes</a> page."
enabled_for_options:
no_one: "No one"
everyone: "Everyone"
@@ -6151,6 +6153,7 @@ en:
impact_type_all: "All impact types"
impact_type_feature: "Feature"
impact_type_other: "Other"
+ impact_type_site_setting_default: "Setting default"
status_all: "All statuses"
status_conceptual: "Conceptual"
status_experimental: "Experimental"
diff --git a/config/site_settings.yml b/config/site_settings.yml
index e1d0e5d7736..8a1a899ceec 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -24,9 +24,12 @@
# is changed.
# themeable - Set to true if themes can override this site setting. Generally, only client side affecting settings
# that change the UI should be themeable, and try limit to simple setting types like bool, list, integer, enum.
+# upcoming_change_default_override - Allow an upcoming change to control the new default value of this setting.
+# - upcoming_change: The site setting of the upcoming change to control this
+# - new_default: The new default value when the upcoming change is enabled
# upcoming_change - Additional metadata with the following options to show in the /admin/config/upcoming-changes page:
# - status: experimental | alpha | beta | stable
-# - impact: two parts separated by comma - feature|other , staff|admins|moderators|all_members|developers
+# - impact: two parts separated by comma - feature|other|site_setting_default , staff|admins|moderators|all_members|developers
# - learn_more_url: a url for more information
# - disallow_enabled_for_groups: true (optional) - restricts "Enabled for"
# to only "Everyone" and "No One", removing group-based options.
diff --git a/frontend/discourse/admin/components/admin-config-areas/upcoming-change-item.gjs b/frontend/discourse/admin/components/admin-config-areas/upcoming-change-item.gjs
index c8db1756799..8167427f6ee 100644
--- a/frontend/discourse/admin/components/admin-config-areas/upcoming-change-item.gjs
+++ b/frontend/discourse/admin/components/admin-config-areas/upcoming-change-item.gjs
@@ -103,6 +103,10 @@ export default class UpcomingChangeItem extends Component {
);
}
+ get showRelatedSettingLink() {
+ return this.args.change.related && this.bufferedEnabledFor !== "no_one";
+ }
+
@action
groupFinder(term) {
return Group.findAll({ term, ignore_automatic: false });
@@ -419,6 +423,17 @@ export default class UpcomingChangeItem extends Component {
</div>
{{/if}}
+ {{#if this.showRelatedSettingLink}}
+ <div class="upcoming-change__related">
+ <LinkTo
+ @route="adminSiteSettings"
+ @query={{hash [email protected]}}
+ >
+ {{i18n "admin.upcoming_changes.show_related_settings"}}
+ </LinkTo>
+ </div>
+ {{/if}}
+
{{#if (eq this.bufferedEnabledFor "groups")}}
<div class="upcoming-change__group-selection-wrapper">
<GroupSelector
diff --git a/frontend/discourse/admin/components/admin-config-areas/upcoming-changes.gjs b/frontend/discourse/admin/components/admin-config-areas/upcoming-changes.gjs
index a155c89573a..a5f06eee8bf 100644
--- a/frontend/discourse/admin/components/admin-config-areas/upcoming-changes.gjs
+++ b/frontend/discourse/admin/components/admin-config-areas/upcoming-changes.gjs
@@ -77,6 +77,14 @@ export default class AdminConfigAreasUpcomingChanges extends Component {
value: "other",
filterFn: (change) => change.upcoming_change.impact_type === "other",
},
+ {
+ label: i18n(
+ "admin.upcoming_changes.filter.impact_type_site_setting_default"
+ ),
+ value: "site_setting_default",
+ filterFn: (change) =>
+ change.upcoming_change.impact_type === "site_setting_default",
+ },
],
impactRole: [
{
diff --git a/frontend/discourse/admin/components/site-setting.gjs b/frontend/discourse/admin/components/site-setting.gjs
index e9fd4635d6b..ae1f2ef2d94 100644
--- a/frontend/discourse/admin/components/site-setting.gjs
+++ b/frontend/discourse/admin/components/site-setting.gjs
@@ -149,6 +149,10 @@ export default class SiteSettingComponent extends Component {
return this.setting.themeable;
}
+ get showUpcomingChangeDefaultWarning() {
+ return this.setting.upcoming_change_default_override_metadata;
+ }
+
get themeSiteSettingWarningText() {
return trustHTML(
i18n("admin.theme_site_settings.site_setting_warning", {
@@ -159,6 +163,45 @@ export default class SiteSettingComponent extends Component {
);
}
+ get upcomingChangeDefaultWarningText() {
+ // We only want to show the changed value for basic setting
+ // types otherwise the warning might get too long (e.g. strings)
+ // or hard to represent (e.g. upload, tag group list etc.)
+ if (
+ [
+ "icon",
+ "enum",
+ "email",
+ "username",
+ "bool",
+ "integer",
+ "float",
+ ].includes(this.setting.type)
+ ) {
+ return trustHTML(
+ i18n("admin.upcoming_changes.default_warning", {
+ basePath,
+ changeNamesFilter:
+ this.setting.upcoming_change_default_override_metadata
+ .change_setting_name,
+ oldDefaultValue:
+ this.setting.upcoming_change_default_override_metadata.old_default,
+ newDefaultValue:
+ this.setting.upcoming_change_default_override_metadata.new_default,
+ })
+ );
+ }
+
+ return trustHTML(
+ i18n("admin.upcoming_changes.default_warning_short", {
+ basePath,
+ changeNamesFilter:
+ this.setting.upcoming_change_default_override_metadata
+ .change_setting_name,
+ })
+ );
+ }
+
get dirty() {
let bufferVal = this.buffered.get("value");
let settingVal = this.setting?.value;
@@ -496,13 +539,23 @@ export default class SiteSettingComponent extends Component {
@outletArgs={{lazyHash setting=this.setting}}
/>
{{#if this.showThemeSiteSettingWarning}}
- <div class="setting-theme-warning">
+ <div class="setting-override-warning setting-theme-warning">
<p class="setting-theme-warning__text">
{{icon "paintbrush"}}
{{this.themeSiteSettingWarningText}}
</p>
</div>
{{/if}}
+ {{#if this.showUpcomingChangeDefaultWarning}}
+ <div
+ class="setting-override-warning setting-upcoming-change-warning"
+ >
+ <p class="setting-upcoming-change-warning__text">
+ {{icon "flask"}}
+ {{this.upcomingChangeDefaultWarningText}}
+ </p>
+ </div>
+ {{/if}}
{{/if}}
</div>
diff --git a/frontend/discourse/tests/integration/components/admin-config-areas/upcoming-change-item-test.gjs b/frontend/discourse/tests/integration/components/admin-config-areas/upcoming-change-item-test.gjs
new file mode 100644
index 00000000000..1e0ce3abd75
--- /dev/null
+++ b/frontend/discourse/tests/integration/components/admin-config-areas/upcoming-change-item-test.gjs
@@ -0,0 +1,131 @@
+import { render } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import UpcomingChangeItem from "discourse/admin/components/admin-config-areas/upcoming-change-item";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+
+function buildChange(overrides = {}) {
+ return {
+ setting: "test_setting",
+ humanized_name: "Test setting",
+ description: "A test setting",
+ value: true,
+ plugin: null,
+ dependents: [],
+ related: null,
+ groups: "",
+ upcoming_change: {
+ status: "beta",
+ impact: "feature,all_members",
+ impact_type: "feature",
+ impact_role: "all_members",
+ enabled_for: "everyone",
+ },
+ ...overrides,
+ };
+}
+
+module("Integration | Component | UpcomingChangeItem", function (hooks) {
+ setupRenderingTest(hooks);
+
+ test("renders dependent settings link when dependents exist and enabled", async function (assert) {
+ const change = buildChange({
+ dependents: ["other_setting"],
+ });
+
+ await render(
+ <template>
+ <table>
+ <tbody><UpcomingChangeItem @change={{change}} /></tbody>
+ </table>
+ </template>
+ );
+
+ assert
+ .dom(".upcoming-change__dependents a")
+ .exists("shows the dependent settings link");
+ });
+
+ test("does not render dependent settings link when enabled_for is no_one", async function (assert) {
+ const change = buildChange({
+ dependents: ["other_setting"],
+ upcoming_change: {
+ status: "beta",
+ impact: "feature,all_members",
+ impact_type: "feature",
+ impact_role: "all_members",
+ enabled_for: "no_one",
+ },
+ });
+
+ await render(
+ <template>
+ <table>
+ <tbody><UpcomingChangeItem @change={{change}} /></tbody>
+ </table>
+ </template>
+ );
+
+ assert
+ .dom(".upcoming-change__dependents")
+ .doesNotExist("does not show the dependent settings link");
+ });
+
+ test("renders related setting link when related exists and enabled", async function (assert) {
+ const change = buildChange({
+ related: "some_other_setting",
+ });
+
+ await render(
+ <template>
+ <table>
+ <tbody><UpcomingChangeItem @change={{change}} /></tbody>
+ </table>
+ </template>
+ );
+
+ assert
+ .dom(".upcoming-change__related a")
+ .exists("shows the related setting link");
+ });
+
+ test("does not render related setting link when related is null", async function (assert) {
+ const change = buildChange();
+
+ await render(
+ <template>
+ <table>
+ <tbody><UpcomingChangeItem @change={{change}} /></tbody>
+ </table>
+ </template>
+ );
+
+ assert
+ .dom(".upcoming-change__related")
+ .doesNotExist("does not show the related setting link");
+ });
+
+ test("does not render related setting link when enabled_for is no_one", async function (assert) {
+ const change = buildChange({
+ related: "some_other_setting",
+ upcoming_change: {
+ status: "beta",
+ impact: "feature,all_members",
+ impact_type: "feature",
+ impact_role: "all_members",
+ enabled_for: "no_one",
+ },
+ });
+
+ await render(
+ <template>
+ <table>
+ <tbody><UpcomingChangeItem @change={{change}} /></tbody>
+ </table>
+ </template>
+ );
+
+ assert
+ .dom(".upcoming-change__related")
+ .doesNotExist("does not show the related setting link when disabled");
+ });
+});
diff --git a/lib/site_setting_extension.rb b/lib/site_setting_extension.rb
index 510fb6b6885..4dd3076a864 100644
--- a/lib/site_setting_extension.rb
+++ b/lib/site_setting_extension.rb
@@ -165,6 +165,20 @@ module SiteSettingExtension
@upcoming_change_metadata ||= {}
end
+ # Has a pointer from a site setting name to the upcoming change name
+ # and overriden default value. Looks like this in site_settings.yml:
+ #
+ # setting_name:
+ # upcoming_change_default_override:
+ # upcoming_change: upcoming_change_name
+ # new_default: new_default_value
+ #
+ # The upcoming change name is used to determine if the override is active.
+ # The new default value is used to override the default value for the site setting.
+ def upcoming_change_default_overrides
+ @upcoming_change_default_overrides ||= {}
+ end
+
def hidden_settings_provider
@hidden_settings_provider ||= SiteSettings::HiddenProvider.new
end
@@ -405,7 +419,7 @@ module SiteSettingExtension
end
.select do |setting_name, _|
if only_upcoming_changes
- upcoming_change_metadata.key?(setting_name) &&
+ UpcomingChanges.exists?(setting_name) &&
UpcomingChanges.change_status(setting_name) != :conceptual
else
true
@@ -413,8 +427,16 @@ module SiteSettingExtension
end
.map do |s, v|
type_hash = type_supervisor.type_hash(s)
+
+ # NOTE: This can be overridden by an upcoming change, see .refresh!() for the
+ # logic here, and .setting() for where we load the upcoming change default overrides
+ # from yaml.
default = defaults.get(s, default_locale).to_s
+ # Has the old default, new default, and upcoming change name. Will only
+ # have these values if there is an override and the upcoming change is enabled.
+ upcoming_change_default_override_metadata = defaults.upcoming_change_override_metadata(s)
+
if themeable[s]
value = public_send(s, { theme_id: SiteSetting.default_theme_id })
else
@@ -463,8 +485,8 @@ module SiteSettingExtension
value.to_s
end
- opts.merge!(
- default: default,
+ opts_data = {
+ default:,
value: serialized_value,
preview: previews[s],
secret: secret_settings.include?(s),
@@ -475,8 +497,15 @@ module SiteSettingExtension
upcoming_change: only_upcoming_changes ? upcoming_change_metadata[s] : nil,
themeable: themeable[s],
depends_on: type_supervisor.dependencies[s],
- )
- opts.merge!(type_hash)
+ }
+
+ if upcoming_change_default_override_metadata
+ opts_data[
+ :upcoming_change_default_override_metadata
+ ] = upcoming_change_default_override_metadata
+ end
+
+ opts.merge!(opts_data.merge(type_hash))
end
opts[:plugin] = plugins[s] if plugins[s]
@@ -518,44 +547,68 @@ module SiteSettingExtension
"theme_site_settings_json_#{theme_id}__#{Discourse.git_version}"
end
- # Refresh all the site settings and theme site settings
+ # Merges the provider values of site settings (whether it be from the DB or wherever)
+ # and theme site settings with the default values of those settings, also taking into
+ # account shadowed site settings and upcoming change behaviour.
def refresh!(refresh_site_settings: true, refresh_theme_site_settings: true)
mutex.synchronize do
ensure_listen_for_changes
if refresh_site_settings
- new_hash =
- Hash[
- *(
- provider
- .all
- .map do |s|
- [s.name.to_sym, type_supervisor.to_rb_value(s.name, s.value, s.data_type)]
- end
- .to_a
- .flatten
- )
- ]
+ new_current_hash = fetch_setting_hash_from_provider
- new_modified = new_hash.dup
+ # We have to do this because we need to reject any settings that are the
+ # same as the default value before we merge the defaults view onto the current
+ # hash.
+ new_modified = new_current_hash.dup
refresh_site_setting_group_ids!
- defaults_view = defaults.all(new_hash[:default_locale])
+ defaults_view =
+ defaults.all(new_current_hash[:default_locale], include_upcoming_changes_overrides: false)
+
+ # If the "modified" value is the same as the setting default,
+ # this doesn't really count and is more of a quirk on how we
+ # store site settings. In this case, we should not consider this
+ # setting to be modified.
+ #
+ # The only exception is for upcoming changes, which have automatic
+ # promotion systems, and admins need to be able to manually opt out
+ # even if the default itself isn't changing.
+ new_modified.reject! do |name, val|
+ if UpcomingChanges.exists?(name)
+ false
+ else
+ val.to_s == defaults_view[name].to_s
+ end
+ end
- # add locale default and defaults based on default_locale, cause they are cached
- new_hash = defaults_view.merge!(new_hash)
+ # Merge in the default values for the current locale, since these are
+ # precomputed and cached. This ensures that locale-specific and default
+ # settings are always available in current_hash.
+ new_current_hash = defaults_view.merge!(new_current_hash)
- # add shadowed
- shadowed_settings.each { |ss| new_hash[ss] = GlobalSetting.public_send(ss) }
+ shadowed_settings.each { |ss| new_current_hash[ss] = GlobalSetting.public_send(ss) }
- changes, deletions = diff_hash(new_hash, current)
+ changes, deletions = diff_hash(new_current_hash, current)
changes.each { |name, val| current[name] = val }
deletions.each { |name, _| current[name] = defaults_view[name] }
+
modified.clear
modified.merge!(new_modified)
uploads.clear
+
+ upcoming_change_default_overrides.each do |setting_name, override|
+ if UpcomingChanges.enabled?(override[:upcoming_change])
+ defaults.activate_upcoming_change_override(override[:upcoming_change])
+ if !setting_modified_from_default?(setting_name)
+ current[setting_name] = override[:new_default]
+ end
+ else
+ defaults.deactivate_upcoming_change_override(override[:upcoming_change])
+ end
+ end
end
refresh_theme_site_settings! if refresh_theme_site_settings
@@ -567,6 +620,14 @@ module SiteSettingExtension
end
end
+ # Whether an admin or something else has changed the site setting from its
+ # default value, writing a new value to the database.
+ #
+ # Note that if the DB value is the same as the default value, this will return false.
+ def setting_modified_from_default?(setting_name)
+ modified.key?(setting_name)
+ end
+
def refresh_site_setting_group_ids!
new_site_setting_group_ids_hash = SiteSettingGroup.generate_setting_group_map
site_setting_group_id_changes, site_setting_group_id_deletions =
@@ -651,6 +712,13 @@ module SiteSettingExtension
current[name] = defaults.get(name, default_locale)
modified.delete(name)
+ if UpcomingChanges.exists?(name)
+ apply_upcoming_change_default_overrides_for!(
+ name,
+ upcoming_change_enabled: UpcomingChanges.enabled?(name),
+ )
+ end
+
return if current[name] == old_val
clear_uploads_cache(name)
@@ -711,6 +779,10 @@ module SiteSettingExtension
current[name] = type_supervisor.to_rb_value(name, sanitized_val)
modified[name] = current[name]
+ if UpcomingChanges.exists?(name)
+ apply_upcoming_change_default_overrides_for!(name, upcoming_change_enabled: current[name])
+ end
+
return if current[name] == old_val
clear_uploads_cache(name)
@@ -1029,9 +1101,12 @@ module SiteSettingExtension
refresh! if current[name].nil?
value =
- if upcoming_change_metadata[name]
- UpcomingChanges.resolved_value(name)
+ if UpcomingChanges.exists?(name)
+ UpcomingChanges.enabled?(name)
else
+ # Will either be the value the admin changed the site setting to
+ # in the DB/provider, or the default value, which may also be affected
+ # by an upcoming change.
current[name]
end
@@ -1056,7 +1131,7 @@ module SiteSettingExtension
# certain groups to the change early. We use the data from SiteSettingGroup to define
# a getter with _groups_map on the end, e.g. allow_unlimited_uploads_groups_map,
# to avoid having to manually split and convert to integer for these settings.
- if upcoming_change_metadata[name] && type_supervisor.get_type(name) == :bool
+ if UpcomingChanges.exists?(name) && type_supervisor.get_type(name) == :bool
define_singleton_method("#{clean_name}_groups_map") do
site_setting_group_ids[name].presence || []
end
@@ -1105,6 +1180,57 @@ module SiteSettingExtension
private
+ # Gets all of the site setting values from the provider (DbProvider (most
+ # common), TestProvider, LocalProcessProvider etc.) and returns a hash of
+ # setting names to values based on the type of the setting.
+ #
+ # @return [Hash] a hash of setting names to values based on the type of the setting
+ #
+ # @example
+ # {
+ # enable_mobile_theme: true,
+ # topics_per_period_in_top_page: 50,
+ # title: "My awesome forum"
+ # }
+ def fetch_setting_hash_from_provider
+ Hash[
+ *(
+ provider
+ .all
+ .map do |setting|
+ [
+ setting.name.to_sym,
+ type_supervisor.to_rb_value(setting.name, setting.value, setting.data_type),
+ ]
+ end
+ .flatten
+ )
+ ]
+ end
+
+ def apply_upcoming_change_default_overrides_for!(name, upcoming_change_enabled:)
+ if upcoming_change_enabled
+ defaults.activate_upcoming_change_override(name)
+ else
+ defaults.deactivate_upcoming_change_override(name)
+ end
+
+ # Similar to what we do in refresh!, but we are being more reactive here.
+ # If the admin enables/disables an upcoming change we need to make sure
+ # the overridden default is used, otherwise we fall back to the old default,
+ # but only if the admin hasn't modified the target setting themselves.
+ upcoming_change_default_overrides.each do |setting_name, override|
+ next if override[:upcoming_change] != name.to_sym
+ next if setting_modified_from_default?(setting_name)
+
+ if upcoming_change_enabled
+ current[setting_name] = override[:new_default]
+ else
+ current[setting_name] = defaults.get(setting_name, default_locale)
+ end
+ end
+ end
+
def setting(name_arg, default = nil, opts = {})
name = name_arg.to_sym
@@ -1131,11 +1257,22 @@ module SiteSettingExtension
)
if opts[:upcoming_change]
- upcoming_change_metadata[name] = opts[:upcoming_change]
- impact_type, impact_role = upcoming_change_metadata[name][:impact].split(",")
- upcoming_change_metadata[name][:impact_type] = impact_type
- upcoming_change_metadata[name][:impact_role] = impact_role
- upcoming_change_metadata[name][:status] = opts[:upcoming_change][:status].to_sym
+ upcoming_change_metadata[name] ||= {}
+ impact_type, impact_role = opts[:upcoming_change][:impact].split(",")
+ upcoming_change_metadata[name].merge!(
+ **opts[:upcoming_change].except(:impact),
+ impact_type: impact_type,
+ impact_role: impact_role,
+ status: opts[:upcoming_change][:status].to_sym,
+ )
+ end
+
+ upcoming_change_default_override = opts[:upcoming_change_default_override]
+ if upcoming_change_default_override.present?
+ upcoming_change_default_overrides[name] = {
+ upcoming_change: upcoming_change_default_override[:upcoming_change].to_sym,
+ new_default: upcoming_change_default_override[:new_default],
+ }
end
categories[name] = opts[:category] || :uncategorized
diff --git a/lib/site_settings/defaults_provider.rb b/lib/site_settings/defaults_provider.rb
index 5e5612f2472..2e1ec862df1 100644
--- a/lib/site_settings/defaults_provider.rb
+++ b/lib/site_settings/defaults_provider.rb
@@ -11,6 +11,7 @@ class SiteSettings::DefaultsProvider
@site_setting = site_setting
@defaults = {}
@defaults[DEFAULT_LOCALE.to_sym] = {}
+ @active_upcoming_change_overrides = Set.new
end
def load_setting(name_arg, value, locale_defaults)
@@ -26,21 +27,62 @@ class SiteSettings::DefaultsProvider
end
end
+ def activate_upcoming_change_override(upcoming_change_setting)
+ @active_upcoming_change_overrides.add(upcoming_change_setting.to_sym)
+ end
+
+ def deactivate_upcoming_change_override(upcoming_change_setting)
+ @active_upcoming_change_overrides.delete(upcoming_change_setting.to_sym)
+ end
+
def db_all
@site_setting.provider.all
end
- def all(locale = nil)
- result =
- if locale
- @defaults[DEFAULT_LOCALE.to_sym].merge(@defaults[locale.to_sym] || {})
- else
- @defaults[DEFAULT_LOCALE.to_sym].dup
+ # Defaults loaded from yaml files before mutation by upcoming
+ # changes and modifiers.
+ def all_clean(locale = nil)
+ if locale
+ @defaults[DEFAULT_LOCALE.to_sym].merge(@defaults[locale.to_sym] || {})
+ else
+ @defaults[DEFAULT_LOCALE.to_sym].dup
+ end
+ end
+
+ def all(locale = nil, include_upcoming_changes_overrides: true)
+ result = all_clean(locale)
+
+ if include_upcoming_changes_overrides
+ # Only support upcoming change default overrides on default locale for now,
+ # we can come back to this later if we need the extra complexity.
+ @site_setting.upcoming_change_default_overrides.each do |setting_name, override|
+ result[setting_name] = override[:new_default] if @active_upcoming_change_overrides.include?(
+ override[:upcoming_change],
+ )
end
+ end
DiscoursePluginRegistry.apply_modifier(:site_setting_defaults, result)
end
+ def upcoming_change_override_metadata(setting_name)
+ upcoming_change_default_override =
+ @site_setting.upcoming_change_default_overrides[setting_name.to_sym]
+
+ if upcoming_change_default_override.blank? ||
+ !@active_upcoming_change_overrides.include?(
+ upcoming_change_default_override[:upcoming_change],
+ )
+ return
+ end
+
+ {
+ old_default: all_clean[setting_name].to_s,
+ new_default: upcoming_change_default_override[:new_default].to_s,
+ change_setting_name: upcoming_change_default_override[:upcoming_change].to_sym,
+ }
+ end
+
def get(name, locale = DEFAULT_LOCALE)
all(locale)[name.to_sym]
end
diff --git a/lib/site_settings/type_supervisor.rb b/lib/site_settings/type_supervisor.rb
index f36c66386ab..9141c5cfb0e 100644
--- a/lib/site_settings/type_supervisor.rb
+++ b/lib/site_settings/type_supervisor.rb
@@ -86,7 +86,7 @@ class SiteSettings::TypeSupervisor
when TrueClass, FalseClass
self.types[:bool]
else
- raise ArgumentError.new :val
+ raise ArgumentError.new("Invalid value type for site setting: #{val.class}")
end
end
@@ -164,6 +164,18 @@ class SiteSettings::TypeSupervisor
@dependencies.change_behavior(name, opts[:depends_behavior]) if opts[:depends_behavior]
end
+ # Converts a site setting value to a Ruby value based on the type of the setting,
+ # which is necessary because the value is stored in the database as a string.
+ #
+ # @param name [Symbol] the name of the setting
+ # @param value [String] the value of the setting
+ # @param override_type [Symbol] the type of the setting to override the type of the setting
+ # @return [Object] the Ruby value of the setting
+ #
+ # @example
+ # to_rb_value(:enable_mobile_theme, "true") # => true
+ # to_rb_value(:topics_per_period_in_top_page, "50") # => 50
+ # to_rb_value(:title, "My awesome forum") # => "My awesome forum"
def to_rb_value(name, value, override_type = nil)
name = name.to_sym
@types[name] = (@types[name] || get_data_type(name, value))
diff --git a/lib/upcoming_changes.rb b/lib/upcoming_changes.rb
index f684280b31d..b51482a4de7 100644
--- a/lib/upcoming_changes.rb
+++ b/lib/upcoming_changes.rb
@@ -24,6 +24,13 @@ module UpcomingChanges
)
end
+ # Mostly used for testing, to allow stubbing the SiteSetting provider,
+ # like for SiteSettingExtension spec. This is not ideal, but the SiteSettingExtension spec
+ # is extremely gnarly.
+ def self.settings_provider
+ SiteSetting
+ end
+
def self.previous_status_value(status)
status_value = self.statuses[status.to_sym]
self.statuses.values.select { |value| value < status_value }.max || -100
@@ -39,7 +46,7 @@ module UpcomingChanges
end
def self.image_path(change_setting_name)
- plugin_name = SiteSetting.plugins[change_setting_name.to_sym]
+ plugin_name = settings_provider.plugins[change_setting_name.to_sym]
if plugin_name.present?
File.join("plugins", plugin_name, "images", "upcoming_changes", "#{change_setting_name}.png")
else
@@ -66,7 +73,7 @@ module UpcomingChanges
def self.change_metadata(change_setting_name)
change_setting_name = change_setting_name.to_sym
- SiteSetting.upcoming_change_metadata[change_setting_name] || {}
+ settings_provider.upcoming_change_metadata[change_setting_name] || {}
end
def self.not_yet_stable?(change_setting_name)
@@ -97,34 +104,40 @@ module UpcomingChanges
).order(created_at: :desc)
end
+ def self.exists?(change_setting_name)
+ change_metadata(change_setting_name.to_sym).present?
+ end
+
# We dynamically determine if an upcoming change is enabled
# or disabled based on the current status of the change as well
# as whether the admin has manually toggled the change.
#
# @param change_setting_name [Symbol] The name of the upcoming change
# @return [Boolean]
- def self.resolved_value(change_setting_name)
+ def self.enabled?(change_setting_name)
+ change_setting_name = change_setting_name.to_sym
+
# An admin has modified the setting and a value is stored
# in the database, since the default for upcoming changes
# is false.
#
# If the change is permanent though, the admin has no choice
# in the matter.
- if SiteSetting.modified.key?(change_setting_name) &&
+ if settings_provider.setting_modified_from_default?(change_setting_name) &&
UpcomingChanges.change_status(change_setting_name) != :permanent
- SiteSetting.current[change_setting_name]
+ settings_provider.current[change_setting_name]
# The change has reached the promotion status and is forcibly
# enabled, admins can still disable it.
elsif UpcomingChanges.meets_or_exceeds_status?(
change_setting_name,
- SiteSetting.promote_upcoming_changes_on_status.to_sym,
+ settings_provider.promote_upcoming_changes_on_status.to_sym,
) || UpcomingChanges.change_status(change_setting_name) == :permanent
true
else
# Otherwise use the default value, which for upcoming changes
# is false.
- SiteSetting.defaults[change_setting_name]
+ settings_provider.defaults[change_setting_name]
end
end
@@ -134,7 +147,7 @@ module UpcomingChanges
def self.group_ids_for(change_setting_name)
change_setting_name = change_setting_name.to_sym
- SiteSetting.site_setting_group_ids[change_setting_name].presence || []
+ settings_provider.site_setting_group_ids[change_setting_name].presence || []
end
# Checks if a given upcoming change is enabled for a user,
@@ -148,7 +161,7 @@ module UpcomingChanges
# @return [Boolean]
def self.enabled_for_user?(change_setting_name, user)
change_setting_name = change_setting_name.to_sym
- setting_enabled = SiteSetting.public_send(change_setting_name)
+ setting_enabled = UpcomingChanges.enabled?(change_setting_name)
# Anon users can only have upcoming changes enabled if it's set for Everyone
if user.blank?
@@ -200,7 +213,7 @@ module UpcomingChanges
guardian_visible_group_ids = Group.visible_groups(acting_guardian.user).pluck(:id)
user_belonging_to_group_ids = user.belonging_to_group_ids
- SiteSetting.upcoming_change_site_settings.filter_map do |name|
+ settings_provider.upcoming_change_site_settings.filter_map do |name|
next if UpcomingChanges.change_status(name) == :conceptual
enabled = user.upcoming_change_enabled?(name)
has_groups = UpcomingChanges.has_groups?(name)
@@ -226,8 +239,8 @@ module UpcomingChanges
{
name:,
- humanized_name: SiteSetting.humanized_name(name),
- description: SiteSetting.description(name),
+ humanized_name: settings_provider.humanized_name(name),
+ description: settings_provider.description(name),
enabled:,
specific_groups:,
reason:,
@@ -249,7 +262,7 @@ module UpcomingChanges
# @example
# enabled_for_with_groups(:new_feature, true, { 1 => "Group 1", 2 => "Group 2" })
def self.enabled_for_with_groups(setting_name, setting_value, upcoming_change_selected_groups)
- group_ids_for_setting = SiteSetting.site_setting_group_ids[setting_name]
+ group_ids_for_setting = settings_provider.site_setting_group_ids[setting_name]
setting_groups =
upcoming_change_selected_groups.values_at(*group_ids_for_setting).join(
",",
@@ -325,12 +338,20 @@ module UpcomingChanges
Discourse
.cache
.fetch(permanent_upcoming_changes_cache_key) do
- UpcomingChanges::List.call(
- guardian: Discourse.system_user.guardian,
- options: {
- filter_statuses: [:permanent],
- },
- )&.upcoming_changes
+ result =
+ UpcomingChanges::List.call(
+ guardian: Discourse.system_user.guardian,
+ options: {
+ filter_statuses: [:permanent],
+ },
+ )
+
+ if !result.success? && Rails.env.local?
+ puts result.inspect_steps
+ raise
+ end
+
+ result.upcoming_changes
end
end
@@ -342,4 +363,24 @@ module UpcomingChanges
def self.should_notify_admins?
Migration::Helpers.existing_site? || Rails.env.development?
end
+
+ # Some upcoming changes have a depends_on relationship with other settings,
+ # where it doesn't make sense to show the dependent settings in the site
+ # settings UI unless the upcoming change is enabled.
+ #
+ # This is done via depends_on and depends_behavior: hidden in site_settings.yml.
+ def self.find_dependents_for_change(change_setting_name)
+ settings_provider.type_supervisor.dependencies.dependents(change_setting_name.to_s)
+ end
+
+ # Some upcoming changes when enabled will override the default value
+ # of another setting.
+ #
+ # This is done via upcoming_change_default_override in site_settings.yml.
+ def self.find_related_default_override_for_change(change_setting_name)
+ settings_provider
+ .upcoming_change_default_overrides
+ .find { |_, override| override[:upcoming_change] == change_setting_name.to_sym }
+ &.first
+ end
end
diff --git a/spec/integrity/upcoming_change_metadata_spec.rb b/spec/integrity/upcoming_change_metadata_spec.rb
index 6eeed5375fb..adc891062b1 100644
--- a/spec/integrity/upcoming_change_metadata_spec.rb
+++ b/spec/integrity/upcoming_change_metadata_spec.rb
@@ -32,7 +32,7 @@ def upcoming_change_setting_label(setting)
end
def valid_upcoming_change_impact_types
- %w[feature other]
+ %w[feature other site_setting_default]
end
def valid_upcoming_change_impact_roles
diff --git a/spec/lib/site_setting_extension_spec.rb b/spec/lib/site_setting_extension_spec.rb
index 5afeb29fb57..7f873d2a5f7 100644
--- a/spec/lib/site_setting_extension_spec.rb
+++ b/spec/lib/site_setting_extension_spec.rb
@@ -155,6 +155,33 @@ RSpec.describe SiteSettingExtension do
%Q|{"default_locale":"#{SiteSetting.default_locale}","upload_type":"a_new_url","string_type":"changed"}|,
)
end
+
+ context "when the provider value equals the YAML default" do
+ it "does not mark a normal setting as modified from default" do
+ settings.setting(:hello, 1)
+ settings.provider.save(:hello, 1, SiteSetting.types[:integer])
+ settings.refresh!
+
+ expect(settings.setting_modified_from_default?(:hello)).to eq(false)
+ end
+
+ it "still marks an upcoming change setting as modified from default so admins can opt out" do
+ settings.setting(
+ :upcoming_change_opt_out_flag,
+ false,
+ upcoming_change: {
+ status: :experimental,
+ impact: "feature,staff",
+ },
+ )
+ UpcomingChanges.stubs(:settings_provider).returns(settings)
+
+ settings.provider.save(:upcoming_change_opt_out_flag, false, SiteSetting.types[:bool])
+ settings.refresh!
+
+ expect(settings.setting_modified_from_default?(:upcoming_change_opt_out_flag)).to eq(true)
+ end
+ end
end
describe "DiscourseEvent" do
@@ -697,9 +724,7 @@ RSpec.describe SiteSettingExtension do
},
)
settings.refresh!
- allow(UpcomingChanges).to receive(:resolved_value).with(:enable_cool_thing).and_return(
- true,
- )
+ allow(UpcomingChanges).to receive(:enabled?).with(:enable_cool_thing).and_return(true)
end
it "is present in all_settings" do
@@ -720,9 +745,7 @@ RSpec.describe SiteSettingExtension do
},
)
settings.refresh!
- allow(UpcomingChanges).to receive(:resolved_value).with(:enable_cool_thing).and_return(
- true,
- )
+ allow(UpcomingChanges).to receive(:enabled?).with(:enable_cool_thing).and_return(true)
end
it "is present in all_settings" do
@@ -805,6 +828,8 @@ RSpec.describe SiteSettingExtension do
settings.refresh!
end
+ after { DiscoursePluginRegistry.reset! }
+
it "is in the `hidden_settings` collection" do
expect(settings.hidden_settings.include?(:superman_identity)).to eq(true)
end
@@ -1895,4 +1920,128 @@ RSpec.describe SiteSettingExtension do
end
end
end
+
+ describe "upcoming_change_default_override" do
+ before do
+ settings.setting(
+ :increase_suggested_topics_max_days_old_default,
+ false,
+ upcoming_change: {
+ status: :experimental,
+ impact: "site_setting_default,all_members",
+ },
+ )
+ settings.setting(
+ :suggested_topics_max_days_old,
+ 365,
+ upcoming_change_default_override: {
+ upcoming_change: "increase_suggested_topics_max_days_old_default",
+ new_default: 1000,
+ },
+ )
+ settings.setting(:promote_upcoming_changes_on_status, "stable")
+ UpcomingChanges.stubs(:settings_provider).returns(settings)
+ end
+
+ context "when the linked upcoming change is active" do
+ before do
+ settings.provider.save(
+ :increase_suggested_topics_max_days_old_default,
+ true,
+ SiteSetting.types[:bool],
+ )
+ settings.refresh!
+ end
+
+ it "returns the override default" do
+ expect(settings.suggested_topics_max_days_old).to eq(1000)
+ end
+ end
+
+ context "when the linked upcoming change is not active" do
+ it "returns the YAML default" do
+ expect(settings.suggested_topics_max_days_old).to eq(365)
+ end
+ end
+
+ context "when the linked upcoming change is enabled via add_override!" do
+ it "immediately applies the override default without needing refresh!" do
+ settings.add_override!(:increase_suggested_topics_max_days_old_default, true)
+ expect(settings.suggested_topics_max_days_old).to eq(1000)
+ end
+
+ it "immediately reverts the override default when disabled" do
+ settings.add_override!(:increase_suggested_topics_max_days_old_default, true)
+ expect(settings.suggested_topics_max_days_old).to eq(1000)
+ settings.add_override!(:increase_suggested_topics_max_days_old_default, false)
+ expect(settings.suggested_topics_max_days_old).to eq(365)
+ end
+
+ it "does not override a manually set target setting value" do
+ settings.add_override!(:suggested_topics_max_days_old, 730)
+ settings.add_override!(:increase_suggested_topics_max_days_old_default, true)
+ expect(settings.suggested_topics_max_days_old).to eq(730)
+ end
+ end
+
+ context "when the linked upcoming change is reset via remove_override!" do
+ it "immediately reverts the override default" do
+ settings.add_override!(:increase_suggested_topics_max_days_old_default, true)
+ expect(settings.suggested_topics_max_days_old).to eq(1000)
+
+ settings.remove_override!(:increase_suggested_topics_max_days_old_default)
+
+ expect(settings.increase_suggested_topics_max_days_old_default).to eq(false)
+ expect(settings.suggested_topics_max_days_old).to eq(365)
+ expect(
+ settings.defaults.upcoming_change_override_metadata(:suggested_topics_max_days_old),
+ ).to be_nil
+ end
+
+ it "reapplies the override default when a manual opt out is removed after auto-promotion" do
+ settings.add_override!(:promote_upcoming_changes_on_status, "experimental")
+ settings.refresh!
+ expect(settings.suggested_topics_max_days_old).to eq(1000)
+
+ settings.add_override!(:increase_suggested_topics_max_days_old_default, false)
+ expect(settings.suggested_topics_max_days_old).to eq(365)
+
+ settings.remove_override!(:increase_suggested_topics_max_days_old_default)
+
+ expect(settings.increase_suggested_topics_max_days_old_default).to eq(true)
+ expect(settings.suggested_topics_max_days_old).to eq(1000)
+ end
+ end
+
+ describe "all_settings effective default" do
+ context "when the linked upcoming change is active" do
+ before do
+ UpcomingChanges.stubs(:settings_provider).returns(settings)
+ settings.provider.save(
+ :increase_suggested_topics_max_days_old_default,
+ true,
+ SiteSetting.types[:bool],
+ )
+ settings.refresh!
+ end
+
+ it "shows the override default in all_settings" do
+ setting = settings.all_settings.find { |s| s[:setting] == :suggested_topics_max_days_old }
+ expect(setting[:default]).to eq("1000")
+ end
+
+ it "does not show the setting as overridden when value matches effective default" do
+ setting = settings.all_settings.find { |s| s[:setting] == :suggested_topics_max_days_old }
+ expect(setting[:value]).to eq(setting[:default])
+ end
+ end
+
+ context "when the linked upcoming change is not active" do
+ it "shows the original YAML default in all_settings" do
+ setting = settings.all_settings.find { |s| s[:setting] == :suggested_topics_max_days_old }
+ expect(setting[:default]).to eq("365")
+ end
+ end
+ end
+ end
end
diff --git a/spec/lib/site_settings/defaults_provider_spec.rb b/spec/lib/site_settings/defaults_provider_spec.rb
index 22c9a22dea6..51386b323e3 100644
--- a/spec/lib/site_settings/defaults_provider_spec.rb
+++ b/spec/lib/site_settings/defaults_provider_spec.rb
@@ -151,4 +151,87 @@ RSpec.describe SiteSettings::DefaultsProvider do
DiscoursePluginRegistry.reset!
end
end
+
+ describe "upcoming change default overrides" do
+ before do
+ settings.setting(:suggested_topics_max_days_old, 365)
+ settings.instance_variable_set(
+ :@upcoming_change_default_overrides,
+ {
+ suggested_topics_max_days_old: {
+ new_default: 1000,
+ upcoming_change: :increase_suggested_topics_max_days_old_default,
+ },
+ },
+ )
+ end
+
+ describe ".all" do
+ context "when the upcoming change override is active" do
+ before do
+ settings.defaults.activate_upcoming_change_override(
+ :increase_suggested_topics_max_days_old_default,
+ )
+ end
+
+ it "uses upcoming change default overrides by default" do
+ expect(settings.defaults.all[:suggested_topics_max_days_old]).to eq(1000)
+ end
+
+ context "when include_upcoming_changes_overrides is false" do
+ it "does not use upcoming change default overrides" do
+ expect(
+ settings.defaults.all(include_upcoming_changes_overrides: false)[
+ :suggested_topics_max_days_old
+ ],
+ ).to eq(365)
+ end
+ end
+ end
+
+ context "when the upcoming change override is not active" do
+ before do
+ settings.defaults.deactivate_upcoming_change_override(
+ :increase_suggested_topics_max_days_old_default,
+ )
+ end
+
+ it "does not use upcoming change default overrides" do
+ expect(settings.defaults.all[:suggested_topics_max_days_old]).to eq(365)
+ end
+ end
+ end
+
+ describe ".upcoming_change_override_metadata" do
+ before do
+ settings.defaults.activate_upcoming_change_override(
+ :increase_suggested_topics_max_days_old_default,
+ )
+ end
+
+ it "returns the upcoming change override metadata" do
+ expect(
+ settings.defaults.upcoming_change_override_metadata(:suggested_topics_max_days_old),
+ ).to eq(
+ old_default: "365",
+ new_default: "1000",
+ change_setting_name: :increase_suggested_topics_max_days_old_default,
+ )
+ end
+
+ context "when the upcoming change override is not active" do
+ before do
+ settings.defaults.deactivate_upcoming_change_override(
+ :increase_suggested_topics_max_days_old_default,
+ )
+ end
+
+ it "returns nil" do
+ expect(
+ settings.defaults.upcoming_change_override_metadata(:suggested_topics_max_days_old),
+ ).to be_nil
+ end
+ end
+ end
+ end
end
diff --git a/spec/lib/upcoming_changes_spec.rb b/spec/lib/upcoming_changes_spec.rb
index dceefe61e3a..311963e1e53 100644
--- a/spec/lib/upcoming_changes_spec.rb
+++ b/spec/lib/upcoming_changes_spec.rb
@@ -385,6 +385,102 @@ RSpec.describe UpcomingChanges do
end
end
+ describe ".enabled?" do
+ after do
+ SiteSetting.remove_override!(setting_name)
+ SiteSetting.promote_upcoming_changes_on_status = :stable
+ UpcomingChanges.clear_caches!
+ end
+
+ context "when the setting has no row in the database (admin has not saved it)" do
+ before { SiteSetting.remove_override!(setting_name) }
+
+ after { clear_mocked_upcoming_change_metadata }
+
+ it "returns the yaml default when the change is below promote_upcoming_changes_on_status" do
+ mock_upcoming_change_metadata(
+ {
+ enable_upload_debug_mode: {
+ impact: "other,developers",
+ status: :experimental,
+ impact_type: "other",
+ impact_role: "developers",
+ },
+ },
+ )
+ SiteSetting.promote_upcoming_changes_on_status = :stable
+
+ expect(described_class.enabled?(setting_name)).to eq(SiteSetting.defaults[setting_name])
+ end
+
+ it "returns true when the change meets or exceeds promote_upcoming_changes_on_status" do
+ mock_upcoming_change_metadata(
+ {
+ enable_upload_debug_mode: {
+ impact: "other,developers",
+ status: :stable,
+ impact_type: "other",
+ impact_role: "developers",
+ },
+ },
+ )
+ SiteSetting.promote_upcoming_changes_on_status = :stable
+
+ expect(described_class.enabled?(setting_name)).to eq(true)
+ end
+ end
+
+ context "when an admin has saved a value to the database" do
+ after { clear_mocked_upcoming_change_metadata }
+
+ it "returns the stored value when true" do
+ SiteSetting.enable_upload_debug_mode = true
+
+ expect(described_class.enabled?(setting_name)).to eq(true)
+ end
+
+ it "returns the stored value when false even when the change meets promote_upcoming_changes_on_status" do
+ mock_upcoming_change_metadata(
+ {
+ enable_upload_debug_mode: {
+ impact: "other,developers",
+ status: :beta,
+ impact_type: "other",
+ impact_role: "developers",
+ },
+ },
+ )
+ SiteSetting.promote_upcoming_changes_on_status = :beta
+ SiteSetting.enable_upload_debug_mode = false
+
+ expect(described_class.enabled?(setting_name)).to eq(false)
+ end
+ end
+
+ context "when the change is permanent" do
+ before do
+ mock_upcoming_change_metadata(
+ {
+ enable_upload_debug_mode: {
+ impact: "other,developers",
+ status: :permanent,
+ impact_type: "other",
+ impact_role: "developers",
+ },
+ },
+ )
+ end
+
+ after { clear_mocked_upcoming_change_metadata }
+
+ it "returns true even when the database value is false" do
+ SiteSetting.enable_upload_debug_mode = false
+
+ expect(described_class.enabled?(setting_name)).to eq(true)
+ end
+ end
+ end
+
describe ".current_statuses" do
include ActiveSupport::Testing::TimeHelpers
diff --git a/spec/services/upcoming_changes/list_spec.rb b/spec/services/upcoming_changes/list_spec.rb
index 59ddd2f461b..92a3dace22d 100644
--- a/spec/services/upcoming_changes/list_spec.rb
+++ b/spec/services/upcoming_changes/list_spec.rb
@@ -89,6 +89,35 @@ RSpec.describe UpcomingChanges::List do
expect(mock_setting[:groups]).to eq("trust_level_0,trust_level_1")
end
+ describe "related setting" do
+ context "when an upcoming_change_default_override points to the change" do
+ before do
+ mock_upcoming_change_default_overrides(
+ {
+ suggested_topics_max_days_old: {
+ upcoming_change: :enable_upload_debug_mode,
+ new_default: 1000,
+ },
+ },
+ )
+ end
+
+ after { clear_mocked_upcoming_change_default_overrides }
+
+ it "includes the related setting name" do
+ results = result.upcoming_changes
+ mock_setting = results.find { |change| change[:setting] == :enable_upload_debug_mode }
+ expect(mock_setting[:related]).to eq(:suggested_topics_max_days_old)
+ end
+ end
+
+ it "returns nil for related when no upcoming_change_default_override points to the change" do
+ results = result.upcoming_changes
+ mock_setting = results.find { |change| change[:setting] == :allow_user_locale }
+ expect(mock_setting[:related]).to be_nil
+ end
+ end
+
describe "enabled_for logic" do
it "sets enabled_for to 'no_one' when setting value is false" do
SiteSetting.enable_upload_debug_mode = false
diff --git a/spec/services/upcoming_changes/toggle_spec.rb b/spec/services/upcoming_changes/toggle_spec.rb
index 72fcb9ca370..9c058c1ec97 100644
--- a/spec/services/upcoming_changes/toggle_spec.rb
+++ b/spec/services/upcoming_changes/toggle_spec.rb
@@ -131,7 +131,7 @@ RSpec.describe UpcomingChanges::Toggle do
.track_events { result }
.select { |e| e[:event_name] == :upcoming_change_enabled }
- expect(events.first[:params]).to eq([setting_name.to_s])
+ expect(events.first[:params]).to eq([setting_name])
end
end
@@ -172,7 +172,7 @@ RSpec.describe UpcomingChanges::Toggle do
.track_events { result }
.select { |e| e[:event_name] == :upcoming_change_disabled }
- expect(events.first[:params]).to eq([setting_name.to_s])
+ expect(events.first[:params]).to eq([setting_name])
end
end
end
@@ -208,7 +208,7 @@ RSpec.describe UpcomingChanges::Toggle do
.track_events { result }
.select { |e| e[:event_name] == :upcoming_change_enabled }
- expect(events.first[:params]).to eq([setting_name.to_s])
+ expect(events.first[:params]).to eq([setting_name])
end
end
@@ -240,7 +240,7 @@ RSpec.describe UpcomingChanges::Toggle do
.track_events { result }
.select { |e| e[:event_name] == :upcoming_change_disabled }
- expect(events.first[:params]).to eq([setting_name.to_s])
+ expect(events.first[:params]).to eq([setting_name])
end
end
end
diff --git a/spec/support/helpers.rb b/spec/support/helpers.rb
index acd5675d44c..a4249585aeb 100644
--- a/spec/support/helpers.rb
+++ b/spec/support/helpers.rb
@@ -364,6 +364,30 @@ module Helpers
)
end
+ def mock_upcoming_change_default_overrides(overrides)
+ # Without ||= here nested blocks would further mutate the instance var so
+ # resetting in clear_mocked_upcoming_change_metadata would not work.
+ @original_upcoming_change_default_overrides ||=
+ SiteSetting.upcoming_change_default_overrides.dup
+
+ # We do this because upcoming changes are ephemeral in site settings,
+ # so we cannot rely on them for specs. Instead we can fake some metadata
+ # for an existing stable setting.
+ SiteSetting.instance_variable_set(
+ :@upcoming_change_default_overrides,
+ @original_upcoming_change_default_overrides.merge(overrides),
+ )
+ end
+
+ def clear_mocked_upcoming_change_default_overrides
+ return if @original_upcoming_change_default_overrides.nil?
+
+ SiteSetting.instance_variable_set(
+ :@upcoming_change_default_overrides,
+ @original_upcoming_change_default_overrides,
+ )
+ end
+
private
def directory_from_caller
diff --git a/spec/system/admin_upcoming_changes_spec.rb b/spec/system/admin_upcoming_changes_spec.rb
index ee706eb3d8a..75010483240 100644
--- a/spec/system/admin_upcoming_changes_spec.rb
+++ b/spec/system/admin_upcoming_changes_spec.rb
@@ -305,4 +305,45 @@ describe "Admin upcoming changes" do
)
end
end
+
+ context "when the upcoming change has a default override" do
+ let(:settings_page) { PageObjects::Pages::AdminSiteSettings.new }
+
+ before do
+ mock_upcoming_change_metadata(
+ {
+ enable_upload_debug_mode: {
+ impact: "other,developers",
+ status: :experimental,
+ impact_type: "other",
+ impact_role: "developers",
+ },
+ },
+ )
+ mock_upcoming_change_default_overrides(
+ {
+ suggested_topics_max_days_old: {
+ upcoming_change: :enable_upload_debug_mode,
+ new_default: 1000,
+ },
+ },
+ )
+ SiteSetting.enable_upload_debug_mode = true
+ SiteSetting.refresh!
+ end
+
+ after do
+ clear_mocked_upcoming_change_metadata
+ clear_mocked_upcoming_change_default_overrides
+ end
+
+ it "shows information about the default override in the site settings UI" do
+ settings_page.visit("suggested_topics_max_days_old")
+ expect(settings_page).to have_upcoming_change_default_warning(
+ :suggested_topics_max_days_old,
+ old_default: 365,
+ new_default: 1000,
+ )
+ end
+ end
end
diff --git a/spec/system/page_objects/pages/admin_site_settings.rb b/spec/system/page_objects/pages/admin_site_settings.rb
index 959cba65de6..0165dbb396b 100644
--- a/spec/system/page_objects/pages/admin_site_settings.rb
+++ b/spec/system/page_objects/pages/admin_site_settings.rb
@@ -155,6 +155,15 @@ module PageObjects
)
end
+ def has_upcoming_change_default_warning?(setting_name, old_default:, new_default:)
+ find_setting(setting_name).find(".setting-upcoming-change-warning__text").has_text?(
+ "The default for this setting has changed from #{old_default} to #{new_default}",
+ ) &&
+ find_setting(setting_name).find(".setting-upcoming-change-warning__text").has_link?(
+ href: "/admin/config/upcoming-changes?changeNamesFilter=enable_upload_debug_mode",
+ )
+ end
+
def has_disabled_input?(setting_name)
find_setting(setting_name).has_css?("input[disabled]")
end