Commit 7c4f4efd7ee4cb93c49342cbe7695d8ba36c06b5

Authored by Rafael Reggiani Manzo
1 parent 880fbdca

Fix blog archives block unit test

By removing the content method, it is now necessary to turn the test
into a view one using the BoxesHelper method and properly stubbing other
helpers.

This was inspired by the same conversion made by commit
23cca52cdaea5cc3b46e4982f30ad3426891e2f2 on MyNetworkBlock test.
Showing 1 changed file with 111 additions and 61 deletions   Show diff stats
test/unit/blog_archives_block_test.rb
@@ -17,6 +17,69 @@ class BlogArchivesBlockTest < ActiveSupport::TestCase @@ -17,6 +17,69 @@ class BlogArchivesBlockTest < ActiveSupport::TestCase
17 assert l.editable? 17 assert l.editable?
18 end 18 end
19 19
  20 + should 'has field to configure blog' do
  21 + b = BlogArchivesBlock.new
  22 + assert b.respond_to?(:blog_id)
  23 + assert b.respond_to?(:blog_id=)
  24 + end
  25 +
  26 + should 'not try to load a removed blog' do
  27 + block = fast_create(BlogArchivesBlock)
  28 + block.blog_id = profile.blog.id
  29 + block.save!
  30 + block.stubs(:owner).returns(profile)
  31 + profile.blog.destroy
  32 + assert_nothing_raised do
  33 + assert_nil block.blog
  34 + end
  35 + end
  36 +
  37 + should 'load next blog if configured blog was removed' do
  38 + other_blog = fast_create(Blog, :profile_id => profile.id)
  39 + block = fast_create(BlogArchivesBlock)
  40 + block.blog_id = profile.blog.id
  41 + block.save!
  42 + block.stubs(:owner).returns(profile)
  43 + profile.blog.destroy
  44 + assert_nothing_raised do
  45 + assert_equal other_blog, block.blog
  46 + end
  47 + end
  48 +
  49 +#FIXME Performance issues with display_to. Must convert it to a scope.
  50 +# Checkout this page for further information: http://noosfero.org/Development/ActionItem2705
  51 +#
  52 +# should 'not count articles if the user can\'t see them' do
  53 +# person = create_user('testuser').person
  54 +# blog = fast_create(Blog, :profile_id => profile.id, :path => 'blog_path')
  55 +# block = fast_create(BlogArchivesBlock)
  56 +#
  57 +# feed = mock()
  58 +# feed.stubs(:url).returns(blog.url)
  59 +# blog.stubs(:feed).returns(feed)
  60 +# block.stubs(:blog).returns(blog)
  61 +# block.stubs(:owner).returns(profile)
  62 +#
  63 +# public_post = fast_create(TextileArticle, :profile_id => profile.id, :parent_id => blog.id, :published => true, :published_at => Time.mktime(2012, 'jan'))
  64 +# private_post = fast_create(TextileArticle, :profile_id => profile.id, :parent_id => blog.id, :published => false, :published_at => Time.mktime(2012, 'jan'))
  65 +#
  66 +# assert_match /January \(1\)/, block.content({:person => person})
  67 +# assert_match /January \(1\)/, block.content()
  68 +# assert_match /January \(2\)/, block.content({:person => profile})
  69 +# end
  70 +end
  71 +
  72 +require 'boxes_helper'
  73 +
  74 +class BlogArchivesBlockViewTest < ActionView::TestCase
  75 + include BoxesHelper
  76 +
  77 + def setup
  78 + @profile = create_user('flatline').person
  79 + @profile.articles << Blog.new(:name => 'Blog One', :profile => @profile)
  80 + end
  81 + attr_reader :profile
  82 +
