aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailer/test/mail_layout_test.rb
blob: 0877e7b2cb3030a44a1a7c4cb06d4565ef40afa5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
require 'abstract_unit'

class AutoLayoutMailer < ActionMailer::Base
  def hello(recipient)
    recipients recipient
    subject    "You have a mail"
    from       "tester@example.com"
  end

  def spam(recipient)
    recipients recipient
    subject    "You have a mail"
    from       "tester@example.com"

    @world = "Earth"
    render(:inline => "Hello, <%= @world %>", :layout => 'spam')
  end

  def nolayout(recipient)
    recipients recipient
    subject    "You have a mail"
    from       "tester@example.com"

    @world = "Earth"
    render(:inline => "Hello, <%= @world %>", :layout => false)
  end

  def multipart(recipient, type = nil)
    recipients recipient
    subject    "You have a mail"
    from       "tester@example.com"

    content_type(type) if type
  end
end

class ExplicitLayoutMailer < ActionMailer::Base
  layout 'spam', :except => [:logout]

  def signup(recipient)
    recipients recipient
    subject    "You have a mail"
    from       "tester@example.com"
  end

  def logout(recipient)
    recipients recipient
    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
    ActionMailer::Base.deliveries = []

    @recipient = 'test@localhost'
  end

  def teardown
    restore_delivery_method
  end

  def test_should_pickup_default_layout
    mail = AutoLayoutMailer.create_hello(@recipient)
    assert_equal "Hello from layout Inside", mail.body.to_s.strip
  end

  def test_should_pickup_multipart_layout
    mail = AutoLayoutMailer.create_multipart(@recipient)
    # CHANGED: content_type returns an object
    # assert_equal "multipart/alternative", mail.content_type
    assert_equal "multipart/alternative", mail.mime_type
    assert_equal 2, mail.parts.size

    # CHANGED: content_type returns an object
    # assert_equal 'text/plain', mail.parts.first.content_type
    assert_equal 'text/plain', mail.parts.first.mime_type
    
    # CHANGED: body returns an object
    # assert_equal "text/plain layout - text/plain multipart", mail.parts.first.body
    assert_equal "text/plain layout - text/plain multipart", mail.parts.first.body.to_s

    # CHANGED: content_type returns an object
    # assert_equal 'text/html', mail.parts.last.content_type
    assert_equal 'text/html', mail.parts.last.mime_type

    # CHANGED: body returns an object
    # assert_equal "Hello from layout text/html multipart", mail.parts.last.body
    assert_equal "Hello from layout text/html multipart", mail.parts.last.body.to_s
  end

  def test_should_pickup_multipartmixed_layout
    mail = AutoLayoutMailer.create_multipart(@recipient, "multipart/mixed")
    # CHANGED: content_type returns an object
    # assert_equal "multipart/mixed", mail.content_type
    assert_equal "multipart/mixed", mail.mime_type
    assert_equal 2, mail.parts.size

    # CHANGED: content_type returns an object
    # assert_equal 'text/plain', mail.parts.first.content_type
    assert_equal 'text/plain', mail.parts.first.mime_type
    # CHANGED: body returns an object
    # assert_equal "text/plain layout - text/plain multipart", mail.parts.first.body
    assert_equal "text/plain layout - text/plain multipart", mail.parts.first.body.to_s

    # CHANGED: content_type returns an object
    # assert_equal 'text/html', mail.parts.last.content_type
    assert_equal 'text/html', mail.parts.last.mime_type
    # CHANGED: body returns an object
    # assert_equal "Hello from layout text/html multipart", mail.parts.last.body
    assert_equal "Hello from layout text/html multipart", mail.parts.last.body.to_s
  end

  def test_should_fix_multipart_layout
    mail = AutoLayoutMailer.create_multipart(@recipient, "text/plain")
    assert_equal "multipart/alternative", mail.mime_type
    assert_equal 2, mail.parts.size

    assert_equal 'text/plain', mail.parts.first.mime_type
    assert_equal "text/plain layout - text/plain multipart", mail.parts.first.body.to_s

    assert_equal 'text/html', mail.parts.last.mime_type
    assert_equal "Hello from layout text/html multipart", mail.parts.last.body.to_s
  end


  def test_should_pickup_layout_given_to_render
    mail = AutoLayoutMailer.create_spam(@recipient)
    assert_equal "Spammer layout Hello, Earth", mail.body.to_s.strip
  end

  def test_should_respect_layout_false
    mail = AutoLayoutMailer.create_nolayout(@recipient)
    assert_equal "Hello, Earth", mail.body.to_s.strip
  end

  def test_explicit_class_layout
    mail = ExplicitLayoutMailer.create_signup(@recipient)
    assert_equal "Spammer layout We do not spam", mail.body.to_s.strip
  end

  def test_explicit_layout_exceptions
    mail = ExplicitLayoutMailer.create_logout(@recipient)
    assert_equal "You logged out", mail.body.to_s.strip
  end
end