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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[37963] Fix formatting of multi-user custom field journal details
Oliver Güntherf4da09001124
diff --git a/config/locales/en.yml b/config/locales/en.ymlindex c25a412dc89..55359632079 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -1478,6 +1478,7 @@ en:label_deleted: "deleted"label_deleted_custom_field: "(deleted custom field)"label_deleted_custom_option: "(deleted option)"+ label_missing_or_hidden_custom_option: "(missing value or lacking permissions to access)"label_descending: "Descending"label_details: "Details"label_development_roadmap: "Development roadmap"diff --git a/lib/open_project/journal_formatter/custom_field.rb b/lib/open_project/journal_formatter/custom_field.rbindex 215e4102116..2ce45175f62 100644--- a/lib/open_project/journal_formatter/custom_field.rb+++ b/lib/open_project/journal_formatter/custom_field.rb@@ -36,7 +36,7 @@ class OpenProject::JournalFormatter::CustomField < ::JournalFormatter::Baseif custom_fieldlabel = custom_field.name- old_value, value = get_old_and_new_value custom_field, values+ old_value, value = get_formatted_values custom_field, valueselselabel = I18n.t(:label_deleted_custom_field)old_value = values.first@@ -46,30 +46,50 @@ class OpenProject::JournalFormatter::CustomField < ::JournalFormatter::Base[label, old_value, value]end- def get_old_and_new_value(custom_field, values)- if custom_field.list?- format_list custom_field, values+ def get_formatted_values(custom_field, values)+ modifier_fn = method(get_modifier_function(custom_field))+ formatted_values custom_field, values, modifier_fn+ end++ def get_modifier_function(custom_field)+ case custom_field.field_format+ when 'list'+ :find_list_value+ when 'user'+ :find_user_valueelse- format_single custom_field, values+ :format_valueendend- def format_list(custom_field, values)+ def formatted_values(custom_field, values, modifier_fn)old_value, new_value = values- old_option = find_list_value custom_field, old_value if old_value- new_option = find_list_value custom_field, new_value if new_value+ old_option = modifier_fn.call(old_value, custom_field) if old_value+ new_option = modifier_fn.call(new_value, custom_field) if new_value[old_option || old_value, new_option || new_value]end- def format_single(custom_field, values)- old_value = format_value(values.first, custom_field) if values.first- value = format_value(values.last, custom_field) if values.last+ def find_user_value(value, _custom_field)+ ids = value.split(",").map(&:to_i)++ # Lookup any visible user we can find+ user_lookup =+ Principal+ .in_visible_project_or_me(User.current)+ .where(id: ids)+ .index_by(&:id)- [old_value, value]+ ids.map do |id|+ if user_lookup.key?(id)+ user_lookup[id].name+ else+ I18n.t(:label_missing_or_hidden_custom_option)+ end+ end.join(', ')end- def find_list_value(custom_field, id)+ def find_list_value(id, custom_field)ids = id.split(",").map(&:to_i)id_value = custom_fielddiff --git a/spec/lib/journal_formatter/custom_field_spec.rb b/spec/lib/journal_formatter/custom_field_spec.rbindex 3cd3a869842..bea7024d256 100644--- a/spec/lib/journal_formatter/custom_field_spec.rb+++ b/spec/lib/journal_formatter/custom_field_spec.rb@@ -26,13 +26,13 @@# See docs/COPYRIGHT.rdoc for more details.#++-require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')+require 'spec_helper'describe OpenProject::JournalFormatter::CustomField doinclude CustomFieldsHelperinclude ActionView::Helpers::TagHelper- let(:klass) { OpenProject::JournalFormatter::CustomField }+ let(:klass) { described_class }let(:instance) { klass.new(journal) }let(:id) { 1 }let(:journal) do@@ -42,218 +42,290 @@ describe OpenProject::JournalFormatter::CustomField doFactoryBot.build_stubbed(:work_package_custom_field).tap do |cf|allow(CustomField).to receive(:find_by)- .with(id: cf.id)- .and_return(cf)+ .with(id: cf.id)+ .and_return(cf)endend- let(:key) { "custom_fields_#{custom_field.id}" }- describe '#render' do- describe 'WITH the first value being nil, and the second a valid value as string' do- let(:values) { [nil, '1'] }- let(:formatted_value) { format_value(values.last, custom_field) }+ let(:key) do+ "custom_fields_#{custom_field.id}"+ end- let(:expected) do- I18n.t(:text_journal_set_to,- label: "<strong>#{custom_field.name}</strong>",- value: "<i title=\"#{formatted_value}\">#{formatted_value}</i>")- end+ describe 'a multi-select user field' do+ subject(:rendered) { instance.render(key, values) }- it { expect(instance.render(key, values)).to eq(expected) }+ let(:user1) { FactoryBot.build_stubbed :user, firstname: 'Foo', lastname: 'Bar' }+ let(:user2) { FactoryBot.build_stubbed :user, firstname: 'Bla', lastname: 'Blub' }++ let(:custom_field) do+ FactoryBot.build_stubbed(:user_wp_custom_field).tap do |cf|+ allow(CustomField)+ .to receive(:find_by)+ .with(id: cf.id)+ .and_return(cf)+ endend- describe 'WITH the first value being a valid value as a string, and the second being a valid value as a string' do- let(:values) { %w[0 1] }- let(:old_formatted_value) { format_value(values.first, custom_field) }- let(:new_formatted_value) { format_value(values.last, custom_field) }+ let(:wherestub) { class_double(Principal) }+ let(:values) { [nil, "#{user1.id},#{user2.id}"] }- let(:expected) do- I18n.t(:text_journal_changed,- label: "<strong>#{custom_field.name}</strong>",- linebreak: '',- old: "<i title=\"#{old_formatted_value}\">#{old_formatted_value}</i>",- new: "<i title=\"#{new_formatted_value}\">#{new_formatted_value}</i>")- end+ let(:expected) do+ I18n.t(:text_journal_changed,+ label: "<strong>#{custom_field.name}</strong>",+ linebreak: '',+ old: "<i title=\"#{old_formatted_value}\">#{old_formatted_value}</i>",+ new: "<i title=\"#{new_formatted_value}\">#{new_formatted_value}</i>")+ end- it { expect(instance.render(key, values)).to eq(expected) }+ before do+ allow(Principal)+ .to receive(:in_visible_project_or_me).and_return(wherestub)++ allow(wherestub)+ .to receive(:where)+ .with(id: [user1.id, user2.id])+ .and_return(visible_users)end- describe 'WITH the first value being a valid value as a string, and the second being nil' do- let(:values) { ['0', nil] }- let(:formatted_value) { format_value(values.first, custom_field) }+ context 'with two visible users' do+ let(:visible_users) { [user1, user2] }+ let(:formatted_value) do+ "Foo Bar, Bla Blub"+ endlet(:expected) do- I18n.t(:text_journal_deleted,+ I18n.t(:text_journal_set_to,label: "<strong>#{custom_field.name}</strong>",- old: "<strike><i title=\"#{formatted_value}\">#{formatted_value}</i></strike>")+ value: "<i title=\"#{formatted_value}\">#{formatted_value}</i>")end- it { expect(instance.render(key, values)).to eq(expected) }- end+ it 'outputs both formatted names' do+ expect(rendered).to eq expected+ end- describe "WITH the first value being nil, and the second a valid value as string- WITH no html requested" do- let(:values) { [nil, '1'] }+ context 'with only one visible user' do+ let(:visible_users) { [user1] }- let(:expected) do- I18n.t(:text_journal_set_to,- label: custom_field.name,- value: format_value(values.last, custom_field))+ let(:formatted_value) do+ "Foo Bar, (missing value or lacking permissions to access)"+ end+ let(:expected) do+ I18n.t(:text_journal_set_to,+ label: "<strong>#{custom_field.name}</strong>",+ value: "<i title=\"#{formatted_value}\">#{formatted_value}</i>")+ end++ it 'outputs the one visible formatted name' do+ expect(rendered).to eq expected+ endend+ end+ end++ describe 'WITH the first value being nil, and the second a valid value as string' do+ let(:values) { [nil, '1'] }+ let(:formatted_value) { format_value(values.last, custom_field) }- it { expect(instance.render(key, values, no_html: true)).to eq(expected) }+ let(:expected) do+ I18n.t(:text_journal_set_to,+ label: "<strong>#{custom_field.name}</strong>",+ value: "<i title=\"#{formatted_value}\">#{formatted_value}</i>")end- describe "WITH the first value being a valid value as a string, and the second being a valid value as a string- WITH no html requested" do- let(:values) { %w[0 1] }+ it { expect(instance.render(key, values)).to eq(expected) }+ end- let(:expected) do- I18n.t(:text_journal_changed_plain,- label: custom_field.name,- old: format_value(values.first, custom_field),- linebreak: '',- new: format_value(values.last, custom_field))- end+ describe 'WITH the first value being a valid value as a string, and the second being a valid value as a string' do+ let(:values) { %w[0 1] }+ let(:old_formatted_value) { format_value(values.first, custom_field) }+ let(:new_formatted_value) { format_value(values.last, custom_field) }++ let(:expected) do+ I18n.t(:text_journal_changed,+ label: "<strong>#{custom_field.name}</strong>",+ old: "<i title=\"#{format_value(values.first, custom_field)}\">#{format_value(values.first, custom_field)}</i>",+ linebreak: '',+ new: "<i title=\"#{format_value(values.last, custom_field)}\">#{format_value(values.last, custom_field)}</i>")+ end++ it { expect(instance.render(key, values)).to eq(expected) }+ end++ describe 'WITH the first value being a valid value as a string, and the second being nil' do+ let(:values) { ['0', nil] }+ let(:formatted_value) { format_value(values.first, custom_field) }- it { expect(instance.render(key, values, no_html: true)).to eq(expected) }+ let(:expected) do+ I18n.t(:text_journal_deleted,+ label: "<strong>#{custom_field.name}</strong>",+ old: "<strike><i title=\"#{formatted_value}\">#{formatted_value}</i></strike>")end- describe "WITH the first value being a valid value as a string, and the second being nil+ it { expect(instance.render(key, values)).to eq(expected) }+ end++ describe "WITH the first value being nil, and the second a valid value as stringWITH no html requested" do- let(:values) { ['0', nil] }+ let(:values) { [nil, '1'] }- let(:expected) do- I18n.t(:text_journal_deleted,- label: custom_field.name,- old: format_value(values.first, custom_field))- end+ let(:expected) do+ I18n.t(:text_journal_set_to,+ label: custom_field.name,+ value: format_value(values.last, custom_field))+ end- it { expect(instance.render(key, values, no_html: true)).to eq(expected) }+ it { expect(instance.render(key, values, no_html: true)).to eq(expected) }+ end++ describe "WITH the first value being a valid value as a string, and the second being a valid value as a string+ WITH no html requested" do+ let(:values) { %w[0 1] }++ let(:expected) do+ I18n.t(:text_journal_changed_plain,+ label: custom_field.name,+ old: format_value(values.first, custom_field),+ linebreak: '',+ new: format_value(values.last, custom_field))end- describe "WITH the first value being nil, and the second a valid value as string- WITH the custom field being deleted" do- let(:values) { [nil, '1'] }- let(:key) { 'custom_values0' }+ it { expect(instance.render(key, values, no_html: true)).to eq(expected) }+ end- let(:expected) do- I18n.t(:text_journal_set_to,- label: "<strong>#{I18n.t(:label_deleted_custom_field)}</strong>",- value: "<i title=\"#{values.last}\">#{values.last}</i>")- end+ describe "WITH the first value being a valid value as a string, and the second being nil+ WITH no html requested" do+ let(:values) { ['0', nil] }- it { expect(instance.render(key, values)).to eq(expected) }+ let(:expected) do+ I18n.t(:text_journal_deleted,+ label: custom_field.name,+ old: format_value(values.first, custom_field))end- describe "WITH the first value being a valid value as a string, and the second being a valid value as a string- WITH the custom field being deleted" do- let(:values) { %w[0 1] }- let(:key) { 'custom_values0' }+ it { expect(instance.render(key, values, no_html: true)).to eq(expected) }+ end- let(:expected) do- I18n.t(:text_journal_changed,- label: "<strong>#{I18n.t(:label_deleted_custom_field)}</strong>",- linebreak: '',- old: "<i title=\"#{values.first}\">#{values.first}</i>",- new: "<i title=\"#{values.last}\">#{values.last}</i>")- end+ describe "WITH the first value being nil, and the second a valid value as string+ WITH the custom field being deleted" do+ let(:values) { [nil, '1'] }+ let(:key) { 'custom_values0' }- it { expect(instance.render(key, values)).to eq(expected) }+ let(:expected) do+ I18n.t(:text_journal_set_to,+ label: "<strong>#{I18n.t(:label_deleted_custom_field)}</strong>",+ value: "<i title=\"#{values.last}\">#{values.last}</i>")end- describe "WITH the first value being a valid value as a string, and the second being nil+ it { expect(instance.render(key, values)).to eq(expected) }+ end++ describe "WITH the first value being a valid value as a string, and the second being a valid value as a stringWITH the custom field being deleted" do- let(:values) { ['0', nil] }- let(:key) { 'custom_values0' }+ let(:values) { %w[0 1] }+ let(:key) { 'custom_values0' }++ let(:expected) do+ I18n.t(:text_journal_changed,+ label: "<strong>#{I18n.t(:label_deleted_custom_field)}</strong>",+ linebreak: '',+ old: "<i title=\"#{values.first}\">#{values.first}</i>",+ new: "<i title=\"#{values.last}\">#{values.last}</i>")+ end- let(:expected) do- I18n.t(:text_journal_deleted,- label: "<strong>#{I18n.t(:label_deleted_custom_field)}</strong>",- old: "<strike><i title=\"#{values.first}\">#{values.first}</i></strike>")- end+ it { expect(instance.render(key, values)).to eq(expected) }+ end- it { expect(instance.render(key, values)).to eq(expected) }+ describe "WITH the first value being a valid value as a string, and the second being nil+ WITH the custom field being deleted" do+ let(:values) { ['0', nil] }+ let(:key) { 'custom_values0' }++ let(:expected) do+ I18n.t(:text_journal_deleted,+ label: "<strong>#{I18n.t(:label_deleted_custom_field)}</strong>",+ old: "<strike><i title=\"#{values.first}\">#{values.first}</i></strike>")end- context 'for a multi list cf' do- let(:custom_field) do- FactoryBot.build_stubbed(:list_wp_custom_field, multi_value: true).tap do |cf|- allow(CustomField)- .to receive(:find_by)- .with(id: cf.id)- .and_return(cf)-- cf_options = double('custom_options')- old_options = double('selected options')- new_options = double('selected options')-- allow(cf)- .to receive(:custom_options)- .and_return cf_options-- allow(cf_options)- .to receive(:where)- .with(id: [1, 2])- .and_return old_options-- allow(cf_options)- .to receive(:where)- .with(id: [3, 4])- .and_return new_options-- allow(old_options)- .to receive(:order)- .with(:position)- .and_return(old_options)-- allow(new_options)- .to receive(:order)- .with(:position)- .and_return(new_options)-- allow(old_options)- .to receive(:pluck)- .with(:id, :value)- .and_return(old_custom_option_names)-- allow(new_options)- .to receive(:pluck)- .with(:id, :value)- .and_return(new_custom_option_names)- end- end- let(:old_custom_option_names) { [[1, 'cf 1'], [2, 'cf 2']] }- let(:new_custom_option_names) { [[3, 'cf 3'], [4, 'cf 4']] }+ it { expect(instance.render(key, values)).to eq(expected) }+ end- describe "WITH the first value being a comma separated list of ids, and the second being a comma separated list of ids" do- let(:values) { %w[1,2 3,4] }+ context 'for a multi list cf' do+ let(:custom_field) do+ FactoryBot.build_stubbed(:list_wp_custom_field, multi_value: true).tap do |cf|+ allow(CustomField)+ .to receive(:find_by)+ .with(id: cf.id)+ .and_return(cf)++ cf_options = double('custom_options')+ old_options = double('selected options')+ new_options = double('selected options')++ allow(cf)+ .to receive(:custom_options)+ .and_return cf_options++ allow(cf_options)+ .to receive(:where)+ .with(id: [1, 2])+ .and_return old_options++ allow(cf_options)+ .to receive(:where)+ .with(id: [3, 4])+ .and_return new_options++ allow(old_options)+ .to receive(:order)+ .with(:position)+ .and_return(old_options)++ allow(new_options)+ .to receive(:order)+ .with(:position)+ .and_return(new_options)++ allow(old_options)+ .to receive(:pluck)+ .with(:id, :value)+ .and_return(old_custom_option_names)++ allow(new_options)+ .to receive(:pluck)+ .with(:id, :value)+ .and_return(new_custom_option_names)+ end+ end+ let(:old_custom_option_names) { [[1, 'cf 1'], [2, 'cf 2']] }+ let(:new_custom_option_names) { [[3, 'cf 3'], [4, 'cf 4']] }- let(:expected) do- I18n.t(:text_journal_changed,- label: "<strong>#{custom_field.name}</strong>",- linebreak: '',- old: "<i title=\"cf 1, cf 2\">cf 1, cf 2</i>",- new: "<i title=\"cf 3, cf 4\">cf 3, cf 4</i>")- end+ describe "WITH the first value being a comma separated list of ids, and the second being a comma separated list of ids" do+ let(:values) { %w[1,2 3,4] }- it { expect(instance.render(key, values)).to eq(expected) }+ let(:expected) do+ I18n.t(:text_journal_changed,+ label: "<strong>#{custom_field.name}</strong>",+ linebreak: '',+ old: "<i title=\"cf 1, cf 2\">cf 1, cf 2</i>",+ new: "<i title=\"cf 3, cf 4\">cf 3, cf 4</i>")end- describe "WITH the first value being a comma separated list of ids, and the second being a comma separated list of ids that no longer exist" do- let(:values) { %w[1,2 3,4] }- let(:new_custom_option_names) { [[4, 'cf 4']] }+ it { expect(instance.render(key, values)).to eq(expected) }+ end- let(:expected) do- I18n.t(:text_journal_changed,- label: "<strong>#{custom_field.name}</strong>",- linebreak: '',- old: "<i title=\"cf 1, cf 2\">cf 1, cf 2</i>",- new: "<i title=\"(deleted option), cf 4\">(deleted option), cf 4</i>")- end+ describe "WITH the first value being a comma separated list of ids, and the second being a comma separated list of ids that no longer exist" do+ let(:values) { %w[1,2 3,4] }+ let(:new_custom_option_names) { [[4, 'cf 4']] }- it { expect(instance.render(key, values)).to eq(expected) }+ let(:expected) do+ I18n.t(:text_journal_changed,+ label: "<strong>#{custom_field.name}</strong>",+ linebreak: '',+ old: "<i title=\"cf 1, cf 2\">cf 1, cf 2</i>",+ new: "<i title=\"(deleted option), cf 4\">(deleted option), cf 4</i>")end++ it { expect(instance.render(key, values)).to eq(expected) }endendend