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

checking for correct handling of invisible fields in export specs as suggested by @dombesz

Jonas Jabari5415efbd9e2d
diff --git a/modules/xls_export/spec/models/xls_export/project/exporter/xls_integration_spec.rb b/modules/xls_export/spec/models/xls_export/project/exporter/xls_integration_spec.rb
index 094d91f51fd..21a2a620382 100644
--- a/modules/xls_export/spec/models/xls_export/project/exporter/xls_integration_spec.rb
+++ b/modules/xls_export/spec/models/xls_export/project/exporter/xls_integration_spec.rb
@@ -1,10 +1,10 @@
-require 'spec_helper'
-require 'spreadsheet'
-require 'models/projects/exporter/exportable_project_context'
+require "spec_helper"
+require "spreadsheet"
+require "models/projects/exporter/exportable_project_context"
RSpec.describe XlsExport::Project::Exporter::XLS do
- include_context 'with a project with an arrangement of custom fields'
- include_context 'with an instance of the described exporter'
+ include_context "with a project with an arrangement of custom fields"
+ include_context "with an instance of the described exporter"
let(:sheet) do
io = StringIO.new output
@@ -14,89 +14,126 @@ RSpec.describe XlsExport::Project::Exporter::XLS do
let(:header) { sheet.rows.first.compact } # raw values have trailing nil
let(:rows) { sheet.rows.drop(1) }
- describe 'empty result' do
+ describe "empty result" do
before do
allow(instance).to receive(:records).and_return([])
end
- it 'returns an empty XLS' do
+ it "returns an empty XLS" do
expect(sheet.rows.count).to eq 1
expect(rows).to be_empty
end
end
- it 'performs a successful export' do
+ it "performs a successful export" do
expect(rows.count).to eq(1)
expect(sheet.row(1)).to eq [project.id.to_s, project.identifier,
- project.name, project.description, 'Off track', 'false']
+ project.name, project.description, "Off track", "false"]
end
- context 'with project description containing html' do
+ context "with project description containing html" do
before do
project.update_column(:description, "This is an <p>html</p> description.")
end
- it 'performs a successful export' do
+ it "performs a successful export" do
expect(rows.count).to eq(1)
expect(sheet.row(1)).to eq [project.id.to_s, project.identifier, project.name,
- "This is an html description.", 'Off track', 'false']
+ "This is an html description.", "Off track", "false"]
end
end
- context 'with status_explanation enabled' do
- let(:query_columns) { %w[name description project_status status_explanation public ] }
+ context "with status_explanation enabled" do
+ let(:query_columns) { %w[name description project_status status_explanation public] }
- it 'performs a successful export' do
+ it "performs a successful export" do
expect(rows.count).to eq(1)
expect(sheet.row(1)).to eq [project.id.to_s, project.identifier,
project.name, project.description,
- 'Off track', project.status_explanation, 'false']
+ "Off track", project.status_explanation, "false"]
end
end
- describe 'custom field columns selected' do
+ describe "custom field columns selected" do
let(:query_columns) { %w[name description project_status public] + global_project_custom_fields.map(&:column_name) }
- let(:current_user) { build_stubbed(:admin) }
-
- context 'when ee enabled', with_ee: %i[custom_fields_in_projects_list] do
- it 'renders all those columns' do
- cf_names = global_project_custom_fields.map(&:name)
- expect(header).to eq ['ID', 'Identifier', 'Name', 'Description', 'Status', 'Public', *cf_names]
-
- custom_values = global_project_custom_fields.map do |cf|
- case cf
- when bool_cf
- 'true'
- when text_cf
- project.typed_custom_value_for(cf)
- when not_used_string_cf
- nil
- else
- project.formatted_custom_value_for(cf)
+
+ context "when ee enabled", with_ee: %i[custom_fields_in_projects_list] do
+ before do
+ project # re-evaluate project to ensure it is created within the desired user context
+ end
+
+ context "with admin permission" do
+ let(:current_user) { build_stubbed(:admin) }
+
+ it "renders all those columns" do
+ cf_names = global_project_custom_fields.map(&:name)
+ expect(header).to eq ["ID", "Identifier", "Name", "Description", "Status", "Public", *cf_names]
+
+ expect(header).to include not_used_string_cf.name
+ expect(header).to include hidden_cf.name
+
+ custom_values = global_project_custom_fields.map do |cf|
+ case cf
+ when bool_cf
+ "true"
+ when text_cf
+ project.typed_custom_value_for(cf)
+ when not_used_string_cf
+ nil
+ else
+ project.formatted_custom_value_for(cf)
+ end
end
+
+ expect(sheet.row(1))
+ .to eq [project.id.to_s, project.identifier, project.name, project.description, "Off track", "false",
+ *custom_values]
+
+ # The column for the project-level-disabled custom field is blank
+ expect(sheet.row(1)[header.index(not_used_string_cf.name)]).to be_nil
end
+ end
- expect(sheet.row(1))
- .to eq [project.id.to_s, project.identifier, project.name, project.description, 'Off track', 'false',
- *custom_values]
+ context "without admin permission" do
+ it "renders all visible globally available project custom fields in the header" do
+ cf_names = global_project_custom_fields.map(&:name)
+
+ expect(header).to eq ["ID", "Identifier", "Name", "Description", "Status", "Public", *cf_names]
+
+ expect(header).to include not_used_string_cf.name
+ expect(header).not_to include hidden_cf.name
+
+ custom_values = global_project_custom_fields.map do |cf|
+ case cf
+ when bool_cf
+ "true"
+ when text_cf
+ project.typed_custom_value_for(cf)
+ when not_used_string_cf
+ nil
+ else
+ project.formatted_custom_value_for(cf)
+ end
+ end
- # The column for the project-level-disabled custom field is blank
- expect(sheet.row(1)[header.index(not_used_string_cf.name)]).to be_nil
- # TODO: CSV export renders "" instead of nil, why does XLS export render nil?
+ expect(sheet.row(1))
+ .to eq [project.id.to_s, project.identifier, project.name, project.description, "Off track", "false",
+ *custom_values]
+ end
end
end
- context 'when ee not enabled' do
- it 'renders only the default columns' do
+ context "when ee not enabled" do
+ it "renders only the default columns" do
expect(header).to eq %w[ID Identifier Name Description Status Public]
end
end
end
- context 'with no project visible' do
+ context "with no project visible" do
let(:current_user) { User.anonymous }
- it 'does not include the project' do
+ it "does not include the project" do
expect(output).not_to include project.identifier
expect(rows).to be_empty
end
diff --git a/spec/models/projects/customizable_spec.rb b/spec/models/projects/customizable_spec.rb
index a215180f719..65e092bf670 100644
--- a/spec/models/projects/customizable_spec.rb
+++ b/spec/models/projects/customizable_spec.rb
@@ -26,8 +26,8 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-require 'spec_helper'
-RSpec.describe Project, 'customizable' do
+require "spec_helper"
+RSpec.describe Project, "customizable" do
let!(:section) { create(:project_custom_field_section) }
let!(:bool_custom_field) do
@@ -40,11 +40,11 @@ RSpec.describe Project, 'customizable' do
create(:list_project_custom_field, project_custom_field_section: section)
end
- context 'when not persisted' do
+ context "when not persisted" do
let(:project) { build(:project) }
- describe '#available_custom_fields' do
- it 'returns all existing project custom fields as available custom fields' do
+ describe "#available_custom_fields" do
+ it "returns all existing project custom fields as available custom fields" do
expect(project.project_custom_field_project_mappings)
.to be_empty
expect(project.project_custom_fields)
@@ -56,11 +56,11 @@ RSpec.describe Project, 'customizable' do
end
end
- context 'when persisted' do
+ context "when persisted" do
let(:project) { create(:project) }
- describe '#available_custom_fields' do
- it 'returns only mapped project custom fields as available custom fields' do
+ describe "#available_custom_fields" do
+ it "returns only mapped project custom fields as available custom fields" do
expect(project.project_custom_field_project_mappings)
.to be_empty
expect(project.project_custom_fields)
@@ -76,9 +76,9 @@ RSpec.describe Project, 'customizable' do
end
end
- describe '#custom_field_values and #custom_value_for' do
- context 'when no custom fields are mapped to this project' do
- it '#custom_value_for returns nil' do
+ describe "#custom_field_values and #custom_value_for" do
+ context "when no custom fields are mapped to this project" do
+ it "#custom_value_for returns nil" do
expect(project.custom_value_for(text_custom_field))
.to be_nil
expect(project.custom_value_for(bool_custom_field))
@@ -87,19 +87,19 @@ RSpec.describe Project, 'customizable' do
.to be_nil
end
- it '#custom_field_values returns an empty hash' do
+ it "#custom_field_values returns an empty hash" do
expect(project.custom_field_values)
.to be_empty
end
end
- context 'when custom fields are mapped to this project' do
+ context "when custom fields are mapped to this project" do
before do
project.project_custom_fields << [text_custom_field, bool_custom_field]
project.reload # TODO: why is this necessary?
end
- it '#custom_field_values returns a hash of mapped custom fields with nil values' do
+ it "#custom_field_values returns a hash of mapped custom fields with nil values" do
text_custom_field_custom_field_value = project.custom_field_values.find do |custom_value|
custom_value.custom_field_id == text_custom_field.id
end
@@ -115,28 +115,28 @@ RSpec.describe Project, 'customizable' do
expect(bool_custom_field_custom_field_value.value).to be_nil
end
- context 'when values are set for mapped custom fields' do
+ context "when values are set for mapped custom fields" do
before do
project.custom_field_values = {
- text_custom_field.id => 'foo',
+ text_custom_field.id => "foo",
bool_custom_field.id => true
}
end
- it '#custom_value_for returns the set custom values' do
+ it "#custom_value_for returns the set custom values" do
expect(project.custom_value_for(text_custom_field).typed_value)
- .to eq('foo')
+ .to eq("foo")
expect(project.custom_value_for(bool_custom_field).typed_value)
.to be_truthy
expect(project.custom_value_for(list_custom_field).typed_value)
.to be_nil
end
- it '#custom_field_values returns a hash of mapped custom fields with their set values' do
+ it "#custom_field_values returns a hash of mapped custom fields with their set values" do
expect(project.custom_field_values.find do |custom_value|
custom_value.custom_field_id == text_custom_field.id
end.typed_value)
- .to eq('foo')
+ .to eq("foo")
expect(project.custom_field_values.find do |custom_value|
custom_value.custom_field_id == bool_custom_field.id
@@ -148,22 +148,22 @@ RSpec.describe Project, 'customizable' do
end
end
- context 'when creating with custom field values' do
+ context "when creating with custom field values" do
let(:project) do
create(:project, custom_field_values: {
- text_custom_field.id => 'foo',
+ text_custom_field.id => "foo",
bool_custom_field.id => true
})
end
- it 'saves the custom field values properly' do
+ it "saves the custom field values properly" do
expect(project.custom_value_for(text_custom_field).typed_value)
- .to eq('foo')
+ .to eq("foo")
expect(project.custom_value_for(bool_custom_field).typed_value)
.to be_truthy
end
- it 'enables fields with provided values and disables fields with none' do
+ it "enables fields with provided values and disables fields with none" do
# list_custom_field is not provided, thus it should not be enabled
expect(project.project_custom_field_project_mappings.pluck(:custom_field_id))
.to contain_exactly(text_custom_field.id, bool_custom_field.id)
@@ -171,7 +171,7 @@ RSpec.describe Project, 'customizable' do
.to contain_exactly(text_custom_field, bool_custom_field)
end
- context 'with correct validation' do
+ context "with correct validation" do
let(:another_section) { create(:project_custom_field_section) }
let!(:required_text_custom_field) do
@@ -180,9 +180,9 @@ RSpec.describe Project, 'customizable' do
project_custom_field_section: another_section)
end
- it 'validates all custom values if not scoped to a section' do
+ it "validates all custom values if not scoped to a section" do
project = build(:project, custom_field_values: {
- text_custom_field.id => 'foo',
+ text_custom_field.id => "foo",
bool_custom_field.id => true
})
@@ -191,9 +191,9 @@ RSpec.describe Project, 'customizable' do
expect { project.save! }.to raise_error(ActiveRecord::RecordInvalid)
end
- it 'rejects section validation scoping for project creation' do
+ it "rejects section validation scoping for project creation" do
project = build(:project, custom_field_values: {
- text_custom_field.id => 'foo',
+ text_custom_field.id => "foo",
bool_custom_field.id => true
},
_limit_custom_fields_validation_to_section_id: section.id)
@@ -201,11 +201,11 @@ RSpec.describe Project, 'customizable' do
expect { project.save! }.to raise_error(ArgumentError)
end
- it 'temporarly validates only custom values of a section if section scope is provided while updating' do
+ it "temporarly validates only custom values of a section if section scope is provided while updating" do
project = create(:project, custom_field_values: {
- text_custom_field.id => 'foo',
+ text_custom_field.id => "foo",
bool_custom_field.id => true,
- required_text_custom_field.id => 'bar'
+ required_text_custom_field.id => "bar"
})
expect(project).to be_valid
@@ -233,27 +233,27 @@ RSpec.describe Project, 'customizable' do
end
end
- context 'with correct handling of custom fields with default values' do
+ context "with correct handling of custom fields with default values" do
let!(:text_custom_field_with_default) do
create(:text_project_custom_field,
- default_value: 'default',
+ default_value: "default",
project_custom_field_section: section)
end
- it 'activates custom fields with default values if not explicitly set to blank' do
+ it "activates custom fields with default values if not explicitly set to blank" do
project = create(:project, custom_field_values: {
- text_custom_field.id => 'foo',
+ text_custom_field.id => "foo",
bool_custom_field.id => true
})
expect(project.project_custom_field_project_mappings.pluck(:custom_field_id))
.to contain_exactly(text_custom_field.id, bool_custom_field.id, text_custom_field_with_default.id)
end
- it 'does not activate custom fields with default values if explicitly set to blank' do
+ it "does not activate custom fields with default values if explicitly set to blank" do
project = create(:project, custom_field_values: {
- text_custom_field.id => 'foo',
+ text_custom_field.id => "foo",
bool_custom_field.id => true,
- text_custom_field_with_default.id => ''
+ text_custom_field_with_default.id => ""
})
expect(project.project_custom_field_project_mappings.pluck(:custom_field_id))
.to contain_exactly(text_custom_field.id, bool_custom_field.id)
@@ -261,11 +261,11 @@ RSpec.describe Project, 'customizable' do
end
end
- context 'when updating with custom field values' do
+ context "when updating with custom field values" do
let!(:project) { create(:project) }
- shared_examples 'implicitly enabled and saved custom values' do
- it 'enables fields with provided values' do
+ shared_examples "implicitly enabled and saved custom values" do
+ it "enables fields with provided values" do
# list_custom_field is not provided, thus it should not be enabled
expect(project.project_custom_field_project_mappings.pluck(:custom_field_id))
.to contain_exactly(text_custom_field.id, bool_custom_field.id)
@@ -273,52 +273,52 @@ RSpec.describe Project, 'customizable' do
.to contain_exactly(text_custom_field, bool_custom_field)
end
- it 'saves the custom field values properly' do
+ it "saves the custom field values properly" do
expect(project.custom_value_for(text_custom_field).typed_value)
- .to eq('foo')
+ .to eq("foo")
expect(project.custom_value_for(bool_custom_field).typed_value)
.to be_truthy
end
end
- context 'with #update method' do
+ context "with #update method" do
before do
project.update(custom_field_values: {
- text_custom_field.id => 'foo',
+ text_custom_field.id => "foo",
bool_custom_field.id => true
})
end
- it_behaves_like 'implicitly enabled and saved custom values'
+ it_behaves_like "implicitly enabled and saved custom values"
end
- context 'with #update! method' do
+ context "with #update! method" do
before do
project.update!(custom_field_values: {
- text_custom_field.id => 'foo',
+ text_custom_field.id => "foo",
bool_custom_field.id => true
})
end
- it_behaves_like 'implicitly enabled and saved custom values'
+ it_behaves_like "implicitly enabled and saved custom values"
end
- context 'with #custom_field_values= method' do
+ context "with #custom_field_values= method" do
before do
project.custom_field_values = {
- text_custom_field.id => 'foo',
+ text_custom_field.id => "foo",
bool_custom_field.id => true
}
project.save!
end
- it_behaves_like 'implicitly enabled and saved custom values'
+ it_behaves_like "implicitly enabled and saved custom values"
end
- it 'does not re-enable fields without new value which have been disabled in the past (regression)' do
+ it "does not re-enable fields without new value which have been disabled in the past (regression)" do
project.update!(custom_field_values: {
- text_custom_field.id => 'foo',
+ text_custom_field.id => "foo",
bool_custom_field.id => true
})
@@ -338,16 +338,16 @@ RSpec.describe Project, 'customizable' do
.to contain_exactly(bool_custom_field)
end
- context 'with correct handling of custom fields with default values' do
+ context "with correct handling of custom fields with default values" do
let!(:text_custom_field_with_default) do
create(:text_project_custom_field,
- default_value: 'default',
+ default_value: "default",
project_custom_field_section: section)
end
- it 'does not activate custom fields with default values if not explicitly set to a value' do
+ it "does not activate custom fields with default values if not explicitly set to a value" do
project.update!(custom_field_values: {
- text_custom_field.id => 'bar',
+ text_custom_field.id => "bar",
bool_custom_field.id => false
})
@@ -356,11 +356,11 @@ RSpec.describe Project, 'customizable' do
.to contain_exactly(text_custom_field.id, bool_custom_field.id)
end
- it 'does activate custom fields with default values if explicitly set to a value' do
+ it "does activate custom fields with default values if explicitly set to a value" do
project.update!(custom_field_values: {
- text_custom_field.id => 'bar',
+ text_custom_field.id => "bar",
bool_custom_field.id => false,
- text_custom_field_with_default.id => 'overwritten default'
+ text_custom_field_with_default.id => "overwritten default"
})
# text_custom_field_with_default is not provided, thus it should not be enabled (in contrast to creation)
@@ -369,9 +369,9 @@ RSpec.describe Project, 'customizable' do
end
end
- it 'does re-enable fields with new value which have been disabled in the past' do
+ it "does re-enable fields with new value which have been disabled in the past" do
project.update!(custom_field_values: {
- text_custom_field.id => 'foo',
+ text_custom_field.id => "foo",
bool_custom_field.id => true
})
@@ -384,22 +384,22 @@ RSpec.describe Project, 'customizable' do
.to contain_exactly(bool_custom_field)
project.update!(custom_field_values: {
- text_custom_field.id => 'bar'
+ text_custom_field.id => "bar"
})
expect(project.reload.project_custom_fields)
.to contain_exactly(text_custom_field, bool_custom_field)
expect(project.custom_value_for(text_custom_field).typed_value)
- .to eq('bar')
+ .to eq("bar")
end
end
- context 'when updating with custom field setter methods (API approach)' do
+ context "when updating with custom field setter methods (API approach)" do
let(:project) { create(:project) }
- shared_examples 'implicitly enabled and saved custom values' do
- it 'enables fields with provided values' do
+ shared_examples "implicitly enabled and saved custom values" do
+ it "enables fields with provided values" do
# list_custom_field is not provided, thus it should not be enabled
expect(project.project_custom_field_project_mappings.pluck(:custom_field_id))
.to contain_exactly(text_custom_field.id, bool_custom_field.id)
@@ -407,27 +407,68 @@ RSpec.describe Project, 'customizable' do
.to contain_exactly(text_custom_field, bool_custom_field)
end
- it 'saves the custom field values properly' do
+ it "saves the custom field values properly" do
expect(project.custom_value_for(text_custom_field).typed_value)
- .to eq('foo')
+ .to eq("foo")
expect(project.custom_value_for(bool_custom_field).typed_value)
.to be_truthy
# or via getter methods:
- expect(project.send(:"custom_field_#{text_custom_field.id}")).to eq('foo')
+ expect(project.send(:"custom_field_#{text_custom_field.id}")).to eq("foo")
expect(project.send(:"custom_field_#{bool_custom_field.id}")).to be_truthy
end
end
- context 'when setting a value for a disabled custom field' do
+ context "when setting a value for a disabled custom field" do
before do
- project.send(:"custom_field_#{text_custom_field.id}=", 'foo')
+ project.send(:"custom_field_#{text_custom_field.id}=", "foo")
project.send(:"custom_field_#{bool_custom_field.id}=", true)
project.save!
end
- it_behaves_like 'implicitly enabled and saved custom values'
+ it_behaves_like "implicitly enabled and saved custom values"
+ end
+ end
+
+ context "with hidden custom fields" do
+ let!(:hidden_custom_field) do
+ create(:text_project_custom_field, project_custom_field_section: section, visible: false)
+ end
+ let(:project) do
+ create(:project, custom_field_values: {
+ text_custom_field.id => "foo",
+ bool_custom_field.id => true,
+ hidden_custom_field.id => "hidden"
+ })
+ end
+
+ before do
+ User.current = user # needs to be executed before project creation!
+ end
+
+ context "with admin permission" do
+ let(:user) { create(:admin) }
+
+ it "does activate hidden custom fields" do
+ # project creation happens with an admin user as let(:project) called after setting the current user to an admin
+ expect(project.project_custom_field_project_mappings.pluck(:custom_field_id))
+ .to contain_exactly(text_custom_field.id, bool_custom_field.id, hidden_custom_field.id)
+
+ expect(project.custom_value_for(hidden_custom_field)).to eq("hidden")
+ end
+ end
+
+ context "without admin permission" do
+ let(:user) { create(:user) }
+
+ it "does not activate hidden custom fields" do
+ # project creation happens with an non-admin user as let(:project) called after setting the current user to an non-admin
+ expect(project.project_custom_field_project_mappings.pluck(:custom_field_id))
+ .to contain_exactly(text_custom_field.id, bool_custom_field.id)
+
+ expect(project.custom_value_for(hidden_custom_field)).to be_nil
+ end
end
end
end
diff --git a/spec/models/projects/exporter/csv_integration_spec.rb b/spec/models/projects/exporter/csv_integration_spec.rb
index 3de1c4e0a00..464e77efa57 100644
--- a/spec/models/projects/exporter/csv_integration_spec.rb
+++ b/spec/models/projects/exporter/csv_integration_spec.rb
@@ -26,12 +26,12 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-require 'spec_helper'
-require_relative 'exportable_project_context'
+require "spec_helper"
+require_relative "exportable_project_context"
-RSpec.describe Projects::Exports::CSV, 'integration' do
- include_context 'with a project with an arrangement of custom fields'
- include_context 'with an instance of the described exporter'
+RSpec.describe Projects::Exports::CSV, "integration" do
+ include_context "with a project with an arrangement of custom fields"
+ include_context "with an instance of the described exporter"
let(:parsed) do
CSV.parse(output)
@@ -41,74 +41,112 @@ RSpec.describe Projects::Exports::CSV, 'integration' do
let(:rows) { parsed.drop(1) }
- it 'performs a successful export' do
+ it "performs a successful export" do
expect(parsed.size).to eq(2)
expect(parsed.last).to eq [project.id.to_s, project.identifier,
- project.name, project.description, 'Off track', 'false']
+ project.name, project.description, "Off track", "false"]
end
- context 'with status_explanation enabled' do
+ context "with status_explanation enabled" do
let(:query_columns) { %w[name description project_status status_explanation public] }
- it 'performs a successful export' do
+ it "performs a successful export" do
expect(parsed.size).to eq(2)
expect(parsed.last).to eq [project.id.to_s, project.identifier,
project.name, project.description,
- 'Off track', 'some explanation', 'false']
+ "Off track", "some explanation", "false"]
end
end
- describe 'custom field columns selected' do
+ describe "custom field columns selected" do
let(:query_columns) do
%w[name description project_status public] + global_project_custom_fields.map(&:column_name)
end
- context 'when ee enabled', with_ee: %i[custom_fields_in_projects_list] do
- it 'renders all globally available project custom fields in the header' do
- expect(parsed.size).to eq 2
+ context "when ee enabled", with_ee: %i[custom_fields_in_projects_list] do
+ before do
+ project # re-evaluate project to ensure it is created within the desired user context
+ parsed
+ end
- cf_names = global_project_custom_fields.map(&:name)
+ context "without admin permission" do
+ it "renders all visible globally available project custom fields in the header" do
+ expect(parsed.size).to eq 2
- expect(cf_names).to include(not_used_string_cf.name)
+ cf_names = global_project_custom_fields.map(&:name)
- expect(header).to eq ['id', 'Identifier', 'Name', 'Description', 'Status', 'Public', *cf_names]
- end
+ expect(cf_names).to include(not_used_string_cf.name)
+ expect(cf_names).not_to include(hidden_cf.name)
+
+ expect(header).to eq ["id", "Identifier", "Name", "Description", "Status", "Public", *cf_names]
+ end
- it 'renders the custom field values in the rows if enabled for a project' do
- expect(parsed.size).to eq 2
-
- custom_values = global_project_custom_fields.map do |cf|
- case cf
- when bool_cf
- 'true'
- when text_cf
- project.typed_custom_value_for(cf)
- when not_used_string_cf
- ''
- else
- project.formatted_custom_value_for(cf)
+ it "renders the custom field values in the rows if enabled for a project" do
+ custom_values = global_project_custom_fields.map do |cf|
+ case cf
+ when bool_cf
+ "true"
+ when text_cf
+ project.typed_custom_value_for(cf)
+ when not_used_string_cf
+ ""
+ else
+ project.formatted_custom_value_for(cf)
+ end
end
+ expect(rows.first)
+ .to eq [project.id.to_s, project.identifier, project.name,
+ project.description, "Off track", "false", *custom_values]
end
- expect(rows.first)
- .to eq [project.id.to_s, project.identifier, project.name,
- project.description, 'Off track', 'false', *custom_values]
+ end
+
+ context "with admin permission" do
+ let(:current_user) { create(:admin) }
+
+ it "renders all globally available project custom fields including hidden ones in the header" do
+ expect(parsed.size).to eq 3
- # The column for the project-level-disabled custom field is blank
- expect(rows.first[header.index(not_used_string_cf.name)]).to eq ''
+ cf_names = global_project_custom_fields.map(&:name)
+
+ expect(cf_names).to include(not_used_string_cf.name)
+ expect(cf_names).to include(hidden_cf.name)
+
+ expect(header).to eq ["id", "Identifier", "Name", "Description", "Status", "Public", *cf_names]
+ end
+
+ it "renders the custom field values in the rows if enabled for a project" do
+ custom_values = global_project_custom_fields.map do |cf|
+ case cf
+ when bool_cf
+ "true"
+ when hidden_cf
+ "hidden"
+ when not_used_string_cf
+ ""
+ when text_cf
+ project.typed_custom_value_for(cf)
+ else
+ project.formatted_custom_value_for(cf)
+ end
+ end
+ expect(rows.first)
+ .to eq [project.id.to_s, project.identifier, project.name,
+ project.description, "Off track", "false", *custom_values]
+ end
end
end
- context 'when ee not enabled' do
- it 'renders only the default columns' do
+ context "when ee not enabled" do
+ it "renders only the default columns" do
expect(header).to eq %w[id Identifier Name Description Status Public]
end
end
end
- context 'with no project visible' do
+ context "with no project visible" do
let(:current_user) { User.anonymous }
- it 'does not include the project' do
+ it "does not include the project" do
expect(output).not_to include project.identifier
expect(parsed.size).to eq(1)
end
diff --git a/spec/models/projects/exporter/exportable_project_context.rb b/spec/models/projects/exporter/exportable_project_context.rb
index 512155930f9..77960e54256 100644
--- a/spec/models/projects/exporter/exportable_project_context.rb
+++ b/spec/models/projects/exporter/exportable_project_context.rb
@@ -26,7 +26,7 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-RSpec.shared_context 'with a project with an arrangement of custom fields' do
+RSpec.shared_context "with a project with an arrangement of custom fields" do
shared_let(:version_cf) { create(:version_project_custom_field, position: 1) }
shared_let(:bool_cf) { create(:boolean_project_custom_field, position: 2) }
shared_let(:user_cf) { create(:user_project_custom_field, position: 3) }
@@ -35,10 +35,11 @@ RSpec.shared_context 'with a project with an arrangement of custom fields' do
shared_let(:text_cf) { create(:text_project_custom_field, position: 6) }
shared_let(:string_cf) { create(:string_project_custom_field, position: 7) }
shared_let(:date_cf) { create(:date_project_custom_field, position: 8) }
+ shared_let(:hidden_cf) { create(:string_project_custom_field, position: 9, visible: false) }
- let!(:not_used_string_cf) { create(:string_project_custom_field, position: 9) }
+ let!(:not_used_string_cf) { create(:string_project_custom_field, position: 10) }
- shared_let(:system_version) { create(:version, sharing: 'system') }
+ shared_let(:system_version) { create(:version, sharing: "system") }
shared_let(:role) do
create(:project_role)
@@ -46,14 +47,16 @@ RSpec.shared_context 'with a project with an arrangement of custom fields' do
shared_let(:other_user) do
create(:user,
- firstname: 'Other',
- lastname: 'User')
+ firstname: "Other",
+ lastname: "User")
end
- shared_let(:project) do
+ # project needs to be reevaluted before every example as the creation behaves differently from different user contexts
+ # shared_let cannot be used here as it would create the project only once
+ let(:project) do
project = build(:project,
- status_code: 'off_track',
- status_explanation: 'some explanation',
+ status_code: "off_track",
+ status_explanation: "some explanation",
members: { other_user => role },
description: "The description of the project",
custom_field_values: {
@@ -61,10 +64,11 @@ RSpec.shared_context 'with a project with an arrangement of custom fields' do
bool_cf.id => true,
version_cf.id => system_version,
float_cf.id => 4.5,
- text_cf.id => 'Some **long** text',
- string_cf.id => 'Some small text',
+ text_cf.id => "Some **long** text",
+ string_cf.id => "Some small text",
date_cf.id => Time.zone.today,
- user_cf.id => other_user.id
+ user_cf.id => other_user.id,
+ hidden_cf.id => "hidden"
})
project.save!(validate: false)
@@ -72,7 +76,7 @@ RSpec.shared_context 'with a project with an arrangement of custom fields' do
end
end
-RSpec.shared_context 'with an instance of the described exporter' do
+RSpec.shared_context "with an instance of the described exporter" do
before do
login_as current_user
end
@@ -89,7 +93,7 @@ RSpec.shared_context 'with an instance of the described exporter' do
described_class.new(query)
end
- let(:global_project_custom_fields) { ProjectCustomField.all }
+ let(:global_project_custom_fields) { ProjectCustomField.visible }
let(:custom_fields_of_project) { project.available_custom_fields }
let(:output) do