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

Add tests for JournalFormatter::Cause

Klaus Zandersaae92355dbea
diff --git a/lib/open_project/journal_formatter/cause.rb b/lib/open_project/journal_formatter/cause.rb
index 7d0c37d6bb8..b24a2568ae6 100644
--- a/lib/open_project/journal_formatter/cause.rb
+++ b/lib/open_project/journal_formatter/cause.rb
@@ -91,7 +91,8 @@ class OpenProject::JournalFormatter::Cause < JournalFormatter::Base
end
def working_date_change_message(date, working)
- I18n.t("journals.cause_descriptions.working_days_changed.dates.#{working ? :working : :non_working}", date:)
+ I18n.t("journals.cause_descriptions.working_days_changed.dates.#{working ? :working : :non_working}",
+ date: I18n.l(Date.parse(date)))
end
# we need to tell the url_helper that there is not controller to get url_options
diff --git a/spec/lib/journal_formatter/cause_spec.rb b/spec/lib/journal_formatter/cause_spec.rb
new file mode 100644
index 00000000000..29de9a5adfd
--- /dev/null
+++ b/spec/lib/journal_formatter/cause_spec.rb
@@ -0,0 +1,261 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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.
+#++
+
+require 'spec_helper'
+
+RSpec.describe OpenProject::JournalFormatter::Cause do
+ include ApplicationHelper
+ include WorkPackagesHelper
+ include ActionView::Helpers::UrlHelper
+ include Rails.application.routes.url_helpers
+
+ let(:work_package) { build_stubbed(:work_package) }
+ let(:instance) { described_class.new(build(:work_package_journal)) }
+ let(:link) do
+ link_to_work_package(work_package, all_link: true)
+ end
+
+ # we need to tell the url_helper that there is not controller to get url_options so that we can call link_to
+ let(:controller) { nil }
+
+ subject { instance.render("cause", [nil, cause], html:) }
+
+ context 'when the change was caused by a change to the parent' do
+ let(:cause) do
+ {
+ "type" => "work_package_parent_changed_times",
+ "work_package_id" => work_package.id
+ }
+ end
+
+ context 'when rendering HTML variant' do
+ let(:html) { true }
+
+ context 'when the user is able to access the related work package' do
+ before { allow(WorkPackage).to receive_message_chain(:includes, :visible, :find_by).and_return(work_package) }
+
+ it do
+ expect(subject).to eq "<strong>#{I18n.t('journals.caused_changes.dates_changed')}</strong> " \
+ "#{I18n.t('journals.cause_descriptions.work_package_parent_changed_times', link:)}"
+ end
+ end
+
+ context 'when the user is not able to access the related work package' do
+ before { allow(WorkPackage).to receive_message_chain(:includes, :visible, :find_by).and_return(nil) }
+
+ it do
+ expect(subject).to eq "<strong>#{I18n.t('journals.caused_changes.dates_changed')}</strong> " \
+ "#{I18n.t('journals.cause_descriptions.unaccessable_work_package_changed')}"
+ end
+ end
+ end
+
+ context 'when rendering raw variant' do
+ let(:html) { false }
+
+ context 'when the user is able to access the related work package' do
+ before { allow(WorkPackage).to receive_message_chain(:includes, :visible, :find_by).and_return(work_package) }
+
+ let(:link) { "##{work_package.id}" }
+
+ it do
+ expect(subject).to eq "#{I18n.t('journals.caused_changes.dates_changed')} " \
+ "#{I18n.t('journals.cause_descriptions.work_package_parent_changed_times', link:)}"
+ end
+ end
+
+ context 'when the user is not able to access the related work package' do
+ before { allow(WorkPackage).to receive_message_chain(:includes, :visible, :find_by).and_return(nil) }
+
+ it do
+ expect(subject).to eq "#{I18n.t('journals.caused_changes.dates_changed')} " \
+ "#{I18n.t('journals.cause_descriptions.unaccessable_work_package_changed')}"
+ end
+ end
+ end
+ end
+
+ context 'when the change was caused by a change to a predecessor' do
+ let(:cause) do
+ {
+ "type" => "work_package_predecessor_changed_times",
+ "work_package_id" => work_package.id
+ }
+ end
+
+ context 'when rendering HTML variant' do
+ let(:html) { true }
+
+ context 'when the user is able to access the related work package' do
+ before { allow(WorkPackage).to receive_message_chain(:includes, :visible, :find_by).and_return(work_package) }
+
+ it do
+ expect(subject).to eq "<strong>#{I18n.t('journals.caused_changes.dates_changed')}</strong> " \
+ "#{I18n.t('journals.cause_descriptions.work_package_predecessor_changed_times', link:)}"
+ end
+ end
+
+ context 'when the user is not able to access the related work package' do
+ before { allow(WorkPackage).to receive_message_chain(:includes, :visible, :find_by).and_return(nil) }
+
+ it do
+ expect(subject).to eq "<strong>#{I18n.t('journals.caused_changes.dates_changed')}</strong> " \
+ "#{I18n.t('journals.cause_descriptions.unaccessable_work_package_changed')}"
+ end
+ end
+ end
+
+ context 'when rendering raw variant' do
+ let(:html) { false }
+
+ context 'when the user is able to access the related work package' do
+ before { allow(WorkPackage).to receive_message_chain(:includes, :visible, :find_by).and_return(work_package) }
+
+ let(:link) { "##{work_package.id}" }
+
+ it do
+ expect(subject).to eq "#{I18n.t('journals.caused_changes.dates_changed')} " \
+ "#{I18n.t('journals.cause_descriptions.work_package_predecessor_changed_times', link:)}"
+ end
+ end
+
+ context 'when the user is not able to access the related work package' do
+ before { allow(WorkPackage).to receive_message_chain(:includes, :visible, :find_by).and_return(nil) }
+
+ it do
+ expect(subject).to eq "#{I18n.t('journals.caused_changes.dates_changed')} " \
+ "#{I18n.t('journals.cause_descriptions.unaccessable_work_package_changed')}"
+ end
+ end
+ end
+ end
+
+ context 'when the change was caused by a change to a child' do
+ let(:cause) do
+ {
+ "type" => "work_package_children_changed_times",
+ "work_package_id" => work_package.id
+ }
+ end
+
+ context 'when rendering HTML variant' do
+ let(:html) { true }
+
+ context 'when the user is able to access the related work package' do
+ before { allow(WorkPackage).to receive_message_chain(:includes, :visible, :find_by).and_return(work_package) }
+
+ it do
+ expect(subject).to eq "<strong>#{I18n.t('journals.caused_changes.dates_changed')}</strong> " \
+ "#{I18n.t('journals.cause_descriptions.work_package_children_changed_times', link:)}"
+ end
+ end
+
+ context 'when the user is not able to access the related work package' do
+ before { allow(WorkPackage).to receive_message_chain(:includes, :visible, :find_by).and_return(nil) }
+
+ it do
+ expect(subject).to eq "<strong>#{I18n.t('journals.caused_changes.dates_changed')}</strong> " \
+ "#{I18n.t('journals.cause_descriptions.unaccessable_work_package_changed')}"
+ end
+ end
+ end
+
+ context 'when rendering raw variant' do
+ let(:html) { false }
+
+ context 'when the user is able to access the related work package' do
+ before { allow(WorkPackage).to receive_message_chain(:includes, :visible, :find_by).and_return(work_package) }
+
+ let(:link) { "##{work_package.id}" }
+
+ it do
+ expect(subject).to eq "#{I18n.t('journals.caused_changes.dates_changed')} " \
+ "#{I18n.t('journals.cause_descriptions.work_package_children_changed_times', link:)}"
+ end
+ end
+
+ context 'when the user is not able to access the related work package' do
+ before { allow(WorkPackage).to receive_message_chain(:includes, :visible, :find_by).and_return(nil) }
+
+ it do
+ expect(subject).to eq "#{I18n.t('journals.caused_changes.dates_changed')} " \
+ "#{I18n.t('journals.cause_descriptions.unaccessable_work_package_changed')}"
+ end
+ end
+ end
+ end
+
+ context 'when the change was caused by working day changes' do
+ let(:cause) do
+ {
+ "type" => 'working_days_changed',
+ "changed_days" => {
+ "working_days" => {
+ "2" => false,
+ "6" => true
+
+ },
+ "non_working_days" => {
+ "2023-01-01" => true,
+ "2023-12-24" => false
+ }
+ }
+ }
+ end
+
+ context 'when rendering HTML variant' do
+ let(:html) { true }
+
+ it do
+ changes = [
+ I18n.t("journals.cause_descriptions.working_days_changed.days.non_working", day: WeekDay.find_by!(day: 2).name),
+ I18n.t("journals.cause_descriptions.working_days_changed.days.working", day: WeekDay.find_by!(day: 6).name),
+ I18n.t("journals.cause_descriptions.working_days_changed.dates.working", date: I18n.l(Date.new(2023, 1, 1))),
+ I18n.t("journals.cause_descriptions.working_days_changed.dates.non_working", date: I18n.l(Date.new(2023, 12, 24)))
+ ].join(", ")
+ expect(subject).to eq "<strong>#{I18n.t('journals.caused_changes.dates_changed')}</strong> " \
+ "#{I18n.t('journals.cause_descriptions.working_days_changed.changed', changes:)}"
+ end
+ end
+
+ context 'when rendering raw variant' do
+ let(:html) { false }
+
+ it do
+ changes = [
+ I18n.t("journals.cause_descriptions.working_days_changed.days.non_working", day: WeekDay.find_by!(day: 2).name),
+ I18n.t("journals.cause_descriptions.working_days_changed.days.working", day: WeekDay.find_by!(day: 6).name),
+ I18n.t("journals.cause_descriptions.working_days_changed.dates.working", date: I18n.l(Date.new(2023, 1, 1))),
+ I18n.t("journals.cause_descriptions.working_days_changed.dates.non_working", date: I18n.l(Date.new(2023, 12, 24)))
+ ].join(", ")
+ expect(subject).to eq "#{I18n.t('journals.caused_changes.dates_changed')} " \
+ "#{I18n.t('journals.cause_descriptions.working_days_changed.changed', changes:)}"
+ end
+ end
+ end
+end