mirror of
https://github.com/discourse/discourse.git
synced 2024-11-29 22:14:47 +08:00
2404fa7a23
Splits the `ToggleTopicClosed` job into two distinct `OpenTopic` and `CloseTopic` jobs to make the code clearer. The old job cannot be deleted yet because of outstanding sidekiq schedules, so a todo has been added to do so later this year. Also replaced mentions of `topic_status_update` with `topic_timer` in some files, because the `topic_status_update` model is obsolete and replaced by topic timer. Added some shortcut methods for checking if a topic is open/whether a user can change an open topic.
100 lines
2.7 KiB
Ruby
100 lines
2.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'rails_helper'
|
|
|
|
describe Jobs::CloseTopic do
|
|
fab!(:admin) { Fabricate(:admin) }
|
|
|
|
fab!(:topic) do
|
|
Fabricate(:topic_timer, user: admin).topic
|
|
end
|
|
|
|
it 'should be able to close a topic' do
|
|
freeze_time(61.minutes.from_now) do
|
|
described_class.new.execute(
|
|
topic_timer_id: topic.public_topic_timer.id,
|
|
state: true
|
|
)
|
|
|
|
expect(topic.reload.closed).to eq(true)
|
|
|
|
expect(Post.last.raw).to eq(I18n.t(
|
|
'topic_statuses.autoclosed_enabled_minutes', count: 61
|
|
))
|
|
end
|
|
end
|
|
|
|
describe 'when trying to close a topic that has already been closed' do
|
|
it 'should delete the topic timer' do
|
|
freeze_time(topic.public_topic_timer.execute_at + 1.minute)
|
|
|
|
topic.update!(closed: true)
|
|
|
|
expect do
|
|
described_class.new.execute(
|
|
topic_timer_id: topic.public_topic_timer.id,
|
|
state: true
|
|
)
|
|
end.to change { TopicTimer.exists?(topic_id: topic.id) }.from(true).to(false)
|
|
end
|
|
end
|
|
|
|
describe 'when trying to close a topic that has been deleted' do
|
|
it 'should delete the topic timer' do
|
|
freeze_time(topic.public_topic_timer.execute_at + 1.minute)
|
|
|
|
topic.trash!
|
|
|
|
expect do
|
|
described_class.new.execute(
|
|
topic_timer_id: topic.public_topic_timer.id,
|
|
state: true
|
|
)
|
|
end.to change { TopicTimer.exists?(topic_id: topic.id) }.from(true).to(false)
|
|
end
|
|
end
|
|
|
|
describe 'when user is no longer authorized to close topics' do
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:topic) do
|
|
Fabricate(:topic_timer, user: user).topic
|
|
end
|
|
|
|
it 'should destroy the topic timer' do
|
|
freeze_time(topic.public_topic_timer.execute_at + 1.minute)
|
|
|
|
expect do
|
|
described_class.new.execute(
|
|
topic_timer_id: topic.public_topic_timer.id,
|
|
state: true
|
|
)
|
|
end.to change { TopicTimer.exists?(topic_id: topic.id) }.from(true).to(false)
|
|
|
|
expect(topic.reload.closed).to eq(false)
|
|
end
|
|
|
|
it "should reconfigure topic timer if category's topics are set to autoclose" do
|
|
category = Fabricate(:category,
|
|
auto_close_based_on_last_post: true,
|
|
auto_close_hours: 5
|
|
)
|
|
|
|
topic = Fabricate(:topic, category: category)
|
|
topic.public_topic_timer.update!(user: user)
|
|
|
|
freeze_time(topic.public_topic_timer.execute_at + 1.minute)
|
|
|
|
expect do
|
|
described_class.new.execute(
|
|
topic_timer_id: topic.public_topic_timer.id,
|
|
state: true
|
|
)
|
|
end.to change { topic.reload.public_topic_timer.user }.from(user).to(Discourse.system_user)
|
|
.and change { topic.public_topic_timer.id }
|
|
|
|
expect(topic.reload.closed).to eq(false)
|
|
end
|
|
end
|
|
end
|