block_cache_test.rb
7.05 KB
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
require 'test_helper'
# Controllers to be tested
class BlockCachingTestController < HomeController
end
class ArticleCachingTestController < ProfileController
end
# Test case
class BlockCacheTest < ActionController::TestCase
CACHE_DIR = 'cache_test'
FILE_STORE_PATH = Rails.root.join('tmp/test', CACHE_DIR)
UP_DIR = ".."
include NoosferoTestHelper
fixtures :environments
#tests CachingTestController
def setup
super
# Enable caching in this test
enable_cache
end
def teardown
#disable_cache
end
def create_blog_with_articles(community)
blog = fast_create(Blog, :name => 'Blog', :profile_id => @person.id)
@article1 = fast_create(TinyMceArticle, :name => "First Post", :profile_id => @person.id, :parent_id => blog.id, :body => '<p> Wasserstoffbombe </p>')
fast_create(TinyMceArticle, :name => "A Post", :profile_id => @person.id, :parent_id => blog.id, :body => '<p>Lorem ipsum dolor sit amet</p> <p>Second paragraph</p>')
block = ArticleBlock.new
block.article = blog
@person.boxes << Box.new
@person.boxes.first.blocks << block
return block
end
# Event item CSS selector
ev = '.event-plugin_event-block ul.events li.event[itemscope]' +
'[itemtype="http://data-vocabulary.org/Event"] '
should 'update event block cache' do
environment = Environment.default
environment.enable_plugin('EventPlugin')
environment.locales.delete_if {|key, value| key != "en" }
box = Box.create!(:owner => environment)
EventPlugin::EventBlock.create!(:box => box)
person = fast_create(Person, :environment_id => environment.id)
@controller = BlockCachingTestController.new
event1 = Event.create!(:name=>'Event 1', :profile =>person)
event1.slug = 'event1a'
event1.start_date = DateTime.now
event1.end_date = DateTime.now + 3.day
event1.save!
get :index
# Check the cache store and save the entry
cache_entry = verify_cache_entries
assert_select ev + 'time.duration[itemprop="endDate"]', /4 days/
# Change the event
event1.reload
event1.start_date = DateTime.now
event1.end_date = DateTime.now + 5.day
event1.save!
get :index
assert_select ev + 'time.duration[itemprop="endDate"]', /6 days/
# Verify if entry have changed
assert verify_cache_entries != cache_entry
end
should 'timestamp change' do
puts "Initializing test..."
@controller = BlockCachingTestController.new
get :index
puts "portal_community: #{@controller.environment.portal_community}"
c = Community.create!(:name => 'community test', :environment => @controller.environment)
a1 = TextileArticle.create!(:name => "Article 1",
:profile => c,
:abstract => "This is the article1 lead.",
:body => "This is the article1 body.",
:highlighted => true)
puts "A1-updated_at: #{a1.updated_at}"
puts "A1-to_i: #{a1.updated_at.to_i}"
puts "A1-to_time: #{a1.updated_at.to_time}"
puts "A1-cache_key: #{a1.cache_key}"
puts "E1-cache_key: #{@controller.environment.cache_key}"
puts "PE1-cache_key: #{a1.environment.cache_key}"
a1.touch
puts "A2-updated_at: #{a1.updated_at}"
puts "A2-to_i: #{a1.updated_at.to_i}"
puts "A2-to_time: #{a1.updated_at.to_time}"
puts "A2-cache_key: #{a1.cache_key}"
puts "E2-cache_key: #{@controller.environment.cache_key}"
puts "PE2-cache_key: #{a1.environment.cache_key}"
get :index
puts "A3-updated_at: #{a1.updated_at}"
puts "A3-to_i: #{a1.updated_at.to_i}"
puts "A3-to_time: #{a1.updated_at.to_time}"
puts "A3-cache_key: #{a1.cache_key}"
puts "E3-cache_key: #{@controller.environment.cache_key}"
puts "PE3-cache_key: #{a1.environment.cache_key}"
end
should 'update article block cache' do
puts "Initializing test..."
@controller = BlockCachingTestController.new
Noosfero.stubs(:locales).returns({"en"=>"English"})
get :index # creates and set the environment on controller
c = Community.create!(:name => 'community test', :environment => @controller.environment)
@controller.environment.portal_community = c
@controller.environment.enable('use_portal_community')
puts "Test.beforeSave Environment..."
@controller.environment.save!
puts "Test.environment Saved!"
puts "Test.index 0"
get :index
puts "Test.index 1"
get :index
puts "Test.index 2"
get :index
puts "Test.index 3"
get :index
puts "Test.creating Article1..."
a1 = TextileArticle.create!(:name => "Article 1",
:profile => c,
:abstract => "This is the article1 lead.",
:body => "This is the article1 body.",
:highlighted => true)
assert a1.environment == c.environment
assert c.environment == @controller.environment
assert @controller.environment == a1.environment
puts "Article1 created"
puts "Test.creating Article2..."
a2 = TextileArticle.create!(:name => "Article 2",
:profile => c,
:body => "This is the article2 body.",
:highlighted => true)
puts "Article2 created"
assert a2.environment == c.environment
assert c.environment == @controller.environment
assert @controller.environment == a2.environment
assert a1.environment == a2.environment
puts "Before INDEX 1"
get :index
#assert_match(/This is the article1 lead/, @response.body)
# Check the cache store and save the entry
#assert verify_cache_entries != UP_DIR, "Cache entry should have been created"
a1.abstract = "Changed article1 lead"
puts "Changed Article1. Before Save!"
a1.save!
puts "After Save Article1"
# The cache should have been expired!
#assert verify_cache_entries == UP_DIR, "Cache should have been expired"
puts "Before INDEX 2"
get :index
#assert_match(/Changed article1 lead/, @response.body)
# The cache should have been recreated
#assert verify_cache_entries != UP_DIR, "Cache entry should have been created"
end
def verify_cache_entries
entries = Dir.entries(FILE_STORE_PATH)
# The cache entry created
entries.sort.last.to_s
end
def enable_cache
# Remove and create store
FileUtils.rm_rf(File.dirname(FILE_STORE_PATH))
FileUtils.mkdir_p(FILE_STORE_PATH)
ActionController::Base.perform_caching = true
#Backup
@page_cache_directory = ActionController::Base.page_cache_directory
@cache_store = ActionController::Base.cache_store
#Setup
ActionController::Base.page_cache_directory = FILE_STORE_PATH
ActionController::Base.cache_store = :file_store, FILE_STORE_PATH
end
def disable_cache
# Remove cache store
FileUtils.rm_rf(File.dirname(FILE_STORE_PATH))
#Restore
ActionController::Base.perform_caching = false
ActionController::Base.page_cache_directory = @page_cache_directory
ActionController::Base.cache_store = @cache_store
end
end