##// END OF EJS Templates
Fixed: TMail 1.2.7 malforms To header when number of recipients more then 8 (#8751)....
Jean-Philippe Lang -
r7743:e39d7a0ca8d9
parent child
Show More
@@ -1,107 +1,114
1
1
2 require 'active_record'
2 require 'active_record'
3
3
4 module ActiveRecord
4 module ActiveRecord
5 class Base
5 class Base
6 include Redmine::I18n
6 include Redmine::I18n
7
7
8 # Translate attribute names for validation errors display
8 # Translate attribute names for validation errors display
9 def self.human_attribute_name(attr)
9 def self.human_attribute_name(attr)
10 l("field_#{attr.to_s.gsub(/_id$/, '')}", :default => attr)
10 l("field_#{attr.to_s.gsub(/_id$/, '')}", :default => attr)
11 end
11 end
12 end
12 end
13 end
13 end
14
14
15 module ActiveRecord
15 module ActiveRecord
16 class Errors
16 class Errors
17 def full_messages(options = {})
17 def full_messages(options = {})
18 full_messages = []
18 full_messages = []
19
19
20 @errors.each_key do |attr|
20 @errors.each_key do |attr|
21 @errors[attr].each do |message|
21 @errors[attr].each do |message|
22 next unless message
22 next unless message
23
23
24 if attr == "base"
24 if attr == "base"
25 full_messages << message
25 full_messages << message
26 elsif attr == "custom_values"
26 elsif attr == "custom_values"
27 # Replace the generic "custom values is invalid"
27 # Replace the generic "custom values is invalid"
28 # with the errors on custom values
28 # with the errors on custom values
29 @base.custom_values.each do |value|
29 @base.custom_values.each do |value|
30 value.errors.each do |attr, msg|
30 value.errors.each do |attr, msg|
31 full_messages << value.custom_field.name + ' ' + msg
31 full_messages << value.custom_field.name + ' ' + msg
32 end
32 end
33 end
33 end
34 else
34 else
35 attr_name = @base.class.human_attribute_name(attr)
35 attr_name = @base.class.human_attribute_name(attr)
36 full_messages << attr_name + ' ' + message.to_s
36 full_messages << attr_name + ' ' + message.to_s
37 end
37 end
38 end
38 end
39 end
39 end
40 full_messages
40 full_messages
41 end
41 end
42 end
42 end
43 end
43 end
44
44
45 module ActionView
45 module ActionView
46 module Helpers
46 module Helpers
47 module DateHelper
47 module DateHelper
48 # distance_of_time_in_words breaks when difference is greater than 30 years
48 # distance_of_time_in_words breaks when difference is greater than 30 years
49 def distance_of_date_in_words(from_date, to_date = 0, options = {})
49 def distance_of_date_in_words(from_date, to_date = 0, options = {})
50 from_date = from_date.to_date if from_date.respond_to?(:to_date)
50 from_date = from_date.to_date if from_date.respond_to?(:to_date)
51 to_date = to_date.to_date if to_date.respond_to?(:to_date)
51 to_date = to_date.to_date if to_date.respond_to?(:to_date)
52 distance_in_days = (to_date - from_date).abs
52 distance_in_days = (to_date - from_date).abs
53
53
54 I18n.with_options :locale => options[:locale], :scope => :'datetime.distance_in_words' do |locale|
54 I18n.with_options :locale => options[:locale], :scope => :'datetime.distance_in_words' do |locale|
55 case distance_in_days
55 case distance_in_days
56 when 0..60 then locale.t :x_days, :count => distance_in_days.round
56 when 0..60 then locale.t :x_days, :count => distance_in_days.round
57 when 61..720 then locale.t :about_x_months, :count => (distance_in_days / 30).round
57 when 61..720 then locale.t :about_x_months, :count => (distance_in_days / 30).round
58 else locale.t :over_x_years, :count => (distance_in_days / 365).floor
58 else locale.t :over_x_years, :count => (distance_in_days / 365).floor
59 end
59 end
60 end
60 end
61 end
61 end
62 end
62 end
63 end
63 end
64 end
64 end
65
65
66 ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| "#{html_tag}" }
66 ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| "#{html_tag}" }
67
67
68 module AsynchronousMailer
68 module AsynchronousMailer
69 # Adds :async_smtp and :async_sendmail delivery methods
69 # Adds :async_smtp and :async_sendmail delivery methods
70 # to perform email deliveries asynchronously
70 # to perform email deliveries asynchronously
71 %w(smtp sendmail).each do |type|
71 %w(smtp sendmail).each do |type|
72 define_method("perform_delivery_async_#{type}") do |mail|
72 define_method("perform_delivery_async_#{type}") do |mail|
73 Thread.start do
73 Thread.start do
74 send "perform_delivery_#{type}", mail
74 send "perform_delivery_#{type}", mail
75 end
75 end
76 end
76 end
77 end
77 end
78
78
79 # Adds a delivery method that writes emails in tmp/emails for testing purpose
79 # Adds a delivery method that writes emails in tmp/emails for testing purpose
80 def perform_delivery_tmp_file(mail)
80 def perform_delivery_tmp_file(mail)
81 dest_dir = File.join(Rails.root, 'tmp', 'emails')
81 dest_dir = File.join(Rails.root, 'tmp', 'emails')
82 Dir.mkdir(dest_dir) unless File.directory?(dest_dir)
82 Dir.mkdir(dest_dir) unless File.directory?(dest_dir)
83 File.open(File.join(dest_dir, mail.message_id.gsub(/[<>]/, '') + '.eml'), 'wb') {|f| f.write(mail.encoded) }
83 File.open(File.join(dest_dir, mail.message_id.gsub(/[<>]/, '') + '.eml'), 'wb') {|f| f.write(mail.encoded) }
84 end
84 end
85 end
85 end
86
86
87 ActionMailer::Base.send :include, AsynchronousMailer
87 ActionMailer::Base.send :include, AsynchronousMailer
88
88
89 # TMail::Unquoter.convert_to_with_fallback_on_iso_8859_1 introduced in TMail 1.2.7
90 # triggers a test failure in test_add_issue_with_japanese_keywords(MailHandlerTest)
91 module TMail
89 module TMail
90 # TMail::Unquoter.convert_to_with_fallback_on_iso_8859_1 introduced in TMail 1.2.7
91 # triggers a test failure in test_add_issue_with_japanese_keywords(MailHandlerTest)
92 class Unquoter
92 class Unquoter
93 class << self
93 class << self
94 alias_method :convert_to, :convert_to_without_fallback_on_iso_8859_1
94 alias_method :convert_to, :convert_to_without_fallback_on_iso_8859_1
95 end
95 end
96 end
96 end
97
98 # Patch for TMail 1.2.7. See http://www.redmine.org/issues/8751
99 class Encoder
100 def puts_meta(str)
101 add_text str
102 end
103 end
97 end
104 end
98
105
99 module ActionController
106 module ActionController
100 module MimeResponds
107 module MimeResponds
101 class Responder
108 class Responder
102 def api(&block)
109 def api(&block)
103 any(:xml, :json, &block)
110 any(:xml, :json, &block)
104 end
111 end
105 end
112 end
106 end
113 end
107 end
114 end
@@ -1,489 +1,497
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 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 ActionController::Assertions::SelectorAssertions
22 include ActionController::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, :trackers,
25 :tokens, :journals, :journal_details, :changesets, :trackers,
26 :issue_statuses, :enumerations, :messages, :boards, :repositories,
26 :issue_statuses, :enumerations, :messages, :boards, :repositories,
27 :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions,
27 :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions,
28 :versions,
28 :versions,
29 :comments
29 :comments
30
30
31 def setup
31 def setup
32 ActionMailer::Base.deliveries.clear
32 ActionMailer::Base.deliveries.clear
33 Setting.host_name = 'mydomain.foo'
33 Setting.host_name = 'mydomain.foo'
34 Setting.protocol = 'http'
34 Setting.protocol = 'http'
35 Setting.plain_text_mail = '0'
35 Setting.plain_text_mail = '0'
36 end
36 end
37
37
38 def test_generated_links_in_emails
38 def test_generated_links_in_emails
39 Setting.host_name = 'mydomain.foo'
39 Setting.host_name = 'mydomain.foo'
40 Setting.protocol = 'https'
40 Setting.protocol = 'https'
41
41
42 journal = Journal.find(2)
42 journal = Journal.find(2)
43 assert Mailer.deliver_issue_edit(journal)
43 assert Mailer.deliver_issue_edit(journal)
44
44
45 mail = ActionMailer::Base.deliveries.last
45 mail = ActionMailer::Base.deliveries.last
46 assert_kind_of TMail::Mail, mail
46 assert_kind_of TMail::Mail, mail
47
47
48 assert_select_email do
48 assert_select_email do
49 # link to the main ticket
49 # link to the main ticket
50 assert_select "a[href=?]",
50 assert_select "a[href=?]",
51 "https://mydomain.foo/issues/1",
51 "https://mydomain.foo/issues/1",
52 :text => "Bug #1: Can't print recipes"
52 :text => "Bug #1: Can't print recipes"
53 # link to a referenced ticket
53 # link to a referenced ticket
54 assert_select "a[href=?][title=?]",
54 assert_select "a[href=?][title=?]",
55 "https://mydomain.foo/issues/2",
55 "https://mydomain.foo/issues/2",
56 "Add ingredients categories (Assigned)",
56 "Add ingredients categories (Assigned)",
57 :text => "#2"
57 :text => "#2"
58 # link to a changeset
58 # link to a changeset
59 assert_select "a[href=?][title=?]",
59 assert_select "a[href=?][title=?]",
60 "https://mydomain.foo/projects/ecookbook/repository/revisions/2",
60 "https://mydomain.foo/projects/ecookbook/repository/revisions/2",
61 "This commit fixes #1, #2 and references #1 &amp; #3",
61 "This commit fixes #1, #2 and references #1 &amp; #3",
62 :text => "r2"
62 :text => "r2"
63 end
63 end
64 end
64 end
65
65
66 def test_generated_links_with_prefix
66 def test_generated_links_with_prefix
67 relative_url_root = Redmine::Utils.relative_url_root
67 relative_url_root = Redmine::Utils.relative_url_root
68 Setting.host_name = 'mydomain.foo/rdm'
68 Setting.host_name = 'mydomain.foo/rdm'
69 Setting.protocol = 'http'
69 Setting.protocol = 'http'
70 Redmine::Utils.relative_url_root = '/rdm'
70 Redmine::Utils.relative_url_root = '/rdm'
71
71
72 journal = Journal.find(2)
72 journal = Journal.find(2)
73 assert Mailer.deliver_issue_edit(journal)
73 assert Mailer.deliver_issue_edit(journal)
74
74
75 mail = ActionMailer::Base.deliveries.last
75 mail = ActionMailer::Base.deliveries.last
76 assert_kind_of TMail::Mail, mail
76 assert_kind_of TMail::Mail, mail
77
77
78 assert_select_email do
78 assert_select_email do
79 # link to the main ticket
79 # link to the main ticket
80 assert_select "a[href=?]",
80 assert_select "a[href=?]",
81 "http://mydomain.foo/rdm/issues/1",
81 "http://mydomain.foo/rdm/issues/1",
82 :text => "Bug #1: Can't print recipes"
82 :text => "Bug #1: Can't print recipes"
83 # link to a referenced ticket
83 # link to a referenced ticket
84 assert_select "a[href=?][title=?]",
84 assert_select "a[href=?][title=?]",
85 "http://mydomain.foo/rdm/issues/2",
85 "http://mydomain.foo/rdm/issues/2",
86 "Add ingredients categories (Assigned)",
86 "Add ingredients categories (Assigned)",
87 :text => "#2"
87 :text => "#2"
88 # link to a changeset
88 # link to a changeset
89 assert_select "a[href=?][title=?]",
89 assert_select "a[href=?][title=?]",
90 "http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2",
90 "http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2",
91 "This commit fixes #1, #2 and references #1 &amp; #3",
91 "This commit fixes #1, #2 and references #1 &amp; #3",
92 :text => "r2"
92 :text => "r2"
93 end
93 end
94 ensure
94 ensure
95 # restore it
95 # restore it
96 Redmine::Utils.relative_url_root = relative_url_root
96 Redmine::Utils.relative_url_root = relative_url_root
97 end
97 end
98
98
99 def test_generated_links_with_prefix_and_no_relative_url_root
99 def test_generated_links_with_prefix_and_no_relative_url_root
100 relative_url_root = Redmine::Utils.relative_url_root
100 relative_url_root = Redmine::Utils.relative_url_root
101 Setting.host_name = 'mydomain.foo/rdm'
101 Setting.host_name = 'mydomain.foo/rdm'
102 Setting.protocol = 'http'
102 Setting.protocol = 'http'
103 Redmine::Utils.relative_url_root = nil
103 Redmine::Utils.relative_url_root = nil
104
104
105 journal = Journal.find(2)
105 journal = Journal.find(2)
106 assert Mailer.deliver_issue_edit(journal)
106 assert Mailer.deliver_issue_edit(journal)
107
107
108 mail = ActionMailer::Base.deliveries.last
108 mail = ActionMailer::Base.deliveries.last
109 assert_kind_of TMail::Mail, mail
109 assert_kind_of TMail::Mail, mail
110
110
111 assert_select_email do
111 assert_select_email do
112 # link to the main ticket
112 # link to the main ticket
113 assert_select "a[href=?]",
113 assert_select "a[href=?]",
114 "http://mydomain.foo/rdm/issues/1",
114 "http://mydomain.foo/rdm/issues/1",
115 :text => "Bug #1: Can't print recipes"
115 :text => "Bug #1: Can't print recipes"
116 # link to a referenced ticket
116 # link to a referenced ticket
117 assert_select "a[href=?][title=?]",
117 assert_select "a[href=?][title=?]",
118 "http://mydomain.foo/rdm/issues/2",
118 "http://mydomain.foo/rdm/issues/2",
119 "Add ingredients categories (Assigned)",
119 "Add ingredients categories (Assigned)",
120 :text => "#2"
120 :text => "#2"
121 # link to a changeset
121 # link to a changeset
122 assert_select "a[href=?][title=?]",
122 assert_select "a[href=?][title=?]",
123 "http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2",
123 "http://mydomain.foo/rdm/projects/ecookbook/repository/revisions/2",
124 "This commit fixes #1, #2 and references #1 &amp; #3",
124 "This commit fixes #1, #2 and references #1 &amp; #3",
125 :text => "r2"
125 :text => "r2"
126 end
126 end
127 ensure
127 ensure
128 # restore it
128 # restore it
129 Redmine::Utils.relative_url_root = relative_url_root
129 Redmine::Utils.relative_url_root = relative_url_root
130 end
130 end
131
131
132 def test_email_headers
132 def test_email_headers
133 issue = Issue.find(1)
133 issue = Issue.find(1)
134 Mailer.deliver_issue_add(issue)
134 Mailer.deliver_issue_add(issue)
135 mail = ActionMailer::Base.deliveries.last
135 mail = ActionMailer::Base.deliveries.last
136 assert_not_nil mail
136 assert_not_nil mail
137 assert_equal 'OOF', mail.header_string('X-Auto-Response-Suppress')
137 assert_equal 'OOF', mail.header_string('X-Auto-Response-Suppress')
138 assert_equal 'auto-generated', mail.header_string('Auto-Submitted')
138 assert_equal 'auto-generated', mail.header_string('Auto-Submitted')
139 end
139 end
140
140
141 def test_plain_text_mail
141 def test_plain_text_mail
142 Setting.plain_text_mail = 1
142 Setting.plain_text_mail = 1
143 journal = Journal.find(2)
143 journal = Journal.find(2)
144 Mailer.deliver_issue_edit(journal)
144 Mailer.deliver_issue_edit(journal)
145 mail = ActionMailer::Base.deliveries.last
145 mail = ActionMailer::Base.deliveries.last
146 assert_equal "text/plain", mail.content_type
146 assert_equal "text/plain", mail.content_type
147 assert_equal 0, mail.parts.size
147 assert_equal 0, mail.parts.size
148 assert !mail.encoded.include?('href')
148 assert !mail.encoded.include?('href')
149 end
149 end
150
150
151 def test_html_mail
151 def test_html_mail
152 Setting.plain_text_mail = 0
152 Setting.plain_text_mail = 0
153 journal = Journal.find(2)
153 journal = Journal.find(2)
154 Mailer.deliver_issue_edit(journal)
154 Mailer.deliver_issue_edit(journal)
155 mail = ActionMailer::Base.deliveries.last
155 mail = ActionMailer::Base.deliveries.last
156 assert_equal 2, mail.parts.size
156 assert_equal 2, mail.parts.size
157 assert mail.encoded.include?('href')
157 assert mail.encoded.include?('href')
158 end
158 end
159
159
160 def test_from_header
160 def test_from_header
161 with_settings :mail_from => 'redmine@example.net' do
161 with_settings :mail_from => 'redmine@example.net' do
162 Mailer.deliver_test(User.find(1))
162 Mailer.deliver_test(User.find(1))
163 end
163 end
164 mail = ActionMailer::Base.deliveries.last
164 mail = ActionMailer::Base.deliveries.last
165 assert_not_nil mail
165 assert_not_nil mail
166 assert_equal 'redmine@example.net', mail.from_addrs.first.address
166 assert_equal 'redmine@example.net', mail.from_addrs.first.address
167 end
167 end
168
168
169 def test_from_header_with_phrase
169 def test_from_header_with_phrase
170 with_settings :mail_from => 'Redmine app <redmine@example.net>' do
170 with_settings :mail_from => 'Redmine app <redmine@example.net>' do
171 Mailer.deliver_test(User.find(1))
171 Mailer.deliver_test(User.find(1))
172 end
172 end
173 mail = ActionMailer::Base.deliveries.last
173 mail = ActionMailer::Base.deliveries.last
174 assert_not_nil mail
174 assert_not_nil mail
175 assert_equal 'redmine@example.net', mail.from_addrs.first.address
175 assert_equal 'redmine@example.net', mail.from_addrs.first.address
176 assert_equal 'Redmine app', mail.from_addrs.first.name
176 assert_equal 'Redmine app', mail.from_addrs.first.name
177 end
177 end
178
178
179 def test_should_not_send_email_without_recipient
179 def test_should_not_send_email_without_recipient
180 news = News.find(:first)
180 news = News.find(:first)
181 user = news.author
181 user = news.author
182 # Remove members except news author
182 # Remove members except news author
183 news.project.memberships.each {|m| m.destroy unless m.user == user}
183 news.project.memberships.each {|m| m.destroy unless m.user == user}
184
184
185 user.pref[:no_self_notified] = false
185 user.pref[:no_self_notified] = false
186 user.pref.save
186 user.pref.save
187 User.current = user
187 User.current = user
188 Mailer.deliver_news_added(news.reload)
188 Mailer.deliver_news_added(news.reload)
189 assert_equal 1, last_email.bcc.size
189 assert_equal 1, last_email.bcc.size
190
190
191 # nobody to notify
191 # nobody to notify
192 user.pref[:no_self_notified] = true
192 user.pref[:no_self_notified] = true
193 user.pref.save
193 user.pref.save
194 User.current = user
194 User.current = user
195 ActionMailer::Base.deliveries.clear
195 ActionMailer::Base.deliveries.clear
196 Mailer.deliver_news_added(news.reload)
196 Mailer.deliver_news_added(news.reload)
197 assert ActionMailer::Base.deliveries.empty?
197 assert ActionMailer::Base.deliveries.empty?
198 end
198 end
199
199
200 def test_issue_add_message_id
200 def test_issue_add_message_id
201 issue = Issue.find(1)
201 issue = Issue.find(1)
202 Mailer.deliver_issue_add(issue)
202 Mailer.deliver_issue_add(issue)
203 mail = ActionMailer::Base.deliveries.last
203 mail = ActionMailer::Base.deliveries.last
204 assert_not_nil mail
204 assert_not_nil mail
205 assert_equal Mailer.message_id_for(issue), mail.message_id
205 assert_equal Mailer.message_id_for(issue), mail.message_id
206 assert_nil mail.references
206 assert_nil mail.references
207 end
207 end
208
208
209 def test_issue_edit_message_id
209 def test_issue_edit_message_id
210 journal = Journal.find(1)
210 journal = Journal.find(1)
211 Mailer.deliver_issue_edit(journal)
211 Mailer.deliver_issue_edit(journal)
212 mail = ActionMailer::Base.deliveries.last
212 mail = ActionMailer::Base.deliveries.last
213 assert_not_nil mail
213 assert_not_nil mail
214 assert_equal Mailer.message_id_for(journal), mail.message_id
214 assert_equal Mailer.message_id_for(journal), mail.message_id
215 assert_equal Mailer.message_id_for(journal.issue), mail.references.first.to_s
215 assert_equal Mailer.message_id_for(journal.issue), mail.references.first.to_s
216 end
216 end
217
217
218 def test_message_posted_message_id
218 def test_message_posted_message_id
219 message = Message.find(1)
219 message = Message.find(1)
220 Mailer.deliver_message_posted(message)
220 Mailer.deliver_message_posted(message)
221 mail = ActionMailer::Base.deliveries.last
221 mail = ActionMailer::Base.deliveries.last
222 assert_not_nil mail
222 assert_not_nil mail
223 assert_equal Mailer.message_id_for(message), mail.message_id
223 assert_equal Mailer.message_id_for(message), mail.message_id
224 assert_nil mail.references
224 assert_nil mail.references
225 assert_select_email do
225 assert_select_email do
226 # link to the message
226 # link to the message
227 assert_select "a[href=?]",
227 assert_select "a[href=?]",
228 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.id}",
228 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.id}",
229 :text => message.subject
229 :text => message.subject
230 end
230 end
231 end
231 end
232
232
233 def test_reply_posted_message_id
233 def test_reply_posted_message_id
234 message = Message.find(3)
234 message = Message.find(3)
235 Mailer.deliver_message_posted(message)
235 Mailer.deliver_message_posted(message)
236 mail = ActionMailer::Base.deliveries.last
236 mail = ActionMailer::Base.deliveries.last
237 assert_not_nil mail
237 assert_not_nil mail
238 assert_equal Mailer.message_id_for(message), mail.message_id
238 assert_equal Mailer.message_id_for(message), mail.message_id
239 assert_equal Mailer.message_id_for(message.parent), mail.references.first.to_s
239 assert_equal Mailer.message_id_for(message.parent), mail.references.first.to_s
240 assert_select_email do
240 assert_select_email do
241 # link to the reply
241 # link to the reply
242 assert_select "a[href=?]",
242 assert_select "a[href=?]",
243 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.root.id}?r=#{message.id}#message-#{message.id}",
243 "http://mydomain.foo/boards/#{message.board.id}/topics/#{message.root.id}?r=#{message.id}#message-#{message.id}",
244 :text => message.subject
244 :text => message.subject
245 end
245 end
246 end
246 end
247
247
248 context("#issue_add") do
248 context("#issue_add") do
249 setup do
249 setup do
250 ActionMailer::Base.deliveries.clear
250 ActionMailer::Base.deliveries.clear
251 Setting.bcc_recipients = '1'
251 Setting.bcc_recipients = '1'
252 @issue = Issue.find(1)
252 @issue = Issue.find(1)
253 end
253 end
254
254
255 should "notify project members" do
255 should "notify project members" do
256 assert Mailer.deliver_issue_add(@issue)
256 assert Mailer.deliver_issue_add(@issue)
257 assert last_email.bcc.include?('dlopper@somenet.foo')
257 assert last_email.bcc.include?('dlopper@somenet.foo')
258 end
258 end
259
259
260 should "not notify project members that are not allow to view the issue" do
260 should "not notify project members that are not allow to view the issue" do
261 Role.find(2).remove_permission!(:view_issues)
261 Role.find(2).remove_permission!(:view_issues)
262 assert Mailer.deliver_issue_add(@issue)
262 assert Mailer.deliver_issue_add(@issue)
263 assert !last_email.bcc.include?('dlopper@somenet.foo')
263 assert !last_email.bcc.include?('dlopper@somenet.foo')
264 end
264 end
265
265
266 should "notify issue watchers" do
266 should "notify issue watchers" do
267 user = User.find(9)
267 user = User.find(9)
268 # minimal email notification options
268 # minimal email notification options
269 user.pref[:no_self_notified] = '1'
269 user.pref[:no_self_notified] = '1'
270 user.pref.save
270 user.pref.save
271 user.mail_notification = false
271 user.mail_notification = false
272 user.save
272 user.save
273
273
274 Watcher.create!(:watchable => @issue, :user => user)
274 Watcher.create!(:watchable => @issue, :user => user)
275 assert Mailer.deliver_issue_add(@issue)
275 assert Mailer.deliver_issue_add(@issue)
276 assert last_email.bcc.include?(user.mail)
276 assert last_email.bcc.include?(user.mail)
277 end
277 end
278
278
279 should "not notify watchers not allowed to view the issue" do
279 should "not notify watchers not allowed to view the issue" do
280 user = User.find(9)
280 user = User.find(9)
281 Watcher.create!(:watchable => @issue, :user => user)
281 Watcher.create!(:watchable => @issue, :user => user)
282 Role.non_member.remove_permission!(:view_issues)
282 Role.non_member.remove_permission!(:view_issues)
283 assert Mailer.deliver_issue_add(@issue)
283 assert Mailer.deliver_issue_add(@issue)
284 assert !last_email.bcc.include?(user.mail)
284 assert !last_email.bcc.include?(user.mail)
285 end
285 end
286 end
286 end
287
287
288 # test mailer methods for each language
288 # test mailer methods for each language
289 def test_issue_add
289 def test_issue_add
290 issue = Issue.find(1)
290 issue = Issue.find(1)
291 valid_languages.each do |lang|
291 valid_languages.each do |lang|
292 Setting.default_language = lang.to_s
292 Setting.default_language = lang.to_s
293 assert Mailer.deliver_issue_add(issue)
293 assert Mailer.deliver_issue_add(issue)
294 end
294 end
295 end
295 end
296
296
297 def test_issue_edit
297 def test_issue_edit
298 journal = Journal.find(1)
298 journal = Journal.find(1)
299 valid_languages.each do |lang|
299 valid_languages.each do |lang|
300 Setting.default_language = lang.to_s
300 Setting.default_language = lang.to_s
301 assert Mailer.deliver_issue_edit(journal)
301 assert Mailer.deliver_issue_edit(journal)
302 end
302 end
303 end
303 end
304
304
305 def test_document_added
305 def test_document_added
306 document = Document.find(1)
306 document = Document.find(1)
307 valid_languages.each do |lang|
307 valid_languages.each do |lang|
308 Setting.default_language = lang.to_s
308 Setting.default_language = lang.to_s
309 assert Mailer.deliver_document_added(document)
309 assert Mailer.deliver_document_added(document)
310 end
310 end
311 end
311 end
312
312
313 def test_attachments_added
313 def test_attachments_added
314 attachements = [ Attachment.find_by_container_type('Document') ]
314 attachements = [ Attachment.find_by_container_type('Document') ]
315 valid_languages.each do |lang|
315 valid_languages.each do |lang|
316 Setting.default_language = lang.to_s
316 Setting.default_language = lang.to_s
317 assert Mailer.deliver_attachments_added(attachements)
317 assert Mailer.deliver_attachments_added(attachements)
318 end
318 end
319 end
319 end
320
320
321 def test_version_file_added
321 def test_version_file_added
322 attachements = [ Attachment.find_by_container_type('Version') ]
322 attachements = [ Attachment.find_by_container_type('Version') ]
323 assert Mailer.deliver_attachments_added(attachements)
323 assert Mailer.deliver_attachments_added(attachements)
324 assert_not_nil last_email.bcc
324 assert_not_nil last_email.bcc
325 assert last_email.bcc.any?
325 assert last_email.bcc.any?
326 assert_select_email do
326 assert_select_email do
327 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
327 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
328 end
328 end
329 end
329 end
330
330
331 def test_project_file_added
331 def test_project_file_added
332 attachements = [ Attachment.find_by_container_type('Project') ]
332 attachements = [ Attachment.find_by_container_type('Project') ]
333 assert Mailer.deliver_attachments_added(attachements)
333 assert Mailer.deliver_attachments_added(attachements)
334 assert_not_nil last_email.bcc
334 assert_not_nil last_email.bcc
335 assert last_email.bcc.any?
335 assert last_email.bcc.any?
336 assert_select_email do
336 assert_select_email do
337 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
337 assert_select "a[href=?]", "http://mydomain.foo/projects/ecookbook/files"
338 end
338 end
339 end
339 end
340
340
341 def test_news_added
341 def test_news_added
342 news = News.find(:first)
342 news = News.find(:first)
343 valid_languages.each do |lang|
343 valid_languages.each do |lang|
344 Setting.default_language = lang.to_s
344 Setting.default_language = lang.to_s
345 assert Mailer.deliver_news_added(news)
345 assert Mailer.deliver_news_added(news)
346 end
346 end
347 end
347 end
348
348
349 def test_news_comment_added
349 def test_news_comment_added
350 comment = Comment.find(2)
350 comment = Comment.find(2)
351 valid_languages.each do |lang|
351 valid_languages.each do |lang|
352 Setting.default_language = lang.to_s
352 Setting.default_language = lang.to_s
353 assert Mailer.deliver_news_comment_added(comment)
353 assert Mailer.deliver_news_comment_added(comment)
354 end
354 end
355 end
355 end
356
356
357 def test_message_posted
357 def test_message_posted
358 message = Message.find(:first)
358 message = Message.find(:first)
359 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
359 recipients = ([message.root] + message.root.children).collect {|m| m.author.mail if m.author}
360 recipients = recipients.compact.uniq
360 recipients = recipients.compact.uniq
361 valid_languages.each do |lang|
361 valid_languages.each do |lang|
362 Setting.default_language = lang.to_s
362 Setting.default_language = lang.to_s
363 assert Mailer.deliver_message_posted(message)
363 assert Mailer.deliver_message_posted(message)
364 end
364 end
365 end
365 end
366
366
367 def test_wiki_content_added
367 def test_wiki_content_added
368 content = WikiContent.find(:first)
368 content = WikiContent.find(:first)
369 valid_languages.each do |lang|
369 valid_languages.each do |lang|
370 Setting.default_language = lang.to_s
370 Setting.default_language = lang.to_s
371 assert_difference 'ActionMailer::Base.deliveries.size' do
371 assert_difference 'ActionMailer::Base.deliveries.size' do
372 assert Mailer.deliver_wiki_content_added(content)
372 assert Mailer.deliver_wiki_content_added(content)
373 end
373 end
374 end
374 end
375 end
375 end
376
376
377 def test_wiki_content_updated
377 def test_wiki_content_updated
378 content = WikiContent.find(:first)
378 content = WikiContent.find(:first)
379 valid_languages.each do |lang|
379 valid_languages.each do |lang|
380 Setting.default_language = lang.to_s
380 Setting.default_language = lang.to_s
381 assert_difference 'ActionMailer::Base.deliveries.size' do
381 assert_difference 'ActionMailer::Base.deliveries.size' do
382 assert Mailer.deliver_wiki_content_updated(content)
382 assert Mailer.deliver_wiki_content_updated(content)
383 end
383 end
384 end
384 end
385 end
385 end
386
386
387 def test_account_information
387 def test_account_information
388 user = User.find(2)
388 user = User.find(2)
389 valid_languages.each do |lang|
389 valid_languages.each do |lang|
390 user.update_attribute :language, lang.to_s
390 user.update_attribute :language, lang.to_s
391 user.reload
391 user.reload
392 assert Mailer.deliver_account_information(user, 'pAsswORd')
392 assert Mailer.deliver_account_information(user, 'pAsswORd')
393 end
393 end
394 end
394 end
395
395
396 def test_lost_password
396 def test_lost_password
397 token = Token.find(2)
397 token = Token.find(2)
398 valid_languages.each do |lang|
398 valid_languages.each do |lang|
399 token.user.update_attribute :language, lang.to_s
399 token.user.update_attribute :language, lang.to_s
400 token.reload
400 token.reload
401 assert Mailer.deliver_lost_password(token)
401 assert Mailer.deliver_lost_password(token)
402 end
402 end
403 end
403 end
404
404
405 def test_register
405 def test_register
406 token = Token.find(1)
406 token = Token.find(1)
407 Setting.host_name = 'redmine.foo'
407 Setting.host_name = 'redmine.foo'
408 Setting.protocol = 'https'
408 Setting.protocol = 'https'
409
409
410 valid_languages.each do |lang|
410 valid_languages.each do |lang|
411 token.user.update_attribute :language, lang.to_s
411 token.user.update_attribute :language, lang.to_s
412 token.reload
412 token.reload
413 ActionMailer::Base.deliveries.clear
413 ActionMailer::Base.deliveries.clear
414 assert Mailer.deliver_register(token)
414 assert Mailer.deliver_register(token)
415 mail = ActionMailer::Base.deliveries.last
415 mail = ActionMailer::Base.deliveries.last
416 assert mail.body.include?("https://redmine.foo/account/activate?token=#{token.value}")
416 assert mail.body.include?("https://redmine.foo/account/activate?token=#{token.value}")
417 end
417 end
418 end
418 end
419
419
420 def test_test
420 def test_test
421 user = User.find(1)
421 user = User.find(1)
422 valid_languages.each do |lang|
422 valid_languages.each do |lang|
423 user.update_attribute :language, lang.to_s
423 user.update_attribute :language, lang.to_s
424 assert Mailer.deliver_test(user)
424 assert Mailer.deliver_test(user)
425 end
425 end
426 end
426 end
427
427
428 def test_reminders
428 def test_reminders
429 Mailer.reminders(:days => 42)
429 Mailer.reminders(:days => 42)
430 assert_equal 1, ActionMailer::Base.deliveries.size
430 assert_equal 1, ActionMailer::Base.deliveries.size
431 mail = ActionMailer::Base.deliveries.last
431 mail = ActionMailer::Base.deliveries.last
432 assert mail.bcc.include?('dlopper@somenet.foo')
432 assert mail.bcc.include?('dlopper@somenet.foo')
433 assert mail.body.include?('Bug #3: Error 281 when updating a recipe')
433 assert mail.body.include?('Bug #3: Error 281 when updating a recipe')
434 assert_equal '1 issue(s) due in the next 42 days', mail.subject
434 assert_equal '1 issue(s) due in the next 42 days', mail.subject
435 end
435 end
436
436
437 def test_reminders_for_users
437 def test_reminders_for_users
438 Mailer.reminders(:days => 42, :users => ['5'])
438 Mailer.reminders(:days => 42, :users => ['5'])
439 assert_equal 0, ActionMailer::Base.deliveries.size # No mail for dlopper
439 assert_equal 0, ActionMailer::Base.deliveries.size # No mail for dlopper
440 Mailer.reminders(:days => 42, :users => ['3'])
440 Mailer.reminders(:days => 42, :users => ['3'])
441 assert_equal 1, ActionMailer::Base.deliveries.size # No mail for dlopper
441 assert_equal 1, ActionMailer::Base.deliveries.size # No mail for dlopper
442 mail = ActionMailer::Base.deliveries.last
442 mail = ActionMailer::Base.deliveries.last
443 assert mail.bcc.include?('dlopper@somenet.foo')
443 assert mail.bcc.include?('dlopper@somenet.foo')
444 assert mail.body.include?('Bug #3: Error 281 when updating a recipe')
444 assert mail.body.include?('Bug #3: Error 281 when updating a recipe')
445 end
445 end
446
446
447 def last_email
447 def last_email
448 mail = ActionMailer::Base.deliveries.last
448 mail = ActionMailer::Base.deliveries.last
449 assert_not_nil mail
449 assert_not_nil mail
450 mail
450 mail
451 end
451 end
452
452
453 def test_mailer_should_not_change_locale
453 def test_mailer_should_not_change_locale
454 Setting.default_language = 'en'
454 Setting.default_language = 'en'
455 # Set current language to italian
455 # Set current language to italian
456 set_language_if_valid 'it'
456 set_language_if_valid 'it'
457 # Send an email to a french user
457 # Send an email to a french user
458 user = User.find(1)
458 user = User.find(1)
459 user.language = 'fr'
459 user.language = 'fr'
460 Mailer.deliver_account_activated(user)
460 Mailer.deliver_account_activated(user)
461 mail = ActionMailer::Base.deliveries.last
461 mail = ActionMailer::Base.deliveries.last
462 assert mail.body.include?('Votre compte')
462 assert mail.body.include?('Votre compte')
463
463
464 assert_equal :it, current_language
464 assert_equal :it, current_language
465 end
465 end
466
466
467 def test_with_deliveries_off
467 def test_with_deliveries_off
468 Mailer.with_deliveries false do
468 Mailer.with_deliveries false do
469 Mailer.deliver_test(User.find(1))
469 Mailer.deliver_test(User.find(1))
470 end
470 end
471 assert ActionMailer::Base.deliveries.empty?
471 assert ActionMailer::Base.deliveries.empty?
472 # should restore perform_deliveries
472 # should restore perform_deliveries
473 assert ActionMailer::Base.perform_deliveries
473 assert ActionMailer::Base.perform_deliveries
474 end
474 end
475
476 def test_tmail_to_header_field_should_not_include_blank_lines
477 mail = TMail::Mail.new
478 mail.to = ["a.user@example.com", "v.user2@example.com", "e.smith@example.com", "info@example.com", "v.pupkin@example.com",
479 "b.user@example.com", "w.user2@example.com", "f.smith@example.com", "info2@example.com", "w.pupkin@example.com"]
480
481 assert !mail.encoded.strip.split("\r\n").detect(&:blank?), "#{mail.encoded} malformed"
482 end
475
483
476 context "layout" do
484 context "layout" do
477 should "include the emails_header" do
485 should "include the emails_header" do
478 with_settings(:emails_header => "*Header content*") do
486 with_settings(:emails_header => "*Header content*") do
479 assert Mailer.deliver_test(User.find(1))
487 assert Mailer.deliver_test(User.find(1))
480
488
481 assert_select_email do
489 assert_select_email do
482 assert_select ".header" do
490 assert_select ".header" do
483 assert_select "strong", :text => "Header content"
491 assert_select "strong", :text => "Header content"
484 end
492 end
485 end
493 end
486 end
494 end
487 end
495 end
488 end
496 end
489 end
497 end
General Comments 0
You need to be logged in to leave comments. Login now