aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailer/test/caching_test.rb
blob: b4344eb1676830c85ecf7814a96caa937d0736b7 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
require 'fileutils'
require 'abstract_unit'
require 'mailers/base_mailer'
require 'mailers/caching_mailer'

CACHE_DIR = 'test_cache'
# Don't change '/../temp/' cavalierly or you might hose something you don't want hosed
FILE_STORE_PATH = File.join(File.dirname(__FILE__), '/../temp/', CACHE_DIR)

class FragmentCachingMailer < ActionMailer::Base
  abstract!

  def some_action; end
end

class BaseCachingTest < ActiveSupport::TestCase
  def setup
    super
    @store = ActiveSupport::Cache::MemoryStore.new
    @mailer = FragmentCachingMailer.new
    @mailer.perform_caching = true
    @mailer.cache_store = @store
  end

  def test_fragment_cache_key
    assert_equal 'views/what a key', @mailer.fragment_cache_key('what a key')
  end
end

class FragmentCachingTest < BaseCachingTest
  def test_read_fragment_with_caching_enabled
    @store.write('views/name', 'value')
    assert_equal 'value', @mailer.read_fragment('name')
  end

  def test_read_fragment_with_caching_disabled
    @mailer.perform_caching = false
    @store.write('views/name', 'value')
    assert_nil @mailer.read_fragment('name')
  end

  def test_fragment_exist_with_caching_enabled
    @store.write('views/name', 'value')
    assert @mailer.fragment_exist?('name')
    assert !@mailer.fragment_exist?('other_name')
  end

  def test_fragment_exist_with_caching_disabled
    @mailer.perform_caching = false
    @store.write('views/name', 'value')
    assert !@mailer.fragment_exist?('name')
    assert !@mailer.fragment_exist?('other_name')
  end

  def test_write_fragment_with_caching_enabled
    assert_nil @store.read('views/name')
    assert_equal 'value', @mailer.write_fragment('name', 'value')
    assert_equal 'value', @store.read('views/name')
  end

  def test_write_fragment_with_caching_disabled
    assert_nil @store.read('views/name')
    @mailer.perform_caching = false
    assert_equal 'value', @mailer.write_fragment('name', 'value')
    assert_nil @store.read('views/name')
  end

  def test_expire_fragment_with_simple_key
    @store.write('views/name', 'value')
    @mailer.expire_fragment 'name'
    assert_nil @store.read('views/name')
  end

  def test_expire_fragment_with_regexp
    @store.write('views/name', 'value')
    @store.write('views/another_name', 'another_value')
    @store.write('views/primalgrasp', 'will not expire ;-)')

    @mailer.expire_fragment(/name/)

    assert_nil @store.read('views/name')
    assert_nil @store.read('views/another_name')
    assert_equal 'will not expire ;-)', @store.read('views/primalgrasp')
  end

  def test_fragment_for
    @store.write('views/expensive', 'fragment content')
    fragment_computed = false

    view_context = @mailer.view_context

    buffer = 'generated till now -> '.html_safe
    buffer << view_context.send(:fragment_for, 'expensive') { fragment_computed = true }

    assert !fragment_computed
    assert_equal 'generated till now -> fragment content', buffer
  end

  def test_html_safety
    assert_nil @store.read('views/name')
    content = 'value'.html_safe
    assert_equal content, @mailer.write_fragment('name', content)

    cached = @store.read('views/name')
    assert_equal content, cached
    assert_equal String, cached.class

    html_safe = @mailer.read_fragment('name')
    assert_equal content, html_safe
    assert html_safe.html_safe?
  end
end

class FunctionalFragmentCachingTest < BaseCachingTest
  def setup
    super
    @store = ActiveSupport::Cache::MemoryStore.new
    @mailer = CachingMailer.new
    @mailer.perform_caching = true
    @mailer.cache_store = @store
  end

  def test_fragment_caching
    email = @mailer.fragment_cache
    expected_body = "\"Welcome\""

    assert_match expected_body, email.body.encoded
    assert_match "\"Welcome\"",
      @store.read("views/caching/#{template_digest("caching_mailer/fragment_cache")}")
  end

  def test_fragment_caching_in_partials
    email = @mailer.fragment_cache_in_partials
    assert_match(/Old fragment caching in a partial/, email.body.encoded)

    assert_match("Old fragment caching in a partial",
      @store.read("views/caching/#{template_digest("caching_mailer/_partial")}"))
  end

  def test_skip_fragment_cache_digesting
    email = @mailer.skip_fragment_cache_digesting
    expected_body = "No Digest"

    assert_match expected_body, email.body.encoded
    assert_match expected_body, @store.read("views/no_digest")
  end

  private

    def template_digest(name)
      ActionView::Digestor.digest(name: name, finder: @mailer.lookup_context)
    end
end

class CacheHelperOutputBufferTest < BaseCachingTest

  class MockController
    def read_fragment(name, options)
      return false
    end

    def write_fragment(name, fragment, options)
      fragment
    end
  end

  def setup
    super
  end

  def test_output_buffer
    output_buffer = ActionView::OutputBuffer.new
    controller = MockController.new
    cache_helper = Class.new do
      def self.controller; end;
      def self.output_buffer; end;
      def self.output_buffer=; end;
    end
    cache_helper.extend(ActionView::Helpers::CacheHelper)

    cache_helper.stub :controller, controller do
      cache_helper.stub :output_buffer, output_buffer do
        assert_called_with cache_helper, :output_buffer=, [output_buffer.class.new(output_buffer)] do
          assert_nothing_raised do
            cache_helper.send :fragment_for, 'Test fragment name', 'Test fragment', &Proc.new{ nil }
          end
        end
      end
    end
  end

  def test_safe_buffer
    output_buffer = ActiveSupport::SafeBuffer.new
    controller = MockController.new
    cache_helper = Class.new do
      def self.controller; end;
      def self.output_buffer; end;
      def self.output_buffer=; end;
    end
    cache_helper.extend(ActionView::Helpers::CacheHelper)

    cache_helper.stub :controller, controller do
      cache_helper.stub :output_buffer, output_buffer do
        assert_called_with cache_helper, :output_buffer=, [output_buffer.class.new(output_buffer)] do
          assert_nothing_raised do
            cache_helper.send :fragment_for, 'Test fragment name', 'Test fragment', &Proc.new{ nil }
          end
        end
      end
    end
  end
end

class ViewCacheDependencyTest < BaseCachingTest
  class NoDependenciesMailer < ActionMailer::Base
  end
  class HasDependenciesMailer < ActionMailer::Base
    view_cache_dependency { "trombone" }
    view_cache_dependency { "flute" }
  end

  def test_view_cache_dependencies_are_empty_by_default
    assert NoDependenciesMailer.new.view_cache_dependencies.empty?
  end

  def test_view_cache_dependencies_are_listed_in_declaration_order
    assert_equal %w(trombone flute), HasDependenciesMailer.new.view_cache_dependencies
  end
end