cirandas.net

ref: master

test/unit/blog_helper_test.rb


  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
require_relative "../test_helper"

class BlogHelperTest < ActionView::TestCase

  include BlogHelper
  include ContentViewerHelper
  include ActionView::Helpers::AssetTagHelper
  include ApplicationHelper

  helper 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 blog posts with identifiers and classes' do
    blog.children << older_post = create(TextArticle, :name => 'First post',
                     :profile => profile, :parent => blog, :published => true)
    blog.children << some_post = create(TextArticle, :name => 'Some post',
                     :profile => profile, :parent => blog, :published => true)
    blog.children << hidden_post = create(TextArticle, :name => 'Hidden post',
                     :profile => profile, :parent => blog, :published => false)
    blog.children << newer_post = create(TextArticle, :name => 'Last post',
                     :profile => profile, :parent => blog, :published => true)

    html = Nokogiri::HTML list_posts(blog.posts).html_safe

    assert_select html, "div#post-#{newer_post.id}.blog-post.position-1.first.odd-post" +
                        " > div.odd-post-inner.blog-post-inner > .title", 'Last post'
    assert_select html, "div#post-#{hidden_post.id}.blog-post.position-2.even-post" +
                        " > div.even-post-inner.blog-post-inner > .title", 'Hidden post'
    assert_select html, "div#post-#{hidden_post.id}.blog-post.position-2.even-post" +
                        " > div.even-post-inner.blog-post-inner > .not-published", true
    assert_select html, "div#post-#{some_post.id}.blog-post.position-3.odd-post" +
                        " > div.odd-post-inner.blog-post-inner > .title", 'Some post'
    assert_select html, "div#post-#{older_post.id}.blog-post.position-4.last.even-post" +
                        " > div.even-post-inner.blog-post-inner > .title", 'First post'
  end


  should 'display post' do
    blog.children << article = create(TextArticle, :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 = create(UploadedFile, :uploaded_data => fixture_file_upload('/files/test.txt', 'text/plain'), :profile => profile, :published => true, :parent => blog)
    result = display_post(file)

    assert_tag_in_string result, :tag => 'a', :content => _('Download')
  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
  include NoosferoTestHelper

end