FIX: Regression in custom homepage modifier used in theme components (#27569)

This commit is contained in:
Penar Musaraj 2024-06-21 11:24:11 -04:00 committed by GitHub
parent 099bffe37a
commit f4108702c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 91 additions and 65 deletions

View File

@ -492,7 +492,7 @@ class ApplicationController < ActionController::Base
end
def current_homepage
current_user&.user_option&.homepage || HomepageHelper.resolve(@theme_id, current_user)
current_user&.user_option&.homepage || HomepageHelper.resolve(request, current_user)
end
def serialize_data(obj, serializer, opts = nil)

View File

@ -560,7 +560,7 @@ module ApplicationHelper
end
def current_homepage
current_user&.user_option&.homepage || HomepageHelper.resolve(theme_id, current_user)
current_user&.user_option&.homepage || HomepageHelper.resolve(request, current_user)
end
def build_plugin_html(name)

View File

@ -9,11 +9,10 @@ class HomePageConstraint
return @filter == "finish_installation" if SiteSetting.has_login_hint?
current_user = CurrentUser.lookup_from_env(request.env)
# ensures we resolve the theme id as early as possible
theme_id = ThemeResolver.resolve_theme_id(request, Guardian.new(current_user), current_user)
ThemeResolver.resolve_theme_id(request, Guardian.new(current_user), current_user)
homepage = current_user&.user_option&.homepage || HomepageHelper.resolve(theme_id, current_user)
homepage = current_user&.user_option&.homepage || HomepageHelper.resolve(request, current_user)
homepage == @filter
rescue Discourse::InvalidAccess, Discourse::ReadOnly
false

View File

@ -1,8 +1,8 @@
# frozen_string_literal: true
class HomepageHelper
def self.resolve(theme_id = nil, current_user = nil)
return "custom" if ThemeModifierHelper.new(theme_ids: theme_id).custom_homepage
def self.resolve(request = nil, current_user = nil)
return "custom" if ThemeModifierHelper.new(request: request).custom_homepage
current_user ? SiteSetting.homepage : SiteSetting.anonymous_homepage
end

View File

@ -53,29 +53,6 @@ describe "Homepage", type: :system do
expect(page).to have_css(".navigation-container .categories.active", text: "Categories")
end
context "when default theme uses a custom_homepage modifier" do
before do
theme.theme_modifier_set.custom_homepage = true
theme.theme_modifier_set.save!
theme.set_default!
end
it "shows empty state to regular users" do
sign_in user
visit "/"
expect(page).to have_no_css(".list-container")
expect(page).to have_no_css(".alert-info")
end
it "shows empty state and notice to admins" do
sign_in admin
visit "/"
expect(page).to have_no_css(".list-container")
expect(page).to have_css(".alert-info")
end
shared_examples "a custom homepage" do
it "shows the custom homepage component" do
visit "/"
@ -134,6 +111,29 @@ describe "Homepage", type: :system do
end
end
context "when default theme uses a custom_homepage modifier" do
before do
theme.theme_modifier_set.custom_homepage = true
theme.theme_modifier_set.save!
theme.set_default!
end
it "shows empty state to regular users" do
sign_in user
visit "/"
expect(page).to have_no_css(".list-container")
expect(page).to have_no_css(".alert-info")
end
it "shows empty state and notice to admins" do
sign_in admin
visit "/"
expect(page).to have_no_css(".list-container")
expect(page).to have_css(".alert-info")
end
context "when the theme adds content to the [custom-homepage] connector" do
let!(:basic_html_field) do
Fabricate(
@ -175,4 +175,31 @@ describe "Homepage", type: :system do
include_examples "a custom homepage"
end
end
context "when a theme component uses the custom_homepage modifier" do
let!(:component) { Fabricate(:theme, component: true) }
let!(:component_html_field) do
Fabricate(
:theme_field,
theme: component,
type_id: ThemeField.types[:html],
target_id: Theme.targets[:common],
name: "head_tag",
value: <<~HTML,
<script type="text/x-handlebars" data-template-name="/connectors/custom-homepage/new-home">
<div class="new-home">Hi friends!</div>
</script>
HTML
)
end
before do
component.theme_modifier_set.custom_homepage = true
component.theme_modifier_set.save!
theme.add_relative_theme!(:child, component)
theme.set_default!
end
include_examples "a custom homepage"
end
end