Commit 4a0c41a88d0e51150cf922872c79e1e8d3166ead

Authored by Victor Costa
1 parent 4d6d3322
Exists in master

comment_paragraph: fix tests

lib/comment_paragraph_plugin.rb
... ... @@ -41,7 +41,7 @@ class CommentParagraphPlugin < Noosfero::Plugin
41 41 end
42 42  
43 43 def self.activation_mode_default_setting
44   - 'auto'
  44 + 'manual'
45 45 end
46 46  
47 47 end
... ...
lib/comment_paragraph_plugin/macros/allow_comment.rb
... ... @@ -12,7 +12,7 @@ class CommentParagraphPlugin::AllowComment < Noosfero::Plugin::Macro
12 12 count = article.paragraph_comments.without_spam.in_paragraph(paragraph_uuid).count
13 13  
14 14 proc {
15   - if controller.kind_of?(ContentViewerController)
  15 + if controller.kind_of?(ContentViewerController) && article.comment_paragraph_plugin_activated?
16 16 render :partial => 'comment_paragraph_plugin_profile/comment_paragraph',
17 17 :locals => {:paragraph_uuid => paragraph_uuid, :article_id => article.id, :inner_html => inner_html, :count => count, :profile_identifier => article.profile.identifier }
18 18 else
... ...
lib/ext/article.rb
... ... @@ -6,25 +6,26 @@ class Article
6 6  
7 7 before_save :comment_paragraph_plugin_parse_html
8 8  
  9 + before_create :comment_paragraph_plugin_set_initial_value
  10 +
9 11 settings_items :comment_paragraph_plugin_activate, :type => :boolean, :default => false
10 12  
11 13 def comment_paragraph_plugin_enabled?
12 14 environment.plugin_enabled?(CommentParagraphPlugin) && self.kind_of?(TextArticle)
13 15 end
14 16  
15   - protected
16   -
17   - def comment_paragraph_plugin_activate?
18   - comment_paragraph_plugin_enabled? && comment_paragraph_plugin_settings.activation_mode == 'auto'
  17 + def comment_paragraph_plugin_activated?
  18 + comment_paragraph_plugin_activate && comment_paragraph_plugin_enabled?
19 19 end
20 20  
  21 + protected
  22 +
21 23 def comment_paragraph_plugin_parse_html
22   - comment_paragraph_plugin_activate = comment_paragraph_plugin_activate?
23   - return unless comment_paragraph_plugin_activate
  24 + return unless comment_paragraph_plugin_activated?
24 25  
25   - if body && body_changed?
  26 + if body && (body_changed? || setting_changed?(:comment_paragraph_plugin_activate))
26 27 parsed_paragraphs = []
27   - updated = body_change[1]
  28 + updated = body_changed? ? body_change[1] : body
28 29 doc = Hpricot(updated)
29 30 doc.search("/*").each do |paragraph|
30 31 if paragraph.to_html =~ /^<div(.*)paragraph_comment(.*)$/ || paragraph.to_html =~ /^<p>\W<\/p>$/
... ... @@ -41,6 +42,12 @@ class Article
41 42 end
42 43 end
43 44  
  45 + def comment_paragraph_plugin_set_initial_value
  46 + self.comment_paragraph_plugin_activate = comment_paragraph_plugin_enabled? &&
  47 + comment_paragraph_plugin_settings.activation_mode == 'auto'
  48 + true
  49 + end
  50 +
44 51 def comment_paragraph_plugin_settings
45 52 @comment_paragraph_plugin_settings ||= Noosfero::Plugin::Settings.new(environment, CommentParagraphPlugin)
46 53 end
... ...
test/functional/comment_paragraph_plugin_admin_controller_test.rb
... ... @@ -22,10 +22,10 @@ class CommentParagraphPluginAdminControllerTest &lt; ActionController::TestCase
22 22 end
23 23  
24 24 should 'update comment paragraph plugin settings' do
25   - assert_not_equal 'manual', plugin_settings.get_setting(:activation_mode)
26   - post :index, :settings => { :activation_mode => 'manual' }
  25 + assert_not_equal 'auto', plugin_settings.get_setting(:activation_mode)
  26 + post :index, :settings => { :activation_mode => 'auto' }
27 27 environment.reload
28   - assert_equal 'manual', plugin_settings.get_setting(:activation_mode)
  28 + assert_equal 'auto', plugin_settings.get_setting(:activation_mode)
29 29 end
30 30  
31 31 should 'get article types previously selected' do
... ...
test/functional/comment_paragraph_plugin_public_controller_test.rb
... ... @@ -9,8 +9,7 @@ class CommentParagraphPluginPublicControllerTest &lt; ActionController::TestCase
9 9  
10 10 def setup
11 11 @profile = create_user('testuser').person
12   - @article = profile.articles.build(:name => 'test')
13   - @article.save!
  12 + @article = profile.articles.create!(:name => 'test')
14 13 end
15 14 attr_reader :article, :profile
16 15  
... ...
test/functional/content_viewer_controller_test.rb
... ... @@ -10,10 +10,12 @@ end
10 10 class ContentViewerControllerTest < ActionController::TestCase
11 11  
12 12 def setup
13   - @profile = fast_create(Community)
14   - @page = fast_create(Article, :profile_id => @profile.id, :body => "<div class=\"macro\" data-macro-paragraph_uuid=\"0\" data-macro='comment_paragraph_plugin/allow_comment' ></div>")
15 13 @environment = Environment.default
16 14 @environment.enable_plugin(CommentParagraphPlugin)
  15 + @profile = fast_create(Community)
  16 + @page = fast_create(TextArticle, :profile_id => @profile.id, :body => "<p>inner text</p>")
  17 + @page.comment_paragraph_plugin_activate = true
  18 + @page.save!
17 19 end
18 20  
19 21 attr_reader :page
... ...
test/unit/allow_comment_test.rb
... ... @@ -4,34 +4,43 @@ class AllowCommentTest &lt; ActiveSupport::TestCase
4 4  
5 5 def setup
6 6 @macro = CommentParagraphPlugin::AllowComment.new
  7 + @environment = Environment.default
  8 + @environment.enable_plugin(CommentParagraphPlugin)
  9 +
  10 + @profile = fast_create(Community)
  11 +
  12 + @article = fast_create(TextArticle, :profile_id => profile.id, :body => 'inner')
  13 + @article.comment_paragraph_plugin_activate = true
  14 + @article.save!
  15 +
  16 + @comment = fast_create(Comment, :paragraph_uuid => 1, :source_id => article.id)
  17 + @controller = mock
7 18 end
8 19  
9   - attr_reader :macro
  20 + attr_reader :macro, :profile, :article, :controller, :comment, :environment
10 21  
11 22 should 'have a configuration' do
12 23 assert CommentParagraphPlugin::AllowComment.configuration
13 24 end
14 25  
15 26 should 'parse contents to include comment paragraph view' do
16   - profile = fast_create(Community)
17   - article = fast_create(Article, :profile_id => profile.id)
18   - comment = fast_create(Comment, :paragraph_uuid => 1, :source_id => article.id)
19   - inner_html = 'inner'
20   - content = macro.parse({:paragraph_uuid => comment.paragraph_uuid}, inner_html, article)
21   - expects(:controller).returns(ContentViewerController.new)
22   -
23   - expects(:render).with({:partial => 'comment_paragraph_plugin_profile/comment_paragraph', :locals => {:paragraph_uuid => comment.paragraph_uuid, :article_id => article.id, :inner_html => inner_html, :count => 1, :profile_identifier => profile.identifier} })
  27 + content = macro.parse({:paragraph_uuid => comment.paragraph_uuid}, article.body, article)
  28 + controller.expects(:kind_of?).with(ContentViewerController).returns(true)
  29 +
  30 + expects(:render).with({:partial => 'comment_paragraph_plugin_profile/comment_paragraph', :locals => {:paragraph_uuid => comment.paragraph_uuid, :article_id => article.id, :inner_html => article.body, :count => 1, :profile_identifier => profile.identifier} })
24 31 instance_eval(&content)
25 32 end
26 33  
27 34 should 'not parse contents outside content viewer controller' do
28   - profile = fast_create(Community)
29   - article = fast_create(Article, :profile_id => profile.id)
30   - comment = fast_create(Comment, :paragraph_uuid => 1, :source_id => article.id)
31   - inner_html = 'inner'
32   - content = macro.parse({:paragraph_uuid => comment.paragraph_uuid}, inner_html, article)
33   - expects(:controller).returns(HomeController.new)
  35 + content = macro.parse({:paragraph_uuid => comment.paragraph_uuid}, article.body, article)
  36 + controller.expects(:kind_of?).with(ContentViewerController).returns(false)
  37 + assert_equal 'inner', instance_eval(&content)
  38 + end
34 39  
  40 + should 'not parse contents if comment_paragraph is not activated' do
  41 + article.expects(:comment_paragraph_plugin_activated?).returns(false)
  42 + content = macro.parse({:paragraph_uuid => comment.paragraph_uuid}, article.body, article)
  43 + controller.expects(:kind_of?).with(ContentViewerController).returns(true)
35 44 assert_equal 'inner', instance_eval(&content)
36 45 end
37 46  
... ...
test/unit/article_test.rb
... ... @@ -4,13 +4,13 @@ require &#39;benchmark&#39;
4 4 class ArticleTest < ActiveSupport::TestCase
5 5  
6 6 def setup
7   - profile = fast_create(Community)
8   - @article = fast_create(Article, :profile_id => profile.id)
  7 + @profile = fast_create(Community)
  8 + @article = fast_create(TextArticle, :profile_id => profile.id)
9 9 @environment = Environment.default
10 10 @environment.enable_plugin(CommentParagraphPlugin)
11 11 end
12 12  
13   - attr_reader :article, :environment
  13 + attr_reader :article, :environment, :profile
14 14  
15 15 should 'return paragraph comments from article' do
16 16 comment1 = fast_create(Comment, :paragraph_uuid => 1, :source_id => article.id)
... ... @@ -34,9 +34,67 @@ class ArticleTest &lt; ActiveSupport::TestCase
34 34  
35 35 should 'parse html if the plugin is not enabled' do
36 36 article.body = "<p>paragraph 1</p><p>paragraph 2</p>"
37   - article.expects(:comment_paragraph_plugin_enabled?).returns(true)
  37 + article.comment_paragraph_plugin_activate = true
38 38 article.save!
39   - assert_match /data-macro='comment_paragraph_plugin\/allow_comment'/, article.body
  39 + assert_match /data-macro="comment_paragraph_plugin\/allow_comment"/, article.body
  40 + end
  41 +
  42 + should 'do not remove macro div when disable comment paragraph' do
  43 + article.body = "<p>paragraph 1</p><p>paragraph 2</p>"
  44 + article.comment_paragraph_plugin_activate = true
  45 + article.save!
  46 + assert_match /data-macro="comment_paragraph_plugin\/allow_comment"/, article.body
  47 + article.comment_paragraph_plugin_activate = false
  48 + article.save!
  49 + assert_match /data-macro="comment_paragraph_plugin\/allow_comment"/, article.body
  50 + end
  51 +
  52 + should 'parse html when activate comment paragraph' do
  53 + article.body = "<p>paragraph 1</p><p>paragraph 2</p>"
  54 + article.comment_paragraph_plugin_activate = false
  55 + article.save!
  56 + assert_equal "<p>paragraph 1</p><p>paragraph 2</p>", article.body
  57 + article.comment_paragraph_plugin_activate = true
  58 + article.save!
  59 + assert_match /data-macro="comment_paragraph_plugin\/allow_comment"/, article.body
  60 + end
  61 +
  62 + should 'be enabled if plugin is enabled and article is a kind of TextArticle' do
  63 + assert article.comment_paragraph_plugin_enabled?
  64 + end
  65 +
  66 + should 'not be enabled if plugin is not enabled' do
  67 + environment.disable_plugin(CommentParagraphPlugin)
  68 + assert !article.comment_paragraph_plugin_enabled?
  69 + end
  70 +
  71 + should 'not be enabled if article if not a kind of TextArticle' do
  72 + article = fast_create(Article, :profile_id => profile.id)
  73 + assert !article.comment_paragraph_plugin_enabled?
  74 + end
  75 +
  76 + should 'not be activated by default' do
  77 + article = fast_create(TextArticle, :profile_id => profile.id)
  78 + assert !article.comment_paragraph_plugin_activated?
  79 + end
  80 +
  81 + should 'be activated by default if it is enabled and activation mode is auto' do
  82 + settings = Noosfero::Plugin::Settings.new(environment, CommentParagraphPlugin)
  83 + settings.activation_mode = 'auto'
  84 + settings.save!
  85 + article = TextArticle.create!(:profile => profile, :name => 'title')
  86 + assert article.comment_paragraph_plugin_activated?
  87 + end
  88 +
  89 + should 'be activated when forced' do
  90 + article.comment_paragraph_plugin_activate = true
  91 + assert article.comment_paragraph_plugin_activated?
  92 + end
  93 +
  94 + should 'not be activated if plugin is not enabled' do
  95 + article.comment_paragraph_plugin_activate = true
  96 + environment.disable_plugin(CommentParagraphPlugin)
  97 + assert !article.comment_paragraph_plugin_enabled?
40 98 end
41 99  
42 100 end
... ...