##// END OF EJS Templates
Backported r9687 from trunk....
Jean-Philippe Lang -
r9505:5a1be1d8df72
parent child
Show More
@@ -1,498 +1,499
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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 ActionController::UrlWriter
25 25 include Redmine::I18n
26 26
27 27 def self.default_url_options
28 28 h = Setting.host_name
29 29 h = h.to_s.gsub(%r{\/.*$}, '') unless Redmine::Utils.relative_url_root.blank?
30 30 { :host => h, :protocol => Setting.protocol }
31 31 end
32 32
33 33 # Builds a tmail object used to email recipients of the added issue.
34 34 #
35 35 # Example:
36 36 # issue_add(issue) => tmail object
37 37 # Mailer.deliver_issue_add(issue) => sends an email to issue recipients
38 38 def issue_add(issue)
39 39 redmine_headers 'Project' => issue.project.identifier,
40 40 'Issue-Id' => issue.id,
41 41 'Issue-Author' => issue.author.login
42 42 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
43 43 message_id issue
44 44 @author = issue.author
45 45 recipients issue.recipients
46 46 cc(issue.watcher_recipients - @recipients)
47 47 subject "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}"
48 48 body :issue => issue,
49 49 :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue)
50 50 render_multipart('issue_add', body)
51 51 end
52 52
53 53 # Builds a tmail object used to email recipients of the edited issue.
54 54 #
55 55 # Example:
56 56 # issue_edit(journal) => tmail object
57 57 # Mailer.deliver_issue_edit(journal) => sends an email to issue recipients
58 58 def issue_edit(journal)
59 59 issue = journal.journalized.reload
60 60 redmine_headers 'Project' => issue.project.identifier,
61 61 'Issue-Id' => issue.id,
62 62 'Issue-Author' => issue.author.login
63 63 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
64 64 message_id journal
65 65 references issue
66 66 @author = journal.user
67 67 recipients issue.recipients
68 68 # Watchers in cc
69 69 cc(issue.watcher_recipients - @recipients)
70 70 s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] "
71 71 s << "(#{issue.status.name}) " if journal.new_value_for('status_id')
72 72 s << issue.subject
73 73 subject s
74 74 body :issue => issue,
75 75 :journal => journal,
76 76 :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue, :anchor => "change-#{journal.id}")
77 77
78 78 render_multipart('issue_edit', body)
79 79 end
80 80
81 81 def reminder(user, issues, days)
82 82 set_language_if_valid user.language
83 83 recipients user.mail
84 84 subject l(:mail_subject_reminder, :count => issues.size, :days => days)
85 85 body :issues => issues,
86 86 :days => days,
87 87 :issues_url => url_for(:controller => 'issues', :action => 'index',
88 88 :set_filter => 1, :assigned_to_id => user.id,
89 89 :sort => 'due_date:asc')
90 90 render_multipart('reminder', body)
91 91 end
92 92
93 93 # Builds a tmail object used to email users belonging to the added document's project.
94 94 #
95 95 # Example:
96 96 # document_added(document) => tmail object
97 97 # Mailer.deliver_document_added(document) => sends an email to the document's project recipients
98 98 def document_added(document)
99 99 redmine_headers 'Project' => document.project.identifier
100 100 recipients document.recipients
101 101 @author = User.current
102 102 subject "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}"
103 103 body :document => document,
104 104 :document_url => url_for(:controller => 'documents', :action => 'show', :id => document)
105 105 render_multipart('document_added', body)
106 106 end
107 107
108 108 # Builds a tmail object used to email recipients of a project when an attachements are added.
109 109 #
110 110 # Example:
111 111 # attachments_added(attachments) => tmail object
112 112 # Mailer.deliver_attachments_added(attachments) => sends an email to the project's recipients
113 113 def attachments_added(attachments)
114 114 container = attachments.first.container
115 115 added_to = ''
116 116 added_to_url = ''
117 117 @author = attachments.first.author
118 118 case container.class.name
119 119 when 'Project'
120 120 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container)
121 121 added_to = "#{l(:label_project)}: #{container}"
122 122 recipients container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
123 123 when 'Version'
124 124 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container.project)
125 125 added_to = "#{l(:label_version)}: #{container.name}"
126 126 recipients container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
127 127 when 'Document'
128 128 added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id)
129 129 added_to = "#{l(:label_document)}: #{container.title}"
130 130 recipients container.recipients
131 131 end
132 132 redmine_headers 'Project' => container.project.identifier
133 133 subject "[#{container.project.name}] #{l(:label_attachment_new)}"
134 134 body :attachments => attachments,
135 135 :added_to => added_to,
136 136 :added_to_url => added_to_url
137 137 render_multipart('attachments_added', body)
138 138 end
139 139
140 140 # Builds a tmail object used to email recipients of a news' project when a news item is added.
141 141 #
142 142 # Example:
143 143 # news_added(news) => tmail object
144 144 # Mailer.deliver_news_added(news) => sends an email to the news' project recipients
145 145 def news_added(news)
146 146 redmine_headers 'Project' => news.project.identifier
147 147 @author = news.author
148 148 message_id news
149 149 recipients news.recipients
150 150 subject "[#{news.project.name}] #{l(:label_news)}: #{news.title}"
151 151 body :news => news,
152 152 :news_url => url_for(:controller => 'news', :action => 'show', :id => news)
153 153 render_multipart('news_added', body)
154 154 end
155 155
156 156 # Builds a tmail object used to email recipients of a news' project when a news comment is added.
157 157 #
158 158 # Example:
159 159 # news_comment_added(comment) => tmail object
160 160 # Mailer.news_comment_added(comment) => sends an email to the news' project recipients
161 161 def news_comment_added(comment)
162 162 news = comment.commented
163 163 redmine_headers 'Project' => news.project.identifier
164 164 @author = comment.author
165 165 message_id comment
166 166 recipients news.recipients
167 167 cc news.watcher_recipients
168 168 subject "Re: [#{news.project.name}] #{l(:label_news)}: #{news.title}"
169 169 body :news => news,
170 170 :comment => comment,
171 171 :news_url => url_for(:controller => 'news', :action => 'show', :id => news)
172 172 render_multipart('news_comment_added', body)
173 173 end
174 174
175 175 # Builds a tmail object used to email the recipients of the specified message that was posted.
176 176 #
177 177 # Example:
178 178 # message_posted(message) => tmail object
179 179 # Mailer.deliver_message_posted(message) => sends an email to the recipients
180 180 def message_posted(message)
181 181 redmine_headers 'Project' => message.project.identifier,
182 182 'Topic-Id' => (message.parent_id || message.id)
183 183 @author = message.author
184 184 message_id message
185 185 references message.parent unless message.parent.nil?
186 186 recipients(message.recipients)
187 187 cc((message.root.watcher_recipients + message.board.watcher_recipients).uniq - @recipients)
188 188 subject "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}"
189 189 body :message => message,
190 190 :message_url => url_for(message.event_url)
191 191 render_multipart('message_posted', body)
192 192 end
193 193
194 194 # Builds a tmail object used to email the recipients of a project of the specified wiki content was added.
195 195 #
196 196 # Example:
197 197 # wiki_content_added(wiki_content) => tmail object
198 198 # Mailer.deliver_wiki_content_added(wiki_content) => sends an email to the project's recipients
199 199 def wiki_content_added(wiki_content)
200 200 redmine_headers 'Project' => wiki_content.project.identifier,
201 201 'Wiki-Page-Id' => wiki_content.page.id
202 202 @author = wiki_content.author
203 203 message_id wiki_content
204 204 recipients wiki_content.recipients
205 205 cc(wiki_content.page.wiki.watcher_recipients - recipients)
206 206 subject "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}"
207 207 body :wiki_content => wiki_content,
208 208 :wiki_content_url => url_for(:controller => 'wiki', :action => 'show',
209 209 :project_id => wiki_content.project,
210 210 :id => wiki_content.page.title)
211 211 render_multipart('wiki_content_added', body)
212 212 end
213 213
214 214 # Builds a tmail object used to email the recipients of a project of the specified wiki content was updated.
215 215 #
216 216 # Example:
217 217 # wiki_content_updated(wiki_content) => tmail object
218 218 # Mailer.deliver_wiki_content_updated(wiki_content) => sends an email to the project's recipients
219 219 def wiki_content_updated(wiki_content)
220 220 redmine_headers 'Project' => wiki_content.project.identifier,
221 221 'Wiki-Page-Id' => wiki_content.page.id
222 222 @author = wiki_content.author
223 223 message_id wiki_content
224 224 recipients wiki_content.recipients
225 225 cc(wiki_content.page.wiki.watcher_recipients + wiki_content.page.watcher_recipients - recipients)
226 226 subject "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_updated, :id => wiki_content.page.pretty_title)}"
227 227 body :wiki_content => wiki_content,
228 228 :wiki_content_url => url_for(:controller => 'wiki', :action => 'show',
229 229 :project_id => wiki_content.project,
230 230 :id => wiki_content.page.title),
231 231 :wiki_diff_url => url_for(:controller => 'wiki', :action => 'diff',
232 232 :project_id => wiki_content.project, :id => wiki_content.page.title,
233 233 :version => wiki_content.version)
234 234 render_multipart('wiki_content_updated', body)
235 235 end
236 236
237 237 # Builds a tmail object used to email the specified user their account information.
238 238 #
239 239 # Example:
240 240 # account_information(user, password) => tmail object
241 241 # Mailer.deliver_account_information(user, password) => sends account information to the user
242 242 def account_information(user, password)
243 243 set_language_if_valid user.language
244 244 recipients user.mail
245 245 subject l(:mail_subject_register, Setting.app_title)
246 246 body :user => user,
247 247 :password => password,
248 248 :login_url => url_for(:controller => 'account', :action => 'login')
249 249 render_multipart('account_information', body)
250 250 end
251 251
252 252 # Builds a tmail object used to email all active administrators of an account activation request.
253 253 #
254 254 # Example:
255 255 # account_activation_request(user) => tmail object
256 256 # Mailer.deliver_account_activation_request(user)=> sends an email to all active administrators
257 257 def account_activation_request(user)
258 258 # Send the email to all active administrators
259 259 recipients User.active.find(:all, :conditions => {:admin => true}).collect { |u| u.mail }.compact
260 260 subject l(:mail_subject_account_activation_request, Setting.app_title)
261 261 body :user => user,
262 262 :url => url_for(:controller => 'users', :action => 'index',
263 263 :status => User::STATUS_REGISTERED,
264 264 :sort_key => 'created_on', :sort_order => 'desc')
265 265 render_multipart('account_activation_request', body)
266 266 end
267 267
268 268 # Builds a tmail object used to email the specified user that their account was activated by an administrator.
269 269 #
270 270 # Example:
271 271 # account_activated(user) => tmail object
272 272 # Mailer.deliver_account_activated(user) => sends an email to the registered user
273 273 def account_activated(user)
274 274 set_language_if_valid user.language
275 275 recipients user.mail
276 276 subject l(:mail_subject_register, Setting.app_title)
277 277 body :user => user,
278 278 :login_url => url_for(:controller => 'account', :action => 'login')
279 279 render_multipart('account_activated', body)
280 280 end
281 281
282 282 def lost_password(token)
283 283 set_language_if_valid(token.user.language)
284 284 recipients token.user.mail
285 285 subject l(:mail_subject_lost_password, Setting.app_title)
286 286 body :token => token,
287 287 :url => url_for(:controller => 'account', :action => 'lost_password', :token => token.value)
288 288 render_multipart('lost_password', body)
289 289 end
290 290
291 291 def register(token)
292 292 set_language_if_valid(token.user.language)
293 293 recipients token.user.mail
294 294 subject l(:mail_subject_register, Setting.app_title)
295 295 body :token => token,
296 296 :url => url_for(:controller => 'account', :action => 'activate', :token => token.value)
297 297 render_multipart('register', body)
298 298 end
299 299
300 300 def test_email(user)
301 301 set_language_if_valid(user.language)
302 302 recipients user.mail
303 303 subject 'Redmine test'
304 304 body :url => url_for(:controller => 'welcome')
305 305 render_multipart('test_email', body)
306 306 end
307 307
308 308 # Overrides default deliver! method to prevent from sending an email
309 309 # with no recipient, cc or bcc
310 310 def deliver!(mail = @mail)
311 311 set_language_if_valid @initial_language
312 312 return false if (recipients.nil? || recipients.empty?) &&
313 313 (cc.nil? || cc.empty?) &&
314 314 (bcc.nil? || bcc.empty?)
315 315
316 316 # Set Message-Id and References
317 317 if @message_id_object
318 318 mail.message_id = self.class.message_id_for(@message_id_object)
319 319 end
320 320 if @references_objects
321 321 mail.references = @references_objects.collect {|o| self.class.message_id_for(o)}
322 322 end
323 323
324 324 # Log errors when raise_delivery_errors is set to false, Rails does not
325 325 raise_errors = self.class.raise_delivery_errors
326 326 self.class.raise_delivery_errors = true
327 327 begin
328 328 return super(mail)
329 329 rescue Exception => e
330 330 if raise_errors
331 331 raise e
332 332 elsif mylogger
333 333 mylogger.error "The following error occured while sending email notification: \"#{e.message}\". Check your configuration in config/configuration.yml."
334 334 end
335 335 ensure
336 336 self.class.raise_delivery_errors = raise_errors
337 337 end
338 338 end
339 339
340 340 # Sends reminders to issue assignees
341 341 # Available options:
342 342 # * :days => how many days in the future to remind about (defaults to 7)
343 343 # * :tracker => id of tracker for filtering issues (defaults to all trackers)
344 344 # * :project => id or identifier of project to process (defaults to all projects)
345 345 # * :users => array of user ids who should be reminded
346 346 def self.reminders(options={})
347 347 days = options[:days] || 7
348 348 project = options[:project] ? Project.find(options[:project]) : nil
349 349 tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil
350 350 user_ids = options[:users]
351 351
352 352 scope = Issue.open.scoped(:conditions => ["#{Issue.table_name}.assigned_to_id IS NOT NULL" +
353 353 " AND #{Project.table_name}.status = #{Project::STATUS_ACTIVE}" +
354 354 " AND #{Issue.table_name}.due_date <= ?", days.day.from_now.to_date]
355 355 )
356 356 scope = scope.scoped(:conditions => {:assigned_to_id => user_ids}) if user_ids.present?
357 357 scope = scope.scoped(:conditions => {:project_id => project.id}) if project
358 358 scope = scope.scoped(:conditions => {:tracker_id => tracker.id}) if tracker
359 359
360 360 issues_by_assignee = scope.all(:include => [:status, :assigned_to, :project, :tracker]).group_by(&:assigned_to)
361 361 issues_by_assignee.each do |assignee, issues|
362 362 deliver_reminder(assignee, issues, days) if assignee.is_a?(User) && assignee.active?
363 363 end
364 364 end
365 365
366 366 # Activates/desactivates email deliveries during +block+
367 367 def self.with_deliveries(enabled = true, &block)
368 368 was_enabled = ActionMailer::Base.perform_deliveries
369 369 ActionMailer::Base.perform_deliveries = !!enabled
370 370 yield
371 371 ensure
372 372 ActionMailer::Base.perform_deliveries = was_enabled
373 373 end
374 374
375 375 # Sends emails synchronously in the given block
376 376 def self.with_synched_deliveries(&block)
377 377 saved_method = ActionMailer::Base.delivery_method
378 378 if m = saved_method.to_s.match(%r{^async_(.+)$})
379 379 ActionMailer::Base.delivery_method = m[1].to_sym
380 380 end
381 381 yield
382 382 ensure
383 383 ActionMailer::Base.delivery_method = saved_method
384 384 end
385 385
386 386 private
387 387 def initialize_defaults(method_name)
388 388 super
389 389 @initial_language = current_language
390 390 set_language_if_valid Setting.default_language
391 391 from Setting.mail_from
392 392
393 393 # Common headers
394 394 headers 'X-Mailer' => 'Redmine',
395 395 'X-Redmine-Host' => Setting.host_name,
396 396 'X-Redmine-Site' => Setting.app_title,
397 397 'X-Auto-Response-Suppress' => 'OOF',
398 'Auto-Submitted' => 'auto-generated'
398 'Auto-Submitted' => 'auto-generated',
399 'List-Id' => "<#{Setting.mail_from.to_s.gsub('@', '.')}>"
399 400 end
400 401
401 402 # Appends a Redmine header field (name is prepended with 'X-Redmine-')
402 403 def redmine_headers(h)
403 404 h.each { |k,v| headers["X-Redmine-#{k}"] = v.to_s }
404 405 end
405 406
406 407 # Overrides the create_mail method
407 408 def create_mail
408 409 # Removes the author from the recipients and cc
409 410 # if he doesn't want to receive notifications about what he does
410 411 if @author && @author.logged? && @author.pref[:no_self_notified]
411 412 if recipients
412 413 recipients((recipients.is_a?(Array) ? recipients : [recipients]) - [@author.mail])
413 414 end
414 415 if cc
415 416 cc((cc.is_a?(Array) ? cc : [cc]) - [@author.mail])
416 417 end
417 418 end
418 419
419 420 if @author && @author.logged?
420 421 redmine_headers 'Sender' => @author.login
421 422 end
422 423
423 424 notified_users = [recipients, cc].flatten.compact.uniq
424 425 # Rails would log recipients only, not cc and bcc
425 426 mylogger.info "Sending email notification to: #{notified_users.join(', ')}" if mylogger
426 427
427 428 # Blind carbon copy recipients
428 429 if Setting.bcc_recipients?
429 430 bcc(notified_users)
430 431 recipients []
431 432 cc []
432 433 end
433 434 super
434 435 end
435 436
436 437 # Rails 2.3 has problems rendering implicit multipart messages with
437 438 # layouts so this method will wrap an multipart messages with
438 439 # explicit parts.
439 440 #
440 441 # https://rails.lighthouseapp.com/projects/8994/tickets/2338-actionmailer-mailer-views-and-content-type
441 442 # https://rails.lighthouseapp.com/projects/8994/tickets/1799-actionmailer-doesnt-set-template_format-when-rendering-layouts
442 443
443 444 def render_multipart(method_name, body)
444 445 if Setting.plain_text_mail?
445 446 content_type "text/plain"
446 447 body render(:file => "#{method_name}.text.erb",
447 448 :body => body,
448 449 :layout => 'mailer.text.erb')
449 450 else
450 451 content_type "multipart/alternative"
451 452 part :content_type => "text/plain",
452 453 :body => render(:file => "#{method_name}.text.erb",
453 454 :body => body, :layout => 'mailer.text.erb')
454 455 part :content_type => "text/html",
455 456 :body => render_message("#{method_name}.html.erb", body)
456 457 end
457 458 end
458 459
459 460 # Makes partial rendering work with Rails 1.2 (retro-compatibility)
460 461 def self.controller_path
461 462 ''
462 463 end unless respond_to?('controller_path')
463 464
464 465 # Returns a predictable Message-Id for the given object
465 466 def self.message_id_for(object)
466 467 # id + timestamp should reduce the odds of a collision
467 468 # as far as we don't send multiple emails for the same object
468 469 timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on)
469 470 hash = "redmine.#{object.class.name.demodulize.underscore}-#{object.id}.#{timestamp.strftime("%Y%m%d%H%M%S")}"
470 471 host = Setting.mail_from.to_s.gsub(%r{^.*@}, '')
471 472 host = "#{::Socket.gethostname}.redmine" if host.empty?
472 473 "<#{hash}@#{host}>"
473 474 end
474 475
475 476 private
476 477
477 478 def message_id(object)
478 479 @message_id_object = object
479 480 end
480 481
481 482 def references(object)
482 483 @references_objects ||= []
483 484 @references_objects << object
484 485 end
485 486
486 487 def mylogger
487 488 Rails.logger
488 489 end
489 490 end
490 491
491 492 # Patch TMail so that message_id is not overwritten
492 493 module TMail
493 494 class Mail
494 495 def add_message_id( fqdn = nil )
495 496 self.message_id ||= ::TMail::new_message_id(fqdn)
496 497 end
497 498 end
498 499 end
@@ -1,550 +1,551
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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 ActionController::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, :trackers,
26 26 :issue_statuses, :enumerations, :messages, :boards, :repositories,
27 27 :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions,
28 28 :versions,
29 29 :comments
30 30
31 31 def setup
32 32 ActionMailer::Base.deliveries.clear
33 33 Setting.host_name = 'mydomain.foo'
34 34 Setting.protocol = 'http'
35 35 Setting.plain_text_mail = '0'
36 36 end
37 37
38 38 def test_generated_links_in_emails
39 39 Setting.default_language = 'en'
40 40 Setting.host_name = 'mydomain.foo'
41 41 Setting.protocol = 'https'
42 42
43 43 journal = Journal.find(3)
44 44 assert Mailer.deliver_issue_edit(journal)
45 45
46 46 mail = last_email
47 47 assert_not_nil mail
48 48
49 49 assert_select_email do
50 50 # link to the main ticket
51 51 assert_select 'a[href=?]',
52 52 'https://mydomain.foo/issues/2#change-3',
53 53 :text => 'Feature request #2: Add ingredients categories'
54 54 # link to a referenced ticket
55 55 assert_select 'a[href=?][title=?]',
56 56 'https://mydomain.foo/issues/1',
57 57 'Can\'t print recipes (New)',
58 58 :text => '#1'
59 59 # link to a changeset
60 60 assert_select 'a[href=?][title=?]',
61 61 'https://mydomain.foo/projects/ecookbook/repository/revisions/2',
62 62 'This commit fixes #1, #2 and references #1 &amp; #3',
63 63 :text => 'r2'
64 64 # link to a description diff
65 65 assert_select 'a[href=?][title=?]',
66 66 'https://mydomain.foo/journals/diff/3?detail_id=4',
67 67 'View differences',
68 68 :text => 'diff'
69 69 # link to an attachment
70 70 assert_select 'a[href=?]',
71 71 'https://mydomain.foo/attachments/download/4/source.rb',
72 72 :text => 'source.rb'
73 73 end
74 74 end
75 75
76 76 def test_generated_links_with_prefix
77 77 Setting.default_language = 'en'
78 78 relative_url_root = Redmine::Utils.relative_url_root
79 79 Setting.host_name = 'mydomain.foo/rdm'
80 80 Setting.protocol = 'http'
81 81 Redmine::Utils.relative_url_root = '/rdm'
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\'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 ensure
115 115 # restore it
116 116 Redmine::Utils.relative_url_root = relative_url_root
117 117 end
118 118
119 119 def test_generated_links_with_prefix_and_no_relative_url_root
120 120 Setting.default_language = 'en'
121 121 relative_url_root = Redmine::Utils.relative_url_root
122 122 Setting.host_name = 'mydomain.foo/rdm'
123 123 Setting.protocol = 'http'
124 124 Redmine::Utils.relative_url_root = nil
125 125
126 126 journal = Journal.find(3)
127 127 assert Mailer.deliver_issue_edit(journal)
128 128
129 129 mail = last_email
130 130 assert_not_nil mail
131 131
132 132 assert_select_email do
133 133 # link to the main ticket
134 134 assert_select 'a[href=?]',
135 135 'http://mydomain.foo/rdm/issues/2#change-3',
136 136 :text => 'Feature request #2: Add ingredients categories'
137 137 # link to a referenced ticket
138 138 assert_select 'a[href=?][title=?]',
139 139 'http://mydomain.foo/rdm/issues/1',
140 140 'Can\'t print recipes (New)',
141 141 :text => '#1'
142 142 # link to a changeset
143 143 assert_select 'a[href=?][title=?]',
144 144 'http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2',
145 145 'This commit fixes #1, #2 and references #1 &amp; #3',
146 146 :text => 'r2'
147 147 # link to a description diff
148 148 assert_select 'a[href=?][title=?]',
149 149 'http://mydomain.foo/rdm/journals/diff/3?detail_id=4',
150 150 'View differences',
151 151 :text => 'diff'
152 152 # link to an attachment
153 153 assert_select 'a[href=?]',
154 154 'http://mydomain.foo/rdm/attachments/download/4/source.rb',
155 155 :text => 'source.rb'
156 156 end
157 157 ensure
158 158 # restore it
159 159 Redmine::Utils.relative_url_root = relative_url_root
160 160 end
161 161
162 162 def test_email_headers
163 163 issue = Issue.find(1)
164 164 Mailer.deliver_issue_add(issue)
165 165 mail = last_email
166 166 assert_not_nil mail
167 167 assert_equal 'OOF', mail.header_string('X-Auto-Response-Suppress')
168 168 assert_equal 'auto-generated', mail.header_string('Auto-Submitted')
169 assert_equal '<redmine.example.net>', mail.header_string('List-Id')
169 170 end
170 171
171 172 def test_email_headers_should_include_sender
172 173 issue = Issue.find(1)
173 174 Mailer.deliver_issue_add(issue)
174 175 mail = last_email
175 176 assert_equal issue.author.login, mail.header_string('X-Redmine-Sender')
176 177 end
177 178
178 179 def test_plain_text_mail
179 180 Setting.plain_text_mail = 1
180 181 journal = Journal.find(2)
181 182 Mailer.deliver_issue_edit(journal)
182 183 mail = last_email
183 184 assert_equal "text/plain", mail.content_type
184 185 assert_equal 0, mail.parts.size
185 186 assert !mail.encoded.include?('href')
186 187 end
187 188
188 189 def test_html_mail
189 190 Setting.plain_text_mail = 0
190 191 journal = Journal.find(2)
191 192 Mailer.deliver_issue_edit(journal)
192 193 mail = last_email
193 194 assert_equal 2, mail.parts.size
194 195 assert mail.encoded.include?('href')
195 196 end
196 197
197 198 def test_from_header
198 199 with_settings :mail_from => 'redmine@example.net' do
199 200 Mailer.deliver_test_email(User.find(1))
200 201 end
201 202 mail = last_email
202 203 assert_equal 'redmine@example.net', mail.from_addrs.first.address
203 204 end
204 205
205 206 def test_from_header_with_phrase
206 207 with_settings :mail_from => 'Redmine app <redmine@example.net>' do
207 208 Mailer.deliver_test_email(User.find(1))
208 209 end
209 210 mail = last_email
210 211 assert_equal 'redmine@example.net', mail.from_addrs.first.address
211 212 assert_equal 'Redmine app', mail.from_addrs.first.name
212 213 end
213 214
214 215 def test_should_not_send_email_without_recipient
215 216 news = News.find(:first)
216 217 user = news.author
217 218 # Remove members except news author
218 219 news.project.memberships.each {|m| m.destroy unless m.user == user}
219 220
220 221 user.pref[:no_self_notified] = false
221 222 user.pref.save
222 223 User.current = user
223 224 Mailer.deliver_news_added(news.reload)
224 225 assert_equal 1, last_email.bcc.size
225 226
226 227 # nobody to notify
227 228 user.pref[:no_self_notified] = true
228 229 user.pref.save
229 230 User.current = user
230 231 ActionMailer::Base.deliveries.clear
231 232 Mailer.deliver_news_added(news.reload)
232 233 assert ActionMailer::Base.deliveries.empty?
233 234 end
234 235
235 236 def test_issue_add_message_id
236 237 issue = Issue.find(1)
237 238 Mailer.deliver_issue_add(issue)
238 239 mail = last_email
239 240 assert_equal Mailer.message_id_for(issue), mail.message_id
240 241 assert_nil mail.references
241 242 end
242 243
243 244 def test_issue_edit_message_id
244 245 journal = Journal.find(1)
245 246 Mailer.deliver_issue_edit(journal)
246 247 mail = last_email
247 248 assert_equal Mailer.message_id_for(journal), mail.message_id
248 249 assert_equal Mailer.message_id_for(journal.issue), mail.references.first.to_s
249 250 assert_select_email do
250 251 # link to the update
251 252 assert_select "a[href=?]",
252 253 "http://mydomain.foo/issues/#{journal.journalized_id}#change-#{journal.id}"
253 254 end
254 255 end
255 256
256 257 def test_message_posted_message_id
257 258 message = Message.find(1)
258 259 Mailer.deliver_message_posted(message)
259 260 mail = last_email
260 261 assert_equal Mailer.message_id_for(message), mail.message_id
261 262 assert_nil mail.references
262 263 assert_select_email do
263 264 # link to the message
264 265 assert_select "a[href=?]",
265 266 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.id}",
266 267 :text => message.subject
267 268 end
268 269 end
269 270
270 271 def test_reply_posted_message_id
271 272 message = Message.find(3)
272 273 Mailer.deliver_message_posted(message)
273 274 mail = last_email
274 275 assert_equal Mailer.message_id_for(message), mail.message_id
275 276 assert_equal Mailer.message_id_for(message.parent), mail.references.first.to_s
276 277 assert_select_email do
277 278 # link to the reply
278 279 assert_select "a[href=?]",
279 280 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.root.id}?r=#{message.id}#message-#{message.id}",
280 281 :text => message.subject
281 282 end
282 283 end
283 284
284 285 context("#issue_add") do
285 286 setup do
286 287 ActionMailer::Base.deliveries.clear
287 288 Setting.bcc_recipients = '1'
288 289 @issue = Issue.find(1)
289 290 end
290 291
291 292 should "notify project members" do
292 293 assert Mailer.deliver_issue_add(@issue)
293 294 assert last_email.bcc.include?('dlopper@somenet.foo')
294 295 end
295 296
296 297 should "not notify project members that are not allow to view the issue" do
297 298 Role.find(2).remove_permission!(:view_issues)
298 299 assert Mailer.deliver_issue_add(@issue)
299 300 assert !last_email.bcc.include?('dlopper@somenet.foo')
300 301 end
301 302
302 303 should "notify issue watchers" do
303 304 user = User.find(9)
304 305 # minimal email notification options
305 306 user.pref[:no_self_notified] = '1'
306 307 user.pref.save
307 308 user.mail_notification = false
308 309 user.save
309 310
310 311 Watcher.create!(:watchable => @issue, :user => user)
311 312 assert Mailer.deliver_issue_add(@issue)
312 313 assert last_email.bcc.include?(user.mail)
313 314 end
314 315
315 316 should "not notify watchers not allowed to view the issue" do
316 317 user = User.find(9)
317 318 Watcher.create!(:watchable => @issue, :user => user)
318 319 Role.non_member.remove_permission!(:view_issues)
319 320 assert Mailer.deliver_issue_add(@issue)
320 321 assert !last_email.bcc.include?(user.mail)
321 322 end
322 323 end
323 324
324 325 # test mailer methods for each language
325 326 def test_issue_add
326 327 issue = Issue.find(1)
327 328 valid_languages.each do |lang|
328 329 Setting.default_language = lang.to_s
329 330 assert Mailer.deliver_issue_add(issue)
330 331 end
331 332 end
332 333
333 334 def test_issue_edit
334 335 journal = Journal.find(1)
335 336 valid_languages.each do |lang|
336 337 Setting.default_language = lang.to_s
337 338 assert Mailer.deliver_issue_edit(journal)
338 339 end
339 340 end
340 341
341 342 def test_document_added
342 343 document = Document.find(1)
343 344 valid_languages.each do |lang|
344 345 Setting.default_language = lang.to_s
345 346 assert Mailer.deliver_document_added(document)
346 347 end
347 348 end
348 349
349 350 def test_attachments_added
350 351 attachements = [ Attachment.find_by_container_type('Document') ]
351 352 valid_languages.each do |lang|
352 353 Setting.default_language = lang.to_s
353 354 assert Mailer.deliver_attachments_added(attachements)
354 355 end
355 356 end
356 357
357 358 def test_version_file_added
358 359 attachements = [ Attachment.find_by_container_type('Version') ]
359 360 assert Mailer.deliver_attachments_added(attachements)
360 361 assert_not_nil last_email.bcc
361 362 assert last_email.bcc.any?
362 363 assert_select_email do
363 364 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
364 365 end
365 366 end
366 367
367 368 def test_project_file_added
368 369 attachements = [ Attachment.find_by_container_type('Project') ]
369 370 assert Mailer.deliver_attachments_added(attachements)
370 371 assert_not_nil last_email.bcc
371 372 assert last_email.bcc.any?
372 373 assert_select_email do
373 374 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
374 375 end
375 376 end
376 377
377 378 def test_news_added
378 379 news = News.find(:first)
379 380 valid_languages.each do |lang|
380 381 Setting.default_language = lang.to_s
381 382 assert Mailer.deliver_news_added(news)
382 383 end
383 384 end
384 385
385 386 def test_news_comment_added
386 387 comment = Comment.find(2)
387 388 valid_languages.each do |lang|
388 389 Setting.default_language = lang.to_s
389 390 assert Mailer.deliver_news_comment_added(comment)
390 391 end
391 392 end
392 393
393 394 def test_message_posted
394 395 message = Message.find(:first)
395 396 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
396 397 recipients = recipients.compact.uniq
397 398 valid_languages.each do |lang|
398 399 Setting.default_language = lang.to_s
399 400 assert Mailer.deliver_message_posted(message)
400 401 end
401 402 end
402 403
403 404 def test_wiki_content_added
404 405 content = WikiContent.find(1)
405 406 valid_languages.each do |lang|
406 407 Setting.default_language = lang.to_s
407 408 assert_difference 'ActionMailer::Base.deliveries.size' do
408 409 assert Mailer.deliver_wiki_content_added(content)
409 410 end
410 411 end
411 412 end
412 413
413 414 def test_wiki_content_updated
414 415 content = WikiContent.find(1)
415 416 valid_languages.each do |lang|
416 417 Setting.default_language = lang.to_s
417 418 assert_difference 'ActionMailer::Base.deliveries.size' do
418 419 assert Mailer.deliver_wiki_content_updated(content)
419 420 end
420 421 end
421 422 end
422 423
423 424 def test_account_information
424 425 user = User.find(2)
425 426 valid_languages.each do |lang|
426 427 user.update_attribute :language, lang.to_s
427 428 user.reload
428 429 assert Mailer.deliver_account_information(user, 'pAsswORd')
429 430 end
430 431 end
431 432
432 433 def test_lost_password
433 434 token = Token.find(2)
434 435 valid_languages.each do |lang|
435 436 token.user.update_attribute :language, lang.to_s
436 437 token.reload
437 438 assert Mailer.deliver_lost_password(token)
438 439 end
439 440 end
440 441
441 442 def test_register
442 443 token = Token.find(1)
443 444 Setting.host_name = 'redmine.foo'
444 445 Setting.protocol = 'https'
445 446
446 447 valid_languages.each do |lang|
447 448 token.user.update_attribute :language, lang.to_s
448 449 token.reload
449 450 ActionMailer::Base.deliveries.clear
450 451 assert Mailer.deliver_register(token)
451 452 mail = last_email
452 453 assert_select_email do
453 454 assert_select "a[href=?]",
454 455 "https://redmine.foo/account/activate?token=#{token.value}",
455 456 :text => "https://redmine.foo/account/activate?token=#{token.value}"
456 457 end
457 458 end
458 459 end
459 460
460 461 def test_test
461 462 user = User.find(1)
462 463 valid_languages.each do |lang|
463 464 user.update_attribute :language, lang.to_s
464 465 assert Mailer.deliver_test_email(user)
465 466 end
466 467 end
467 468
468 469 def test_reminders
469 470 Mailer.reminders(:days => 42)
470 471 assert_equal 1, ActionMailer::Base.deliveries.size
471 472 mail = last_email
472 473 assert mail.bcc.include?('dlopper@somenet.foo')
473 474 assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
474 475 assert_equal '1 issue(s) due in the next 42 days', mail.subject
475 476 end
476 477
477 478 def test_reminders_should_not_include_closed_issues
478 479 with_settings :default_language => 'en' do
479 480 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 5,
480 481 :subject => 'Closed issue', :assigned_to_id => 3,
481 482 :due_date => 5.days.from_now,
482 483 :author_id => 2)
483 484 ActionMailer::Base.deliveries.clear
484 485
485 486 Mailer.reminders(:days => 42)
486 487 assert_equal 1, ActionMailer::Base.deliveries.size
487 488 mail = last_email
488 489 assert mail.bcc.include?('dlopper@somenet.foo')
489 490 assert_mail_body_no_match 'Closed issue', mail
490 491 end
491 492 end
492 493
493 494 def test_reminders_for_users
494 495 Mailer.reminders(:days => 42, :users => ['5'])
495 496 assert_equal 0, ActionMailer::Base.deliveries.size # No mail for dlopper
496 497 Mailer.reminders(:days => 42, :users => ['3'])
497 498 assert_equal 1, ActionMailer::Base.deliveries.size # No mail for dlopper
498 499 mail = last_email
499 500 assert mail.bcc.include?('dlopper@somenet.foo')
500 501 assert_mail_body_match 'Bug #3: Error 281 when updating a recipe', mail
501 502 end
502 503
503 504 def last_email
504 505 mail = ActionMailer::Base.deliveries.last
505 506 assert_not_nil mail
506 507 mail
507 508 end
508 509
509 510 def test_mailer_should_not_change_locale
510 511 Setting.default_language = 'en'
511 512 # Set current language to italian
512 513 set_language_if_valid 'it'
513 514 # Send an email to a french user
514 515 user = User.find(1)
515 516 user.language = 'fr'
516 517 Mailer.deliver_account_activated(user)
517 518 mail = last_email
518 519 assert_mail_body_match 'Votre compte', mail
519 520
520 521 assert_equal :it, current_language
521 522 end
522 523
523 524 def test_with_deliveries_off
524 525 Mailer.with_deliveries false do
525 526 Mailer.deliver_test_email(User.find(1))
526 527 end
527 528 assert ActionMailer::Base.deliveries.empty?
528 529 # should restore perform_deliveries
529 530 assert ActionMailer::Base.perform_deliveries
530 531 end
531 532
532 533 def test_tmail_to_header_field_should_not_include_blank_lines
533 534 mail = TMail::Mail.new
534 535 mail.to = ["a.user@example.com", "v.user2@example.com", "e.smith@example.com", "info@example.com", "v.pupkin@example.com",
535 536 "b.user@example.com", "w.user2@example.com", "f.smith@example.com", "info2@example.com", "w.pupkin@example.com"]
536 537
537 538 assert !mail.encoded.strip.split("\r\n").detect(&:blank?), "#{mail.encoded} malformed"
538 539 end
539 540
540 541 def test_layout_should_include_the_emails_header
541 542 with_settings :emails_header => "*Header content*" do
542 543 assert Mailer.deliver_test_email(User.find(1))
543 544 assert_select_email do
544 545 assert_select ".header" do
545 546 assert_select "strong", :text => "Header content"
546 547 end
547 548 end
548 549 end
549 550 end
550 551 end
General Comments 0
You need to be logged in to leave comments. Login now