2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2014-01-31 01:44:28 +08:00
|
|
|
require_dependency 'topics_bulk_action'
|
2014-01-31 00:43:01 +08:00
|
|
|
|
|
|
|
describe TopicsBulkAction do
|
|
|
|
|
2014-08-08 14:29:51 +08:00
|
|
|
describe "dismiss_posts" do
|
|
|
|
it "dismisses posts" do
|
|
|
|
post1 = create_post
|
2014-10-31 06:40:35 +08:00
|
|
|
p = create_post(topic_id: post1.topic_id)
|
2014-08-08 14:29:51 +08:00
|
|
|
create_post(topic_id: post1.topic_id)
|
|
|
|
|
2014-10-31 06:40:35 +08:00
|
|
|
PostDestroyer.new(Fabricate(:admin), p).destroy
|
|
|
|
|
2014-08-08 14:29:51 +08: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-31 06:40:35 +08:00
|
|
|
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(tu.last_read_post_number).to eq(3)
|
|
|
|
expect(tu.highest_seen_post_number).to eq(3)
|
2014-08-08 14:29:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-31 00:43:01 +08:00
|
|
|
describe "invalid operation" do
|
2014-01-31 01:44:28 +08:00
|
|
|
let(:user) { Fabricate.build(:user) }
|
2014-01-31 00:43:01 +08:00
|
|
|
|
|
|
|
it "raises an error with an invalid operation" do
|
|
|
|
tba = TopicsBulkAction.new(user, [1], type: 'rm_root')
|
2015-01-10 00:34:37 +08:00
|
|
|
expect { tba.perform! }.to raise_error(Discourse::InvalidParameters)
|
2014-01-31 00:43:01 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "change_category" do
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
2014-01-31 01:44:28 +08:00
|
|
|
let(:category) { Fabricate(:category) }
|
2014-01-31 00:43:01 +08:00
|
|
|
|
|
|
|
context "when the user can edit the topic" do
|
|
|
|
it "changes the category and returns the topic_id" do
|
2014-07-17 03:39:39 +08:00
|
|
|
tba = TopicsBulkAction.new(topic.user, [topic.id], type: 'change_category', category_id: category.id)
|
2014-01-31 00:43:01 +08:00
|
|
|
topic_ids = tba.perform!
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(topic_ids).to eq([topic.id])
|
2014-01-31 00:43:01 +08:00
|
|
|
topic.reload
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(topic.category).to eq(category)
|
2014-01-31 00:43:01 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the user can't edit the topic" do
|
2014-02-22 02:03:50 +08:00
|
|
|
it "doesn't change the category" do
|
2014-01-31 00:43:01 +08:00
|
|
|
Guardian.any_instance.expects(:can_edit?).returns(false)
|
2014-07-17 03:39:39 +08:00
|
|
|
tba = TopicsBulkAction.new(topic.user, [topic.id], type: 'change_category', category_id: category.id)
|
2014-01-31 00:43:01 +08:00
|
|
|
topic_ids = tba.perform!
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(topic_ids).to eq([])
|
2014-01-31 00:43:01 +08:00
|
|
|
topic.reload
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(topic.category).not_to eq(category)
|
2014-01-31 00:43:01 +08:00
|
|
|
end
|
|
|
|
end
|
2014-01-31 01:44:28 +08:00
|
|
|
end
|
|
|
|
|
2014-02-22 02:03:50 +08:00
|
|
|
describe "reset_read" do
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
|
|
|
|
it "delegates to PostTiming.destroy_for" do
|
|
|
|
tba = TopicsBulkAction.new(topic.user, [topic.id], type: 'reset_read')
|
|
|
|
PostTiming.expects(:destroy_for).with(topic.user_id, [topic.id])
|
|
|
|
topic_ids = tba.perform!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-12 03:13:44 +08:00
|
|
|
describe "delete" do
|
2017-09-28 20:22:49 +08:00
|
|
|
let(:topic) { Fabricate(:post).topic }
|
2014-08-12 03:13:44 +08:00
|
|
|
let(:moderator) { Fabricate(:moderator) }
|
|
|
|
|
|
|
|
it "deletes the topic" do
|
|
|
|
tba = TopicsBulkAction.new(moderator, [topic.id], type: 'delete')
|
|
|
|
tba.perform!
|
|
|
|
topic.reload
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(topic).to be_trashed
|
2014-08-12 03:13:44 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-15 06:38:55 +08:00
|
|
|
describe "change_notification_level" do
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
|
2014-02-22 02:03:50 +08:00
|
|
|
context "when the user can see the topic" do
|
2014-02-15 06:38:55 +08: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-10 00:34:37 +08:00
|
|
|
expect(topic_ids).to eq([topic.id])
|
|
|
|
expect(TopicUser.get(topic, topic.user).notification_level).to eq(2)
|
2014-02-15 06:38:55 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-22 02:03:50 +08:00
|
|
|
context "when the user can't see the topic" do
|
2014-02-15 06:38:55 +08: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-10 00:34:37 +08:00
|
|
|
expect(topic_ids).to eq([])
|
|
|
|
expect(TopicUser.get(topic, topic.user)).to be_blank
|
2014-02-15 06:38:55 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-31 01:44:28 +08:00
|
|
|
describe "close" do
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
2014-01-31 00:43:01 +08:00
|
|
|
|
2014-01-31 01:44:28 +08:00
|
|
|
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-10 00:34:37 +08:00
|
|
|
expect(topic_ids).to eq([topic.id])
|
2014-01-31 01:44:28 +08:00
|
|
|
topic.reload
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(topic).to be_closed
|
2014-01-31 01:44:28 +08: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-10 00:34:37 +08:00
|
|
|
expect(topic_ids).to be_blank
|
2014-01-31 01:44:28 +08:00
|
|
|
topic.reload
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(topic).not_to be_closed
|
2014-01-31 01:44:28 +08:00
|
|
|
end
|
|
|
|
end
|
2014-01-31 00:43:01 +08:00
|
|
|
end
|
2014-09-23 02:56:48 +08:00
|
|
|
|
|
|
|
describe "archive" do
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
|
|
|
|
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-10 00:34:37 +08:00
|
|
|
expect(topic_ids).to eq([topic.id])
|
2014-09-23 02:56:48 +08:00
|
|
|
topic.reload
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(topic).to be_archived
|
2014-09-23 02:56:48 +08: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-10 00:34:37 +08:00
|
|
|
expect(topic_ids).to be_blank
|
2014-09-23 02:56:48 +08:00
|
|
|
topic.reload
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(topic).not_to be_archived
|
2014-09-23 02:56:48 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-10-28 04:57:40 +08:00
|
|
|
|
|
|
|
describe "unlist" do
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
|
|
|
|
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-05 02:02:47 +08:00
|
|
|
|
|
|
|
describe "change_tags" do
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
let(:tag1) { Fabricate(:tag) }
|
|
|
|
let(:tag2) { Fabricate(:tag) }
|
|
|
|
|
|
|
|
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-28 04:47:41 +08:00
|
|
|
expect(topic.tags.map(&:name)).to contain_exactly('newtag', tag1.name)
|
2016-05-05 02:02:47 +08: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-28 04:47:41 +08:00
|
|
|
expect(topic.tags.map(&:name)).to contain_exactly(tag1.name)
|
2016-05-05 02:02:47 +08: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 do
|
|
|
|
Guardian.any_instance.expects(:can_edit?).returns(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
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 15:46:42 +08:00
|
|
|
expect(topic.tags.map(&:name)).to contain_exactly(tag1.name, tag2.name)
|
2016-05-05 02:02:47 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-12-24 07:45:21 +08:00
|
|
|
|
|
|
|
describe "append tags" do
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
let(:tag1) { Fabricate(:tag) }
|
|
|
|
let(:tag2) { Fabricate(:tag) }
|
|
|
|
let(:tag3) { Fabricate(:tag) }
|
|
|
|
|
|
|
|
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-28 04:47:41 +08:00
|
|
|
expect(topic.tags.map(&:name)).to contain_exactly(tag1.name, tag2.name, tag3.name, 'newtag')
|
2016-12-24 07:45:21 +08: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-28 04:47:41 +08:00
|
|
|
expect(topic.tags.map(&:name)).to contain_exactly(tag1.name, tag2.name)
|
2016-12-24 07:45:21 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when the user can't create new topics" do
|
|
|
|
before do
|
|
|
|
SiteSetting.min_trust_to_create_tag = 4
|
|
|
|
end
|
|
|
|
|
|
|
|
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-28 04:47:41 +08:00
|
|
|
expect(topic.tags.map(&:name)).to contain_exactly(tag1.name, tag2.name, tag3.name)
|
2016-12-24 07:45:21 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when user can't edit topic" do
|
|
|
|
before do
|
|
|
|
Guardian.any_instance.expects(:can_edit?).returns(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
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-28 04:47:41 +08:00
|
|
|
expect(topic.tags.map(&:name)).to contain_exactly(tag1.name, tag2.name)
|
2016-12-24 07:45:21 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-01-31 00:43:01 +08:00
|
|
|
end
|