From d864f40e88215c54fd94c78d59ba24ca926a3eb0 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Tue, 27 Jan 2015 11:56:12 -0300 Subject: [PATCH] comment_paragraph: fix tests --- plugins/comment_paragraph/lib/comment_paragraph_plugin.rb | 2 +- plugins/comment_paragraph/lib/comment_paragraph_plugin/macros/allow_comment.rb | 2 +- plugins/comment_paragraph/lib/ext/article.rb | 23 +++++++++++++++-------- plugins/comment_paragraph/test/functional/comment_paragraph_plugin_admin_controller_test.rb | 6 +++--- plugins/comment_paragraph/test/functional/comment_paragraph_plugin_public_controller_test.rb | 3 +-- plugins/comment_paragraph/test/functional/content_viewer_controller_test.rb | 6 ++++-- plugins/comment_paragraph/test/unit/allow_comment_test.rb | 39 ++++++++++++++++++++++++--------------- plugins/comment_paragraph/test/unit/article_test.rb | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 8 files changed, 112 insertions(+), 37 deletions(-) diff --git a/plugins/comment_paragraph/lib/comment_paragraph_plugin.rb b/plugins/comment_paragraph/lib/comment_paragraph_plugin.rb index a4b86da..f23fc0d 100644 --- a/plugins/comment_paragraph/lib/comment_paragraph_plugin.rb +++ b/plugins/comment_paragraph/lib/comment_paragraph_plugin.rb @@ -41,7 +41,7 @@ class CommentParagraphPlugin < Noosfero::Plugin end def self.activation_mode_default_setting - 'auto' + 'manual' end end diff --git a/plugins/comment_paragraph/lib/comment_paragraph_plugin/macros/allow_comment.rb b/plugins/comment_paragraph/lib/comment_paragraph_plugin/macros/allow_comment.rb index 55c641e..4f88899 100644 --- a/plugins/comment_paragraph/lib/comment_paragraph_plugin/macros/allow_comment.rb +++ b/plugins/comment_paragraph/lib/comment_paragraph_plugin/macros/allow_comment.rb @@ -12,7 +12,7 @@ class CommentParagraphPlugin::AllowComment < Noosfero::Plugin::Macro count = article.paragraph_comments.without_spam.in_paragraph(paragraph_uuid).count proc { - if controller.kind_of?(ContentViewerController) + if controller.kind_of?(ContentViewerController) && article.comment_paragraph_plugin_activated? render :partial => 'comment_paragraph_plugin_profile/comment_paragraph', :locals => {:paragraph_uuid => paragraph_uuid, :article_id => article.id, :inner_html => inner_html, :count => count, :profile_identifier => article.profile.identifier } else diff --git a/plugins/comment_paragraph/lib/ext/article.rb b/plugins/comment_paragraph/lib/ext/article.rb index 128e943..82d894f 100644 --- a/plugins/comment_paragraph/lib/ext/article.rb +++ b/plugins/comment_paragraph/lib/ext/article.rb @@ -6,25 +6,26 @@ class Article before_save :comment_paragraph_plugin_parse_html + before_create :comment_paragraph_plugin_set_initial_value + settings_items :comment_paragraph_plugin_activate, :type => :boolean, :default => false def comment_paragraph_plugin_enabled? environment.plugin_enabled?(CommentParagraphPlugin) && self.kind_of?(TextArticle) end - protected - - def comment_paragraph_plugin_activate? - comment_paragraph_plugin_enabled? && comment_paragraph_plugin_settings.activation_mode == 'auto' + def comment_paragraph_plugin_activated? + comment_paragraph_plugin_activate && comment_paragraph_plugin_enabled? end + protected + def comment_paragraph_plugin_parse_html - comment_paragraph_plugin_activate = comment_paragraph_plugin_activate? - return unless comment_paragraph_plugin_activate + return unless comment_paragraph_plugin_activated? - if body && body_changed? + if body && (body_changed? || setting_changed?(:comment_paragraph_plugin_activate)) parsed_paragraphs = [] - updated = body_change[1] + updated = body_changed? ? body_change[1] : body doc = Hpricot(updated) doc.search("/*").each do |paragraph| if paragraph.to_html =~ /^\W<\/p>$/ @@ -41,6 +42,12 @@ class Article end end + def comment_paragraph_plugin_set_initial_value + self.comment_paragraph_plugin_activate = comment_paragraph_plugin_enabled? && + comment_paragraph_plugin_settings.activation_mode == 'auto' + true + end + def comment_paragraph_plugin_settings @comment_paragraph_plugin_settings ||= Noosfero::Plugin::Settings.new(environment, CommentParagraphPlugin) end diff --git a/plugins/comment_paragraph/test/functional/comment_paragraph_plugin_admin_controller_test.rb b/plugins/comment_paragraph/test/functional/comment_paragraph_plugin_admin_controller_test.rb index 1429d70..c94d97d 100644 --- a/plugins/comment_paragraph/test/functional/comment_paragraph_plugin_admin_controller_test.rb +++ b/plugins/comment_paragraph/test/functional/comment_paragraph_plugin_admin_controller_test.rb @@ -22,10 +22,10 @@ class CommentParagraphPluginAdminControllerTest < ActionController::TestCase end should 'update comment paragraph plugin settings' do - assert_not_equal 'manual', plugin_settings.get_setting(:activation_mode) - post :index, :settings => { :activation_mode => 'manual' } + assert_not_equal 'auto', plugin_settings.get_setting(:activation_mode) + post :index, :settings => { :activation_mode => 'auto' } environment.reload - assert_equal 'manual', plugin_settings.get_setting(:activation_mode) + assert_equal 'auto', plugin_settings.get_setting(:activation_mode) end should 'get article types previously selected' do diff --git a/plugins/comment_paragraph/test/functional/comment_paragraph_plugin_public_controller_test.rb b/plugins/comment_paragraph/test/functional/comment_paragraph_plugin_public_controller_test.rb index d612fb1..9884939 100644 --- a/plugins/comment_paragraph/test/functional/comment_paragraph_plugin_public_controller_test.rb +++ b/plugins/comment_paragraph/test/functional/comment_paragraph_plugin_public_controller_test.rb @@ -9,8 +9,7 @@ class CommentParagraphPluginPublicControllerTest < ActionController::TestCase def setup @profile = create_user('testuser').person - @article = profile.articles.build(:name => 'test') - @article.save! + @article = profile.articles.create!(:name => 'test') end attr_reader :article, :profile diff --git a/plugins/comment_paragraph/test/functional/content_viewer_controller_test.rb b/plugins/comment_paragraph/test/functional/content_viewer_controller_test.rb index e932348..e003716 100644 --- a/plugins/comment_paragraph/test/functional/content_viewer_controller_test.rb +++ b/plugins/comment_paragraph/test/functional/content_viewer_controller_test.rb @@ -10,10 +10,12 @@ end class ContentViewerControllerTest < ActionController::TestCase def setup - @profile = fast_create(Community) - @page = fast_create(Article, :profile_id => @profile.id, :body => "
") @environment = Environment.default @environment.enable_plugin(CommentParagraphPlugin) + @profile = fast_create(Community) + @page = fast_create(TextArticle, :profile_id => @profile.id, :body => "

inner text

") + @page.comment_paragraph_plugin_activate = true + @page.save! end attr_reader :page diff --git a/plugins/comment_paragraph/test/unit/allow_comment_test.rb b/plugins/comment_paragraph/test/unit/allow_comment_test.rb index e69fa77..ebbe109 100644 --- a/plugins/comment_paragraph/test/unit/allow_comment_test.rb +++ b/plugins/comment_paragraph/test/unit/allow_comment_test.rb @@ -4,34 +4,43 @@ class AllowCommentTest < ActiveSupport::TestCase def setup @macro = CommentParagraphPlugin::AllowComment.new + @environment = Environment.default + @environment.enable_plugin(CommentParagraphPlugin) + + @profile = fast_create(Community) + + @article = fast_create(TextArticle, :profile_id => profile.id, :body => 'inner') + @article.comment_paragraph_plugin_activate = true + @article.save! + + @comment = fast_create(Comment, :paragraph_uuid => 1, :source_id => article.id) + @controller = mock end - attr_reader :macro + attr_reader :macro, :profile, :article, :controller, :comment, :environment should 'have a configuration' do assert CommentParagraphPlugin::AllowComment.configuration end should 'parse contents to include comment paragraph view' do - profile = fast_create(Community) - article = fast_create(Article, :profile_id => profile.id) - comment = fast_create(Comment, :paragraph_uuid => 1, :source_id => article.id) - inner_html = 'inner' - content = macro.parse({:paragraph_uuid => comment.paragraph_uuid}, inner_html, article) - expects(:controller).returns(ContentViewerController.new) - - 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} }) + content = macro.parse({:paragraph_uuid => comment.paragraph_uuid}, article.body, article) + controller.expects(:kind_of?).with(ContentViewerController).returns(true) + + 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} }) instance_eval(&content) end should 'not parse contents outside content viewer controller' do - profile = fast_create(Community) - article = fast_create(Article, :profile_id => profile.id) - comment = fast_create(Comment, :paragraph_uuid => 1, :source_id => article.id) - inner_html = 'inner' - content = macro.parse({:paragraph_uuid => comment.paragraph_uuid}, inner_html, article) - expects(:controller).returns(HomeController.new) + content = macro.parse({:paragraph_uuid => comment.paragraph_uuid}, article.body, article) + controller.expects(:kind_of?).with(ContentViewerController).returns(false) + assert_equal 'inner', instance_eval(&content) + end + should 'not parse contents if comment_paragraph is not activated' do + article.expects(:comment_paragraph_plugin_activated?).returns(false) + content = macro.parse({:paragraph_uuid => comment.paragraph_uuid}, article.body, article) + controller.expects(:kind_of?).with(ContentViewerController).returns(true) assert_equal 'inner', instance_eval(&content) end diff --git a/plugins/comment_paragraph/test/unit/article_test.rb b/plugins/comment_paragraph/test/unit/article_test.rb index 7612373..40074cb 100644 --- a/plugins/comment_paragraph/test/unit/article_test.rb +++ b/plugins/comment_paragraph/test/unit/article_test.rb @@ -4,13 +4,13 @@ require 'benchmark' class ArticleTest < ActiveSupport::TestCase def setup - profile = fast_create(Community) - @article = fast_create(Article, :profile_id => profile.id) + @profile = fast_create(Community) + @article = fast_create(TextArticle, :profile_id => profile.id) @environment = Environment.default @environment.enable_plugin(CommentParagraphPlugin) end - attr_reader :article, :environment + attr_reader :article, :environment, :profile should 'return paragraph comments from article' do comment1 = fast_create(Comment, :paragraph_uuid => 1, :source_id => article.id) @@ -34,9 +34,67 @@ class ArticleTest < ActiveSupport::TestCase should 'parse html if the plugin is not enabled' do article.body = "

paragraph 1

paragraph 2

" - article.expects(:comment_paragraph_plugin_enabled?).returns(true) + article.comment_paragraph_plugin_activate = true article.save! - assert_match /data-macro='comment_paragraph_plugin\/allow_comment'/, article.body + assert_match /data-macro="comment_paragraph_plugin\/allow_comment"/, article.body + end + + should 'do not remove macro div when disable comment paragraph' do + article.body = "

paragraph 1

paragraph 2

" + article.comment_paragraph_plugin_activate = true + article.save! + assert_match /data-macro="comment_paragraph_plugin\/allow_comment"/, article.body + article.comment_paragraph_plugin_activate = false + article.save! + assert_match /data-macro="comment_paragraph_plugin\/allow_comment"/, article.body + end + + should 'parse html when activate comment paragraph' do + article.body = "

paragraph 1

paragraph 2

" + article.comment_paragraph_plugin_activate = false + article.save! + assert_equal "

paragraph 1

paragraph 2

", article.body + article.comment_paragraph_plugin_activate = true + article.save! + assert_match /data-macro="comment_paragraph_plugin\/allow_comment"/, 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 end -- libgit2 0.21.2