2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2015-07-25 12:06:49 -04:00
|
|
|
|
2017-05-22 03:01:33 -04:00
|
|
|
describe TopicTimestampChanger do
|
2015-07-25 12:06:49 -04:00
|
|
|
describe "change!" do
|
|
|
|
let(:old_timestamp) { Time.zone.now }
|
2017-05-22 04:03:49 -04:00
|
|
|
let(:topic) { Fabricate(:topic, created_at: old_timestamp) }
|
2015-07-25 12:06:49 -04:00
|
|
|
let!(:p1) { Fabricate(:post, topic: topic, created_at: old_timestamp) }
|
|
|
|
let!(:p2) { Fabricate(:post, topic: topic, created_at: old_timestamp + 1.day) }
|
|
|
|
|
2017-05-22 03:01:33 -04:00
|
|
|
context 'new timestamp is in the future' do
|
|
|
|
let(:new_timestamp) { old_timestamp + 2.day }
|
2015-07-25 12:06:49 -04:00
|
|
|
|
2017-05-22 04:03:49 -04:00
|
|
|
it 'should raise the right error' do
|
|
|
|
expect { TopicTimestampChanger.new(topic: topic, timestamp: new_timestamp.to_f).change! }
|
|
|
|
.to raise_error(TopicTimestampChanger::InvalidTimestampError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'new timestamp is in the past' do
|
|
|
|
let(:new_timestamp) { old_timestamp - 2.day }
|
|
|
|
|
2017-05-22 03:01:33 -04:00
|
|
|
it 'changes the timestamp of the topic and opening post' do
|
2017-07-24 09:17:42 -04:00
|
|
|
freeze_time
|
|
|
|
TopicTimestampChanger.new(topic: topic, timestamp: new_timestamp.to_f).change!
|
2017-05-22 03:01:33 -04:00
|
|
|
|
2017-07-24 09:17:42 -04:00
|
|
|
topic.reload
|
2021-07-16 03:56:51 -04:00
|
|
|
p1.reload
|
|
|
|
p2.reload
|
|
|
|
last_post_created_at = p2.created_at
|
|
|
|
|
2020-03-10 17:13:17 -04:00
|
|
|
expect(topic.created_at).to eq_time(new_timestamp)
|
|
|
|
expect(topic.updated_at).to eq_time(new_timestamp)
|
2021-07-16 03:56:51 -04:00
|
|
|
expect(topic.bumped_at).to eq_time(last_post_created_at)
|
|
|
|
expect(topic.last_posted_at).to eq_time(last_post_created_at)
|
2015-10-19 22:12:52 -04:00
|
|
|
|
2020-03-10 17:13:17 -04:00
|
|
|
expect(p1.created_at).to eq_time(new_timestamp)
|
|
|
|
expect(p1.updated_at).to eq_time(new_timestamp)
|
2015-07-25 12:06:49 -04:00
|
|
|
|
2020-03-10 17:13:17 -04:00
|
|
|
expect(p2.created_at).to eq_time(new_timestamp + 1.day)
|
|
|
|
expect(p2.updated_at).to eq_time(new_timestamp + 1.day)
|
2015-07-25 12:06:49 -04:00
|
|
|
end
|
|
|
|
|
2017-05-22 04:03:49 -04:00
|
|
|
describe 'when posts have timestamps in the future' do
|
|
|
|
it 'should set the new timestamp as the default timestamp' do
|
2020-03-10 17:13:17 -04:00
|
|
|
new_timestamp = freeze_time
|
2015-07-25 12:06:49 -04:00
|
|
|
|
2020-03-10 17:13:17 -04:00
|
|
|
p3 = Fabricate(:post, topic: topic, created_at: new_timestamp + 3.days)
|
2017-07-24 09:17:42 -04:00
|
|
|
TopicTimestampChanger.new(topic: topic, timestamp: new_timestamp.to_f).change!
|
|
|
|
|
|
|
|
p3.reload
|
|
|
|
|
2020-03-10 17:13:17 -04:00
|
|
|
expect(p3.created_at).to eq_time(new_timestamp)
|
|
|
|
expect(p3.updated_at).to eq_time(new_timestamp)
|
2015-07-25 12:06:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'deletes the stats cache' do
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.set AdminDashboardData.stats_cache_key, "X"
|
|
|
|
Discourse.redis.set About.stats_cache_key, "X"
|
2017-03-20 16:15:23 -04:00
|
|
|
|
2017-05-22 04:03:49 -04:00
|
|
|
TopicTimestampChanger.new(topic: topic, timestamp: Time.zone.now.to_f).change!
|
2017-03-20 16:15:23 -04:00
|
|
|
|
2019-12-03 04:05:53 -05:00
|
|
|
expect(Discourse.redis.get(AdminDashboardData.stats_cache_key)).to eq(nil)
|
|
|
|
expect(Discourse.redis.get(About.stats_cache_key)).to eq(nil)
|
2015-07-25 12:06:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|