');
+ hasTag = true;
+ }
+ });
+
+ if(!hasTag) {
+ tags = start.siblings().add(start);
+ tags = tags.slice(tags.index(start), tags.index(end)>=0?tags.index(end)+1:tags.index(start)+1);
+ tags.wrapAll('
');
+
+ contents = jQuery('#article_body_ifr').contents();
+ lastP = contents.find('p.article_comments_last_paragraph');
+ if(lastP.text().trim().length > 0) {
+ lastP.removeClass('article_comments_last_paragraph');
+ } else {
+ lastP.remove();
+ }
+ lastDiv = contents.find('div.article_comments').last();
+ if(lastDiv.next().length==0) {
+ lastDiv.after("
");
+ }
+ }
+}
+
diff --git a/plugins/comment_paragraph/public/comment_paragraph_macro.js b/plugins/comment_paragraph/public/comment_paragraph_macro.js
new file mode 100644
index 0000000..3814002
--- /dev/null
+++ b/plugins/comment_paragraph/public/comment_paragraph_macro.js
@@ -0,0 +1,39 @@
+var comment_paragraph_anchor;
+jQuery(document).ready(function($) {
+ var anchor = window.location.hash;
+ if(anchor.length==0) return;
+
+ var val = anchor.split('-'); //anchor format = #comment-\d+
+ if(val.length!=2 || val[0]!='#comment') return;
+ if($('div[data-macro=comment_paragraph_plugin/allow_comment]').length==0) return; //comment_paragraph_plugin/allow_comment div must exists
+ var comment_id = val[1];
+ if(!/^\d+$/.test(comment_id)) return; //test for integer
+
+ comment_paragraph_anchor = anchor;
+ var url = '/plugin/comment_paragraph/public/comment_paragraph/'+comment_id;
+ $.getJSON(url, function(data) {
+ if(data.paragraph_id!=null) {
+ var button = $('div.comment_paragraph_'+ data.paragraph_id + ' a');
+ button.click();
+ $.scrollTo(button);
+ }
+ });
+});
+
+function toggleParagraph(paragraph) {
+ var div = jQuery('div.comments_list_toggle_paragraph_'+paragraph);
+ var visible = div.is(':visible');
+ if(!visible)
+ jQuery('div.comment-paragraph-loading-'+paragraph).addClass('comment-button-loading');
+
+ div.toggle('fast');
+ return visible;
+}
+
+function loadCompleted(paragraph) {
+ jQuery('div.comment-paragraph-loading-'+paragraph).removeClass('comment-button-loading')
+ if(comment_paragraph_anchor) {
+ jQuery.scrollTo(jQuery(comment_paragraph_anchor));
+ comment_paragraph_anchor = null;
+ }
+}
diff --git a/plugins/comment_paragraph/public/images/comments.gif b/plugins/comment_paragraph/public/images/comments.gif
new file mode 100644
index 0000000..e3982a9
Binary files /dev/null and b/plugins/comment_paragraph/public/images/comments.gif differ
diff --git a/plugins/comment_paragraph/public/style.css b/plugins/comment_paragraph/public/style.css
new file mode 100644
index 0000000..d8f9e6f
--- /dev/null
+++ b/plugins/comment_paragraph/public/style.css
@@ -0,0 +1,7 @@
+div.article-comments-list-more{
+ width: 100%;
+ height: 30px;
+ text-align: center;
+ font-size: 20px;
+ margin-bottom: 5px;
+}
diff --git a/plugins/comment_paragraph/test/functional/comment_group_plugin_profile_controller_test.rb b/plugins/comment_paragraph/test/functional/comment_group_plugin_profile_controller_test.rb
new file mode 100644
index 0000000..012dc93
--- /dev/null
+++ b/plugins/comment_paragraph/test/functional/comment_group_plugin_profile_controller_test.rb
@@ -0,0 +1,72 @@
+require File.dirname(__FILE__) + '/../test_helper'
+require File.dirname(__FILE__) + '/../../controllers/profile/comment_paragraph_plugin_profile_controller'
+
+# Re-raise errors caught by the controller.
+class CommentParagraphPluginProfileController; def rescue_action(e) raise e end; end
+
+class CommentParagraphPluginProfileControllerTest < ActionController::TestCase
+
+ def setup
+ @controller = CommentParagraphPluginProfileController.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+
+ @profile = create_user('testuser').person
+ @article = profile.articles.build(:name => 'test')
+ @article.save!
+ end
+ attr_reader :article
+ attr_reader :profile
+
+ should 'be able to show paragraph comments' do
+ comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :paragraph_id => 0)
+ xhr :get, :view_comments, :profile => @profile.identifier, :article_id => article.id, :paragraph_id => 0
+ assert_template 'comment_paragraph_plugin_profile/view_comments'
+ assert_match /comments_list_paragraph_0/, @response.body
+ assert_match /\"comment-count-0\", \"1\"/, @response.body
+ end
+
+ should 'do not show global comments' do
+ fast_create(Comment, :source_id => article, :author_id => profile, :title => 'global comment', :body => 'global', :paragraph_id => nil)
+ fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :paragraph_id => 0)
+ xhr :get, :view_comments, :profile => @profile.identifier, :article_id => article.id, :paragraph_id => 0
+ assert_template 'comment_paragraph_plugin_profile/view_comments'
+ assert_match /comments_list_paragraph_0/, @response.body
+ assert_match /\"comment-count-0\", \"1\"/, @response.body
+ end
+
+ should 'show first page comments only' do
+ comment1 = fast_create(Comment, :created_at => Time.now - 1.days, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'secondpage', :paragraph_id => 0)
+ comment2 = fast_create(Comment, :created_at => Time.now - 2.days, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'firstpage 1', :paragraph_id => 0)
+ comment3 = fast_create(Comment, :created_at => Time.now - 3.days, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'firstpage 2', :paragraph_id => 0)
+ comment4 = fast_create(Comment, :created_at => Time.now - 4.days, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'firstpage 3', :paragraph_id => 0)
+ xhr :get, :view_comments, :profile => @profile.identifier, :article_id => article.id, :paragraph_id => 0
+ assert_match /firstpage 1/, @response.body
+ assert_match /firstpage 2/, @response.body
+ assert_match /firstpage 3/, @response.body
+ assert_no_match /secondpage/, @response.body
+ end
+
+ should 'show link to display more comments' do
+ comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :paragraph_id => 0)
+ comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :paragraph_id => 0)
+ comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :paragraph_id => 0)
+ comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'secondpage', :body => 'secondpage', :paragraph_id => 0)
+ xhr :get, :view_comments, :profile => @profile.identifier, :article_id => article.id, :paragraph_id => 0
+ assert_match /paragraph_comment_page=2/, @response.body
+ end
+
+ should 'do not show link to display more comments if do not have more pages' do
+ comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :paragraph_id => 0)
+ comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :paragraph_id => 0)
+ comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :paragraph_id => 0)
+ xhr :get, :view_comments, :profile => @profile.identifier, :article_id => article.id, :paragraph_id => 0
+ assert_no_match /paragraph_comment_page/, @response.body
+ end
+
+ should 'do not show link to display more comments if do not have any comments' do
+ xhr :get, :view_comments, :profile => @profile.identifier, :article_id => article.id, :paragraph_id => 0
+ assert_no_match /paragraph_comment_page/, @response.body
+ end
+
+end
diff --git a/plugins/comment_paragraph/test/functional/comment_group_plugin_public_controller_test.rb b/plugins/comment_paragraph/test/functional/comment_group_plugin_public_controller_test.rb
new file mode 100644
index 0000000..01bc577
--- /dev/null
+++ b/plugins/comment_paragraph/test/functional/comment_group_plugin_public_controller_test.rb
@@ -0,0 +1,33 @@
+require File.dirname(__FILE__) + '/../test_helper'
+require File.dirname(__FILE__) + '/../../controllers/public/comment_paragraph_plugin_public_controller'
+
+# Re-raise errors caught by the controller.
+class CommentParagraphPluginPublicController; def rescue_action(e) raise e end; end
+
+class CommentParagraphPluginPublicControllerTest < ActionController::TestCase
+
+ def setup
+ @controller = CommentParagraphPluginPublicController.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+
+ @profile = create_user('testuser').person
+ @article = profile.articles.build(:name => 'test')
+ @article.save!
+ end
+ attr_reader :article
+ attr_reader :profile
+
+ should 'be able to return paragraph_id for a comment' do
+ comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :paragraph_id => 0)
+ xhr :get, :comment_paragraph, :id => comment.id
+ assert_match /\{\"paragraph_id\":0\}/, @response.body
+ end
+
+ should 'return paragraph_id=null for a global comment' do
+ comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala' )
+ xhr :get, :comment_paragraph, :id => comment.id
+ assert_match /\{\"paragraph_id\":null\}/, @response.body
+ end
+
+end
diff --git a/plugins/comment_paragraph/test/functional/content_viewer_controller_test.rb b/plugins/comment_paragraph/test/functional/content_viewer_controller_test.rb
new file mode 100644
index 0000000..994c2d3
--- /dev/null
+++ b/plugins/comment_paragraph/test/functional/content_viewer_controller_test.rb
@@ -0,0 +1,28 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class ContentViewerController
+ append_view_path File.join(File.dirname(__FILE__) + '/../../views')
+ def rescue_action(e)
+ raise e
+ end
+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)
+ end
+
+ attr_reader :page
+
+ should 'parse article body and render comment paragraph view' do
+ comment1 = fast_create(Comment, :paragraph_id => 1, :source_id => page.id)
+ get :view_page, @page.url
+ assert_tag 'div', :attributes => {:class => 'comment_paragraph_1'}
+ assert_tag 'div', :attributes => {:id => 'comments_paragraph_count_1'}
+ end
+
+end
diff --git a/plugins/comment_paragraph/test/test_helper.rb b/plugins/comment_paragraph/test/test_helper.rb
new file mode 100644
index 0000000..cca1fd3
--- /dev/null
+++ b/plugins/comment_paragraph/test/test_helper.rb
@@ -0,0 +1 @@
+require File.dirname(__FILE__) + '/../../../test/test_helper'
diff --git a/plugins/comment_paragraph/test/unit/allow_comment_test.rb b/plugins/comment_paragraph/test/unit/allow_comment_test.rb
new file mode 100644
index 0000000..f37a6f5
--- /dev/null
+++ b/plugins/comment_paragraph/test/unit/allow_comment_test.rb
@@ -0,0 +1,26 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class AllowCommentTest < ActiveSupport::TestCase
+
+ def setup
+ @macro = CommentParagraphPlugin::AllowComment.new
+ end
+
+ attr_reader :macro
+
+ 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_id => 1, :source_id => article.id)
+ inner_html = 'inner'
+ content = macro.parse({:paragraph_id => comment.paragraph_id}, inner_html, article)
+
+ expects(:render).with({:partial => 'comment_paragraph_plugin_profile/comment_paragraph', :locals => {:paragraph_id => comment.paragraph_id, :article_id => article.id, :inner_html => inner_html, :count => 1, :profile_identifier => profile.identifier} })
+ instance_eval(&content)
+ end
+
+end
diff --git a/plugins/comment_paragraph/test/unit/article_test.rb b/plugins/comment_paragraph/test/unit/article_test.rb
new file mode 100644
index 0000000..b62af14
--- /dev/null
+++ b/plugins/comment_paragraph/test/unit/article_test.rb
@@ -0,0 +1,56 @@
+require File.dirname(__FILE__) + '/../test_helper'
+require 'benchmark'
+
+class ArticleTest < ActiveSupport::TestCase
+
+ def setup
+ profile = fast_create(Community)
+ @article = fast_create(Article, :profile_id => profile.id)
+ end
+
+ attr_reader :article
+
+ should 'return paragraph comments from article' do
+ comment1 = fast_create(Comment, :paragraph_id => 1, :source_id => article.id)
+ comment2 = fast_create(Comment, :paragraph_id => nil, :source_id => article.id)
+ assert_equal [comment1], article.paragraph_comments
+ end
+
+ should 'do not allow a exclusion of a paragraph comment macro if this paragraph has comments' do
+ article.body = "
"
+ comment1 = fast_create(Comment, :paragraph_id => 1, :source_id => article.id)
+ assert !article.save
+ assert_equal ['Not empty paragraph comment cannot be removed'], article.errors[:base]
+ end
+
+ should 'allow save if comment paragraph macro is not removed for paragraph with comments' do
+ article.body = "
"
+ comment1 = fast_create(Comment, :paragraph_id => 1, :source_id => article.id)
+ assert article.save
+ end
+
+ should 'do not validate empty paragraph if article body is not changed' do
+ article.body = "
"
+ assert article.save
+ comment1 = fast_create(Comment, :paragraph_id => 1, :source_id => article.id)
+ article.name = article.name + 'changed'
+ assert article.save
+ end
+
+ should 'improve performance checking changes in body' do
+ i = 1
+ time0 = (Benchmark.measure { 50.times {
+ i = i + 1
+ article.body = "i = #{i}"
+ assert article.save
+ }})
+ i = 1
+ time1 = (Benchmark.measure { 50.times {
+ i = i + 1
+ article.body = "i = 1"
+ assert article.save
+ }})
+ assert time0.total > time1.total
+ end
+
+end
diff --git a/plugins/comment_paragraph/test/unit/comment_paragraph_plugin_test.rb b/plugins/comment_paragraph/test/unit/comment_paragraph_plugin_test.rb
new file mode 100644
index 0000000..4562b73
--- /dev/null
+++ b/plugins/comment_paragraph/test/unit/comment_paragraph_plugin_test.rb
@@ -0,0 +1,60 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class CommentParagraphPluginTest < ActiveSupport::TestCase
+
+ def setup
+ @environment = Environment.default
+ @plugin = CommentParagraphPlugin.new
+ end
+
+ attr_reader :environment, :plugin
+
+ should 'have a name' do
+ assert_not_equal Noosfero::Plugin.plugin_name, CommentParagraphPlugin::plugin_name
+ end
+
+ should 'describe yourself' do
+ assert_not_equal Noosfero::Plugin.plugin_description, CommentParagraphPlugin::plugin_description
+ end
+
+ should 'have a js file' do
+ assert !plugin.js_files.blank?
+ end
+
+ should 'have stylesheet' do
+ assert plugin.stylesheet?
+ end
+
+ should 'have extra contents for comment form' do
+ comment = fast_create(Comment, :paragraph_id => 1)
+ content = plugin.comment_form_extra_contents({:comment => comment})
+ expects(:hidden_field_tag).with('comment[paragraph_id]', comment.paragraph_id).once
+ instance_eval(&content)
+ end
+
+ should 'do not have extra contents for comments without paragraph' do
+ comment = fast_create(Comment, :paragraph_id => nil)
+ content = plugin.comment_form_extra_contents({:comment => comment})
+ assert_equal nil, instance_eval(&content)
+ end
+
+ should 'call without_paragraph for scope passed as parameter to unavailable_comments' do
+ article = fast_create(Article)
+ article.expects(:without_paragraph).once
+ plugin.unavailable_comments(article)
+ end
+
+#FIXME Obsolete test
+#
+# should 'filter_comments returns all the comments wihout paragraph of an article passed as parameter' do
+# article = fast_create(Article)
+# c1 = fast_create(Comment, :source_id => article.id, :paragraph_id => 1)
+# c2 = fast_create(Comment, :source_id => article.id)
+# c3 = fast_create(Comment, :source_id => article.id)
+#
+# plugin = CommentParagraphPlugin.new
+# assert_equal [], [c2, c3] - plugin.filter_comments(article.comments)
+# assert_equal [], plugin.filter_comments(article.comments) - [c2, c3]
+# end
+
+end
diff --git a/plugins/comment_paragraph/test/unit/comment_test.rb b/plugins/comment_paragraph/test/unit/comment_test.rb
new file mode 100644
index 0000000..9ba97f3
--- /dev/null
+++ b/plugins/comment_paragraph/test/unit/comment_test.rb
@@ -0,0 +1,26 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class CommentTest < ActiveSupport::TestCase
+
+ def setup
+ profile = fast_create(Community)
+ @article = fast_create(Article, :profile_id => profile.id)
+ end
+
+ attr_reader :article
+
+ should 'return comments that belongs to a specified paragraph' do
+ comment1 = fast_create(Comment, :paragraph_id => 1, :source_id => article.id)
+ comment2 = fast_create(Comment, :paragraph_id => nil, :source_id => article.id)
+ comment3 = fast_create(Comment, :paragraph_id => 2, :source_id => article.id)
+ assert_equal [comment1], article.comments.in_paragraph(1)
+ end
+
+ should 'return comments that do not belongs to any paragraph' do
+ comment1 = fast_create(Comment, :paragraph_id => 1, :source_id => article.id)
+ comment2 = fast_create(Comment, :paragraph_id => nil, :source_id => article.id)
+ comment3 = fast_create(Comment, :paragraph_id => 2, :source_id => article.id)
+ assert_equal [comment2], article.comments.without_paragraph
+ end
+
+end
diff --git a/plugins/comment_paragraph/views/comment_paragraph_plugin_profile/_comment_paragraph.html.erb b/plugins/comment_paragraph/views/comment_paragraph_plugin_profile/_comment_paragraph.html.erb
new file mode 100644
index 0000000..756728b
--- /dev/null
+++ b/plugins/comment_paragraph/views/comment_paragraph_plugin_profile/_comment_paragraph.html.erb
@@ -0,0 +1,31 @@
+
diff --git a/plugins/comment_paragraph/views/comment_paragraph_plugin_profile/view_comments.rjs b/plugins/comment_paragraph/views/comment_paragraph_plugin_profile/view_comments.rjs
new file mode 100644
index 0000000..fc4a2cb
--- /dev/null
+++ b/plugins/comment_paragraph/views/comment_paragraph_plugin_profile/view_comments.rjs
@@ -0,0 +1,12 @@
+if @paragraph_comment_page == 1
+ page.replace_html "comments_list_paragraph_#{@paragraph_id}", :partial => 'comment/comment.html.erb', :collection => @comments
+else
+ page.insert_html :bottom, "comments_list_paragraph_#{@paragraph_id}", :partial => 'comment/comment.html.erb', :collection => @comments
+end
+page.replace_html "comment-count-#{@paragraph_id}", @comments_count
+
+if @no_more_pages
+ page.replace_html "comments_list_paragraph_#{@paragraph_id}_more", ""
+else
+ page.replace_html "comments_list_paragraph_#{@paragraph_id}_more", link_to_remote(_('More'), :url => { :profile => profile.identifier, :controller => 'comment_paragraph_plugin_profile', :action => 'view_comments', :paragraph_id => @paragraph_id, :article_id => @article_id, :paragraph_comment_page => @paragraph_comment_page + 1}, :loaded => visual_effect(:highlight, "comments_list_paragraph_#{@paragraph_id}"), :method => :post, :complete => "loadCompleted(#{@paragraph_id})")
+end
--
libgit2 0.21.2
"); + } + } +} + diff --git a/plugins/comment_paragraph/public/comment_paragraph_macro.js b/plugins/comment_paragraph/public/comment_paragraph_macro.js new file mode 100644 index 0000000..3814002 --- /dev/null +++ b/plugins/comment_paragraph/public/comment_paragraph_macro.js @@ -0,0 +1,39 @@ +var comment_paragraph_anchor; +jQuery(document).ready(function($) { + var anchor = window.location.hash; + if(anchor.length==0) return; + + var val = anchor.split('-'); //anchor format = #comment-\d+ + if(val.length!=2 || val[0]!='#comment') return; + if($('div[data-macro=comment_paragraph_plugin/allow_comment]').length==0) return; //comment_paragraph_plugin/allow_comment div must exists + var comment_id = val[1]; + if(!/^\d+$/.test(comment_id)) return; //test for integer + + comment_paragraph_anchor = anchor; + var url = '/plugin/comment_paragraph/public/comment_paragraph/'+comment_id; + $.getJSON(url, function(data) { + if(data.paragraph_id!=null) { + var button = $('div.comment_paragraph_'+ data.paragraph_id + ' a'); + button.click(); + $.scrollTo(button); + } + }); +}); + +function toggleParagraph(paragraph) { + var div = jQuery('div.comments_list_toggle_paragraph_'+paragraph); + var visible = div.is(':visible'); + if(!visible) + jQuery('div.comment-paragraph-loading-'+paragraph).addClass('comment-button-loading'); + + div.toggle('fast'); + return visible; +} + +function loadCompleted(paragraph) { + jQuery('div.comment-paragraph-loading-'+paragraph).removeClass('comment-button-loading') + if(comment_paragraph_anchor) { + jQuery.scrollTo(jQuery(comment_paragraph_anchor)); + comment_paragraph_anchor = null; + } +} diff --git a/plugins/comment_paragraph/public/images/comments.gif b/plugins/comment_paragraph/public/images/comments.gif new file mode 100644 index 0000000..e3982a9 Binary files /dev/null and b/plugins/comment_paragraph/public/images/comments.gif differ diff --git a/plugins/comment_paragraph/public/style.css b/plugins/comment_paragraph/public/style.css new file mode 100644 index 0000000..d8f9e6f --- /dev/null +++ b/plugins/comment_paragraph/public/style.css @@ -0,0 +1,7 @@ +div.article-comments-list-more{ + width: 100%; + height: 30px; + text-align: center; + font-size: 20px; + margin-bottom: 5px; +} diff --git a/plugins/comment_paragraph/test/functional/comment_group_plugin_profile_controller_test.rb b/plugins/comment_paragraph/test/functional/comment_group_plugin_profile_controller_test.rb new file mode 100644 index 0000000..012dc93 --- /dev/null +++ b/plugins/comment_paragraph/test/functional/comment_group_plugin_profile_controller_test.rb @@ -0,0 +1,72 @@ +require File.dirname(__FILE__) + '/../test_helper' +require File.dirname(__FILE__) + '/../../controllers/profile/comment_paragraph_plugin_profile_controller' + +# Re-raise errors caught by the controller. +class CommentParagraphPluginProfileController; def rescue_action(e) raise e end; end + +class CommentParagraphPluginProfileControllerTest < ActionController::TestCase + + def setup + @controller = CommentParagraphPluginProfileController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + + @profile = create_user('testuser').person + @article = profile.articles.build(:name => 'test') + @article.save! + end + attr_reader :article + attr_reader :profile + + should 'be able to show paragraph comments' do + comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :paragraph_id => 0) + xhr :get, :view_comments, :profile => @profile.identifier, :article_id => article.id, :paragraph_id => 0 + assert_template 'comment_paragraph_plugin_profile/view_comments' + assert_match /comments_list_paragraph_0/, @response.body + assert_match /\"comment-count-0\", \"1\"/, @response.body + end + + should 'do not show global comments' do + fast_create(Comment, :source_id => article, :author_id => profile, :title => 'global comment', :body => 'global', :paragraph_id => nil) + fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :paragraph_id => 0) + xhr :get, :view_comments, :profile => @profile.identifier, :article_id => article.id, :paragraph_id => 0 + assert_template 'comment_paragraph_plugin_profile/view_comments' + assert_match /comments_list_paragraph_0/, @response.body + assert_match /\"comment-count-0\", \"1\"/, @response.body + end + + should 'show first page comments only' do + comment1 = fast_create(Comment, :created_at => Time.now - 1.days, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'secondpage', :paragraph_id => 0) + comment2 = fast_create(Comment, :created_at => Time.now - 2.days, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'firstpage 1', :paragraph_id => 0) + comment3 = fast_create(Comment, :created_at => Time.now - 3.days, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'firstpage 2', :paragraph_id => 0) + comment4 = fast_create(Comment, :created_at => Time.now - 4.days, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'firstpage 3', :paragraph_id => 0) + xhr :get, :view_comments, :profile => @profile.identifier, :article_id => article.id, :paragraph_id => 0 + assert_match /firstpage 1/, @response.body + assert_match /firstpage 2/, @response.body + assert_match /firstpage 3/, @response.body + assert_no_match /secondpage/, @response.body + end + + should 'show link to display more comments' do + comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :paragraph_id => 0) + comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :paragraph_id => 0) + comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :paragraph_id => 0) + comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'secondpage', :body => 'secondpage', :paragraph_id => 0) + xhr :get, :view_comments, :profile => @profile.identifier, :article_id => article.id, :paragraph_id => 0 + assert_match /paragraph_comment_page=2/, @response.body + end + + should 'do not show link to display more comments if do not have more pages' do + comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :paragraph_id => 0) + comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :paragraph_id => 0) + comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :paragraph_id => 0) + xhr :get, :view_comments, :profile => @profile.identifier, :article_id => article.id, :paragraph_id => 0 + assert_no_match /paragraph_comment_page/, @response.body + end + + should 'do not show link to display more comments if do not have any comments' do + xhr :get, :view_comments, :profile => @profile.identifier, :article_id => article.id, :paragraph_id => 0 + assert_no_match /paragraph_comment_page/, @response.body + end + +end diff --git a/plugins/comment_paragraph/test/functional/comment_group_plugin_public_controller_test.rb b/plugins/comment_paragraph/test/functional/comment_group_plugin_public_controller_test.rb new file mode 100644 index 0000000..01bc577 --- /dev/null +++ b/plugins/comment_paragraph/test/functional/comment_group_plugin_public_controller_test.rb @@ -0,0 +1,33 @@ +require File.dirname(__FILE__) + '/../test_helper' +require File.dirname(__FILE__) + '/../../controllers/public/comment_paragraph_plugin_public_controller' + +# Re-raise errors caught by the controller. +class CommentParagraphPluginPublicController; def rescue_action(e) raise e end; end + +class CommentParagraphPluginPublicControllerTest < ActionController::TestCase + + def setup + @controller = CommentParagraphPluginPublicController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + + @profile = create_user('testuser').person + @article = profile.articles.build(:name => 'test') + @article.save! + end + attr_reader :article + attr_reader :profile + + should 'be able to return paragraph_id for a comment' do + comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :paragraph_id => 0) + xhr :get, :comment_paragraph, :id => comment.id + assert_match /\{\"paragraph_id\":0\}/, @response.body + end + + should 'return paragraph_id=null for a global comment' do + comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala' ) + xhr :get, :comment_paragraph, :id => comment.id + assert_match /\{\"paragraph_id\":null\}/, @response.body + end + +end diff --git a/plugins/comment_paragraph/test/functional/content_viewer_controller_test.rb b/plugins/comment_paragraph/test/functional/content_viewer_controller_test.rb new file mode 100644 index 0000000..994c2d3 --- /dev/null +++ b/plugins/comment_paragraph/test/functional/content_viewer_controller_test.rb @@ -0,0 +1,28 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class ContentViewerController + append_view_path File.join(File.dirname(__FILE__) + '/../../views') + def rescue_action(e) + raise e + end +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) + end + + attr_reader :page + + should 'parse article body and render comment paragraph view' do + comment1 = fast_create(Comment, :paragraph_id => 1, :source_id => page.id) + get :view_page, @page.url + assert_tag 'div', :attributes => {:class => 'comment_paragraph_1'} + assert_tag 'div', :attributes => {:id => 'comments_paragraph_count_1'} + end + +end diff --git a/plugins/comment_paragraph/test/test_helper.rb b/plugins/comment_paragraph/test/test_helper.rb new file mode 100644 index 0000000..cca1fd3 --- /dev/null +++ b/plugins/comment_paragraph/test/test_helper.rb @@ -0,0 +1 @@ +require File.dirname(__FILE__) + '/../../../test/test_helper' diff --git a/plugins/comment_paragraph/test/unit/allow_comment_test.rb b/plugins/comment_paragraph/test/unit/allow_comment_test.rb new file mode 100644 index 0000000..f37a6f5 --- /dev/null +++ b/plugins/comment_paragraph/test/unit/allow_comment_test.rb @@ -0,0 +1,26 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class AllowCommentTest < ActiveSupport::TestCase + + def setup + @macro = CommentParagraphPlugin::AllowComment.new + end + + attr_reader :macro + + 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_id => 1, :source_id => article.id) + inner_html = 'inner' + content = macro.parse({:paragraph_id => comment.paragraph_id}, inner_html, article) + + expects(:render).with({:partial => 'comment_paragraph_plugin_profile/comment_paragraph', :locals => {:paragraph_id => comment.paragraph_id, :article_id => article.id, :inner_html => inner_html, :count => 1, :profile_identifier => profile.identifier} }) + instance_eval(&content) + end + +end diff --git a/plugins/comment_paragraph/test/unit/article_test.rb b/plugins/comment_paragraph/test/unit/article_test.rb new file mode 100644 index 0000000..b62af14 --- /dev/null +++ b/plugins/comment_paragraph/test/unit/article_test.rb @@ -0,0 +1,56 @@ +require File.dirname(__FILE__) + '/../test_helper' +require 'benchmark' + +class ArticleTest < ActiveSupport::TestCase + + def setup + profile = fast_create(Community) + @article = fast_create(Article, :profile_id => profile.id) + end + + attr_reader :article + + should 'return paragraph comments from article' do + comment1 = fast_create(Comment, :paragraph_id => 1, :source_id => article.id) + comment2 = fast_create(Comment, :paragraph_id => nil, :source_id => article.id) + assert_equal [comment1], article.paragraph_comments + end + + should 'do not allow a exclusion of a paragraph comment macro if this paragraph has comments' do + article.body = "" + comment1 = fast_create(Comment, :paragraph_id => 1, :source_id => article.id) + assert !article.save + assert_equal ['Not empty paragraph comment cannot be removed'], article.errors[:base] + end + + should 'allow save if comment paragraph macro is not removed for paragraph with comments' do + article.body = "" + comment1 = fast_create(Comment, :paragraph_id => 1, :source_id => article.id) + assert article.save + end + + should 'do not validate empty paragraph if article body is not changed' do + article.body = "" + assert article.save + comment1 = fast_create(Comment, :paragraph_id => 1, :source_id => article.id) + article.name = article.name + 'changed' + assert article.save + end + + should 'improve performance checking changes in body' do + i = 1 + time0 = (Benchmark.measure { 50.times { + i = i + 1 + article.body = "i = #{i}" + assert article.save + }}) + i = 1 + time1 = (Benchmark.measure { 50.times { + i = i + 1 + article.body = "i = 1" + assert article.save + }}) + assert time0.total > time1.total + end + +end diff --git a/plugins/comment_paragraph/test/unit/comment_paragraph_plugin_test.rb b/plugins/comment_paragraph/test/unit/comment_paragraph_plugin_test.rb new file mode 100644 index 0000000..4562b73 --- /dev/null +++ b/plugins/comment_paragraph/test/unit/comment_paragraph_plugin_test.rb @@ -0,0 +1,60 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class CommentParagraphPluginTest < ActiveSupport::TestCase + + def setup + @environment = Environment.default + @plugin = CommentParagraphPlugin.new + end + + attr_reader :environment, :plugin + + should 'have a name' do + assert_not_equal Noosfero::Plugin.plugin_name, CommentParagraphPlugin::plugin_name + end + + should 'describe yourself' do + assert_not_equal Noosfero::Plugin.plugin_description, CommentParagraphPlugin::plugin_description + end + + should 'have a js file' do + assert !plugin.js_files.blank? + end + + should 'have stylesheet' do + assert plugin.stylesheet? + end + + should 'have extra contents for comment form' do + comment = fast_create(Comment, :paragraph_id => 1) + content = plugin.comment_form_extra_contents({:comment => comment}) + expects(:hidden_field_tag).with('comment[paragraph_id]', comment.paragraph_id).once + instance_eval(&content) + end + + should 'do not have extra contents for comments without paragraph' do + comment = fast_create(Comment, :paragraph_id => nil) + content = plugin.comment_form_extra_contents({:comment => comment}) + assert_equal nil, instance_eval(&content) + end + + should 'call without_paragraph for scope passed as parameter to unavailable_comments' do + article = fast_create(Article) + article.expects(:without_paragraph).once + plugin.unavailable_comments(article) + end + +#FIXME Obsolete test +# +# should 'filter_comments returns all the comments wihout paragraph of an article passed as parameter' do +# article = fast_create(Article) +# c1 = fast_create(Comment, :source_id => article.id, :paragraph_id => 1) +# c2 = fast_create(Comment, :source_id => article.id) +# c3 = fast_create(Comment, :source_id => article.id) +# +# plugin = CommentParagraphPlugin.new +# assert_equal [], [c2, c3] - plugin.filter_comments(article.comments) +# assert_equal [], plugin.filter_comments(article.comments) - [c2, c3] +# end + +end diff --git a/plugins/comment_paragraph/test/unit/comment_test.rb b/plugins/comment_paragraph/test/unit/comment_test.rb new file mode 100644 index 0000000..9ba97f3 --- /dev/null +++ b/plugins/comment_paragraph/test/unit/comment_test.rb @@ -0,0 +1,26 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class CommentTest < ActiveSupport::TestCase + + def setup + profile = fast_create(Community) + @article = fast_create(Article, :profile_id => profile.id) + end + + attr_reader :article + + should 'return comments that belongs to a specified paragraph' do + comment1 = fast_create(Comment, :paragraph_id => 1, :source_id => article.id) + comment2 = fast_create(Comment, :paragraph_id => nil, :source_id => article.id) + comment3 = fast_create(Comment, :paragraph_id => 2, :source_id => article.id) + assert_equal [comment1], article.comments.in_paragraph(1) + end + + should 'return comments that do not belongs to any paragraph' do + comment1 = fast_create(Comment, :paragraph_id => 1, :source_id => article.id) + comment2 = fast_create(Comment, :paragraph_id => nil, :source_id => article.id) + comment3 = fast_create(Comment, :paragraph_id => 2, :source_id => article.id) + assert_equal [comment2], article.comments.without_paragraph + end + +end diff --git a/plugins/comment_paragraph/views/comment_paragraph_plugin_profile/_comment_paragraph.html.erb b/plugins/comment_paragraph/views/comment_paragraph_plugin_profile/_comment_paragraph.html.erb new file mode 100644 index 0000000..756728b --- /dev/null +++ b/plugins/comment_paragraph/views/comment_paragraph_plugin_profile/_comment_paragraph.html.erb @@ -0,0 +1,31 @@ +