2013-02-09 10:33:07 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
require 'post_revisor'
|
|
|
|
|
|
|
|
describe PostRevisor do
|
|
|
|
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
2013-04-17 19:11:13 -04:00
|
|
|
let(:newuser) { Fabricate(:newuser) }
|
|
|
|
let(:post_args) { {user: newuser, topic: topic} }
|
2013-02-09 10:33:07 -05: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
|
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
subject.revise!(post.user, post.raw).should be_false
|
|
|
|
end
|
|
|
|
|
2013-12-11 21:41:34 -05:00
|
|
|
it "doesn't change version" do
|
|
|
|
lambda { subject.revise!(post.user, post.raw); post.reload }.should_not change(post, :version)
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'ninja editing' do
|
|
|
|
before do
|
|
|
|
SiteSetting.expects(:ninja_edit_window).returns(1.minute.to_i)
|
|
|
|
subject.revise!(post.user, 'updated body', revised_at: post.updated_at + 10.seconds)
|
|
|
|
post.reload
|
|
|
|
end
|
|
|
|
|
2013-12-11 21:41:34 -05:00
|
|
|
it 'does not update version' do
|
|
|
|
post.version.should == 1
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
2013-12-11 21:41:34 -05:00
|
|
|
it 'does not create a new revision' do
|
|
|
|
post.revisions.size.should == 0
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't change the last_version_at" do
|
|
|
|
post.last_version_at.should == first_version_at
|
|
|
|
end
|
2013-02-21 18:09:56 -05:00
|
|
|
|
|
|
|
it "doesn't update a category" do
|
|
|
|
subject.category_changed.should be_blank
|
|
|
|
end
|
|
|
|
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'revision much later' do
|
|
|
|
|
|
|
|
let!(:revised_at) { post.updated_at + 2.minutes }
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.stubs(:ninja_edit_window).returns(1.minute.to_i)
|
|
|
|
subject.revise!(post.user, 'updated body', revised_at: revised_at)
|
|
|
|
post.reload
|
|
|
|
end
|
|
|
|
|
2013-02-21 18:09:56 -05:00
|
|
|
it "doesn't update a category" do
|
|
|
|
subject.category_changed.should be_blank
|
|
|
|
end
|
|
|
|
|
2013-12-11 21:41:34 -05:00
|
|
|
it 'updates the version' do
|
|
|
|
post.version.should == 2
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a new version' do
|
2013-12-11 21:41:34 -05:00
|
|
|
post.revisions.size.should == 1
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "updates the last_version_at" do
|
|
|
|
post.last_version_at.to_i.should == revised_at.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "new edit window" do
|
|
|
|
|
|
|
|
before do
|
|
|
|
subject.revise!(post.user, 'yet another updated body', revised_at: revised_at)
|
|
|
|
post.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't create a new version if you do another" do
|
2013-12-11 21:41:34 -05:00
|
|
|
post.version.should == 2
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't change last_version_at" do
|
|
|
|
post.last_version_at.to_i.should == revised_at.to_i
|
|
|
|
end
|
|
|
|
|
2013-02-21 18:09:56 -05:00
|
|
|
it "doesn't update a category" do
|
|
|
|
subject.category_changed.should be_blank
|
|
|
|
end
|
|
|
|
|
2013-02-09 10:33:07 -05:00
|
|
|
context "after second window" do
|
|
|
|
|
|
|
|
let!(:new_revised_at) {revised_at + 2.minutes}
|
|
|
|
|
|
|
|
before do
|
|
|
|
subject.revise!(post.user, 'yet another, another updated body', revised_at: new_revised_at)
|
|
|
|
post.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does create a new version after the edit window" do
|
2013-12-11 21:41:34 -05:00
|
|
|
post.version.should == 3
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does create a new version after the edit window" do
|
|
|
|
post.last_version_at.to_i.should == new_revised_at.to_i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-21 18:09:56 -05:00
|
|
|
describe 'category topic' do
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
let!(:category) do
|
2013-02-21 18:09:56 -05:00
|
|
|
category = Fabricate(:category)
|
|
|
|
category.update_column(:topic_id, topic.id)
|
|
|
|
category
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:new_description) { "this is my new description." }
|
|
|
|
|
|
|
|
it "should have to description by default" do
|
|
|
|
category.description.should be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
context "one paragraph description" do
|
|
|
|
before do
|
|
|
|
subject.revise!(post.user, new_description)
|
|
|
|
category.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true for category_changed" do
|
|
|
|
subject.category_changed.should be_true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "updates the description of the category" do
|
|
|
|
category.description.should == new_description
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "multiple paragraph description" do
|
|
|
|
before do
|
|
|
|
subject.revise!(post.user, "#{new_description}\n\nOther content goes here.")
|
|
|
|
category.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the changed category info" do
|
|
|
|
subject.category_changed.should == category
|
|
|
|
end
|
|
|
|
|
|
|
|
it "updates the description of the category" do
|
|
|
|
category.description.should == new_description
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-21 18:09:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when updating back to the original paragraph' do
|
|
|
|
before do
|
|
|
|
category.update_column(:description, 'this is my description')
|
|
|
|
subject.revise!(post.user, Category.post_template)
|
|
|
|
category.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it "puts the description back to nothing" do
|
|
|
|
category.description.should be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true for category_changed" do
|
|
|
|
subject.category_changed.should == category
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-09 10:33:07 -05:00
|
|
|
describe 'rate limiter' do
|
|
|
|
let(:changed_by) { Fabricate(:coding_horror) }
|
|
|
|
|
|
|
|
it "triggers a rate limiter" do
|
|
|
|
EditRateLimiter.any_instance.expects(:performed!)
|
|
|
|
subject.revise!(changed_by, 'updated body')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-17 19:11:13 -04:00
|
|
|
describe "admin editing a new user's post" do
|
2013-04-05 13:59:00 -04:00
|
|
|
let(:changed_by) { Fabricate(:admin) }
|
|
|
|
|
|
|
|
before do
|
2013-10-23 23:55:55 -04:00
|
|
|
SiteSetting.stubs(:newuser_max_images).returns(0)
|
2014-01-27 15:09:09 -05:00
|
|
|
url = "http://i.imgur.com/wfn7rgU.jpg"
|
|
|
|
Oneboxer.stubs(:onebox).with(url, anything).returns("<img src='#{url}'>")
|
|
|
|
subject.revise!(changed_by, "So, post them here!\n#{url}")
|
2013-04-05 13:59:00 -04:00
|
|
|
end
|
|
|
|
|
2013-04-17 19:11:13 -04:00
|
|
|
it "allows an admin to insert images into a new user's post" do
|
2013-04-05 13:59:00 -04:00
|
|
|
post.errors.should be_blank
|
|
|
|
end
|
2013-11-06 05:43:40 -05:00
|
|
|
|
|
|
|
it "marks the admin as the last updater" do
|
2013-12-11 21:41:34 -05:00
|
|
|
post.last_editor_id.should == changed_by.id
|
2013-11-06 05:43:40 -05:00
|
|
|
end
|
|
|
|
|
2013-04-05 13:59:00 -04:00
|
|
|
end
|
|
|
|
|
2013-04-17 19:11:13 -04:00
|
|
|
describe "new user editing their own post" do
|
2013-04-05 13:59:00 -04:00
|
|
|
before do
|
2013-10-23 23:55:55 -04:00
|
|
|
SiteSetting.stubs(:newuser_max_images).returns(0)
|
|
|
|
url = "http://i.imgur.com/FGg7Vzu.gif"
|
2014-03-18 00:22:39 -04:00
|
|
|
Oneboxer.stubs(:cached_onebox).with(url, anything).returns("<img src='#{url}'>")
|
2013-10-23 23:55:55 -04:00
|
|
|
subject.revise!(post.user, "So, post them here!\n#{url}")
|
2013-04-05 13:59:00 -04:00
|
|
|
end
|
|
|
|
|
2014-01-27 15:09:09 -05:00
|
|
|
it "doesn't allow images to be inserted" do
|
2013-04-05 13:59:00 -04:00
|
|
|
post.errors.should be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-02-09 10:33:07 -05:00
|
|
|
describe 'with a new body' do
|
|
|
|
let(:changed_by) { Fabricate(:coding_horror) }
|
2013-12-10 13:47:07 -05:00
|
|
|
let!(:result) { subject.revise!(changed_by, "lets update the body") }
|
2013-02-09 10:33:07 -05:00
|
|
|
|
|
|
|
it 'returns true' do
|
|
|
|
result.should be_true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the body' do
|
2013-12-10 13:47:07 -05:00
|
|
|
post.raw.should == "lets update the body"
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the invalidate oneboxes attribute' do
|
|
|
|
post.invalidate_oneboxes.should == true
|
|
|
|
end
|
|
|
|
|
2013-12-11 21:41:34 -05:00
|
|
|
it 'increased the version' do
|
|
|
|
post.version.should == 2
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
2013-12-11 21:41:34 -05:00
|
|
|
it 'has the new revision' do
|
|
|
|
post.revisions.size.should == 1
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
2013-12-11 21:41:34 -05:00
|
|
|
it "saved the user who made the change in the revisions" do
|
|
|
|
post.revisions.first.user_id.should == changed_by.id
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
2013-12-10 13:47:07 -05:00
|
|
|
it "updates the word count" do
|
|
|
|
post.word_count.should == 4
|
|
|
|
post.topic.reload
|
|
|
|
post.topic.word_count.should == 4
|
|
|
|
end
|
|
|
|
|
2013-02-09 10:33:07 -05:00
|
|
|
context 'second poster posts again quickly' do
|
|
|
|
before do
|
|
|
|
SiteSetting.expects(:ninja_edit_window).returns(1.minute.to_i)
|
|
|
|
subject.revise!(changed_by, 'yet another updated body', revised_at: post.updated_at + 10.seconds)
|
|
|
|
post.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is a ninja edit, because the second poster posted again quickly' do
|
2013-12-11 21:41:34 -05:00
|
|
|
post.version.should == 2
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is a ninja edit, because the second poster posted again quickly' do
|
2013-12-11 21:41:34 -05:00
|
|
|
post.revisions.size.should == 1
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-03-18 13:40:40 -04: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 {
|
|
|
|
revisor.revise!(first_post.user, 'Edit the first post', revised_at: first_post.updated_at + 10.seconds)
|
|
|
|
topic.reload
|
|
|
|
}.to change { topic.excerpt }
|
|
|
|
second_post = Fabricate(:post, post_args.merge(post_number: 2, topic_id: topic.id))
|
|
|
|
expect {
|
|
|
|
described_class.new(second_post).revise!(second_post.user, 'Edit the 2nd post')
|
|
|
|
topic.reload
|
|
|
|
}.to_not change { topic.excerpt }
|
|
|
|
end
|
|
|
|
end
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
end
|