From 336f3209ae1c9b975aada4ab3dd676287709f4b8 Mon Sep 17 00:00:00 2001 From: Carlos Purificacao Date: Wed, 24 Feb 2016 15:58:09 -0300 Subject: [PATCH] Testing environment caching timestamp --- app/helpers/application_helper.rb | 2 +- test/functional/block_cache_test.rb | 227 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 228 insertions(+), 1 deletion(-) create mode 100644 test/functional/block_cache_test.rb diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 4789429..9c3171f 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1241,7 +1241,7 @@ module ApplicationHelper end def cache_timeout(key, timeout, &block) - cache(key, { :expires_in => timeout }, &block) + cache(key, { :expires_in => timeout, :skip_digest => true }, &block) end def is_cache_expired?(key) diff --git a/test/functional/block_cache_test.rb b/test/functional/block_cache_test.rb new file mode 100644 index 0000000..80fa9f1 --- /dev/null +++ b/test/functional/block_cache_test.rb @@ -0,0 +1,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 => '

Wasserstoffbombe

') + fast_create(TinyMceArticle, :name => "A Post", :profile_id => @person.id, :parent_id => blog.id, :body => '

Lorem ipsum dolor sit amet

Second paragraph

') + 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 \ No newline at end of file -- libgit2 0.21.2