20 should 'list amount posts by year' do 83 should 'list amount posts by year' do
21 date = DateTime.parse('2008-01-10') 84 date = DateTime.parse('2008-01-10')
22 blog = profile.blog 85 blog = profile.blog
@@ -26,7 +89,9 @@ class BlogArchivesBlockTest &lt; ActiveSupport::TestCase @@ -26,7 +89,9 @@ class BlogArchivesBlockTest &lt; ActiveSupport::TestCase
26 end 89 end
27 block = BlogArchivesBlock.new 90 block = BlogArchivesBlock.new
28 block.stubs(:owner).returns(profile) 91 block.stubs(:owner).returns(profile)
29 - assert_tag_in_string block.content, :tag => 'li', :content => '2008 (10)' 92 + ActionView::Base.any_instance.stubs(:block_title).returns("")
  93 + ActionView::Base.any_instance.stubs(:month_name).returns("")
  94 + assert_tag_in_string render_block_content(block), :tag => 'li', :content => '2008 (10)'
30 end 95 end
31 96
32 should 'list amount posts by month' do 97 should 'list amount posts by month' do
@@ -38,7 +103,18 @@ class BlogArchivesBlockTest &lt; ActiveSupport::TestCase @@ -38,7 +103,18 @@ class BlogArchivesBlockTest &lt; ActiveSupport::TestCase
38 end 103 end
39 block = BlogArchivesBlock.new 104 block = BlogArchivesBlock.new
40 block.stubs(:owner).returns(profile) 105 block.stubs(:owner).returns(profile)
41 - assert_tag_in_string block.content, :tag => 'a', :content => 'January (10)', :attributes => {:href => /^http:\/\/.*\/flatline\/blog-one\?month=1&year=2008$/ } 106 + ActionView::Base.any_instance.stubs(:block_title).returns("")
  107 + ActionView::Base.any_instance.stubs(:month_name).with(1).returns("January")
  108 + ActionView::Base.any_instance.stubs(:month_name).with(2).returns("February")
  109 + ActionView::Base.any_instance.stubs(:month_name).with(3).returns("March")
  110 + ActionView::Base.any_instance.stubs(:month_name).with(4).returns("April")
  111 + ActionView::Base.any_instance.stubs(:month_name).with(5).returns("May")
  112 + ActionView::Base.any_instance.stubs(:month_name).with(6).returns("June")
  113 + ActionView::Base.any_instance.stubs(:month_name).with(7).returns("July")
  114 + ActionView::Base.any_instance.stubs(:month_name).with(8).returns("August")
  115 + ActionView::Base.any_instance.stubs(:month_name).with(9).returns("September")
  116 + ActionView::Base.any_instance.stubs(:month_name).with(10).returns("Octuber")
  117 + assert_tag_in_string render_block_content(block), :tag => 'a', :content => 'January (10)', :attributes => {:href => /^http:\/\/.*\/flatline\/blog-one\?month=1&year=2008$/ }
42 end 118 end
43 119
44 should 'order list of amount posts' do 120 should 'order list of amount posts' do
@@ -49,7 +125,18 @@ class BlogArchivesBlockTest &lt; ActiveSupport::TestCase @@ -49,7 +125,18 @@ class BlogArchivesBlockTest &lt; ActiveSupport::TestCase
49 end 125 end
50 block = BlogArchivesBlock.new 126 block = BlogArchivesBlock.new
51 block.stubs(:owner).returns(profile) 127 block.stubs(:owner).returns(profile)
52 - assert_tag_in_string block.content, :tag => 'li', :content => 'January (1)', 128 + ActionView::Base.any_instance.stubs(:block_title).returns("")
  129 + ActionView::Base.any_instance.stubs(:month_name).with(1).returns("January")
  130 + ActionView::Base.any_instance.stubs(:month_name).with(2).returns("February")
  131 + ActionView::Base.any_instance.stubs(:month_name).with(3).returns("March")
  132 + ActionView::Base.any_instance.stubs(:month_name).with(4).returns("April")
  133 + ActionView::Base.any_instance.stubs(:month_name).with(5).returns("May")
  134 + ActionView::Base.any_instance.stubs(:month_name).with(6).returns("June")
  135 + ActionView::Base.any_instance.stubs(:month_name).with(7).returns("July")
  136 + ActionView::Base.any_instance.stubs(:month_name).with(8).returns("August")
  137 + ActionView::Base.any_instance.stubs(:month_name).with(9).returns("September")
  138 + ActionView::Base.any_instance.stubs(:month_name).with(10).returns("Octuber")
  139 + assert_tag_in_string render_block_content(block), :tag => 'li', :content => 'January (1)',
