Skip to content

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

DEV: Improve `upload_theme` system test helper (#23663)

Alan Guo Xiang Tan16777547626a
diff --git a/spec/support/system_helpers.rb b/spec/support/system_helpers.rb
index f59851c2a61..87e11c8b0f9 100644
--- a/spec/support/system_helpers.rb
+++ b/spec/support/system_helpers.rb
@@ -22,8 +22,45 @@ module SystemHelpers
expect(page).to have_content("Signed in to #{user.encoded_username} successfully")
end
- def upload_theme(theme_dir)
- RemoteTheme.import_theme_from_directory(theme_dir)
+ # Uploads a theme from a directory.
+ #
+ # @param set_theme_as_default [Boolean] Whether to set the uploaded theme as the default theme for the site. Defaults to true.
+ #
+ # @return [Theme] The uploaded theme model given by `models/theme.rb`.
+ #
+ # @example Upload a theme and set it as default
+ # upload_theme("/path/to/theme")
+ def upload_theme(set_theme_as_default: true)
+ theme = RemoteTheme.import_theme_from_directory(theme_dir_from_caller)
+
+ if theme.component
+ raise "Uploaded theme is a theme component, please use the `upload_theme_component` method instead."
+ end
+
+ theme.set_default! if set_theme_as_default
+ theme
+ end
+
+ # Uploads a theme component from a directory.
+ #
+ # @param parent_theme_id [Integer] The ID of the theme to add the theme component to. Defaults to `SiteSetting.default_theme_id`.
+ #
+ # @return [Theme] The uploaded theme model given by `models/theme.rb`.
+ #
+ # @example Upload a theme component
+ # upload_theme_component("/path/to/theme_component")
+ #
+ # @example Upload a theme component and add it to a specific theme
+ # upload_theme_component("/path/to/theme_component", parent_theme_id: 123)
+ def upload_theme_component(parent_theme_id: SiteSetting.default_theme_id)
+ theme = RemoteTheme.import_theme_from_directory(theme_dir_from_caller)
+
+ if !theme.component
+ raise "Uploaded theme is not a theme component, please use the `upload_theme` method instead."
+ end
+
+ Theme.find(parent_theme_id).child_themes << theme
+ theme
end
def setup_system_test
@@ -170,4 +207,14 @@ module SystemHelpers
)
end
end
+
+ private
+
+ def theme_dir_from_caller
+ caller.each do |line|
+ if (split = line.split(%r{/spec/system/.+_spec.rb})).length > 1
+ return split.first
+ end
+ end
+ end
end