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

cleanup custom field spec

Ivan Kuchinb969d7072a30
diff --git a/spec/lib/journal_formatter/custom_field_spec.rb b/spec/lib/journal_formatter/custom_field_spec.rb
index 32f6c7b422c..42f9ebeb209 100644
--- a/spec/lib/journal_formatter/custom_field_spec.rb
+++ b/spec/lib/journal_formatter/custom_field_spec.rb
@@ -32,267 +32,251 @@ RSpec.describe OpenProject::JournalFormatter::CustomField do
include CustomFieldsHelper
include ActionView::Helpers::TagHelper
- let(:klass) { described_class }
- let(:instance) { klass.new(journal) }
+ let(:instance) { described_class.new(journal) }
let(:id) { 1 }
- let(:journal) do
- OpenStruct.new(id:)
- end
- let(:custom_field) do
- build_stubbed(:work_package_custom_field).tap do |cf|
- allow(CustomField)
- .to receive(:find_by)
- .with(id: cf.id)
- .and_return(cf)
- end
- end
-
- let(:key) do
- "custom_fields_#{custom_field.id}"
+ let(:journal) { instance_double(Journal, id:) }
+ let(:key) { "custom_fields_#{custom_field.id}" }
+ let(:options) { {} }
+ let(:custom_field) { build_stubbed(:work_package_custom_field) }
+
+ subject(:rendered) { instance.render(key, values, **options) }
+
+ before do
+ allow(CustomField).to receive(:find_by).and_return(nil)
+ allow(CustomField)
+ .to receive(:find_by)
+ .with(id: custom_field.id)
+ .and_return(custom_field)
end
- describe "a multi-select user field" do
- let(:user1) { build_stubbed(:user, firstname: "Foo", lastname: "Bar") }
- let(:user2) { build_stubbed(:user, firstname: "Bla", lastname: "Blub") }
+ context "with html requested by default" 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(:custom_field) do
- build_stubbed(:user_wp_custom_field).tap do |cf|
- allow(CustomField)
- .to receive(:find_by)
- .with(id: cf.id)
- .and_return(cf)
+ let(:expected) do
+ I18n.t(:text_journal_set_to,
+ label: "<strong>#{custom_field.name}</strong>",
+ value: "<i>#{formatted_value}</i>")
end
- end
- let(:wherestub) { class_double(Principal) }
- let(:values) { [nil, "#{user1.id},#{user2.id}"] }
+ it { expect(rendered).to eq(expected) }
+ end
- subject(:rendered) { instance.render(key, values) }
+ describe "with both values being valid values as strings" 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) }
- before do
- allow(Principal)
- .to receive(:in_visible_project_or_me).and_return(wherestub)
+ let(:expected) do
+ I18n.t(:text_journal_changed_plain,
+ label: "<strong>#{custom_field.name}</strong>",
+ linebreak: "",
+ old: "<i>#{old_formatted_value}</i>",
+ new: "<i>#{new_formatted_value}</i>")
+ end
- allow(wherestub)
- .to receive(:where)
- .with(id: [user1.id, user2.id])
- .and_return(visible_users)
+ it { expect(rendered).to eq(expected) }
end
- context "with two visible users" do
- let(:visible_users) { [user1, user2] }
+ 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) }
- let(:formatted_value) do
- "Foo Bar, Bla Blub"
- end
let(:expected) do
- I18n.t(:text_journal_set_to,
+ I18n.t(:text_journal_deleted,
label: "<strong>#{custom_field.name}</strong>",
- value: "<i>#{formatted_value}</i>")
- end
-
- it "outputs both formatted names" do
- expect(rendered).to eq expected
+ old: "<strike><i>#{formatted_value}</i></strike>")
end
- context "with only one visible user" do
- let(:visible_users) { [user1] }
-
- 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>#{formatted_value}</i>")
- end
-
- it "outputs the one visible formatted name" do
- expect(rendered).to eq expected
- end
- end
+ it { expect(rendered).to eq(expected) }
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) }
+ context "with non html requested" do
+ let(:options) { { html: false } }
- let(:expected) do
- I18n.t(:text_journal_set_to,
- label: "<strong>#{custom_field.name}</strong>",
- value: "<i>#{formatted_value}</i>")
- end
+ describe "with the first value being nil, and the second a valid value as string" do
+ let(:values) { [nil, "1"] }
- it { expect(instance.render(key, values)).to eq(expected) }
- end
+ let(:expected) do
+ I18n.t(:text_journal_set_to,
+ label: custom_field.name,
+ value: 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_plain,
- label: "<strong>#{custom_field.name}</strong>",
- linebreak: "",
- old: "<i>#{old_formatted_value}</i>",
- new: "<i>#{new_formatted_value}</i>")
+ it { expect(rendered).to eq(expected) }
end
- it { expect(instance.render(key, values)).to eq(expected) }
- end
+ describe "with both values being valid values as strings" do
+ let(:values) { %w[0 1] }
- 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) }
+ 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
- let(:expected) do
- I18n.t(:text_journal_deleted,
- label: "<strong>#{custom_field.name}</strong>",
- old: "<strike><i>#{formatted_value}</i></strike>")
+ it { expect(rendered).to eq(expected) }
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] }
- describe "WITH the first value being nil, and the second a valid value as string " \
- "and WITH no html requested" do
- 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))
+ it { expect(rendered).to eq(expected) }
end
-
- it { expect(instance.render(key, values, html: false)).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 " \
- "and WITH no html requested" do
- let(:values) { %w[0 1] }
+ context "with the custom field being deleted" do
+ let(:key) { "custom_fields_#{custom_field.id + 1}" }
+
+ describe "with the first value being nil, and the second a valid value as string" do
+ let(:values) { [nil, "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))
+ let(:expected) do
+ I18n.t(:text_journal_set_to,
+ label: "<strong>#{I18n.t(:label_deleted_custom_field)}</strong>",
+ value: "<i>#{values.last}</i>")
+ end
+
+ it { expect(rendered).to eq(expected) }
end
- it { expect(instance.render(key, values, html: false)).to eq(expected) }
- end
+ describe "with both values being valid values as strings" do
+ let(:values) { %w[0 1] }
- describe "WITH the first value being a valid value as a string, and the second being nil " \
- "and WITH no html requested" do
- let(:values) { ["0", nil] }
+ let(:expected) do
+ I18n.t(:text_journal_changed_plain,
+ label: "<strong>#{I18n.t(:label_deleted_custom_field)}</strong>",
+ linebreak: "",
+ old: "<i>#{values.first}</i>",
+ new: "<i>#{values.last}</i>")
+ end
- let(:expected) do
- I18n.t(:text_journal_deleted,
- label: custom_field.name,
- old: format_value(values.first, custom_field))
+ it { expect(rendered).to eq(expected) }
end
- it { expect(instance.render(key, values, html: false)).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] }
- describe "WITH the first value being nil, and the second a valid value as string " \
- "and WITH the custom field being deleted" do
- let(:values) { [nil, "1"] }
- let(:key) { "custom_values0" }
+ let(:expected) do
+ I18n.t(:text_journal_deleted,
+ label: "<strong>#{I18n.t(:label_deleted_custom_field)}</strong>",
+ old: "<strike><i>#{values.first}</i></strike>")
+ end
- let(:expected) do
- I18n.t(:text_journal_set_to,
- label: "<strong>#{I18n.t(:label_deleted_custom_field)}</strong>",
- value: "<i>#{values.last}</i>")
+ it { expect(rendered).to eq(expected) }
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 a valid value as a string " \
- "and WITH the custom field being deleted" do
- let(:values) { %w[0 1] }
- let(:key) { "custom_values0" }
-
- let(:expected) do
- I18n.t(:text_journal_changed_plain,
- label: "<strong>#{I18n.t(:label_deleted_custom_field)}</strong>",
- linebreak: "",
- old: "<i>#{values.first}</i>",
- new: "<i>#{values.last}</i>")
+ context "for a multi-select custom field" do
+ let(:custom_field) { build_stubbed(:user_wp_custom_field) }
+
+ let(:user1) { build_stubbed(:user, firstname: "Foo", lastname: "Bar") }
+ let(:user2) { build_stubbed(:user, firstname: "Bla", lastname: "Blub") }
+ let(:wherestub) { class_double(Principal) }
+ let(:values) { [nil, "#{user1.id},#{user2.id}"] }
+
+ 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
- it { expect(instance.render(key, values)).to eq(expected) }
- end
+ describe "with two visible users" do
+ let(:visible_users) { [user1, user2] }
- describe "WITH the first value being a valid value as a string, and the second being nil " \
- "and WITH the custom field being deleted" do
- let(:values) { ["0", nil] }
- let(:key) { "custom_values0" }
+ let(:formatted_value) do
+ "Foo Bar, Bla Blub"
+ end
+ let(:expected) do
+ I18n.t(:text_journal_set_to,
+ label: "<strong>#{custom_field.name}</strong>",
+ value: "<i>#{formatted_value}</i>")
+ end
- let(:expected) do
- I18n.t(:text_journal_deleted,
- label: "<strong>#{I18n.t(:label_deleted_custom_field)}</strong>",
- old: "<strike><i>#{values.first}</i></strike>")
+ it "outputs both formatted names" do
+ expect(rendered).to eq expected
+ end
end
- it { expect(instance.render(key, values)).to eq(expected) }
- end
+ describe "with only one visible user" do
+ let(:visible_users) { [user1] }
+
+ 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>#{formatted_value}</i>")
+ end
- context "for a multi list cf" do
- let(:custom_field) do
- 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)
+ it "outputs the one visible formatted name" do
+ expect(rendered).to eq expected
end
end
+ end
+
+ context "for a multi list custom field" do
+ let(:custom_field) { build_stubbed(:list_wp_custom_field, multi_value: true) }
+
let(:old_custom_option_names) { [[1, "cf 1"], [2, "cf 2"]] }
let(:new_custom_option_names) { [[3, "cf 3"], [4, "cf 4"]] }
- describe "WITH the first value being a comma separated list of ids, " \
- "and the second being a comma separated list of ids" do
+ before do
+ cf_options = instance_double(ActiveRecord::Associations::CollectionProxy)
+ old_options = instance_double(ActiveRecord::AssociationRelation)
+ new_options = instance_double(ActiveRecord::AssociationRelation)
+
+ allow(custom_field)
+ .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
+
+ describe "with both values being a comma separated list of ids" do
let(:values) { %w[1,2 3,4] }
let(:expected) do
@@ -303,11 +287,10 @@ RSpec.describe OpenProject::JournalFormatter::CustomField do
new: "<i>cf 3, cf 4</i>")
end
- it { expect(instance.render(key, values)).to eq(expected) }
+ it { expect(rendered).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 that no longer exist" do
+ describe "with both values being a comma separated list of ids, and second being ids that no longer exist" do
let(:values) { %w[1,2 3,4] }
let(:new_custom_option_names) { [[4, "cf 4"]] }
@@ -319,7 +302,7 @@ RSpec.describe OpenProject::JournalFormatter::CustomField do
new: "<i>(deleted option), cf 4</i>")
end
- it { expect(instance.render(key, values)).to eq(expected) }
+ it { expect(rendered).to eq(expected) }
end
end
end