From 853e130b9f87c444574ca9ff5fc1fb45c51590ef Mon Sep 17 00:00:00 2001 From: gauravano Date: Mon, 12 Feb 2018 21:07:20 +0530 Subject: [PATCH 1/7] Controller and model method for marking --- app/controllers/admin_controller.rb | 17 +++++++++++++++++ app/models/comment.rb | 7 +++++++ 2 files changed, 24 insertions(+) diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index e081a612b9..3f84e5f0e5 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -123,6 +123,23 @@ def mark_spam end end + def mark_comment_spam + @comment = Comment.find params[:id] + if current_user && (current_user.role == 'moderator' || current_user.role == 'admin') + if @comment.status == 0 + @comment.comment_spam + flash[:notice] = "Comment has been marked as spam." + redirect_to '/dashboard' + else + flash[:notice] = "Comment already marked as spam." + redirect_to '/dashboard' + end + else + flash[:error] = 'Only moderators can moderate comments.' + redirect_to '/dashboard' + end + end + def publish if current_user && (current_user.role == 'moderator' || current_user.role == 'admin') @node = Node.find params[:id] diff --git a/app/models/comment.rb b/app/models/comment.rb index ebe8b1120e..6817f6bd4d 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -142,4 +142,11 @@ def answer_comment_notify(current_user) notify_users(uids, current_user) notify_tag_followers(already + uids) end + + def comment_spam + self.status = 2 + save + self + end + end From 1bdc48a3f2e96a2fc6442f0e64c54680a874b51b Mon Sep 17 00:00:00 2001 From: gauravano Date: Fri, 16 Feb 2018 16:33:01 +0530 Subject: [PATCH 2/7] Publish comment method added --- app/controllers/admin_controller.rb | 21 ++++++++++++++++++--- app/models/comment.rb | 8 +++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index 3f84e5f0e5..7d2d8b6e74 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -127,15 +127,30 @@ def mark_comment_spam @comment = Comment.find params[:id] if current_user && (current_user.role == 'moderator' || current_user.role == 'admin') if @comment.status == 0 - @comment.comment_spam + @comment.spam flash[:notice] = "Comment has been marked as spam." - redirect_to '/dashboard' else flash[:notice] = "Comment already marked as spam." - redirect_to '/dashboard' end else flash[:error] = 'Only moderators can moderate comments.' + end + redirect_to '/dashboard' + end + + def publish_comment + if current_user && (current_user.role == 'moderator' || current_user.role == 'admin') + @comment = Comment.find params[:id] + if @comment.status == 0 + flash[:notice] = 'Comment already published.' + else + @comment.publish + flash[:notice] = 'Comment published.' + end + @node = @comment.node + redirect_to @node.path + else + flash[:error] = 'Only moderators can publish comments.' redirect_to '/dashboard' end end diff --git a/app/models/comment.rb b/app/models/comment.rb index 6817f6bd4d..667175828e 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -143,10 +143,16 @@ def answer_comment_notify(current_user) notify_tag_followers(already + uids) end - def comment_spam + def spam self.status = 2 save self end + def publish + self.status = 0 + save + self + end + end From fbeb3a9f246201b50ea448b33c0423fb6b9c731b Mon Sep 17 00:00:00 2001 From: gauravano Date: Fri, 16 Feb 2018 18:13:41 +0530 Subject: [PATCH 3/7] test for marking comment --- app/controllers/admin_controller.rb | 4 +- test/fixtures/comments.yml | 8 ++++ test/functional/admin_controller_test.rb | 57 ++++++++++++++++++++++++ test/functional/notes_controller_test.rb | 2 +- test/unit/node_test.rb | 2 +- 5 files changed, 69 insertions(+), 4 deletions(-) diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index 7d2d8b6e74..83fda31810 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -1,5 +1,5 @@ class AdminController < ApplicationController - before_filter :require_user, only: %i(spam spam_revisions) + before_filter :require_user, only: %i(spam spam_revisions mark_comment_spam publish_comment) def promote_admin @user = User.find params[:id] @@ -126,7 +126,7 @@ def mark_spam def mark_comment_spam @comment = Comment.find params[:id] if current_user && (current_user.role == 'moderator' || current_user.role == 'admin') - if @comment.status == 0 + if @comment.status != 2 @comment.spam flash[:notice] = "Comment has been marked as spam." else diff --git a/test/fixtures/comments.yml b/test/fixtures/comments.yml index de8cbe1e6d..d06e65b566 100644 --- a/test/fixtures/comments.yml +++ b/test/fixtures/comments.yml @@ -112,3 +112,11 @@ question_tag: comment: 'Question #everything' timestamp: <%= Time.now.to_i + 13 %> thread: /04 + +spam_comment: + uid: 1 + nid: 1 + status: 2 + comment: This thing is spam. + timestamp: <%= Time.now.to_i + 1 %> + thread: /01 diff --git a/test/functional/admin_controller_test.rb b/test/functional/admin_controller_test.rb index b1285123a3..3ee3e48c99 100644 --- a/test/functional/admin_controller_test.rb +++ b/test/functional/admin_controller_test.rb @@ -396,4 +396,61 @@ def teardown assert_equal 1, node.author.status assert_redirected_to node.path(:question) end + + test 'should mark comment as spam if moderator' do + UserSession.create(users(:moderator)) + comment = comments(:first) + + post :mark_comment_spam, id: comment.id + + comment = assigns(:comment) + assert_equal 2, comment.status + assert_equal "Comment has been marked as spam.", flash[:notice] + assert_redirected_to '/dashboard' + end + + test 'should mark comment as spam if admin' do + UserSession.create(users(:admin)) + comment = comments(:first) + + post :mark_comment_spam, id: comment.id + + comment = assigns(:comment) + assert_equal 2, comment.status + assert_equal "Comment has been marked as spam.", flash[:notice] + assert_redirected_to '/dashboard' + end + + test 'should not mark comment as spam if no user' do + comment = comments(:first) + + post :mark_comment_spam, id: comment.id + + assert_redirected_to '/login' + end + + test 'should not mark comment as spam if normal user' do + UserSession.create(users(:bob)) + comment = comments(:first) + + post :mark_comment_spam, id: comment.id + + comment = assigns(:comment) + assert_equal 1, comment.status + assert_equal "Only moderators can moderate comments.", flash[:error] + assert_redirected_to '/dashboard' + end + + test 'should not mark comment as spam if it is already marked as spam' do + UserSession.create(users(:admin)) + comment = comments(:spam_comment) + + post :mark_comment_spam, id: comment.id + + comment = assigns(:comment) + assert_equal 2, comment.status + assert_equal "Comment already marked as spam.", flash[:notice] + assert_redirected_to '/dashboard' + end + end diff --git a/test/functional/notes_controller_test.rb b/test/functional/notes_controller_test.rb index 0d112fce5b..aeb52e002c 100644 --- a/test/functional/notes_controller_test.rb +++ b/test/functional/notes_controller_test.rb @@ -406,7 +406,7 @@ def teardown author: node.author.username, date: node.created_at.strftime('%m-%d-%Y'), id: node.title.parameterize - assert_select '.fa-fire', 3 + assert_select '.fa-fire', 4 end test 'should redirect to questions show page after creating a new question' do diff --git a/test/unit/node_test.rb b/test/unit/node_test.rb index e9320c772a..8abb0bf533 100644 --- a/test/unit/node_test.rb +++ b/test/unit/node_test.rb @@ -339,7 +339,7 @@ class NodeTest < ActiveSupport::TestCase test 'should delete associated comments when a node is deleted' do node = nodes(:one) - assert_equal node.comments.count, 4 + assert_equal node.comments.count, 5 deleted_node = node.destroy assert_equal node.comments.count, 0 end From 63f08d76e9336d0641bc90d7ff0c1a56b9aa1bb8 Mon Sep 17 00:00:00 2001 From: gauravano Date: Fri, 16 Feb 2018 18:31:54 +0530 Subject: [PATCH 4/7] tests for publish comment --- app/controllers/admin_controller.rb | 2 +- test/functional/admin_controller_test.rb | 57 ++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index 83fda31810..9608434f58 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -141,7 +141,7 @@ def mark_comment_spam def publish_comment if current_user && (current_user.role == 'moderator' || current_user.role == 'admin') @comment = Comment.find params[:id] - if @comment.status == 0 + if @comment.status != 2 flash[:notice] = 'Comment already published.' else @comment.publish diff --git a/test/functional/admin_controller_test.rb b/test/functional/admin_controller_test.rb index 3ee3e48c99..76a02b6fdf 100644 --- a/test/functional/admin_controller_test.rb +++ b/test/functional/admin_controller_test.rb @@ -453,4 +453,61 @@ def teardown assert_redirected_to '/dashboard' end + test 'should publish comment from spam if admin' do + UserSession.create(users(:admin)) + comment = comments(:spam_comment) + node = comment.node + post :publish_comment, id: comment.id + + comment = assigns(:comment) + assert_equal 0, comment.status + assert_equal "Comment published.", flash[:notice] + assert_redirected_to node.path + end + + test 'should publish comment from spam if moderator' do + UserSession.create(users(:moderator)) + comment = comments(:spam_comment) + node = comment.node + post :publish_comment, id: comment.id + + comment = assigns(:comment) + assert_equal 0, comment.status + assert_equal "Comment published.", flash[:notice] + assert_redirected_to node.path + end + + test 'should login if want to publish comment from spam' do + comment = comments(:spam_comment) + + post :publish_comment, id: comment.id + + assert_equal 2, comment.status + assert_redirected_to '/login' + end + + test 'should not publish comment from spam if any other user' do + UserSession.create(users(:newcomer)) + comment = comments(:spam_comment) + node = comment.node + + post :publish_comment, id: comment.id + + assert_equal 2, comment.status + assert_equal "Only moderators can publish comments.", flash[:error] + assert_redirected_to '/dashboard' + end + + test 'should not publish comment from spam if already published' do + UserSession.create(users(:admin)) + comment = comments(:first) + node = comment.node + + post :publish_comment, id: comment.id + + assert_equal 1, comment.status + assert_equal "Comment already published.", flash[:notice] + assert_redirected_to node.path + end + end From 12039881df968efc2e3256ab6f90249a9ddbd723 Mon Sep 17 00:00:00 2001 From: gauravano Date: Sat, 17 Feb 2018 00:10:06 +0530 Subject: [PATCH 5/7] Small changes --- app/controllers/admin_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index 9608434f58..5bc31ddc1e 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -126,7 +126,7 @@ def mark_spam def mark_comment_spam @comment = Comment.find params[:id] if current_user && (current_user.role == 'moderator' || current_user.role == 'admin') - if @comment.status != 2 + if @comment.status == 0 || @comment.status == 1 @comment.spam flash[:notice] = "Comment has been marked as spam." else @@ -141,7 +141,7 @@ def mark_comment_spam def publish_comment if current_user && (current_user.role == 'moderator' || current_user.role == 'admin') @comment = Comment.find params[:id] - if @comment.status != 2 + if @comment.status == 0 || @comment.status == 1 flash[:notice] = 'Comment already published.' else @comment.publish From d72ced41dedb2ce26e005b026603f1d07f7bf7d0 Mon Sep 17 00:00:00 2001 From: gauravano Date: Wed, 21 Feb 2018 01:40:41 +0530 Subject: [PATCH 6/7] controller changed --- app/controllers/admin_controller.rb | 4 ++-- app/models/comment.rb | 4 ++-- test/fixtures/comments.yml | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index 5bc31ddc1e..bcbe6d6e07 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -126,7 +126,7 @@ def mark_spam def mark_comment_spam @comment = Comment.find params[:id] if current_user && (current_user.role == 'moderator' || current_user.role == 'admin') - if @comment.status == 0 || @comment.status == 1 + if @comment.status == 1 @comment.spam flash[:notice] = "Comment has been marked as spam." else @@ -141,7 +141,7 @@ def mark_comment_spam def publish_comment if current_user && (current_user.role == 'moderator' || current_user.role == 'admin') @comment = Comment.find params[:id] - if @comment.status == 0 || @comment.status == 1 + if @comment.status == 1 flash[:notice] = 'Comment already published.' else @comment.publish diff --git a/app/models/comment.rb b/app/models/comment.rb index 667175828e..15112a07ae 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -144,13 +144,13 @@ def answer_comment_notify(current_user) end def spam - self.status = 2 + self.status = 0 save self end def publish - self.status = 0 + self.status = 1 save self end diff --git a/test/fixtures/comments.yml b/test/fixtures/comments.yml index d06e65b566..d3d3d0e031 100644 --- a/test/fixtures/comments.yml +++ b/test/fixtures/comments.yml @@ -116,7 +116,7 @@ question_tag: spam_comment: uid: 1 nid: 1 - status: 2 + status: 0 comment: This thing is spam. timestamp: <%= Time.now.to_i + 1 %> thread: /01 From ee93fb8f1ed4c84fbcde80c0a0cfa81fd810107d Mon Sep 17 00:00:00 2001 From: gauravano Date: Wed, 21 Feb 2018 01:48:17 +0530 Subject: [PATCH 7/7] updated test as per new comment system --- test/functional/admin_controller_test.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/functional/admin_controller_test.rb b/test/functional/admin_controller_test.rb index 76a02b6fdf..6453ae46bb 100644 --- a/test/functional/admin_controller_test.rb +++ b/test/functional/admin_controller_test.rb @@ -404,7 +404,7 @@ def teardown post :mark_comment_spam, id: comment.id comment = assigns(:comment) - assert_equal 2, comment.status + assert_equal 0, comment.status assert_equal "Comment has been marked as spam.", flash[:notice] assert_redirected_to '/dashboard' end @@ -416,7 +416,7 @@ def teardown post :mark_comment_spam, id: comment.id comment = assigns(:comment) - assert_equal 2, comment.status + assert_equal 0, comment.status assert_equal "Comment has been marked as spam.", flash[:notice] assert_redirected_to '/dashboard' end @@ -448,7 +448,7 @@ def teardown post :mark_comment_spam, id: comment.id comment = assigns(:comment) - assert_equal 2, comment.status + assert_equal 0, comment.status assert_equal "Comment already marked as spam.", flash[:notice] assert_redirected_to '/dashboard' end @@ -460,7 +460,7 @@ def teardown post :publish_comment, id: comment.id comment = assigns(:comment) - assert_equal 0, comment.status + assert_equal 1, comment.status assert_equal "Comment published.", flash[:notice] assert_redirected_to node.path end @@ -472,7 +472,7 @@ def teardown post :publish_comment, id: comment.id comment = assigns(:comment) - assert_equal 0, comment.status + assert_equal 1, comment.status assert_equal "Comment published.", flash[:notice] assert_redirected_to node.path end @@ -482,7 +482,7 @@ def teardown post :publish_comment, id: comment.id - assert_equal 2, comment.status + assert_equal 0, comment.status assert_redirected_to '/login' end @@ -493,7 +493,7 @@ def teardown post :publish_comment, id: comment.id - assert_equal 2, comment.status + assert_equal 0, comment.status assert_equal "Only moderators can publish comments.", flash[:error] assert_redirected_to '/dashboard' end