2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2013-02-09 23:33:07 +08:00
|
|
|
require 'post_revisor'
|
|
|
|
|
|
|
|
describe PostRevisor do
|
|
|
|
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
2013-04-18 07:11:13 +08:00
|
|
|
let(:newuser) { Fabricate(:newuser) }
|
2015-05-30 02:08:39 +08:00
|
|
|
let(:post_args) { { user: newuser, topic: topic } }
|
2013-02-09 23:33:07 +08:00
|
|
|
|
2015-01-28 01:13:45 +08:00
|
|
|
context 'TopicChanges' do
|
|
|
|
let(:tc) {
|
|
|
|
topic.reload
|
|
|
|
PostRevisor::TopicChanges.new(topic, topic.user)
|
|
|
|
}
|
|
|
|
|
|
|
|
it 'provides a guardian' do
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(tc.guardian).to be_an_instance_of Guardian
|
2015-01-28 01:13:45 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'tracks changes properly' do
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(tc.diff).to eq({})
|
2015-01-28 01:13:45 +08:00
|
|
|
|
|
|
|
# it remembers changes we tell it to
|
|
|
|
tc.record_change('height', '180cm', '170cm')
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(tc.diff['height']).to eq(['180cm', '170cm'])
|
2015-01-28 01:13:45 +08:00
|
|
|
|
|
|
|
# it works with arrays of values
|
|
|
|
tc.record_change('colors', nil, ['red', 'blue'])
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(tc.diff['colors']).to eq([nil, ['red', 'blue']])
|
2015-01-28 01:13:45 +08:00
|
|
|
|
|
|
|
# it does not record changes to the same val
|
|
|
|
tc.record_change('wat', 'js', 'js')
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(tc.diff['wat']).to be_nil
|
2015-01-28 01:13:45 +08:00
|
|
|
|
|
|
|
tc.record_change('tags', ['a', 'b'], ['a', 'b'])
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(tc.diff['tags']).to be_nil
|
2015-01-28 01:13:45 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-03 01:44:21 +08:00
|
|
|
context 'revise wiki' do
|
|
|
|
|
|
|
|
before do
|
|
|
|
# There used to be a bug where wiki changes were considered posting "too similar"
|
|
|
|
# so this is enabled and checked
|
|
|
|
$redis.delete_prefixed('unique-post')
|
|
|
|
SiteSetting.unique_posts_mins = 10
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows the user to change it to a wiki' do
|
|
|
|
pc = PostCreator.new(newuser, topic_id: topic.id, raw: 'this is a post that will become a wiki')
|
|
|
|
post = pc.create
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(post.revise(post.user, wiki: true)).to be_truthy
|
2015-02-03 01:44:21 +08:00
|
|
|
post.reload
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(post.wiki).to be_truthy
|
2015-02-03 01:44:21 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-09 23:33:07 +08:00
|
|
|
context 'revise' do
|
|
|
|
let(:post) { Fabricate(:post, post_args) }
|
|
|
|
let(:first_version_at) { post.last_version_at }
|
|
|
|
|
|
|
|
subject { described_class.new(post) }
|
|
|
|
|
|
|
|
describe 'with the same body' do
|
2013-12-12 10:41:34 +08:00
|
|
|
it "doesn't change version" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect {
|
|
|
|
expect(subject.revise!(post.user, { raw: post.raw })).to eq(false)
|
2014-06-16 10:13:28 +08:00
|
|
|
post.reload
|
2015-01-10 00:34:37 +08:00
|
|
|
}.not_to change(post, :version)
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'ninja editing' do
|
2014-06-16 10:13:28 +08:00
|
|
|
it 'correctly applies edits' do
|
2015-11-25 03:28:42 +08:00
|
|
|
SiteSetting.stubs(:editing_grace_period).returns(1.minute)
|
2015-05-30 02:08:39 +08:00
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
subject.revise!(post.user, { raw: 'updated body' }, revised_at: post.updated_at + 10.seconds)
|
2013-02-09 23:33:07 +08:00
|
|
|
post.reload
|
|
|
|
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(post.version).to eq(1)
|
|
|
|
expect(post.public_version).to eq(1)
|
|
|
|
expect(post.revisions.size).to eq(0)
|
|
|
|
expect(post.last_version_at).to eq(first_version_at)
|
|
|
|
expect(subject.category_changed).to be_blank
|
2013-02-22 07:09:56 +08:00
|
|
|
end
|
2015-05-30 02:08:39 +08:00
|
|
|
|
|
|
|
it "doesn't create a new version" do
|
2015-11-25 03:28:42 +08:00
|
|
|
SiteSetting.stubs(:editing_grace_period).returns(1.minute)
|
2015-05-30 02:08:39 +08:00
|
|
|
|
|
|
|
# making a revision
|
2015-11-25 03:28:42 +08:00
|
|
|
subject.revise!(post.user, { raw: 'updated body' }, revised_at: post.updated_at + SiteSetting.editing_grace_period + 1.seconds)
|
2015-05-30 02:08:39 +08:00
|
|
|
# "roll back"
|
2015-11-25 03:28:42 +08:00
|
|
|
subject.revise!(post.user, { raw: 'Hello world' }, revised_at: post.updated_at + SiteSetting.editing_grace_period + 2.seconds)
|
2015-05-30 02:08:39 +08:00
|
|
|
|
|
|
|
post.reload
|
|
|
|
|
|
|
|
expect(post.version).to eq(1)
|
|
|
|
expect(post.public_version).to eq(1)
|
|
|
|
expect(post.revisions.size).to eq(0)
|
|
|
|
end
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'revision much later' do
|
|
|
|
|
|
|
|
let!(:revised_at) { post.updated_at + 2.minutes }
|
|
|
|
|
|
|
|
before do
|
2015-11-25 03:28:42 +08:00
|
|
|
SiteSetting.stubs(:editing_grace_period).returns(1.minute.to_i)
|
2014-10-28 05:06:43 +08:00
|
|
|
subject.revise!(post.user, { raw: 'updated body' }, revised_at: revised_at)
|
2013-02-09 23:33:07 +08:00
|
|
|
post.reload
|
|
|
|
end
|
|
|
|
|
2013-02-22 07:09:56 +08:00
|
|
|
it "doesn't update a category" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(subject.category_changed).to be_blank
|
2013-02-22 07:09:56 +08:00
|
|
|
end
|
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
it 'updates the versions' do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(post.version).to eq(2)
|
|
|
|
expect(post.public_version).to eq(2)
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
it 'creates a new revision' do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(post.revisions.size).to eq(1)
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "updates the last_version_at" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(post.last_version_at.to_i).to eq(revised_at.to_i)
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "new edit window" do
|
|
|
|
|
|
|
|
before do
|
2014-10-28 05:06:43 +08:00
|
|
|
subject.revise!(post.user, { raw: 'yet another updated body' }, revised_at: revised_at)
|
2013-02-09 23:33:07 +08:00
|
|
|
post.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't create a new version if you do another" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(post.version).to eq(2)
|
|
|
|
expect(post.public_version).to eq(2)
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't change last_version_at" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(post.last_version_at.to_i).to eq(revised_at.to_i)
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
|
2013-02-22 07:09:56 +08:00
|
|
|
it "doesn't update a category" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(subject.category_changed).to be_blank
|
2013-02-22 07:09:56 +08:00
|
|
|
end
|
|
|
|
|
2013-02-09 23:33:07 +08:00
|
|
|
context "after second window" do
|
|
|
|
|
|
|
|
let!(:new_revised_at) {revised_at + 2.minutes}
|
|
|
|
|
|
|
|
before do
|
2014-10-28 05:06:43 +08:00
|
|
|
subject.revise!(post.user, { raw: 'yet another, another updated body' }, revised_at: new_revised_at)
|
2013-02-09 23:33:07 +08:00
|
|
|
post.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does create a new version after the edit window" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(post.version).to eq(3)
|
|
|
|
expect(post.public_version).to eq(3)
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does create a new version after the edit window" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(post.last_version_at.to_i).to eq(new_revised_at.to_i)
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-22 07:09:56 +08:00
|
|
|
describe 'category topic' do
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
let!(:category) do
|
2013-02-22 07:09:56 +08:00
|
|
|
category = Fabricate(:category)
|
|
|
|
category.update_column(:topic_id, topic.id)
|
|
|
|
category
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:new_description) { "this is my new description." }
|
|
|
|
|
2014-09-25 23:44:48 +08:00
|
|
|
it "should have no description by default" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(category.description).to be_blank
|
2013-02-22 07:09:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "one paragraph description" do
|
|
|
|
before do
|
2014-10-28 05:06:43 +08:00
|
|
|
subject.revise!(post.user, { raw: new_description })
|
2013-02-22 07:09:56 +08:00
|
|
|
category.reload
|
|
|
|
end
|
|
|
|
|
2014-09-25 23:44:48 +08:00
|
|
|
it "returns the changed category info" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(subject.category_changed).to eq(category)
|
2013-02-22 07:09:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "updates the description of the category" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(category.description).to eq(new_description)
|
2013-02-22 07:09:56 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "multiple paragraph description" do
|
|
|
|
before do
|
2014-10-28 05:06:43 +08:00
|
|
|
subject.revise!(post.user, { raw: "#{new_description}\n\nOther content goes here." })
|
2013-02-22 07:09:56 +08:00
|
|
|
category.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the changed category info" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(subject.category_changed).to eq(category)
|
2013-02-22 07:09:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "updates the description of the category" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(category.description).to eq(new_description)
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-22 07:09:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when updating back to the original paragraph' do
|
|
|
|
before do
|
|
|
|
category.update_column(:description, 'this is my description')
|
2014-10-28 05:06:43 +08:00
|
|
|
subject.revise!(post.user, { raw: Category.post_template })
|
2013-02-22 07:09:56 +08:00
|
|
|
category.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it "puts the description back to nothing" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(category.description).to be_blank
|
2013-02-22 07:09:56 +08:00
|
|
|
end
|
|
|
|
|
2014-09-25 23:44:48 +08:00
|
|
|
it "returns the changed category info" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(subject.category_changed).to eq(category)
|
2013-02-22 07:09:56 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-09 23:33:07 +08:00
|
|
|
describe 'rate limiter' do
|
|
|
|
let(:changed_by) { Fabricate(:coding_horror) }
|
|
|
|
|
|
|
|
it "triggers a rate limiter" do
|
|
|
|
EditRateLimiter.any_instance.expects(:performed!)
|
2014-10-28 05:06:43 +08:00
|
|
|
subject.revise!(changed_by, { raw: 'updated body' })
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-18 07:11:13 +08:00
|
|
|
describe "admin editing a new user's post" do
|
2013-04-06 01:59:00 +08:00
|
|
|
let(:changed_by) { Fabricate(:admin) }
|
|
|
|
|
|
|
|
before do
|
2013-10-24 11:55:55 +08:00
|
|
|
SiteSetting.stubs(:newuser_max_images).returns(0)
|
2014-01-28 04:09:09 +08:00
|
|
|
url = "http://i.imgur.com/wfn7rgU.jpg"
|
|
|
|
Oneboxer.stubs(:onebox).with(url, anything).returns("<img src='#{url}'>")
|
2014-10-28 05:06:43 +08:00
|
|
|
subject.revise!(changed_by, { raw: "So, post them here!\n#{url}" })
|
2013-04-06 01:59:00 +08:00
|
|
|
end
|
|
|
|
|
2013-04-18 07:11:13 +08:00
|
|
|
it "allows an admin to insert images into a new user's post" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(post.errors).to be_blank
|
2013-04-06 01:59:00 +08:00
|
|
|
end
|
2013-11-06 18:43:40 +08:00
|
|
|
|
|
|
|
it "marks the admin as the last updater" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(post.last_editor_id).to eq(changed_by.id)
|
2013-11-06 18:43:40 +08:00
|
|
|
end
|
|
|
|
|
2013-04-06 01:59:00 +08:00
|
|
|
end
|
|
|
|
|
2013-04-18 07:11:13 +08:00
|
|
|
describe "new user editing their own post" do
|
2013-04-06 01:59:00 +08:00
|
|
|
before do
|
2013-10-24 11:55:55 +08:00
|
|
|
SiteSetting.stubs(:newuser_max_images).returns(0)
|
|
|
|
url = "http://i.imgur.com/FGg7Vzu.gif"
|
2014-03-18 12:22:39 +08:00
|
|
|
Oneboxer.stubs(:cached_onebox).with(url, anything).returns("<img src='#{url}'>")
|
2014-10-28 05:06:43 +08:00
|
|
|
subject.revise!(post.user, { raw: "So, post them here!\n#{url}" })
|
2013-04-06 01:59:00 +08:00
|
|
|
end
|
|
|
|
|
2014-01-28 04:09:09 +08:00
|
|
|
it "doesn't allow images to be inserted" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(post.errors).to be_present
|
2013-04-06 01:59:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-02-09 23:33:07 +08:00
|
|
|
describe 'with a new body' do
|
|
|
|
let(:changed_by) { Fabricate(:coding_horror) }
|
2016-01-11 18:16:23 +08:00
|
|
|
let!(:result) { subject.revise!(changed_by, { raw: "lets update the body. Здравствуйте" }) }
|
2013-02-09 23:33:07 +08:00
|
|
|
|
|
|
|
it 'returns true' do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(result).to eq(true)
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the body' do
|
2016-01-11 18:16:23 +08:00
|
|
|
expect(post.raw).to eq("lets update the body. Здравствуйте")
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the invalidate oneboxes attribute' do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(post.invalidate_oneboxes).to eq(true)
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
it 'increased the versions' do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(post.version).to eq(2)
|
|
|
|
expect(post.public_version).to eq(2)
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
|
2013-12-12 10:41:34 +08:00
|
|
|
it 'has the new revision' do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(post.revisions.size).to eq(1)
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
|
2013-12-12 10:41:34 +08:00
|
|
|
it "saved the user who made the change in the revisions" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(post.revisions.first.user_id).to eq(changed_by.id)
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
|
2013-12-11 02:47:07 +08:00
|
|
|
it "updates the word count" do
|
2016-01-11 18:16:23 +08:00
|
|
|
expect(post.word_count).to eq(5)
|
2013-12-11 02:47:07 +08:00
|
|
|
post.topic.reload
|
2016-01-11 18:16:23 +08:00
|
|
|
expect(post.topic.word_count).to eq(5)
|
2013-12-11 02:47:07 +08:00
|
|
|
end
|
|
|
|
|
2013-02-09 23:33:07 +08:00
|
|
|
context 'second poster posts again quickly' do
|
|
|
|
before do
|
2015-11-25 03:28:42 +08:00
|
|
|
SiteSetting.stubs(:editing_grace_period).returns(1.minute.to_i)
|
2014-10-28 05:06:43 +08:00
|
|
|
subject.revise!(changed_by, { raw: 'yet another updated body' }, revised_at: post.updated_at + 10.seconds)
|
2013-02-09 23:33:07 +08:00
|
|
|
post.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is a ninja edit, because the second poster posted again quickly' do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(post.version).to eq(2)
|
|
|
|
expect(post.public_version).to eq(2)
|
|
|
|
expect(post.revisions.size).to eq(1)
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-03-19 01:40:40 +08:00
|
|
|
|
|
|
|
describe "topic excerpt" do
|
|
|
|
it "topic excerpt is updated only if first post is revised" do
|
|
|
|
revisor = described_class.new(post)
|
|
|
|
first_post = topic.posts.by_post_number.first
|
|
|
|
expect {
|
2014-10-28 05:06:43 +08:00
|
|
|
revisor.revise!(first_post.user, { raw: 'Edit the first post' }, revised_at: first_post.updated_at + 10.seconds)
|
2014-03-19 01:40:40 +08:00
|
|
|
topic.reload
|
|
|
|
}.to change { topic.excerpt }
|
|
|
|
second_post = Fabricate(:post, post_args.merge(post_number: 2, topic_id: topic.id))
|
|
|
|
expect {
|
2014-10-28 05:06:43 +08:00
|
|
|
described_class.new(second_post).revise!(second_post.user, { raw: 'Edit the 2nd post' })
|
2014-03-19 01:40:40 +08:00
|
|
|
topic.reload
|
|
|
|
}.to_not change { topic.excerpt }
|
|
|
|
end
|
|
|
|
end
|
2014-09-02 07:18:06 +08:00
|
|
|
|
|
|
|
it "doesn't strip starting whitespaces" do
|
2014-10-28 05:06:43 +08:00
|
|
|
subject.revise!(post.user, { raw: " <-- whitespaces --> " })
|
2014-09-02 07:18:06 +08:00
|
|
|
post.reload
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(post.raw).to eq(" <-- whitespaces -->")
|
2014-09-02 07:18:06 +08:00
|
|
|
end
|
|
|
|
|
2016-04-07 22:29:01 +08:00
|
|
|
context "#publish_changes" do
|
|
|
|
let!(:post) { Fabricate(:post, topic_id: topic.id) }
|
|
|
|
|
|
|
|
it "should publish topic changes to clients" do
|
|
|
|
revisor = described_class.new(topic.ordered_posts.first, topic)
|
|
|
|
|
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
revisor.revise!(newuser, { title: 'this is a test topic' })
|
|
|
|
end
|
|
|
|
|
|
|
|
message = messages.find { |message| message.channel == "/topic/#{topic.id}" }
|
|
|
|
payload = message.data
|
|
|
|
expect(payload[:reload_topic]).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2013-02-09 23:33:07 +08:00
|
|
|
end
|
|
|
|
end
|