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

Fix/copy project wiki notifications (#8777)

ulfertsae2edad19b7f
diff --git a/app/contracts/model_contract.rb b/app/contracts/model_contract.rb
index 7db867d6dcf..6d29c237587 100644
--- a/app/contracts/model_contract.rb
+++ b/app/contracts/model_contract.rb
@@ -70,11 +70,11 @@ class ModelContract < Disposable::Twin
end
def property(name, options = {}, &block)
- if twin = options.delete(:form)
+ if (twin = options.delete(:form))
options[:twin] = twin
end
- if validates_options = options[:validates]
+ if (validates_options = options[:validates])
validates name, validates_options
end
@@ -211,7 +211,7 @@ class ModelContract < Disposable::Twin
end
def attributes_changed_by_user
- changed = model.changed
+ changed = changed_attributes
if options[:changed_by_system]
changed -= options[:changed_by_system]
@@ -220,6 +220,10 @@ class ModelContract < Disposable::Twin
changed
end
+ def changed_attributes
+ model.changed
+ end
+
def collect_ancestor_attribute_aliases
@ancestor_attribute_aliases ||= collect_ancestor_attributes(:attribute_aliases)
end
diff --git a/app/contracts/wiki_pages/base_contract.rb b/app/contracts/wiki_pages/base_contract.rb
new file mode 100644
index 00000000000..c4b442bb463
--- /dev/null
+++ b/app/contracts/wiki_pages/base_contract.rb
@@ -0,0 +1,77 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 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-2017 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.
+#++
+
+# Validates both the wiki page as well as its associated wiki content. The two are
+# considered to be one outside of this contract.
+module WikiPages
+ class BaseContract < ::ModelContract
+ attribute :wiki
+ attribute :title
+ attribute :slug
+ attribute :parent
+ attribute :text
+ attribute :protected
+
+ validate :validate_author_is_set
+ validate :validate_wiki_is_set
+ validate :validate_content_is_set
+ validate :validate_user_edit_allowed
+ validate :validate_user_protect_permission
+
+ private
+
+ def validate_user_edit_allowed
+ if model.project && !user.allowed_to?(:edit_wiki_pages, model.project) ||
+ (model.protected_was && !user.allowed_to?(:protect_wiki_pages))
+ errors.add :base, :error_unauthorized
+ end
+ end
+
+ def validate_author_is_set
+ errors.add :author, :blank if model.content&.author.nil?
+ end
+
+ def validate_wiki_is_set
+ errors.add :wiki, :blank if model.wiki.nil?
+ end
+
+ def validate_content_is_set
+ errors.add :content, :blank if model.content.nil?
+ end
+
+ def validate_user_protect_permission
+ if model.protected_changed? && !user.allowed_to?(:protect_wiki_pages, model.project)
+ errors.add :protected, :error_unauthorized
+ end
+ end
+
+ def changed_attributes
+ model.changed + (model.content&.changed || [])
+ end
+ end
+end
diff --git a/app/contracts/wiki_pages/copy_contract.rb b/app/contracts/wiki_pages/copy_contract.rb
new file mode 100644
index 00000000000..2ffab6cbef7
--- /dev/null
+++ b/app/contracts/wiki_pages/copy_contract.rb
@@ -0,0 +1,37 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 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-2017 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.
+#++
+
+module WikiPages
+ class CopyContract < CreateContract
+ # Disable check for edit_wiki_pages permission
+ def validate_user_edit_allowed; end
+
+ # Disable check for protect_wiki_pages permission
+ def validate_user_protect_permission; end
+ end
+end
diff --git a/app/contracts/wiki_pages/create_contract.rb b/app/contracts/wiki_pages/create_contract.rb
new file mode 100644
index 00000000000..a2ae99ea65a
--- /dev/null
+++ b/app/contracts/wiki_pages/create_contract.rb
@@ -0,0 +1,39 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 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-2017 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.
+#++
+
+module WikiPages
+ class CreateContract < BaseContract
+ validate :validate_user_current_user
+
+ private
+
+ def validate_user_current_user
+ errors.add :author, :not_current_user if model.content&.author != user
+ end
+ end
+end
diff --git a/app/models/journal/aggregated_journal.rb b/app/models/journal/aggregated_journal.rb
index 6ddc0a53c39..77e6692181f 100644
--- a/app/models/journal/aggregated_journal.rb
+++ b/app/models/journal/aggregated_journal.rb
@@ -76,7 +76,7 @@ class Journal::AggregatedJournal
# Returns whether "notification-hiding" should be assumed for the given journal pair.
# This leads to an aggregated journal effectively blocking notifications of an earlier journal,
# because it "steals" the addition from its predecessor. See the specs section under
- # "mail suppressing aggregation" (for EnqueueWorkPackageNotificationJob) for more details
+ # "mail suppressing aggregation" (for NotifyJournalCompletedJob) for more details
def hides_notifications?(successor, predecessor)
return false unless successor && predecessor
return false if belong_to_different_groups?(predecessor, successor)
diff --git a/app/models/wiki_content.rb b/app/models/wiki_content.rb
index 893f2af963e..58e25d0356b 100644
--- a/app/models/wiki_content.rb
+++ b/app/models/wiki_content.rb
@@ -27,9 +27,9 @@
# See docs/COPYRIGHT.rdoc for more details.
#++
-require 'zlib'
-
class WikiContent < ApplicationRecord
+ extend DeprecatedAlias
+
belongs_to :page, class_name: 'WikiPage', foreign_key: 'page_id'
belongs_to :author, class_name: 'User', foreign_key: 'author_id'
validates_length_of :comments, maximum: 255, allow_nil: true
@@ -37,8 +37,6 @@ class WikiContent < ApplicationRecord
attr_accessor :comments
before_save :comments_to_journal_notes
- after_create :send_content_added_mail
- after_update :send_content_updated_mail, if: :saved_change_to_text?
acts_as_journalized
@@ -76,16 +74,7 @@ class WikiContent < ApplicationRecord
super value.presence || ''
end
- # Returns the mail adresses of users that should be notified
- def recipients
- notified = project.notified_users
- notified.select { |user| visible?(user) }
- end
-
- # FIXME: Deprecate
- def versions
- journals
- end
+ deprecated_alias :versions, :journals
# REVIEW
def version
@@ -97,31 +86,4 @@ class WikiContent < ApplicationRecord
def comments_to_journal_notes
add_journal author, comments
end
-
- def send_content_added_mail
- return unless Setting.notified_events.include?('wiki_content_added')
-
- create_recipients.uniq.each do |user|
- UserMailer.wiki_content_added(user, self, User.current).deliver_later
- end
- end
-
- def send_content_updated_mail
- return unless Setting.notified_events.include?('wiki_content_updated')
-
- update_recipients.uniq.each do |user|
- UserMailer.wiki_content_updated(user, self, User.current).deliver_later
- end
- end
-
- def create_recipients
- recipients +
- page.wiki.watcher_recipients
- end
-
- def update_recipients
- recipients +
- page.wiki.watcher_recipients +
- page.watcher_recipients
- end
end
diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb
index c4b4157085a..861e64d01ae 100644
--- a/app/models/wiki_page.rb
+++ b/app/models/wiki_page.rb
@@ -217,11 +217,7 @@ class WikiPage < ApplicationRecord
end
def breadcrumb_title
- if item = menu_item
- item.title
- else
- title
- end
+ menu_item&.title || title
end
def to_param
diff --git a/app/services/concerns/contracted.rb b/app/services/concerns/contracted.rb
index 73a169bf63b..df46b047471 100644
--- a/app/services/concerns/contracted.rb
+++ b/app/services/concerns/contracted.rb
@@ -103,7 +103,11 @@ module Contracted
end
def changed_compared_to(prior_changes)
- model.changed.select { |c| !prior_changes[c] || prior_changes[c].last != model.changes[c].last }
+ 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/copy/concerns/copy_attachments.rb b/app/services/copy/concerns/copy_attachments.rb
index e549b0fd4ba..4dd53ca73df 100644
--- a/app/services/copy/concerns/copy_attachments.rb
+++ b/app/services/copy/concerns/copy_attachments.rb
@@ -5,7 +5,7 @@ module Copy
##
# Tries to copy the given attachment between containers
def copy_attachments(from_container_id, to_container_id, container_type)
- Attachment.where(container_id: from_container_id).find_each do |old_attachment|
+ Attachment.where(container_id: from_container_id, container_type: container_type).find_each do |old_attachment|
copied = old_attachment.dup
old_attachment.file.copy_to(copied)
diff --git a/app/services/notifications/journal_notification_service.rb b/app/services/notifications/journal_notification_service.rb
index b3a38e39875..bfc82727226 100644
--- a/app/services/notifications/journal_notification_service.rb
+++ b/app/services/notifications/journal_notification_service.rb
@@ -30,18 +30,14 @@
class Notifications::JournalNotificationService
class << self
- include Notifications::JournalNotifier
-
def call(journal, send_mails)
- if journal.journable_type == 'WorkPackage'
- enqueue_work_package_notification(journal, send_mails)
- end
+ enqueue_work_package_notification(journal, send_mails) if supported?(journal)
end
private
def enqueue_work_package_notification(journal, send_mails)
- EnqueueWorkPackageNotificationJob
+ NotifyJournalCompletedJob
.set(wait_until: delivery_time)
.perform_later(journal.id, send_mails)
end
@@ -49,5 +45,9 @@ class Notifications::JournalNotificationService
def delivery_time
Setting.journal_aggregation_time_minutes.to_i.minutes.from_now
end
+
+ def supported?(journal)
+ %w(WorkPackage WikiContent).include?(journal.journable_type)
+ end
end
end
diff --git a/app/services/notifications/journal_wiki_mail_service.rb b/app/services/notifications/journal_wiki_mail_service.rb
new file mode 100644
index 00000000000..a5d3523899b
--- /dev/null
+++ b/app/services/notifications/journal_wiki_mail_service.rb
@@ -0,0 +1,120 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 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-2017 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.
+#++
+
+class Notifications::JournalWikiMailService
+ class << self
+ def call(journal, send_mails)
+ new(journal)
+ .call(send_mails)
+ end
+ end
+
+ attr_reader :journal
+
+ def initialize(journal)
+ @journal = journal
+ end
+
+ def call(send_mails)
+ return unless send_mail?(send_mails)
+
+ if journal.initial?
+ send_content_added_mail
+ else
+ send_content_updated_mail
+ end
+ end
+
+ private
+
+ def send_mail?(send_mails)
+ send_mails && ::UserMailer.perform_deliveries && !journal.noop?
+ end
+
+ def send_content_added_mail
+ send_content(create_recipients, :wiki_content_added)
+ end
+
+ def send_content_updated_mail
+ send_content(update_recipients, :wiki_content_updated)
+ end
+
+ def notification_disabled?(name)
+ !Setting.notified_events.include?(name)
+ end
+
+ # Returns the mail addresses of users that should be notified
+ def recipients
+ project
+ .notified_users
+ .select { |user| wiki_content.visible?(user) }
+ end
+
+ def send_content(recipients, method)
+ return if notification_disabled?(method.to_s)
+
+ recipients.uniq.each do |user|
+ UserMailer
+ .send(method, user, wiki_content, journal_user)
+ .deliver_later
+ end
+ end
+
+ def create_recipients
+ recipients +
+ wiki.watcher_recipients
+ end
+
+ def update_recipients
+ recipients +
+ wiki.watcher_recipients +
+ page.watcher_recipients
+ end
+
+ def wiki_content
+ journal.journable
+ end
+
+ def page
+ wiki_content.page
+ end
+
+ def wiki
+ page.wiki
+ end
+
+ def project
+ wiki.project
+ end
+
+ def journal_user
+ journal.user || DeletedUser.first
+ end
+end
diff --git a/app/services/notifications/journal_wp_mail_service.rb b/app/services/notifications/journal_wp_mail_service.rb
index ea195b03b08..47b3e3e28ce 100644
--- a/app/services/notifications/journal_wp_mail_service.rb
+++ b/app/services/notifications/journal_wp_mail_service.rb
@@ -30,8 +30,6 @@
class Notifications::JournalWpMailService
class << self
- include Notifications::JournalNotifier
-
def call(journal, send_mails)
journal_complete_mail(journal, send_mails)
end
diff --git a/app/services/projects/copy/wiki_dependent_service.rb b/app/services/projects/copy/wiki_dependent_service.rb
index 9840fee5b55..eb74a657056 100644
--- a/app/services/projects/copy/wiki_dependent_service.rb
+++ b/app/services/projects/copy/wiki_dependent_service.rb
@@ -46,47 +46,72 @@ module Projects::Copy
target.wiki.wiki_menu_items.delete_all
copy_wiki_pages(params)
- copy_wiki_menu_items(params)
+ copy_wiki_menu_items
end
# Copies wiki pages from +project+, requires a wiki to be already set
def copy_wiki_pages(params)
wiki_pages_map = {}
- source.wiki.pages.find_each do |page|
- # Skip pages without content
- next if page.content.nil?
-
- new_wiki_content = WikiContent.new(page.content.attributes.dup.except('id', 'page_id', 'updated_at'))
- attributes = page
- .attributes.dup.except('id', 'wiki_id', 'created_on', 'parent_id')
- .merge(content: new_wiki_content)
-
- new_wiki_page = target.wiki.pages.create attributes
- wiki_pages_map[page] = new_wiki_page
- end
-
- # Save the wiki
- target.wiki.save
-
- # Reproduce page hierarchy
- source.project.wiki.pages.each do |page|
- if page.parent_id && wiki_pages_map[page]
- wiki_pages_map[page].parent = wiki_pages_map[page.parent]
- wiki_pages_map[page].save
- end
+ # Copying top down so that the hierarchy (parent attribute)
+ # can be rewritten along the way.
+ pages_top_down do |page|
+ new_parent = wiki_pages_map[page.parent]
+ wiki_pages_map[page] = copy_wiki_page(page, new_parent)
end
# Copy attachments
if should_copy?(params, :wiki_page_attachments)
wiki_pages_map.each do |old_page, new_page|
+ next unless old_page && new_page
+
copy_attachments(old_page.id, new_page.id, new_page.class.name)
end
end
end
+ def copy_wiki_page(source_page, new_parent)
+ # Skip pages without content
+ return if source_page.content.nil?
+
+ # Relying on ActionMailer::Base.perform_deliveries is violating cohesion
+ # but the value is currently not otherwise provided
+ service_call = WikiPages::CopyService
+ .new(user: User.current, model: source_page, contract_class: WikiPages::CopyContract)
+ .call(wiki: target.wiki,
+ parent: new_parent,
+ send_notifications: ActionMailer::Base.perform_deliveries)
+
+ if service_call.success?
+ service_call.result
+ else
+ add_error!(source_page, service_call.errors)
+ Rails.logger.warn do
+ "Project#copy_wiki_page: wiki_page ##{source_page.id} could not be copied: #{service_call.message}"
+ end
+
+ nil
+ end
+ end
+
+ def pages_top_down(&block)
+ id_by_parent = source.wiki.pages.pluck(:parent_id, :id).inject(Hash.new { [] }) { |h, (k, v)| h[k] += [v]; h }
+
+ yield_downwards(id_by_parent, nil, &block)
+ end
+
+ def yield_downwards(map, current, &block)
+ map[current].each do |child_id|
+ child = source.wiki.pages.find(child_id)
+
+ yield child
+
+ yield_downwards(map, child_id, &block)
+ end
+ end
+
# Copies wiki_menu_items from +project+, requires a wiki to be already set
- def copy_wiki_menu_items(params)
+ def copy_wiki_menu_items
wiki_menu_items_map = {}
source.wiki.wiki_menu_items.each do |item|
diff --git a/app/services/wiki_pages/copy_service.rb b/app/services/wiki_pages/copy_service.rb
new file mode 100644
index 00000000000..94e6c0709e5
--- /dev/null
+++ b/app/services/wiki_pages/copy_service.rb
@@ -0,0 +1,79 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 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-2017 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.
+#++
+
+class WikiPages::CopyService
+ include ::Shared::ServiceContext
+ include Contracted
+
+ attr_accessor :user,
+ :model,
+ :contract_class
+
+ def initialize(user:, model:, contract_class: WikiPages::CreateContract)
+ self.user = user
+ self.model = model
+ self.contract_class = contract_class
+ end
+
+ def call(send_notifications: true, **attributes)
+ in_context(model, send_notifications) do
+ copy(attributes)
+ end
+ end
+
+ protected
+
+ def copy(attribute_override)
+ attributes = copied_attributes(attribute_override)
+
+ create(attributes)
+ end
+
+ def create(attributes)
+ WikiPages::CreateService
+ .new(user: user,
+ contract_class: contract_class)
+ .call(attributes.symbolize_keys)
+ end
+
+ # Copy the wiki page attributes together with the wiki page content attributes
+ def copied_attributes(override)
+ model
+ .attributes
+ .merge(model.content.attributes)
+ .slice(*writable_attributes)
+ .merge(override)
+ end
+
+ def writable_attributes
+ instantiate_contract(model, user)
+ .writable_attributes
+ end
+end
diff --git a/app/services/notifications/journal_notifier.rb b/app/services/wiki_pages/create_service.rb
similarity index 76%
rename from app/services/notifications/journal_notifier.rb
rename to app/services/wiki_pages/create_service.rb
index 214a83c5627..9183fe0fdc5 100644
--- a/app/services/notifications/journal_notifier.rb
+++ b/app/services/wiki_pages/create_service.rb
@@ -28,18 +28,5 @@
# See docs/COPYRIGHT.rdoc for more details.
#++
-module Notifications::JournalNotifier
- private
-
- def find_aggregated_journal_for(raw_journal)
- Journal::AggregatedJournal.with_version(raw_journal)
- end
-
- def notify_journal_complete(journal, send_mails)
- OpenProject::Notifications.send(
- OpenProject::Events::AGGREGATED_WORK_PACKAGE_JOURNAL_READY,
- journal: journal,
- send_mail: send_mails
- )
- end
+class WikiPages::CreateService < ::BaseServices::Create
end
diff --git a/app/services/wiki_pages/set_attributes_service.rb b/app/services/wiki_pages/set_attributes_service.rb
new file mode 100644
index 00000000000..a952f2f44b3
--- /dev/null
+++ b/app/services/wiki_pages/set_attributes_service.rb
@@ -0,0 +1,72 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 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-2017 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.
+#++
+
+# Handles setting the attributes of a wiki page.
+# The wiki page is treated as one single entity although the data layer separates
+# between the page and the content.
+#
+# In the long run, those two should probably be unified on the data layer as well.
+#
+# Attributes for both the page as well as for the content are accepted.
+class WikiPages::SetAttributesService < ::BaseServices::SetAttributes
+ private
+
+ def set_attributes(params)
+ content_params, page_params = split_page_and_content_params(params.with_indifferent_access)
+
+ set_page_attributes(page_params)
+
+ set_default_attributes(params) if model.new_record?
+
+ set_content_attributes(content_params)
+ end
+
+ def set_page_attributes(params)
+ model.attributes = params
+ end
+
+ def set_default_attributes(_params)
+ change_by_system do
+ model.build_content author: user
+ end
+ end
+
+ def set_content_attributes(params)
+ model.content.attributes = params
+ end
+
+ 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/workers/enqueue_work_package_notification_job.rb b/app/workers/notify_journal_completed_job.rb
similarity index 79%
rename from app/workers/enqueue_work_package_notification_job.rb
rename to app/workers/notify_journal_completed_job.rb
index ccbda53c8ba..9ee6fe4da01 100644
--- a/app/workers/enqueue_work_package_notification_job.rb
+++ b/app/workers/notify_journal_completed_job.rb
@@ -27,12 +27,9 @@
# See docs/COPYRIGHT.rdoc for more details.
#++
-# Enqueues
-class EnqueueWorkPackageNotificationJob < ApplicationJob
+class NotifyJournalCompletedJob < ApplicationJob
queue_with_priority :notification
- include Notifications::JournalNotifier
-
def perform(journal_id, send_mails)
# This is caused by a DJ job running as ActiveJob
@journal_id = journal_id
@@ -72,6 +69,16 @@ class EnqueueWorkPackageNotificationJob < ApplicationJob
notify_journal_complete(aggregated_predecessor, @send_mails)
end
+ def find_aggregated_journal_for(raw_journal)
+ Journal::AggregatedJournal.with_version(raw_journal)
+ end
+
+ def notify_journal_complete(journal, send_mails)
+ OpenProject::Notifications.send(notification_event_type(journal),
+ journal: journal,
+ send_mail: send_mails)
+ end
+
def raw_journal
@raw_journal ||= Journal.find_by(id: @journal_id)
end
@@ -79,4 +86,15 @@ class EnqueueWorkPackageNotificationJob < ApplicationJob
def work_package
@work_package ||= raw_journal.journable
end
+
+ def notification_event_type(journal)
+ case journal.journable_type
+ when WikiContent.name
+ OpenProject::Events::AGGREGATED_WIKI_JOURNAL_READY
+ when WorkPackage.name
+ OpenProject::Events::AGGREGATED_WORK_PACKAGE_JOURNAL_READY
+ else
+ raise 'Unsupported journal created event type'
+ end
+ end
end
diff --git a/config/initializers/subscribe_listeners.rb b/config/initializers/subscribe_listeners.rb
index 46d16e07207..152982da7b0 100644
--- a/config/initializers/subscribe_listeners.rb
+++ b/config/initializers/subscribe_listeners.rb
@@ -36,6 +36,10 @@ OpenProject::Notifications.subscribe(OpenProject::Events::AGGREGATED_WORK_PACKAG
Notifications::JournalWpMailService.call(payload[:journal], payload[:send_mail])
end
+OpenProject::Notifications.subscribe(OpenProject::Events::AGGREGATED_WIKI_JOURNAL_READY) do |payload|
+ Notifications::JournalWikiMailService.call(payload[:journal], payload[:send_mail])
+end
+
OpenProject::Notifications.subscribe('watcher_added') do |payload|
WatcherAddedNotificationMailer.handle_watcher(payload[:watcher], payload[:watcher_setter])
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index a8a4155cc4e..d097951cb1f 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -588,6 +588,7 @@ en:
invalid_url: 'is not a valid URL.'
invalid_url_scheme: 'is not a supported protocol (allowed: %{allowed_schemes}).'
less_than_or_equal_to: "must be less than or equal to %{count}."
+ not_current_user: "is not the current user."
not_a_date: "is not a valid date."
not_a_datetime: "is not a valid date time."
not_a_number: "is not a number."
diff --git a/lib/open_project/events.rb b/lib/open_project/events.rb
index b815ee3511a..ff65db48687 100644
--- a/lib/open_project/events.rb
+++ b/lib/open_project/events.rb
@@ -38,6 +38,7 @@ module OpenProject
# @see OpenProject::Notifications
module Events
AGGREGATED_WORK_PACKAGE_JOURNAL_READY = "aggregated_work_package_journal_ready".freeze
+ AGGREGATED_WIKI_JOURNAL_READY = "aggregated_wiki_journal_ready".freeze
NEW_TIME_ENTRY_CREATED = "new_time_entry_created".freeze
PROJECT_CREATED = "project_created".freeze
diff --git a/modules/costs/app/contracts/time_entries/create_contract.rb b/modules/costs/app/contracts/time_entries/create_contract.rb
index ebb048df911..5dede79d2f2 100644
--- a/modules/costs/app/contracts/time_entries/create_contract.rb
+++ b/modules/costs/app/contracts/time_entries/create_contract.rb
@@ -42,7 +42,7 @@ module TimeEntries
end
def validate_user_current_user
- errors.add :user_id, :invalid if model.user != user
+ errors.add :user_id, :not_current_user if model.user != user
end
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 7373afe672c..68d9f446b0e 100644
--- a/modules/costs/spec/contracts/time_entries/create_contract_spec.rb
+++ b/modules/costs/spec/contracts/time_entries/create_contract_spec.rb
@@ -62,7 +62,7 @@ describe TimeEntries::CreateContract do
let(:time_entry_user) { other_user }
it 'is invalid' do
- expect_valid(false, user_id: %i(invalid))
+ expect_valid(false, user_id: %i(not_current_user))
end
end
@@ -71,7 +71,7 @@ describe TimeEntries::CreateContract do
let(:changed_by_system) { %w() }
it 'is invalid' do
- expect_valid(false, user_id: %i(invalid error_readonly))
+ expect_valid(false, user_id: %i(not_current_user error_readonly))
end
end
@@ -79,7 +79,7 @@ describe TimeEntries::CreateContract do
let(:time_entry_user) { nil }
it 'is invalid' do
- expect_valid(false, user_id: %i(blank invalid))
+ expect_valid(false, user_id: %i(blank not_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
new file mode 100644
index 00000000000..392c5c9de9f
--- /dev/null
+++ b/spec/contracts/wiki_pages/create_contract_spec.rb
@@ -0,0 +1,69 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 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-2017 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'
+require_relative './shared_contract_examples'
+
+describe WikiPages::CreateContract do
+ it_behaves_like 'wiki page contract' do
+ let(:page) do
+ WikiPage.new(wiki: page_wiki,
+ title: page_title,
+ slug: page_slug,
+ protected: page_protected,
+ parent: page_parent).tap do |page|
+ page.build_content text: page_text,
+ author: page_author
+ 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 }) }
+
+ describe '#validation' do
+ context 'if the author is different from the current user' do
+ let(:page_author) { FactoryBot.build_stubbed(:user) }
+
+ it 'is invalid' do
+ expect_valid(false, author: :not_current_user)
+ end
+ end
+
+ context 'if the author was not set by system' do
+ let(:changed_by_system) { %w() }
+
+ it 'is invalid' do
+ expect_valid(false, author_id: %i(error_readonly))
+ end
+ end
+ end
+ end
+end
diff --git a/spec/contracts/wiki_pages/shared_contract_examples.rb b/spec/contracts/wiki_pages/shared_contract_examples.rb
new file mode 100644
index 00000000000..201798de092
--- /dev/null
+++ b/spec/contracts/wiki_pages/shared_contract_examples.rb
@@ -0,0 +1,162 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 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-2017 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'
+
+shared_examples_for 'wiki page contract' do
+ let(:current_user) do
+ FactoryBot.build_stubbed(:user) do |user|
+ allow(user)
+ .to receive(:allowed_to?) do |permission, permission_project|
+ permissions.include?(permission) && page_wiki.project == permission_project
+ end
+ end
+ end
+ let(:page_wiki) { FactoryBot.build_stubbed(:wiki) }
+ let(:page_author) { current_user }
+ let(:page_title) { 'Wiki title'}
+ let(:page_slug) { 'wiki slug'}
+ let(:page_protected) { false }
+ let(:page_parent) { nil }
+ let(:page_text) { 'Wiki text' }
+ let(:permissions) { %i[view_wiki edit_wiki_pages] }
+
+ def expect_valid(valid, symbols = {})
+ expect(contract.validate).to eq(valid)
+
+ symbols.each do |key, arr|
+ expect(contract.errors.symbols_for(key)).to match_array arr
+ end
+ end
+
+ describe 'validation' do
+ shared_examples 'is valid' do
+ it 'is valid' do
+ expect_valid(true)
+ end
+ end
+
+ it_behaves_like 'is valid'
+
+ context 'if the title is nil' do
+ let(:page_title) { nil }
+
+ it 'is invalid' do
+ expect_valid(false, title: :blank)
+ end
+ end
+
+ context 'if the slug is nil' do
+ let(:page_slug) { nil }
+
+ it_behaves_like 'is valid'
+ end
+
+ context 'if the wiki is nil' do
+ let(:page_wiki) { nil }
+
+ it 'is invalid' do
+ expect_valid(false, wiki: :blank)
+ end
+ end
+
+ context 'if the content is nil' do
+ it 'is invalid' do
+ page.content = nil
+
+ expect_valid(false, content: :blank)
+ end
+ end
+
+ context 'if the parent is in the same wiki' do
+ let(:page_parent) { FactoryBot.build_stubbed(:wiki_page, wiki: page_wiki) }
+
+ it_behaves_like 'is valid'
+ end
+
+ context 'if the parent is in a different wiki' do
+ let(:page_parent) { FactoryBot.build_stubbed(:wiki_page) }
+
+ it 'is invalid' do
+ expect_valid(false, parent_title: :not_same_project)
+ end
+ end
+
+ context 'if the parent is a child of the page (circular dependency)' do
+ it 'is invalid' do
+ page.parent = FactoryBot.build_stubbed(:wiki_page, wiki: page_wiki).tap do |parent|
+ # Using stubbing here to avoid infinite loops
+ allow(parent)
+ .to receive(:ancestors)
+ .and_return([page])
+ end
+
+ expect_valid(false, parent_title: :circular_dependency)
+ end
+ end
+
+ context 'if the parent the page itself (circular dependency' do
+ it 'is invalid' do
+ page.parent = page
+
+ expect_valid(false, parent_title: :circular_dependency)
+ end
+ end
+
+ context 'if the author is nil' do
+ let(:page_author) { nil }
+
+ it 'is invalid' do
+ expect_valid(false, author: %i[blank not_current_user])
+ end
+ end
+
+ context 'if the user lacks permission' do
+ let(:permissions) { %i[view_wiki] }
+
+ it 'is invalid' do
+ expect_valid(false, base: :error_unauthorized)
+ end
+ end
+
+ context 'if the page is protected and the user has permission to protect pages' do
+ let(:permissions) { %i[view_wiki edit_wiki_pages protect_wiki_pages] }
+ let(:page_protected) { true }
+
+ it_behaves_like 'is valid'
+ end
+
+ context 'if the page is protected and the user lacks permission to protect pages' do
+ let(:page_protected) { true }
+
+ it 'is invalid' do
+ expect_valid(false, protected: :error_unauthorized)
+ end
+ end
+ end
+end
diff --git a/spec/factories/wiki_page_factory.rb b/spec/factories/wiki_page_factory.rb
index eae4f07cfc0..889d9c6a094 100644
--- a/spec/factories/wiki_page_factory.rb
+++ b/spec/factories/wiki_page_factory.rb
@@ -32,9 +32,7 @@ FactoryBot.define do
sequence(:title) { |n| "Wiki Page No. #{n}" }
factory :wiki_page_with_content do
- callback(:after_build) do |wiki_page|
- wiki_page.content = FactoryBot.build :wiki_content, page: wiki_page
- end
+ content { association :wiki_content, page: instance }
end
end
end
diff --git a/spec/models/wiki_content_spec.rb b/spec/models/wiki_content_spec.rb
index 057b02791a9..8eb06959cba 100644
--- a/spec/models/wiki_content_spec.rb
+++ b/spec/models/wiki_content_spec.rb
@@ -32,11 +32,22 @@ describe WikiContent, type: :model do
let(:wiki) { FactoryBot.create(:wiki) }
let(:page) { FactoryBot.create(:wiki_page, wiki: wiki) }
let(:content) { FactoryBot.create(:wiki_content, page: page, author: author) }
- let(:author) { FactoryBot.create(:user, member_in_project: wiki.project, member_with_permissions: [:view_wiki_pages]) }
+ let(:author) do
+ FactoryBot.create(:user,
+ member_in_project: wiki.project,
+ member_with_permissions: [:view_wiki_pages],
+ mail_notification: nil)
+ end
+ let(:project_watcher) do
+ FactoryBot.create(:user,
+ member_in_project: wiki.project,
+ member_with_permissions: [:view_wiki_pages])
+ end
let(:page_watcher) do
watcher = FactoryBot.create(:user,
member_in_project: wiki.project,
- member_with_permissions: [:view_wiki_pages])
+ member_with_permissions: [:view_wiki_pages],
+ mail_notification: nil)
page.watcher_users << watcher
watcher
@@ -45,7 +56,8 @@ describe WikiContent, type: :model do
let(:wiki_watcher) do
watcher = FactoryBot.create(:user,
member_in_project: wiki.project,
- member_with_permissions: [:view_wiki_pages])
+ member_with_permissions: [:view_wiki_pages],
+ mail_notification: nil)
wiki.watcher_users << watcher
watcher
@@ -54,22 +66,29 @@ describe WikiContent, type: :model do
describe '#save (create)' do
let(:content) { FactoryBot.build(:wiki_content, page: page) }
- it 'sends mails to the wiki`s watchers', with_settings: { notified_events: ['wiki_content_added'] } do
+ it 'sends mails to the wiki`s watchers and project all watchers', with_settings: { notified_events: ['wiki_content_added'] } do
wiki_watcher
+ project_watcher
expect {
content.save!
perform_enqueued_jobs
}
.to change { ActionMailer::Base.deliveries.size }
- .by(1)
+ .by(2)
end
end
describe '#save (update)' do
- it 'sends mails to the author, the watchers and the wiki`s watchers', with_settings: { notified_events: ['wiki_content_updated'] } do
+ it 'sends mails to the watchers, the wiki`s watchers and project all watchers',
+ with_settings: { notified_events: ['wiki_content_updated'], journal_aggregation_time_minutes: 0 } do
page_watcher
wiki_watcher
+ project_watcher
+
+ content
+ perform_enqueued_jobs
+
content.text = 'My new content'
expect {
diff --git a/spec/requests/api/v3/work_package_resource_spec.rb b/spec/requests/api/v3/work_package_resource_spec.rb
index 32e886443c7..01db2aba6b6 100644
--- a/spec/requests/api/v3/work_package_resource_spec.rb
+++ b/spec/requests/api/v3/work_package_resource_spec.rb
@@ -404,7 +404,7 @@ describe 'API v3 Work package resource',
context 'not set' do
let(:params) { update_params }
- it { expect(EnqueueWorkPackageNotificationJob).to have_been_enqueued.at_least(1) }
+ it { expect(NotifyJournalCompletedJob).to have_been_enqueued.at_least(1) }
end
context 'disabled' do
@@ -412,7 +412,7 @@ describe 'API v3 Work package resource',
let(:params) { update_params }
it do
- expect(EnqueueWorkPackageNotificationJob)
+ expect(NotifyJournalCompletedJob)
.to have_been_enqueued
.at_least(1)
end
@@ -423,7 +423,7 @@ describe 'API v3 Work package resource',
let(:params) { update_params }
it do
- expect(EnqueueWorkPackageNotificationJob)
+ expect(NotifyJournalCompletedJob)
.to have_been_enqueued
.at_least(1)
end
diff --git a/spec/services/notifications/journal_notification_service_spec.rb b/spec/services/notifications/journal_notification_service_spec.rb
index 5b0d1de7c44..f1e25f64ee5 100644
--- a/spec/services/notifications/journal_notification_service_spec.rb
+++ b/spec/services/notifications/journal_notification_service_spec.rb
@@ -29,7 +29,6 @@
require 'spec_helper'
describe Notifications::JournalNotificationService do
- let(:journable) { FactoryBot.build_stubbed(:stubbed_work_package) }
let(:journal) { FactoryBot.build_stubbed(:journal, journable: journable) }
let(:send_mails) { true }
@@ -42,7 +41,7 @@ describe Notifications::JournalNotificationService do
notification_set = double('notification set')
- expect(EnqueueWorkPackageNotificationJob)
+ expect(NotifyJournalCompletedJob)
.to receive(:set)
.with(wait_until: Setting.journal_aggregation_time_minutes.to_i.minutes.from_now)
.and_return(notification_set)
@@ -59,7 +58,7 @@ describe Notifications::JournalNotificationService do
shared_examples_for 'enqueues no notification' do
before do
- expect(EnqueueWorkPackageNotificationJob)
+ expect(NotifyJournalCompletedJob)
.not_to receive(:set)
end
@@ -69,6 +68,12 @@ describe Notifications::JournalNotificationService do
end
context 'for a work package journal' do
+ let(:journable) { FactoryBot.build_stubbed(:stubbed_work_package) }
+ it_behaves_like 'enqueues a notification'
+ end
+
+ context 'for a wiki content journal' do
+ let(:journable) { FactoryBot.build_stubbed(:wiki_content) }
it_behaves_like 'enqueues a notification'
end
diff --git a/spec/services/notifications/journal_wiki_mail_service_spec.rb b/spec/services/notifications/journal_wiki_mail_service_spec.rb
new file mode 100644
index 00000000000..f18a413a337
--- /dev/null
+++ b/spec/services/notifications/journal_wiki_mail_service_spec.rb
@@ -0,0 +1,240 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 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-2017 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 Notifications::JournalWikiMailService do
+ let(:project) do
+ FactoryBot.build_stubbed(:project).tap do |p|
+ allow(p)
+ .to receive(:notified_users)
+ .and_return([project_notified_user_with_permission, project_notified_user_wo_permission])
+ end
+ end
+ let(:wiki) do
+ FactoryBot.build_stubbed(:wiki, project: project).tap do |w|
+ allow(w)
+ .to receive(:watcher_recipients)
+ .and_return([wiki_watcher_user])
+ end
+ end
+ let(:wiki_page) do
+ FactoryBot.build_stubbed(:wiki_page, wiki: wiki).tap do |w|
+ allow(w)
+ .to receive(:watcher_recipients)
+ .and_return([wiki_page_watcher_user])
+ end
+ end
+ let(:wiki_content) do
+ FactoryBot.build_stubbed(:wiki_content, page: wiki_page).tap do |wc|
+ allow(wc)
+ .to receive(:visible?)
+ .with(project_notified_user_with_permission)
+ .and_return(true)
+
+ allow(wc)
+ .to receive(:visible?)
+ .with(project_notified_user_wo_permission)
+ .and_return(false)
+ end
+ end
+ let(:journal) do
+ FactoryBot.build_stubbed(:wiki_content_journal, journable: wiki_content, user: current_user).tap do |j|
+ allow(j)
+ .to receive(:initial?)
+ .and_return(journal_initial)
+
+ allow(j)
+ .to receive(:noop?)
+ .and_return(journal_noop)
+ end
+ end
+ let(:journal_initial) { true }
+ let(:journal_noop) { false }
+
+ let(:notification_setting) { %w(wiki_content_added wiki_content_updated) }
+
+ let(:current_user) do
+ FactoryBot.build_stubbed(:user)
+ end
+
+ let(:project_notified_user_with_permission) do
+ FactoryBot.build_stubbed(:user)
+ end
+
+ let(:project_notified_user_wo_permission) do
+ FactoryBot.build_stubbed(:user)
+ end
+
+ let(:wiki_watcher_user) do
+ FactoryBot.build_stubbed(:user)
+ end
+
+ let(:wiki_page_watcher_user) do
+ FactoryBot.build_stubbed(:user)
+ end
+
+ context '.call' do
+ let(:subject) { described_class.call(journal, send_mails) }
+ let(:send_mails) { true }
+
+ before do
+ allow(Setting).to receive(:notified_events).and_return(notification_setting)
+ end
+
+ shared_examples_for 'sends no mails' do
+ it 'sends no mails' do
+ expect(UserMailer)
+ .not_to receive(:wiki_content_updated)
+
+ expect(UserMailer)
+ .not_to receive(:wiki_content_added)
+
+ subject
+ end
+ end
+
+ context 'with the settings allowing email sending for newly added content' do
+ let(:notification_setting) { %w(wiki_content_added) }
+
+ context 'for an initial journal' do
+ let(:journal_initial) { true }
+
+ it 'sends mails to users listening on all changes and to watchers of the wiki' do
+ [project_notified_user_with_permission, wiki_watcher_user].each do |u|
+ mailer = double('mailer')
+
+ expect(UserMailer)
+ .to receive(:wiki_content_added)
+ .with(u, wiki_content, current_user)
+ .and_return(mailer)
+
+ expect(mailer)
+ .to receive(:deliver_later)
+ end
+
+ subject
+ end
+
+ context 'with send_mails set to false' do
+ let(:send_mails) { false }
+
+ it_behaves_like 'sends no mails'
+ end
+
+ context 'with perform_deliveries set to false' do
+ before do
+ allow(UserMailer)
+ .to receive(:perform_deliveries)
+ .and_return(false)
+ end
+
+ it_behaves_like 'sends no mails'
+ end
+
+ context 'with the journal being a noop' do
+ let(:journal_noop) { true }
+
+ it_behaves_like 'sends no mails'
+ end
+ end
+
+ context 'for a non initial journal' do
+ let(:journal_initial) { false }
+
+ it_behaves_like 'sends no mails'
+ end
+ end
+
+ context 'with the settings allowing email sending for updated content' do
+ let(:notification_setting) { %w(wiki_content_updated) }
+
+ context 'for a non initial journal' do
+ let(:journal_initial) { false }
+
+ it 'sends mails to users listening on all changes and to watchers of the wiki' do
+ [project_notified_user_with_permission, wiki_watcher_user, wiki_page_watcher_user].each do |u|
+ mailer = double('mailer')
+
+ expect(UserMailer)
+ .to receive(:wiki_content_updated)
+ .with(u, wiki_content, current_user)
+ .and_return(mailer)
+
+ expect(mailer)
+ .to receive(:deliver_later)
+ end
+
+ subject
+ end
+
+ context 'with send_mails set to false' do
+ let(:send_mails) { false }
+
+ it_behaves_like 'sends no mails'
+ end
+
+ context 'with perform_deliveries set to false' do
+ before do
+ allow(UserMailer)
+ .to receive(:perform_deliveries)
+ .and_return(false)
+ end
+
+ it_behaves_like 'sends no mails'
+ end
+
+ context 'with the journal being a noop' do
+ let(:journal_noop) { true }
+
+ it_behaves_like 'sends no mails'
+ end
+ end
+
+ context 'for an initial journal' do
+ let(:journal_initial) { true }
+
+ it_behaves_like 'sends no mails'
+ end
+ end
+ end
+
+ it 'listener is subscribed' do
+ journal = double('journal')
+ send_mail = true
+
+ expect(Notifications::JournalWikiMailService)
+ .to receive(:call)
+ .with(journal, send_mail)
+
+ OpenProject::Notifications.send(OpenProject::Events::AGGREGATED_WIKI_JOURNAL_READY,
+ journal: journal,
+ send_mail: send_mail)
+ end
+end
diff --git a/spec/services/projects/copy_service_integration_spec.rb b/spec/services/projects/copy_service_integration_spec.rb
index 37a9d00a39e..d8a3c8cb62f 100644
--- a/spec/services/projects/copy_service_integration_spec.rb
+++ b/spec/services/projects/copy_service_integration_spec.rb
@@ -37,6 +37,7 @@ describe Projects::CopyService, 'integration', type: :model do
shared_let(:source_category) { FactoryBot.create :category, project: source, name: 'Stock management' }
shared_let(:source_version) { FactoryBot.create :version, project: source, name: 'Version A' }
shared_let(:source_wiki_page) { FactoryBot.create(:wiki_page_with_content, wiki: source.wiki) }
+ shared_let(:source_child_wiki_page) { FactoryBot.create(:wiki_page_with_content, wiki: source.wiki, parent: source_wiki_page) }
shared_let(:source_forum) { FactoryBot.create(:forum, project: source) }
shared_let(:source_topic) { FactoryBot.create(:message, forum: source_forum) }
@@ -86,10 +87,11 @@ describe Projects::CopyService, 'integration', type: :model do
expect(project_copy.forums.count).to eq 1
expect(project_copy.forums.first.messages.count).to eq 1
expect(project_copy.wiki).to be_present
- expect(project_copy.wiki.pages.count).to eq 1
+ expect(project_copy.wiki.pages.count).to eq 2
expect(project_copy.queries.count).to eq 1
expect(project_copy.versions.count).to eq 1
- expect(project_copy.wiki.pages.first.content.text).to eq source_wiki_page.content.text
+ expect(project_copy.wiki.pages.root.content.text).to eq source_wiki_page.content.text
+ expect(project_copy.wiki.pages.leaves.first.content.text).to eq source_child_wiki_page.content.text
expect(project_copy.wiki.start_page).to eq 'Wiki'
# Cleared attributes
@@ -229,10 +231,11 @@ describe Projects::CopyService, 'integration', type: :model do
describe '#copy_wiki' do
it 'will not copy wiki pages without content' do
source.wiki.pages << FactoryBot.create(:wiki_page)
- expect(source.wiki.pages.count).to eq 2
+ expect(source.wiki.pages.count).to eq 3
expect(subject).to be_success
- expect(project_copy.wiki.pages.count).to eq 1
+ expect(subject.errors).to be_empty
+ expect(project_copy.wiki.pages.count).to eq 2
end
it 'will copy menu items' do
diff --git a/spec/services/wiki_pages/copy_service_integration_spec.rb b/spec/services/wiki_pages/copy_service_integration_spec.rb
new file mode 100644
index 00000000000..9816023b811
--- /dev/null
+++ b/spec/services/wiki_pages/copy_service_integration_spec.rb
@@ -0,0 +1,106 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 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-2017 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 WikiPages::CopyService, 'integration', type: :model do
+ let(:user) do
+ FactoryBot.create(:user) do |user|
+ FactoryBot.create(:member,
+ project: source_project,
+ principal: user,
+ roles: [role])
+
+ FactoryBot.create(:member,
+ project: sink_project,
+ principal: user,
+ roles: [role])
+ end
+ end
+
+ let(:role) do
+ FactoryBot.create(:role,
+ permissions: permissions)
+ end
+
+ let(:permissions) do
+ %i(view_wiki edit_wiki_pages)
+ end
+ let(:source_wiki) { FactoryBot.create(:wiki) }
+ let(:source_project) { source_wiki.project }
+
+ let(:sink_wiki) { FactoryBot.create(:wiki) }
+ let(:sink_project) { sink_wiki.project }
+
+ let(:wiki_page) { FactoryBot.create(:wiki_page_with_content) }
+
+ let(:instance) { described_class.new(model: wiki_page, user: user) }
+
+ let(:attributes) { {} }
+
+ let(:copy) do
+ service_result
+ .result
+ end
+ let(:service_result) do
+ instance
+ .call(**attributes)
+ end
+
+ before do
+ login_as(user)
+ end
+
+ describe '#call' do
+ shared_examples_for 'copied wiki page' do
+ it 'is a success' do
+ expect(service_result)
+ .to be_success
+ end
+
+ it 'is a new, persisted wiki page' do
+ expect(copy).to be_persisted
+ expect(copy.id).not_to eq(wiki_page.id)
+ end
+
+ it 'copies the content' do
+ expect(copy.content.text).to eq(wiki_page.content.text)
+ end
+
+ it 'sets the author to be the current user' do
+ expect(copy.content.author).to eq(user)
+ end
+ end
+
+ describe 'to a different wiki' do
+ let(:attributes) { { wiki: sink_wiki } }
+
+ it_behaves_like 'copied wiki page'
+ end
+ end
+end
diff --git a/spec/services/wiki_pages/set_attributes_service_spec.rb b/spec/services/wiki_pages/set_attributes_service_spec.rb
new file mode 100644
index 00000000000..63f9c29c6f2
--- /dev/null
+++ b/spec/services/wiki_pages/set_attributes_service_spec.rb
@@ -0,0 +1,110 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 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-2017 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 WikiPages::SetAttributesService, type: :model do
+ let(:user) { FactoryBot.build_stubbed(:user) }
+ let(:contract_class) do
+ contract = double('contract_class')
+
+ allow(contract)
+ .to receive(:new)
+ .with(wiki_page, user, options: { changed_by_system: [] })
+ .and_return(contract_instance)
+
+ contract
+ end
+ let(:contract_instance) do
+ double('contract_instance', validate: contract_valid, errors: contract_errors)
+ end
+ let(:contract_valid) { true }
+ let(:contract_errors) do
+ double('contract_errors')
+ end
+ let(:wiki_page_valid) { true }
+ let(:instance) do
+ described_class.new(user: user,
+ model: wiki_page,
+ contract_class: contract_class)
+ end
+ let(:call_attributes) { {} }
+ let(:wiki_page) do
+ FactoryBot.build_stubbed(:wiki_page_with_content)
+ end
+
+ describe 'call' do
+ let(:call_attributes) do
+ {
+ text: 'some new text',
+ title: 'a new title',
+ slug: 'a new slug'
+ }
+ end
+
+ before do
+ allow(wiki_page)
+ .to receive(:valid?)
+ .and_return(wiki_page_valid)
+
+ expect(contract_instance)
+ .to receive(:validate)
+ .and_return(contract_valid)
+ end
+
+ subject { instance.call(call_attributes) }
+
+ it 'is successful' do
+ expect(subject.success?).to be_truthy
+ end
+
+ context 'for an existing wiki page' do
+ it 'sets the attributes' do
+ subject
+
+ expect(wiki_page.attributes.slice(*wiki_page.changed).symbolize_keys)
+ .to eql call_attributes.slice(:title, :slug)
+
+ expect(wiki_page.content.attributes.slice(*wiki_page.content.changed).symbolize_keys)
+ .to eql call_attributes.slice(:text)
+ end
+
+ it 'does not persist the wiki_page' do
+ expect(wiki_page)
+ .not_to receive(:save)
+
+ expect(wiki_page.content)
+ .not_to receive(:save)
+
+ subject
+ end
+ end
+ end
+end
diff --git a/spec/services/work_packages/copy_service_integration_spec.rb b/spec/services/work_packages/copy_service_integration_spec.rb
index 2fa5ee4a432..d1038988d26 100644
--- a/spec/services/work_packages/copy_service_integration_spec.rb
+++ b/spec/services/work_packages/copy_service_integration_spec.rb
@@ -70,7 +70,7 @@ describe WorkPackages::CopyService, 'integration', type: :model do
end
let(:service_result) do
instance
- .call(attributes)
+ .call(**attributes)
end
before do
diff --git a/spec/workers/mail_notification_jobs/enqueue_work_package_notification_job_spec.rb b/spec/workers/mail_notification_jobs/enqueue_work_package_notification_job_spec.rb
deleted file mode 100644
index f425539d759..00000000000
--- a/spec/workers/mail_notification_jobs/enqueue_work_package_notification_job_spec.rb
+++ /dev/null
@@ -1,253 +0,0 @@
-#-- encoding: UTF-8
-
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 2012-2020 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-2017 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 EnqueueWorkPackageNotificationJob, type: :model do
- let(:project) { FactoryBot.create(:project) }
- let(:recipient) do
- FactoryBot.create(:user, member_in_project: project, member_with_permissions: [:view_work_packages], login: "johndoe")
- end
- let(:author) { FactoryBot.create(:user, login: "marktwain") }
- let(:work_package) do
- FactoryBot.create(:work_package,
- project: project,
- author: author,
- assigned_to: recipient)
- end
- let(:journal) { journal_1 }
- let(:journal_1) { work_package.journals.first }
- let(:journal_2) do
- work_package.add_journal author, 'something I have to say'
- work_package.save(validate: false)
- work_package.journals.last
- end
- let(:send_mail) { true }
-
- subject { described_class.new.perform(journal.id, send_mail) }
-
- before do
- # make sure no other calls are made due to WP creation/update
- allow(OpenProject::Notifications).to receive(:send) # ... and do nothing
- end
-
- shared_examples_for 'sends notification' do
- it 'sends a notification' do
- expect(OpenProject::Notifications)
- .to receive(:send)
- .with(OpenProject::Events::AGGREGATED_WORK_PACKAGE_JOURNAL_READY,
- journal: an_instance_of(Journal::AggregatedJournal),
- send_mail: send_mail)
-
- subject
- end
- end
-
- shared_examples_for 'sends no notification' do
- it 'sends no notification' do
- expect(OpenProject::Notifications)
- .not_to receive(:send)
-
- subject
- end
- end
-
- it_behaves_like 'sends notification'
-
- context 'non-existant journal' do
- before do
- journal.destroy
- end
-
- it_behaves_like 'sends no notification'
- end
-
- context 'outdated journal' do
- before do
- # make sure there is a later journal, that supersedes the original one
- work_package.subject = 'changed subject'
- work_package.save!
- end
-
- it_behaves_like 'sends no notification'
- end
-
- describe 'journal creation' do
- context 'work_package_created' do
- before do
- FactoryBot.create(:work_package, project: project)
- end
-
- it_behaves_like 'sends notification'
- end
-
- context 'work_package_updated' do
- before do
- work_package.add_journal(author)
- work_package.subject = 'A change to the issue'
- work_package.save!(validate: false)
- end
-
- it_behaves_like 'sends notification'
- end
-
- context 'work_package_note_added' do
- before do
- work_package.add_journal(author, 'This update has a note')
- work_package.save!(validate: false)
- end
-
- it_behaves_like 'sends notification'
- end
- end
-
- describe 'mail suppressing aggregation' do
- # business logic of whether to send or not to send a mail is mainly driven by the presence
- # of an aggregated journal. However, there is an edge case that could lead to a notification
- # getting lost. Sadly this is very implementation specific, so I'll describe it:
- # Journal 1: comment
- # Journal 2: change (this can also be multiple journals)
- # Journal 3: comment
- #
- # The Job for the first journal will not send any mail, because Journal 2 supersedes it.
- # However, after adding Journal 3, the aggregation will look like (1), (2, 3). Therefore the
- # job for Journal 2 will not send a notification. Finally the job for Journal 3 will send a
- # notification, but only containing the changes of 2 and 3. The comment of journal 1 is lost.
- # Therefore two things have to happen:
- # - someone needs to send notifications for the hidden journal
- # (done by JournalNotificationMailer)
- # - in case a journal is hidden, its Job is not allowed to enqueue a mail for it
- # (because someone else will do it on behalf)
- # This is important since late exec of a Job might cause it to _not_ skip notifications
-
- before do
- change = { subject: 'new subject' }
- note = { journal_notes: 'a comment' }
-
- allow(WorkPackages::UpdateContract).to receive(:new).and_return(NoopContract.new)
- service = WorkPackages::UpdateService.new(user: author, model: work_package)
-
- expect(service.call(note)).to be_success
- expect(service.call(change)).to be_success
- expect(service.call(note)).to be_success
- end
-
- let(:timeout) { Setting.journal_aggregation_time_minutes.to_i.minutes }
- let(:journal_1) { work_package.journals[1] }
- let(:journal_2) { work_package.journals[2] }
- let(:journal_3) { work_package.journals[3] }
-
- context 'all changes happen within the timeout of journal 1' do
- # The job for 1 will know, that Journal 3 took its addition.
- # The job for 2 will know, that it has become part of Journal 3.
- # -> no special behaviour required
-
- context 'for journal 1' do
- let(:journal) { journal_1 }
-
- it_behaves_like 'sends notification' # for journal 1
- end
-
- context 'for journal 2' do
- let(:journal) { journal_2 }
-
- it_behaves_like 'sends no notification'
- end
-
- context 'for journal 3' do
- let(:journal) { journal_3 }
-
- it_behaves_like 'sends notification' # for journals 2 and 3
- end
- end
-
- context 'journal 3 created after timeout of 1, but inside of timeout for 2' do
- # Job 1 will not send a mail because it does not know about journal 3
- # (thinking 2 will take its mail)
- # The mail of Job 1 is taken over by the JournalNotificationMailer for Journal 3
- # Even if Job 1 knew of journal 3 (due to late execution), it was not allowed to send a mail
- # (that would cause a duplicate mail delivery)
-
- before do
- journal_2.created_at = journal_1.created_at + (timeout / 2)
- journal_3.created_at = journal_1.created_at + timeout + 5.seconds
- journal_2.save!
- journal_3.save!
- end
-
- context 'for journal 1' do
- let(:journal) { journal_1 }
-
- it_behaves_like 'sends no notification'
- end
-
- context 'for journal 2' do
- let(:journal) { journal_2 }
-
- it_behaves_like 'sends no notification'
- end
-
- context 'for journal 3' do
- let(:journal) { journal_3 }
-
- it_behaves_like 'sends notification' # for journals 1, 2 and 3
- end
- end
-
- context 'journal 3 created after timeout of 1 and 2' do
- # This is a normal case again, ensuring nobody takes responsibility when not necessary.
-
- before do
- journal_2.created_at = journal_1.created_at + (timeout / 2)
- journal_3.created_at = journal_2.created_at + timeout + 5.seconds
- journal_2.save!
- journal_3.save!
- end
-
- context 'for journal 1' do
- let(:journal) { journal_1 }
-
- it_behaves_like 'sends no notification'
- end
-
- context 'for journal 2' do
- let(:journal) { journal_2 }
-
- it_behaves_like 'sends notification' # for journals 1 and 2
- end
-
- context 'for journal 3' do
- let(:journal) { journal_3 }
-
- it_behaves_like 'sends notification' # for journal 3
- end
- end
- end
-end
diff --git a/spec/workers/notify_journal_completed_job_integration_spec.rb b/spec/workers/notify_journal_completed_job_integration_spec.rb
new file mode 100644
index 00000000000..59e7eeb0cbc
--- /dev/null
+++ b/spec/workers/notify_journal_completed_job_integration_spec.rb
@@ -0,0 +1,284 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 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-2017 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 NotifyJournalCompletedJob, type: :model do
+ let(:project) { FactoryBot.create(:project) }
+ let(:permissions) { [:view_work_packages] }
+ let(:recipient) do
+ FactoryBot.create(:user, member_in_project: project, member_with_permissions: permissions, login: "johndoe")
+ end
+ let(:author) { FactoryBot.create(:user, login: "marktwain") }
+ let(:send_mail) { true }
+
+ subject { described_class.new.perform(journal.id, send_mail) }
+
+ before do
+ # make sure no other calls are made due to WP creation/update
+ allow(OpenProject::Notifications).to receive(:send) # ... and do nothing
+ end
+
+ context 'for work packages' do
+ let(:work_package) do
+ FactoryBot.create(:work_package,
+ project: project,
+ author: author,
+ assigned_to: recipient)
+ end
+ let(:journal) { journal_1 }
+ let(:journal_1) { work_package.journals.first }
+ let(:journal_2) do
+ work_package.add_journal author, 'something I have to say'
+ work_package.save(validate: false)
+ work_package.journals.last
+ end
+
+ shared_examples_for 'sends notification' do
+ it 'sends a notification' do
+ expect(OpenProject::Notifications)
+ .to receive(:send)
+ .with(OpenProject::Events::AGGREGATED_WORK_PACKAGE_JOURNAL_READY,
+ journal: an_instance_of(Journal::AggregatedJournal),
+ send_mail: send_mail)
+
+ subject
+ end
+ end
+
+ shared_examples_for 'sends no notification' do
+ it 'sends no notification' do
+ expect(OpenProject::Notifications)
+ .not_to receive(:send)
+
+ subject
+ end
+ end
+
+ it_behaves_like 'sends notification'
+
+ context 'non-existant journal' do
+ before do
+ journal.destroy
+ end
+
+ it_behaves_like 'sends no notification'
+ end
+
+ context 'outdated journal' do
+ before do
+ # make sure there is a later journal, that supersedes the original one
+ work_package.subject = 'changed subject'
+ work_package.save!
+ end
+
+ it_behaves_like 'sends no notification'
+ end
+
+ describe 'journal creation' do
+ context 'work_package_created' do
+ before do
+ FactoryBot.create(:work_package, project: project)
+ end
+
+ it_behaves_like 'sends notification'
+ end
+
+ context 'work_package_updated' do
+ before do
+ work_package.add_journal(author)
+ work_package.subject = 'A change to the issue'
+ work_package.save!(validate: false)
+ end
+
+ it_behaves_like 'sends notification'
+ end
+
+ context 'work_package_note_added' do
+ before do
+ work_package.add_journal(author, 'This update has a note')
+ work_package.save!(validate: false)
+ end
+
+ it_behaves_like 'sends notification'
+ end
+ end
+
+ describe 'mail suppressing aggregation' do
+ # business logic of whether to send or not to send a mail is mainly driven by the presence
+ # of an aggregated journal. However, there is an edge case that could lead to a notification
+ # getting lost. Sadly this is very implementation specific, so I'll describe it:
+ # Journal 1: comment
+ # Journal 2: change (this can also be multiple journals)
+ # Journal 3: comment
+ #
+ # The Job for the first journal will not send any mail, because Journal 2 supersedes it.
+ # However, after adding Journal 3, the aggregation will look like (1), (2, 3). Therefore the
+ # job for Journal 2 will not send a notification. Finally the job for Journal 3 will send a
+ # notification, but only containing the changes of 2 and 3. The comment of journal 1 is lost.
+ # Therefore two things have to happen:
+ # - someone needs to send notifications for the hidden journal
+ # (done by JournalNotificationMailer)
+ # - in case a journal is hidden, its Job is not allowed to enqueue a mail for it
+ # (because someone else will do it on behalf)
+ # This is important since late exec of a Job might cause it to _not_ skip notifications
+
+ before do
+ change = { subject: 'new subject' }
+ note = { journal_notes: 'a comment' }
+
+ allow(WorkPackages::UpdateContract).to receive(:new).and_return(NoopContract.new)
+ service = WorkPackages::UpdateService.new(user: author, model: work_package)
+
+ expect(service.call(**note)).to be_success
+ expect(service.call(**change)).to be_success
+ expect(service.call(**note)).to be_success
+ end
+
+ let(:timeout) { Setting.journal_aggregation_time_minutes.to_i.minutes }
+ let(:journal_1) { work_package.journals[1] }
+ let(:journal_2) { work_package.journals[2] }
+ let(:journal_3) { work_package.journals[3] }
+
+ context 'all changes happen within the timeout of journal 1' do
+ # The job for 1 will know, that Journal 3 took its addition.
+ # The job for 2 will know, that it has become part of Journal 3.
+ # -> no special behaviour required
+
+ context 'for journal 1' do
+ let(:journal) { journal_1 }
+
+ it_behaves_like 'sends notification' # for journal 1
+ end
+
+ context 'for journal 2' do
+ let(:journal) { journal_2 }
+
+ it_behaves_like 'sends no notification'
+ end
+
+ context 'for journal 3' do
+ let(:journal) { journal_3 }
+
+ it_behaves_like 'sends notification' # for journals 2 and 3
+ end
+ end
+
+ context 'journal 3 created after timeout of 1, but inside of timeout for 2' do
+ # Job 1 will not send a mail because it does not know about journal 3
+ # (thinking 2 will take its mail)
+ # The mail of Job 1 is taken over by the JournalNotificationMailer for Journal 3
+ # Even if Job 1 knew of journal 3 (due to late execution), it was not allowed to send a mail
+ # (that would cause a duplicate mail delivery)
+
+ before do
+ journal_2.created_at = journal_1.created_at + (timeout / 2)
+ journal_3.created_at = journal_1.created_at + timeout + 5.seconds
+ journal_2.save!
+ journal_3.save!
+ end
+
+ context 'for journal 1' do
+ let(:journal) { journal_1 }
+
+ it_behaves_like 'sends no notification'
+ end
+
+ context 'for journal 2' do
+ let(:journal) { journal_2 }
+
+ it_behaves_like 'sends no notification'
+ end
+
+ context 'for journal 3' do
+ let(:journal) { journal_3 }
+
+ it_behaves_like 'sends notification' # for journals 1, 2 and 3
+ end
+ end
+
+ context 'journal 3 created after timeout of 1 and 2' do
+ # This is a normal case again, ensuring nobody takes responsibility when not necessary.
+
+ before do
+ journal_2.created_at = journal_1.created_at + (timeout / 2)
+ journal_3.created_at = journal_2.created_at + timeout + 5.seconds
+ journal_2.save!
+ journal_3.save!
+ end
+
+ context 'for journal 1' do
+ let(:journal) { journal_1 }
+
+ it_behaves_like 'sends no notification'
+ end
+
+ context 'for journal 2' do
+ let(:journal) { journal_2 }
+
+ it_behaves_like 'sends notification' # for journals 1 and 2
+ end
+
+ context 'for journal 3' do
+ let(:journal) { journal_3 }
+
+ it_behaves_like 'sends notification' # for journal 3
+ end
+ end
+ end
+ end
+
+ context 'for wiki page content' do
+ let(:wiki_page_content) do
+ wiki = FactoryBot.create(:wiki,
+ project: project)
+
+ FactoryBot.create(:wiki_page_with_content, wiki: wiki).content
+ end
+
+ let(:journal) { journal_1 }
+ let(:journal_1) { wiki_page_content.journals.first }
+ let(:journal_2) do
+ wiki_page_content.add_journal author, 'something I have to say'
+ wiki_page_content.save(validate: false)
+ wiki_page_content.journals.last
+ end
+
+ it 'sends a notification' do
+ expect(OpenProject::Notifications)
+ .to receive(:send)
+ .with(OpenProject::Events::AGGREGATED_WIKI_JOURNAL_READY,
+ journal: an_instance_of(Journal::AggregatedJournal),
+ send_mail: send_mail)
+
+ subject
+ end
+ end
+end