2008-09-09 22:25:09 +00:00
|
|
|
require 'abstract_unit'
|
|
|
|
|
|
|
|
class AutoLayoutMailer < ActionMailer::Base
|
2010-08-29 23:42:09 +00:00
|
|
|
default :to => 'test@localhost',
|
|
|
|
:subject => "You have a mail",
|
|
|
|
:from => "tester@example.com"
|
2010-01-26 15:27:21 +00:00
|
|
|
|
2010-01-24 16:31:18 +00:00
|
|
|
def hello
|
2010-08-29 23:42:09 +00:00
|
|
|
mail()
|
2008-09-09 22:25:09 +00:00
|
|
|
end
|
|
|
|
|
2010-01-24 16:31:18 +00:00
|
|
|
def spam
|
2009-10-21 23:26:10 +00:00
|
|
|
@world = "Earth"
|
2010-08-29 23:42:09 +00:00
|
|
|
mail(:body => render(:inline => "Hello, <%= @world %>", :layout => 'spam'))
|
2008-09-09 22:25:09 +00:00
|
|
|
end
|
|
|
|
|
2010-01-24 16:31:18 +00:00
|
|
|
def nolayout
|
2009-10-21 23:26:10 +00:00
|
|
|
@world = "Earth"
|
2010-08-29 23:42:09 +00:00
|
|
|
mail(:body => render(:inline => "Hello, <%= @world %>", :layout => false))
|
2008-09-09 22:25:09 +00:00
|
|
|
end
|
2008-11-20 21:39:34 +00:00
|
|
|
|
2010-01-24 16:31:18 +00:00
|
|
|
def multipart(type = nil)
|
2010-08-29 23:42:09 +00:00
|
|
|
mail(:content_type => type) do |format|
|
|
|
|
format.text { render }
|
|
|
|
format.html { render }
|
|
|
|
end
|
2008-11-20 21:39:34 +00:00
|
|
|
end
|
2008-09-09 22:25:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
class ExplicitLayoutMailer < ActionMailer::Base
|
|
|
|
layout 'spam', :except => [:logout]
|
|
|
|
|
2010-08-29 23:42:09 +00:00
|
|
|
default :to => 'test@localhost',
|
|
|
|
:subject => "You have a mail",
|
|
|
|
:from => "tester@example.com"
|
|
|
|
|
2010-01-24 16:31:18 +00:00
|
|
|
def signup
|
2010-08-29 23:42:09 +00:00
|
|
|
mail()
|
2008-09-09 22:25:09 +00:00
|
|
|
end
|
|
|
|
|
2010-01-24 16:31:18 +00:00
|
|
|
def logout
|
2010-08-29 23:42:09 +00:00
|
|
|
mail()
|
2008-09-09 22:25:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class LayoutMailerTest < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
set_delivery_method :test
|
|
|
|
ActionMailer::Base.perform_deliveries = true
|
2010-01-24 00:15:42 +00:00
|
|
|
ActionMailer::Base.deliveries.clear
|
2008-09-09 22:25:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
restore_delivery_method
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_pickup_default_layout
|
2010-01-24 16:31:18 +00:00
|
|
|
mail = AutoLayoutMailer.hello
|
2009-12-27 09:56:16 +00:00
|
|
|
assert_equal "Hello from layout Inside", mail.body.to_s.strip
|
2008-09-09 22:25:09 +00:00
|
|
|
end
|
|
|
|
|
2008-11-20 21:39:34 +00:00
|
|
|
def test_should_pickup_multipart_layout
|
2010-01-24 16:31:18 +00:00
|
|
|
mail = AutoLayoutMailer.multipart
|
2010-01-03 00:15:50 +00:00
|
|
|
assert_equal "multipart/alternative", mail.mime_type
|
2009-03-10 16:23:52 +00:00
|
|
|
assert_equal 2, mail.parts.size
|
|
|
|
|
2010-01-03 00:15:50 +00:00
|
|
|
assert_equal 'text/plain', mail.parts.first.mime_type
|
2009-12-27 09:56:16 +00:00
|
|
|
assert_equal "text/plain layout - text/plain multipart", mail.parts.first.body.to_s
|
2009-03-10 16:23:52 +00:00
|
|
|
|
2010-01-03 00:15:50 +00:00
|
|
|
assert_equal 'text/html', mail.parts.last.mime_type
|
2009-12-27 09:56:16 +00:00
|
|
|
assert_equal "Hello from layout text/html multipart", mail.parts.last.body.to_s
|
2009-03-10 16:23:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_pickup_multipartmixed_layout
|
2010-01-24 16:31:18 +00:00
|
|
|
mail = AutoLayoutMailer.multipart("multipart/mixed")
|
2010-01-03 00:15:50 +00:00
|
|
|
assert_equal "multipart/mixed", mail.mime_type
|
2008-11-20 21:39:34 +00:00
|
|
|
assert_equal 2, mail.parts.size
|
|
|
|
|
2010-01-03 00:15:50 +00:00
|
|
|
assert_equal 'text/plain', mail.parts.first.mime_type
|
2009-12-27 09:56:16 +00:00
|
|
|
assert_equal "text/plain layout - text/plain multipart", mail.parts.first.body.to_s
|
2008-11-20 21:39:34 +00:00
|
|
|
|
2010-01-03 00:15:50 +00:00
|
|
|
assert_equal 'text/html', mail.parts.last.mime_type
|
2009-12-27 09:56:16 +00:00
|
|
|
assert_equal "Hello from layout text/html multipart", mail.parts.last.body.to_s
|
2008-11-20 21:39:34 +00:00
|
|
|
end
|
|
|
|
|
2008-09-09 22:25:09 +00:00
|
|
|
def test_should_pickup_layout_given_to_render
|
2010-01-24 16:31:18 +00:00
|
|
|
mail = AutoLayoutMailer.spam
|
2009-12-27 09:56:16 +00:00
|
|
|
assert_equal "Spammer layout Hello, Earth", mail.body.to_s.strip
|
2008-09-09 22:25:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_respect_layout_false
|
2010-01-24 16:31:18 +00:00
|
|
|
mail = AutoLayoutMailer.nolayout
|
2009-12-27 09:56:16 +00:00
|
|
|
assert_equal "Hello, Earth", mail.body.to_s.strip
|
2008-09-09 22:25:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_explicit_class_layout
|
2010-01-24 16:31:18 +00:00
|
|
|
mail = ExplicitLayoutMailer.signup
|
2009-12-27 09:56:16 +00:00
|
|
|
assert_equal "Spammer layout We do not spam", mail.body.to_s.strip
|
2008-09-09 22:25:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_explicit_layout_exceptions
|
2010-01-24 16:31:18 +00:00
|
|
|
mail = ExplicitLayoutMailer.logout
|
2009-12-27 09:56:16 +00:00
|
|
|
assert_equal "You logged out", mail.body.to_s.strip
|
2008-09-09 22:25:09 +00:00
|
|
|
end
|
|
|
|
end
|