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

ensure journal notifications value is reset

ulferts60b4b47c7518
diff --git a/app/models/journal_manager.rb b/app/models/journal_manager.rb
index 2f540cbf10a..c6012db2699 100644
--- a/app/models/journal_manager.rb
+++ b/app/models/journal_manager.rb
@@ -355,7 +355,7 @@ class JournalManager
self.send_notification = send_notifications
result = block.call
-
+ ensure
self.send_notification = old_value
result
diff --git a/spec/models/journal_manager_spec.rb b/spec/models/journal_manager_spec.rb
index 55fe9bc710b..42b7206edd5 100644
--- a/spec/models/journal_manager_spec.rb
+++ b/spec/models/journal_manager_spec.rb
@@ -160,4 +160,41 @@ describe JournalManager, type: :model do
end
end
end
+
+ describe '.with_send_notifications' do
+ let!(:send_notification_before) { described_class.send_notification }
+ let(:proc_called_counter) { OpenStruct.new called: false, send_notifications: !send_notification_before }
+ let(:proc) { Proc.new { proc_called_counter.called = true } }
+
+ it 'executes the block' do
+ described_class.with_send_notifications !send_notification_before, &proc
+
+ expect(proc_called_counter.called)
+ .to be_truthy
+ end
+
+ it 'uses the provided send_notifications value within the proc' do
+ described_class.with_send_notifications !send_notification_before, &proc
+
+ expect(proc_called_counter.send_notifications)
+ .to eql !send_notification_before
+ end
+
+ it 'resets the send_notifications to the value before' do
+ described_class.with_send_notifications !send_notification_before, &proc
+
+ expect(described_class.send_notification)
+ .to eql send_notification_before
+ end
+
+ context 'with an exception being raised within the block' do
+ it 'raises the exception but always resets the notification value' do
+ expect { described_class.with_send_notifications(!send_notification_before) { raise ArgumentError } }
+ .to raise_error ArgumentError
+
+ expect(described_class.send_notification)
+ .to eql send_notification_before
+ end
+ end
+ end
end
diff --git a/spec/models/work_package/openproject_notifications_spec.rb b/spec/models/work_package/openproject_notifications_spec.rb
index 2580e730412..0a1f7779806 100644
--- a/spec/models/work_package/openproject_notifications_spec.rb
+++ b/spec/models/work_package/openproject_notifications_spec.rb
@@ -38,9 +38,9 @@ describe WorkPackage, type: :model do
let(:project) { FactoryBot.create :project }
let(:work_package) do
FactoryBot.create :work_package,
- author: user,
- subject: 'I can see you',
- project: project
+ author: user,
+ subject: 'I can see you',
+ project: project
end
let(:journal_ids) { [] }