cirandas.net

ref: master

test/unit/comment_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
require_relative "../test_helper"

class CommentHelperTest < ActionView::TestCase

  include CommentHelper

  helper ApplicationHelper

  def setup
    @user = create_user('usertest').person
    @profile = @user
    self.stubs(:logged_in?).returns(true)
    self.stubs(:report_abuse).returns('<a href="#">link</a>')
    self.stubs(:expirable_comment_link).returns('<a href="#">link</a>')
    self.stubs(:url_for)
    @plugins = mock
    @plugins.stubs(:dispatch).returns([])
  end

  attr_reader :user, :profile

  should 'show menu if it has links for actions' do
    article = Article.new(:profile => profile)
    comment = build(Comment, :article => article)
    menu = comment_actions(comment)
    assert_match /class=\"comment-actions\"/, menu
  end

  should 'do not show menu if it has no actions' do
    comment = Comment.new
    self.stubs(:links_for_comment_actions).returns([])
    menu = comment_actions(comment)
    assert_no_match /class=\"comment-actions\"/, menu
  end

  should 'do not show menu if it has nil actions only' do
    comment = Comment.new
    self.stubs(:link_for_report_abuse).returns(nil)
    self.stubs(:link_for_spam).returns(nil)
    self.stubs(:link_for_edit).returns(nil)
    self.stubs(:link_for_remove).returns(nil)
    menu = comment_actions(comment)
    assert_no_match /class=\"comment-actions\"/, menu
  end

  should 'include actions of plugins in menu' do
    article = Article.new(:profile => profile)
    comment = build(Comment, :article => article)
    plugin_action = {:link => 'plugin_action'}
    @plugins.stubs(:dispatch).returns([plugin_action])
    links = links_for_comment_actions(comment)
    assert_includes links, plugin_action
  end

  should 'include lambda actions of plugins in menu' do
    article = Article.new(:profile => profile)
    comment = build(Comment, :article => article)
    plugin_action = proc{[{:link => 'plugin_action'}, {:link => 'plugin_action2'}]}
    @plugins.stubs(:dispatch).returns([plugin_action])
    links = links_for_comment_actions(comment)
    assert_includes links, {:link => 'plugin_action'}
    assert_includes links, {:link => 'plugin_action2'}
  end

  should 'return link for report abuse action when comment has a author' do
    comment = Comment.new
    comment.author = user
    link = link_for_report_abuse(comment)
    assert link
  end

  should 'do not return link for report abuse action when comment has no author' do
    comment = Comment.new
    link = link_for_report_abuse(comment)
    refute link
  end

  should 'return link for mark comment as spam' do
    comment = Comment.new
    comment.stubs(:can_be_marked_as_spam_by?).with(user).returns(true)
    link = link_for_spam(comment)
    assert_match /Mark as SPAM/, link[:link]
  end

  should 'not return link for mark comment as spam if user does not have the permissions' do
    comment = Comment.new
    comment.stubs(:can_be_marked_as_spam_by?).with(user).returns(false)
    link = link_for_spam(comment)
    assert_nil link
  end

  should 'return link for mark comment as not spam' do
    comment = Comment.new
    comment.spam = true
    comment.stubs(:can_be_marked_as_spam_by?).with(user).returns(true)
    link = link_for_spam(comment)
    assert_match /Mark as NOT SPAM/, link[:link]
  end

  should 'not return link for mark comment as not spam if user does not have the permissions' do
    comment = Comment.new
    comment.spam = true
    comment.stubs(:can_be_marked_as_spam_by?).with(user).returns(false)
    link = link_for_spam(comment)
    assert_nil link
  end

  should 'do not return link for edit comment' do
    comment = Comment.new
    comment.stubs(:can_be_updated_by?).with(user).returns(false)
    link = link_for_edit(comment)
    assert_nil link
  end

  should 'return link for edit comment' do
    comment = Comment.new
    comment.stubs(:can_be_updated_by?).with(user).returns(true)
    link = link_for_edit(comment)
    assert link
  end

  should 'do not return link for remove comment' do
    comment = Comment.new
    comment.stubs(:can_be_destroyed_by?).with(user).returns(false)
    link = link_for_remove(comment)
    assert_nil link
  end

  should 'return link for remove comment' do
    comment = Comment.new
    comment.stubs(:can_be_destroyed_by?).with(user).returns(true)
    link = link_for_remove(comment)
    assert link
  end

  should 'include actions of plugins in action bar' do
    comment = Comment.new
    plugin_action = {:link => 'plugin_action', :action_bar => true}
    @plugins.stubs(:dispatch).returns([plugin_action])
    html = comment_actions(comment)
    assert_match /plugin_action/, Nokogiri::HTML.fragment(html).css('.comments-action-bar').to_html
  end

end