2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe Jobs::ToggleTopicClosed do
|
2023-11-09 16:47:59 -06:00
|
|
|
fab!(:admin)
|
2017-03-21 22:12:02 -05:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
fab!(:topic) { Fabricate(:topic_timer, user: admin).topic }
|
2017-03-21 22:12:02 -05:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "should be able to close a topic" do
|
2017-03-21 22:12:02 -05:00
|
|
|
topic
|
|
|
|
|
2018-06-05 02:29:17 -05:00
|
|
|
freeze_time(61.minutes.from_now) do
|
2023-01-09 05:18:21 -06:00
|
|
|
described_class.new.execute(topic_timer_id: topic.public_topic_timer.id, state: true)
|
2017-03-21 22:12:02 -05:00
|
|
|
|
|
|
|
expect(topic.reload.closed).to eq(true)
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(Post.last.raw).to eq(I18n.t("topic_statuses.autoclosed_enabled_minutes", count: 61))
|
2017-03-21 22:12:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "opening a topic" do
|
|
|
|
it "should be work" do
|
2018-02-27 01:31:59 -06:00
|
|
|
topic.update!(closed: true)
|
|
|
|
|
2018-06-14 01:10:30 -05:00
|
|
|
freeze_time(61.minutes.from_now) do
|
2023-01-09 05:18:21 -06:00
|
|
|
described_class.new.execute(topic_timer_id: topic.public_topic_timer.id, state: false)
|
2018-02-27 01:31:59 -06:00
|
|
|
|
|
|
|
expect(topic.reload.closed).to eq(false)
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(Post.last.raw).to eq(I18n.t("topic_statuses.autoclosed_disabled_minutes", count: 61))
|
2018-02-27 01:31:59 -06:00
|
|
|
end
|
|
|
|
end
|
2017-03-21 22:12:02 -05:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "when category has auto close configured" do
|
2020-08-25 23:59:05 -05:00
|
|
|
fab!(:category) do
|
2023-01-09 05:18:21 -06:00
|
|
|
Fabricate(:category, auto_close_based_on_last_post: true, auto_close_hours: 5)
|
2020-08-25 23:59:05 -05:00
|
|
|
end
|
|
|
|
|
2019-05-06 22:12:20 -05:00
|
|
|
fab!(:topic) { Fabricate(:topic, category: category, closed: true) }
|
2017-03-21 22:12:02 -05:00
|
|
|
|
2018-02-27 01:31:59 -06:00
|
|
|
it "should restore the category's auto close timer" do
|
2023-01-09 05:18:21 -06:00
|
|
|
Fabricate(:topic_timer, status_type: TopicTimer.types[:open], topic: topic, user: admin)
|
2017-03-21 22:12:02 -05:00
|
|
|
|
2018-06-19 01:13:14 -05:00
|
|
|
freeze_time(61.minutes.from_now) do
|
2023-01-09 05:18:21 -06:00
|
|
|
described_class.new.execute(topic_timer_id: topic.public_topic_timer.id, state: false)
|
2018-02-27 01:31:59 -06:00
|
|
|
|
|
|
|
expect(topic.reload.closed).to eq(false)
|
|
|
|
|
|
|
|
topic_timer = topic.public_topic_timer
|
|
|
|
|
|
|
|
expect(topic_timer.status_type).to eq(TopicTimer.types[:close])
|
2019-03-28 01:28:01 -05:00
|
|
|
expect(topic_timer.execute_at).to eq_time(5.hours.from_now)
|
2018-02-27 01:31:59 -06:00
|
|
|
end
|
|
|
|
end
|
2017-03-21 22:12:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "when trying to close a topic that has already been closed" do
|
|
|
|
it "should delete the topic timer" do
|
2020-08-25 23:18:33 -05:00
|
|
|
freeze_time(topic.public_topic_timer.execute_at + 1.minute)
|
|
|
|
|
|
|
|
topic.update!(closed: true)
|
|
|
|
|
|
|
|
expect do
|
2023-01-09 05:18:21 -06:00
|
|
|
described_class.new.execute(topic_timer_id: topic.public_topic_timer.id, state: true)
|
2020-08-25 23:18:33 -05:00
|
|
|
end.to change { TopicTimer.exists?(topic_id: topic.id) }.from(true).to(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "when trying to close a topic that has been deleted" do
|
|
|
|
it "should delete the topic timer" do
|
2020-08-25 23:18:33 -05:00
|
|
|
freeze_time(topic.public_topic_timer.execute_at + 1.minute)
|
2017-03-21 22:12:02 -05:00
|
|
|
|
2020-08-25 23:18:33 -05:00
|
|
|
topic.trash!
|
2017-03-21 22:12:02 -05:00
|
|
|
|
2020-08-25 23:18:33 -05:00
|
|
|
expect do
|
2023-01-09 05:18:21 -06:00
|
|
|
described_class.new.execute(topic_timer_id: topic.public_topic_timer.id, state: true)
|
2020-08-25 23:18:33 -05:00
|
|
|
end.to change { TopicTimer.exists?(topic_id: topic.id) }.from(true).to(false)
|
2017-03-21 22:12:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "when user is no longer authorized to close topics" do
|
2023-11-09 16:47:59 -06:00
|
|
|
fab!(:user)
|
2020-08-25 23:59:05 -05:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
fab!(:topic) { Fabricate(:topic_timer, user: user).topic }
|
2017-03-21 22:12:02 -05:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "should destroy the topic timer" do
|
2020-08-25 23:59:05 -05:00
|
|
|
freeze_time(topic.public_topic_timer.execute_at + 1.minute)
|
|
|
|
|
|
|
|
expect do
|
2023-01-09 05:18:21 -06:00
|
|
|
described_class.new.execute(topic_timer_id: topic.public_topic_timer.id, state: true)
|
2020-08-25 23:59:05 -05:00
|
|
|
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
|
2023-01-09 05:18:21 -06:00
|
|
|
category = Fabricate(:category, auto_close_based_on_last_post: true, auto_close_hours: 5)
|
2017-03-21 22:12:02 -05:00
|
|
|
|
2020-08-25 23:59:05 -05:00
|
|
|
topic = Fabricate(:topic, category: category)
|
|
|
|
topic.public_topic_timer.update!(user: user)
|
|
|
|
|
|
|
|
freeze_time(topic.public_topic_timer.execute_at + 1.minute)
|
|
|
|
|
|
|
|
expect do
|
2023-01-09 05:18:21 -06:00
|
|
|
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 }
|
2020-08-25 23:59:05 -05:00
|
|
|
|
2017-03-21 22:12:02 -05:00
|
|
|
expect(topic.reload.closed).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|