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
Fix replacing references of presenter_id
Oliver Günther1f4d3a34620f
diff --git a/app/services/principals/replace_references_service.rb b/app/services/principals/replace_references_service.rbindex d74dcd0b1dc..e1c0810a49c 100644--- a/app/services/principals/replace_references_service.rb+++ b/app/services/principals/replace_references_service.rb@@ -49,6 +49,7 @@ module Principalsrewrite_actor(from, to)rewrite_owner(from, to)rewrite_logged_by(from, to)+ rewrite_presenter(from, to)enddef rewrite_custom_value(from, to)@@ -135,12 +136,20 @@ module Principalsendend+ def rewrite_presenter(from, to)+ [+ MeetingAgendaItem+ ].each do |klass|+ rewrite(klass, :presenter_id, from, to)+ end+ end+def journal_classes[Journal] + Journal::BaseJournal.subclassesenddef foreign_keys- %w[author_id user_id assigned_to_id responsible_id logged_by_id]+ %w[author_id user_id assigned_to_id responsible_id logged_by_id presenter_id]enddef rewrite(klass, attribute, from, to)diff --git a/modules/meeting/spec/services/principals/replace_references_service_call_integration_spec.rb b/modules/meeting/spec/services/principals/replace_references_service_call_integration_spec.rbindex 1a46851cc7d..aa6c6ff398d 100644--- a/modules/meeting/spec/services/principals/replace_references_service_call_integration_spec.rb+++ b/modules/meeting/spec/services/principals/replace_references_service_call_integration_spec.rb@@ -30,38 +30,43 @@require "spec_helper"require_module_spec_helper+require Rails.root.join('spec/services/principals/replace_references_context')RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do+ subject(:service_call) { instance.call(from: principal, to: to_principal) }++ shared_let(:other_user) { create(:user) }shared_let(:principal) { create(:user) }shared_let(:to_principal) { create(:user) }- subject(:service_call) { instance.call(from: principal, to: to_principal) }-let(:instance) dodescribed_class.newend- shared_examples "replaces the creator" do- before do- model- end-- it "is successful" do- expect(service_call)- .to be_success+ context "with MeetingAgendaItem" do+ it_behaves_like "rewritten record",+ :meeting_agenda_item,+ :author_id do+ let(:attributes) do+ {+ author_id: principal.id,+ created_at: "NOW()",+ updated_at: "NOW()"+ }+ endend- it "replaces principal with to_principal" do- service_call- model.reload+ it_behaves_like "rewritten record",+ :meeting_agenda_item,+ :presenter_id do+ let(:attributes) do+ {+ presenter_id: principal.id,+ created_at: "NOW()",+ updated_at: "NOW()"+ }- expect(model.author).to eql to_principal- end- end-- context "with MeetingAgendaItem" do- it_behaves_like "replaces the creator" do- let(:model) { create(:meeting_agenda_item, author: principal) }+ endendendenddiff --git a/spec/services/principals/replace_references_context.rb b/spec/services/principals/replace_references_context.rbnew file mode 100644index 00000000000..8c8063b2675--- /dev/null+++ b/spec/services/principals/replace_references_context.rb@@ -0,0 +1,86 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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.+#++++RSpec.shared_examples_for "rewritten record" do |factory, attribute, format = Integer|+ let!(:model) do+ klass = FactoryBot.factories.find(factory).build_class+ all_attributes = other_attributes.merge(attribute => principal_id)++ inserted = ActiveRecord::Base.connection.select_one <<~SQL.squish+ INSERT INTO #{klass.table_name}+ (#{all_attributes.keys.join(', ')})+ VALUES+ (#{all_attributes.values.join(', ')})+ RETURNING id+ SQL++ klass.find(inserted["id"])+ end++ let(:other_attributes) do+ defined?(attributes) ? attributes : {}+ end++ def expected(user, format)+ if format == String+ user.id.to_s+ else+ user.id+ end+ end++ context "for #{factory}" do+ context "with the replaced user" do+ let(:principal_id) { principal.id }++ before do+ service_call+ model.reload+ end++ it "replaces #{attribute}" do+ expect(model.send(attribute))+ .to eql expected(to_principal, format)+ end+ end++ context "with a different user" do+ let(:principal_id) { other_user.id }++ before do+ service_call+ model.reload+ end++ it "keeps #{attribute}" do+ expect(model.send(attribute))+ .to eql expected(other_user, format)+ end+ end+ end+enddiff --git a/spec/services/principals/replace_references_service_call_integration_spec.rb b/spec/services/principals/replace_references_service_call_integration_spec.rbindex eed56ed882d..e10e39371e8 100644--- a/spec/services/principals/replace_references_service_call_integration_spec.rb+++ b/spec/services/principals/replace_references_service_call_integration_spec.rb@@ -27,6 +27,7 @@#++require "spec_helper"+require_relative "replace_references_context"RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model dosubject(:service_call) { instance.call(from: principal, to: to_principal) }@@ -330,8 +331,7 @@ RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model doit_behaves_like "rewritten record",:journal_wiki_page_journal,- :author_id do- end+ :author_idendcontext "with WorkPackage" do