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