Merge pull request #3678 from tgxworld/allow_admin_to_change_timestamp

FEATURE: Allow admin to change timestamp of topic.
This commit is contained in:
Sam
2015-08-21 10:34:37 +10:00
11 changed files with 257 additions and 0 deletions

View File

@@ -263,6 +263,45 @@ describe TopicsController do
end
end
context 'change_timestamps' do
let(:params) { { topic_id: 1, timestamp: Time.zone.now } }
it 'needs you to be logged in' do
expect { xhr :put, :change_timestamps, params }.to raise_error(Discourse::NotLoggedIn)
end
[:moderator, :trust_level_4].each do |user|
describe "forbidden to #{user}" do
let!(user) { log_in(user) }
it 'correctly denies' do
xhr :put, :change_timestamps, params
expect(response).to be_forbidden
end
end
end
describe 'changing timestamps' do
let!(:admin) { log_in(:admin) }
let(:old_timestamp) { Time.zone.now }
let(:new_timestamp) { old_timestamp - 1.day }
let!(:topic) { Fabricate(:topic, created_at: old_timestamp) }
let!(:p1) { Fabricate(:post, topic_id: topic.id, created_at: old_timestamp) }
let!(:p2) { Fabricate(:post, topic_id: topic.id, created_at: old_timestamp + 1.day) }
it 'raises an error with a missing parameter' do
expect { xhr :put, :change_timestamps, topic_id: 1 }.to raise_error(ActionController::ParameterMissing)
end
it 'should update the timestamps of selected posts' do
xhr :put, :change_timestamps, topic_id: topic.id, timestamp: new_timestamp.to_f
expect(topic.reload.created_at.to_s).to eq(new_timestamp.to_s)
expect(p1.reload.created_at.to_s).to eq(new_timestamp.to_s)
expect(p2.reload.created_at.to_s).to eq(old_timestamp.to_s)
end
end
end
context 'clear_pin' do
it 'needs you to be logged in' do
expect { xhr :put, :clear_pin, topic_id: 1 }.to raise_error(Discourse::NotLoggedIn)

View File

@@ -0,0 +1,55 @@
require 'spec_helper'
describe PostTimestampChanger do
describe "change!" do
let(:old_timestamp) { Time.zone.now }
let(:new_timestamp) { old_timestamp + 1.day }
let!(:topic) { Fabricate(:topic, created_at: old_timestamp) }
let!(:p1) { Fabricate(:post, topic: topic, created_at: old_timestamp) }
let!(:p2) { Fabricate(:post, topic: topic, created_at: old_timestamp + 1.day) }
let(:params) { { topic_id: topic.id, timestamp: new_timestamp.to_f } }
it 'changes the timestamp of the topic and opening post' do
PostTimestampChanger.new(params).change!
topic.reload
[:created_at, :updated_at, :bumped_at].each do |column|
expect(topic.public_send(column).to_s).to eq(new_timestamp.to_s)
end
p1.reload
[:created_at, :updated_at].each do |column|
expect(p1.public_send(column).to_s).to eq(new_timestamp.to_s)
end
end
describe 'predated timestamp' do
it 'updates the timestamp of posts in the topic with the time difference applied' do
PostTimestampChanger.new(params).change!
p2.reload
[:created_at, :updated_at].each do |column|
expect(p2.public_send(column).to_s).to eq((old_timestamp + 2.day).to_s)
end
end
end
describe 'backdated timestamp' do
let(:new_timestamp) { old_timestamp - 1.day }
it 'updates the timestamp of posts in the topic with the time difference applied' do
PostTimestampChanger.new(params).change!
p2.reload
[:created_at, :updated_at].each do |column|
expect(p2.public_send(column).to_s).to eq((old_timestamp).to_s)
end
end
end
it 'deletes the stats cache' do
$redis.expects(:del).twice
PostTimestampChanger.new(params).change!
end
end
end