From 265fa7193873ffce78ae8f8679bc31783cfb6c39 Mon Sep 17 00:00:00 2001 From: Joenio Costa Date: Wed, 23 Feb 2011 12:55:12 -0300 Subject: [PATCH] Not try to load a removed blog on BlogArchivesBlock --- app/models/blog_archives_block.rb | 2 +- test/factories.rb | 2 ++ test/unit/blog_archives_block_test.rb | 23 +++++++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/app/models/blog_archives_block.rb b/app/models/blog_archives_block.rb index 6d370b9..7d04790 100644 --- a/app/models/blog_archives_block.rb +++ b/app/models/blog_archives_block.rb @@ -17,7 +17,7 @@ class BlogArchivesBlock < Block settings_items :blog_id, Integer def blog - blog_id ? owner.blogs.find(blog_id) : owner.blog + blog_id && owner.blogs.exists?(blog_id) ? owner.blogs.find(blog_id) : owner.blog end def content diff --git a/test/factories.rb b/test/factories.rb index 78c4922..1b8d04e 100644 --- a/test/factories.rb +++ b/test/factories.rb @@ -310,6 +310,8 @@ module Noosfero::Factory { } end + alias :defaults_for_blog_archives_block :defaults_for_block + ############################################### # Task ############################################### diff --git a/test/unit/blog_archives_block_test.rb b/test/unit/blog_archives_block_test.rb index a15656c..0cbf96c 100644 --- a/test/unit/blog_archives_block_test.rb +++ b/test/unit/blog_archives_block_test.rb @@ -134,4 +134,27 @@ class BlogArchivesBlockTest < ActiveSupport::TestCase assert_tag_in_string block.content, :tag => 'a', :content => 'January (2)', :attributes => {:href => /^http:\/\/.*\/flatline\/blog-one\?month=01&year=2008$/ } end + should 'not try to load a removed blog' do + block = fast_create(BlogArchivesBlock) + block.blog_id = profile.blog.id + block.save! + block.stubs(:owner).returns(profile) + profile.blog.destroy + assert_nothing_raised do + assert_nil block.blog + end + end + + should 'load next blog if configured blog was removed' do + other_blog = fast_create(Blog, :profile_id => profile.id) + block = fast_create(BlogArchivesBlock) + block.blog_id = profile.blog.id + block.save! + block.stubs(:owner).returns(profile) + profile.blog.destroy + assert_nothing_raised do + assert_equal other_blog, block.blog + end + end + end -- libgit2 0.21.2