2024-02-25 12:15:55 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-12-04 13:41:29 -05:00
|
|
|
require "test_helper"
|
2010-02-15 13:59:58 -05:00
|
|
|
|
|
|
|
class CommentTest < ActiveSupport::TestCase
|
|
|
|
context "A comment" do
|
|
|
|
setup do
|
2023-12-04 13:41:29 -05:00
|
|
|
@user = create(:user)
|
|
|
|
CurrentUser.user = @user
|
2010-02-15 13:59:58 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2012-05-11 18:04:50 -04:00
|
|
|
context "created by a limited user" do
|
|
|
|
setup do
|
2022-03-28 15:17:34 -04:00
|
|
|
Danbooru.config.stubs(:disable_throttles?).returns(false)
|
2012-05-11 18:04:50 -04:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2012-05-11 18:04:50 -04:00
|
|
|
should "fail creation" do
|
2022-11-25 15:06:54 -05:00
|
|
|
comment = build(:comment, post: create(:post))
|
2012-05-11 18:04:50 -04:00
|
|
|
comment.save
|
2022-09-11 12:54:43 -04:00
|
|
|
assert_equal(["Creator can not yet perform this action. Account is too new"], comment.errors.full_messages)
|
2012-05-11 18:04:50 -04:00
|
|
|
end
|
2010-02-15 13:59:58 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2012-05-11 18:04:50 -04:00
|
|
|
context "created by an unlimited user" do
|
|
|
|
setup do
|
|
|
|
Danbooru.config.stubs(:member_comment_limit).returns(100)
|
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2013-02-24 17:54:42 -05:00
|
|
|
context "that is then deleted" do
|
|
|
|
setup do
|
2022-11-25 15:06:54 -05:00
|
|
|
@post = create(:post)
|
|
|
|
@comment = create(:comment, post_id: @post.id)
|
2022-03-28 15:17:34 -04:00
|
|
|
@comment.destroy
|
2013-02-24 17:54:42 -05:00
|
|
|
@post.reload
|
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2013-02-24 17:54:42 -05:00
|
|
|
should "nullify the last_commented_at field" do
|
|
|
|
assert_nil(@post.last_commented_at)
|
|
|
|
end
|
|
|
|
end
|
2010-02-15 13:59:58 -05:00
|
|
|
|
2012-05-11 18:04:50 -04:00
|
|
|
should "be created" do
|
2022-11-25 15:06:54 -05:00
|
|
|
post = create(:post)
|
|
|
|
comment = build(:comment, post: post)
|
2012-05-11 18:04:50 -04:00
|
|
|
comment.save
|
|
|
|
assert(comment.errors.empty?, comment.errors.full_messages.join(", "))
|
|
|
|
end
|
|
|
|
|
2016-10-06 05:12:23 -04:00
|
|
|
should "not validate if the post does not exist" do
|
2022-11-25 15:06:54 -05:00
|
|
|
comment = build(:comment, post_id: -1)
|
2016-10-06 05:12:23 -04:00
|
|
|
|
|
|
|
assert_not(comment.valid?)
|
2022-03-28 15:17:34 -04:00
|
|
|
assert_match(/must exist/, comment.errors[:post].join(", "))
|
2016-10-06 05:12:23 -04:00
|
|
|
end
|
|
|
|
|
2012-05-11 18:04:50 -04:00
|
|
|
should "not bump the parent post" do
|
2022-11-25 15:06:54 -05:00
|
|
|
post = create(:post)
|
|
|
|
comment = create(:comment, do_not_bump_post: true, post: post)
|
2012-05-11 18:04:50 -04:00
|
|
|
post.reload
|
2013-12-24 20:59:19 -05:00
|
|
|
assert_nil(post.last_comment_bumped_at)
|
2012-05-11 18:04:50 -04:00
|
|
|
|
2022-11-25 15:06:54 -05:00
|
|
|
comment = create(:comment, post: post)
|
2012-05-11 18:04:50 -04:00
|
|
|
post.reload
|
2013-12-24 20:59:19 -05:00
|
|
|
assert_not_nil(post.last_comment_bumped_at)
|
2012-05-11 18:04:50 -04:00
|
|
|
end
|
|
|
|
|
2013-12-24 20:59:19 -05:00
|
|
|
should "not bump the post after exceeding the threshold" do
|
2012-05-11 18:04:50 -04:00
|
|
|
Danbooru.config.stubs(:comment_threshold).returns(1)
|
2022-11-25 15:06:54 -05:00
|
|
|
p = create(:post)
|
|
|
|
c1 = create(:comment, post: p)
|
2022-11-25 08:32:56 -05:00
|
|
|
travel_to(2.seconds.from_now) do
|
2022-11-25 15:06:54 -05:00
|
|
|
c2 = create(:comment, post: p)
|
2013-05-24 15:59:13 -04:00
|
|
|
end
|
2012-05-11 18:04:50 -04:00
|
|
|
p.reload
|
2013-12-24 20:59:19 -05:00
|
|
|
assert_equal(c1.created_at.to_s, p.last_comment_bumped_at.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
should "always record the last_commented_at properly" do
|
2022-11-25 15:06:54 -05:00
|
|
|
post = create(:post)
|
2013-12-24 20:59:19 -05:00
|
|
|
Danbooru.config.stubs(:comment_threshold).returns(1)
|
|
|
|
|
2022-11-25 15:06:54 -05:00
|
|
|
c1 = create(:comment, do_not_bump_post: true, post: post)
|
2013-12-24 20:59:19 -05:00
|
|
|
post.reload
|
|
|
|
assert_equal(c1.created_at.to_s, post.last_commented_at.to_s)
|
|
|
|
|
2022-11-25 15:06:54 -05:00
|
|
|
c2 = create(:comment, post: post)
|
2022-11-25 08:32:56 -05:00
|
|
|
post.reload
|
|
|
|
assert_equal(c2.created_at.to_s, post.last_commented_at.to_s)
|
2012-05-11 18:04:50 -04:00
|
|
|
end
|
|
|
|
|
2013-03-18 19:03:29 -04:00
|
|
|
should "not record the user id of the voter" do
|
2022-11-25 15:06:54 -05:00
|
|
|
user = create(:user)
|
|
|
|
user2 = create(:user)
|
|
|
|
post = create(:post)
|
|
|
|
c1 = create(:comment, post: post)
|
2022-05-16 08:20:11 -04:00
|
|
|
|
2022-11-26 09:20:15 -05:00
|
|
|
as(user2) do
|
2022-04-30 06:48:53 -04:00
|
|
|
VoteManager.comment_vote!(user: user2, comment: c1, score: -1)
|
|
|
|
c1.reload
|
|
|
|
assert_not_equal(user2.id, c1.updater_id)
|
|
|
|
end
|
2013-03-18 19:03:29 -04:00
|
|
|
end
|
|
|
|
|
2012-05-11 18:04:50 -04:00
|
|
|
should "not allow duplicate votes" do
|
2022-11-25 15:06:54 -05:00
|
|
|
user = create(:user)
|
|
|
|
user2 = create(:user)
|
|
|
|
post = create(:post)
|
|
|
|
c1 = create(:comment, post: post)
|
|
|
|
c2 = create(:comment, post: post)
|
2022-05-16 08:20:11 -04:00
|
|
|
|
2022-11-26 09:20:15 -05:00
|
|
|
as(user2) do
|
2022-04-30 06:48:53 -04:00
|
|
|
assert_nothing_raised { VoteManager.comment_vote!(user: user2, comment: c1, score: -1) }
|
|
|
|
assert_equal(:need_unvote, VoteManager.comment_vote!(user: user2, comment: c1, score: -1))
|
|
|
|
assert_equal(1, CommentVote.count)
|
|
|
|
assert_equal(-1, CommentVote.last.score)
|
|
|
|
|
|
|
|
assert_nothing_raised { VoteManager.comment_vote!(user: user2, comment: c2, score: -1) }
|
|
|
|
assert_equal(2, CommentVote.count)
|
|
|
|
end
|
2012-05-11 18:04:50 -04:00
|
|
|
end
|
|
|
|
|
2016-11-06 02:20:10 -05:00
|
|
|
should "not allow upvotes by the creator" do
|
2022-11-25 15:06:54 -05:00
|
|
|
user = create(:user)
|
|
|
|
post = create(:post)
|
|
|
|
c1 = create(:comment, post: post)
|
2016-11-06 02:20:10 -05:00
|
|
|
|
2022-03-28 15:17:34 -04:00
|
|
|
exception = assert_raises(ActiveRecord::RecordInvalid) { VoteManager.comment_vote!(user: user, comment: c1, score: 1) }
|
2022-04-30 06:48:53 -04:00
|
|
|
assert_equal("Validation failed: You cannot vote on your own comments", exception.message)
|
|
|
|
end
|
2022-05-16 08:20:11 -04:00
|
|
|
|
2022-04-30 06:48:53 -04:00
|
|
|
should "not allow downvotes by the creator" do
|
2022-11-25 15:06:54 -05:00
|
|
|
user = create(:user)
|
|
|
|
post = create(:post)
|
|
|
|
c1 = create(:comment, post: post)
|
2022-04-30 06:48:53 -04:00
|
|
|
|
|
|
|
exception = assert_raises(ActiveRecord::RecordInvalid) { VoteManager.comment_vote!(user: user, comment: c1, score: -1) }
|
|
|
|
assert_equal("Validation failed: You cannot vote on your own comments", exception.message)
|
2016-11-06 02:20:10 -05:00
|
|
|
end
|
|
|
|
|
2022-05-16 08:42:44 -04:00
|
|
|
should "not allow votes on sticky comments" do
|
2022-11-25 15:06:54 -05:00
|
|
|
user = create(:user)
|
|
|
|
post = create(:post)
|
|
|
|
c1 = create(:comment, post: post, is_sticky: true)
|
2022-05-16 08:42:44 -04:00
|
|
|
|
|
|
|
exception = assert_raises(ActiveRecord::RecordInvalid) { VoteManager.comment_vote!(user: user, comment: c1, score: -1) }
|
|
|
|
assert_match(/You cannot vote on sticky comments/, exception.message)
|
|
|
|
end
|
|
|
|
|
2013-06-29 15:10:09 -04:00
|
|
|
should "allow undoing of votes" do
|
2022-11-25 15:06:54 -05:00
|
|
|
user = create(:user)
|
|
|
|
user2 = create(:user)
|
|
|
|
post = create(:post)
|
|
|
|
comment = create(:comment, post: post)
|
2022-11-26 09:20:15 -05:00
|
|
|
as(user2) do
|
2022-03-28 15:17:34 -04:00
|
|
|
VoteManager.comment_vote!(user: user2, comment: comment, score: 1)
|
|
|
|
comment.reload
|
|
|
|
assert_equal(1, comment.score)
|
|
|
|
VoteManager.comment_unvote!(user: user2, comment: comment)
|
2013-06-29 15:10:09 -04:00
|
|
|
comment.reload
|
|
|
|
assert_equal(0, comment.score)
|
2022-03-28 15:17:34 -04:00
|
|
|
assert_nothing_raised { VoteManager.comment_vote!(user: user2, comment: comment, score: -1) }
|
2013-06-29 15:10:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-11 18:04:50 -04:00
|
|
|
should "be searchable" do
|
2022-11-25 15:06:54 -05:00
|
|
|
c1 = create(:comment, body: "aaa bbb ccc")
|
|
|
|
c2 = create(:comment, body: "aaa ddd")
|
|
|
|
c3 = create(:comment, body: "eee")
|
2012-05-11 18:04:50 -04:00
|
|
|
|
2018-08-31 20:23:25 -04:00
|
|
|
matches = Comment.search(body_matches: "aaa")
|
2012-05-11 18:04:50 -04:00
|
|
|
assert_equal(2, matches.count)
|
|
|
|
assert_equal(c2.id, matches.all[0].id)
|
|
|
|
assert_equal(c1.id, matches.all[1].id)
|
|
|
|
end
|
2016-12-27 00:37:16 -05:00
|
|
|
|
2017-05-07 11:02:15 -04:00
|
|
|
should "default to id_desc order when searched with no options specified" do
|
2022-11-25 15:06:54 -05:00
|
|
|
comms = create_list(:comment, 3)
|
2017-05-07 11:02:15 -04:00
|
|
|
matches = Comment.search({})
|
|
|
|
|
|
|
|
assert_equal([comms[2].id, comms[1].id, comms[0].id], matches.map(&:id))
|
|
|
|
end
|
|
|
|
|
2017-01-03 19:02:36 -05:00
|
|
|
context "that is edited by a moderator" do
|
|
|
|
setup do
|
2022-11-25 15:06:54 -05:00
|
|
|
@post = create(:post)
|
|
|
|
@comment = create(:comment, post_id: @post.id)
|
|
|
|
@mod = create(:moderator_user)
|
2017-01-03 19:02:36 -05:00
|
|
|
CurrentUser.user = @mod
|
|
|
|
end
|
|
|
|
|
|
|
|
should "create a mod action" do
|
|
|
|
assert_difference("ModAction.count") do
|
2023-12-04 13:41:29 -05:00
|
|
|
@comment.update(body: "nope")
|
2017-01-03 19:02:36 -05:00
|
|
|
end
|
|
|
|
end
|
2017-12-02 20:10:38 -05:00
|
|
|
|
|
|
|
should "credit the moderator as the updater" do
|
2018-04-02 13:51:26 -04:00
|
|
|
@comment.update(body: "test")
|
2017-12-02 20:10:38 -05:00
|
|
|
assert_equal(@mod.id, @comment.updater_id)
|
|
|
|
end
|
2017-01-03 19:02:36 -05:00
|
|
|
end
|
|
|
|
|
2023-12-04 13:41:29 -05:00
|
|
|
context "that is hidden by a moderator" do
|
|
|
|
setup do
|
|
|
|
@comment = create(:comment)
|
|
|
|
@mod = create(:moderator_user)
|
|
|
|
CurrentUser.user = @mod
|
|
|
|
end
|
|
|
|
|
|
|
|
should "create a mod action" do
|
|
|
|
assert_difference(-> { ModAction.count }, 1) do
|
|
|
|
@comment.update(is_hidden: true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
should "credit the moderator as the updater" do
|
|
|
|
@comment.update(is_hidden: true)
|
|
|
|
assert_equal(@mod.id, @comment.updater_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "that is stickied by a moderator" do
|
|
|
|
setup do
|
|
|
|
@comment = create(:comment)
|
|
|
|
@mod = create(:moderator_user)
|
|
|
|
CurrentUser.user = @mod
|
|
|
|
end
|
|
|
|
|
|
|
|
should "create a mod action" do
|
|
|
|
assert_difference(-> { ModAction.count }, 1) do
|
|
|
|
@comment.update(is_sticky: true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
should "credit the moderator as the updater" do
|
|
|
|
@comment.update(is_sticky: true)
|
|
|
|
assert_equal(@mod.id, @comment.updater_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "that is deleted" do
|
|
|
|
setup do
|
|
|
|
@comment = create(:comment)
|
|
|
|
end
|
|
|
|
|
|
|
|
should "create a mod action" do
|
|
|
|
assert_difference(-> { ModAction.count }, 1) do
|
|
|
|
@comment.destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-27 00:37:16 -05:00
|
|
|
context "that is below the score threshold" do
|
|
|
|
should "be hidden if not stickied" do
|
2022-11-25 15:06:54 -05:00
|
|
|
user = create(:user, comment_threshold: 0)
|
|
|
|
post = create(:post)
|
|
|
|
comment = create(:comment, post: post, score: -5)
|
2016-12-27 00:37:16 -05:00
|
|
|
|
|
|
|
assert_equal([comment], post.comments.hidden(user))
|
|
|
|
assert_equal([], post.comments.visible(user))
|
|
|
|
end
|
|
|
|
|
|
|
|
should "be visible if stickied" do
|
2022-11-25 15:06:54 -05:00
|
|
|
user = create(:user, comment_threshold: 0)
|
|
|
|
post = create(:post)
|
|
|
|
comment = create(:comment, post: post, score: -5, is_sticky: true)
|
2016-12-27 00:37:16 -05:00
|
|
|
|
|
|
|
assert_equal([], post.comments.hidden(user))
|
|
|
|
assert_equal([comment], post.comments.visible(user))
|
|
|
|
end
|
|
|
|
end
|
2017-04-24 22:14:36 -04:00
|
|
|
|
2022-05-16 08:20:11 -04:00
|
|
|
context "on a comment locked post" do
|
|
|
|
setup do
|
2023-09-05 11:27:05 -04:00
|
|
|
@post = create(:post, is_comment_locked: true)
|
2022-05-16 08:20:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
should "prevent new comments" do
|
2022-11-25 15:06:54 -05:00
|
|
|
comment = build(:comment, post: @post)
|
2022-05-16 08:20:11 -04:00
|
|
|
comment.save
|
2023-09-05 11:27:05 -04:00
|
|
|
assert_equal(["Post has comments locked"], comment.errors.full_messages)
|
2022-05-16 08:20:11 -04:00
|
|
|
end
|
|
|
|
end
|
2024-10-28 16:56:22 -04:00
|
|
|
|
|
|
|
context "on a comment disabled post" do
|
|
|
|
setup do
|
|
|
|
@post = create(:post, is_comment_disabled: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
should "prevent new comments" do
|
|
|
|
comment = build(:comment, post: @post)
|
|
|
|
comment.save
|
|
|
|
assert_equal(["Post has comments disabled"], comment.errors.full_messages)
|
|
|
|
end
|
|
|
|
end
|
2010-02-15 13:59:58 -05:00
|
|
|
end
|
2018-09-20 20:13:31 -04:00
|
|
|
|
|
|
|
context "during validation" do
|
2022-11-25 15:06:54 -05:00
|
|
|
subject { build(:comment) }
|
2018-09-20 20:13:31 -04:00
|
|
|
should_not allow_value(" ").for(:body)
|
|
|
|
end
|
2010-02-15 13:59:58 -05:00
|
|
|
end
|
|
|
|
end
|