discourse/spec/requests/admin/theme_site_settings_controller_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

82 lines
2.8 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe Admin::Config::ThemeSiteSettingsController do
fab!(:admin)
fab!(:theme_1) { Fabricate(:theme) }
fab!(:theme_2) { Fabricate(:theme) }
fab!(:theme_3) { Fabricate(:theme) }
fab!(:theme_1_theme_site_setting) do
Fabricate(
:theme_site_setting_with_service,
theme: theme_1,
name: "search_experience",
value: "search_field",
)
end
fab!(:theme_2_theme_site_setting) do
Fabricate(
:theme_site_setting_with_service,
theme: theme_2,
name: "enable_welcome_banner",
value: false,
)
end
before { sign_in(admin) }
describe "#index" do
it "gets all theme site settings and the themes which have overridden values for these settings" do
get "/admin/config/theme-site-settings.json"
expect(response.status).to eq(200)
json = response.parsed_body
expect(json["themeable_site_settings"]).to include(
"search_experience",
"enable_welcome_banner",
)
search_setting =
json["themes_with_site_setting_overrides"]["search_experience"].deep_symbolize_keys
expect(search_setting[:setting]).to eq("search_experience")
expect(search_setting[:default]).to eq("search_icon")
expect(search_setting[:description]).to eq(I18n.t("site_settings.search_experience"))
expect(search_setting[:type]).to eq("enum")
expect(search_setting[:themes].find { |t| t[:theme_id] == theme_1.id }).to include(
theme_name: theme_1.name,
theme_id: theme_1.id,
value: "search_field",
)
welcome_banner_setting =
json["themes_with_site_setting_overrides"]["enable_welcome_banner"].deep_symbolize_keys
expect(welcome_banner_setting[:setting]).to eq("enable_welcome_banner")
expect(welcome_banner_setting[:default]).to eq(true)
expect(welcome_banner_setting[:description]).to eq(
I18n.t("site_settings.enable_welcome_banner"),
)
expect(welcome_banner_setting[:type]).to eq("bool")
expect(welcome_banner_setting[:themes].find { |t| t[:theme_id] == theme_2.id }).to include(
theme_name: theme_2.name,
theme_id: theme_2.id,
value: false,
)
end
it "does not count theme site settings with same value as site setting default as overridden" do
theme_2_theme_site_setting.update!(
value: SiteSetting.type_supervisor.to_db_value(:enable_welcome_banner, true).first,
)
get "/admin/config/theme-site-settings.json"
expect(response.status).to eq(200)
json = response.parsed_body
welcome_banner_setting =
json["themes_with_site_setting_overrides"]["enable_welcome_banner"].deep_symbolize_keys
expect(welcome_banner_setting[:themes].find { |t| t[:theme_id] == theme_2.id }).to be_nil
end
end
end