0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-08 17:53:55 +08:00
discourse/spec/lib/homepage_helper_spec.rb
Kris d2d79d290e
FEATURE: explicit default homepage setting (#41785)
This creates an explicit "default homepage" setting, because right now
the experience of changing the homepage via the "top menu" setting isn't
very intuitive. This will also make it easier to support new homepage
options in the future that don't necessarily need to exist within "top
menu."

The setting is empty by default, and when empty it falls back to "First
item in top menu" — using the current functionality as a fallback means
we don't need a migration.

The options in the list are the core top menu choices plus anything
registered via `Discourse.filters.push(:filter)`. Registered filters are
an existing pattern that come with all the prerequisites to function as
a homepage (server & client routes, and a topic list).

So for example, how the "votes" option is produced

plugins/discourse-topic-voting/plugin.rb
```ruby
Discourse.filters.push(:votes)
Discourse.anonymous_filters.push(:votes)
```

anonymous_filters is optional, if there's no anonymous equivalent of a
given option, we fall back to the first anon-viewable top menu item
(there's always at least one)

lib/discourse_topic_voting/topic_query_extension.rb
```ruby
def list_votes
  create_list(:votes, unordered: true) do |topics|
    topics.joins(
      "LEFT JOIN topic_voting_topic_vote_count dvtvc ON dvtvc.topic_id = topics.id",
    ).order("COALESCE(dvtvc.votes_count,'0')::integer DESC, topics.bumped_at DESC")
  end
end
```

config/locales/client.en.yml
```yaml
js:
  filters:
    votes:
      title: "Votes"
      help: "topics with the all time most votes"
```

<img width="600" alt="image"
src="https://github.com/user-attachments/assets/6b580e92-1b58-477c-81ab-6ceacc020672"
/>

This also adds a persistent "Default" option to the Default Home Page
user preference, which was previously only visible when a custom
homepage is set by a theme.

A future enhancement could add a plugin API for registering non-filter
homepage options (e.g. a chat channel)

Some related fixes along for the ride:

* select-kit treated a blank value as "clear selection", so it could not
be selected or displayed... this allows "First item in top menu" as an
alias for being unset
* the admin combo-box couldn't detect when the first choice's value was
blank, and submitted the whole object
* topic lists advertised RSS feeds that don't exist for registered
filters (this was happening with /votes)
2026-07-22 16:22:19 -04:00

102 lines
3.4 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe HomepageHelper do
describe "resolver" do
fab!(:user)
it "returns latest by default" do
expect(HomepageHelper.resolve).to eq("latest")
end
context "when a theme has a custom homepage" do
before { ThemeModifierHelper.any_instance.stubs(:custom_homepage).returns(true) }
it "returns custom" do
expect(HomepageHelper.resolve).to eq("custom")
end
it "returns the configured crawler route for crawler requests" do
SiteSetting.custom_homepage_crawler_route = "categories"
request = ActionDispatch::TestRequest.create("HTTP_USER_AGENT" => "Googlebot")
expect(HomepageHelper.resolve(request)).to eq("categories")
end
end
context "when a plugin modifies the custom_homepage_enabled to true" do
before do
DiscoursePluginRegistry
.expects(:apply_modifier)
.with(:custom_homepage_enabled, false, request: nil, current_user: nil)
.returns(true)
end
it "returns custom" do
expect(HomepageHelper.resolve).to eq("custom")
end
end
it "returns custom when a plugin modifies the custom_homepage_enabled to true" do
DiscoursePluginRegistry
.expects(:apply_modifier)
.with(:custom_homepage_enabled, false, request: nil, current_user: nil)
.returns(true)
expect(HomepageHelper.resolve).to eq("custom")
end
context "when the configured homepage is not valid for anons" do
before do
SiteSetting.top_menu = "new|top|latest"
SiteSetting.default_homepage = "new"
end
it "distinguishes between auth homepage and anon homepage" do
expect(HomepageHelper.resolve(nil, user)).to eq("new")
# new is not a valid route for anon users, so the anon homepage falls back
# to the first anon-visible item in the top menu, top
expect(HomepageHelper.resolve).to eq(SiteSetting.anonymous_homepage)
expect(HomepageHelper.resolve).to eq("top")
end
end
context "when default_homepage is set" do
before { SiteSetting.top_menu = "latest|new|top|categories" }
it "uses default_homepage regardless of top_menu order" do
SiteSetting.default_homepage = "categories"
expect(HomepageHelper.resolve(nil, user)).to eq("categories")
expect(HomepageHelper.resolve).to eq("categories")
end
it "uses default_homepage even when it is not one of the top_menu items" do
SiteSetting.top_menu = "latest|new|categories"
SiteSetting.default_homepage = "top"
expect(HomepageHelper.resolve(nil, user)).to eq("top")
expect(HomepageHelper.resolve).to eq("top")
end
end
context "when default_homepage is not set" do
before { SiteSetting.top_menu = "new|top|latest" }
it "falls back to the first top_menu item, and the first anon-visible one for anons" do
expect(HomepageHelper.resolve(nil, user)).to eq("new")
expect(HomepageHelper.resolve).to eq("top")
end
end
context "with login required" do
before do
SiteSetting.login_required = true
SiteSetting.top_menu = "new|top|latest"
SiteSetting.default_homepage = "new"
end
it "returns a blank route for anon, and the configured homepage for an authenticated user" do
expect(HomepageHelper.resolve).to eq("blank")
expect(HomepageHelper.resolve(nil, user)).to eq("new")
end
end
end
end