aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/application_mailer_spec.rb
blob: cc4609e5a1a475b305e5f65929c5901878f9f2b8 (plain)
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
150
151
152
153
154
155
156
157
158
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')


describe ApplicationMailer do

    context 'when using plugins' do

        def set_base_views
            ApplicationMailer.class_eval do
                @previous_view_paths = self.view_paths.dup
                self.view_paths = [File.join(Rails.root, 'spec', 'fixtures', 'theme_views', 'core')]
            end
        end

        def add_mail_methods(method_names)
            method_names.each{ |method_name| ApplicationMailer.send(:define_method, method_name){} }
        end

        def remove_mail_methods(method_names)
            method_names.each do |method_name|
                if ApplicationMailer.respond_to?(method_name)
                    ApplicationMailer.send(:remove_method, method_name)
                end
            end
        end

        def prepend_theme_views(theme_name)
            ApplicationMailer.class_eval do
                prepend_view_path File.join(Rails.root, 'spec', 'fixtures', 'theme_views', theme_name)
            end
        end

        def append_theme_views(theme_name)
            ApplicationMailer.class_eval do
                append_view_path File.join(Rails.root, 'spec', 'fixtures', 'theme_views', theme_name)
            end
        end

        def reset_views
            ApplicationMailer.class_eval do
                 self.view_paths = @previous_view_paths
            end
        end

        def create_multipart_method(method_name)
            ApplicationMailer.send(:define_method, method_name) do
                attachments['original.eml'] = 'xxx'
                mail
            end
        end

        before do
            set_base_views
            add_mail_methods(['simple', 'theme_only', 'core_only', 'neither'])
        end

        describe 'when a plugin prepends its mail templates to the view paths' do

            it 'should render a theme template in preference to a core template' do
                prepend_theme_views('theme_one')
                @mail = ApplicationMailer.create_simple()
                @mail.body.should match('Theme simple')
            end

            it 'should render the template provided by the theme if no template is available in core' do
                prepend_theme_views('theme_one')
                @mail = ApplicationMailer.create_theme_only()
                @mail.body.should match('Theme only')
            end

            it 'should render the template provided by core if there is no theme template' do
                prepend_theme_views('theme_one')
                @mail = ApplicationMailer.create_core_only()
                @mail.body.should match('Core only')
            end

            it 'should render an empty body if the template is in neither core nor theme' do
                prepend_theme_views('theme_one')
                @mail = ApplicationMailer.create_neither()
                @mail.body.should be_empty
            end

            it 'should render a multipart email using a theme template' do
                prepend_theme_views('theme_one')
                create_multipart_method('multipart_theme_only')
                @mail = ApplicationMailer.create_multipart_theme_only()
                @mail.parts.size.should == 2
                message_part = @mail.parts[0].to_s
                message_part.should match("Theme multipart")
            end

            it 'should render a multipart email using a core template' do
                prepend_theme_views('theme_one')
                create_multipart_method('multipart_core_only')
                @mail = ApplicationMailer.create_multipart_core_only()
                @mail.parts.size.should == 2
                message_part = @mail.parts[0].to_s
                message_part.should match("Core multipart")
            end

        end

        describe 'when a plugin appends its mail templates to the view paths' do

            it 'should render a core template in preference to a theme template' do
                append_theme_views('theme_one')
                @mail = ApplicationMailer.create_simple()
                @mail.body.should match('Core simple')
            end

            it 'should render the template provided by the theme if no template is available in core' do
                append_theme_views('theme_one')
                @mail = ApplicationMailer.create_theme_only()
                @mail.body.should match('Theme only')
            end

            it 'should render the template provided by core if there is no theme template' do
                append_theme_views('theme_one')
                @mail = ApplicationMailer.create_core_only()
                @mail.body.should match('Core only')
            end

            it 'should render an empty body if the template is in neither core nor theme' do
                append_theme_views('theme_one')
                @mail = ApplicationMailer.create_neither()
                @mail.body.should be_empty
            end

            it 'should render a multipart email using a core template' do
                append_theme_views('theme_one')
                create_multipart_method('multipart_core_only')
                @mail = ApplicationMailer.create_multipart_core_only()
                @mail.parts.size.should == 2
                message_part = @mail.parts[0].to_s
                message_part.should match("Core multipart")
            end

            it 'should render a multipart email using a theme template' do
                append_theme_views('theme_one')
                create_multipart_method('multipart_theme_only')
                @mail = ApplicationMailer.create_multipart_theme_only()
                @mail.parts.size.should == 2
                message_part = @mail.parts[0].to_s
                message_part.should match("Theme multipart")
            end

        end

        after do
            reset_views
            remove_mail_methods(['simple', 'theme_only', 'core_only', 'neither', 'multipart'])
        end
    end

end