Skip to content

Commit

Permalink
Add some tests to collector with templates and any.
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim and Mikel Lindsaar committed Jan 23, 2010
1 parent 6ba9446 commit c985a0e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 14 deletions.
2 changes: 1 addition & 1 deletion actionmailer/lib/action_mailer/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'active_support/core_ext/module/delegation'
require 'mail'
require 'action_mailer/tmail_compat'
require 'action_mailer/collector'

module ActionMailer #:nodoc:
# Action Mailer allows you to send email from your application using a mailer model and views.
Expand Down Expand Up @@ -442,7 +443,6 @@ def mail(headers = {})
end

content_type ||= create_parts_from_responses(m, responses, charset)

m.content_type = content_type
m.charset = charset
m.mime_version = mime_version
Expand Down
3 changes: 1 addition & 2 deletions actionmailer/lib/action_mailer/collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ def initialize(context, &block)
@default_formats = context.formats
end

# TODO Test me
def any(*args, &block)
options = args.extract_options!
raise "You have to supply at least one format" if args.empty?
args.each { |type| send(type, options, &block) }
args.each { |type| send(type, options.dup, &block) }
end
alias :all :any

Expand Down
57 changes: 46 additions & 11 deletions actionmailer/test/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,19 @@ def explicit_multipart(hash = {})
format.html { render :text => "HTML Explicit Multipart" }
end
end


def explicit_multipart_templates(hash = {})
mail(DEFAULT_HEADERS.merge(hash)) do |format|
format.html
format.text
end
end

def explicit_multipart_with_any(hash = {})
mail(DEFAULT_HEADERS.merge(hash)) do |format|
format.any(:text, :html){ render :text => "Format with any!" }
end
end
end

test "method call to mail does not raise error" do
Expand Down Expand Up @@ -214,7 +226,7 @@ def explicit_multipart(hash = {})
end

# Implicit multipart
test "implicit multipart tests" do
test "implicit multipart" do
email = BaseMailer.deliver_implicit_multipart
assert_equal(2, email.parts.size)
assert_equal("multipart/alternate", email.mime_type)
Expand All @@ -224,7 +236,7 @@ def explicit_multipart(hash = {})
assert_equal("HTML Implicit Multipart", email.parts[1].body.encoded)
end

test "implicit multipart tests with sort order" do
test "implicit multipart with sort order" do
order = ["text/html", "text/plain"]
swap BaseMailer, :default_implicit_parts_order => order do
email = BaseMailer.deliver_implicit_multipart
Expand Down Expand Up @@ -258,7 +270,8 @@ def explicit_multipart(hash = {})
end
end

test "explicit multipart tests" do
# Explicit multipart
test "explicit multipart" do
email = BaseMailer.deliver_explicit_multipart
assert_equal(2, email.parts.size)
assert_equal("multipart/alternate", email.mime_type)
Expand All @@ -282,15 +295,37 @@ def explicit_multipart(hash = {})
end

test "explicit multipart with attachments creates nested parts" do
email = BaseMailer.deliver_explicit_multipart(:attachments => true)
assert_equal("application/pdf", email.parts[0].mime_type)
assert_equal("multipart/alternate", email.parts[1].mime_type)
assert_equal("text/plain", email.parts[1].parts[0].mime_type)
assert_equal("TEXT Explicit Multipart", email.parts[1].parts[0].body.encoded)
assert_equal("text/html", email.parts[1].parts[1].mime_type)
assert_equal("HTML Explicit Multipart", email.parts[1].parts[1].body.encoded)
email = BaseMailer.deliver_explicit_multipart(:attachments => true)
assert_equal("application/pdf", email.parts[0].mime_type)
assert_equal("multipart/alternate", email.parts[1].mime_type)
assert_equal("text/plain", email.parts[1].parts[0].mime_type)
assert_equal("TEXT Explicit Multipart", email.parts[1].parts[0].body.encoded)
assert_equal("text/html", email.parts[1].parts[1].mime_type)
assert_equal("HTML Explicit Multipart", email.parts[1].parts[1].body.encoded)
end

# TODO Seems Mail is sorting the templates automatically, and not on demand
# test "explicit multipart with templates" do
# email = BaseMailer.deliver_explicit_multipart_templates
# assert_equal(2, email.parts.size)
# assert_equal("multipart/alternate", email.mime_type)
# assert_equal("text/html", email.parts[0].mime_type)
# assert_equal("HTML Explicit Multipart Templates", email.parts[0].body.encoded)
# assert_equal("text/plain", email.parts[1].mime_type)
# assert_equal("TEXT Explicit Multipart Templates", email.parts[1].body.encoded)
# end

test "explicit multipart with any" do
email = BaseMailer.deliver_explicit_multipart_with_any
assert_equal(2, email.parts.size)
assert_equal("multipart/alternate", email.mime_type)
assert_equal("text/plain", email.parts[0].mime_type)
assert_equal("Format with any!", email.parts[0].body.encoded)
assert_equal("text/html", email.parts[1].mime_type)
assert_equal("Format with any!", email.parts[1].body.encoded)
end


protected

# Execute the block setting the given values and restoring old values after
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
HTML Explicit Multipart Templates
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TEXT Explicit Multipart Templates

0 comments on commit c985a0e

Please sign in to comment.