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