blog_helper_test.rb
5.21 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
require File.dirname(__FILE__) + '/../test_helper'
class BlogHelperTest < Test::Unit::TestCase
  include BlogHelper
  include ContentViewerHelper
  include ActionView::Helpers::AssetTagHelper
  include ApplicationHelper
  def setup
    stubs(:show_date).returns('')
    @environment = Environment.default
    @profile = create_user('blog_helper_test').person
    @blog = fast_create(Blog, :profile_id => profile.id, :name => 'Blog test')
  end
  attr :profile
  attr :blog
  def _(s); s; end
  def h(s); s; end
  should 'list published posts with class blog-post' do
    blog.children << published_post = TextileArticle.create!(:name => 'Post', :profile => profile, :parent => blog, :published => true)
    expects(:display_post).with(anything, anything).returns('POST')
    expects(:content_tag).with('div', "POST<br style=\"clear:both\"/>", :class => 'blog-post position-1 first last odd-post-inner', :id => "post-#{published_post.id}").returns('POST')
    expects(:content_tag).with('div', 'POST', {:class => 'odd-post'}).returns('RESULT')
    assert_equal 'RESULT', list_posts(blog.posts)
  end
  should 'list even/odd posts with a different class' do
    blog.children << older_post = TextileArticle.create!(:name => 'First post', :profile => profile, :parent => blog, :published => true)
    blog.children << newer_post = TextileArticle.create!(:name => 'Second post', :profile => profile, :parent => blog, :published => true)
    expects(:display_post).with(anything, anything).returns('POST').times(2)
    expects(:content_tag).with('div', "POST<br style=\"clear:both\"/>", :class => 'blog-post position-1 first odd-post-inner', :id => "post-#{newer_post.id}").returns('POST 1')
    expects(:content_tag).with('div', "POST 1", :class => 'odd-post').returns('ODD-POST')
    expects(:content_tag).with('div', "POST<br style=\"clear:both\"/>", :class => 'blog-post position-2 last even-post-inner', :id => "post-#{older_post.id}").returns('POST 2')
    expects(:content_tag).with('div', "POST 2", :class => 'even-post').returns('EVEN-POST')
    assert_equal "ODD-POST\n<hr class='sep-posts'/>\nEVEN-POST", list_posts(blog.posts)
  end
  should 'display post' do
    blog.children << article = TextileArticle.create!(:name => 'Second post', :profile => profile, :parent => blog, :published => true)
    expects(:article_title).with(article, anything).returns('TITLE')
    expects(:content_tag).with('p', article.to_html).returns(' TO_HTML')
    self.stubs(:params).returns({:npage => nil})
    assert_equal 'TITLE TO_HTML', display_post(article)
  end
  should 'display empty post if body is nil' do
    blog.children << article = fast_create(Article, :profile_id => profile.id, :parent_id => blog.id, :body => nil)
    expects(:article_title).with(article, anything).returns('TITLE')
    expects(:content_tag).with('p', '').returns('')
    self.stubs(:params).returns({:npage => nil})
    assert_equal 'TITLE', display_post(article)
  end
  should 'display full post by default' do
    blog.children << article = fast_create(Article, :profile_id => profile.id, :parent_id => blog.id, :body => nil)
    expects(:article_title).with(article, anything).returns('')
    expects(:display_full_format).with(article).returns('FULL POST')
    assert_equal 'FULL POST', display_post(article)
  end
  should 'no_comments is false if blog displays full post' do
    blog.children << article = fast_create(Article, :profile_id => profile.id, :parent_id => blog.id, :body => nil)
    expects(:article_title).with(article, :no_comments => false).returns('')
    expects(:display_full_format).with(article).returns('FULL POST')
    assert_equal 'FULL POST', display_post(article, 'full')
  end
  should 'no_comments is true if blog displays short post' do
    blog.update_attribute(:visualization_format, 'short')
    blog.children << article = fast_create(Article, :profile_id => profile.id, :parent_id => blog.id, :body => nil)
    expects(:article_title).with(article, :no_comments => true).returns('')
    expects(:display_short_format).with(article).returns('SHORT POST')
    assert_equal 'SHORT POST', display_post(article, 'short')
  end
  should 'display link to file if post is an uploaded_file' do
    file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/test.txt', 'text/plain'), :profile => profile, :published => true, :parent => blog)
    expects(:article_to_html).with(file).returns('TO HTML')
    result = display_post(file)
    assert_tag_in_string result, :content => /TO HTML/
  end
  should 'display image if post is an image' do
    file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => profile, :published => true, :parent => blog)
    self.stubs(:params).returns({:npage => nil})
    display_filename = file.public_filename(:display)
    result = display_post(file)
    assert_match /rails.png/, result
    assert_tag_in_string result, :tag => 'img', :attributes => { :src => /#{display_filename}/ }
  end
  protected
  def will_paginate(arg1, arg2)
  end
  def link_to(content, url)
    content
  end
  def tag(tag, args = {})
    attrs = args.map{|k,v| "#{k}='#{v}'"}.join(' ')
    "<#{tag} #{attrs} />"
  end
  def content_tag(tag, content, options = {})
    "<#{tag}>#{content}</#{tag}>"
  end
end