Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[40427] Better error messages on wiki edition conflict

Christophe Bliard71502d3aba25
diff --git a/app/contracts/wiki_pages/update_contract.rb b/app/contracts/wiki_pages/update_contract.rb
index 860e581ba33..748a7258a1f 100644
--- a/app/contracts/wiki_pages/update_contract.rb
+++ b/app/contracts/wiki_pages/update_contract.rb
@@ -28,5 +28,10 @@
module WikiPages
class UpdateContract < BaseContract
+ attribute :lock_version do
+ if model.lock_version.nil? || model.lock_version_changed?
+ errors.add :base, :error_conflict
+ end
+ end
end
end
diff --git a/app/helpers/error_message_helper.rb b/app/helpers/error_message_helper.rb
index 42c7acc0644..e71cf2aa553 100644
--- a/app/helpers/error_message_helper.rb
+++ b/app/helpers/error_message_helper.rb
@@ -27,32 +27,50 @@
#++
module ErrorMessageHelper
+ include ActionView::Helpers::OutputSafetyHelper
+
def error_messages_for(object)
object = instance_variable_get("@#{object}") unless object.respond_to?(:to_model)
object = convert_to_model(object)
return unless object
- render_error_messages_partial(object.errors.full_messages, object:)
+ render_error_messages_partial(object.errors, object)
end
# Will take a contract to display the errors in a rails form.
- # In order to have faulty field highlighted, the method sets
- # all errors in the contract on the object as well.
def error_messages_for_contract(object, errors)
return unless errors
- error_messages = errors.full_messages
-
+ # In order to have faulty field highlighted, the method sets
+ # all errors in the contract on the object as well.
object.errors.merge!(errors)
- render_error_messages_partial(error_messages, object:)
+ render_error_messages_partial(errors, object)
+ end
+
+ def render_error_messages_partial(errors, object)
+ return '' if errors.empty?
+
+ base_error_messages = errors.full_messages_for(:base)
+ fields_error_messages = errors.full_messages - base_error_messages
+
+ render partial: 'common/validation_error',
+ locals: { base_error_messages:,
+ fields_error_messages:,
+ object_name: object.class.model_name.human }
end
- def render_error_messages_partial(messages, object:)
- unless messages.empty?
- render partial: 'common/validation_error',
- locals: { error_messages: messages,
- object_name: object.class.model_name.human }
- end
+ def text_header_invalid_fields(base_error_messages, fields_error_messages)
+ return if fields_error_messages.blank?
+
+ i18n_key = base_error_messages.present? ? 'errors.header_additional_invalid_fields' : 'errors.header_invalid_fields'
+ t(i18n_key, count: fields_error_messages.count)
+ end
+
+ def list_of_messages(messages)
+ return if messages.blank?
+
+ messages = messages.map { |message| tag.li message }
+ tag.ul { safe_join(messages, "\n") }
end
end
diff --git a/app/views/common/_error_base.html.erb b/app/views/common/_error_base.html.erb
index 23a0689ac9f..11a31e6d853 100644
--- a/app/views/common/_error_base.html.erb
+++ b/app/views/common/_error_base.html.erb
@@ -37,9 +37,7 @@ See COPYRIGHT and LICENSE files for more details.
<p>
<strong><%= error_message %></strong>
</p>
- <p>
- <%= content_for :error_details %>
- </p>
+ <%= content_for :error_details %>
<% else %>
<%= error_message %>
<% end %>
diff --git a/app/views/common/_validation_error.html.erb b/app/views/common/_validation_error.html.erb
index 221937a7600..5fa3f9c2a3b 100644
--- a/app/views/common/_validation_error.html.erb
+++ b/app/views/common/_validation_error.html.erb
@@ -28,16 +28,15 @@ See COPYRIGHT and LICENSE files for more details.
++#%>
<% content_for :error_details, flush: true do %>
- <p><%= t("errors.header_invalid_fields") %></p>
- <ul>
- <% error_messages.each do |message| %>
- <li><%= message %></li>
- <% end %>
- </ul>
+ <%= list_of_messages(base_error_messages) %>
+ <%= text_header_invalid_fields(base_error_messages, fields_error_messages) %>
+ <%= list_of_messages(fields_error_messages) %>
<% end %>
<%= render partial: "common/error_base",
locals: {
- error_message: t("activerecord.errors.template.header", model: object_name, count: error_messages.count)
+ error_message: t("activerecord.errors.template.header",
+ model: object_name,
+ count: base_error_messages.count + fields_error_messages.count)
}
%>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 8941930dd7b..e9b7857d08b 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -900,6 +900,7 @@ en:
not_allowed_to_view: "is not allowed to view this resource."
locked: "is locked."
wiki_page:
+ error_conflict: "The wiki page has been updated by someone else while you were editing it."
attributes:
slug:
undeducible: "cannot be deduced from the title '%{title}'."
@@ -1018,7 +1019,12 @@ en:
'doorkeeper/application': "OAuth application"
errors:
- header_invalid_fields: "There were problems with the following fields:"
+ header_invalid_fields:
+ one: "There was a problem with the following field:"
+ other: "There were problems with the following fields:"
+ header_additional_invalid_fields:
+ one: "Additionally, there was a problem with the following field:"
+ other: "Additionally, there were problems with the following fields:"
field_erroneous_label: "This field is invalid: %{full_errors}\nPlease enter a valid value."
activity:
diff --git a/spec/helpers/error_message_helper_spec.rb b/spec/helpers/error_message_helper_spec.rb
new file mode 100644
index 00000000000..2fbc30758d1
--- /dev/null
+++ b/spec/helpers/error_message_helper_spec.rb
@@ -0,0 +1,127 @@
+#-- 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 'spec_helper'
+
+RSpec.describe ErrorMessageHelper do
+ let(:model) { WikiPage.new }
+
+ def escape_html(array)
+ array.map { CGI.escapeHTML _1 }
+ end
+
+ shared_examples 'error messages rendering' do
+ context 'when no errors' do
+ it { expect(rendered).to eq('') }
+ end
+
+ context 'with one field error' do
+ before do
+ errors.add(:title, :blank)
+ end
+
+ it { expect(rendered).to include(t('activerecord.errors.template.header', model: 'Wiki page', count: 1)) }
+ it { expect(rendered).to include(t('errors.header_invalid_fields', count: 1)) }
+ it { expect(rendered).to include(*escape_html(errors.full_messages)) }
+ end
+
+ context 'with two field errors' do
+ before do
+ errors.add(:title, :blank)
+ errors.add(:author, :blank)
+ end
+
+ it { expect(rendered).to include(t('activerecord.errors.template.header', model: 'Wiki page', count: 2)) }
+ it { expect(rendered).to include(t('errors.header_invalid_fields', count: 2)) }
+ it { expect(rendered).to include(*escape_html(errors.full_messages)) }
+ end
+
+ context 'with one base error' do
+ before do
+ errors.add(:base, :error_unauthorized)
+ end
+
+ it { expect(rendered).to include(t('activerecord.errors.template.header', model: 'Wiki page', count: 1)) }
+ it { expect(rendered).not_to include(t('errors.header_invalid_fields', count: 1)) }
+ it { expect(rendered).not_to include(t('errors.header_additional_invalid_fields', count: 1)) }
+ it { expect(rendered).to include(*escape_html(errors.full_messages)) }
+ end
+
+ context 'with one base error and one field error' do
+ before do
+ errors.add(:base, :error_unauthorized)
+ errors.add(:title, :blank)
+ end
+
+ it { expect(rendered).to include(t('activerecord.errors.template.header', model: 'Wiki page', count: 2)) }
+ it { expect(rendered).to include(t('errors.header_additional_invalid_fields', count: 1)) }
+ it { expect(rendered).to include(*escape_html(errors.full_messages)) }
+ end
+
+ context 'with two base errors and two field errors' do
+ before do
+ errors.add(:base, :error_unauthorized)
+ errors.add(:title, :blank)
+ errors.add(:base, :error_conflict)
+ errors.add(:author, :blank)
+ end
+
+ it { expect(rendered).to include(t('activerecord.errors.template.header', model: 'Wiki page', count: 4)) }
+ it { expect(rendered).to include(t('errors.header_additional_invalid_fields', count: 2)) }
+ it { expect(rendered).to include(*escape_html(errors.full_messages)) }
+ end
+ end
+
+ describe '#error_messages_for_contract' do
+ let(:errors) { ActiveModel::Errors.new(model) }
+
+ subject(:rendered) { helper.error_messages_for_contract(model, errors) }
+
+ include_examples 'error messages rendering'
+ end
+
+ describe '#error_messages_for' do
+ let(:errors) { model.errors }
+
+ subject(:rendered) { helper.error_messages_for(model) }
+
+ it 'accesses the model from instance variables if a name is given' do
+ helper.instance_variable_set(:@wiki_page, model)
+ model.errors.add(:base, :error_conflict)
+ expect(helper.error_messages_for('wiki_page')).to include(*escape_html(errors.full_messages))
+ end
+
+ it 'renders nothing if there is no instance variable with the given name' do
+ helper.instance_variable_set(:@wiki_page, model)
+ model.errors.add(:base, :error_conflict)
+ expect(helper.error_messages_for('work_package')).to be_nil
+ end
+
+ include_examples 'error messages rendering'
+ end
+end
diff --git a/spec/views/common/validation_error.html.erb_spec.rb b/spec/views/common/validation_error.html.erb_spec.rb
index 95f5a7a689f..30dd4cfbabf 100644
--- a/spec/views/common/validation_error.html.erb_spec.rb
+++ b/spec/views/common/validation_error.html.erb_spec.rb
@@ -29,15 +29,26 @@
require 'spec_helper'
RSpec.describe 'common/_validation_error' do
- let(:error_messages) { ['Something went completely wrong!'] }
+ let(:base_error_messages) { ['Something went completely wrong!'] }
+ let(:fields_error_messages) { ['This field is incorrect.', 'This cannot be blank.'] }
before do
view.content_for(:error_details, 'Clear this!')
render partial: 'common/validation_error',
- locals: { error_messages:,
+ locals: { base_error_messages:,
+ fields_error_messages:,
object_name: 'Test' }
end
- it { expect(rendered).not_to include('Clear this!') }
+ it 'flushes the buffer before rendering' do
+ # that means the same partial can be called multiple times without side effects
+ expect(rendered).not_to include('Clear this!')
+ end
+
+ it 'includes all given error messages' do
+ expect(rendered).to include('Something went completely wrong!')
+ expect(rendered).to include('This field is incorrect.')
+ expect(rendered).to include('This cannot be blank.')
+ end
end