2017-07-23 11:17:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
require "abstract_unit"
|
2008-09-09 18:25:09 -04:00
|
|
|
|
|
|
|
class AutoLayoutMailer < ActionMailer::Base
|
2016-08-06 13:03:39 -04:00
|
|
|
default to: "test@localhost",
|
2012-10-07 13:54:14 -04:00
|
|
|
subject: "You have a mail",
|
|
|
|
from: "tester@example.com"
|
2010-01-26 10:27:21 -05:00
|
|
|
|
2010-01-24 11:31:18 -05:00
|
|
|
def hello
|
2010-08-29 19:42:09 -04:00
|
|
|
mail()
|
2008-09-09 18:25:09 -04:00
|
|
|
end
|
|
|
|
|
2010-01-24 11:31:18 -05:00
|
|
|
def spam
|
2009-10-21 19:26:10 -04:00
|
|
|
@world = "Earth"
|
2016-08-06 13:03:39 -04:00
|
|
|
mail(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
|
2009-10-21 19:26:10 -04:00
|
|
|
@world = "Earth"
|
2012-10-07 13:54:14 -04:00
|
|
|
mail(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)
|
2012-10-07 13:54:14 -04:00
|
|
|
mail(content_type: type) do |format|
|
2010-08-29 19:42:09 -04:00
|
|
|
format.text { render }
|
|
|
|
format.html { render }
|
|
|
|
end
|
2008-11-20 16:39:34 -05:00
|
|
|
end
|
2008-09-09 18:25:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class ExplicitLayoutMailer < ActionMailer::Base
|
2016-08-06 13:03:39 -04:00
|
|
|
layout "spam", except: [:logout]
|
2008-09-09 18:25:09 -04:00
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
default to: "test@localhost",
|
2012-10-07 13:54:14 -04:00
|
|
|
subject: "You have a mail",
|
|
|
|
from: "tester@example.com"
|
2010-08-29 19:42:09 -04:00
|
|
|
|
2010-01-24 11:31:18 -05:00
|
|
|
def signup
|
2010-08-29 19:42:09 -04:00
|
|
|
mail()
|
2008-09-09 18:25:09 -04:00
|
|
|
end
|
|
|
|
|
2010-01-24 11:31:18 -05:00
|
|
|
def logout
|
2010-08-29 19:42:09 -04:00
|
|
|
mail()
|
2008-09-09 18:25:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-05 19:55:54 -05:00
|
|
|
class LayoutMailerTest < ActiveSupport::TestCase
|
2008-09-09 18:25:09 -04:00
|
|
|
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
|
|
|
|
|
2016-08-06 13:03:39 -04: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
|
|
|
|
2016-08-06 13:03:39 -04: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
|
|
|
|
|
2016-08-06 13:03:39 -04: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
|
|
|
|
2016-08-06 13:03:39 -04: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
|
|
|
|
|
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
|