##// END OF EJS Templates
Don't notify users about relations that are not visible (#1005)....
Jean-Philippe Lang -
r11785:0087d237f764
parent child
Show More
@@ -1,155 +1,168
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class Journal < ActiveRecord::Base
19 19 belongs_to :journalized, :polymorphic => true
20 20 # added as a quick fix to allow eager loading of the polymorphic association
21 21 # since always associated to an issue, for now
22 22 belongs_to :issue, :foreign_key => :journalized_id
23 23
24 24 belongs_to :user
25 25 has_many :details, :class_name => "JournalDetail", :dependent => :delete_all
26 26 attr_accessor :indice
27 27
28 28 acts_as_event :title => Proc.new {|o| status = ((s = o.new_status) ? " (#{s})" : nil); "#{o.issue.tracker} ##{o.issue.id}#{status}: #{o.issue.subject}" },
29 29 :description => :notes,
30 30 :author => :user,
31 31 :group => :issue,
32 32 :type => Proc.new {|o| (s = o.new_status) ? (s.is_closed? ? 'issue-closed' : 'issue-edit') : 'issue-note' },
33 33 :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.issue.id, :anchor => "change-#{o.id}"}}
34 34
35 35 acts_as_activity_provider :type => 'issues',
36 36 :author_key => :user_id,
37 37 :find_options => {:include => [{:issue => :project}, :details, :user],
38 38 :conditions => "#{Journal.table_name}.journalized_type = 'Issue' AND" +
39 39 " (#{JournalDetail.table_name}.prop_key = 'status_id' OR #{Journal.table_name}.notes <> '')"}
40 40
41 41 before_create :split_private_notes
42 42
43 43 scope :visible, lambda {|*args|
44 44 user = args.shift || User.current
45 45
46 46 includes(:issue => :project).
47 47 where(Issue.visible_condition(user, *args)).
48 48 where("(#{Journal.table_name}.private_notes = ? OR (#{Project.allowed_to_condition(user, :view_private_notes, *args)}))", false)
49 49 }
50 50
51 51 def save(*args)
52 52 # Do not save an empty journal
53 53 (details.empty? && notes.blank?) ? false : super
54 54 end
55 55
56 56 # Returns journal details that are visible to user
57 57 def visible_details(user=User.current)
58 58 details.select do |detail|
59 59 if detail.property == 'cf'
60 60 field_id = detail.prop_key
61 61 field = CustomField.find_by_id(field_id)
62 62 field && field.visible_by?(project, user)
63 63 elsif detail.property == 'relation'
64 64 Issue.find_by_id(detail.value || detail.old_value).try(:visible?, user)
65 65 else
66 66 true
67 67 end
68 68 end
69 69 end
70 70
71 def each_notification(users, &block)
72 if users.any?
73 users_by_details_visibility = users.group_by do |user|
74 visible_details(user)
75 end
76 users_by_details_visibility.each do |visible_details, users|
77 if notes? || visible_details.any?
78 yield(users)
79 end
80 end
81 end
82 end
83
71 84 # Returns the new status if the journal contains a status change, otherwise nil
72 85 def new_status
73 86 c = details.detect {|detail| detail.prop_key == 'status_id'}
74 87 (c && c.value) ? IssueStatus.find_by_id(c.value.to_i) : nil
75 88 end
76 89
77 90 def new_value_for(prop)
78 91 c = details.detect {|detail| detail.prop_key == prop}
79 92 c ? c.value : nil
80 93 end
81 94
82 95 def editable_by?(usr)
83 96 usr && usr.logged? && (usr.allowed_to?(:edit_issue_notes, project) || (self.user == usr && usr.allowed_to?(:edit_own_issue_notes, project)))
84 97 end
85 98
86 99 def project
87 100 journalized.respond_to?(:project) ? journalized.project : nil
88 101 end
89 102
90 103 def attachments
91 104 journalized.respond_to?(:attachments) ? journalized.attachments : nil
92 105 end
93 106
94 107 # Returns a string of css classes
95 108 def css_classes
96 109 s = 'journal'
97 110 s << ' has-notes' unless notes.blank?
98 111 s << ' has-details' unless details.blank?
99 112 s << ' private-notes' if private_notes?
100 113 s
101 114 end
102 115
103 116 def notify?
104 117 @notify != false
105 118 end
106 119
107 120 def notify=(arg)
108 121 @notify = arg
109 122 end
110 123
111 124 def notified_users
112 125 notified = journalized.notified_users
113 126 if private_notes?
114 127 notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)}
115 128 end
116 129 notified
117 130 end
118 131
119 132 def recipients
120 133 notified_users.map(&:mail)
121 134 end
122 135
123 136 def notified_watchers
124 137 notified = journalized.notified_watchers
125 138 if private_notes?
126 139 notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)}
127 140 end
128 141 notified
129 142 end
130 143
131 144 def watcher_recipients
132 145 notified_watchers.map(&:mail)
133 146 end
134 147
135 148 private
136 149
137 150 def split_private_notes
138 151 if private_notes?
139 152 if notes.present?
140 153 if details.any?
141 154 # Split the journal (notes/changes) so we don't have half-private journals
142 155 journal = Journal.new(:journalized => journalized, :user => user, :notes => nil, :private_notes => false)
143 156 journal.details = details
144 157 journal.save
145 158 self.details = []
146 159 self.created_on = journal.created_on
147 160 end
148 161 else
149 162 # Blank notes should not be private
150 163 self.private_notes = false
151 164 end
152 165 end
153 166 true
154 167 end
155 168 end
@@ -1,488 +1,489
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class Mailer < ActionMailer::Base
19 19 layout 'mailer'
20 20 helper :application
21 21 helper :issues
22 22 helper :custom_fields
23 23
24 24 include Redmine::I18n
25 25
26 26 def self.default_url_options
27 27 { :host => Setting.host_name, :protocol => Setting.protocol }
28 28 end
29 29
30 30 # Builds a mail for notifying to_users and cc_users about a new issue
31 31 def issue_add(issue, to_users, cc_users)
32 32 redmine_headers 'Project' => issue.project.identifier,
33 33 'Issue-Id' => issue.id,
34 34 'Issue-Author' => issue.author.login
35 35 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
36 36 message_id issue
37 37 references issue
38 38 @author = issue.author
39 39 @issue = issue
40 40 @users = to_users + cc_users
41 41 @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue)
42 42 mail :to => to_users.map(&:mail),
43 43 :cc => cc_users.map(&:mail),
44 44 :subject => "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}"
45 45 end
46 46
47 47 # Notifies users about a new issue
48 48 def self.deliver_issue_add(issue)
49 49 to = issue.notified_users
50 50 cc = issue.notified_watchers - to
51 51 issue.each_notification(to + cc) do |users|
52 52 Mailer.issue_add(issue, to & users, cc & users).deliver
53 53 end
54 54 end
55 55
56 56 # Builds a mail for notifying to_users and cc_users about an issue update
57 57 def issue_edit(journal, to_users, cc_users)
58 58 issue = journal.journalized
59 59 redmine_headers 'Project' => issue.project.identifier,
60 60 'Issue-Id' => issue.id,
61 61 'Issue-Author' => issue.author.login
62 62 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
63 63 message_id journal
64 64 references issue
65 65 @author = journal.user
66 66 s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] "
67 67 s << "(#{issue.status.name}) " if journal.new_value_for('status_id')
68 68 s << issue.subject
69 69 @issue = issue
70 70 @users = to_users + cc_users
71 71 @journal = journal
72 72 @journal_details = journal.visible_details(@users.first)
73 73 @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue, :anchor => "change-#{journal.id}")
74 74 mail :to => to_users.map(&:mail),
75 75 :cc => cc_users.map(&:mail),
76 76 :subject => s
77 77 end
78 78
79 79 # Notifies users about an issue update
80 80 def self.deliver_issue_edit(journal)
81 81 issue = journal.journalized.reload
82 82 to = journal.notified_users
83 83 cc = journal.notified_watchers
84 issue.each_notification(to + cc) do |users|
85 next unless journal.notes? || journal.visible_details(users.first).any?
86 Mailer.issue_edit(journal, to & users, cc & users).deliver
84 journal.each_notification(to + cc) do |users|
85 issue.each_notification(users) do |users2|
86 Mailer.issue_edit(journal, to & users2, cc & users2).deliver
87 end
87 88 end
88 89 end
89 90
90 91 def reminder(user, issues, days)
91 92 set_language_if_valid user.language
92 93 @issues = issues
93 94 @days = days
94 95 @issues_url = url_for(:controller => 'issues', :action => 'index',
95 96 :set_filter => 1, :assigned_to_id => user.id,
96 97 :sort => 'due_date:asc')
97 98 mail :to => user.mail,
98 99 :subject => l(:mail_subject_reminder, :count => issues.size, :days => days)
99 100 end
100 101
101 102 # Builds a Mail::Message object used to email users belonging to the added document's project.
102 103 #
103 104 # Example:
104 105 # document_added(document) => Mail::Message object
105 106 # Mailer.document_added(document).deliver => sends an email to the document's project recipients
106 107 def document_added(document)
107 108 redmine_headers 'Project' => document.project.identifier
108 109 @author = User.current
109 110 @document = document
110 111 @document_url = url_for(:controller => 'documents', :action => 'show', :id => document)
111 112 mail :to => document.recipients,
112 113 :subject => "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}"
113 114 end
114 115
115 116 # Builds a Mail::Message object used to email recipients of a project when an attachements are added.
116 117 #
117 118 # Example:
118 119 # attachments_added(attachments) => Mail::Message object
119 120 # Mailer.attachments_added(attachments).deliver => sends an email to the project's recipients
120 121 def attachments_added(attachments)
121 122 container = attachments.first.container
122 123 added_to = ''
123 124 added_to_url = ''
124 125 @author = attachments.first.author
125 126 case container.class.name
126 127 when 'Project'
127 128 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container)
128 129 added_to = "#{l(:label_project)}: #{container}"
129 130 recipients = container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
130 131 when 'Version'
131 132 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container.project)
132 133 added_to = "#{l(:label_version)}: #{container.name}"
133 134 recipients = container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
134 135 when 'Document'
135 136 added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id)
136 137 added_to = "#{l(:label_document)}: #{container.title}"
137 138 recipients = container.recipients
138 139 end
139 140 redmine_headers 'Project' => container.project.identifier
140 141 @attachments = attachments
141 142 @added_to = added_to
142 143 @added_to_url = added_to_url
143 144 mail :to => recipients,
144 145 :subject => "[#{container.project.name}] #{l(:label_attachment_new)}"
145 146 end
146 147
147 148 # Builds a Mail::Message object used to email recipients of a news' project when a news item is added.
148 149 #
149 150 # Example:
150 151 # news_added(news) => Mail::Message object
151 152 # Mailer.news_added(news).deliver => sends an email to the news' project recipients
152 153 def news_added(news)
153 154 redmine_headers 'Project' => news.project.identifier
154 155 @author = news.author
155 156 message_id news
156 157 references news
157 158 @news = news
158 159 @news_url = url_for(:controller => 'news', :action => 'show', :id => news)
159 160 mail :to => news.recipients,
160 161 :subject => "[#{news.project.name}] #{l(:label_news)}: #{news.title}"
161 162 end
162 163
163 164 # Builds a Mail::Message object used to email recipients of a news' project when a news comment is added.
164 165 #
165 166 # Example:
166 167 # news_comment_added(comment) => Mail::Message object
167 168 # Mailer.news_comment_added(comment) => sends an email to the news' project recipients
168 169 def news_comment_added(comment)
169 170 news = comment.commented
170 171 redmine_headers 'Project' => news.project.identifier
171 172 @author = comment.author
172 173 message_id comment
173 174 references news
174 175 @news = news
175 176 @comment = comment
176 177 @news_url = url_for(:controller => 'news', :action => 'show', :id => news)
177 178 mail :to => news.recipients,
178 179 :cc => news.watcher_recipients,
179 180 :subject => "Re: [#{news.project.name}] #{l(:label_news)}: #{news.title}"
180 181 end
181 182
182 183 # Builds a Mail::Message object used to email the recipients of the specified message that was posted.
183 184 #
184 185 # Example:
185 186 # message_posted(message) => Mail::Message object
186 187 # Mailer.message_posted(message).deliver => sends an email to the recipients
187 188 def message_posted(message)
188 189 redmine_headers 'Project' => message.project.identifier,
189 190 'Topic-Id' => (message.parent_id || message.id)
190 191 @author = message.author
191 192 message_id message
192 193 references message.root
193 194 recipients = message.recipients
194 195 cc = ((message.root.watcher_recipients + message.board.watcher_recipients).uniq - recipients)
195 196 @message = message
196 197 @message_url = url_for(message.event_url)
197 198 mail :to => recipients,
198 199 :cc => cc,
199 200 :subject => "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}"
200 201 end
201 202
202 203 # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was added.
203 204 #
204 205 # Example:
205 206 # wiki_content_added(wiki_content) => Mail::Message object
206 207 # Mailer.wiki_content_added(wiki_content).deliver => sends an email to the project's recipients
207 208 def wiki_content_added(wiki_content)
208 209 redmine_headers 'Project' => wiki_content.project.identifier,
209 210 'Wiki-Page-Id' => wiki_content.page.id
210 211 @author = wiki_content.author
211 212 message_id wiki_content
212 213 recipients = wiki_content.recipients
213 214 cc = wiki_content.page.wiki.watcher_recipients - recipients
214 215 @wiki_content = wiki_content
215 216 @wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
216 217 :project_id => wiki_content.project,
217 218 :id => wiki_content.page.title)
218 219 mail :to => recipients,
219 220 :cc => cc,
220 221 :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}"
221 222 end
222 223
223 224 # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was updated.
224 225 #
225 226 # Example:
226 227 # wiki_content_updated(wiki_content) => Mail::Message object
227 228 # Mailer.wiki_content_updated(wiki_content).deliver => sends an email to the project's recipients
228 229 def wiki_content_updated(wiki_content)
229 230 redmine_headers 'Project' => wiki_content.project.identifier,
230 231 'Wiki-Page-Id' => wiki_content.page.id
231 232 @author = wiki_content.author
232 233 message_id wiki_content
233 234 recipients = wiki_content.recipients
234 235 cc = wiki_content.page.wiki.watcher_recipients + wiki_content.page.watcher_recipients - recipients
235 236 @wiki_content = wiki_content
236 237 @wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
237 238 :project_id => wiki_content.project,
238 239 :id => wiki_content.page.title)
239 240 @wiki_diff_url = url_for(:controller => 'wiki', :action => 'diff',
240 241 :project_id => wiki_content.project, :id => wiki_content.page.title,
241 242 :version => wiki_content.version)
242 243 mail :to => recipients,
243 244 :cc => cc,
244 245 :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_updated, :id => wiki_content.page.pretty_title)}"
245 246 end
246 247
247 248 # Builds a Mail::Message object used to email the specified user their account information.
248 249 #
249 250 # Example:
250 251 # account_information(user, password) => Mail::Message object
251 252 # Mailer.account_information(user, password).deliver => sends account information to the user
252 253 def account_information(user, password)
253 254 set_language_if_valid user.language
254 255 @user = user
255 256 @password = password
256 257 @login_url = url_for(:controller => 'account', :action => 'login')
257 258 mail :to => user.mail,
258 259 :subject => l(:mail_subject_register, Setting.app_title)
259 260 end
260 261
261 262 # Builds a Mail::Message object used to email all active administrators of an account activation request.
262 263 #
263 264 # Example:
264 265 # account_activation_request(user) => Mail::Message object
265 266 # Mailer.account_activation_request(user).deliver => sends an email to all active administrators
266 267 def account_activation_request(user)
267 268 # Send the email to all active administrators
268 269 recipients = User.active.where(:admin => true).all.collect { |u| u.mail }.compact
269 270 @user = user
270 271 @url = url_for(:controller => 'users', :action => 'index',
271 272 :status => User::STATUS_REGISTERED,
272 273 :sort_key => 'created_on', :sort_order => 'desc')
273 274 mail :to => recipients,
274 275 :subject => l(:mail_subject_account_activation_request, Setting.app_title)
275 276 end
276 277
277 278 # Builds a Mail::Message object used to email the specified user that their account was activated by an administrator.
278 279 #
279 280 # Example:
280 281 # account_activated(user) => Mail::Message object
281 282 # Mailer.account_activated(user).deliver => sends an email to the registered user
282 283 def account_activated(user)
283 284 set_language_if_valid user.language
284 285 @user = user
285 286 @login_url = url_for(:controller => 'account', :action => 'login')
286 287 mail :to => user.mail,
287 288 :subject => l(:mail_subject_register, Setting.app_title)
288 289 end
289 290
290 291 def lost_password(token)
291 292 set_language_if_valid(token.user.language)
292 293 @token = token
293 294 @url = url_for(:controller => 'account', :action => 'lost_password', :token => token.value)
294 295 mail :to => token.user.mail,
295 296 :subject => l(:mail_subject_lost_password, Setting.app_title)
296 297 end
297 298
298 299 def register(token)
299 300 set_language_if_valid(token.user.language)
300 301 @token = token
301 302 @url = url_for(:controller => 'account', :action => 'activate', :token => token.value)
302 303 mail :to => token.user.mail,
303 304 :subject => l(:mail_subject_register, Setting.app_title)
304 305 end
305 306
306 307 def test_email(user)
307 308 set_language_if_valid(user.language)
308 309 @url = url_for(:controller => 'welcome')
309 310 mail :to => user.mail,
310 311 :subject => 'Redmine test'
311 312 end
312 313
313 314 # Sends reminders to issue assignees
314 315 # Available options:
315 316 # * :days => how many days in the future to remind about (defaults to 7)
316 317 # * :tracker => id of tracker for filtering issues (defaults to all trackers)
317 318 # * :project => id or identifier of project to process (defaults to all projects)
318 319 # * :users => array of user/group ids who should be reminded
319 320 def self.reminders(options={})
320 321 days = options[:days] || 7
321 322 project = options[:project] ? Project.find(options[:project]) : nil
322 323 tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil
323 324 user_ids = options[:users]
324 325
325 326 scope = Issue.open.where("#{Issue.table_name}.assigned_to_id IS NOT NULL" +
326 327 " AND #{Project.table_name}.status = #{Project::STATUS_ACTIVE}" +
327 328 " AND #{Issue.table_name}.due_date <= ?", days.day.from_now.to_date
328 329 )
329 330 scope = scope.where(:assigned_to_id => user_ids) if user_ids.present?
330 331 scope = scope.where(:project_id => project.id) if project
331 332 scope = scope.where(:tracker_id => tracker.id) if tracker
332 333
333 334 issues_by_assignee = scope.includes(:status, :assigned_to, :project, :tracker).all.group_by(&:assigned_to)
334 335 issues_by_assignee.keys.each do |assignee|
335 336 if assignee.is_a?(Group)
336 337 assignee.users.each do |user|
337 338 issues_by_assignee[user] ||= []
338 339 issues_by_assignee[user] += issues_by_assignee[assignee]
339 340 end
340 341 end
341 342 end
342 343
343 344 issues_by_assignee.each do |assignee, issues|
344 345 reminder(assignee, issues, days).deliver if assignee.is_a?(User) && assignee.active?
345 346 end
346 347 end
347 348
348 349 # Activates/desactivates email deliveries during +block+
349 350 def self.with_deliveries(enabled = true, &block)
350 351 was_enabled = ActionMailer::Base.perform_deliveries
351 352 ActionMailer::Base.perform_deliveries = !!enabled
352 353 yield
353 354 ensure
354 355 ActionMailer::Base.perform_deliveries = was_enabled
355 356 end
356 357
357 358 # Sends emails synchronously in the given block
358 359 def self.with_synched_deliveries(&block)
359 360 saved_method = ActionMailer::Base.delivery_method
360 361 if m = saved_method.to_s.match(%r{^async_(.+)$})
361 362 synched_method = m[1]
362 363 ActionMailer::Base.delivery_method = synched_method.to_sym
363 364 ActionMailer::Base.send "#{synched_method}_settings=", ActionMailer::Base.send("async_#{synched_method}_settings")
364 365 end
365 366 yield
366 367 ensure
367 368 ActionMailer::Base.delivery_method = saved_method
368 369 end
369 370
370 371 def mail(headers={})
371 372 headers.merge! 'X-Mailer' => 'Redmine',
372 373 'X-Redmine-Host' => Setting.host_name,
373 374 'X-Redmine-Site' => Setting.app_title,
374 375 'X-Auto-Response-Suppress' => 'OOF',
375 376 'Auto-Submitted' => 'auto-generated',
376 377 'From' => Setting.mail_from,
377 378 'List-Id' => "<#{Setting.mail_from.to_s.gsub('@', '.')}>"
378 379
379 380 # Removes the author from the recipients and cc
380 381 # if the author does not want to receive notifications
381 382 # about what the author do
382 383 if @author && @author.logged? && @author.pref.no_self_notified
383 384 headers[:to].delete(@author.mail) if headers[:to].is_a?(Array)
384 385 headers[:cc].delete(@author.mail) if headers[:cc].is_a?(Array)
385 386 end
386 387
387 388 if @author && @author.logged?
388 389 redmine_headers 'Sender' => @author.login
389 390 end
390 391
391 392 # Blind carbon copy recipients
392 393 if Setting.bcc_recipients?
393 394 headers[:bcc] = [headers[:to], headers[:cc]].flatten.uniq.reject(&:blank?)
394 395 headers[:to] = nil
395 396 headers[:cc] = nil
396 397 end
397 398
398 399 if @message_id_object
399 400 headers[:message_id] = "<#{self.class.message_id_for(@message_id_object)}>"
400 401 end
401 402 if @references_objects
402 403 headers[:references] = @references_objects.collect {|o| "<#{self.class.references_for(o)}>"}.join(' ')
403 404 end
404 405
405 406 super headers do |format|
406 407 format.text
407 408 format.html unless Setting.plain_text_mail?
408 409 end
409 410
410 411 set_language_if_valid @initial_language
411 412 end
412 413
413 414 def initialize(*args)
414 415 @initial_language = current_language
415 416 set_language_if_valid Setting.default_language
416 417 super
417 418 end
418 419
419 420 def self.deliver_mail(mail)
420 421 return false if mail.to.blank? && mail.cc.blank? && mail.bcc.blank?
421 422 begin
422 423 # Log errors when raise_delivery_errors is set to false, Rails does not
423 424 mail.raise_delivery_errors = true
424 425 super
425 426 rescue Exception => e
426 427 if ActionMailer::Base.raise_delivery_errors
427 428 raise e
428 429 else
429 430 Rails.logger.error "Email delivery error: #{e.message}"
430 431 end
431 432 end
432 433 end
433 434
434 435 def self.method_missing(method, *args, &block)
435 436 if m = method.to_s.match(%r{^deliver_(.+)$})
436 437 ActiveSupport::Deprecation.warn "Mailer.deliver_#{m[1]}(*args) is deprecated. Use Mailer.#{m[1]}(*args).deliver instead."
437 438 send(m[1], *args).deliver
438 439 else
439 440 super
440 441 end
441 442 end
442 443
443 444 private
444 445
445 446 # Appends a Redmine header field (name is prepended with 'X-Redmine-')
446 447 def redmine_headers(h)
447 448 h.each { |k,v| headers["X-Redmine-#{k}"] = v.to_s }
448 449 end
449 450
450 451 def self.token_for(object, rand=true)
451 452 timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on)
452 453 hash = [
453 454 "redmine",
454 455 "#{object.class.name.demodulize.underscore}-#{object.id}",
455 456 timestamp.strftime("%Y%m%d%H%M%S")
456 457 ]
457 458 if rand
458 459 hash << Redmine::Utils.random_hex(8)
459 460 end
460 461 host = Setting.mail_from.to_s.gsub(%r{^.*@}, '')
461 462 host = "#{::Socket.gethostname}.redmine" if host.empty?
462 463 "#{hash.join('.')}@#{host}"
463 464 end
464 465
465 466 # Returns a Message-Id for the given object
466 467 def self.message_id_for(object)
467 468 token_for(object, true)
468 469 end
469 470
470 471 # Returns a uniq token for a given object referenced by all notifications
471 472 # related to this object
472 473 def self.references_for(object)
473 474 token_for(object, false)
474 475 end
475 476
476 477 def message_id(object)
477 478 @message_id_object = object
478 479 end
479 480
480 481 def references(object)
481 482 @references_objects ||= []
482 483 @references_objects << object
483 484 end
484 485
485 486 def mylogger
486 487 Rails.logger
487 488 end
488 489 end
@@ -1,704 +1,720
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class MailerTest < ActiveSupport::TestCase
21 21 include Redmine::I18n
22 22 include ActionDispatch::Assertions::SelectorAssertions
23 23 fixtures :projects, :enabled_modules, :issues, :users, :members,
24 24 :member_roles, :roles, :documents, :attachments, :news,
25 25 :tokens, :journals, :journal_details, :changesets,
26 26 :trackers, :projects_trackers,
27 27 :issue_statuses, :enumerations, :messages, :boards, :repositories,
28 28 :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions,
29 29 :versions,
30 30 :comments
31 31
32 32 def setup
33 33 ActionMailer::Base.deliveries.clear
34 34 Setting.host_name = 'mydomain.foo'
35 35 Setting.protocol = 'http'
36 36 Setting.plain_text_mail = '0'
37 37 end
38 38
39 39 def test_generated_links_in_emails
40 40 Setting.default_language = 'en'
41 41 Setting.host_name = 'mydomain.foo'
42 42 Setting.protocol = 'https'
43 43
44 44 journal = Journal.find(3)
45 45 assert Mailer.deliver_issue_edit(journal)
46 46
47 47 mail = last_email
48 48 assert_not_nil mail
49 49
50 50 assert_select_email do
51 51 # link to the main ticket
52 52 assert_select 'a[href=?]',
53 53 'https://mydomain.foo/issues/2#change-3',
54 54 :text => 'Feature request #2: Add ingredients categories'
55 55 # link to a referenced ticket
56 56 assert_select 'a[href=?][title=?]',
57 57 'https://mydomain.foo/issues/1',
58 58 'Can&#x27;t print recipes (New)',
59 59 :text => '#1'
60 60 # link to a changeset
61 61 assert_select 'a[href=?][title=?]',
62 62 'https://mydomain.foo/projects/ecookbook/repository/revisions/2',
63 63 'This commit fixes #1, #2 and references #1 &amp; #3',
64 64 :text => 'r2'
65 65 # link to a description diff
66 66 assert_select 'a[href=?][title=?]',
67 67 'https://mydomain.foo/journals/diff/3?detail_id=4',
68 68 'View differences',
69 69 :text => 'diff'
70 70 # link to an attachment
71 71 assert_select 'a[href=?]',
72 72 'https://mydomain.foo/attachments/download/4/source.rb',
73 73 :text => 'source.rb'
74 74 end
75 75 end
76 76
77 77 def test_generated_links_with_prefix
78 78 Setting.default_language = 'en'
79 79 relative_url_root = Redmine::Utils.relative_url_root
80 80 Setting.host_name = 'mydomain.foo/rdm'
81 81 Setting.protocol = 'http'
82 82
83 83 journal = Journal.find(3)
84 84 assert Mailer.deliver_issue_edit(journal)
85 85
86 86 mail = last_email
87 87 assert_not_nil mail
88 88
89 89 assert_select_email do
90 90 # link to the main ticket
91 91 assert_select 'a[href=?]',
92 92 'http://mydomain.foo/rdm/issues/2#change-3',
93 93 :text => 'Feature request #2: Add ingredients categories'
94 94 # link to a referenced ticket
95 95 assert_select 'a[href=?][title=?]',
96 96 'http://mydomain.foo/rdm/issues/1',
97 97 'Can&#x27;t print recipes (New)',
98 98 :text => '#1'
99 99 # link to a changeset
100 100 assert_select 'a[href=?][title=?]',
101 101 'http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2',
102 102 'This commit fixes #1, #2 and references #1 &amp; #3',
103 103 :text => 'r2'
104 104 # link to a description diff
105 105 assert_select 'a[href=?][title=?]',
106 106 'http://mydomain.foo/rdm/journals/diff/3?detail_id=4',
107 107 'View differences',
108 108 :text => 'diff'
109 109 # link to an attachment
110 110 assert_select 'a[href=?]',
111 111 'http://mydomain.foo/rdm/attachments/download/4/source.rb',
112 112 :text => 'source.rb'
113 113 end
114 114 end
115 115
116 116 def test_generated_links_with_prefix_and_no_relative_url_root
117 117 Setting.default_language = 'en'
118 118 relative_url_root = Redmine::Utils.relative_url_root
119 119 Setting.host_name = 'mydomain.foo/rdm'
120 120 Setting.protocol = 'http'
121 121 Redmine::Utils.relative_url_root = nil
122 122
123 123 journal = Journal.find(3)
124 124 assert Mailer.deliver_issue_edit(journal)
125 125
126 126 mail = last_email
127 127 assert_not_nil mail
128 128
129 129 assert_select_email do
130 130 # link to the main ticket
131 131 assert_select 'a[href=?]',
132 132 'http://mydomain.foo/rdm/issues/2#change-3',
133 133 :text => 'Feature request #2: Add ingredients categories'
134 134 # link to a referenced ticket
135 135 assert_select 'a[href=?][title=?]',
136 136 'http://mydomain.foo/rdm/issues/1',
137 137 'Can&#x27;t print recipes (New)',
138 138 :text => '#1'
139 139 # link to a changeset
140 140 assert_select 'a[href=?][title=?]',
141 141 'http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2',
142 142 'This commit fixes #1, #2 and references #1 &amp; #3',
143 143 :text => 'r2'
144 144 # link to a description diff
145 145 assert_select 'a[href=?][title=?]',
146 146 'http://mydomain.foo/rdm/journals/diff/3?detail_id=4',
147 147 'View differences',
148 148 :text => 'diff'
149 149 # link to an attachment
150 150 assert_select 'a[href=?]',
151 151 'http://mydomain.foo/rdm/attachments/download/4/source.rb',
152 152 :text => 'source.rb'
153 153 end
154 154 ensure
155 155 # restore it
156 156 Redmine::Utils.relative_url_root = relative_url_root
157 157 end
158 158
159 159 def test_email_headers
160 160 issue = Issue.find(1)
161 161 Mailer.deliver_issue_add(issue)
162 162 mail = last_email
163 163 assert_not_nil mail
164 164 assert_equal 'OOF', mail.header['X-Auto-Response-Suppress'].to_s
165 165 assert_equal 'auto-generated', mail.header['Auto-Submitted'].to_s
166 166 assert_equal '<redmine.example.net>', mail.header['List-Id'].to_s
167 167 end
168 168
169 169 def test_email_headers_should_include_sender
170 170 issue = Issue.find(1)
171 171 Mailer.deliver_issue_add(issue)
172 172 mail = last_email
173 173 assert_equal issue.author.login, mail.header['X-Redmine-Sender'].to_s
174 174 end
175 175
176 176 def test_plain_text_mail
177 177 Setting.plain_text_mail = 1
178 178 journal = Journal.find(2)
179 179 Mailer.deliver_issue_edit(journal)
180 180 mail = last_email
181 181 assert_equal "text/plain; charset=UTF-8", mail.content_type
182 182 assert_equal 0, mail.parts.size
183 183 assert !mail.encoded.include?('href')
184 184 end
185 185
186 186 def test_html_mail
187 187 Setting.plain_text_mail = 0
188 188 journal = Journal.find(2)
189 189 Mailer.deliver_issue_edit(journal)
190 190 mail = last_email
191 191 assert_equal 2, mail.parts.size
192 192 assert mail.encoded.include?('href')
193 193 end
194 194
195 195 def test_from_header
196 196 with_settings :mail_from => 'redmine@example.net' do
197 197 Mailer.test_email(User.find(1)).deliver
198 198 end
199 199 mail = last_email
200 200 assert_equal 'redmine@example.net', mail.from_addrs.first
201 201 end
202 202
203 203 def test_from_header_with_phrase
204 204 with_settings :mail_from => 'Redmine app <redmine@example.net>' do
205 205 Mailer.test_email(User.find(1)).deliver
206 206 end
207 207 mail = last_email
208 208 assert_equal 'redmine@example.net', mail.from_addrs.first
209 209 assert_equal 'Redmine app <redmine@example.net>', mail.header['From'].to_s
210 210 end
211 211
212 212 def test_should_not_send_email_without_recipient
213 213 news = News.first
214 214 user = news.author
215 215 # Remove members except news author
216 216 news.project.memberships.each {|m| m.destroy unless m.user == user}
217 217
218 218 user.pref.no_self_notified = false
219 219 user.pref.save
220 220 User.current = user
221 221 Mailer.news_added(news.reload).deliver
222 222 assert_equal 1, last_email.bcc.size
223 223
224 224 # nobody to notify
225 225 user.pref.no_self_notified = true
226 226 user.pref.save
227 227 User.current = user
228 228 ActionMailer::Base.deliveries.clear
229 229 Mailer.news_added(news.reload).deliver
230 230 assert ActionMailer::Base.deliveries.empty?
231 231 end
232 232
233 233 def test_issue_add_message_id
234 234 issue = Issue.find(2)
235 235 Mailer.deliver_issue_add(issue)
236 236 mail = last_email
237 237 assert_match /^redmine\.issue-2\.20060719190421\.[a-f0-9]+@example\.net/, mail.message_id
238 238 assert_include "redmine.issue-2.20060719190421@example.net", mail.references
239 239 end
240 240
241 241 def test_issue_edit_message_id
242 242 journal = Journal.find(3)
243 243 journal.issue = Issue.find(2)
244 244
245 245 Mailer.deliver_issue_edit(journal)
246 246 mail = last_email
247 247 assert_match /^redmine\.journal-3\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
248 248 assert_include "redmine.issue-2.20060719190421@example.net", mail.references
249 249 assert_select_email do
250 250 # link to the update
251 251 assert_select "a[href=?]",
252 252 "http://mydomain.foo/issues/#{journal.journalized_id}#change-#{journal.id}"
253 253 end
254 254 end
255 255
256 256 def test_message_posted_message_id
257 257 message = Message.find(1)
258 258 Mailer.message_posted(message).deliver
259 259 mail = last_email
260 260 assert_match /^redmine\.message-1\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
261 261 assert_include "redmine.message-1.20070512151532@example.net", mail.references
262 262 assert_select_email do
263 263 # link to the message
264 264 assert_select "a[href=?]",
265 265 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.id}",
266 266 :text => message.subject
267 267 end
268 268 end
269 269
270 270 def test_reply_posted_message_id
271 271 message = Message.find(3)
272 272 Mailer.message_posted(message).deliver
273 273 mail = last_email
274 274 assert_match /^redmine\.message-3\.\d+\.[a-f0-9]+@example\.net/, mail.message_id
275 275 assert_include "redmine.message-1.20070512151532@example.net", mail.references
276 276 assert_select_email do
277 277 # link to the reply
278 278 assert_select "a[href=?]",
279 279 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.root.id}?r=#{message.id}#message-#{message.id}",
280 280 :text => message.subject
281 281 end
282 282 end
283 283
284 284 test "#issue_add should notify project members" do
285 285 issue = Issue.find(1)
286 286 assert Mailer.deliver_issue_add(issue)
287 287 assert last_email.bcc.include?('dlopper@somenet.foo')
288 288 end
289 289
290 290 test "#issue_add should not notify project members that are not allow to view the issue" do
291 291 issue = Issue.find(1)
292 292 Role.find(2).remove_permission!(:view_issues)
293 293 assert Mailer.deliver_issue_add(issue)
294 294 assert !last_email.bcc.include?('dlopper@somenet.foo')
295 295 end
296 296
297 297 test "#issue_add should notify issue watchers" do
298 298 issue = Issue.find(1)
299 299 user = User.find(9)
300 300 # minimal email notification options
301 301 user.pref.no_self_notified = '1'
302 302 user.pref.save
303 303 user.mail_notification = false
304 304 user.save
305 305
306 306 Watcher.create!(:watchable => issue, :user => user)
307 307 assert Mailer.deliver_issue_add(issue)
308 308 assert last_email.bcc.include?(user.mail)
309 309 end
310 310
311 311 test "#issue_add should not notify watchers not allowed to view the issue" do
312 312 issue = Issue.find(1)
313 313 user = User.find(9)
314 314 Watcher.create!(:watchable => issue, :user => user)
315 315 Role.non_member.remove_permission!(:view_issues)
316 316 assert Mailer.deliver_issue_add(issue)
317 317 assert !last_email.bcc.include?(user.mail)
318 318 end
319 319
320 320 # test mailer methods for each language
321 321 def test_issue_add
322 322 issue = Issue.find(1)
323 323 valid_languages.each do |lang|
324 324 Setting.default_language = lang.to_s
325 325 assert Mailer.deliver_issue_add(issue)
326 326 end
327 327 end
328 328
329 329 def test_issue_edit
330 330 journal = Journal.find(1)
331 331 valid_languages.each do |lang|
332 332 Setting.default_language = lang.to_s
333 333 assert Mailer.deliver_issue_edit(journal)
334 334 end
335 335 end
336 336
337 337 def test_issue_edit_should_send_private_notes_to_users_with_permission_only
338 338 journal = Journal.find(1)
339 339 journal.private_notes = true
340 340 journal.save!
341 341
342 342 Role.find(2).add_permission! :view_private_notes
343 343 Mailer.deliver_issue_edit(journal)
344 344 assert_equal %w(dlopper@somenet.foo jsmith@somenet.foo), ActionMailer::Base.deliveries.last.bcc.sort
345 345
346 346 Role.find(2).remove_permission! :view_private_notes
347 347 Mailer.deliver_issue_edit(journal)
348 348 assert_equal %w(jsmith@somenet.foo), ActionMailer::Base.deliveries.last.bcc.sort
349 349 end
350 350
351 351 def test_issue_edit_should_send_private_notes_to_watchers_with_permission_only
352 352 Issue.find(1).set_watcher(User.find_by_login('someone'))
353 353 journal = Journal.find(1)
354 354 journal.private_notes = true
355 355 journal.save!
356 356
357 357 Role.non_member.add_permission! :view_private_notes
358 358 Mailer.deliver_issue_edit(journal)
359 359 assert_include 'someone@foo.bar', ActionMailer::Base.deliveries.last.bcc.sort
360 360
361 361 Role.non_member.remove_permission! :view_private_notes
362 362 Mailer.deliver_issue_edit(journal)
363 363 assert_not_include 'someone@foo.bar', ActionMailer::Base.deliveries.last.bcc.sort
364 364 end
365 365
366 366 def test_issue_edit_should_mark_private_notes
367 367 journal = Journal.find(2)
368 368 journal.private_notes = true
369 369 journal.save!
370 370
371 371 with_settings :default_language => 'en' do
372 372 Mailer.deliver_issue_edit(journal)
373 373 end
374 374 assert_mail_body_match '(Private notes)', last_email
375 375 end
376 376
377 def test_issue_edit_with_relation_should_notify_users_who_can_see_the_related_issue
378 issue = Issue.generate!
379 private_issue = Issue.generate!(:is_private => true)
380 IssueRelation.create!(:issue_from => issue, :issue_to => private_issue, :relation_type => 'relates')
381 issue.reload
382 assert_equal 1, issue.journals.size
383 journal = issue.journals.first
384 ActionMailer::Base.deliveries.clear
385
386 Mailer.deliver_issue_edit(journal)
387 last_email.bcc.each do |email|
388 user = User.find_by_mail(email)
389 assert private_issue.visible?(user), "Issue was not visible to #{user}"
390 end
391 end
392
377 393 def test_document_added
378 394 document = Document.find(1)
379 395 valid_languages.each do |lang|
380 396 Setting.default_language = lang.to_s
381 397 assert Mailer.document_added(document).deliver
382 398 end
383 399 end
384 400
385 401 def test_attachments_added
386 402 attachements = [ Attachment.find_by_container_type('Document') ]
387 403 valid_languages.each do |lang|
388 404 Setting.default_language = lang.to_s
389 405 assert Mailer.attachments_added(attachements).deliver
390 406 end
391 407 end
392 408
393 409 def test_version_file_added
394 410 attachements = [ Attachment.find_by_container_type('Version') ]
395 411 assert Mailer.attachments_added(attachements).deliver
396 412 assert_not_nil last_email.bcc
397 413 assert last_email.bcc.any?
398 414 assert_select_email do
399 415 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
400 416 end
401 417 end
402 418
403 419 def test_project_file_added
404 420 attachements = [ Attachment.find_by_container_type('Project') ]
405 421 assert Mailer.attachments_added(attachements).deliver
406 422 assert_not_nil last_email.bcc
407 423 assert last_email.bcc.any?
408 424 assert_select_email do
409 425 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
410 426 end
411 427 end
412 428
413 429 def test_news_added
414 430 news = News.first
415 431 valid_languages.each do |lang|
416 432 Setting.default_language = lang.to_s
417 433 assert Mailer.news_added(news).deliver
418 434 end
419 435 end
420 436
421 437 def test_news_comment_added
422 438 comment = Comment.find(2)
423 439 valid_languages.each do |lang|
424 440 Setting.default_language = lang.to_s
425 441 assert Mailer.news_comment_added(comment).deliver
426 442 end
427 443 end
428 444
429 445 def test_message_posted
430 446 message = Message.first
431 447 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
432 448 recipients = recipients.compact.uniq
433 449 valid_languages.each do |lang|
434 450 Setting.default_language = lang.to_s
435 451 assert Mailer.message_posted(message).deliver
436 452 end
437 453 end
438 454
439 455 def test_wiki_content_added
440 456 content = WikiContent.find(1)
441 457 valid_languages.each do |lang|
442 458 Setting.default_language = lang.to_s
443 459 assert_difference 'ActionMailer::Base.deliveries.size' do
444 460 assert Mailer.wiki_content_added(content).deliver
445 461 assert_select_email do
446 462 assert_select 'a[href=?]',
447 463 'http://mydomain.foo/projects/ecookbook/wiki/CookBook_documentation',
448 464 :text => 'CookBook documentation'
449 465 end
450 466 end
451 467 end
452 468 end
453 469
454 470 def test_wiki_content_updated
455 471 content = WikiContent.find(1)
456 472 valid_languages.each do |lang|
457 473 Setting.default_language = lang.to_s
458 474 assert_difference 'ActionMailer::Base.deliveries.size' do
459 475 assert Mailer.wiki_content_updated(content).deliver
460 476 assert_select_email do
461 477 assert_select 'a[href=?]',
462 478 'http://mydomain.foo/projects/ecookbook/wiki/CookBook_documentation',
463 479 :text => 'CookBook documentation'
464 480 end
465 481 end
466 482 end
467 483 end
468 484
469 485 def test_account_information
470 486 user = User.find(2)
471 487 valid_languages.each do |lang|
472 488 user.update_attribute :language, lang.to_s
473 489 user.reload
474 490 assert Mailer.account_information(user, 'pAsswORd').deliver
475 491 end
476 492 end
477 493
478 494 def test_lost_password
479 495 token = Token.find(2)
480 496 valid_languages.each do |lang|
481 497 token.user.update_attribute :language, lang.to_s
482 498 token.reload
483 499 assert Mailer.lost_password(token).deliver
484 500 end
485 501 end
486 502
487 503 def test_register
488 504 token = Token.find(1)
489 505 Setting.host_name = 'redmine.foo'
490 506 Setting.protocol = 'https'
491 507
492 508 valid_languages.each do |lang|
493 509 token.user.update_attribute :language, lang.to_s
494 510 token.reload
495 511 ActionMailer::Base.deliveries.clear
496 512 assert Mailer.register(token).deliver
497 513 mail = last_email
498 514 assert_select_email do
499 515 assert_select "a[href=?]",
500 516 "https://redmine.foo/account/activate?token=#{token.value}",
501 517 :text => "https://redmine.foo/account/activate?token=#{token.value}"
502 518 end
503 519 end
504 520 end
505 521
506 522 def test_test
507 523 user = User.find(1)
508 524 valid_languages.each do |lang|
509 525 user.update_attribute :language, lang.to_s
510 526 assert Mailer.test_email(user).deliver
511 527 end
512 528 end
513 529
514 530 def test_reminders
515 531 Mailer.reminders(:days => 42)
516 532 assert_equal 1, ActionMailer::Base.deliveries.size
517 533 mail = last_email
518 534 assert mail.bcc.include?('dlopper@somenet.foo')
519 535 assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
520 536 assert_equal '1 issue(s) due in the next 42 days', mail.subject
521 537 end
522 538
523 539 def test_reminders_should_not_include_closed_issues
524 540 with_settings :default_language => 'en' do
525 541 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 5,
526 542 :subject => 'Closed issue', :assigned_to_id => 3,
527 543 :due_date => 5.days.from_now,
528 544 :author_id => 2)
529 545 ActionMailer::Base.deliveries.clear
530 546
531 547 Mailer.reminders(:days => 42)
532 548 assert_equal 1, ActionMailer::Base.deliveries.size
533 549 mail = last_email
534 550 assert mail.bcc.include?('dlopper@somenet.foo')
535 551 assert_mail_body_no_match 'Closed issue', mail
536 552 end
537 553 end
538 554
539 555 def test_reminders_for_users
540 556 Mailer.reminders(:days => 42, :users => ['5'])
541 557 assert_equal 0, ActionMailer::Base.deliveries.size # No mail for dlopper
542 558 Mailer.reminders(:days => 42, :users => ['3'])
543 559 assert_equal 1, ActionMailer::Base.deliveries.size # No mail for dlopper
544 560 mail = last_email
545 561 assert mail.bcc.include?('dlopper@somenet.foo')
546 562 assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
547 563 end
548 564
549 565 def test_reminder_should_include_issues_assigned_to_groups
550 566 with_settings :default_language => 'en' do
551 567 group = Group.generate!
552 568 group.users << User.find(2)
553 569 group.users << User.find(3)
554 570
555 571 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 1,
556 572 :subject => 'Assigned to group', :assigned_to => group,
557 573 :due_date => 5.days.from_now,
558 574 :author_id => 2)
559 575 ActionMailer::Base.deliveries.clear
560 576
561 577 Mailer.reminders(:days => 7)
562 578 assert_equal 2, ActionMailer::Base.deliveries.size
563 579 assert_equal %w(dlopper@somenet.foo jsmith@somenet.foo), ActionMailer::Base.deliveries.map(&:bcc).flatten.sort
564 580 ActionMailer::Base.deliveries.each do |mail|
565 581 assert_mail_body_match 'Assigned to group', mail
566 582 end
567 583 end
568 584 end
569 585
570 586 def test_mailer_should_not_change_locale
571 587 Setting.default_language = 'en'
572 588 # Set current language to italian
573 589 set_language_if_valid 'it'
574 590 # Send an email to a french user
575 591 user = User.find(1)
576 592 user.language = 'fr'
577 593 Mailer.account_activated(user).deliver
578 594 mail = last_email
579 595 assert_mail_body_match 'Votre compte', mail
580 596
581 597 assert_equal :it, current_language
582 598 end
583 599
584 600 def test_with_deliveries_off
585 601 Mailer.with_deliveries false do
586 602 Mailer.test_email(User.find(1)).deliver
587 603 end
588 604 assert ActionMailer::Base.deliveries.empty?
589 605 # should restore perform_deliveries
590 606 assert ActionMailer::Base.perform_deliveries
591 607 end
592 608
593 609 def test_layout_should_include_the_emails_header
594 610 with_settings :emails_header => "*Header content*" do
595 611 with_settings :plain_text_mail => 0 do
596 612 assert Mailer.test_email(User.find(1)).deliver
597 613 assert_select_email do
598 614 assert_select ".header" do
599 615 assert_select "strong", :text => "Header content"
600 616 end
601 617 end
602 618 end
603 619 with_settings :plain_text_mail => 1 do
604 620 assert Mailer.test_email(User.find(1)).deliver
605 621 mail = last_email
606 622 assert_not_nil mail
607 623 assert_include "*Header content*", mail.body.decoded
608 624 end
609 625 end
610 626 end
611 627
612 628 def test_layout_should_not_include_empty_emails_header
613 629 with_settings :emails_header => "", :plain_text_mail => 0 do
614 630 assert Mailer.test_email(User.find(1)).deliver
615 631 assert_select_email do
616 632 assert_select ".header", false
617 633 end
618 634 end
619 635 end
620 636
621 637 def test_layout_should_include_the_emails_footer
622 638 with_settings :emails_footer => "*Footer content*" do
623 639 with_settings :plain_text_mail => 0 do
624 640 assert Mailer.test_email(User.find(1)).deliver
625 641 assert_select_email do
626 642 assert_select ".footer" do
627 643 assert_select "strong", :text => "Footer content"
628 644 end
629 645 end
630 646 end
631 647 with_settings :plain_text_mail => 1 do
632 648 assert Mailer.test_email(User.find(1)).deliver
633 649 mail = last_email
634 650 assert_not_nil mail
635 651 assert_include "\n-- \n", mail.body.decoded
636 652 assert_include "*Footer content*", mail.body.decoded
637 653 end
638 654 end
639 655 end
640 656
641 657 def test_layout_should_not_include_empty_emails_footer
642 658 with_settings :emails_footer => "" do
643 659 with_settings :plain_text_mail => 0 do
644 660 assert Mailer.test_email(User.find(1)).deliver
645 661 assert_select_email do
646 662 assert_select ".footer", false
647 663 end
648 664 end
649 665 with_settings :plain_text_mail => 1 do
650 666 assert Mailer.test_email(User.find(1)).deliver
651 667 mail = last_email
652 668 assert_not_nil mail
653 669 assert_not_include "\n-- \n", mail.body.decoded
654 670 end
655 671 end
656 672 end
657 673
658 674 def test_should_escape_html_templates_only
659 675 Issue.generate!(:project_id => 1, :tracker_id => 1, :subject => 'Subject with a <tag>')
660 676 mail = last_email
661 677 assert_equal 2, mail.parts.size
662 678 assert_include '<tag>', text_part.body.encoded
663 679 assert_include '&lt;tag&gt;', html_part.body.encoded
664 680 end
665 681
666 682 def test_should_raise_delivery_errors_when_raise_delivery_errors_is_true
667 683 mail = Mailer.test_email(User.find(1))
668 684 mail.delivery_method.stubs(:deliver!).raises(Exception.new("delivery error"))
669 685
670 686 ActionMailer::Base.raise_delivery_errors = true
671 687 assert_raise Exception, "delivery error" do
672 688 mail.deliver
673 689 end
674 690 ensure
675 691 ActionMailer::Base.raise_delivery_errors = false
676 692 end
677 693
678 694 def test_should_log_delivery_errors_when_raise_delivery_errors_is_false
679 695 mail = Mailer.test_email(User.find(1))
680 696 mail.delivery_method.stubs(:deliver!).raises(Exception.new("delivery error"))
681 697
682 698 Rails.logger.expects(:error).with("Email delivery error: delivery error")
683 699 ActionMailer::Base.raise_delivery_errors = false
684 700 assert_nothing_raised do
685 701 mail.deliver
686 702 end
687 703 end
688 704
689 705 private
690 706
691 707 def last_email
692 708 mail = ActionMailer::Base.deliveries.last
693 709 assert_not_nil mail
694 710 mail
695 711 end
696 712
697 713 def text_part
698 714 last_email.parts.detect {|part| part.content_type.include?('text/plain')}
699 715 end
700 716
701 717 def html_part
702 718 last_email.parts.detect {|part| part.content_type.include?('text/html')}
703 719 end
704 720 end
General Comments 0
You need to be logged in to leave comments. Login now