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

added project creation feature specs around project custom fields and their mapping

Jonas Jabari2e54aa00eda8
diff --git a/app/models/projects/acts_as_customizable_patches.rb b/app/models/projects/acts_as_customizable_patches.rb
index e4ebb4fa890..c630fd8fc33 100644
--- a/app/models/projects/acts_as_customizable_patches.rb
+++ b/app/models/projects/acts_as_customizable_patches.rb
@@ -37,6 +37,7 @@ module Projects::ActsAsCustomizablePatches
included do
has_many :project_custom_field_project_mappings, class_name: 'ProjectCustomFieldProjectMapping', foreign_key: :project_id,
dependent: :destroy, inverse_of: :project
+ has_many :project_custom_fields, through: :project_custom_field_project_mappings, class_name: 'ProjectCustomField'
before_save :build_missing_project_custom_field_project_mappings
after_create :disable_custom_fields_with_empty_values
diff --git a/spec/features/projects/create_spec.rb b/spec/features/projects/create_spec.rb
index dea5ad1e81f..0993bbc731a 100644
--- a/spec/features/projects/create_spec.rb
+++ b/spec/features/projects/create_spec.rb
@@ -111,11 +111,13 @@ RSpec.describe 'Projects', 'creation',
context 'with optional and required custom fields' do
let!(:optional_custom_field) do
create(:custom_field, name: 'Optional Foo',
+ field_format: 'string',
type: ProjectCustomField,
is_for_all: true)
end
let!(:required_custom_field) do
create(:custom_field, name: 'Required Foo',
+ field_format: 'string',
type: ProjectCustomField,
is_for_all: true,
is_required: true)
@@ -133,5 +135,106 @@ RSpec.describe 'Projects', 'creation',
expect(page).to have_no_text 'Required Foo'
end
end
+
+ context 'with correct custom field activation' do
+ let!(:unused_custom_field) do
+ create(:custom_field, name: 'Unused Foo',
+ field_format: 'string',
+ type: ProjectCustomField,
+ is_for_all: true)
+ end
+
+ before do
+ visit new_project_path
+ fill_in 'Name', with: 'Foo bar'
+ fill_in 'Required Foo', with: 'Required value'
+
+ click_on 'Advanced settings'
+ end
+
+ it 'enables custom fields with provided values for this project' do
+ fill_in 'Optional Foo', with: 'Optional value'
+ fill_in 'Unused Foo', with: ''
+
+ click_on 'Save'
+
+ expect(page).to have_current_path /\/projects\/foo-bar\/?/
+
+ project = Project.last
+
+ # unused custom field should not be activated
+ expect(project.project_custom_field_ids).to contain_exactly(
+ optional_custom_field.id, required_custom_field.id
+ )
+ end
+
+ context 'with correct handling of default values' do
+ let!(:custom_field_with_default_value) do
+ create(:custom_field, name: 'Foo with default value',
+ field_format: 'string',
+ default_value: 'Default value',
+ type: ProjectCustomField,
+ is_for_all: true)
+ end
+
+ before do
+ visit new_project_path
+ fill_in 'Name', with: 'Foo bar'
+ fill_in 'Required Foo', with: 'Required value'
+
+ click_on 'Advanced settings'
+ end
+
+ it 'enables custom fields with default values if not set to blank explicitly' do
+ # don't touch the default value
+ click_on 'Save'
+
+ expect(page).to have_current_path /\/projects\/foo-bar\/?/
+
+ project = Project.last
+
+ # custom_field_with_default_value should be activated and contain the default value
+ expect(project.project_custom_field_ids).to contain_exactly(
+ custom_field_with_default_value.id, required_custom_field.id
+ )
+
+ expect(project.custom_value_for(custom_field_with_default_value).value).to eq('Default value')
+ end
+
+ it 'does not enable custom fields with default values if set to blank explicitly' do
+ # native blank input does not work with this input, using support class here
+ field = FormFields::InputFormField.new(custom_field_with_default_value)
+ field.set_value("")
+
+ click_on 'Save'
+
+ expect(page).to have_current_path /\/projects\/foo-bar\/?/
+
+ project = Project.last
+
+ # custom_field_with_default_value should not be activated
+ expect(project.project_custom_field_ids).to contain_exactly(
+ required_custom_field.id
+ )
+ end
+
+ it 'does enable custom fields with default values if overwritten with a new value' do
+ fill_in 'Foo with default value', with: 'foo'
+
+ click_on 'Save'
+
+ expect(page).to have_current_path /\/projects\/foo-bar\/?/
+
+ project = Project.last
+
+ # custom_field_with_default_value should be activated and contain the overwritten value
+ expect(project.project_custom_field_ids).to contain_exactly(
+ custom_field_with_default_value.id, required_custom_field.id
+ )
+
+ expect(project.custom_value_for(custom_field_with_default_value).value).to eq('foo')
+ end
+ end
+ end
end
end