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
[#44673] Editing custom fields on work packages in readonly status possible via bulk edit
Dombi Attilab28c088a54df
diff --git a/app/contracts/model_contract.rb b/app/contracts/model_contract.rbindex 9dbc267af58..5a64e86170f 100644--- a/app/contracts/model_contract.rb+++ b/app/contracts/model_contract.rb@@ -77,6 +77,9 @@ class ModelContract < BaseContractenddef changed_by_user- model.respond_to?(:changed_by_user) ? model.changed_by_user : model.changed+ return model.changed_by_user if model.respond_to?(:changed_by_user)+ return model.changed_with_custom_fields if model.respond_to?(:changed_with_custom_fields)++ model.changedendenddiff --git a/frontend/src/app/shared/components/enterprise-page/enterprise-page.component.html b/frontend/src/app/shared/components/enterprise-page/enterprise-page.component.htmlindex 1f056b821de..572ff8ceb85 100644--- a/frontend/src/app/shared/components/enterprise-page/enterprise-page.component.html+++ b/frontend/src/app/shared/components/enterprise-page/enterprise-page.component.html@@ -41,7 +41,7 @@<a[attr.href]="text.upgrade_link"- target=”_blank”+ target="_blank"class="button -highlight"><span class="spot-icon spot-icon_enterprise-badge"></span>{{ text.upgrade }}diff --git a/lib/open_project/changed_by_system.rb b/lib/open_project/changed_by_system.rbindex a9f087c3397..33c887b5f48 100644--- a/lib/open_project/changed_by_system.rb+++ b/lib/open_project/changed_by_system.rb@@ -95,17 +95,29 @@ module OpenProject# currently changed. But it will only include those attributes, that have not# been changed within a #change_by_system block and as such are caused by user input.def changed_by_user- (changes.reject { |key, change| changed_by_system[key] == change }).keys+ (model_changes.reject { |key, change| changed_by_system[key] == change }).keysendprivatedef non_no_op_changes- changes.reject { |_, (old, new)| old == 0 && new.nil? }+ model_changes.reject { |_, (old, new)| old == 0 && new.nil? }enddef changes_compared_to(prior_changes)- changes.select { |c| !prior_changes[c] || prior_changes[c].last != changes[c].last }+ model_changes.select { |c| !prior_changes[c] || prior_changes[c].last != model_changes[c].last }+ end++ # Construct the custom model changes method, which is based on the `ActiveRecord::Base#changes`.+ # Includes the changes of the custom fields, if the object extends the acts_as_customizable+ # (Redmine::Acts::Customizable::InstanceMethods) plugin.+ # Ideally we would override the `ActiveRecord::Base#changes`, but adding the custom field attributes+ # to the `ActiveRecord::Base#changes` may produce some unwanted side effects.+ def model_changes+ changes.tap do |c|+ c.merge!(custom_field_changes) if respond_to?(:custom_field_changes)+ c+ endendendenddiff --git a/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb b/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rbindex af5c9ee8438..ca4fee51796 100644--- a/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb+++ b/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb@@ -157,9 +157,10 @@ module Redminedef typed_custom_value_for(c)cvs = custom_value_for(c)- if cvs.is_a? Array+ case cvs+ when Arraycvs.map(&:typed_value)- elsif cvs.is_a? CustomValue+ when CustomValuecvs.typed_valueelsecvs@@ -169,9 +170,10 @@ module Redminedef formatted_custom_value_for(c)cvs = custom_value_for(c)- if cvs.is_a? Array+ case cvs+ when Arraycvs.map(&:formatted_value)- elsif cvs.is_a? CustomValue+ when CustomValuecvs.formatted_valueelsecvs@@ -226,6 +228,29 @@ module Redmine.each { |custom_value| add_custom_value_errors! custom_value }end+ # Build the changes hash similar to ActiveRecord::Base#changes,+ # but for the custom field values that have been changed.+ def custom_field_changes+ custom_field_values.reduce({}) do |cfv_changes, cfv|+ next cfv_changes unless cfv.changed?++ # In order to construct a valid changes hash, we need to find the old value if it exists.+ # Otherwise set it to nil.+ cfv_was = custom_values.find do |cv|+ cv.marked_for_destruction? && cv.custom_field_id == cfv.custom_field_id+ end+ value_was = cfv_was&.value+ # Skip when the old value equals the new value (no change happened).+ next cfv_changes if value_was == cfv.value++ cfv_changes.merge("custom_field_#{cfv.custom_field_id}": [value_was, cfv.value])+ end+ end++ def changed_with_custom_fields+ changed + custom_field_changes.keys+ end+def add_custom_value_errors!(custom_value)custom_value.errors.each do |error|name = custom_value.custom_field.accessor_name.to_symdiff --git a/spec/contracts/work_packages/update_contract_spec.rb b/spec/contracts/work_packages/update_contract_spec.rbindex 66c6813c473..c44f14678b7 100644--- a/spec/contracts/work_packages/update_contract_spec.rb+++ b/spec/contracts/work_packages/update_contract_spec.rb@@ -381,27 +381,65 @@ describe WorkPackages::UpdateContract dodescribe 'readonly status' docontext 'with the status being readonly', with_ee: %i[readonly_work_packages] dolet(:status) { build_stubbed(:status, is_readonly: true) }- let(:new_priority) { build_stubbed(:priority) }- before do- work_package.priority = new_priority+ describe 'updating the priority' do+ let(:new_priority) { build_stubbed(:priority) }- contract.validate- end+ before do+ work_package.priority = new_priority- it 'is invalid' do- expect(contract)- .not_to be_valid- end+ contract.validate+ end++ it 'is invalid' do+ expect(contract)+ .not_to be_valid+ end++ it 'adds an error to the written to attribute' do+ expect(contract.errors.symbols_for(:priority_id))+ .to include(:error_readonly)+ end- it 'adds an error to the written to attribute' do- expect(contract.errors.symbols_for(:priority_id))- .to include(:error_readonly)+ it 'adds an error to base to better explain' do+ expect(contract.errors.symbols_for(:base))+ .to include(:readonly_status)+ endend- it 'adds an error to base to better explain' do- expect(contract.errors.symbols_for(:base))- .to include(:readonly_status)+ describe 'updating the custom field values' do+ let(:cf1) { create(:string_wp_custom_field) }++ before do+ work_package_project.work_package_custom_fields << cf1+ type.custom_fields << cf1+ work_package.custom_field_values = { cf1.id => 'test' }+ contract.validate+ end++ shared_examples_for 'custom_field readonly errors' do+ it 'adds an error to the written custom field attribute' do+ expect(contract.errors.symbols_for(:"custom_field_#{cf1.id}"))+ .to include(:error_readonly)+ end++ it 'adds an error to base to better explain' do+ expect(contract.errors.symbols_for(:base))+ .to include(:readonly_status)+ end+ end++ context 'when the subject does not extends OpenProject::ChangedBySystem' do+ include_examples 'custom_field readonly errors'+ end++ context 'when the subject extends OpenProject::ChangedBySystem' do+ before do+ work_package.extend(OpenProject::ChangedBySystem)+ end++ include_examples 'custom_field readonly errors'+ endendendenddiff --git a/spec/factories/status_factory.rb b/spec/factories/status_factory.rbindex 1fc33ae2075..5b079d4f849 100644--- a/spec/factories/status_factory.rb+++ b/spec/factories/status_factory.rb@@ -39,5 +39,9 @@ FactoryBot.define dofactory :default_status dois_default { true }end++ trait :readonly do+ is_readonly { true }+ endendenddiff --git a/spec/features/work_packages/bulk/update_work_package_spec.rb b/spec/features/work_packages/bulk/update_work_package_spec.rbindex 32db96fb2e3..87b02b69a58 100644--- a/spec/features/work_packages/bulk/update_work_package_spec.rb+++ b/spec/features/work_packages/bulk/update_work_package_spec.rb@@ -2,43 +2,57 @@ require 'spec_helper'require 'features/page_objects/notification'describe 'Bulk update work packages through Rails view', js: true do- let(:dev_role) do- create :role,- permissions: %i[view_work_packages]+ shared_let(:type) { create(:type, name: 'Bug') }+ shared_let(:project) { create(:project, name: 'Source', types: [type]) }+ shared_let(:status) { create(:status) }+ shared_let(:custom_field) do+ create(:string_wp_custom_field,+ name: 'Text CF',+ types: [type],+ projects: [project])end- let(:mover_role) do- create :role,- permissions: %i[view_work_packages copy_work_packages move_work_packages manage_subtasks add_work_packages]++ shared_let(:dev_role) do+ create(:role,+ permissions: %i[view_work_packages])+ end+ shared_let(:mover_role) do+ create(:role,+ permissions: %i[view_work_packages copy_work_packages move_work_packages manage_subtasks add_work_packages])end- let(:dev) do- create :user,+ shared_let(:dev) do+ create(:user,firstname: 'Dev',lastname: 'Guy',member_in_project: project,- member_through_role: dev_role+ member_through_role: dev_role)end- let(:mover) do- create :admin,+ shared_let(:mover) do+ create(:admin,firstname: 'Manager',lastname: 'Guy',member_in_project: project,- member_through_role: mover_role+ member_through_role: mover_role)end- let(:type) { create :type, name: 'Bug' }-- let!(:project) { create(:project, name: 'Source', types: [type]) }-- let!(:status) { create :status }- let(:work_package2_status) { status }-- let!(:work_package) do+ shared_let(:work_package) docreate(:work_package,author: dev,status:,project:,type:)end++ shared_let(:status2) { create(:default_status) }+ shared_let(:workflow) do+ create(:workflow,+ type_id: type.id,+ old_status: work_package.status,+ new_status: status2,+ role: mover_role)+ end++ let(:work_package2_status) { status }let!(:work_package2) docreate(:work_package,author: dev,@@ -47,15 +61,6 @@ describe 'Bulk update work packages through Rails view', js: true dotype:)end- let!(:status2) { create :default_status }- let!(:workflow) do- create :workflow,- type_id: type.id,- old_status: work_package.status,- new_status: status2,- role: mover_role- end-let(:wp_table) { ::Pages::WorkPackagesTable.new(project) }let(:context_menu) { Components::WorkPackages::ContextMenu.new }let(:display_representation) { ::Components::WorkPackages::DisplayRepresentation.new }@@ -78,11 +83,11 @@ describe 'Bulk update work packages through Rails view', js: true docontext_menu.open_for work_packagecontext_menu.choose 'Bulk edit'- select status2.name, from: 'work_package_status_id'notes.set_markdown('The typed note')endit 'sets status and leaves a note' do+ select status2.name, from: 'work_package_status_id'click_on 'Submit'expect_angular_frontend_initialized@@ -100,6 +105,7 @@ describe 'Bulk update work packages through Rails view', js: true dolet(:work_package2_status) { create(:status) } # without creating a workflowit 'does not update the work packages' do+ select status2.name, from: 'work_package_status_id'fill_in 'Parent', with: '-1'click_on 'Submit'@@ -132,6 +138,68 @@ describe 'Bulk update work packages through Rails view', js: true doexpect(work_package2.status_id).to eq(work_package2_status.id)endend++ context 'when editing custom field of work packages with a readonly status (regression#44673)' do+ let(:work_package2_status) { create(:status, :readonly) }++ context 'with enterprise', with_ee: [:readonly_work_packages] do+ it 'does not update the work packages' do+ expect(work_package.send(custom_field.accessor_name)).to be_nil+ expect(work_package2.send(custom_field.accessor_name)).to be_nil++ fill_in custom_field.name, with: 'Custom field text'+ click_on 'Submit'++ expect(page)+ .to have_selector(+ '.flash.error',+ text: I18n.t('work_packages.bulk.x_out_of_y_could_be_saved', total: 2, failing: 1, success: 1)+ )++ expect(page)+ .to have_selector(+ '.flash.error',+ text: <<~MSG.squish+ #{work_package2.id}:+ #{custom_field.name} #{I18n.t('activerecord.errors.messages.error_readonly')}+ #{I18n.t('activerecord.errors.models.work_package.readonly_status')}+ MSG+ )++ # Should update 1 work package custom field only+ work_package.reload+ work_package2.reload++ expect(work_package.send(custom_field.accessor_name))+ .to eq('Custom field text')++ expect(work_package2.send(custom_field.accessor_name))+ .to be_nil+ end+ end++ context 'without enterprise', with_ee: false do+ it 'ignores the readonly status and updates the work packages' do+ expect(work_package.send(custom_field.accessor_name)).to be_nil+ expect(work_package2.send(custom_field.accessor_name)).to be_nil++ fill_in custom_field.name, with: 'Custom field text'+ click_on 'Submit'++ expect(page).to have_selector('.flash.notice', text: I18n.t(:notice_successful_update))++ # Should update 2 work package custom fields+ work_package.reload+ work_package2.reload++ expect(work_package.send(custom_field.accessor_name))+ .to eq('Custom field text')++ expect(work_package2.send(custom_field.accessor_name))+ .to eq('Custom field text')+ end+ end+ endendcontext 'without permission' dodiff --git a/spec/lib/api/contracts/model_contract_spec.rb b/spec/lib/api/contracts/model_contract_spec.rbindex d2c23921293..1c937a0be0a 100644--- a/spec/lib/api/contracts/model_contract_spec.rb+++ b/spec/lib/api/contracts/model_contract_spec.rb@@ -33,63 +33,130 @@ describe ModelContract dochild_attribute: nil,grand_child_attribute: nil,overwritten_attribute: nil,+ custom_field1: nil,+ not_allowed: nil,changed: [],valid?: true,errors: ActiveModel::Errors.new(nil))end- let(:child_contract) { ChildContract.new(model, nil) }- let(:grand_child_contract) { GrandChildContract.new(model, nil) }- before do- child_contract.child_value = 0- grand_child_contract.child_value = 0- end+ context 'with child and grand_child' do+ let(:child_contract_class) do+ Class.new(ModelContract) do+ attr_accessor :child_value++ attribute :child_attribute+ attribute :overwritten_attribute do+ @child_value = 1+ end+ end+ end+ let(:child_contract) { child_contract_class.new(model, nil) }- describe 'child' do- class ChildContract < ModelContract- attr_accessor :child_value+ let(:grand_child_contract_class) do+ Class.new(child_contract_class) do+ attr_accessor :grand_child_value- attribute :child_attribute- attribute :overwritten_attribute do- @child_value = 1+ attribute :grand_child_attribute+ attribute :overwritten_attribute do+ @grand_child_value = 2+ endendend+ let(:grand_child_contract) { grand_child_contract_class.new(model, nil) }++ before do+ child_contract.child_value = 0+ grand_child_contract.child_value = 0+ end++ describe 'child' do+ it 'collects its own writable attributes' do+ expect(child_contract.writable_attributes).to include('child_attribute',+ 'overwritten_attribute')+ end- it 'collects its own writable attributes' do- expect(child_contract.writable_attributes).to include('child_attribute',- 'overwritten_attribute')+ it 'collects its own attribute validations' do+ child_contract.validate+ expect(child_contract.child_value).to eq(1)+ endend- it 'collects its own attribute validations' do- child_contract.validate- expect(child_contract.child_value).to eq(1)+ describe 'grand_child' do+ it 'considers its ancestor writable attributes' do+ expect(grand_child_contract.writable_attributes).to include('child_attribute',+ 'overwritten_attribute',+ 'grand_child_attribute')+ end++ it 'does not contain the same attribute twice, but also has the _id variant' do+ expect(grand_child_contract.writable_attributes.count).to eq(6)+ end++ it 'executes all the validations' do+ grand_child_contract.validate+ expect(grand_child_contract.child_value).to eq(1)+ expect(grand_child_contract.grand_child_value).to eq(2)+ endendend- describe 'grand_child' do- class GrandChildContract < ChildContract- attr_accessor :grand_child_value+ describe 'valid?' do+ let(:model_contract_class) do+ Class.new(ModelContract) do+ attribute :custom_field1+ attribute :not_allowed+ end+ end+ let(:model_contract) { model_contract_class.new(model, nil) }++ context 'when the model extends no plugins' do+ before do+ allow(model).to receive(:changed).and_return([:custom_field1])+ end- attribute :grand_child_attribute- attribute :overwritten_attribute do- @grand_child_value = 2+ it 'adds an error to the custom field attribute' do+ model_contract.valid?+ expect(model_contract.errors.symbols_for(:custom_field1))+ .to include(:error_readonly)endend- it 'considers its ancestor writable attributes' do- expect(grand_child_contract.writable_attributes).to include('child_attribute',- 'overwritten_attribute',- 'grand_child_attribute')+ context 'when the model extends the acts_as_customizable plugin' do+ before do+ allow(model).to receive(:changed_with_custom_fields).and_return([:custom_field1])+ end++ it 'adds an error to the custom field attribute' do+ model_contract.valid?+ expect(model_contract.errors.symbols_for(:custom_field1))+ .to include(:error_readonly)+ endend- it 'does not contain the same attribute twice, but also has the _id variant' do- expect(grand_child_contract.writable_attributes.count).to eq(6)+ context 'when the model extends the OpenProject::ChangedBySystem module' do+ before do+ allow(model).to receive(:changed_by_user).and_return([:custom_field1])+ end++ it 'adds an error to the custom field attribute' do+ model_contract.valid?+ expect(model_contract.errors.symbols_for(:custom_field1))+ .to include(:error_readonly)+ endend- it 'executes all the validations' do- grand_child_contract.validate- expect(grand_child_contract.child_value).to eq(1)- expect(grand_child_contract.grand_child_value).to eq(2)+ context 'when the model extends both modules' do+ before do+ allow(model).to receive(:changed_by_user).and_return([:custom_field1])+ allow(model).to receive(:changed_with_custom_fields).and_return([:no_allowed])+ end++ it 'adds an error to the custom field attribute from the OpenProject::ChangedBySystem module' do+ model_contract.valid?+ expect(model_contract.errors.symbols_for(:custom_field1))+ .to include(:error_readonly)+ endendendenddiff --git a/spec/lib/open_project/changed_by_system_spec.rb b/spec/lib/open_project/changed_by_system_spec.rbindex f427829e3b8..21478ded68b 100644--- a/spec/lib/open_project/changed_by_system_spec.rb+++ b/spec/lib/open_project/changed_by_system_spec.rb@@ -89,5 +89,28 @@ describe OpenProject::ChangedBySystem do.to be_emptyendend++ context 'when the model has the acts_as_customizable plugin included' do+ subject(:model) do+ create(:work_package, project:).tap do |wp|+ wp.extend(described_class)+ end+ end++ let(:type) { create(:type_standard) }+ let(:project) { create(:project, types: [type]) }+ let(:cf1) { create(:work_package_custom_field) }++ before do+ project.work_package_custom_fields << cf1+ type.custom_fields << cf1+ end++ it 'returns the custom fields too' do+ model.custom_field_values = { cf1.id => 'test' }+ expect(model.changed_by_user)+ .to include("custom_field_#{cf1.id}")+ end+ endendenddiff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rbindex def817fe143..a1b00a52082 100644--- a/spec/models/group_spec.rb+++ b/spec/models/group_spec.rb@@ -29,10 +29,10 @@require 'spec_helper'require_relative '../support/shared/become_member'-describe Group, type: :model do+describe Group dolet(:group) { create(:group) }let(:user) { create(:user) }- let(:watcher) { create :user }+ let(:watcher) { create(:user) }let(:project) { create(:project_with_types) }let(:status) { create(:status) }let(:package) do@@ -43,7 +43,7 @@ describe Group, type: :model doendit 'creates' do- g = Group.new(lastname: 'New group')+ g = described_class.new(lastname: 'New group')expect(g.save).to be trueend@@ -109,7 +109,7 @@ describe Group, type: :model dodescribe 'group with empty group name' dolet(:group) { build(:group, lastname: '') }- it { expect(group.valid?).to be_falsey }+ it { expect(group).not_to be_valid }describe 'error message' dobefore do@@ -141,4 +141,9 @@ describe Group, type: :model doinclude_examples 'creates an audit trail on destroy' dosubject { create(:attachment) }end++ it_behaves_like 'acts_as_customizable included' do+ let(:model_instance) { group }+ let(:custom_field) { create(:string_group_custom_field) }+ endenddiff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rbindex 895418d77d2..94d0f8c74ac 100644--- a/spec/models/project_spec.rb+++ b/spec/models/project_spec.rb@@ -29,9 +29,9 @@require 'spec_helper'require File.expand_path('../support/shared/become_member', __dir__)-describe Project, type: :model do+describe Project doinclude BecomeMember- shared_let(:admin) { create :admin }+ shared_let(:admin) { create(:admin) }let(:active) { true }let(:project) { create(:project, active:) }@@ -105,7 +105,7 @@ describe Project, type: :model docontext 'with copy project permission' doit 'is true' do- expect(project.copy_allowed?).to be_truthy+ expect(project).to be_copy_allowedendend@@ -113,7 +113,7 @@ describe Project, type: :model dolet(:permission_granted) { false }it 'is false' do- expect(project.copy_allowed?).to be_falsey+ expect(project).not_to be_copy_allowedendendend@@ -372,4 +372,9 @@ describe Project, type: :model doendendend++ it_behaves_like 'acts_as_customizable included' do+ let(:model_instance) { project }+ let(:custom_field) { create(:string_project_custom_field) }+ endenddiff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rbindex 86c437b8174..95dc64016cd 100644--- a/spec/models/user_spec.rb+++ b/spec/models/user_spec.rb@@ -28,7 +28,7 @@require 'spec_helper'-describe User, type: :model do+describe User dolet(:user) { build(:user) }let(:project) { create(:project_with_types) }let(:role) { create(:role, permissions: [:view_work_packages]) }@@ -49,7 +49,7 @@ describe User, type: :model doit 'is valid' douser.firstname = 'a' * 256user.lastname = 'b' * 256- user.mail = 'fo' + ('o' * 237) + '@mail.example.com'+ user.mail = "fo#{'o' * 237}@mail.example.com"expect(user).to be_validexpect(user.save).to be_truthyend@@ -299,7 +299,7 @@ describe User, type: :model dolet!(:blocked_user) docreate(:user,failed_login_count: 3,- last_failed_login_on: Time.now)+ last_failed_login_on: Time.zone.now)endbefore do@@ -309,8 +309,8 @@ describe User, type: :model doendit 'returns the single blocked user' do- expect(User.blocked.length).to eq(1)- expect(User.blocked.first.id).to eq(blocked_user.id)+ expect(described_class.blocked.length).to eq(1)+ expect(described_class.blocked.first.id).to eq(blocked_user.id)endend@@ -328,7 +328,7 @@ describe User, type: :model doendcontext 'for user with an auth source' do- let(:allowed_auth_source) { create :auth_source }+ let(:allowed_auth_source) { create(:auth_source) }context 'that allows password changes' dobefore do@@ -337,12 +337,12 @@ describe User, type: :model doendit 'allows password changes' do- expect(user.change_password_allowed?).to be_truthy+ expect(user).to be_change_password_allowedendendcontext 'that does not allow password changes' do- let(:denied_auth_source) { create :auth_source }+ let(:denied_auth_source) { create(:auth_source) }before dodef denied_auth_source.allow_password_changes?; false; end@@ -350,7 +350,7 @@ describe User, type: :model doendit 'does not allow password changes' do- expect(user.change_password_allowed?).to be_falsey+ expect(user).not_to be_change_password_allowedendendend@@ -362,7 +362,7 @@ describe User, type: :model doendit 'does not allow a password change' do- expect(user.change_password_allowed?).to be_falsey+ expect(user).not_to be_change_password_allowedendendend@@ -402,7 +402,7 @@ describe User, type: :model dolet(:user) { build(:user, identity_url: 'test_provider:veryuniqueid') }it 'returns true' do- expect(user.uses_external_authentication?).to be_truthy+ expect(user).to be_uses_external_authenticationendend@@ -410,39 +410,46 @@ describe User, type: :model dolet(:user) { build(:user, identity_url: nil) }it 'returns false' do- expect(user.uses_external_authentication?).to be_falsey+ expect(user).not_to be_uses_external_authenticationendendenddescribe 'user create with empty password' do+ let(:user) { described_class.new(firstname: 'new', lastname: 'user', mail: '[email protected]') }+before do- @u = User.new(firstname: 'new', lastname: 'user', mail: '[email protected]')- @u.login = 'new_user'- @u.password = ''- @u.password_confirmation = ''- @u.save+ user.login = 'new_user'+ user.password = ''+ user.password_confirmation = ''+ user.saveend- it { expect(@u.valid?).to be_falsey }+ it { expect(user).not_to be_valid }it {- expect(@u.errors[:password]).to include I18n.t('activerecord.errors.messages.too_short',+ expect(user.errors[:password]).to include I18n.t('activerecord.errors.messages.too_short',count: Setting.password_min_length.to_i)}enddescribe '#random_password' do- before do- @u = User.new- expect(@u.password).to be_nil- expect(@u.password_confirmation).to be_nil- @u.random_password!+ let(:user) { described_class.new }++ context 'without generation' do+ it { expect(user.password).to be_nil }+ it { expect(user.password_confirmation).to be_nil }end- it { expect(@u.password).not_to be_blank }- it { expect(@u.password_confirmation).not_to be_blank }- it { expect(@u.force_password_change).to be_truthy }+ context 'with generation' do+ before do+ user.random_password!+ end++ it { expect(user.password).not_to be_blank }+ it { expect(user.password_confirmation).not_to be_blank }+ it { expect(user.force_password_change).to be_truthy }+ endenddescribe '#try_authentication_for_existing_user' do@@ -463,7 +470,7 @@ describe User, type: :model douser_double = build_user_double_with_expired_password(true)# use !! to ensure value is boolean- expect(!!User.try_authentication_for_existing_user(user_double, 'anypassword')).to \+ expect(!!described_class.try_authentication_for_existing_user(user_double, 'anypassword')).to \be(false)end@@ -471,7 +478,7 @@ describe User, type: :model douser_double = build_user_double_with_expired_password(false)# use !! to ensure value is boolean- expect(!!User.try_authentication_for_existing_user(user_double, 'anypassword')).to \+ expect(!!described_class.try_authentication_for_existing_user(user_double, 'anypassword')).to \be(true)end@@ -489,7 +496,7 @@ describe User, type: :model doendit 'succeeds' do- expect(User.try_authentication_for_existing_user(user_with_external_auth_source, 'password'))+ expect(described_class.try_authentication_for_existing_user(user_with_external_auth_source, 'password')).to eq(user_with_external_auth_source)endend@@ -500,7 +507,7 @@ describe User, type: :model doendit 'fails when the authentication fails' do- expect(User.try_authentication_for_existing_user(user_with_external_auth_source, 'password'))+ expect(described_class.try_authentication_for_existing_user(user_with_external_auth_source, 'password')).to be_nilendend@@ -564,24 +571,24 @@ describe User, type: :model doit 'creates a SystemUser' doexpect do- system_user = User.system- expect(system_user.new_record?).to be_falsey- expect(system_user.is_a?(SystemUser)).to be_truthy- end.to change(User, :count).by(1)+ system_user = described_class.system+ expect(system_user).not_to be_new_record+ expect(system_user).to be_a(SystemUser)+ end.to change(described_class, :count).by(1)endendcontext 'a SystemUser exists' dobefore do- @u = User.system+ @u = described_class.systemexpect(SystemUser.first).to eq(@u)endit 'returns existing SystemUser' doexpect do- system_user = User.system+ system_user = described_class.systemexpect(system_user).to eq(@u)- end.not_to change(User, :count)+ end.not_to change(described_class, :count)endendend@@ -600,7 +607,7 @@ describe User, type: :model dodefault_admin.saveend- it { expect(User.default_admin_account_changed?).to be_falsey }+ it { expect(described_class).not_to be_default_admin_account_changed }endcontext 'default admin account exists with changed password' do@@ -610,7 +617,7 @@ describe User, type: :model dodefault_admin.saveend- it { expect(User.default_admin_account_changed?).to be_truthy }+ it { expect(described_class).to be_default_admin_account_changed }endcontext 'default admin account was deleted' do@@ -619,16 +626,16 @@ describe User, type: :model dodefault_admin.deleteend- it { expect(User.default_admin_account_changed?).to be_truthy }+ it { expect(described_class).to be_default_admin_account_changed }endcontext 'default admin account was disabled' dobefore do- default_admin.status = User.statuses[:locked]+ default_admin.status = described_class.statuses[:locked]default_admin.saveend- it { expect(User.default_admin_account_changed?).to be_truthy }+ it { expect(described_class).to be_default_admin_account_changed }endend@@ -676,11 +683,11 @@ describe User, type: :model doenddescribe '.newest' do- let!(:anonymous) { User.anonymous }+ let!(:anonymous) { described_class.anonymous }let!(:user1) { create(:user) }let!(:user2) { create(:user) }- let(:newest) { User.newest.to_a }+ let(:newest) { described_class.newest.to_a }it 'without anonymous user', :aggregate_failures doexpect(newest).to include(user1)@@ -691,7 +698,7 @@ describe User, type: :model dodescribe '#mail_regexp' doit 'handles suffixed mails' do- _, suffixed = User.mail_regexp('[email protected]')+ _, suffixed = described_class.mail_regexp('[email protected]')expect(suffixed).to be_truthyendend@@ -709,10 +716,10 @@ describe User, type: :model domatch2 = described_class.find_by_mail('[email protected]')expect([user1.id, user2.id]).to include(match2.id)- matches = User.where_mail_with_suffix('[email protected]')+ matches = described_class.where_mail_with_suffix('[email protected]')expect(matches.pluck(:id)).to match_array [user1.id, user2.id]- matches = User.where_mail_with_suffix('[email protected]')+ matches = described_class.where_mail_with_suffix('[email protected]')expect(matches.pluck(:id)).to match_array [user1.id]endend@@ -728,7 +735,7 @@ describe User, type: :model domatch2 = described_class.find_by_mail('[email protected]')expect([user1.id, user2.id, user3.id]).to include(match2.id)- matches = User.where_mail_with_suffix('[email protected]')+ matches = described_class.where_mail_with_suffix('[email protected]')expect(matches.pluck(:id)).to match_array [user1.id, user2.id, user3.id]endend@@ -836,11 +843,11 @@ describe User, type: :model dodescribe '.find_by_mail' dolet(:mail) { '[email protected]' }- let!(:user) { create :user, mail: }+ let!(:user) { create(:user, mail:) }context 'with the exact mail' doit 'finds the user' do- expect(described_class.find_by_mail(mail))+ expect(described_class.find_by(mail:)).to eq userendend@@ -884,4 +891,9 @@ describe User, type: :model doinclude_examples 'creates an audit trail on destroy' dosubject { create(:attachment) }end++ it_behaves_like 'acts_as_customizable included' do+ let(:model_instance) { user }+ let(:custom_field) { create(:string_user_custom_field) }+ endenddiff --git a/spec/models/version_spec.rb b/spec/models/version_spec.rbindex 12a6be0e3ab..098e6612456 100644--- a/spec/models/version_spec.rb+++ b/spec/models/version_spec.rb@@ -28,7 +28,7 @@require 'spec_helper'-describe Version, type: :model do+describe Version dosubject(:version) { build(:version, name: 'Test Version') }it { is_expected.to be_valid }@@ -87,21 +87,21 @@ describe Version, type: :model doversion.sharing = 'system'version.save!- expect(Version.systemwide).to match_array [version]+ expect(described_class.systemwide).to match_array [version]endit 'is empty if the version is not shared' doversion.sharing = 'none'version.save!- expect(Version.systemwide).to be_empty+ expect(described_class.systemwide).to be_emptyendit 'is empty if the version is shared with the project hierarchy' doversion.sharing = 'hierarchy'version.save!- expect(Version.systemwide).to be_empty+ expect(described_class.systemwide).to be_emptyendend@@ -252,7 +252,7 @@ describe Version, type: :model doendit 'is empty for a new version' do- expect(Version.new.projects).to be_empty+ expect(described_class.new.projects).to be_emptyendit 'returns project the version is defined in for unshared' do@@ -501,4 +501,9 @@ describe Version, type: :model doendendend++ it_behaves_like 'acts_as_customizable included' do+ let(:model_instance) { version }+ let(:custom_field) { create(:version_custom_field) }+ endenddiff --git a/spec/models/work_package/work_package_acts_as_customizable_spec.rb b/spec/models/work_package/work_package_acts_as_customizable_spec.rbindex f7b5eed02a1..6ce4144292d 100644--- a/spec/models/work_package/work_package_acts_as_customizable_spec.rb+++ b/spec/models/work_package/work_package_acts_as_customizable_spec.rb@@ -28,7 +28,7 @@require 'spec_helper'-describe WorkPackage, 'acts_as_customizable', type: :model do+describe WorkPackage, 'acts_as_customizable' dolet(:type) { create(:type_standard) }let(:project) { create(:project, types: [type]) }let(:user) { create(:user) }@@ -37,12 +37,12 @@ describe WorkPackage, 'acts_as_customizable', type: :model dolet(:work_package) { create(:work_package, project:, type:) }let(:new_work_package) do- WorkPackage.new type:,- project:,- author: user,- status:,- priority:,- subject: 'some subject'+ described_class.new type:,+ project:,+ author: user,+ status:,+ priority:,+ subject: 'some subject'enddef setup_custom_field(cf)@@ -85,7 +85,7 @@ describe WorkPackage, 'acts_as_customizable', type: :model doendit 'says to respond to valid custom field accessors' do- expect(work_package.respond_to?(included_cf.accessor_name)).to be_truthy+ expect(work_package).to respond_to(included_cf.accessor_name)endit 'really responds to valid custom field accessors' do@@ -93,7 +93,7 @@ describe WorkPackage, 'acts_as_customizable', type: :model doendit 'says to not respond to foreign custom field accessors' do- expect(work_package.respond_to?(other_cf.accessor_name)).to be_falsey+ expect(work_package).not_to respond_to(other_cf.accessor_name)endit 'does really not respond to foreign custom field accessors' do@@ -128,4 +128,12 @@ describe WorkPackage, 'acts_as_customizable', type: :model doexpect(work_package.errors["custom_field_#{cf2.id}"].size).to eq 1endend++ it_behaves_like 'acts_as_customizable included' do+ let(:model_instance) { work_package }+ let(:custom_field) { create(:string_wp_custom_field) }+ before do+ setup_custom_field(custom_field)+ end+ endenddiff --git a/spec/requests/api/v3/work_packages/update_resource_spec.rb b/spec/requests/api/v3/work_packages/update_resource_spec.rbindex d20b71d5eba..9bc75339df4 100644--- a/spec/requests/api/v3/work_packages/update_resource_spec.rb+++ b/spec/requests/api/v3/work_packages/update_resource_spec.rb@@ -30,7 +30,6 @@ require 'spec_helper'require 'rack/test'describe 'API v3 Work package resource',- type: :request,content_type: :json doinclude API::V3::Utilities::PathHelper@@ -74,7 +73,7 @@ describe 'API v3 Work package resource',context 'user without needed permissions' docontext 'no permission to see the work package' dolet(:work_package) { create(:work_package) }- let(:current_user) { create :user }+ let(:current_user) { create(:user) }let(:params) { valid_params }include_context 'patch request'@@ -399,12 +398,13 @@ describe 'API v3 Work package resource',let(:project_link) { api_v3_paths.project target_project.id }let(:project_parameter) { { _links: { project: { href: project_link } } } }let(:params) { valid_params.merge(project_parameter) }+ let(:member_permissions) { [:move_work_packages] }before do- create :member,+ create(:member,user: current_user,project: target_project,- roles: [create(:role, permissions: [:move_work_packages])]+ roles: [create(:role, permissions: member_permissions)])allow(User).to receive(:current).and_return current_userend@@ -428,6 +428,7 @@ describe 'API v3 Work package resource',endcontext 'with a custom field defined on the target project' do+ let(:member_permissions) { %i[move_work_packages edit_work_packages] }let(:custom_field) { create(:work_package_custom_field) }let(:custom_field_parameter) { { "customField#{custom_field.id}": true } }let(:params) { valid_params.merge(project_parameter).merge(custom_field_parameter) }diff --git a/spec/support/shared/acts_as_customizable.rb b/spec/support/shared/acts_as_customizable.rbnew file mode 100644index 00000000000..0b225a9c4ea--- /dev/null+++ b/spec/support/shared/acts_as_customizable.rb@@ -0,0 +1,99 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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.+#++++shared_examples_for 'acts_as_customizable included' do+ describe '#custom_field_changes' do+ context 'when no custom field value exists' do+ before do+ model_instance.custom_values.destroy_all+ end++ it 'returns no changes' do+ expect(model_instance.custom_field_changes).to be_empty+ end++ context 'when a field value is set' do+ before do+ model_instance.custom_values.destroy_all+ end++ it 'returns the field changes' do+ model_instance.custom_field_values = { custom_field.id => 'test' }+ expect(model_instance.custom_field_changes)+ .to eq({ "custom_field_#{custom_field.id}": [nil, 'test'] })+ end+ end+ end++ context 'when a field value is changed from nil' do+ it 'returns the field changes' do+ model_instance.custom_field_values = { custom_field.id => 'test' }+ expect(model_instance.custom_field_changes)+ .to eq({ "custom_field_#{custom_field.id}": [nil, 'test'] })+ end+ end++ context 'when a field value is changed from a string' do+ before do+ model_instance.custom_field_values = { custom_field.id => 'test' }+ model_instance.save+ end++ it 'returns the field changes' do+ model_instance.custom_field_values = { custom_field.id => 'test2' }+ expect(model_instance.custom_field_changes)+ .to eq({ "custom_field_#{custom_field.id}": ['test', 'test2'] })+ end+ end++ context 'when a field is set to the same value (unchanged)' do+ before do+ model_instance.custom_field_values = { custom_field.id => 'test' }+ model_instance.save+ end++ it 'returns no changes' do+ model_instance.custom_field_values = { custom_field.id => 'test' }+ expect(model_instance.custom_field_changes).to be_empty+ end+ end++ context 'when a field value is changed to nil' do+ before do+ model_instance.custom_field_values = { custom_field.id => 'test' }+ model_instance.save+ end++ it 'returns the field changes' do+ model_instance.custom_field_values = { custom_field.id => nil }+ expect(model_instance.custom_field_changes)+ .to eq({ "custom_field_#{custom_field.id}": ['test', nil] })+ end+ end+ end+end