##// END OF EJS Templates
Fixed: no email notification on new project/version file added (#4966)....
Jean-Philippe Lang -
r3418:cdb86d5ef7d2
parent child
Show More
@@ -1,428 +1,428
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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 recipients issue.recipients
45 45 cc(issue.watcher_recipients - @recipients)
46 46 subject "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}"
47 47 body :issue => issue,
48 48 :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue)
49 49 render_multipart('issue_add', body)
50 50 end
51 51
52 52 # Builds a tmail object used to email recipients of the edited issue.
53 53 #
54 54 # Example:
55 55 # issue_edit(journal) => tmail object
56 56 # Mailer.deliver_issue_edit(journal) => sends an email to issue recipients
57 57 def issue_edit(journal)
58 58 issue = journal.journalized.reload
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 recipients issue.recipients
67 67 # Watchers in cc
68 68 cc(issue.watcher_recipients - @recipients)
69 69 s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] "
70 70 s << "(#{issue.status.name}) " if journal.new_value_for('status_id')
71 71 s << issue.subject
72 72 subject s
73 73 body :issue => issue,
74 74 :journal => journal,
75 75 :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue)
76 76
77 77 render_multipart('issue_edit', body)
78 78 end
79 79
80 80 def reminder(user, issues, days)
81 81 set_language_if_valid user.language
82 82 recipients user.mail
83 83 subject l(:mail_subject_reminder, issues.size)
84 84 body :issues => issues,
85 85 :days => days,
86 86 :issues_url => url_for(:controller => 'issues', :action => 'index', :set_filter => 1, :assigned_to_id => user.id, :sort_key => 'due_date', :sort_order => 'asc')
87 87 render_multipart('reminder', body)
88 88 end
89 89
90 90 # Builds a tmail object used to email users belonging to the added document's project.
91 91 #
92 92 # Example:
93 93 # document_added(document) => tmail object
94 94 # Mailer.deliver_document_added(document) => sends an email to the document's project recipients
95 95 def document_added(document)
96 96 redmine_headers 'Project' => document.project.identifier
97 97 recipients document.recipients
98 98 subject "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}"
99 99 body :document => document,
100 100 :document_url => url_for(:controller => 'documents', :action => 'show', :id => document)
101 101 render_multipart('document_added', body)
102 102 end
103 103
104 104 # Builds a tmail object used to email recipients of a project when an attachements are added.
105 105 #
106 106 # Example:
107 107 # attachments_added(attachments) => tmail object
108 108 # Mailer.deliver_attachments_added(attachments) => sends an email to the project's recipients
109 109 def attachments_added(attachments)
110 110 container = attachments.first.container
111 111 added_to = ''
112 112 added_to_url = ''
113 113 case container.class.name
114 114 when 'Project'
115 115 added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container)
116 116 added_to = "#{l(:label_project)}: #{container}"
117 recipients container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}
117 recipients container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
118 118 when 'Version'
119 119 added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container.project_id)
120 120 added_to = "#{l(:label_version)}: #{container.name}"
121 recipients container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}
121 recipients container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
122 122 when 'Document'
123 123 added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id)
124 124 added_to = "#{l(:label_document)}: #{container.title}"
125 125 recipients container.recipients
126 126 end
127 127 redmine_headers 'Project' => container.project.identifier
128 128 subject "[#{container.project.name}] #{l(:label_attachment_new)}"
129 129 body :attachments => attachments,
130 130 :added_to => added_to,
131 131 :added_to_url => added_to_url
132 132 render_multipart('attachments_added', body)
133 133 end
134 134
135 135 # Builds a tmail object used to email recipients of a news' project when a news item is added.
136 136 #
137 137 # Example:
138 138 # news_added(news) => tmail object
139 139 # Mailer.deliver_news_added(news) => sends an email to the news' project recipients
140 140 def news_added(news)
141 141 redmine_headers 'Project' => news.project.identifier
142 142 message_id news
143 143 recipients news.recipients
144 144 subject "[#{news.project.name}] #{l(:label_news)}: #{news.title}"
145 145 body :news => news,
146 146 :news_url => url_for(:controller => 'news', :action => 'show', :id => news)
147 147 render_multipart('news_added', body)
148 148 end
149 149
150 150 # Builds a tmail object used to email the recipients of the specified message that was posted.
151 151 #
152 152 # Example:
153 153 # message_posted(message) => tmail object
154 154 # Mailer.deliver_message_posted(message) => sends an email to the recipients
155 155 def message_posted(message)
156 156 redmine_headers 'Project' => message.project.identifier,
157 157 'Topic-Id' => (message.parent_id || message.id)
158 158 message_id message
159 159 references message.parent unless message.parent.nil?
160 160 recipients(message.recipients)
161 161 cc((message.root.watcher_recipients + message.board.watcher_recipients).uniq - @recipients)
162 162 subject "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}"
163 163 body :message => message,
164 164 :message_url => url_for(message.event_url)
165 165 render_multipart('message_posted', body)
166 166 end
167 167
168 168 # Builds a tmail object used to email the recipients of a project of the specified wiki content was added.
169 169 #
170 170 # Example:
171 171 # wiki_content_added(wiki_content) => tmail object
172 172 # Mailer.deliver_wiki_content_added(wiki_content) => sends an email to the project's recipients
173 173 def wiki_content_added(wiki_content)
174 174 redmine_headers 'Project' => wiki_content.project.identifier,
175 175 'Wiki-Page-Id' => wiki_content.page.id
176 176 message_id wiki_content
177 177 recipients wiki_content.recipients
178 178 cc(wiki_content.page.wiki.watcher_recipients - recipients)
179 179 subject "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :page => wiki_content.page.pretty_title)}"
180 180 body :wiki_content => wiki_content,
181 181 :wiki_content_url => url_for(:controller => 'wiki', :action => 'index', :id => wiki_content.project, :page => wiki_content.page.title)
182 182 render_multipart('wiki_content_added', body)
183 183 end
184 184
185 185 # Builds a tmail object used to email the recipients of a project of the specified wiki content was updated.
186 186 #
187 187 # Example:
188 188 # wiki_content_updated(wiki_content) => tmail object
189 189 # Mailer.deliver_wiki_content_updated(wiki_content) => sends an email to the project's recipients
190 190 def wiki_content_updated(wiki_content)
191 191 redmine_headers 'Project' => wiki_content.project.identifier,
192 192 'Wiki-Page-Id' => wiki_content.page.id
193 193 message_id wiki_content
194 194 recipients wiki_content.recipients
195 195 cc(wiki_content.page.wiki.watcher_recipients + wiki_content.page.watcher_recipients - recipients)
196 196 subject "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_updated, :page => wiki_content.page.pretty_title)}"
197 197 body :wiki_content => wiki_content,
198 198 :wiki_content_url => url_for(:controller => 'wiki', :action => 'index', :id => wiki_content.project, :page => wiki_content.page.title),
199 199 :wiki_diff_url => url_for(:controller => 'wiki', :action => 'diff', :id => wiki_content.project, :page => wiki_content.page.title, :version => wiki_content.version)
200 200 render_multipart('wiki_content_updated', body)
201 201 end
202 202
203 203 # Builds a tmail object used to email the specified user their account information.
204 204 #
205 205 # Example:
206 206 # account_information(user, password) => tmail object
207 207 # Mailer.deliver_account_information(user, password) => sends account information to the user
208 208 def account_information(user, password)
209 209 set_language_if_valid user.language
210 210 recipients user.mail
211 211 subject l(:mail_subject_register, Setting.app_title)
212 212 body :user => user,
213 213 :password => password,
214 214 :login_url => url_for(:controller => 'account', :action => 'login')
215 215 render_multipart('account_information', body)
216 216 end
217 217
218 218 # Builds a tmail object used to email all active administrators of an account activation request.
219 219 #
220 220 # Example:
221 221 # account_activation_request(user) => tmail object
222 222 # Mailer.deliver_account_activation_request(user)=> sends an email to all active administrators
223 223 def account_activation_request(user)
224 224 # Send the email to all active administrators
225 225 recipients User.active.find(:all, :conditions => {:admin => true}).collect { |u| u.mail }.compact
226 226 subject l(:mail_subject_account_activation_request, Setting.app_title)
227 227 body :user => user,
228 228 :url => url_for(:controller => 'users', :action => 'index', :status => User::STATUS_REGISTERED, :sort_key => 'created_on', :sort_order => 'desc')
229 229 render_multipart('account_activation_request', body)
230 230 end
231 231
232 232 # Builds a tmail object used to email the specified user that their account was activated by an administrator.
233 233 #
234 234 # Example:
235 235 # account_activated(user) => tmail object
236 236 # Mailer.deliver_account_activated(user) => sends an email to the registered user
237 237 def account_activated(user)
238 238 set_language_if_valid user.language
239 239 recipients user.mail
240 240 subject l(:mail_subject_register, Setting.app_title)
241 241 body :user => user,
242 242 :login_url => url_for(:controller => 'account', :action => 'login')
243 243 render_multipart('account_activated', body)
244 244 end
245 245
246 246 def lost_password(token)
247 247 set_language_if_valid(token.user.language)
248 248 recipients token.user.mail
249 249 subject l(:mail_subject_lost_password, Setting.app_title)
250 250 body :token => token,
251 251 :url => url_for(:controller => 'account', :action => 'lost_password', :token => token.value)
252 252 render_multipart('lost_password', body)
253 253 end
254 254
255 255 def register(token)
256 256 set_language_if_valid(token.user.language)
257 257 recipients token.user.mail
258 258 subject l(:mail_subject_register, Setting.app_title)
259 259 body :token => token,
260 260 :url => url_for(:controller => 'account', :action => 'activate', :token => token.value)
261 261 render_multipart('register', body)
262 262 end
263 263
264 264 def test(user)
265 265 set_language_if_valid(user.language)
266 266 recipients user.mail
267 267 subject 'Redmine test'
268 268 body :url => url_for(:controller => 'welcome')
269 269 render_multipart('test', body)
270 270 end
271 271
272 272 # Overrides default deliver! method to prevent from sending an email
273 273 # with no recipient, cc or bcc
274 274 def deliver!(mail = @mail)
275 275 set_language_if_valid @initial_language
276 276 return false if (recipients.nil? || recipients.empty?) &&
277 277 (cc.nil? || cc.empty?) &&
278 278 (bcc.nil? || bcc.empty?)
279 279
280 280 # Set Message-Id and References
281 281 if @message_id_object
282 282 mail.message_id = self.class.message_id_for(@message_id_object)
283 283 end
284 284 if @references_objects
285 285 mail.references = @references_objects.collect {|o| self.class.message_id_for(o)}
286 286 end
287 287
288 288 # Log errors when raise_delivery_errors is set to false, Rails does not
289 289 raise_errors = self.class.raise_delivery_errors
290 290 self.class.raise_delivery_errors = true
291 291 begin
292 292 return super(mail)
293 293 rescue Exception => e
294 294 if raise_errors
295 295 raise e
296 296 elsif mylogger
297 297 mylogger.error "The following error occured while sending email notification: \"#{e.message}\". Check your configuration in config/email.yml."
298 298 end
299 299 ensure
300 300 self.class.raise_delivery_errors = raise_errors
301 301 end
302 302 end
303 303
304 304 # Sends reminders to issue assignees
305 305 # Available options:
306 306 # * :days => how many days in the future to remind about (defaults to 7)
307 307 # * :tracker => id of tracker for filtering issues (defaults to all trackers)
308 308 # * :project => id or identifier of project to process (defaults to all projects)
309 309 def self.reminders(options={})
310 310 days = options[:days] || 7
311 311 project = options[:project] ? Project.find(options[:project]) : nil
312 312 tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil
313 313
314 314 s = ARCondition.new ["#{IssueStatus.table_name}.is_closed = ? AND #{Issue.table_name}.due_date <= ?", false, days.day.from_now.to_date]
315 315 s << "#{Issue.table_name}.assigned_to_id IS NOT NULL"
316 316 s << "#{Project.table_name}.status = #{Project::STATUS_ACTIVE}"
317 317 s << "#{Issue.table_name}.project_id = #{project.id}" if project
318 318 s << "#{Issue.table_name}.tracker_id = #{tracker.id}" if tracker
319 319
320 320 issues_by_assignee = Issue.find(:all, :include => [:status, :assigned_to, :project, :tracker],
321 321 :conditions => s.conditions
322 322 ).group_by(&:assigned_to)
323 323 issues_by_assignee.each do |assignee, issues|
324 324 deliver_reminder(assignee, issues, days) unless assignee.nil?
325 325 end
326 326 end
327 327
328 328 private
329 329 def initialize_defaults(method_name)
330 330 super
331 331 @initial_language = current_language
332 332 set_language_if_valid Setting.default_language
333 333 from Setting.mail_from
334 334
335 335 # Common headers
336 336 headers 'X-Mailer' => 'Redmine',
337 337 'X-Redmine-Host' => Setting.host_name,
338 338 'X-Redmine-Site' => Setting.app_title,
339 339 'Precedence' => 'bulk',
340 340 'Auto-Submitted' => 'auto-generated'
341 341 end
342 342
343 343 # Appends a Redmine header field (name is prepended with 'X-Redmine-')
344 344 def redmine_headers(h)
345 345 h.each { |k,v| headers["X-Redmine-#{k}"] = v }
346 346 end
347 347
348 348 # Overrides the create_mail method
349 349 def create_mail
350 350 # Removes the current user from the recipients and cc
351 351 # if he doesn't want to receive notifications about what he does
352 352 @author ||= User.current
353 353 if @author.pref[:no_self_notified]
354 354 recipients.delete(@author.mail) if recipients
355 355 cc.delete(@author.mail) if cc
356 356 end
357 357
358 358 notified_users = [recipients, cc].flatten.compact.uniq
359 359 # Rails would log recipients only, not cc and bcc
360 360 mylogger.info "Sending email notification to: #{notified_users.join(', ')}" if mylogger
361 361
362 362 # Blind carbon copy recipients
363 363 if Setting.bcc_recipients?
364 364 bcc(notified_users)
365 365 recipients []
366 366 cc []
367 367 end
368 368 super
369 369 end
370 370
371 371 # Rails 2.3 has problems rendering implicit multipart messages with
372 372 # layouts so this method will wrap an multipart messages with
373 373 # explicit parts.
374 374 #
375 375 # https://rails.lighthouseapp.com/projects/8994/tickets/2338-actionmailer-mailer-views-and-content-type
376 376 # https://rails.lighthouseapp.com/projects/8994/tickets/1799-actionmailer-doesnt-set-template_format-when-rendering-layouts
377 377
378 378 def render_multipart(method_name, body)
379 379 if Setting.plain_text_mail?
380 380 content_type "text/plain"
381 381 body render(:file => "#{method_name}.text.plain.rhtml", :body => body, :layout => 'mailer.text.plain.erb')
382 382 else
383 383 content_type "multipart/alternative"
384 384 part :content_type => "text/plain", :body => render(:file => "#{method_name}.text.plain.rhtml", :body => body, :layout => 'mailer.text.plain.erb')
385 385 part :content_type => "text/html", :body => render_message("#{method_name}.text.html.rhtml", body)
386 386 end
387 387 end
388 388
389 389 # Makes partial rendering work with Rails 1.2 (retro-compatibility)
390 390 def self.controller_path
391 391 ''
392 392 end unless respond_to?('controller_path')
393 393
394 394 # Returns a predictable Message-Id for the given object
395 395 def self.message_id_for(object)
396 396 # id + timestamp should reduce the odds of a collision
397 397 # as far as we don't send multiple emails for the same object
398 398 timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on)
399 399 hash = "redmine.#{object.class.name.demodulize.underscore}-#{object.id}.#{timestamp.strftime("%Y%m%d%H%M%S")}"
400 400 host = Setting.mail_from.to_s.gsub(%r{^.*@}, '')
401 401 host = "#{::Socket.gethostname}.redmine" if host.empty?
402 402 "<#{hash}@#{host}>"
403 403 end
404 404
405 405 private
406 406
407 407 def message_id(object)
408 408 @message_id_object = object
409 409 end
410 410
411 411 def references(object)
412 412 @references_objects ||= []
413 413 @references_objects << object
414 414 end
415 415
416 416 def mylogger
417 417 RAILS_DEFAULT_LOGGER
418 418 end
419 419 end
420 420
421 421 # Patch TMail so that message_id is not overwritten
422 422 module TMail
423 423 class Mail
424 424 def add_message_id( fqdn = nil )
425 425 self.message_id ||= ::TMail::new_message_id(fqdn)
426 426 end
427 427 end
428 428 end
@@ -1,354 +1,368
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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.dirname(__FILE__) + '/../test_helper'
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, :member_roles, :roles, :documents, :attachments, :news, :tokens, :journals, :journal_details, :changesets, :trackers, :issue_statuses, :enumerations, :messages, :boards, :repositories
24 24
25 25 def setup
26 26 ActionMailer::Base.deliveries.clear
27 27 Setting.host_name = 'mydomain.foo'
28 28 Setting.protocol = 'http'
29 29 end
30 30
31 31 def test_generated_links_in_emails
32 32 Setting.host_name = 'mydomain.foo'
33 33 Setting.protocol = 'https'
34 34
35 35 journal = Journal.find(2)
36 36 assert Mailer.deliver_issue_edit(journal)
37 37
38 38 mail = ActionMailer::Base.deliveries.last
39 39 assert_kind_of TMail::Mail, mail
40 40
41 41 assert_select_email do
42 42 # link to the main ticket
43 43 assert_select "a[href=?]", "https://mydomain.foo/issues/1", :text => "Bug #1: Can't print recipes"
44 44 # link to a referenced ticket
45 45 assert_select "a[href=?][title=?]", "https://mydomain.foo/issues/2", "Add ingredients categories (Assigned)", :text => "#2"
46 46 # link to a changeset
47 47 assert_select "a[href=?][title=?]", "https://mydomain.foo/projects/ecookbook/repository/revisions/2", "This commit fixes #1, #2 and references #1 &amp; #3", :text => "r2"
48 48 end
49 49 end
50 50
51 51 def test_generated_links_with_prefix
52 52 relative_url_root = Redmine::Utils.relative_url_root
53 53 Setting.host_name = 'mydomain.foo/rdm'
54 54 Setting.protocol = 'http'
55 55 Redmine::Utils.relative_url_root = '/rdm'
56 56
57 57 journal = Journal.find(2)
58 58 assert Mailer.deliver_issue_edit(journal)
59 59
60 60 mail = ActionMailer::Base.deliveries.last
61 61 assert_kind_of TMail::Mail, mail
62 62
63 63 assert_select_email do
64 64 # link to the main ticket
65 65 assert_select "a[href=?]", "http://mydomain.foo/rdm/issues/1", :text => "Bug #1: Can't print recipes"
66 66 # link to a referenced ticket
67 67 assert_select "a[href=?][title=?]", "http://mydomain.foo/rdm/issues/2", "Add ingredients categories (Assigned)", :text => "#2"
68 68 # link to a changeset
69 69 assert_select "a[href=?][title=?]", "http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2", "This commit fixes #1, #2 and references #1 &amp; #3", :text => "r2"
70 70 end
71 71 ensure
72 72 # restore it
73 73 Redmine::Utils.relative_url_root = relative_url_root
74 74 end
75 75
76 76 def test_generated_links_with_prefix_and_no_relative_url_root
77 77 relative_url_root = Redmine::Utils.relative_url_root
78 78 Setting.host_name = 'mydomain.foo/rdm'
79 79 Setting.protocol = 'http'
80 80 Redmine::Utils.relative_url_root = nil
81 81
82 82 journal = Journal.find(2)
83 83 assert Mailer.deliver_issue_edit(journal)
84 84
85 85 mail = ActionMailer::Base.deliveries.last
86 86 assert_kind_of TMail::Mail, mail
87 87
88 88 assert_select_email do
89 89 # link to the main ticket
90 90 assert_select "a[href=?]", "http://mydomain.foo/rdm/issues/1", :text => "Bug #1: Can't print recipes"
91 91 # link to a referenced ticket
92 92 assert_select "a[href=?][title=?]", "http://mydomain.foo/rdm/issues/2", "Add ingredients categories (Assigned)", :text => "#2"
93 93 # link to a changeset
94 94 assert_select "a[href=?][title=?]", "http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2", "This commit fixes #1, #2 and references #1 &amp; #3", :text => "r2"
95 95 end
96 96 ensure
97 97 # restore it
98 98 Redmine::Utils.relative_url_root = relative_url_root
99 99 end
100 100
101 101 def test_email_headers
102 102 issue = Issue.find(1)
103 103 Mailer.deliver_issue_add(issue)
104 104 mail = ActionMailer::Base.deliveries.last
105 105 assert_not_nil mail
106 106 assert_equal 'bulk', mail.header_string('Precedence')
107 107 assert_equal 'auto-generated', mail.header_string('Auto-Submitted')
108 108 end
109 109
110 110 def test_plain_text_mail
111 111 Setting.plain_text_mail = 1
112 112 journal = Journal.find(2)
113 113 Mailer.deliver_issue_edit(journal)
114 114 mail = ActionMailer::Base.deliveries.last
115 115 assert_equal "text/plain", mail.content_type
116 116 assert_equal 0, mail.parts.size
117 117 assert !mail.encoded.include?('href')
118 118 end
119 119
120 120 def test_html_mail
121 121 Setting.plain_text_mail = 0
122 122 journal = Journal.find(2)
123 123 Mailer.deliver_issue_edit(journal)
124 124 mail = ActionMailer::Base.deliveries.last
125 125 assert_equal 2, mail.parts.size
126 126 assert mail.encoded.include?('href')
127 127 end
128 128
129 129 def test_mail_from_with_phrase
130 130 with_settings :mail_from => 'Redmine app <redmine@example.net>' do
131 131 Mailer.deliver_test(User.find(1))
132 132 end
133 133 mail = ActionMailer::Base.deliveries.last
134 134 assert_not_nil mail
135 135 assert_equal 'Redmine app', mail.from_addrs.first.name
136 136 end
137 137
138 138 def test_should_not_send_email_without_recipient
139 139 news = News.find(:first)
140 140 user = news.author
141 141 # Remove members except news author
142 142 news.project.memberships.each {|m| m.destroy unless m.user == user}
143 143
144 144 user.pref[:no_self_notified] = false
145 145 user.pref.save
146 146 User.current = user
147 147 Mailer.deliver_news_added(news.reload)
148 148 assert_equal 1, last_email.bcc.size
149 149
150 150 # nobody to notify
151 151 user.pref[:no_self_notified] = true
152 152 user.pref.save
153 153 User.current = user
154 154 ActionMailer::Base.deliveries.clear
155 155 Mailer.deliver_news_added(news.reload)
156 156 assert ActionMailer::Base.deliveries.empty?
157 157 end
158 158
159 159 def test_issue_add_message_id
160 160 issue = Issue.find(1)
161 161 Mailer.deliver_issue_add(issue)
162 162 mail = ActionMailer::Base.deliveries.last
163 163 assert_not_nil mail
164 164 assert_equal Mailer.message_id_for(issue), mail.message_id
165 165 assert_nil mail.references
166 166 end
167 167
168 168 def test_issue_edit_message_id
169 169 journal = Journal.find(1)
170 170 Mailer.deliver_issue_edit(journal)
171 171 mail = ActionMailer::Base.deliveries.last
172 172 assert_not_nil mail
173 173 assert_equal Mailer.message_id_for(journal), mail.message_id
174 174 assert_equal Mailer.message_id_for(journal.issue), mail.references.first.to_s
175 175 end
176 176
177 177 def test_message_posted_message_id
178 178 message = Message.find(1)
179 179 Mailer.deliver_message_posted(message)
180 180 mail = ActionMailer::Base.deliveries.last
181 181 assert_not_nil mail
182 182 assert_equal Mailer.message_id_for(message), mail.message_id
183 183 assert_nil mail.references
184 184 assert_select_email do
185 185 # link to the message
186 186 assert_select "a[href=?]", "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.id}", :text => message.subject
187 187 end
188 188 end
189 189
190 190 def test_reply_posted_message_id
191 191 message = Message.find(3)
192 192 Mailer.deliver_message_posted(message)
193 193 mail = ActionMailer::Base.deliveries.last
194 194 assert_not_nil mail
195 195 assert_equal Mailer.message_id_for(message), mail.message_id
196 196 assert_equal Mailer.message_id_for(message.parent), mail.references.first.to_s
197 197 assert_select_email do
198 198 # link to the reply
199 199 assert_select "a[href=?]", "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.root.id}?r=#{message.id}#message-#{message.id}", :text => message.subject
200 200 end
201 201 end
202 202
203 203 context("#issue_add") do
204 204 setup do
205 205 ActionMailer::Base.deliveries.clear
206 206 Setting.bcc_recipients = '1'
207 207 @issue = Issue.find(1)
208 208 end
209 209
210 210 should "notify project members" do
211 211 assert Mailer.deliver_issue_add(@issue)
212 212 assert last_email.bcc.include?('dlopper@somenet.foo')
213 213 end
214 214
215 215 should "not notify project members that are not allow to view the issue" do
216 216 Role.find(2).remove_permission!(:view_issues)
217 217 assert Mailer.deliver_issue_add(@issue)
218 218 assert !last_email.bcc.include?('dlopper@somenet.foo')
219 219 end
220 220
221 221 should "notify issue watchers" do
222 222 user = User.find(9)
223 223 # minimal email notification options
224 224 user.pref[:no_self_notified] = '1'
225 225 user.pref.save
226 226 user.mail_notification = false
227 227 user.save
228 228
229 229 Watcher.create!(:watchable => @issue, :user => user)
230 230 assert Mailer.deliver_issue_add(@issue)
231 231 assert last_email.bcc.include?(user.mail)
232 232 end
233 233
234 234 should "not notify watchers not allowed to view the issue" do
235 235 user = User.find(9)
236 236 Watcher.create!(:watchable => @issue, :user => user)
237 237 Role.non_member.remove_permission!(:view_issues)
238 238 assert Mailer.deliver_issue_add(@issue)
239 239 assert !last_email.bcc.include?(user.mail)
240 240 end
241 241 end
242 242
243 243 # test mailer methods for each language
244 244 def test_issue_add
245 245 issue = Issue.find(1)
246 246 valid_languages.each do |lang|
247 247 Setting.default_language = lang.to_s
248 248 assert Mailer.deliver_issue_add(issue)
249 249 end
250 250 end
251 251
252 252 def test_issue_edit
253 253 journal = Journal.find(1)
254 254 valid_languages.each do |lang|
255 255 Setting.default_language = lang.to_s
256 256 assert Mailer.deliver_issue_edit(journal)
257 257 end
258 258 end
259 259
260 260 def test_document_added
261 261 document = Document.find(1)
262 262 valid_languages.each do |lang|
263 263 Setting.default_language = lang.to_s
264 264 assert Mailer.deliver_document_added(document)
265 265 end
266 266 end
267 267
268 268 def test_attachments_added
269 269 attachements = [ Attachment.find_by_container_type('Document') ]
270 270 valid_languages.each do |lang|
271 271 Setting.default_language = lang.to_s
272 272 assert Mailer.deliver_attachments_added(attachements)
273 273 end
274 274 end
275 275
276 def test_version_file_added
277 attachements = [ Attachment.find_by_container_type('Version') ]
278 assert Mailer.deliver_attachments_added(attachements)
279 assert_not_nil last_email.bcc
280 assert last_email.bcc.any?
281 end
282
283 def test_project_file_added
284 attachements = [ Attachment.find_by_container_type('Project') ]
285 assert Mailer.deliver_attachments_added(attachements)
286 assert_not_nil last_email.bcc
287 assert last_email.bcc.any?
288 end
289
276 290 def test_news_added
277 291 news = News.find(:first)
278 292 valid_languages.each do |lang|
279 293 Setting.default_language = lang.to_s
280 294 assert Mailer.deliver_news_added(news)
281 295 end
282 296 end
283 297
284 298 def test_message_posted
285 299 message = Message.find(:first)
286 300 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
287 301 recipients = recipients.compact.uniq
288 302 valid_languages.each do |lang|
289 303 Setting.default_language = lang.to_s
290 304 assert Mailer.deliver_message_posted(message)
291 305 end
292 306 end
293 307
294 308 def test_account_information
295 309 user = User.find(:first)
296 310 valid_languages.each do |lang|
297 311 user.update_attribute :language, lang.to_s
298 312 user.reload
299 313 assert Mailer.deliver_account_information(user, 'pAsswORd')
300 314 end
301 315 end
302 316
303 317 def test_lost_password
304 318 token = Token.find(2)
305 319 valid_languages.each do |lang|
306 320 token.user.update_attribute :language, lang.to_s
307 321 token.reload
308 322 assert Mailer.deliver_lost_password(token)
309 323 end
310 324 end
311 325
312 326 def test_register
313 327 token = Token.find(1)
314 328 Setting.host_name = 'redmine.foo'
315 329 Setting.protocol = 'https'
316 330
317 331 valid_languages.each do |lang|
318 332 token.user.update_attribute :language, lang.to_s
319 333 token.reload
320 334 ActionMailer::Base.deliveries.clear
321 335 assert Mailer.deliver_register(token)
322 336 mail = ActionMailer::Base.deliveries.last
323 337 assert mail.body.include?("https://redmine.foo/account/activate?token=#{token.value}")
324 338 end
325 339 end
326 340
327 341 def test_reminders
328 342 Mailer.reminders(:days => 42)
329 343 assert_equal 1, ActionMailer::Base.deliveries.size
330 344 mail = ActionMailer::Base.deliveries.last
331 345 assert mail.bcc.include?('dlopper@somenet.foo')
332 346 assert mail.body.include?('Bug #3: Error 281 when updating a recipe')
333 347 end
334 348
335 349 def last_email
336 350 mail = ActionMailer::Base.deliveries.last
337 351 assert_not_nil mail
338 352 mail
339 353 end
340 354
341 355 def test_mailer_should_not_change_locale
342 356 Setting.default_language = 'en'
343 357 # Set current language to italian
344 358 set_language_if_valid 'it'
345 359 # Send an email to a french user
346 360 user = User.find(1)
347 361 user.language = 'fr'
348 362 Mailer.deliver_account_activated(user)
349 363 mail = ActionMailer::Base.deliveries.last
350 364 assert mail.body.include?('Votre compte')
351 365
352 366 assert_equal :it, current_language
353 367 end
354 368 end
General Comments 0
You need to be logged in to leave comments. Login now