53 :sibling => {:tag => 'li', :content => 'February (1)', 140 :sibling => {:tag => 'li', :content => 'February (1)',
54 :sibling => {:tag => 'li', :content => 'March (1)', 141 :sibling => {:tag => 'li', :content => 'March (1)',
55 :sibling => {:tag => 'li', :content => 'April (1)', 142 :sibling => {:tag => 'li', :content => 'April (1)',
@@ -63,7 +150,9 @@ class BlogArchivesBlockTest &lt; ActiveSupport::TestCase @@ -63,7 +150,9 @@ class BlogArchivesBlockTest &lt; ActiveSupport::TestCase
63 end 150 end
64 block = BlogArchivesBlock.new 151 block = BlogArchivesBlock.new
65 block.stubs(:owner).returns(profile) 152 block.stubs(:owner).returns(profile)
66 - assert_match(/2009.*2008.*2007.*2006.*2005/m, block.content) 153 + ActionView::Base.any_instance.stubs(:block_title).returns("")
  154 + ActionView::Base.any_instance.stubs(:month_name).returns("")
  155 + assert_match(/2009.*2008.*2007.*2006.*2005/m, render_block_content(block))
67 end 156 end
68 157
69 should 'order months from later to former' do 158 should 'order months from later to former' do
@@ -73,20 +162,20 @@ class BlogArchivesBlockTest &lt; ActiveSupport::TestCase @@ -73,20 +162,20 @@ class BlogArchivesBlockTest &lt; ActiveSupport::TestCase
73 end 162 end
74 block = BlogArchivesBlock.new 163 block = BlogArchivesBlock.new
75 block.stubs(:owner).returns(profile) 164 block.stubs(:owner).returns(profile)
76 - assert_match(/.*March.*February.*January.*/m, block.content) 165 + ActionView::Base.any_instance.stubs(:block_title).returns("")
  166 + ActionView::Base.any_instance.stubs(:month_name).with(1).returns("January")
  167 + ActionView::Base.any_instance.stubs(:month_name).with(2).returns("February")
  168 + ActionView::Base.any_instance.stubs(:month_name).with(3).returns("March")
  169 + assert_match(/.*March.*February.*January.*/m, render_block_content(block))
77 end 170 end
78 171
79 should 'not display any content if has no blog' do 172 should 'not display any content if has no blog' do
80 profile.blogs.destroy_all 173 profile.blogs.destroy_all
81 block = BlogArchivesBlock.new 174 block = BlogArchivesBlock.new
82 block.stubs(:owner).returns(profile) 175 block.stubs(:owner).returns(profile)
83 - assert_nil block.content  
84 - end  
85 -  
86 - should 'has field to configure blog' do  
87 - b = BlogArchivesBlock.new  
88 - assert b.respond_to?(:blog_id)  
89 - assert b.respond_to?(:blog_id=) 176 + ActionView::Base.any_instance.stubs(:block_title).returns("")
  177 + ActionView::Base.any_instance.stubs(:month_name).returns("")
  178 + assert_empty render_block_content(block)
90 end 179 end
91 180
92 should 'show posts from first blog' do 181 should 'show posts from first blog' do
@@ -98,8 +187,10 @@ class BlogArchivesBlockTest &lt; ActiveSupport::TestCase @@ -98,8 +187,10 @@ class BlogArchivesBlockTest &lt; ActiveSupport::TestCase
98 end 187 end
99 block = BlogArchivesBlock.new 188 block = BlogArchivesBlock.new
100 block.stubs(:owner).returns(profile) 189 block.stubs(:owner).returns(profile)
101 - assert_match(/blog-one/m, block.content)  
102 - assert_no_match(/blog-two/m, block.content) 190 + ActionView::Base.any_instance.stubs(:block_title).returns("")
  191 + ActionView::Base.any_instance.stubs(:month_name).returns("")
  192 + assert_match(/blog-one/m, render_block_content(block))
  193 + assert_no_match(/blog-two/m, render_block_content(block))
103 end 194 end
104 195
105 should 'list amount native posts by year' do 196 should 'list amount native posts by year' do
@@ -115,7 +206,9 @@ class BlogArchivesBlockTest &lt; ActiveSupport::TestCase @@ -115,7 +206,9 @@ class BlogArchivesBlockTest &lt; ActiveSupport::TestCase
115 end 206 end
116 block = BlogArchivesBlock.new 207 block = BlogArchivesBlock.new
117 block.stubs(:owner).returns(profile) 208 block.stubs(:owner).returns(profile)
118 - assert_tag_in_string block.content, :tag => 'li', :content => '2008 (2)' 209 + ActionView::Base.any_instance.stubs(:block_title).returns("")
  210 + ActionView::Base.any_instance.stubs(:month_name).returns("")
  211 + assert_tag_in_string render_block_content(block), :tag => 'li', :content => '2008 (2)'
119 end 212 end
120 213
121 should 'list amount native posts by month' do 214 should 'list amount native posts by month' do
@@ -131,51 +224,8 @@ class BlogArchivesBlockTest &lt; ActiveSupport::TestCase @@ -131,51 +224,8 @@ class BlogArchivesBlockTest &lt; ActiveSupport::TestCase
131 end 224 end
132 block = BlogArchivesBlock.new 225 block = BlogArchivesBlock.new
133 block.stubs(:owner).returns(profile) 226 block.stubs(:owner).returns(profile)
134 - assert_tag_in_string block.content, :tag => 'a', :content => 'January (2)', :attributes => {:href => /^http:\/\/.*\/flatline\/blog-one\?month=1&year=2008$/ }  
135 - end  
136 -  
137 - should 'not try to load a removed blog' do  
138 - block = fast_create(BlogArchivesBlock)  
139 - block.blog_id = profile.blog.id  
140 - block.save!  
141 - block.stubs(:owner).returns(profile)  
142 - profile.blog.destroy  
143 - assert_nothing_raised do  
144 - assert_nil block.blog  
145 - end 227 + ActionView::Base.any_instance.stubs(:block_title).returns("")
  228 + ActionView::Base.any_instance.stubs(:month_name).with(1).returns("January")
  229 + assert_tag_in_string render_block_content(block), :tag => 'a', :content => 'January (2)', :attributes => {:href => /^http:\/\/.*\/flatline\/blog-one\?month=1&year=2008$/ }
146 end 230 end
147 -  
148 - should 'load next blog if configured blog was removed' do  
149 - other_blog = fast_create(Blog, :profile_id => profile.id)  
150 - block = fast_create(BlogArchivesBlock)  
151 - block.blog_id = profile.blog.id  
152 - block.save!  
153 - block.stubs(:owner).returns(profile)  
154 - profile.blog.destroy  
155 - assert_nothing_raised do  
156 - assert_equal other_blog, block.blog  
157 - end  
158 - end  
159 -  
160 -#FIXME Performance issues with display_to. Must convert it to a scope.  
161 -# Checkout this page for further information: http://noosfero.org/Development/ActionItem2705  
162 -#  
163 -# should 'not count articles if the user can\'t see them' do  
164 -# person = create_user('testuser').person  
165 -# blog = fast_create(Blog, :profile_id => profile.id, :path => 'blog_path')  
166 -# block = fast_create(BlogArchivesBlock)  
167 -#  
168 -# feed = mock()  
169 -# feed.stubs(:url).returns(blog.url)  
170 -# blog.stubs(:feed).returns(feed)  
171 -# block.stubs(:blog).returns(blog)  
172 -# block.stubs(:owner).returns(profile)  
173 -#  
174 -# public_post = fast_create(TextileArticle, :profile_id => profile.id, :parent_id => blog.id, :published => true, :published_at => Time.mktime(2012, 'jan'))  
175 -# private_post = fast_create(TextileArticle, :profile_id => profile.id, :parent_id => blog.id, :published => false, :published_at => Time.mktime(2012, 'jan'))  
176 -#  
177 -# assert_match /January \(1\)/, block.content({:person => person})  
178 -# assert_match /January \(1\)/, block.content()  
179 -# assert_match /January \(2\)/, block.content({:person => profile})  
180 -# end  
181 end 231 end