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

rework changed_by_system (#9043)

ulferts7ecf08e0050a
diff --git a/app/contracts/model_contract.rb b/app/contracts/model_contract.rb
index ef2f84de874..f0bcff79625 100644
--- a/app/contracts/model_contract.rb
+++ b/app/contracts/model_contract.rb
@@ -67,26 +67,18 @@ class ModelContract < BaseContract
private
def readonly_attributes_unchanged
- invalid_changes = attributes_changed_by_user - writable_attributes
-
- invalid_changes.each do |attribute|
+ unauthenticated_changed.each do |attribute|
outside_attribute = collect_ancestor_attribute_aliases[attribute] || attribute
errors.add outside_attribute, :error_readonly
end
end
- def attributes_changed_by_user
- changed = changed_attributes
-
- if options[:changed_by_system]
- changed -= options[:changed_by_system]
- end
-
- changed
+ def unauthenticated_changed
+ changed_by_user - writable_attributes
end
- def changed_attributes
- model.changed
+ def changed_by_user
+ model.respond_to?(:changed_by_user) ? model.changed_by_user : model.changed
end
end
diff --git a/app/contracts/wiki_pages/base_contract.rb b/app/contracts/wiki_pages/base_contract.rb
index 60571bd7431..cacd761e314 100644
--- a/app/contracts/wiki_pages/base_contract.rb
+++ b/app/contracts/wiki_pages/base_contract.rb
@@ -70,8 +70,14 @@ module WikiPages
end
end
- def changed_attributes
- model.changed + (model.content&.changed || [])
+ def changed_by_user
+ content_changed = if model.content
+ model.content.respond_to?(:changed_by_user) ? model.content.changed_by_user : model.content.changed
+ else
+ []
+ end
+
+ super + content_changed
end
end
end
diff --git a/app/services/base_services/base_contracted.rb b/app/services/base_services/base_contracted.rb
index 0afa8240e15..a2d688a2ced 100644
--- a/app/services/base_services/base_contracted.rb
+++ b/app/services/base_services/base_contracted.rb
@@ -36,6 +36,7 @@ module BaseServices
attr_reader :user
def initialize(user:, contract_class: nil, contract_options: {})
+ super()
@user = user
self.contract_class = contract_class || default_contract_class
self.contract_options = contract_options
diff --git a/app/services/base_services/set_attributes.rb b/app/services/base_services/set_attributes.rb
index 67b0f0f2f6f..eb03cbfb75d 100644
--- a/app/services/base_services/set_attributes.rb
+++ b/app/services/base_services/set_attributes.rb
@@ -34,7 +34,7 @@ module BaseServices
def initialize(user:, model:, contract_class:, contract_options: {})
self.user = user
- self.model = model
+ self.model = prepare_model(model)
self.contract_class = contract_class
self.contract_options = contract_options
@@ -69,5 +69,10 @@ module BaseServices
errors: errors,
result: model)
end
+
+ def prepare_model(model)
+ model.extend(OpenProject::ChangedBySystem)
+ model
+ end
end
end
diff --git a/app/services/concerns/contracted.rb b/app/services/concerns/contracted.rb
index ca5029e49cc..b19045956cb 100644
--- a/app/services/concerns/contracted.rb
+++ b/app/services/concerns/contracted.rb
@@ -43,30 +43,10 @@ module Contracted
@contract_class = cls
end
- def changed_by_system(attributes = nil)
- @changed_by_system ||= []
-
- if attributes
- @changed_by_system += Array(attributes)
- end
-
- @changed_by_system
- end
-
- def change_by_system
- prior_changes = non_no_op_changes
-
- ret = yield
-
- changed_by_system(changed_compared_to(prior_changes))
-
- ret
- end
-
private
def instantiate_contract(object, user, options: {})
- contract_class.new(object, user, options: { changed_by_system: changed_by_system }.merge(options))
+ contract_class.new(object, user, options: options)
end
def validate_and_save(object, user, options: {})
@@ -96,17 +76,5 @@ module Contracted
true
end
end
-
- def non_no_op_changes
- model.changes.reject { |_, (old, new)| old == 0 && new.nil? }
- end
-
- def changed_compared_to(prior_changes)
- changed_attributes.select { |c| !prior_changes[c] || prior_changes[c].last != model.changes[c].last }
- end
-
- def changed_attributes
- model.changed
- end
end
end
diff --git a/app/services/messages/set_attributes_service.rb b/app/services/messages/set_attributes_service.rb
index a2da3e10de1..b78cda5f846 100644
--- a/app/services/messages/set_attributes_service.rb
+++ b/app/services/messages/set_attributes_service.rb
@@ -40,7 +40,7 @@ module Messages
end
def set_default_author
- change_by_system do
+ model.change_by_system do
model.author = user
end
end
diff --git a/app/services/wiki_pages/set_attributes_service.rb b/app/services/wiki_pages/set_attributes_service.rb
index 2620228de7a..c3b89e7c115 100644
--- a/app/services/wiki_pages/set_attributes_service.rb
+++ b/app/services/wiki_pages/set_attributes_service.rb
@@ -53,8 +53,11 @@ class WikiPages::SetAttributesService < ::BaseServices::SetAttributes
end
def set_default_attributes(_params)
- change_by_system do
- model.build_content author: user
+ model.build_content
+ model.content.extend(OpenProject::ChangedBySystem)
+
+ model.content.change_by_system do
+ model.content.author = user
end
end
@@ -65,8 +68,4 @@ class WikiPages::SetAttributesService < ::BaseServices::SetAttributes
def split_page_and_content_params(params)
params.partition { |p, _| WikiContent.column_names.include?(p) }.map(&:to_h)
end
-
- def changed_attributes
- super + (model.content&.changed || [])
- end
end
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rb
index af58ffb1b7a..c3cace0713a 100644
--- a/app/services/work_packages/set_attributes_service.rb
+++ b/app/services/work_packages/set_attributes_service.rb
@@ -37,14 +37,14 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributes
set_attachments_attributes(attributes)
set_static_attributes(attributes)
- change_by_system do
+ model.change_by_system do
set_default_attributes(attributes)
update_project_dependent_attributes
end
set_custom_attributes(attributes)
- change_by_system do
+ model.change_by_system do
update_dates
reassign_invalid_status_if_type_changed
set_templated_description
@@ -160,7 +160,7 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributes
def update_project_dependent_attributes
return unless work_package.project_id_changed? && work_package.project_id
- change_by_system do
+ model.change_by_system do
set_version_to_nil
reassign_category
diff --git a/lib/open_project/changed_by_system.rb b/lib/open_project/changed_by_system.rb
new file mode 100644
index 00000000000..90570c67511
--- /dev/null
+++ b/lib/open_project/changed_by_system.rb
@@ -0,0 +1,114 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+
+# OpenProject in its contracts has checks to govern which attributes are writable. Only attributes marked to be
+# writable, can be set by a user.
+# In some scenarios however, the system needs to set calculated attributes, e.g. if a default value is set - the current user
+# becomes the author of the work package.
+#
+# While an attribute should not be marked as writable, is sometimes still needs to be changed, thus.
+# Such attributes can be changed using this module.
+#
+# The flow would be
+#
+# # The model is an AR class like work package
+# model.extend(OpenProject::ChangedBySystem)
+# model.change_by_system do
+# model.author = current_user
+# end
+#
+# The contract checking later on can then query for
+#
+# model.changed_by_system
+#
+# This method will return the changes carried out inside a change_by_system block. It returns a hash of all the
+# changed attributes as well as the value it was changed from and the value it was changed to.
+#
+# Querying
+#
+# model.changed_by_user
+#
+# will return all attributes changed by the user instead.
+
+module OpenProject
+ module ChangedBySystem
+ def changed_by_system(attributes = nil)
+ @changed_by_system ||= {}
+
+ if attributes
+ @changed_by_system.merge!(attributes)
+ end
+
+ @changed_by_system
+ end
+
+ # Wrapper to track changes carried out in the context of the system.
+ #
+ # model.change_by_system do
+ # model.attribute = 1
+ # end
+ #
+ # Attribute changes carried out within such a scope will not count to be changed
+ # by the user. It can therefore be used to set calculated or default values.
+ #
+ # This should never be used with user provided values.
+ #
+ # No only the attribute is tracked but also the values. So it is safe to e.g. first
+ # set a default value, and then mass assign attributes. If the default value is overwritten
+ # by the mass assignment the change in value will give that away.
+ def change_by_system
+ prior_changes = non_no_op_changes
+
+ ret = yield
+
+ changed_by_system(changes_compared_to(prior_changes))
+
+ ret
+ end
+
+ # Similar to #changed from ActiveRecord this returns all attributes that are
+ # 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
+ end
+
+ private
+
+ def non_no_op_changes
+ changes.reject { |_, (old, new)| old == 0 && new.nil? }
+ end
+
+ def changes_compared_to(prior_changes)
+ changes.select { |c| !prior_changes[c] || prior_changes[c].last != changes[c].last }
+ end
+ end
+end
diff --git a/modules/bim/app/services/bim/ifc_models/set_attributes_service.rb b/modules/bim/app/services/bim/ifc_models/set_attributes_service.rb
index 1cb8a7b10e5..80661c043bb 100644
--- a/modules/bim/app/services/bim/ifc_models/set_attributes_service.rb
+++ b/modules/bim/app/services/bim/ifc_models/set_attributes_service.rb
@@ -38,7 +38,7 @@ module Bim
super
- change_by_system do
+ model.change_by_system do
model.uploader = model.ifc_attachment&.author if model.ifc_attachment&.new_record? || model.ifc_attachment&.pending_direct_upload?
end
end
diff --git a/modules/bim/spec/contracts/ifc_models/create_contract_spec.rb b/modules/bim/spec/contracts/ifc_models/create_contract_spec.rb
index 1033bc786f3..6c312b162b8 100644
--- a/modules/bim/spec/contracts/ifc_models/create_contract_spec.rb
+++ b/modules/bim/spec/contracts/ifc_models/create_contract_spec.rb
@@ -36,14 +36,16 @@ describe Bim::IfcModels::CreateContract do
let(:ifc_model) do
::Bim::IfcModels::IfcModel.new(project: model_project,
title: model_title,
- uploader: model_user)
+ uploader: model_user).tap do |m|
+ m.extend(OpenProject::ChangedBySystem)
+ m.changed_by_system("uploader_id" => [nil, model_user.id])
+ end
end
let(:permissions) { %i(manage_ifc_models) }
let(:other_user) { FactoryBot.build_stubbed(:user) }
- let(:changed_by_system) { %w(uploader_id) }
subject(:contract) do
- described_class.new(ifc_model, current_user, options: { changed_by_system: changed_by_system })
+ described_class.new(ifc_model, current_user, options: {})
end
end
end
diff --git a/modules/bim/spec/contracts/ifc_models/update_contract_spec.rb b/modules/bim/spec/contracts/ifc_models/update_contract_spec.rb
index 8f59b818a19..afa42d0ceda 100644
--- a/modules/bim/spec/contracts/ifc_models/update_contract_spec.rb
+++ b/modules/bim/spec/contracts/ifc_models/update_contract_spec.rb
@@ -33,15 +33,20 @@ require_relative './shared_contract_examples'
describe Bim::IfcModels::UpdateContract do
it_behaves_like 'ifc model contract' do
+ subject(:contract) { described_class.new(ifc_model, current_user) }
+
let(:ifc_model) do
FactoryBot.build_stubbed(:ifc_model,
uploader: model_user,
title: model_title,
project: model_project).tap do |model|
- model.uploader = uploader_user
+ model.extend(OpenProject::ChangedBySystem)
+
+ model.change_by_system do
+ model.uploader = uploader_user
+ end
end
end
- subject(:contract) { described_class.new(ifc_model, current_user, options: { changed_by_system: %w(uploader_id) }) }
let(:permissions) { %i(manage_ifc_models) }
let(:uploader_user) { model_user }
diff --git a/modules/bim/spec/services/bcf/viewpoints/set_attributes_service_spec.rb b/modules/bim/spec/services/bcf/viewpoints/set_attributes_service_spec.rb
index d9297c22b24..7bfc3783d4b 100644
--- a/modules/bim/spec/services/bcf/viewpoints/set_attributes_service_spec.rb
+++ b/modules/bim/spec/services/bcf/viewpoints/set_attributes_service_spec.rb
@@ -37,7 +37,7 @@ describe Bim::Bcf::Viewpoints::SetAttributesService, type: :model do
allow(contract)
.to receive(:new)
- .with(viewpoint, user, options: { changed_by_system: [] })
+ .with(viewpoint, user, options: {})
.and_return(contract_instance)
contract
diff --git a/modules/bim/spec/services/ifc_models/set_attributes_service_spec.rb b/modules/bim/spec/services/ifc_models/set_attributes_service_spec.rb
index 6edd7de896f..533d859149c 100644
--- a/modules/bim/spec/services/ifc_models/set_attributes_service_spec.rb
+++ b/modules/bim/spec/services/ifc_models/set_attributes_service_spec.rb
@@ -38,12 +38,11 @@ describe Bim::IfcModels::SetAttributesService, type: :model do
allow(contract)
.to receive(:new)
- .with(model, user, options: { changed_by_system: changed_by_system })
+ .with(model, user, options: {})
.and_return(contract_instance)
contract
end
- let(:changed_by_system) { [] }
let(:contract_instance) do
double('contract_instance', validate: contract_valid, errors: contract_errors)
end
@@ -113,8 +112,6 @@ describe Bim::IfcModels::SetAttributesService, type: :model do
end
context 'with an ifc_attachment' do
- let(:changed_by_system) { %w(uploader_id) }
-
let(:call_attributes) do
{
ifc_attachment: ifc_file
@@ -143,8 +140,6 @@ describe Bim::IfcModels::SetAttributesService, type: :model do
context 'for an existing model' do
context 'with an ifc_attachment' do
- let(:changed_by_system) { %w(uploader_id) }
-
let(:call_attributes) do
{
ifc_attachment: ifc_file
diff --git a/modules/costs/app/services/time_entries/set_attributes_service.rb b/modules/costs/app/services/time_entries/set_attributes_service.rb
index 38e1d26645c..e5edceeb7c4 100644
--- a/modules/costs/app/services/time_entries/set_attributes_service.rb
+++ b/modules/costs/app/services/time_entries/set_attributes_service.rb
@@ -49,7 +49,7 @@ module TimeEntries
end
def set_default_user
- change_by_system do
+ model.change_by_system do
model.user = user
end
end
diff --git a/modules/costs/spec/contracts/time_entries/create_contract_spec.rb b/modules/costs/spec/contracts/time_entries/create_contract_spec.rb
index 662c8221b6b..7cc87275fdf 100644
--- a/modules/costs/spec/contracts/time_entries/create_contract_spec.rb
+++ b/modules/costs/spec/contracts/time_entries/create_contract_spec.rb
@@ -33,6 +33,10 @@ require_relative './shared_contract_examples'
describe TimeEntries::CreateContract do
it_behaves_like 'time entry contract' do
+ subject(:contract) do
+ described_class.new(time_entry, current_user)
+ end
+
let(:time_entry) do
TimeEntry.new(project: time_entry_project,
work_package: time_entry_work_package,
@@ -40,14 +44,19 @@ describe TimeEntries::CreateContract do
activity: time_entry_activity,
spent_on: time_entry_spent_on,
hours: time_entry_hours,
- comments: time_entry_comments)
+ comments: time_entry_comments).tap do |t|
+ t.extend(OpenProject::ChangedBySystem)
+ t.changed_by_system(changed_by_system) if changed_by_system
+ end
end
let(:permissions) { %i(log_time) }
let(:other_user) { FactoryBot.build_stubbed(:user) }
- let(:changed_by_system) { %w(user_id) }
-
- subject(:contract) do
- described_class.new(time_entry, current_user, options: { changed_by_system: changed_by_system })
+ let(:changed_by_system) do
+ if time_entry_user
+ { "user_id" => [nil, time_entry_user.id] }
+ else
+ {}
+ end
end
context 'if user is not allowed to log time' do
@@ -68,7 +77,7 @@ describe TimeEntries::CreateContract do
context 'if time_entry user was not set by system' do
let(:time_entry_user) { other_user }
- let(:changed_by_system) { %w() }
+ let(:changed_by_system) { {} }
it 'is invalid' do
expect_valid(false, user_id: %i(not_current_user error_readonly))
diff --git a/modules/costs/spec/services/time_entries/set_attributes_service_spec.rb b/modules/costs/spec/services/time_entries/set_attributes_service_spec.rb
index d8f0bb4bef5..c28625c37ef 100644
--- a/modules/costs/spec/services/time_entries/set_attributes_service_spec.rb
+++ b/modules/costs/spec/services/time_entries/set_attributes_service_spec.rb
@@ -61,7 +61,7 @@ describe TimeEntries::SetAttributesService, type: :model do
let(:contract_class) do
allow(TimeEntries::CreateContract)
.to receive(:new)
- .with(anything, user, options: { changed_by_system: ["user_id"] })
+ .with(anything, user, options: {})
.and_return(contract_instance)
TimeEntries::CreateContract
@@ -97,8 +97,8 @@ describe TimeEntries::SetAttributesService, type: :model do
it 'notes the user to be system changed' do
subject
- expect(instance.changed_by_system)
- .to include('user_id')
+ expect(time_entry_instance.changed_by_system['user_id'])
+ .to eql [nil, user.id]
end
it 'assigns the default TimeEntryActivity' do
diff --git a/modules/grids/spec/services/grids/set_attributes_service_spec.rb b/modules/grids/spec/services/grids/set_attributes_service_spec.rb
index 6e6f5725895..6003d17e993 100644
--- a/modules/grids/spec/services/grids/set_attributes_service_spec.rb
+++ b/modules/grids/spec/services/grids/set_attributes_service_spec.rb
@@ -37,7 +37,7 @@ describe Grids::SetAttributesService, type: :model do
allow(contract)
.to receive(:new)
- .with(grid, user, options: { changed_by_system: [] })
+ .with(grid, user, options: {})
.and_return(contract_instance)
contract
diff --git a/spec/contracts/custom_fields/create_contract_spec.rb b/spec/contracts/custom_fields/create_contract_spec.rb
index 788d3fab15d..ffddb5b60ed 100644
--- a/spec/contracts/custom_fields/create_contract_spec.rb
+++ b/spec/contracts/custom_fields/create_contract_spec.rb
@@ -36,7 +36,7 @@ describe CustomFields::CreateContract do
let(:cf) { FactoryBot.build :project_custom_field }
let(:contract) do
- described_class.new(cf, current_user, options: { changed_by_system: [] })
+ described_class.new(cf, current_user, options: {})
end
it_behaves_like 'contract is valid for active admins and invalid for regular users'
diff --git a/spec/contracts/custom_fields/update_contract_spec.rb b/spec/contracts/custom_fields/update_contract_spec.rb
index e8bc50d8bd9..e32ced1b14b 100644
--- a/spec/contracts/custom_fields/update_contract_spec.rb
+++ b/spec/contracts/custom_fields/update_contract_spec.rb
@@ -36,7 +36,7 @@ describe CustomFields::UpdateContract do
let(:cf) { FactoryBot.build :project_custom_field }
let(:contract) do
- described_class.new(cf, current_user, options: { changed_by_system: [] })
+ described_class.new(cf, current_user)
end
it_behaves_like 'contract is valid for active admins and invalid for regular users'
diff --git a/spec/contracts/messages/create_contract_spec.rb b/spec/contracts/messages/create_contract_spec.rb
index be075e007c2..f3a33f5e22f 100644
--- a/spec/contracts/messages/create_contract_spec.rb
+++ b/spec/contracts/messages/create_contract_spec.rb
@@ -41,12 +41,14 @@ describe Messages::CreateContract do
author: message_author,
last_reply: message_last_reply,
locked: message_locked,
- sticky: message_sticky)
+ sticky: message_sticky).tap do |m|
+ m.extend(OpenProject::ChangedBySystem)
+ m.changed_by_system("author_id" => [nil, message_author.id])
+ end
end
- let(:changed_by_system) { %w(author_id) }
subject(:contract) do
- described_class.new(message, current_user, options: { changed_by_system: changed_by_system })
+ described_class.new(message, current_user)
end
end
end
diff --git a/spec/contracts/wiki_pages/create_contract_spec.rb b/spec/contracts/wiki_pages/create_contract_spec.rb
index 6869f3184f5..587435348c2 100644
--- a/spec/contracts/wiki_pages/create_contract_spec.rb
+++ b/spec/contracts/wiki_pages/create_contract_spec.rb
@@ -31,6 +31,8 @@ require_relative './shared_contract_examples'
describe WikiPages::CreateContract do
it_behaves_like 'wiki page contract' do
+ subject(:contract) { described_class.new(page, current_user, options: {}) }
+
let(:page) do
WikiPage.new(wiki: page_wiki,
title: page_title,
@@ -39,14 +41,22 @@ describe WikiPages::CreateContract do
parent: page_parent).tap do |page|
page.build_content text: page_text,
author: page_author
+ page.content.extend(OpenProject::ChangedBySystem)
+ page.content.changed_by_system(changed_by_system)
+
allow(page)
.to receive(:project)
.and_return(page_wiki&.project)
end
end
- let(:changed_by_system) { %w(author_id) }
- subject(:contract) { described_class.new(page, current_user, options: { changed_by_system: changed_by_system }) }
+ let(:changed_by_system) do
+ if page_author
+ { "author_id" => [nil, page_author.id] }
+ else
+ {}
+ end
+ end
describe '#validation' do
context 'if the author is different from the current user' do
@@ -58,7 +68,7 @@ describe WikiPages::CreateContract do
end
context 'if the author was not set by system' do
- let(:changed_by_system) { %w() }
+ let(:changed_by_system) { {} }
it 'is invalid' do
expect_valid(false, author_id: %i(error_readonly))
diff --git a/spec/contracts/work_packages/create_contract_spec.rb b/spec/contracts/work_packages/create_contract_spec.rb
index 9a814c013e7..62b6413e4d5 100644
--- a/spec/contracts/work_packages/create_contract_spec.rb
+++ b/spec/contracts/work_packages/create_contract_spec.rb
@@ -32,7 +32,11 @@ require 'spec_helper'
require 'contracts/work_packages/shared_base_contract'
describe WorkPackages::CreateContract do
- let(:work_package) { WorkPackage.new project: work_package_project }
+ let(:work_package) do
+ WorkPackage.new(project: work_package_project).tap do |wp|
+ wp.extend(OpenProject::ChangedBySystem)
+ end
+ end
let(:work_package_project) { project }
let(:project) { FactoryBot.build_stubbed(:project) }
let(:user) { FactoryBot.build_stubbed(:user) }
@@ -122,10 +126,12 @@ describe WorkPackages::CreateContract do
end
context 'if the user is set by the system and the user is the user the contract is evaluated for' do
- subject(:contract) { described_class.new(work_package, user, options: { changed_by_system: ['author_id'] }) }
+ subject(:contract) { described_class.new(work_package, user) }
it 'is valid' do
- work_package.author = user
+ work_package.change_by_system do
+ work_package.author = user
+ end
expect(validated_contract.errors[:author_id]).to be_empty
end
diff --git a/spec/lib/open_project/changed_by_system_spec.rb b/spec/lib/open_project/changed_by_system_spec.rb
new file mode 100644
index 00000000000..3181cc01274
--- /dev/null
+++ b/spec/lib/open_project/changed_by_system_spec.rb
@@ -0,0 +1,93 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+require 'spec_helper'
+
+describe OpenProject::ChangedBySystem do
+ subject(:model) do
+ model = News.new
+ model.extend(described_class)
+ model
+ end
+
+ describe '#changed_by_user' do
+ context 'when an attribute is changed' do
+ before do
+ model.title = 'abc'
+ end
+
+ it 'returns the attribute' do
+ expect(model.changed_by_user)
+ .to match_array ['title']
+ end
+ end
+
+ context 'when an attribute is changed by the system' do
+ before do
+ model.change_by_system do
+ model.title = 'abc'
+ end
+ end
+
+ it 'returns no attributes' do
+ expect(model.changed_by_user)
+ .to be_empty
+ end
+ end
+
+ context 'when an attribute is changed by the system first and then by the user to a different value' do
+ before do
+ model.change_by_system do
+ model.title = 'abc'
+ end
+
+ model.title = 'xyz'
+ end
+
+ it 'returns the attribute' do
+ expect(model.changed_by_user)
+ .to match_array ['title']
+ end
+ end
+
+ context 'when an attribute is changed by the system first and then by the user to the same value' do
+ before do
+ model.change_by_system do
+ model.title = 'abc'
+ end
+
+ model.title = 'abc'
+ end
+
+ it 'returns no attribute' do
+ expect(model.changed_by_user)
+ .to be_empty
+ end
+ end
+ end
+end
diff --git a/spec/services/custom_actions/update_work_package_service_spec.rb b/spec/services/custom_actions/update_work_package_service_spec.rb
index c7ddfcb6741..e390069dd7e 100644
--- a/spec/services/custom_actions/update_work_package_service_spec.rb
+++ b/spec/services/custom_actions/update_work_package_service_spec.rb
@@ -94,7 +94,7 @@ describe CustomActions::UpdateWorkPackageService do
allow(WorkPackages::UpdateContract)
.to receive(:new)
- .with(work_package, user, options: { changed_by_system: [] })
+ .with(work_package, user, options: {})
.and_return(contract)
allow(contract)
diff --git a/spec/services/custom_fields/set_attributes_service_spec.rb b/spec/services/custom_fields/set_attributes_service_spec.rb
index 61cc9fa2fa2..38d352ec1b5 100644
--- a/spec/services/custom_fields/set_attributes_service_spec.rb
+++ b/spec/services/custom_fields/set_attributes_service_spec.rb
@@ -55,7 +55,7 @@ describe CustomFields::SetAttributesService, type: :model do
let(:contract_class) do
allow(CustomFields::CreateContract)
.to receive(:new)
- .with(cf_instance, user, options: { changed_by_system: [] })
+ .with(cf_instance, user, options: {})
.and_return(contract_instance)
CustomFields::CreateContract
diff --git a/spec/services/groups/set_attributes_service_spec.rb b/spec/services/groups/set_attributes_service_spec.rb
index d51ddb537bf..db2f0f48283 100644
--- a/spec/services/groups/set_attributes_service_spec.rb
+++ b/spec/services/groups/set_attributes_service_spec.rb
@@ -39,7 +39,7 @@ describe Groups::SetAttributesService, type: :model do
allow(contract)
.to receive(:new)
- .with(group, user, options: { changed_by_system: [] })
+ .with(group, user, options: {})
.and_return(contract_instance)
contract
diff --git a/spec/services/members/set_attributes_service_spec.rb b/spec/services/members/set_attributes_service_spec.rb
index 84851762336..83ae36c5edc 100644
--- a/spec/services/members/set_attributes_service_spec.rb
+++ b/spec/services/members/set_attributes_service_spec.rb
@@ -37,7 +37,7 @@ describe Members::SetAttributesService, type: :model do
allow(contract)
.to receive(:new)
- .with(member, user, options: { changed_by_system: [] })
+ .with(member, user, options: {})
.and_return(contract_instance)
contract
diff --git a/spec/services/messages/set_attributes_service_spec.rb b/spec/services/messages/set_attributes_service_spec.rb
index 7bb174cadf3..4dd7e315acd 100644
--- a/spec/services/messages/set_attributes_service_spec.rb
+++ b/spec/services/messages/set_attributes_service_spec.rb
@@ -56,7 +56,7 @@ describe Messages::SetAttributesService, type: :model do
let(:contract_class) do
allow(Messages::CreateContract)
.to receive(:new)
- .with(message_instance, user, options: { changed_by_system: ["author_id"] })
+ .with(message_instance, user, options: {})
.and_return(contract_instance)
Messages::CreateContract
@@ -92,8 +92,8 @@ describe Messages::SetAttributesService, type: :model do
it 'notes the author to be system changed' do
subject
- expect(instance.changed_by_system)
- .to include('author_id')
+ expect(message_instance.changed_by_system['author_id'])
+ .to eql [nil, user.id]
end
context 'with params' do
diff --git a/spec/services/projects/schedule_deletion_service_spec.rb b/spec/services/projects/schedule_deletion_service_spec.rb
index 59e86dfd506..509f1550813 100644
--- a/spec/services/projects/schedule_deletion_service_spec.rb
+++ b/spec/services/projects/schedule_deletion_service_spec.rb
@@ -34,7 +34,7 @@ describe ::Projects::ScheduleDeletionService, type: :model do
allow(contract)
.to receive(:new)
- .with(project, user, options: { changed_by_system: [] })
+ .with(project, user, options: {})
.and_return(contract_instance)
contract
diff --git a/spec/services/projects/set_attributes_service_spec.rb b/spec/services/projects/set_attributes_service_spec.rb
index cb0dfe14af8..7ae48b932a2 100644
--- a/spec/services/projects/set_attributes_service_spec.rb
+++ b/spec/services/projects/set_attributes_service_spec.rb
@@ -37,7 +37,7 @@ describe Projects::SetAttributesService, type: :model do
allow(contract)
.to receive(:new)
- .with(project, user, options: { changed_by_system: [] })
+ .with(project, user, options: {})
.and_return(contract_instance)
contract
diff --git a/spec/services/relations/create_service_spec.rb b/spec/services/relations/create_service_spec.rb
index b786a1558da..774a29295a8 100644
--- a/spec/services/relations/create_service_spec.rb
+++ b/spec/services/relations/create_service_spec.rb
@@ -86,7 +86,7 @@ describe Relations::CreateService do
allow(Relations::CreateContract)
.to receive(:new)
- .with(relation, user, options: { changed_by_system: [] })
+ .with(relation, user, options: {})
.and_return(contract)
allow(contract)
.to receive(:validate)
diff --git a/spec/services/relations/update_service_spec.rb b/spec/services/relations/update_service_spec.rb
index a6498819864..a25afb78863 100644
--- a/spec/services/relations/update_service_spec.rb
+++ b/spec/services/relations/update_service_spec.rb
@@ -86,7 +86,7 @@ describe Relations::UpdateService do
allow(Relations::UpdateContract)
.to receive(:new)
- .with(relation, user, options: { changed_by_system: [] })
+ .with(relation, user, options: {})
.and_return(contract)
allow(contract)
.to receive(:validate)
diff --git a/spec/services/wiki_pages/set_attributes_service_spec.rb b/spec/services/wiki_pages/set_attributes_service_spec.rb
index f7821bab814..885eb1252ec 100644
--- a/spec/services/wiki_pages/set_attributes_service_spec.rb
+++ b/spec/services/wiki_pages/set_attributes_service_spec.rb
@@ -37,7 +37,7 @@ describe WikiPages::SetAttributesService, type: :model do
allow(contract)
.to receive(:new)
- .with(wiki_page, user, options: { changed_by_system: [] })
+ .with(wiki_page, user, options: {})
.and_return(contract_instance)
contract
@@ -106,5 +106,25 @@ describe WikiPages::SetAttributesService, type: :model do
subject
end
end
+
+ context 'for a new wiki page' do
+ let(:wiki_page) do
+ WikiPage.new
+ end
+
+ it 'initializes the content with the user being the author' do
+ subject
+
+ expect(wiki_page.content.author)
+ .to eql user
+ end
+
+ it 'marks the content author to be system changed' do
+ subject
+
+ expect(wiki_page.content.changed_by_system['author_id'])
+ .to eql [nil, user.id]
+ end
+ end
end
end
diff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rb
index eaecc58a221..79fae652c37 100644
--- a/spec/services/work_packages/set_attributes_service_spec.rb
+++ b/spec/services/work_packages/set_attributes_service_spec.rb
@@ -40,7 +40,7 @@ describe WorkPackages::SetAttributesService, type: :model do
end
let(:work_package) do
wp = FactoryBot.build_stubbed(:work_package, project: project)
- wp.type = FactoryBot.build_stubbed(:type)
+ wp.type = initial_type
wp.send(:clear_changes_information)
wp
@@ -48,6 +48,7 @@ describe WorkPackages::SetAttributesService, type: :model do
let(:new_work_package) do
WorkPackage.new
end
+ let(:initial_type) { FactoryBot.build_stubbed(:type) }
let(:statuses) { [] }
let(:contract_class) { WorkPackages::UpdateContract }
let(:mock_contract) do
@@ -222,8 +223,8 @@ describe WorkPackages::SetAttributesService, type: :model do
it 'notes the author to be system changed' do
subject
- expect(instance.changed_by_system)
- .to include('author_id')
+ expect(work_package.changed_by_system['author_id'])
+ .to eql [0, user.id]
end
end
end
@@ -756,8 +757,8 @@ describe WorkPackages::SetAttributesService, type: :model do
it 'adds change to system changes' do
subject
- expect(instance.changed_by_system)
- .to include('category_id')
+ expect(work_package.changed_by_system['category_id'])
+ .to eql [nil, new_category.id]
end
end
end
@@ -785,8 +786,8 @@ describe WorkPackages::SetAttributesService, type: :model do
it 'adds change to system changes' do
subject
- expect(instance.changed_by_system)
- .to include('type_id')
+ expect(work_package.changed_by_system['type_id'])
+ .to eql [initial_type.id, default_type.id]
end
end
@@ -803,8 +804,8 @@ describe WorkPackages::SetAttributesService, type: :model do
it 'adds change to system changes' do
subject
- expect(instance.changed_by_system)
- .to include('type_id')
+ expect(work_package.changed_by_system['type_id'])
+ .to eql [initial_type.id, other_type.id]
end
end
@@ -821,7 +822,7 @@ describe WorkPackages::SetAttributesService, type: :model do
it 'does not set the change to system changes' do
subject
- expect(instance.changed_by_system)
+ expect(work_package.changed_by_system)
.not_to include('type_id')
end
end