Skip to content

Commit

Permalink
Merge pull request #12602 from opf/bug/48158-links-from-the-welcome-t…
Browse files Browse the repository at this point in the history
…ext-should-continue-working-when-text-is-edited

Fix links with edited opSetting:base_url in them
  • Loading branch information
cbliard committed May 10, 2023
2 parents 6b04d61 + 04943bf commit cfd42da
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 15 deletions.
@@ -1,3 +1,5 @@
# frozen_string_literal: true

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2023 the OpenProject GmbH
Expand Down Expand Up @@ -37,14 +39,16 @@ class SettingMacrosFilter < HTML::Pipeline::Filter
def self.regexp
%r{
\{\{opSetting:(.+?)\}\}
|
https?://[^% ]+?/%7B%7BopSetting:(base_url)%7D%7D
}x
end

def call
return html unless applicable?

html.gsub(self.class.regexp) do |matched_string|
variable = $1.to_s
variable = ($1.presence || $2.presence).dup
variable.gsub!('\\', '')

if ALLOWED_SETTINGS.include?(variable)
Expand Down Expand Up @@ -72,7 +76,7 @@ def url_helpers
##
# Faster inclusion check before the regex is being applied
def applicable?
html.include?('{{opSetting:')
html.include?('{{opSetting:') || html.include?('%7B%7BopSetting:')
end
end
end
Expand Down
40 changes: 30 additions & 10 deletions spec/features/homescreen/index_spec.rb
Expand Up @@ -29,25 +29,45 @@
require 'spec_helper'

describe 'Homescreen index' do
let!(:user) { build_stubbed(:user) }
let(:admin) { create(:admin) }
let(:user) { build_stubbed(:user) }
let!(:project) { create(:public_project, identifier: 'public-project') }
let(:general_settings_page) { Pages::Admin::SystemSettings::General.new }

before do
login_as user
visit root_url
end
describe 'with a dynamic URL in the welcome text' do
before do
Setting.welcome_text = "With [a link to the public project]({{opSetting:base_url}}/projects/public-project)"
Setting.welcome_on_homescreen = true
end

describe 'with a dynamic URL in the welcome text',
with_settings: {
welcome_text: "With [a link to the public project]({{opSetting:base_url}}/projects/public-project)",
welcome_on_homescreen?: true
} do
it 'renders the correct link' do
login_as user
visit root_url
expect(page)
.to have_selector("a[href=\"#{OpenProject::Application.root_url}/projects/public-project\"]")

click_link "a link to the public project"
expect(page).to have_current_path project_path(project)
end

it 'can change the welcome text and still have a valid link', js: true do
login_as admin

general_settings_page.visit!

welcome_text_editor = general_settings_page.welcome_text_editor
scroll_to_element(welcome_text_editor.container)
welcome_text_editor.click_and_type_slowly('Hello! ')

general_settings_page.press_save_button
general_settings_page.expect_and_dismiss_toaster

visit root_url
expect(page)
.to have_selector("a[href=\"#{OpenProject::Application.root_url}/projects/public-project\"]")

click_link "a link to the public project"
expect(page).to have_current_path /#{Regexp.escape(project_path(project))}\/?$/
end
end
end
14 changes: 11 additions & 3 deletions spec/features/work_packages/table/switch_types_spec.rb
Expand Up @@ -48,9 +48,17 @@
query
end

let(:type_field) { wp_table.edit_field(work_package, :type) }
let(:text_field) { wp_table.edit_field(work_package, cf_text.attribute_name(:camel_case)) }
let(:req_text_field) { wp_table.edit_field(work_package, cf_req_text.attribute_name(:camel_case)) }
def type_field
wp_table.edit_field(work_package, :type)
end

def text_field
wp_table.edit_field(work_package, cf_text.attribute_name(:camel_case))
end

def req_text_field
wp_table.edit_field(work_package, cf_req_text.attribute_name(:camel_case))
end

before do
login_as(user)
Expand Down
Expand Up @@ -43,6 +43,8 @@
[Link with setting]({{opSetting:base_url}}/foo/bar)
[Saved and transformed link with setting](http://localhost:3000/prefix/%7B%7BopSetting:base_url%7D%7D/foo/bar)
Inline reference to invalid variable: {{opSetting:smtp_password}}
Inline reference to missing variable: {{opSetting:does_not_exist}}
Expand All @@ -61,6 +63,10 @@
<a href="#{OpenProject::Application.root_url}/foo/bar" rel="noopener noreferrer"
class="op-uc-link">Link with setting</a>
</p>
<p class="op-uc-p">
<a href="#{OpenProject::Application.root_url}/foo/bar" rel="noopener noreferrer"
class="op-uc-link">Saved and transformed link with setting</a>
</p>
<p class="op-uc-p">
Inline reference to invalid variable: {{opSetting:smtp_password}}
</p>
Expand Down
54 changes: 54 additions & 0 deletions spec/support/pages/admin/system_settings/general.rb
@@ -0,0 +1,54 @@
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2023 the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++

require 'support/pages/page'

module Pages::Admin::SystemSettings
class General < ::Pages::Page
def path
"/admin/settings/general"
end

def toast_type
:rails
end

def welcome_text_editor
Components::WysiwygEditor.new welcome_text_selector
end

def welcome_text_selector
'ckeditor-augmented-textarea[textarea-selector="#settings_welcome_text"]'
end

def press_save_button
scroll_to(:bottom)
click_button('Save')
end
end
end

0 comments on commit cfd42da

Please sign in to comment.