discourse/spec/services/themes/theme_site_setting_manager_spec.rb
Martin Brennan 19af83d39e
FEATURE: Themeable site settings (#32233)
This commit introduces the concept of themeable site settings,
which is a new tool for theme authors that lives alongside theme
modifiers and theme settings. Here is a quick summary:

* Theme settings - These are custom settings used to control UI and functionality within your theme or component and provide configuration options. These cannot change core Discourse functionality.
* Theme modifiers - Allows a theme or a component to modify selected server-side functionality of core Discourse as an alternative to building a plugin.
* Themeable site settings (new) - Allows a theme (not components) to override a small subset of core site settings, which generally control parts of the UI and other minor functionality. This allows themes to have a greater control over the full site experience.

Themeable site settings will be shown for all themes, whether the theme
changes
the value or not, and have a similar UI to custom theme settings.

We are also introducing a new page at
`/admin/config/theme-site-settings` that
allows admins to see all possible themeable site settings, and which
themes
are changing the value from the default.

### Configuration

Theme authors can configure initial values themeable site settings using
a section in the `about.json` file like so:

```json
"theme_site_settings": {
  "search_experience": "search_field"
}
```

These values will not change when the theme updates, because we cannot
know if admins have manually changed them.

### Limitations

Themeable site settings are only really intended to control elements of
the UI, and when retrieving their value we require a theme ID, so these
limitations apply:

- Themeable site settings cannot be used in Sidekiq jobs
- Themeable site settings cannot be used in markdown rules
- Themeable site settings will be cached separately to client site
settings using theme ID as a key
- Themeable site settings will override keys on the `siteSettings`
service on the client using the application preloader
- `SiteSetting.client_settings_json` will not include themeable site
settings, instead you can call `SiteSetting.theme_site_settings_json`
with a theme ID

### Initial settings

There are only two site settings that will be themeable to begin with:

* `enable_welcome_banner`
* `search_experience`

And our new Horizon theme will take advantage of both. Over time, more
settings that control elements of the UI will be exposed this way.
2025-07-16 11:00:21 +10:00

213 lines
6.9 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe Themes::ThemeSiteSettingManager do
describe described_class::Contract, type: :model do
it { is_expected.to validate_presence_of(:theme_id) }
it { is_expected.to validate_presence_of(:name) }
end
describe ".call" do
subject(:result) { described_class.call(guardian:, params:) }
fab!(:admin)
fab!(:theme)
let(:guardian) { admin.guardian }
let(:params) { { theme_id: theme.id, name: "enable_welcome_banner", value: false } }
before { SiteSetting.refresh! }
context "when data is invalid" do
let(:params) { {} }
it { is_expected.to fail_a_contract }
end
context "when a non-admin user tries to change a setting" do
let(:guardian) { Guardian.new }
it { is_expected.to fail_a_policy(:current_user_is_admin) }
end
context "when the site setting is not a themeable one" do
let(:params) { { theme_id: theme.id, name: "title", value: "New Title" } }
it { is_expected.to fail_a_policy(:ensure_setting_is_themeable) }
end
context "when theme doesn't exist" do
before { theme.destroy! }
it "fails to find the theme" do
expect(result).to fail_to_find_a_model(:theme)
end
end
context "when creating a new theme site setting" do
it { is_expected.to run_successfully }
it "creates a new theme site setting" do
expect { result }.to change { ThemeSiteSetting.count }.by(1)
theme_site_setting = ThemeSiteSetting.last
expect(theme_site_setting).to have_attributes(
theme_id: theme.id,
name: "enable_welcome_banner",
value: "f",
data_type: SiteSetting.types[:bool],
)
end
it "logs the creation in staff action log" do
StaffActionLogger
.any_instance
.expects(:log_theme_site_setting_change)
.with(:enable_welcome_banner, nil, false, theme)
result
end
it "refreshes the value in the SiteSetting cache" do
expect { result }.to change { SiteSetting.enable_welcome_banner(theme_id: theme.id) }.from(
true,
).to(false)
end
it "should publish changes to clients for client site settings" do
message = MessageBus.track_publish("/client_settings") { result }.first
expect(message.data).to eq(
{ name: :enable_welcome_banner, scoped_to: { theme_id: theme.id }, value: false },
)
end
it "sends a DiscourseEvent for the change" do
event =
DiscourseEvent
.track_events { messages = MessageBus.track_publish { result } }
.find { |e| e[:event_name] == :theme_site_setting_changed }
expect(event).to be_present
expect(event[:params]).to eq([:enable_welcome_banner, true, false])
end
end
context "when updating an existing theme site setting" do
fab!(:theme_site_setting) do
# NOTE: This example is a little contrived, because `true` is the same as the site setting default,
# it would usually not ever be inserted into ThemeSiteSetting.
#
# However, we don't have any theme site settings yet with an enum with > 2 choices,
# so we have to fake things a bit here to make sure the update behaviour works.
Fabricate(:theme_site_setting, theme: theme, name: "enable_welcome_banner", value: true)
end
it { is_expected.to run_successfully }
it "updates the existing theme site setting" do
expect { result }.not_to change { ThemeSiteSetting.count }
expect(theme_site_setting.reload.value).to eq("f")
end
it "logs the creation in staff action log" do
StaffActionLogger
.any_instance
.expects(:log_theme_site_setting_change)
.with(:enable_welcome_banner, true, false, theme)
result
end
it "refreshes the value in the SiteSetting cache" do
expect { result }.to change { SiteSetting.enable_welcome_banner(theme_id: theme.id) }.from(
true,
).to(false)
end
it "should publish changes to clients for client site settings" do
message = MessageBus.track_publish("/client_settings") { result }.first
expect(message.data).to eq(
{ name: :enable_welcome_banner, scoped_to: { theme_id: theme.id }, value: false },
)
end
it "sends a DiscourseEvent for the change" do
event =
DiscourseEvent
.track_events { messages = MessageBus.track_publish { result } }
.find { |e| e[:event_name] == :theme_site_setting_changed }
expect(event[:params]).to eq([:enable_welcome_banner, true, false])
end
end
context "when removing a theme site setting by ommitting the value" do
let!(:theme_site_setting) do
Fabricate(
:theme_site_setting_with_service,
theme: theme,
name: "enable_welcome_banner",
value: false,
)
end
before { SiteSetting.refresh! }
let(:params) { { theme_id: theme.id, name: "enable_welcome_banner", value: nil } }
it { is_expected.to run_successfully }
it "updates the theme site setting to the site setting default value" do
result
expect(theme_site_setting.reload.value).to eq("t")
end
it "logs the removal in staff action log" do
StaffActionLogger
.any_instance
.expects(:log_theme_site_setting_change)
.with(:enable_welcome_banner, false, true, theme)
result
end
it "refreshes the value in the SiteSetting cache" do
expect { result }.to change { SiteSetting.enable_welcome_banner(theme_id: theme.id) }.from(
false,
).to(true)
end
it "should publish changes to clients for client site settings" do
message = MessageBus.track_publish("/client_settings") { result }.first
expect(message.data).to eq(
{ name: :enable_welcome_banner, scoped_to: { theme_id: theme.id }, value: true },
)
end
it "sends a DiscourseEvent for the change" do
event =
DiscourseEvent
.track_events { messages = MessageBus.track_publish { result } }
.find { |e| e[:event_name] == :theme_site_setting_changed }
expect(event[:params]).to eq([:enable_welcome_banner, false, true])
end
end
context "when changing value to the same as the site setting default" do
let!(:theme_site_setting) do
Fabricate(
:theme_site_setting_with_service,
theme: theme,
name: "enable_welcome_banner",
value: false,
)
end
let(:params) { { theme_id: theme.id, name: "enable_welcome_banner", value: true } }
it { is_expected.to run_successfully }
it "updates theme site setting when value matches default" do
result
expect(theme_site_setting.reload.value).to eq("t")
end
end
end
end