cirandas.net

ref: master

plugins/comment_paragraph/test/unit/article_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
require_relative '../test_helper'
require 'benchmark'

class ArticleTest < ActiveSupport::TestCase

  def setup
    @profile = fast_create(Community)
    @article = fast_create(TextArticle, :profile_id => profile.id)
    @environment = Environment.default
    @environment.enable_plugin(CommentParagraphPlugin)
  end

  attr_reader :article, :environment, :profile

  should 'return paragraph comments from article' do
    comment1 = fast_create(Comment, :paragraph_uuid => 1, :source_id => article.id)
    comment2 = fast_create(Comment, :paragraph_uuid => nil, :source_id => article.id)
    assert_equal [comment1], article.paragraph_comments
  end

  should 'allow save if comment paragraph macro is not removed for paragraph with comments' do
    article.body = "<div class=\"macro\" data-macro-paragraph_uuid=0></div>"
    comment1 = fast_create(Comment, :paragraph_uuid => 0, :source_id => article.id)
    assert article.save
  end

  should 'not parse html if the plugin is not enabled' do
    article.body = "<p>paragraph 1</p><p>paragraph 2</p>"
    environment.disable_plugin(CommentParagraphPlugin)
    assert !environment.plugin_enabled?(CommentParagraphPlugin)
    article.save!
    assert_equal "<p>paragraph 1</p><p>paragraph 2</p>", article.body
  end

  should 'parse html if the plugin is not enabled' do
    article.body = "<p>paragraph 1</p><div>div 1</div><span>span 1</span>"
    article.comment_paragraph_plugin_activate = true
    article.save!
    assert_mark_paragraph article.body, 'p', 'paragraph 1'
    assert_mark_paragraph article.body, 'div', 'div 1'
    assert_mark_paragraph article.body, 'span', 'span 1'
  end

  should 'parse html li when activate comment paragraph' do
    article.body = '<ul><li class="custom_class">item1</li><li>item2</li></ul>'
    article.comment_paragraph_plugin_activate = true
    article.save!
    assert_mark_paragraph article.body, 'li', 'item1'
    assert_mark_paragraph article.body, 'li', 'item2'
  end

  should 'parse inner html li when activate comment paragraph' do
    article.body = '<div><ul><li class="custom_class">item1</li><li>item2</li></ul><div>'
    article.comment_paragraph_plugin_activate = true
    article.save!
    assert_mark_paragraph article.body, 'li', 'item1'
    assert_mark_paragraph article.body, 'li', 'item2'
  end

  should 'do not remove macro div when disable comment paragraph' do
    article.body = "<p>paragraph 1</p>"
    article.comment_paragraph_plugin_activate = true
    article.save!
    assert_mark_paragraph article.body, 'p', 'paragraph 1'
    article.comment_paragraph_plugin_activate = false
    article.save!
    assert_mark_paragraph article.body, 'p', 'paragraph 1'
  end

  should 'parse html when activate comment paragraph' do
    article.body = "<p>paragraph 1</p><p>paragraph 2</p>"
    article.comment_paragraph_plugin_activate = false
    article.save!
    assert_equal "<p>paragraph 1</p><p>paragraph 2</p>", article.body
    article.comment_paragraph_plugin_activate = true
    article.save!

    assert_mark_paragraph article.body, 'p', 'paragraph 1'
    assert_mark_paragraph article.body, 'p', 'paragraph 2'
  end

  should 'parse html when add new paragraph' do
    article.body = "<p>paragraph 1</p>"
    article.comment_paragraph_plugin_activate = true
    article.save!
    assert_mark_paragraph article.body, 'p', 'paragraph 1'

    article.body += "<p>paragraph 2</p>"
    article.save!
    assert_mark_paragraph article.body, 'p', 'paragraph 1'
    assert_mark_paragraph article.body, 'p', 'paragraph 2'
  end

  should 'keep already marked paragraph attributes when add new paragraph' do
    article.body = "<p>paragraph 1</p>"
    article.comment_paragraph_plugin_activate = true
    article.save!
    assert_mark_paragraph article.body, 'p', 'paragraph 1'
    uuid = Nokogiri::HTML(article.body).at('p span.paragraph_comment')['data-macro-paragraph_uuid']

    article.body += "<p>paragraph 2</p>"
    article.save!
    assert_mark_paragraph article.body, 'p', 'paragraph 1'
    new_uuid = Nokogiri::HTML(article.body).at('p span.paragraph_comment')['data-macro-paragraph_uuid']
    assert_equal uuid, new_uuid
  end

  should 'not parse empty element' do
    article.body = '<div></div>'
    article.comment_paragraph_plugin_activate = true
    article.save!
    assert_equal '<div></div>', article.body
  end

  should 'be enabled if plugin is enabled and article is a kind of TextArticle' do
    assert article.comment_paragraph_plugin_enabled?
  end

  should 'not be enabled if plugin is not enabled' do
    environment.disable_plugin(CommentParagraphPlugin)
    assert !article.comment_paragraph_plugin_enabled?
  end

  should 'not be enabled if article if not a kind of TextArticle' do
    article = fast_create(Article, :profile_id => profile.id)
    assert !article.comment_paragraph_plugin_enabled?
  end

  should 'not be activated by default' do
    article = fast_create(TextArticle, :profile_id => profile.id)
    assert !article.comment_paragraph_plugin_activated?
  end

  should 'be activated by default if it is enabled and activation mode is auto' do
    settings = Noosfero::Plugin::Settings.new(environment, CommentParagraphPlugin)
    settings.activation_mode = 'auto'
    settings.save!
    article = TextArticle.create!(:profile => profile, :name => 'title')
    assert article.comment_paragraph_plugin_activated?
  end

  should 'be activated when forced' do
    article.comment_paragraph_plugin_activate = true
    assert article.comment_paragraph_plugin_activated?
  end

  should 'not be activated if plugin is not enabled' do
    article.comment_paragraph_plugin_activate = true
    environment.disable_plugin(CommentParagraphPlugin)
    assert !article.comment_paragraph_plugin_enabled?
  end

  should 'append not_logged to cache key when user is not logged in' do
    assert_match /-not_logged-/, article.cache_key
  end

  should 'append logged_in to cache key when user is logged in' do
    assert_match /-logged_in-/, article.cache_key({}, fast_create(Person))
  end

  should 'return paragraph content passing paragraph uuid' do
    uuid = 0
    article.body = "<div class=\"macro\" data-macro-paragraph_uuid=#{uuid}>paragraph content</div>"
    assert_equal 'paragraph content', article.comment_paragraph_plugin_paragraph_content(uuid)
  end

  should 'return nil as paragraph content when paragraph uuid is not found' do
    uuid = 0
    article.body = "<div class=\"macro\" data-macro-paragraph_uuid=#{uuid}>paragraph content</div>"
    assert_equal nil, article.comment_paragraph_plugin_paragraph_content(1)
  end

  should 'be enabled if plugin is enabled and article is a kind of Discussion' do
    assert fast_create(CommentParagraphPlugin::Discussion, profile_id: profile.id).comment_paragraph_plugin_enabled?
  end
end