2014-01-30 11:43:01 -05:00
|
|
|
require 'spec_helper'
|
2014-01-30 12:44:28 -05:00
|
|
|
require_dependency 'topics_bulk_action'
|
2014-01-30 11:43:01 -05:00
|
|
|
|
|
|
|
describe TopicsBulkAction do
|
|
|
|
|
2014-08-08 02:29:51 -04:00
|
|
|
describe "dismiss_posts" do
|
|
|
|
it "dismisses posts" do
|
|
|
|
post1 = create_post
|
|
|
|
create_post(topic_id: post1.topic_id)
|
|
|
|
|
|
|
|
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)
|
|
|
|
tu.last_read_post_number.should == 2
|
|
|
|
tu.seen_post_count = 2
|
|
|
|
end
|
|
|
|
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')
|
|
|
|
-> { tba.perform! }.should raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "change_category" do
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
2014-01-30 12:44:28 -05:00
|
|
|
let(:category) { Fabricate(:category) }
|
2014-01-30 11:43:01 -05:00
|
|
|
|
|
|
|
context "when the user can edit the topic" do
|
|
|
|
it "changes the category and returns the topic_id" do
|
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!
|
|
|
|
topic_ids.should == [topic.id]
|
|
|
|
topic.reload
|
|
|
|
topic.category.should == category
|
|
|
|
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!
|
|
|
|
topic_ids.should == []
|
|
|
|
topic.reload
|
|
|
|
topic.category.should_not == category
|
|
|
|
end
|
|
|
|
end
|
2014-01-30 12:44:28 -05:00
|
|
|
end
|
|
|
|
|
2014-02-21 13:03:50 -05: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-11 15:13:44 -04:00
|
|
|
describe "delete" do
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
let(:moderator) { Fabricate(:moderator) }
|
|
|
|
|
|
|
|
it "deletes the topic" do
|
|
|
|
tba = TopicsBulkAction.new(moderator, [topic.id], type: 'delete')
|
|
|
|
tba.perform!
|
|
|
|
topic.reload
|
|
|
|
topic.should be_trashed
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-14 17:38:55 -05:00
|
|
|
describe "change_notification_level" do
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
|
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!
|
|
|
|
topic_ids.should == [topic.id]
|
|
|
|
TopicUser.get(topic, topic.user).notification_level.should == 2
|
|
|
|
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!
|
|
|
|
topic_ids.should == []
|
|
|
|
TopicUser.get(topic, topic.user).should be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-30 12:44:28 -05:00
|
|
|
describe "close" do
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
2014-01-30 11:43:01 -05:00
|
|
|
|
2014-01-30 12:44:28 -05: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!
|
|
|
|
topic_ids.should == [topic.id]
|
|
|
|
topic.reload
|
|
|
|
topic.should be_closed
|
|
|
|
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!
|
|
|
|
topic_ids.should be_blank
|
|
|
|
topic.reload
|
|
|
|
topic.should_not be_closed
|
|
|
|
end
|
|
|
|
end
|
2014-01-30 11:43:01 -05:00
|
|
|
end
|
|
|
|
end
|