2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe TopicsBulkAction do
|
2019-05-06 05:01:18 -04:00
|
|
|
fab!(:topic) { Fabricate(:topic) }
|
2014-01-30 11:43:01 -05:00
|
|
|
|
2021-07-30 05:00:48 -04:00
|
|
|
describe "#dismiss_topics" do
|
2021-05-25 19:38:46 -04:00
|
|
|
fab!(:user) { Fabricate(:user, created_at: 1.days.ago) }
|
|
|
|
fab!(:category) { Fabricate(:category) }
|
|
|
|
fab!(:topic2) { Fabricate(:topic, category: category, created_at: 60.minutes.ago) }
|
|
|
|
fab!(:topic3) { Fabricate(:topic, category: category, created_at: 120.minutes.ago) }
|
|
|
|
|
|
|
|
before { topic.destroy! }
|
|
|
|
|
2021-07-30 05:00:48 -04:00
|
|
|
it "dismisses private messages" do
|
|
|
|
pm = Fabricate(:private_message_topic)
|
|
|
|
|
|
|
|
TopicsBulkAction.new(user, [pm.id], type: "dismiss_topics").perform!
|
|
|
|
|
|
|
|
expect(DismissedTopicUser.exists?(topic: pm)).to eq(true)
|
|
|
|
end
|
|
|
|
|
2021-05-25 19:38:46 -04:00
|
|
|
it "dismisses two topics" do
|
|
|
|
expect {
|
|
|
|
TopicsBulkAction.new(user, [Topic.all.pluck(:id)], type: "dismiss_topics").perform!
|
|
|
|
}.to change { DismissedTopicUser.count }.by(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns dismissed topic ids" do
|
|
|
|
expect(
|
|
|
|
TopicsBulkAction.new(user, [Topic.all.pluck(:id)], type: "dismiss_topics").perform!.sort,
|
|
|
|
).to match_array([topic2.id, topic3.id])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "respects max_new_topics limit" do
|
|
|
|
SiteSetting.max_new_topics = 1
|
|
|
|
expect do
|
|
|
|
TopicsBulkAction.new(user, [Topic.all.pluck(:id)], type: "dismiss_topics").perform!
|
|
|
|
end.to change { DismissedTopicUser.count }.by(1)
|
|
|
|
|
|
|
|
dismissed_topic_user = DismissedTopicUser.last
|
|
|
|
|
|
|
|
expect(dismissed_topic_user.user_id).to eq(user.id)
|
|
|
|
expect(dismissed_topic_user.topic_id).to eq(topic2.id)
|
|
|
|
expect(dismissed_topic_user.created_at).not_to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "respects seen topics" do
|
|
|
|
Fabricate(:topic_user, user: user, topic: topic2, last_read_post_number: 1)
|
|
|
|
Fabricate(:topic_user, user: user, topic: topic3, last_read_post_number: 1)
|
|
|
|
expect do
|
|
|
|
TopicsBulkAction.new(user, [Topic.all.pluck(:id)], type: "dismiss_topics").perform!
|
2022-07-19 10:03:03 -04:00
|
|
|
end.not_to change { DismissedTopicUser.count }
|
2021-05-25 19:38:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "dismisses when topic user without last_read_post_number" do
|
|
|
|
Fabricate(:topic_user, user: user, topic: topic2, last_read_post_number: nil)
|
|
|
|
Fabricate(:topic_user, user: user, topic: topic3, last_read_post_number: nil)
|
|
|
|
expect do
|
|
|
|
TopicsBulkAction.new(user, [Topic.all.pluck(:id)], type: "dismiss_topics").perform!
|
|
|
|
end.to change { DismissedTopicUser.count }.by(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "respects new_topic_duration_minutes" do
|
|
|
|
user.user_option.update!(new_topic_duration_minutes: 70)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
TopicsBulkAction.new(user, [Topic.all.pluck(:id)], type: "dismiss_topics").perform!
|
|
|
|
end.to change { DismissedTopicUser.count }.by(1)
|
|
|
|
|
|
|
|
dismissed_topic_user = DismissedTopicUser.last
|
|
|
|
|
|
|
|
expect(dismissed_topic_user.user_id).to eq(user.id)
|
|
|
|
expect(dismissed_topic_user.topic_id).to eq(topic2.id)
|
|
|
|
expect(dismissed_topic_user.created_at).not_to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-08 02:29:51 -04:00
|
|
|
describe "dismiss_posts" do
|
|
|
|
it "dismisses posts" do
|
|
|
|
post1 = create_post
|
2014-10-30 18:40:35 -04:00
|
|
|
p = create_post(topic_id: post1.topic_id)
|
2014-08-08 02:29:51 -04:00
|
|
|
create_post(topic_id: post1.topic_id)
|
|
|
|
|
2014-10-30 18:40:35 -04:00
|
|
|
PostDestroyer.new(Fabricate(:admin), p).destroy
|
|
|
|
|
2014-08-08 02:29:51 -04:00
|
|
|
TopicsBulkAction.new(post1.user, [post1.topic_id], type: "dismiss_posts").perform!
|
|
|
|
|
|
|
|
tu = TopicUser.find_by(user_id: post1.user_id, topic_id: post1.topic_id)
|
2014-10-30 18:40:35 -04:00
|
|
|
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(tu.last_read_post_number).to eq(3)
|
2014-08-08 02:29:51 -04:00
|
|
|
end
|
2019-10-31 19:19:43 -04:00
|
|
|
|
|
|
|
context "when the user is staff" do
|
|
|
|
fab!(:user) { Fabricate(:admin) }
|
|
|
|
|
|
|
|
context "when the highest_staff_post_number is > highest_post_number for a topic (e.g. whisper is last post)" do
|
|
|
|
it "dismisses posts" do
|
2022-12-16 11:42:51 -05:00
|
|
|
SiteSetting.whispers_allowed_groups = "#{Group::AUTO_GROUPS[:staff]}"
|
2019-10-31 19:19:43 -04:00
|
|
|
post1 = create_post(user: user)
|
|
|
|
p = create_post(topic_id: post1.topic_id)
|
|
|
|
create_post(topic_id: post1.topic_id)
|
|
|
|
|
|
|
|
whisper =
|
|
|
|
PostCreator.new(
|
|
|
|
user,
|
|
|
|
topic_id: post1.topic.id,
|
|
|
|
post_type: Post.types[:whisper],
|
|
|
|
raw: "this is a whispered reply",
|
|
|
|
).create
|
|
|
|
|
|
|
|
TopicsBulkAction.new(user, [post1.topic_id], type: "dismiss_posts").perform!
|
|
|
|
|
|
|
|
tu = TopicUser.find_by(user_id: user.id, topic_id: post1.topic_id)
|
|
|
|
|
|
|
|
expect(tu.last_read_post_number).to eq(4)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-08-08 02:29:51 -04:00
|
|
|
end
|
|
|
|
|
2014-01-30 11:43:01 -05:00
|
|
|
describe "invalid operation" do
|
2014-01-30 12:44:28 -05:00
|
|
|
let(:user) { Fabricate.build(:user) }
|
2014-01-30 11:43:01 -05:00
|
|
|
|
|
|
|
it "raises an error with an invalid operation" do
|
|
|
|
tba = TopicsBulkAction.new(user, [1], type: "rm_root")
|
2015-01-09 11:34:37 -05:00
|
|
|
expect { tba.perform! }.to raise_error(Discourse::InvalidParameters)
|
2014-01-30 11:43:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "change_category" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:category) { Fabricate(:category) }
|
2020-11-12 07:57:12 -05:00
|
|
|
fab!(:fist_post) { Fabricate(:post, topic: topic) }
|
2014-01-30 11:43:01 -05:00
|
|
|
|
|
|
|
context "when the user can edit the topic" do
|
2020-11-12 07:57:12 -05:00
|
|
|
context "with 'create_revision_on_bulk_topic_moves' setting enabled" do
|
|
|
|
before { SiteSetting.create_revision_on_bulk_topic_moves = true }
|
|
|
|
|
|
|
|
it "changes the category, creates a post revision and returns the topic_id" do
|
|
|
|
old_category_id = topic.category_id
|
|
|
|
tba =
|
|
|
|
TopicsBulkAction.new(
|
|
|
|
topic.user,
|
|
|
|
[topic.id],
|
|
|
|
type: "change_category",
|
|
|
|
category_id: category.id,
|
|
|
|
)
|
|
|
|
topic_ids = tba.perform!
|
|
|
|
expect(topic_ids).to eq([topic.id])
|
|
|
|
topic.reload
|
|
|
|
expect(topic.category).to eq(category)
|
|
|
|
|
|
|
|
revision = topic.first_post.revisions.last
|
|
|
|
expect(revision).to be_present
|
|
|
|
expect(revision.modifications).to eq ({ "category_id" => [old_category_id, category.id] })
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't do anything when category stays the same" do
|
|
|
|
tba =
|
|
|
|
TopicsBulkAction.new(
|
|
|
|
topic.user,
|
|
|
|
[topic.id],
|
|
|
|
type: "change_category",
|
|
|
|
category_id: topic.category_id,
|
|
|
|
)
|
|
|
|
topic_ids = tba.perform!
|
|
|
|
expect(topic_ids).to be_empty
|
|
|
|
|
|
|
|
topic.reload
|
|
|
|
revision = topic.first_post.revisions.last
|
|
|
|
expect(revision).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with 'create_revision_on_bulk_topic_moves' setting disabled" do
|
|
|
|
before { SiteSetting.create_revision_on_bulk_topic_moves = false }
|
|
|
|
|
|
|
|
it "changes the category, doesn't create a post revision and returns the topic_id" do
|
|
|
|
tba =
|
|
|
|
TopicsBulkAction.new(
|
|
|
|
topic.user,
|
|
|
|
[topic.id],
|
|
|
|
type: "change_category",
|
|
|
|
category_id: category.id,
|
|
|
|
)
|
|
|
|
topic_ids = tba.perform!
|
|
|
|
expect(topic_ids).to eq([topic.id])
|
|
|
|
topic.reload
|
|
|
|
expect(topic.category).to eq(category)
|
|
|
|
|
|
|
|
revision = topic.first_post.revisions.last
|
|
|
|
expect(revision).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't do anything when category stays the same" do
|
|
|
|
tba =
|
|
|
|
TopicsBulkAction.new(
|
|
|
|
topic.user,
|
|
|
|
[topic.id],
|
|
|
|
type: "change_category",
|
|
|
|
category_id: topic.category_id,
|
|
|
|
)
|
|
|
|
topic_ids = tba.perform!
|
|
|
|
expect(topic_ids).to be_empty
|
|
|
|
end
|
2014-01-30 11:43:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the user can't edit the topic" do
|
2014-02-21 13:03:50 -05:00
|
|
|
it "doesn't change the category" do
|
2014-01-30 11:43:01 -05:00
|
|
|
Guardian.any_instance.expects(:can_edit?).returns(false)
|
2014-07-16 15:39:39 -04:00
|
|
|
tba =
|
|
|
|
TopicsBulkAction.new(
|
|
|
|
topic.user,
|
|
|
|
[topic.id],
|
|
|
|
type: "change_category",
|
|
|
|
category_id: category.id,
|
|
|
|
)
|
2014-01-30 11:43:01 -05:00
|
|
|
topic_ids = tba.perform!
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic_ids).to eq([])
|
2014-01-30 11:43:01 -05:00
|
|
|
topic.reload
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic.category).not_to eq(category)
|
2014-01-30 11:43:01 -05:00
|
|
|
end
|
|
|
|
end
|
2014-01-30 12:44:28 -05:00
|
|
|
end
|
|
|
|
|
2022-02-21 15:45:01 -05:00
|
|
|
describe "destroy_post_timing" do
|
|
|
|
fab!(:fist_post) { Fabricate(:post, topic: topic) }
|
|
|
|
|
|
|
|
before { PostTiming.process_timings(topic.user, topic.id, 10, [[1, 10]]) }
|
|
|
|
|
2014-02-21 13:03:50 -05:00
|
|
|
it "delegates to PostTiming.destroy_for" do
|
2022-02-21 15:45:01 -05:00
|
|
|
tba = TopicsBulkAction.new(topic.user, [topic.id], type: "destroy_post_timing")
|
|
|
|
topic_ids = nil
|
|
|
|
expect { topic_ids = tba.perform! }.to change { PostTiming.count }.by(-1)
|
|
|
|
expect(topic_ids).to contain_exactly(topic.id)
|
2014-02-21 13:03:50 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-11 15:13:44 -04:00
|
|
|
describe "delete" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:topic) { Fabricate(:post).topic }
|
|
|
|
fab!(:moderator) { Fabricate(:moderator) }
|
2014-08-11 15:13:44 -04:00
|
|
|
|
|
|
|
it "deletes the topic" do
|
|
|
|
tba = TopicsBulkAction.new(moderator, [topic.id], type: "delete")
|
|
|
|
tba.perform!
|
|
|
|
topic.reload
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic).to be_trashed
|
2014-08-11 15:13:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-14 17:38:55 -05:00
|
|
|
describe "change_notification_level" do
|
2014-02-21 13:03:50 -05:00
|
|
|
context "when the user can see the topic" do
|
2014-02-14 17:38:55 -05:00
|
|
|
it "updates the notification level" do
|
|
|
|
tba =
|
|
|
|
TopicsBulkAction.new(
|
|
|
|
topic.user,
|
|
|
|
[topic.id],
|
|
|
|
type: "change_notification_level",
|
|
|
|
notification_level_id: 2,
|
|
|
|
)
|
|
|
|
topic_ids = tba.perform!
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic_ids).to eq([topic.id])
|
|
|
|
expect(TopicUser.get(topic, topic.user).notification_level).to eq(2)
|
2014-02-14 17:38:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-21 13:03:50 -05:00
|
|
|
context "when the user can't see the topic" do
|
2014-02-14 17:38:55 -05:00
|
|
|
it "doesn't change the level" do
|
|
|
|
Guardian.any_instance.expects(:can_see?).returns(false)
|
|
|
|
tba =
|
|
|
|
TopicsBulkAction.new(
|
|
|
|
topic.user,
|
|
|
|
[topic.id],
|
|
|
|
type: "change_notification_level",
|
|
|
|
notification_level_id: 2,
|
|
|
|
)
|
|
|
|
topic_ids = tba.perform!
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic_ids).to eq([])
|
|
|
|
expect(TopicUser.get(topic, topic.user)).to be_blank
|
2014-02-14 17:38:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-30 12:44:28 -05:00
|
|
|
describe "close" do
|
|
|
|
context "when the user can moderate the topic" do
|
|
|
|
it "closes the topic and returns the topic_id" do
|
|
|
|
Guardian.any_instance.expects(:can_moderate?).returns(true)
|
|
|
|
Guardian.any_instance.expects(:can_create?).returns(true)
|
|
|
|
tba = TopicsBulkAction.new(topic.user, [topic.id], type: "close")
|
|
|
|
topic_ids = tba.perform!
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic_ids).to eq([topic.id])
|
2014-01-30 12:44:28 -05:00
|
|
|
topic.reload
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic).to be_closed
|
2014-01-30 12:44:28 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the user can't edit the topic" do
|
|
|
|
it "doesn't close the topic" do
|
|
|
|
Guardian.any_instance.expects(:can_moderate?).returns(false)
|
|
|
|
tba = TopicsBulkAction.new(topic.user, [topic.id], type: "close")
|
|
|
|
topic_ids = tba.perform!
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic_ids).to be_blank
|
2014-01-30 12:44:28 -05:00
|
|
|
topic.reload
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic).not_to be_closed
|
2014-01-30 12:44:28 -05:00
|
|
|
end
|
|
|
|
end
|
2014-01-30 11:43:01 -05:00
|
|
|
end
|
2014-09-22 14:56:48 -04:00
|
|
|
|
|
|
|
describe "archive" do
|
|
|
|
context "when the user can moderate the topic" do
|
|
|
|
it "archives the topic and returns the topic_id" do
|
|
|
|
Guardian.any_instance.expects(:can_moderate?).returns(true)
|
|
|
|
Guardian.any_instance.expects(:can_create?).returns(true)
|
|
|
|
tba = TopicsBulkAction.new(topic.user, [topic.id], type: "archive")
|
|
|
|
topic_ids = tba.perform!
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic_ids).to eq([topic.id])
|
2014-09-22 14:56:48 -04:00
|
|
|
topic.reload
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic).to be_archived
|
2014-09-22 14:56:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the user can't edit the topic" do
|
|
|
|
it "doesn't archive the topic" do
|
|
|
|
Guardian.any_instance.expects(:can_moderate?).returns(false)
|
|
|
|
tba = TopicsBulkAction.new(topic.user, [topic.id], type: "archive")
|
|
|
|
topic_ids = tba.perform!
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic_ids).to be_blank
|
2014-09-22 14:56:48 -04:00
|
|
|
topic.reload
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic).not_to be_archived
|
2014-09-22 14:56:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-10-27 16:57:40 -04:00
|
|
|
|
|
|
|
describe "unlist" do
|
|
|
|
context "when the user can moderate the topic" do
|
|
|
|
it "unlists the topic and returns the topic_id" do
|
|
|
|
Guardian.any_instance.expects(:can_moderate?).returns(true)
|
|
|
|
Guardian.any_instance.expects(:can_create?).returns(true)
|
|
|
|
tba = TopicsBulkAction.new(topic.user, [topic.id], type: "unlist")
|
|
|
|
topic_ids = tba.perform!
|
|
|
|
expect(topic_ids).to eq([topic.id])
|
|
|
|
topic.reload
|
|
|
|
expect(topic).not_to be_visible
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the user can't edit the topic" do
|
|
|
|
it "doesn't unlist the topic" do
|
|
|
|
Guardian.any_instance.expects(:can_moderate?).returns(false)
|
|
|
|
tba = TopicsBulkAction.new(topic.user, [topic.id], type: "unlist")
|
|
|
|
topic_ids = tba.perform!
|
|
|
|
expect(topic_ids).to be_blank
|
|
|
|
topic.reload
|
|
|
|
expect(topic).to be_visible
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-05-04 14:02:47 -04:00
|
|
|
|
2022-05-22 12:32:55 -04:00
|
|
|
describe "reset_bump_dates" do
|
|
|
|
context "when the user can update bumped at" do
|
|
|
|
it "does reset the topic bump date" do
|
|
|
|
post_created_at = 1.day.ago
|
|
|
|
create_post(topic_id: topic.id, created_at: post_created_at)
|
|
|
|
topic.update!(bumped_at: 1.hour.ago)
|
|
|
|
Guardian.any_instance.expects(:can_update_bumped_at?).returns(true)
|
|
|
|
tba = TopicsBulkAction.new(topic.user, [topic.id], type: "reset_bump_dates")
|
|
|
|
topic_ids = tba.perform!
|
|
|
|
expect(topic_ids).to eq([topic.id])
|
|
|
|
expect(topic.reload.bumped_at).to eq_time(post_created_at)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the user can't update bumped at" do
|
|
|
|
it "doesn't reset the topic bump date" do
|
|
|
|
create_post(topic_id: topic.id, created_at: 1.day.ago)
|
|
|
|
bumped_at = 1.hour.ago
|
|
|
|
topic.update!(bumped_at: bumped_at)
|
|
|
|
Guardian.any_instance.expects(:can_update_bumped_at?).returns(false)
|
|
|
|
tba = TopicsBulkAction.new(topic.user, [topic.id], type: "reset_bump_dates")
|
|
|
|
topic_ids = tba.perform!
|
|
|
|
expect(topic_ids).to eq([])
|
|
|
|
expect(topic.reload.bumped_at).to eq_time(bumped_at)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-04 14:02:47 -04:00
|
|
|
describe "change_tags" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:tag1) { Fabricate(:tag) }
|
|
|
|
fab!(:tag2) { Fabricate(:tag) }
|
2016-05-04 14:02:47 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.tagging_enabled = true
|
|
|
|
SiteSetting.min_trust_level_to_tag_topics = 0
|
|
|
|
topic.tags = [tag1, tag2]
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can change the tags, and can create new tags" do
|
|
|
|
SiteSetting.min_trust_to_create_tag = 0
|
|
|
|
tba =
|
|
|
|
TopicsBulkAction.new(
|
|
|
|
topic.user,
|
|
|
|
[topic.id],
|
|
|
|
type: "change_tags",
|
|
|
|
tags: ["newtag", tag1.name],
|
|
|
|
)
|
|
|
|
topic_ids = tba.perform!
|
|
|
|
expect(topic_ids).to eq([topic.id])
|
|
|
|
topic.reload
|
2017-03-27 16:47:41 -04:00
|
|
|
expect(topic.tags.map(&:name)).to contain_exactly("newtag", tag1.name)
|
2016-05-04 14:02:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can change the tags but not create new ones" do
|
|
|
|
SiteSetting.min_trust_to_create_tag = 4
|
|
|
|
tba =
|
|
|
|
TopicsBulkAction.new(
|
|
|
|
topic.user,
|
|
|
|
[topic.id],
|
|
|
|
type: "change_tags",
|
|
|
|
tags: ["newtag", tag1.name],
|
|
|
|
)
|
|
|
|
topic_ids = tba.perform!
|
|
|
|
expect(topic_ids).to eq([topic.id])
|
|
|
|
topic.reload
|
2017-03-27 16:47:41 -04:00
|
|
|
expect(topic.tags.map(&:name)).to contain_exactly(tag1.name)
|
2016-05-04 14:02:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can remove all tags" do
|
|
|
|
tba = TopicsBulkAction.new(topic.user, [topic.id], type: "change_tags", tags: [])
|
|
|
|
topic_ids = tba.perform!
|
|
|
|
expect(topic_ids).to eq([topic.id])
|
|
|
|
topic.reload
|
|
|
|
expect(topic.tags.size).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when user can't edit topic" do
|
|
|
|
before { Guardian.any_instance.expects(:can_edit?).returns(false) }
|
|
|
|
|
|
|
|
it "doesn't change the tags" do
|
|
|
|
tba =
|
|
|
|
TopicsBulkAction.new(
|
|
|
|
topic.user,
|
|
|
|
[topic.id],
|
|
|
|
type: "change_tags",
|
|
|
|
tags: ["newtag", tag1.name],
|
|
|
|
)
|
|
|
|
topic_ids = tba.perform!
|
|
|
|
expect(topic_ids).to eq([])
|
|
|
|
topic.reload
|
2017-03-24 03:46:42 -04:00
|
|
|
expect(topic.tags.map(&:name)).to contain_exactly(tag1.name, tag2.name)
|
2016-05-04 14:02:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-12-23 18:45:21 -05:00
|
|
|
|
|
|
|
describe "append tags" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:tag1) { Fabricate(:tag) }
|
|
|
|
fab!(:tag2) { Fabricate(:tag) }
|
|
|
|
fab!(:tag3) { Fabricate(:tag) }
|
2016-12-23 18:45:21 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.tagging_enabled = true
|
|
|
|
SiteSetting.min_trust_level_to_tag_topics = 0
|
|
|
|
topic.tags = [tag1, tag2]
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can append new or existing tags" do
|
|
|
|
SiteSetting.min_trust_to_create_tag = 0
|
|
|
|
tba =
|
|
|
|
TopicsBulkAction.new(
|
|
|
|
topic.user,
|
|
|
|
[topic.id],
|
|
|
|
type: "append_tags",
|
|
|
|
tags: [tag1.name, tag3.name, "newtag"],
|
|
|
|
)
|
|
|
|
topic_ids = tba.perform!
|
|
|
|
expect(topic_ids).to eq([topic.id])
|
|
|
|
topic.reload
|
2017-03-27 16:47:41 -04:00
|
|
|
expect(topic.tags.map(&:name)).to contain_exactly(tag1.name, tag2.name, tag3.name, "newtag")
|
2016-12-23 18:45:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can append empty tags" do
|
|
|
|
tba = TopicsBulkAction.new(topic.user, [topic.id], type: "append_tags", tags: [])
|
|
|
|
topic_ids = tba.perform!
|
|
|
|
expect(topic_ids).to eq([topic.id])
|
|
|
|
topic.reload
|
2017-03-27 16:47:41 -04:00
|
|
|
expect(topic.tags.map(&:name)).to contain_exactly(tag1.name, tag2.name)
|
2016-12-23 18:45:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when the user can't create new topics" do
|
|
|
|
before { SiteSetting.min_trust_to_create_tag = 4 }
|
|
|
|
|
|
|
|
it "can append existing tags but doesn't append new tags" do
|
|
|
|
tba =
|
|
|
|
TopicsBulkAction.new(
|
|
|
|
topic.user,
|
|
|
|
[topic.id],
|
|
|
|
type: "append_tags",
|
|
|
|
tags: [tag3.name, "newtag"],
|
|
|
|
)
|
|
|
|
topic_ids = tba.perform!
|
|
|
|
expect(topic_ids).to eq([topic.id])
|
|
|
|
topic.reload
|
2017-03-27 16:47:41 -04:00
|
|
|
expect(topic.tags.map(&:name)).to contain_exactly(tag1.name, tag2.name, tag3.name)
|
2016-12-23 18:45:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when user can't edit topic" do
|
|
|
|
before { Guardian.any_instance.expects(:can_edit?).returns(false) }
|
|
|
|
|
|
|
|
it "doesn't change the tags" do
|
|
|
|
tba =
|
|
|
|
TopicsBulkAction.new(
|
|
|
|
topic.user,
|
|
|
|
[topic.id],
|
|
|
|
type: "append_tags",
|
|
|
|
tags: ["newtag", tag3.name],
|
|
|
|
)
|
|
|
|
topic_ids = tba.perform!
|
|
|
|
expect(topic_ids).to eq([])
|
|
|
|
topic.reload
|
2017-03-27 16:47:41 -04:00
|
|
|
expect(topic.tags.map(&:name)).to contain_exactly(tag1.name, tag2.name)
|
2016-12-23 18:45:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-10-06 13:11:55 -04:00
|
|
|
|
|
|
|
describe "remove_tags" do
|
|
|
|
fab!(:tag1) { Fabricate(:tag) }
|
|
|
|
fab!(:tag2) { Fabricate(:tag) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.tagging_enabled = true
|
|
|
|
SiteSetting.min_trust_level_to_tag_topics = 0
|
|
|
|
topic.tags = [tag1, tag2]
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can remove all tags" do
|
|
|
|
tba = TopicsBulkAction.new(topic.user, [topic.id], type: "remove_tags")
|
|
|
|
topic_ids = tba.perform!
|
|
|
|
expect(topic_ids).to eq([topic.id])
|
|
|
|
topic.reload
|
|
|
|
expect(topic.tags.size).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when user can't edit topic" do
|
|
|
|
before { Guardian.any_instance.expects(:can_edit?).returns(false) }
|
|
|
|
|
|
|
|
it "doesn't remove the tags" do
|
|
|
|
tba = TopicsBulkAction.new(topic.user, [topic.id], type: "remove_tags")
|
|
|
|
topic_ids = tba.perform!
|
|
|
|
expect(topic_ids).to eq([])
|
|
|
|
topic.reload
|
|
|
|
expect(topic.tags.map(&:name)).to contain_exactly(tag1.name, tag2.name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-01-30 11:43:01 -05:00
|
|
|
end
|