2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-09 10:33:07 -05:00
|
|
|
require 'post_revisor'
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe PostRevisor do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:topic) { Fabricate(:topic) }
|
2020-04-30 18:33:57 -04:00
|
|
|
fab!(:newuser) { Fabricate(:newuser, last_seen_at: Date.today) }
|
2019-05-06 04:49:11 -04:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2021-12-15 12:41:14 -05:00
|
|
|
fab!(:coding_horror) { Fabricate(:coding_horror) }
|
2019-05-06 04:49:11 -04:00
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
|
|
|
fab!(:moderator) { Fabricate(:moderator) }
|
2015-05-29 14:08:39 -04:00
|
|
|
let(:post_args) { { user: newuser, topic: topic } }
|
2013-02-09 10:33:07 -05:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe 'TopicChanges' do
|
2015-01-27 12:13:45 -05:00
|
|
|
let(:tc) {
|
|
|
|
topic.reload
|
|
|
|
PostRevisor::TopicChanges.new(topic, topic.user)
|
|
|
|
}
|
|
|
|
|
|
|
|
it 'provides a guardian' do
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(tc.guardian).to be_an_instance_of Guardian
|
2015-01-27 12:13:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'tracks changes properly' do
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(tc.diff).to eq({})
|
2015-01-27 12:13:45 -05:00
|
|
|
|
|
|
|
# it remembers changes we tell it to
|
|
|
|
tc.record_change('height', '180cm', '170cm')
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(tc.diff['height']).to eq(['180cm', '170cm'])
|
2015-01-27 12:13:45 -05:00
|
|
|
|
|
|
|
# it works with arrays of values
|
|
|
|
tc.record_change('colors', nil, ['red', 'blue'])
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(tc.diff['colors']).to eq([nil, ['red', 'blue']])
|
2015-01-27 12:13:45 -05:00
|
|
|
|
|
|
|
# it does not record changes to the same val
|
|
|
|
tc.record_change('wat', 'js', 'js')
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(tc.diff['wat']).to be_nil
|
2015-01-27 12:13:45 -05:00
|
|
|
|
|
|
|
tc.record_change('tags', ['a', 'b'], ['a', 'b'])
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(tc.diff['tags']).to be_nil
|
2015-01-27 12:13:45 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe 'editing category' do
|
2021-03-24 20:24:50 -04:00
|
|
|
it "triggers the :post_edited event with topic_changed?" do
|
|
|
|
category = Fabricate(:category)
|
|
|
|
category.set_permissions(everyone: :full)
|
|
|
|
category.save!
|
|
|
|
post = create_post
|
|
|
|
events = DiscourseEvent.track_events do
|
|
|
|
post.revise(post.user, category_id: category.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
event = events.find { |e| e[:event_name] == :post_edited }
|
|
|
|
|
|
|
|
expect(event[:params].first).to eq(post)
|
|
|
|
expect(event[:params].second).to eq(true)
|
|
|
|
expect(event[:params].third).to be_kind_of(PostRevisor)
|
|
|
|
expect(event[:params].third.topic_diff).to eq({ "category_id" => [SiteSetting.uncategorized_category_id, category.id] })
|
|
|
|
end
|
2018-03-01 20:13:04 -05:00
|
|
|
|
|
|
|
it 'does not revise category when no permission to create a topic in category' do
|
|
|
|
category = Fabricate(:category)
|
|
|
|
category.set_permissions(staff: :full)
|
|
|
|
category.save!
|
|
|
|
|
|
|
|
post = create_post
|
|
|
|
old_id = post.topic.category_id
|
|
|
|
|
|
|
|
post.revise(post.user, category_id: category.id)
|
|
|
|
|
|
|
|
post.reload
|
|
|
|
expect(post.topic.category_id).to eq(old_id)
|
|
|
|
|
|
|
|
category.set_permissions(everyone: :full)
|
|
|
|
category.save!
|
|
|
|
|
|
|
|
post.revise(post.user, category_id: category.id)
|
|
|
|
|
|
|
|
post.reload
|
|
|
|
expect(post.topic.category_id).to eq(category.id)
|
|
|
|
end
|
|
|
|
|
2018-07-12 22:51:08 -04:00
|
|
|
it 'does not revise category when the destination category requires topic approval' do
|
|
|
|
new_category = Fabricate(:category)
|
|
|
|
new_category.custom_fields[Category::REQUIRE_TOPIC_APPROVAL] = true
|
|
|
|
new_category.save!
|
|
|
|
|
|
|
|
post = create_post
|
|
|
|
old_category_id = post.topic.category_id
|
|
|
|
|
|
|
|
post.revise(post.user, category_id: new_category.id)
|
|
|
|
expect(post.reload.topic.category_id).to eq(old_category_id)
|
|
|
|
|
|
|
|
new_category.custom_fields[Category::REQUIRE_TOPIC_APPROVAL] = false
|
|
|
|
new_category.save!
|
|
|
|
|
|
|
|
post.revise(post.user, category_id: new_category.id)
|
|
|
|
expect(post.reload.topic.category_id).to eq(new_category.id)
|
2021-03-02 18:59:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not revise category if incorrect amount of tags' do
|
|
|
|
SiteSetting.min_trust_to_create_tag = 0
|
|
|
|
SiteSetting.min_trust_level_to_tag_topics = 0
|
|
|
|
|
|
|
|
new_category = Fabricate(:category, minimum_required_tags: 1)
|
|
|
|
|
|
|
|
post = create_post
|
|
|
|
old_category_id = post.topic.category_id
|
|
|
|
|
|
|
|
post.revise(post.user, category_id: new_category.id)
|
|
|
|
expect(post.reload.topic.category_id).to eq(old_category_id)
|
|
|
|
|
|
|
|
tag = Fabricate(:tag)
|
|
|
|
topic_tag = Fabricate(:topic_tag, topic: post.topic, tag: tag)
|
|
|
|
post.revise(post.user, category_id: new_category.id)
|
|
|
|
expect(post.reload.topic.category_id).to eq(new_category.id)
|
|
|
|
topic_tag.destroy
|
|
|
|
|
|
|
|
post.revise(post.user, category_id: new_category.id, tags: ['test_tag'])
|
|
|
|
expect(post.reload.topic.category_id).to eq(new_category.id)
|
2018-07-12 22:51:08 -04:00
|
|
|
end
|
2018-03-01 20:13:04 -05:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe 'editing tags' do
|
2022-07-15 02:14:46 -04:00
|
|
|
fab!(:post) { Fabricate(:post) }
|
|
|
|
|
|
|
|
subject { PostRevisor.new(post) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
Jobs.run_immediately!
|
|
|
|
|
|
|
|
TopicUser.change(
|
|
|
|
newuser.id,
|
|
|
|
post.topic_id,
|
|
|
|
notification_level: TopicUser.notification_levels[:watching]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates notifications' do
|
|
|
|
expect { subject.revise!(admin, tags: ['new-tag']) }
|
|
|
|
.to change { Notification.count }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'skips notifications if disable_tags_edit_notifications' do
|
|
|
|
SiteSetting.disable_tags_edit_notifications = true
|
|
|
|
|
|
|
|
expect { subject.revise!(admin, tags: ['new-tag']) }
|
2022-07-19 10:03:03 -04:00
|
|
|
.not_to change { Notification.count }
|
2022-07-15 02:14:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe 'revise wiki' do
|
2015-02-02 12:44:21 -05:00
|
|
|
before do
|
|
|
|
# There used to be a bug where wiki changes were considered posting "too similar"
|
|
|
|
# so this is enabled and checked
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.delete_prefixed('unique-post')
|
2015-02-02 12:44:21 -05:00
|
|
|
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 11:18:35 -04:00
|
|
|
expect(post.revise(post.user, wiki: true)).to be_truthy
|
2015-02-02 12:44:21 -05:00
|
|
|
post.reload
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(post.wiki).to be_truthy
|
2015-02-02 12:44:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe 'revise' do
|
2019-05-08 23:28:28 -04:00
|
|
|
let(:post) { Fabricate(:post, post_args) }
|
2013-02-09 10:33:07 -05:00
|
|
|
let(:first_version_at) { post.last_version_at }
|
|
|
|
|
2018-06-05 03:29:17 -04:00
|
|
|
subject { PostRevisor.new(post) }
|
2013-02-09 10:33:07 -05:00
|
|
|
|
2021-11-09 09:29:37 -05:00
|
|
|
it 'destroys last revision if edit is undone' do
|
|
|
|
old_raw = post.raw
|
|
|
|
|
|
|
|
subject.revise!(admin, raw: 'new post body', tags: ['new-tag'])
|
|
|
|
expect(post.topic.reload.tags.map(&:name)).to contain_exactly('new-tag')
|
|
|
|
expect(post.post_revisions.reload.size).to eq(1)
|
|
|
|
|
|
|
|
subject.revise!(admin, raw: old_raw, tags: [])
|
|
|
|
expect(post.topic.reload.tags.map(&:name)).to be_empty
|
|
|
|
expect(post.post_revisions.reload.size).to eq(0)
|
|
|
|
|
|
|
|
subject.revise!(admin, raw: 'next post body', tags: ['new-tag'])
|
|
|
|
expect(post.topic.reload.tags.map(&:name)).to contain_exactly('new-tag')
|
|
|
|
expect(post.post_revisions.reload.size).to eq(1)
|
|
|
|
end
|
|
|
|
|
2013-02-09 10:33:07 -05:00
|
|
|
describe 'with the same body' do
|
2013-12-11 21:41:34 -05:00
|
|
|
it "doesn't change version" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect {
|
|
|
|
expect(subject.revise!(post.user, raw: post.raw)).to eq(false)
|
2014-06-15 22:13:28 -04:00
|
|
|
post.reload
|
2015-01-09 11:34:37 -05:00
|
|
|
}.not_to change(post, :version)
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-12 15:28:38 -05:00
|
|
|
describe 'with nil raw contents' do
|
|
|
|
it "doesn't change version" do
|
|
|
|
expect {
|
|
|
|
expect(subject.revise!(post.user, raw: nil)).to eq(false)
|
|
|
|
post.reload
|
|
|
|
}.not_to change(post, :version)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-28 15:47:50 -04:00
|
|
|
describe 'topic is in slow mode' do
|
|
|
|
before do
|
|
|
|
topic.update!(slow_mode_seconds: 1000)
|
|
|
|
end
|
|
|
|
|
2021-08-05 14:07:29 -04:00
|
|
|
it 'regular edits are not allowed by default' do
|
|
|
|
subject.revise!(
|
|
|
|
post.user,
|
|
|
|
{ raw: 'updated body' },
|
|
|
|
revised_at: post.updated_at + 1000.minutes
|
|
|
|
)
|
2020-10-28 15:47:50 -04:00
|
|
|
|
2021-08-05 14:07:29 -04:00
|
|
|
post.reload
|
2020-10-28 15:47:50 -04:00
|
|
|
expect(post.errors.present?).to eq(true)
|
|
|
|
expect(post.errors.messages[:base].first).to be I18n.t("cannot_edit_on_slow_mode")
|
|
|
|
end
|
|
|
|
|
2021-06-30 21:27:11 -04:00
|
|
|
it 'grace period editing is allowed' do
|
2020-10-28 15:47:50 -04:00
|
|
|
SiteSetting.editing_grace_period = 1.minute
|
|
|
|
|
2021-08-05 14:07:29 -04:00
|
|
|
subject.revise!(
|
|
|
|
post.user,
|
|
|
|
{ raw: 'updated body' },
|
|
|
|
revised_at: post.updated_at + 10.seconds
|
|
|
|
)
|
2020-10-28 15:47:50 -04:00
|
|
|
|
|
|
|
post.reload
|
|
|
|
expect(post.errors).to be_empty
|
|
|
|
end
|
|
|
|
|
2021-08-05 14:07:29 -04:00
|
|
|
it 'regular edits are allowed if it was turned on in settings' do
|
2021-06-26 20:09:53 -04:00
|
|
|
SiteSetting.slow_mode_prevents_editing = false
|
|
|
|
|
2021-08-05 14:07:29 -04:00
|
|
|
subject.revise!(
|
|
|
|
post.user,
|
|
|
|
{ raw: 'updated body' },
|
|
|
|
revised_at: post.updated_at + 10.minutes
|
|
|
|
)
|
2021-06-26 20:09:53 -04:00
|
|
|
|
|
|
|
post.reload
|
|
|
|
expect(post.errors).to be_empty
|
|
|
|
end
|
|
|
|
|
2020-10-28 15:47:50 -04:00
|
|
|
it 'staff is allowed to edit posts even if the topic is in slow mode' do
|
|
|
|
admin = Fabricate(:admin)
|
2021-08-05 14:07:29 -04:00
|
|
|
subject.revise!(
|
|
|
|
admin,
|
|
|
|
{ raw: 'updated body' },
|
|
|
|
revised_at: post.updated_at + 10.minutes
|
|
|
|
)
|
2020-10-28 15:47:50 -04:00
|
|
|
|
|
|
|
post.reload
|
|
|
|
expect(post.errors).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-30 21:27:11 -04:00
|
|
|
describe 'grace period editing' do
|
2014-06-15 22:13:28 -04:00
|
|
|
it 'correctly applies edits' do
|
2017-07-07 02:09:14 -04:00
|
|
|
SiteSetting.editing_grace_period = 1.minute
|
2015-05-29 14:08:39 -04:00
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
subject.revise!(post.user, { raw: 'updated body' }, revised_at: post.updated_at + 10.seconds)
|
2013-02-09 10:33:07 -05:00
|
|
|
post.reload
|
|
|
|
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(post.version).to eq(1)
|
|
|
|
expect(post.public_version).to eq(1)
|
|
|
|
expect(post.revisions.size).to eq(0)
|
2020-03-10 17:13:17 -04:00
|
|
|
expect(post.last_version_at).to eq_time(first_version_at)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(subject.category_changed).to be_blank
|
2013-02-21 18:09:56 -05:00
|
|
|
end
|
2015-05-29 14:08:39 -04:00
|
|
|
|
2018-03-07 00:44:21 -05:00
|
|
|
it "does create a new version if a large diff happens" do
|
|
|
|
SiteSetting.editing_grace_period_max_diff = 10
|
|
|
|
|
|
|
|
post = Fabricate(:post, raw: 'hello world')
|
|
|
|
revisor = PostRevisor.new(post)
|
|
|
|
revisor.revise!(post.user, { raw: 'hello world123456789' }, revised_at: post.updated_at + 1.second)
|
|
|
|
|
|
|
|
post.reload
|
|
|
|
|
|
|
|
expect(post.version).to eq(1)
|
|
|
|
|
|
|
|
revisor = PostRevisor.new(post)
|
|
|
|
revisor.revise!(post.user, { raw: 'hello world12345678901' }, revised_at: post.updated_at + 1.second)
|
|
|
|
|
|
|
|
post.reload
|
|
|
|
expect(post.version).to eq(2)
|
|
|
|
|
|
|
|
expect(post.revisions.first.modifications["raw"][0]).to eq("hello world")
|
|
|
|
expect(post.revisions.first.modifications["cooked"][0]).to eq("<p>hello world</p>")
|
2018-03-08 19:58:50 -05:00
|
|
|
|
|
|
|
SiteSetting.editing_grace_period_max_diff_high_trust = 100
|
|
|
|
|
|
|
|
post.user.update_columns(trust_level: 2)
|
|
|
|
|
|
|
|
revisor = PostRevisor.new(post)
|
|
|
|
revisor.revise!(post.user, { raw: 'hello world12345678901 123456789012' }, revised_at: post.updated_at + 1.second)
|
|
|
|
|
|
|
|
post.reload
|
|
|
|
expect(post.version).to eq(2)
|
|
|
|
expect(post.revisions.count).to eq(1)
|
|
|
|
|
2018-03-07 00:44:21 -05:00
|
|
|
end
|
|
|
|
|
2015-05-29 14:08:39 -04:00
|
|
|
it "doesn't create a new version" do
|
2017-07-07 02:09:14 -04:00
|
|
|
SiteSetting.editing_grace_period = 1.minute
|
2018-03-07 00:44:21 -05:00
|
|
|
SiteSetting.editing_grace_period_max_diff = 100
|
2015-05-29 14:08:39 -04:00
|
|
|
|
|
|
|
# making a revision
|
2015-11-24 14:28:42 -05:00
|
|
|
subject.revise!(post.user, { raw: 'updated body' }, revised_at: post.updated_at + SiteSetting.editing_grace_period + 1.seconds)
|
2015-05-29 14:08:39 -04:00
|
|
|
# "roll back"
|
2015-11-24 14:28:42 -05:00
|
|
|
subject.revise!(post.user, { raw: 'Hello world' }, revised_at: post.updated_at + SiteSetting.editing_grace_period + 2.seconds)
|
2015-05-29 14:08:39 -04:00
|
|
|
|
|
|
|
post.reload
|
|
|
|
|
|
|
|
expect(post.version).to eq(1)
|
|
|
|
expect(post.public_version).to eq(1)
|
|
|
|
expect(post.revisions.size).to eq(0)
|
|
|
|
end
|
2019-10-21 08:27:31 -04:00
|
|
|
|
|
|
|
it "should bump the topic" do
|
|
|
|
expect {
|
|
|
|
subject.revise!(post.user, { raw: 'updated body' }, revised_at: post.updated_at + SiteSetting.editing_grace_period + 1.seconds)
|
|
|
|
}.to change { post.topic.bumped_at }
|
|
|
|
end
|
2020-04-30 18:33:57 -04:00
|
|
|
|
|
|
|
it "should send muted and latest message" do
|
|
|
|
TopicUser.create!(topic: post.topic, user: post.user, notification_level: 0)
|
|
|
|
messages = MessageBus.track_publish("/latest") do
|
|
|
|
subject.revise!(post.user, { raw: 'updated body' }, revised_at: post.updated_at + SiteSetting.editing_grace_period + 1.seconds)
|
|
|
|
end
|
|
|
|
|
|
|
|
muted_message = messages.find { |message| message.data["message_type"] == "muted" }
|
|
|
|
latest_message = messages.find { |message| message.data["message_type"] == "latest" }
|
|
|
|
|
|
|
|
expect(muted_message.data["topic_id"]).to eq(topic.id)
|
|
|
|
expect(latest_message.data["topic_id"]).to eq(topic.id)
|
|
|
|
end
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
2019-11-17 22:08:54 -05:00
|
|
|
describe 'edit reasons' do
|
|
|
|
it "does create a new version if an edit reason is provided" do
|
|
|
|
post = Fabricate(:post, raw: 'hello world')
|
|
|
|
revisor = PostRevisor.new(post)
|
|
|
|
revisor.revise!(post.user, { raw: 'hello world123456789', edit_reason: 'this is my reason' }, revised_at: post.updated_at + 1.second)
|
|
|
|
post.reload
|
|
|
|
expect(post.version).to eq(2)
|
|
|
|
expect(post.revisions.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
2020-02-06 23:10:16 -05:00
|
|
|
it "resets the edit_reason attribute in post model" do
|
2020-03-10 17:13:17 -04:00
|
|
|
freeze_time
|
2022-07-24 20:32:15 -04:00
|
|
|
SiteSetting.editing_grace_period = 5.seconds
|
2020-02-06 23:10:16 -05:00
|
|
|
post = Fabricate(:post, raw: 'hello world')
|
|
|
|
revisor = PostRevisor.new(post)
|
|
|
|
revisor.revise!(post.user, { raw: 'hello world123456789', edit_reason: 'this is my reason' }, revised_at: post.updated_at + 1.second)
|
|
|
|
post.reload
|
|
|
|
expect(post.edit_reason).to eq('this is my reason')
|
|
|
|
|
2020-03-10 17:13:17 -04:00
|
|
|
revisor.revise!(post.user, { raw: 'hello world4321' }, revised_at: post.updated_at + 7.seconds)
|
2020-02-06 23:10:16 -05:00
|
|
|
post.reload
|
|
|
|
expect(post.edit_reason).not_to be_present
|
|
|
|
end
|
|
|
|
|
2019-11-17 22:08:54 -05:00
|
|
|
it "does not create a new version if an edit reason is provided and its the same as the current edit reason" do
|
|
|
|
post = Fabricate(:post, raw: 'hello world', edit_reason: 'this is my reason')
|
|
|
|
revisor = PostRevisor.new(post)
|
|
|
|
revisor.revise!(post.user, { raw: 'hello world123456789', edit_reason: 'this is my reason' }, revised_at: post.updated_at + 1.second)
|
|
|
|
post.reload
|
|
|
|
expect(post.version).to eq(1)
|
|
|
|
expect(post.revisions.count).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not clobber the existing edit reason for a revision if it is not provided in a subsequent revision" do
|
|
|
|
post = Fabricate(:post, raw: 'hello world')
|
|
|
|
revisor = PostRevisor.new(post)
|
|
|
|
revisor.revise!(post.user, { raw: 'hello world123456789', edit_reason: 'this is my reason' }, revised_at: post.updated_at + 1.second)
|
|
|
|
post.reload
|
|
|
|
revisor.revise!(post.user, { raw: 'hello some other thing' }, revised_at: post.updated_at + 1.second)
|
|
|
|
expect(post.revisions.first.modifications[:edit_reason]).to eq([nil, 'this is my reason'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-27 11:05:48 -05:00
|
|
|
describe 'hidden post' do
|
|
|
|
it "correctly stores the modification value" do
|
|
|
|
post.update(hidden: true, hidden_reason_id: Post.hidden_reasons[:flag_threshold_reached])
|
|
|
|
revisor = PostRevisor.new(post)
|
|
|
|
revisor.revise!(post.user, { raw: 'hello world' }, revised_at: post.updated_at + 11.minutes)
|
|
|
|
expect(post.revisions.first.modifications.symbolize_keys).to eq(
|
|
|
|
cooked: ["<p>Hello world</p>", "<p>hello world</p>"],
|
|
|
|
raw: ["Hello world", "hello world"]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-09 10:33:07 -05:00
|
|
|
describe 'revision much later' do
|
|
|
|
|
|
|
|
let!(:revised_at) { post.updated_at + 2.minutes }
|
|
|
|
|
|
|
|
before do
|
2017-07-07 02:09:14 -04:00
|
|
|
SiteSetting.editing_grace_period = 1.minute
|
2014-10-27 17:06:43 -04:00
|
|
|
subject.revise!(post.user, { raw: 'updated body' }, revised_at: revised_at)
|
2013-02-09 10:33:07 -05:00
|
|
|
post.reload
|
|
|
|
end
|
|
|
|
|
2013-02-21 18:09:56 -05:00
|
|
|
it "doesn't update a category" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(subject.category_changed).to be_blank
|
2013-02-21 18:09:56 -05:00
|
|
|
end
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
it 'updates the versions' do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(post.version).to eq(2)
|
|
|
|
expect(post.public_version).to eq(2)
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
it 'creates a new revision' do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(post.revisions.size).to eq(1)
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "updates the last_version_at" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(post.last_version_at.to_i).to eq(revised_at.to_i)
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "new edit window" do
|
|
|
|
|
|
|
|
before do
|
2014-10-27 17:06:43 -04:00
|
|
|
subject.revise!(post.user, { raw: 'yet another updated body' }, revised_at: revised_at)
|
2013-02-09 10:33:07 -05:00
|
|
|
post.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't create a new version if you do another" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(post.version).to eq(2)
|
|
|
|
expect(post.public_version).to eq(2)
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't change last_version_at" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(post.last_version_at.to_i).to eq(revised_at.to_i)
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
2013-02-21 18:09:56 -05:00
|
|
|
it "doesn't update a category" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(subject.category_changed).to be_blank
|
2013-02-21 18:09:56 -05:00
|
|
|
end
|
|
|
|
|
2013-02-09 10:33:07 -05:00
|
|
|
context "after second window" do
|
|
|
|
|
|
|
|
let!(:new_revised_at) { revised_at + 2.minutes }
|
|
|
|
|
|
|
|
before do
|
2014-10-27 17:06:43 -04:00
|
|
|
subject.revise!(post.user, { raw: 'yet another, another updated body' }, revised_at: new_revised_at)
|
2013-02-09 10:33:07 -05:00
|
|
|
post.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does create a new version after the edit window" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(post.version).to eq(3)
|
|
|
|
expect(post.public_version).to eq(3)
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does create a new version after the edit window" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(post.last_version_at.to_i).to eq(new_revised_at.to_i)
|
2013-02-09 10:33:07 -05:00
|
|
|
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." }
|
|
|
|
|
2014-09-25 11:44:48 -04:00
|
|
|
it "should have no description by default" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(category.description).to be_blank
|
2013-02-21 18:09:56 -05:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with one paragraph description" do
|
2013-02-21 18:09:56 -05:00
|
|
|
before do
|
2014-10-27 17:06:43 -04:00
|
|
|
subject.revise!(post.user, raw: new_description)
|
2013-02-21 18:09:56 -05:00
|
|
|
category.reload
|
|
|
|
end
|
|
|
|
|
2014-09-25 11:44:48 -04:00
|
|
|
it "returns the changed category info" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(subject.category_changed).to eq(category)
|
2013-02-21 18:09:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "updates the description of the category" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(category.description).to eq(new_description)
|
2013-02-21 18:09:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with multiple paragraph description" do
|
2013-02-21 18:09:56 -05:00
|
|
|
before do
|
2014-10-27 17:06:43 -04:00
|
|
|
subject.revise!(post.user, raw: "#{new_description}\n\nOther content goes here.")
|
2013-02-21 18:09:56 -05:00
|
|
|
category.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the changed category info" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(subject.category_changed).to eq(category)
|
2013-02-21 18:09:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "updates the description of the category" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(category.description).to eq(new_description)
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-21 18:09:56 -05:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with invalid description without paragraphs" do
|
2017-07-21 15:07:29 -04:00
|
|
|
before do
|
|
|
|
subject.revise!(post.user, raw: "# This is a title")
|
|
|
|
category.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a error for the user" do
|
|
|
|
expect(post.errors.present?).to eq(true)
|
|
|
|
expect(post.errors.messages[:base].first).to be I18n.t("category.errors.description_incomplete")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't update the description of the category" do
|
|
|
|
expect(category.description).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-21 18:09:56 -05:00
|
|
|
context 'when updating back to the original paragraph' do
|
|
|
|
before do
|
|
|
|
category.update_column(:description, 'this is my description')
|
2014-10-27 17:06:43 -04:00
|
|
|
subject.revise!(post.user, raw: Category.post_template)
|
2013-02-21 18:09:56 -05:00
|
|
|
category.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it "puts the description back to nothing" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(category.description).to be_blank
|
2013-02-21 18:09:56 -05:00
|
|
|
end
|
|
|
|
|
2014-09-25 11:44:48 -04:00
|
|
|
it "returns the changed category info" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(subject.category_changed).to eq(category)
|
2013-02-21 18:09:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-09 10:33:07 -05:00
|
|
|
describe 'rate limiter' do
|
2021-12-15 12:41:14 -05:00
|
|
|
fab!(:changed_by) { coding_horror }
|
2013-02-09 10:33:07 -05:00
|
|
|
|
2021-05-19 05:57:21 -04:00
|
|
|
before do
|
|
|
|
RateLimiter.enable
|
|
|
|
RateLimiter.clear_all!
|
|
|
|
SiteSetting.editing_grace_period = 0
|
|
|
|
end
|
|
|
|
|
2013-02-09 10:33:07 -05:00
|
|
|
it "triggers a rate limiter" do
|
|
|
|
EditRateLimiter.any_instance.expects(:performed!)
|
2014-10-27 17:06:43 -04:00
|
|
|
subject.revise!(changed_by, raw: 'updated body')
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
2021-05-19 05:57:21 -04:00
|
|
|
|
|
|
|
it "raises error when a user gets rate limited" do
|
|
|
|
SiteSetting.max_edits_per_day = 1
|
|
|
|
user = Fabricate(:user, trust_level: 1)
|
|
|
|
|
|
|
|
subject.revise!(user, raw: 'body (edited)')
|
|
|
|
|
|
|
|
expect do
|
|
|
|
subject.revise!(user, raw: 'body (edited twice) ')
|
|
|
|
end.to raise_error(RateLimiter::LimitExceeded)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "edit limits scale up depending on user's trust level" do
|
|
|
|
SiteSetting.max_edits_per_day = 1
|
|
|
|
SiteSetting.tl2_additional_edits_per_day_multiplier = 2
|
|
|
|
SiteSetting.tl3_additional_edits_per_day_multiplier = 3
|
|
|
|
SiteSetting.tl4_additional_edits_per_day_multiplier = 4
|
|
|
|
|
|
|
|
user = Fabricate(:user, trust_level: 2)
|
|
|
|
expect { subject.revise!(user, raw: 'body (edited)') }.to_not raise_error
|
|
|
|
expect { subject.revise!(user, raw: 'body (edited twice)') }.to_not raise_error
|
|
|
|
expect do
|
|
|
|
subject.revise!(user, raw: 'body (edited three times) ')
|
|
|
|
end.to raise_error(RateLimiter::LimitExceeded)
|
|
|
|
|
|
|
|
user = Fabricate(:user, trust_level: 3)
|
|
|
|
expect { subject.revise!(user, raw: 'body (edited)') }.to_not raise_error
|
|
|
|
expect { subject.revise!(user, raw: 'body (edited twice)') }.to_not raise_error
|
|
|
|
expect { subject.revise!(user, raw: 'body (edited three times)') }.to_not raise_error
|
|
|
|
expect do
|
|
|
|
subject.revise!(user, raw: 'body (edited four times) ')
|
|
|
|
end.to raise_error(RateLimiter::LimitExceeded)
|
|
|
|
|
|
|
|
user = Fabricate(:user, trust_level: 4)
|
|
|
|
expect { subject.revise!(user, raw: 'body (edited)') }.to_not raise_error
|
|
|
|
expect { subject.revise!(user, raw: 'body (edited twice)') }.to_not raise_error
|
|
|
|
expect { subject.revise!(user, raw: 'body (edited three times)') }.to_not raise_error
|
|
|
|
expect { subject.revise!(user, raw: 'body (edited four times)') }.to_not raise_error
|
|
|
|
expect do
|
|
|
|
subject.revise!(user, raw: 'body (edited five times) ')
|
|
|
|
end.to raise_error(RateLimiter::LimitExceeded)
|
|
|
|
end
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
|
2013-04-17 19:11:13 -04:00
|
|
|
describe "admin editing a new user's post" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:changed_by) { Fabricate(:admin) }
|
2013-04-05 13:59:00 -04:00
|
|
|
|
|
|
|
before do
|
2020-08-07 12:08:59 -04:00
|
|
|
SiteSetting.newuser_max_embedded_media = 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}'>")
|
2014-10-27 17:06:43 -04:00
|
|
|
subject.revise!(changed_by, raw: "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
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(post.errors).to be_blank
|
2013-04-05 13:59:00 -04:00
|
|
|
end
|
2013-11-06 05:43:40 -05:00
|
|
|
|
|
|
|
it "marks the admin as the last updater" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(post.last_editor_id).to eq(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
|
2020-08-07 12:08:59 -04:00
|
|
|
SiteSetting.newuser_max_embedded_media = 0
|
2013-10-23 23:55:55 -04:00
|
|
|
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}'>")
|
2014-10-27 17:06:43 -04:00
|
|
|
subject.revise!(post.user, raw: "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
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(post.errors).to be_present
|
2013-04-05 13:59:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-09 10:33:07 -05:00
|
|
|
describe 'with a new body' do
|
2018-03-07 00:44:21 -05:00
|
|
|
before do
|
|
|
|
SiteSetting.editing_grace_period_max_diff = 1000
|
|
|
|
end
|
|
|
|
|
2021-12-15 12:41:14 -05:00
|
|
|
fab!(:changed_by) { coding_horror }
|
2016-01-11 05:16:23 -05:00
|
|
|
let!(:result) { subject.revise!(changed_by, raw: "lets update the body. Здравствуйте") }
|
2013-02-09 10:33:07 -05:00
|
|
|
|
2018-03-07 00:44:21 -05:00
|
|
|
it 'correctly updates raw' do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(result).to eq(true)
|
2016-01-11 05:16:23 -05:00
|
|
|
expect(post.raw).to eq("lets update the body. Здравствуйте")
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(post.invalidate_oneboxes).to eq(true)
|
|
|
|
expect(post.version).to eq(2)
|
|
|
|
expect(post.public_version).to eq(2)
|
|
|
|
expect(post.revisions.size).to eq(1)
|
|
|
|
expect(post.revisions.first.user_id).to eq(changed_by.id)
|
2013-02-09 10:33:07 -05:00
|
|
|
|
2018-03-07 00:44:21 -05:00
|
|
|
# updates word count
|
2016-01-11 05:16:23 -05:00
|
|
|
expect(post.word_count).to eq(5)
|
2013-12-10 13:47:07 -05:00
|
|
|
post.topic.reload
|
2016-01-11 05:16:23 -05:00
|
|
|
expect(post.topic.word_count).to eq(5)
|
2013-12-10 13:47:07 -05:00
|
|
|
end
|
|
|
|
|
2021-08-02 10:15:53 -04:00
|
|
|
it 'increases the post_edits stat count' do
|
|
|
|
expect do
|
|
|
|
subject.revise!(post.user, { raw: "This is a new revision" })
|
|
|
|
end.to change { post.user.user_stat.post_edits_count.to_i }.by(1)
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'when second poster posts again quickly' do
|
2021-06-30 21:27:11 -04:00
|
|
|
it 'is a grace period edit, because the second poster posted again quickly' do
|
2017-07-07 02:09:14 -04:00
|
|
|
SiteSetting.editing_grace_period = 1.minute
|
2014-10-27 17:06:43 -04:00
|
|
|
subject.revise!(changed_by, { raw: 'yet another updated body' }, revised_at: post.updated_at + 10.seconds)
|
2013-02-09 10:33:07 -05:00
|
|
|
post.reload
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(post.version).to eq(2)
|
|
|
|
expect(post.public_version).to eq(2)
|
|
|
|
expect(post.revisions.size).to eq(1)
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
|
|
|
end
|
2016-08-19 15:27:12 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'when passing skip_revision as true' do
|
2016-08-19 15:27:12 -04:00
|
|
|
before do
|
2017-07-07 02:09:14 -04:00
|
|
|
SiteSetting.editing_grace_period = 1.minute
|
2016-08-19 15:27:12 -04:00
|
|
|
subject.revise!(changed_by, { raw: 'yet another updated body' }, revised_at: post.updated_at + 10.hours, skip_revision: true)
|
|
|
|
post.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create new revision ' do
|
|
|
|
expect(post.version).to eq(2)
|
|
|
|
expect(post.public_version).to eq(2)
|
|
|
|
expect(post.revisions.size).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
2022-11-21 11:11:29 -05:00
|
|
|
|
|
|
|
context 'when editing the before_edit_post event signature' do
|
|
|
|
it 'contains post and params' do
|
|
|
|
params = { raw: 'body (edited)' }
|
|
|
|
events = DiscourseEvent.track_events { subject.revise!(user, params) }
|
|
|
|
expect(events).to include(
|
|
|
|
event_name: :before_edit_post,
|
|
|
|
params: [post, params]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2013-02-09 10:33:07 -05:00
|
|
|
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
|
2018-06-05 03:29:17 -04:00
|
|
|
revisor = PostRevisor.new(post)
|
2022-09-01 00:14:21 -04:00
|
|
|
first_post = topic.first_post
|
2014-03-18 13:40:40 -04:00
|
|
|
expect {
|
2014-10-27 17:06:43 -04:00
|
|
|
revisor.revise!(first_post.user, { raw: 'Edit the first post' }, revised_at: first_post.updated_at + 10.seconds)
|
2014-03-18 13:40:40 -04:00
|
|
|
topic.reload
|
|
|
|
}.to change { topic.excerpt }
|
|
|
|
second_post = Fabricate(:post, post_args.merge(post_number: 2, topic_id: topic.id))
|
|
|
|
expect {
|
2018-06-05 03:29:17 -04:00
|
|
|
PostRevisor.new(second_post).revise!(second_post.user, raw: 'Edit the 2nd post')
|
2014-03-18 13:40:40 -04:00
|
|
|
topic.reload
|
|
|
|
}.to_not change { topic.excerpt }
|
|
|
|
end
|
|
|
|
end
|
2014-09-01 19:18:06 -04:00
|
|
|
|
2022-09-01 00:14:21 -04:00
|
|
|
describe "welcome topic" do
|
|
|
|
before do
|
|
|
|
SiteSetting.welcome_topic_id = topic.id
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should publish welcome topic edit message" do
|
|
|
|
revisor = PostRevisor.new(post)
|
|
|
|
first_post = topic.first_post
|
|
|
|
UserAuthToken.generate!(user_id: admin.id)
|
|
|
|
Discourse.cache.write(Site.welcome_topic_banner_cache_key(admin.id), true)
|
|
|
|
|
|
|
|
messages = MessageBus.track_publish("/site/welcome-topic-banner") do
|
|
|
|
revisor.revise!(admin, { raw: 'updated welcome topic body' })
|
|
|
|
end
|
|
|
|
welcome_topic_banner_message = messages.find { |message| message.channel == "/site/welcome-topic-banner" }
|
|
|
|
expect(welcome_topic_banner_message).to be_present
|
|
|
|
expect(welcome_topic_banner_message.data).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-01 19:18:06 -04:00
|
|
|
it "doesn't strip starting whitespaces" do
|
2014-10-27 17:06:43 -04:00
|
|
|
subject.revise!(post.user, raw: " <-- whitespaces --> ")
|
2014-09-01 19:18:06 -04:00
|
|
|
post.reload
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(post.raw).to eq(" <-- whitespaces -->")
|
2014-09-01 19:18:06 -04:00
|
|
|
end
|
|
|
|
|
2021-08-05 05:38:39 -04:00
|
|
|
it "revises and tracks changes of topic titles" do
|
|
|
|
new_title = "New topic title"
|
|
|
|
result = subject.revise!(
|
|
|
|
post.user,
|
|
|
|
{ title: new_title },
|
|
|
|
revised_at: post.updated_at + 10.minutes
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(result).to eq(true)
|
|
|
|
post.reload
|
|
|
|
expect(post.topic.title).to eq(new_title)
|
|
|
|
expect(post.revisions.first.modifications["title"][1]).to eq(new_title)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "revises and tracks changes of topic archetypes" do
|
|
|
|
new_archetype = Archetype.banner
|
|
|
|
result = subject.revise!(
|
|
|
|
post.user,
|
|
|
|
{ archetype: new_archetype },
|
|
|
|
revised_at: post.updated_at + 10.minutes
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(result).to eq(true)
|
|
|
|
post.reload
|
|
|
|
expect(post.topic.archetype).to eq(new_archetype)
|
|
|
|
expect(post.revisions.first.modifications["archetype"][1]).to eq(new_archetype)
|
|
|
|
end
|
|
|
|
|
2021-11-09 09:29:37 -05:00
|
|
|
it "revises and tracks changes of topic tags" do
|
|
|
|
subject.revise!(admin, tags: ['new-tag'])
|
|
|
|
expect(post.post_revisions.last.modifications).to eq('tags' => [[], ['new-tag']])
|
|
|
|
|
|
|
|
subject.revise!(admin, tags: ['new-tag', 'new-tag-2'])
|
|
|
|
expect(post.post_revisions.last.modifications).to eq('tags' => [[], ['new-tag', 'new-tag-2']])
|
|
|
|
|
|
|
|
subject.revise!(admin, tags: ['new-tag-3'])
|
|
|
|
expect(post.post_revisions.last.modifications).to eq('tags' => [[], ['new-tag-3']])
|
|
|
|
end
|
|
|
|
|
2022-07-27 06:21:10 -04:00
|
|
|
describe "#publish_changes" do
|
2018-06-05 03:29:17 -04:00
|
|
|
let!(:post) { Fabricate(:post, topic: topic) }
|
2016-04-07 10:29:01 -04:00
|
|
|
|
|
|
|
it "should publish topic changes to clients" do
|
2018-06-05 03:29:17 -04:00
|
|
|
revisor = PostRevisor.new(topic.ordered_posts.first, topic)
|
2016-04-07 10:29:01 -04:00
|
|
|
|
2017-10-01 23:34:57 -04:00
|
|
|
message = MessageBus.track_publish("/topic/#{topic.id}") do
|
2016-04-07 10:29:01 -04:00
|
|
|
revisor.revise!(newuser, title: 'this is a test topic')
|
2017-10-01 23:34:57 -04:00
|
|
|
end.first
|
2016-04-07 10:29:01 -04:00
|
|
|
|
|
|
|
payload = message.data
|
|
|
|
expect(payload[:reload_topic]).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2016-05-04 14:02:47 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when logging staff edits" do
|
2018-03-12 13:49:52 -04:00
|
|
|
it "doesn't log when a regular user revises a post" do
|
|
|
|
subject.revise!(
|
|
|
|
post.user,
|
|
|
|
raw: "lets totally update the body"
|
|
|
|
)
|
|
|
|
log = UserHistory.where(
|
|
|
|
acting_user_id: post.user.id,
|
|
|
|
action: UserHistory.actions[:post_edit]
|
|
|
|
)
|
|
|
|
expect(log).to be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
it "logs an edit when a staff member revises a post" do
|
|
|
|
subject.revise!(
|
|
|
|
moderator,
|
|
|
|
raw: "lets totally update the body"
|
|
|
|
)
|
|
|
|
log = UserHistory.where(
|
|
|
|
acting_user_id: moderator.id,
|
|
|
|
action: UserHistory.actions[:post_edit]
|
2018-03-14 15:01:36 -04:00
|
|
|
).first
|
2018-03-12 13:49:52 -04:00
|
|
|
expect(log).to be_present
|
2018-03-14 15:01:36 -04:00
|
|
|
expect(log.details).to eq("Hello world\n\n---\n\nlets totally update the body")
|
2018-03-12 13:49:52 -04:00
|
|
|
end
|
|
|
|
|
2019-09-12 10:55:45 -04:00
|
|
|
it "doesn't log an edit when skip_staff_log is true" do
|
|
|
|
subject.revise!(
|
|
|
|
moderator,
|
|
|
|
{ raw: "lets totally update the body" },
|
|
|
|
skip_staff_log: true
|
|
|
|
)
|
|
|
|
log = UserHistory.where(
|
|
|
|
acting_user_id: moderator.id,
|
|
|
|
action: UserHistory.actions[:post_edit]
|
|
|
|
).first
|
|
|
|
expect(log).to be_blank
|
|
|
|
end
|
|
|
|
|
2018-03-12 13:49:52 -04:00
|
|
|
it "doesn't log an edit when a staff member edits their own post" do
|
|
|
|
revisor = PostRevisor.new(
|
|
|
|
Fabricate(:post, user: moderator)
|
|
|
|
)
|
|
|
|
revisor.revise!(
|
|
|
|
moderator,
|
|
|
|
raw: "my own edit to my own thing"
|
|
|
|
)
|
|
|
|
|
|
|
|
log = UserHistory.where(
|
|
|
|
acting_user_id: moderator.id,
|
|
|
|
action: UserHistory.actions[:post_edit]
|
|
|
|
)
|
|
|
|
expect(log).to be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when logging group moderator edits" do
|
2020-07-23 09:50:00 -04:00
|
|
|
fab!(:group_user) { Fabricate(:group_user) }
|
|
|
|
fab!(:category) { Fabricate(:category, reviewable_by_group_id: group_user.group.id, topic: topic) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.enable_category_group_moderation = true
|
|
|
|
topic.update!(category: category)
|
|
|
|
post.update!(topic: topic)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "logs an edit when a group moderator revises the category description" do
|
|
|
|
PostRevisor.new(post).revise!(group_user.user, raw: "a group moderator can update the description")
|
|
|
|
|
|
|
|
log = UserHistory.where(
|
|
|
|
acting_user_id: group_user.user.id,
|
|
|
|
action: UserHistory.actions[:post_edit]
|
|
|
|
).first
|
|
|
|
expect(log).to be_present
|
|
|
|
expect(log.details).to eq("Hello world\n\n---\n\na group moderator can update the description")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with staff_edit_locks_post" do
|
|
|
|
context "when disabled" do
|
2018-01-26 13:45:52 -05:00
|
|
|
before do
|
|
|
|
SiteSetting.staff_edit_locks_post = false
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not lock the post when revised" do
|
|
|
|
result = subject.revise!(
|
2018-03-12 13:49:52 -04:00
|
|
|
moderator,
|
2018-01-26 13:45:52 -05:00
|
|
|
raw: "lets totally update the body"
|
|
|
|
)
|
|
|
|
expect(result).to eq(true)
|
|
|
|
post.reload
|
|
|
|
expect(post).not_to be_locked
|
2018-03-12 13:49:52 -04:00
|
|
|
|
2018-01-26 13:45:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when enabled" do
|
2018-01-26 13:45:52 -05:00
|
|
|
before do
|
|
|
|
SiteSetting.staff_edit_locks_post = true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "locks the post when revised by staff" do
|
|
|
|
result = subject.revise!(
|
|
|
|
moderator,
|
|
|
|
raw: "lets totally update the body"
|
|
|
|
)
|
|
|
|
expect(result).to eq(true)
|
|
|
|
post.reload
|
|
|
|
expect(post).to be_locked
|
|
|
|
end
|
|
|
|
|
2018-12-02 14:59:00 -05:00
|
|
|
it "doesn't lock the wiki posts" do
|
2018-03-05 14:50:06 -05:00
|
|
|
post.wiki = true
|
|
|
|
result = subject.revise!(
|
|
|
|
moderator,
|
|
|
|
raw: "some new raw content"
|
|
|
|
)
|
|
|
|
expect(result).to eq(true)
|
|
|
|
post.reload
|
|
|
|
expect(post).not_to be_locked
|
|
|
|
end
|
2018-12-02 14:59:00 -05:00
|
|
|
|
2018-03-01 20:33:04 -05:00
|
|
|
it "doesn't lock the post when the raw did not change" do
|
|
|
|
result = subject.revise!(
|
|
|
|
moderator,
|
|
|
|
title: "New topic title, cool!"
|
|
|
|
)
|
|
|
|
expect(result).to eq(true)
|
|
|
|
post.reload
|
|
|
|
expect(post.topic.title).to eq("New topic title, cool!")
|
|
|
|
expect(post).not_to be_locked
|
|
|
|
end
|
|
|
|
|
2018-01-26 13:45:52 -05:00
|
|
|
it "doesn't lock the post when revised by a regular user" do
|
|
|
|
result = subject.revise!(
|
2019-05-06 04:49:11 -04:00
|
|
|
user,
|
2018-01-26 13:45:52 -05:00
|
|
|
raw: "lets totally update the body"
|
|
|
|
)
|
|
|
|
expect(result).to eq(true)
|
|
|
|
post.reload
|
|
|
|
expect(post).not_to be_locked
|
|
|
|
end
|
|
|
|
|
2018-12-02 14:59:00 -05:00
|
|
|
it "doesn't lock the post when revised by system user" do
|
|
|
|
result = subject.revise!(
|
|
|
|
Discourse.system_user,
|
|
|
|
raw: "I usually replace hotlinked images"
|
|
|
|
)
|
|
|
|
expect(result).to eq(true)
|
|
|
|
post.reload
|
|
|
|
expect(post).not_to be_locked
|
|
|
|
end
|
|
|
|
|
2018-01-26 13:45:52 -05:00
|
|
|
it "doesn't lock a staff member's post" do
|
|
|
|
staff_post = Fabricate(:post, user: moderator)
|
|
|
|
revisor = PostRevisor.new(staff_post)
|
|
|
|
|
|
|
|
result = revisor.revise!(
|
|
|
|
moderator,
|
|
|
|
raw: "lets totally update the body"
|
|
|
|
)
|
|
|
|
expect(result).to eq(true)
|
|
|
|
staff_post.reload
|
|
|
|
expect(staff_post).not_to be_locked
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with alerts" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:mentioned_user) { Fabricate(:user) }
|
2018-01-30 16:21:07 -05:00
|
|
|
|
2018-05-31 03:53:49 -04:00
|
|
|
before do
|
2019-03-14 10:47:38 -04:00
|
|
|
Jobs.run_immediately!
|
2018-05-31 03:53:49 -04:00
|
|
|
end
|
|
|
|
|
2018-01-30 16:21:07 -05:00
|
|
|
it "generates a notification for a mention" do
|
|
|
|
expect {
|
2019-05-06 04:49:11 -04:00
|
|
|
subject.revise!(user, raw: "Random user is mentioning @#{mentioned_user.username_lower}")
|
2018-01-30 16:21:07 -05:00
|
|
|
}.to change { Notification.where(notification_type: Notification.types[:mentioned]).count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "never generates a notification for a mention when the System user revise a post" do
|
|
|
|
expect {
|
|
|
|
subject.revise!(Discourse.system_user, raw: "System user is mentioning @#{mentioned_user.username_lower}")
|
|
|
|
}.not_to change { Notification.where(notification_type: Notification.types[:mentioned]).count }
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with tagging" do
|
|
|
|
context "with tagging disabled" do
|
2016-05-04 14:02:47 -04:00
|
|
|
before do
|
|
|
|
SiteSetting.tagging_enabled = false
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't add the tags" do
|
2019-05-06 04:49:11 -04:00
|
|
|
result = subject.revise!(user, raw: "lets totally update the body", tags: ['totally', 'update'])
|
2016-05-04 14:02:47 -04:00
|
|
|
expect(result).to eq(true)
|
|
|
|
post.reload
|
|
|
|
expect(post.topic.tags.size).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with tagging enabled" do
|
2016-05-04 14:02:47 -04:00
|
|
|
before do
|
|
|
|
SiteSetting.tagging_enabled = true
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when can create tags" do
|
2016-05-04 14:02:47 -04:00
|
|
|
before do
|
|
|
|
SiteSetting.min_trust_to_create_tag = 0
|
|
|
|
SiteSetting.min_trust_level_to_tag_topics = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can create all tags if none exist" do
|
|
|
|
expect {
|
2019-05-06 04:49:11 -04:00
|
|
|
@result = subject.revise!(user, raw: "lets totally update the body", tags: ['totally', 'update'])
|
2016-05-04 14:02:47 -04:00
|
|
|
}.to change { Tag.count }.by(2)
|
|
|
|
expect(@result).to eq(true)
|
|
|
|
post.reload
|
|
|
|
expect(post.topic.tags.map(&:name).sort).to eq(['totally', 'update'])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates missing tags if some exist" do
|
|
|
|
Fabricate(:tag, name: 'totally')
|
|
|
|
expect {
|
2019-05-06 04:49:11 -04:00
|
|
|
@result = subject.revise!(user, raw: "lets totally update the body", tags: ['totally', 'update'])
|
2016-05-04 14:02:47 -04:00
|
|
|
}.to change { Tag.count }.by(1)
|
|
|
|
expect(@result).to eq(true)
|
|
|
|
post.reload
|
|
|
|
expect(post.topic.tags.map(&:name).sort).to eq(['totally', 'update'])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can remove all tags" do
|
|
|
|
topic.tags = [Fabricate(:tag, name: "super"), Fabricate(:tag, name: "stuff")]
|
2019-05-06 04:49:11 -04:00
|
|
|
result = subject.revise!(user, raw: "lets totally update the body", tags: [])
|
2016-05-04 14:02:47 -04:00
|
|
|
expect(result).to eq(true)
|
|
|
|
post.reload
|
|
|
|
expect(post.topic.tags.size).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can't add staff-only tags" do
|
2020-10-14 13:15:54 -04:00
|
|
|
create_staff_only_tags(['important'])
|
2019-05-06 04:49:11 -04:00
|
|
|
result = subject.revise!(user, raw: "lets totally update the body", tags: ['important', 'stuff'])
|
2016-05-04 14:02:47 -04:00
|
|
|
expect(result).to eq(false)
|
|
|
|
expect(post.topic.errors.present?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "staff can add staff-only tags" do
|
2020-10-14 13:15:54 -04:00
|
|
|
create_staff_only_tags(['important'])
|
2019-05-06 04:49:11 -04:00
|
|
|
result = subject.revise!(admin, raw: "lets totally update the body", tags: ['important', 'stuff'])
|
2016-05-04 14:02:47 -04:00
|
|
|
expect(result).to eq(true)
|
|
|
|
post.reload
|
|
|
|
expect(post.topic.tags.map(&:name).sort).to eq(['important', 'stuff'])
|
|
|
|
end
|
|
|
|
|
2021-03-24 20:24:50 -04:00
|
|
|
it "triggers the :post_edited event with topic_changed?" do
|
|
|
|
topic.tags = [Fabricate(:tag, name: "super"), Fabricate(:tag, name: "stuff")]
|
|
|
|
|
|
|
|
events = DiscourseEvent.track_events do
|
|
|
|
subject.revise!(user, raw: "lets totally update the body", tags: [])
|
|
|
|
end
|
|
|
|
|
|
|
|
event = events.find { |e| e[:event_name] == :post_edited }
|
|
|
|
|
|
|
|
expect(event[:params].first).to eq(post)
|
|
|
|
expect(event[:params].second).to eq(true)
|
|
|
|
expect(event[:params].third).to be_kind_of(PostRevisor)
|
|
|
|
expect(event[:params].third.topic_diff).to eq({ "tags" => [["super", "stuff"], []] })
|
|
|
|
end
|
|
|
|
|
2016-05-04 14:02:47 -04:00
|
|
|
context "with staff-only tags" do
|
|
|
|
before do
|
2020-10-14 13:15:54 -04:00
|
|
|
create_staff_only_tags(['important'])
|
2016-05-04 14:02:47 -04:00
|
|
|
topic = post.topic
|
2018-04-20 15:25:28 -04:00
|
|
|
topic.tags = [Fabricate(:tag, name: "super"), Tag.where(name: "important").first, Fabricate(:tag, name: "stuff")]
|
2016-05-04 14:02:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "staff-only tags can't be removed" do
|
2019-05-06 04:49:11 -04:00
|
|
|
result = subject.revise!(user, raw: "lets totally update the body", tags: ['stuff'])
|
2016-05-04 14:02:47 -04:00
|
|
|
expect(result).to eq(false)
|
|
|
|
expect(post.topic.errors.present?).to eq(true)
|
|
|
|
post.reload
|
|
|
|
expect(post.topic.tags.map(&:name).sort).to eq(['important', 'stuff', 'super'])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can't remove all tags if some are staff-only" do
|
2019-05-06 04:49:11 -04:00
|
|
|
result = subject.revise!(user, raw: "lets totally update the body", tags: [])
|
2016-05-04 14:02:47 -04:00
|
|
|
expect(result).to eq(false)
|
|
|
|
expect(post.topic.errors.present?).to eq(true)
|
|
|
|
post.reload
|
|
|
|
expect(post.topic.tags.map(&:name).sort).to eq(['important', 'stuff', 'super'])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "staff-only tags can be removed by staff" do
|
2019-05-06 04:49:11 -04:00
|
|
|
result = subject.revise!(admin, raw: "lets totally update the body", tags: ['stuff'])
|
2016-05-04 14:02:47 -04:00
|
|
|
expect(result).to eq(true)
|
|
|
|
post.reload
|
|
|
|
expect(post.topic.tags.map(&:name)).to eq(['stuff'])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "staff can remove all tags" do
|
2019-05-06 04:49:11 -04:00
|
|
|
result = subject.revise!(admin, raw: "lets totally update the body", tags: [])
|
2016-05-04 14:02:47 -04:00
|
|
|
expect(result).to eq(true)
|
|
|
|
post.reload
|
|
|
|
expect(post.topic.tags.size).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with hidden tags" do
|
2019-05-06 14:51:51 -04:00
|
|
|
let(:bumped_at) { 1.day.ago }
|
|
|
|
|
|
|
|
before do
|
2019-09-11 20:41:50 -04:00
|
|
|
topic.update!(bumped_at: bumped_at)
|
2019-05-06 14:51:51 -04:00
|
|
|
create_hidden_tags(['important', 'secret'])
|
|
|
|
topic = post.topic
|
|
|
|
topic.tags = [Fabricate(:tag, name: "super"), Tag.where(name: "important").first, Fabricate(:tag, name: "stuff")]
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't bump topic if only staff-only tags are added" do
|
|
|
|
expect {
|
|
|
|
result = subject.revise!(Fabricate(:admin), raw: post.raw, tags: topic.tags.map(&:name) + ['secret'])
|
|
|
|
expect(result).to eq(true)
|
|
|
|
}.to_not change { topic.reload.bumped_at }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't bump topic if only staff-only tags are removed" do
|
|
|
|
expect {
|
|
|
|
result = subject.revise!(Fabricate(:admin), raw: post.raw, tags: topic.tags.map(&:name) - ['important', 'secret'])
|
|
|
|
expect(result).to eq(true)
|
|
|
|
}.to_not change { topic.reload.bumped_at }
|
|
|
|
end
|
2019-06-04 15:48:06 -04:00
|
|
|
|
|
|
|
it "doesn't bump topic if only staff-only tags are removed and there are no tags left" do
|
|
|
|
topic.tags = Tag.where(name: ['important', 'secret']).to_a
|
|
|
|
expect {
|
|
|
|
result = subject.revise!(Fabricate(:admin), raw: post.raw, tags: [])
|
|
|
|
expect(result).to eq(true)
|
|
|
|
}.to_not change { topic.reload.bumped_at }
|
|
|
|
end
|
|
|
|
|
2019-06-07 14:25:42 -04:00
|
|
|
it "doesn't bump topic if empty string is given" do
|
|
|
|
topic.tags = Tag.where(name: ['important', 'secret']).to_a
|
|
|
|
expect {
|
|
|
|
result = subject.revise!(Fabricate(:admin), raw: post.raw, tags: [""])
|
|
|
|
expect(result).to eq(true)
|
|
|
|
}.to_not change { topic.reload.bumped_at }
|
|
|
|
end
|
|
|
|
|
2019-10-21 08:27:31 -04:00
|
|
|
it "should bump topic if non staff-only tags are added" do
|
|
|
|
expect {
|
|
|
|
result = subject.revise!(Fabricate(:admin), raw: post.raw, tags: topic.tags.map(&:name) + [Fabricate(:tag).name])
|
|
|
|
expect(result).to eq(true)
|
|
|
|
}.to change { topic.reload.bumped_at }
|
|
|
|
end
|
|
|
|
|
2019-06-04 15:48:06 -04:00
|
|
|
it "creates a hidden revision" do
|
|
|
|
subject.revise!(Fabricate(:admin), raw: post.raw, tags: topic.tags.map(&:name) + ['secret'])
|
|
|
|
expect(post.reload.revisions.first.hidden).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't notify topic owner about hidden tags" do
|
|
|
|
PostActionNotifier.enable
|
|
|
|
Jobs.run_immediately!
|
|
|
|
expect {
|
|
|
|
subject.revise!(Fabricate(:admin), raw: post.raw, tags: topic.tags.map(&:name) + ['secret'])
|
|
|
|
}.not_to change { Notification.where(notification_type: Notification.types[:edited]).count }
|
|
|
|
end
|
2019-05-06 14:51:51 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with required tag group" do
|
2019-10-30 14:49:00 -04:00
|
|
|
fab!(:tag1) { Fabricate(:tag) }
|
|
|
|
fab!(:tag2) { Fabricate(:tag) }
|
|
|
|
fab!(:tag3) { Fabricate(:tag) }
|
|
|
|
fab!(:tag_group) { Fabricate(:tag_group, tags: [tag1, tag2]) }
|
2022-04-06 09:08:06 -04:00
|
|
|
fab!(:category) { Fabricate(:category, name: "beta", category_required_tag_groups: [CategoryRequiredTagGroup.new(tag_group: tag_group, min_count: 1)]) }
|
2019-10-30 14:49:00 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
post.topic.update(category: category)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't allow removing all tags from the group" do
|
|
|
|
post.topic.tags = [tag1, tag2]
|
|
|
|
result = subject.revise!(user, raw: "lets totally update the body", tags: [])
|
|
|
|
expect(result).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows removing some tags" do
|
|
|
|
post.topic.tags = [tag1, tag2, tag3]
|
|
|
|
result = subject.revise!(user, raw: "lets totally update the body", tags: [tag1.name])
|
|
|
|
expect(result).to eq(true)
|
|
|
|
expect(post.reload.topic.tags.map(&:name)).to eq([tag1.name])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows admins to remove the tags" do
|
|
|
|
post.topic.tags = [tag1, tag2, tag3]
|
|
|
|
result = subject.revise!(admin, raw: "lets totally update the body", tags: [])
|
|
|
|
expect(result).to eq(true)
|
|
|
|
expect(post.reload.topic.tags.size).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
2016-05-04 14:02:47 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when cannot create tags" do
|
2016-05-04 14:02:47 -04:00
|
|
|
before do
|
|
|
|
SiteSetting.min_trust_to_create_tag = 4
|
|
|
|
SiteSetting.min_trust_level_to_tag_topics = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it "only uses existing tags" do
|
|
|
|
Fabricate(:tag, name: 'totally')
|
|
|
|
expect {
|
2019-05-06 04:49:11 -04:00
|
|
|
@result = subject.revise!(user, raw: "lets totally update the body", tags: ['totally', 'update'])
|
2016-05-04 14:02:47 -04:00
|
|
|
}.to_not change { Tag.count }
|
|
|
|
expect(@result).to eq(true)
|
|
|
|
post.reload
|
|
|
|
expect(post.topic.tags.map(&:name)).to eq(['totally'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-09-11 05:48:25 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with uploads" do
|
2020-09-11 05:48:25 -04:00
|
|
|
let(:image1) { Fabricate(:upload) }
|
|
|
|
let(:image2) { Fabricate(:upload) }
|
|
|
|
let(:image3) { Fabricate(:upload) }
|
|
|
|
let(:image4) { Fabricate(:upload) }
|
|
|
|
let(:post_args) do
|
|
|
|
{
|
|
|
|
user: user,
|
|
|
|
topic: topic,
|
|
|
|
raw: <<~RAW
|
|
|
|
This is a post with multiple uploads
|
|
|
|
![image1](#{image1.short_url})
|
|
|
|
![image2](#{image2.short_url})
|
|
|
|
RAW
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it "updates linked post uploads" do
|
|
|
|
post.link_post_uploads
|
2022-06-08 19:24:30 -04:00
|
|
|
expect(post.upload_references.pluck(:upload_id)).to contain_exactly(image1.id, image2.id)
|
2020-09-11 05:48:25 -04:00
|
|
|
|
|
|
|
subject.revise!(user, raw: <<~RAW)
|
|
|
|
This is a post with multiple uploads
|
|
|
|
![image2](#{image2.short_url})
|
|
|
|
![image3](#{image3.short_url})
|
|
|
|
![image4](#{image4.short_url})
|
|
|
|
RAW
|
|
|
|
|
2022-06-08 19:24:30 -04:00
|
|
|
expect(post.reload.upload_references.pluck(:upload_id)).to contain_exactly(image2.id, image3.id, image4.id)
|
2020-09-11 05:48:25 -04:00
|
|
|
end
|
2021-05-20 23:32:32 -04:00
|
|
|
|
2022-09-28 19:24:33 -04:00
|
|
|
context "with secure uploads uploads" do
|
2021-05-20 23:32:32 -04:00
|
|
|
let!(:image5) { Fabricate(:secure_upload) }
|
|
|
|
before do
|
2021-06-21 12:15:24 -04:00
|
|
|
Jobs.run_immediately!
|
2021-05-20 23:32:32 -04:00
|
|
|
setup_s3
|
|
|
|
SiteSetting.authorized_extensions = "png|jpg|gif|mp4"
|
2022-09-28 19:24:33 -04:00
|
|
|
SiteSetting.secure_uploads = true
|
2021-05-20 23:32:32 -04:00
|
|
|
stub_upload(image5)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "updates the upload secure status, which is secure by default from the composer. set to false for a public topic" do
|
2021-10-19 07:42:29 -04:00
|
|
|
stub_image_size
|
2021-05-20 23:32:32 -04:00
|
|
|
subject.revise!(user, raw: <<~RAW)
|
|
|
|
This is a post with a secure upload
|
|
|
|
![image5](#{image5.short_url})
|
|
|
|
RAW
|
|
|
|
|
|
|
|
expect(image5.reload.secure).to eq(false)
|
|
|
|
expect(image5.security_last_changed_reason).to eq("access control post dictates security | source: post revisor")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not update the upload secure status, which is secure by default from the composer for a private" do
|
|
|
|
post.topic.update(category: Fabricate(:private_category, group: Fabricate(:group)))
|
2021-10-19 07:42:29 -04:00
|
|
|
stub_image_size
|
2021-05-20 23:32:32 -04:00
|
|
|
subject.revise!(user, raw: <<~RAW)
|
|
|
|
This is a post with a secure upload
|
|
|
|
![image5](#{image5.short_url})
|
|
|
|
RAW
|
|
|
|
|
|
|
|
expect(image5.reload.secure).to eq(true)
|
|
|
|
expect(image5.security_last_changed_reason).to eq("access control post dictates security | source: post revisor")
|
|
|
|
end
|
|
|
|
end
|
2020-09-11 05:48:25 -04:00
|
|
|
end
|
2022-02-23 02:39:54 -05:00
|
|
|
|
|
|
|
context 'with drafts' do
|
|
|
|
it "does not advance draft sequence if keep_existing_draft option is true" do
|
|
|
|
post = Fabricate(:post, user: user)
|
|
|
|
topic = post.topic
|
|
|
|
draft_key = "topic_#{topic.id}"
|
|
|
|
data = { reply: "test 12222" }.to_json
|
|
|
|
Draft.set(user, draft_key, 0, data)
|
|
|
|
Draft.set(user, draft_key, 0, data)
|
|
|
|
expect {
|
|
|
|
PostRevisor.new(post).revise!(
|
|
|
|
post.user,
|
|
|
|
{ title: "updated title for my topic" },
|
|
|
|
keep_existing_draft: true
|
|
|
|
)
|
2022-07-19 10:03:03 -04:00
|
|
|
}.to not_change { Draft.where(user: user, draft_key: draft_key).first.sequence }
|
|
|
|
.and not_change { DraftSequence.where(user_id: user.id, draft_key: draft_key).first.sequence }
|
2022-02-23 02:39:54 -05:00
|
|
|
|
|
|
|
expect {
|
|
|
|
PostRevisor.new(post).revise!(
|
|
|
|
post.user,
|
|
|
|
{ title: "updated title for my topic" },
|
|
|
|
)
|
|
|
|
}.to change { Draft.where(user: user, draft_key: draft_key).count }.from(1).to(0)
|
|
|
|
.and change { DraftSequence.where(user_id: user.id, draft_key: draft_key).first.sequence }.by(1)
|
|
|
|
end
|
|
|
|
end
|
2022-10-19 11:54:32 -04:00
|
|
|
|
|
|
|
context 'when skipping validations' do
|
|
|
|
fab!(:post) { Fabricate(:post, raw: 'aaa', skip_validation: true) }
|
|
|
|
|
|
|
|
it 'can revise multiple times and remove unnecessary revisions' do
|
|
|
|
subject.revise!(admin, { raw: 'bbb' }, skip_validations: true)
|
|
|
|
expect(post.errors).to be_empty
|
|
|
|
|
|
|
|
# Revert to old version which was invalid to destroy previously created
|
|
|
|
# post revision and trigger another post save.
|
|
|
|
subject.revise!(admin, { raw: 'aaa' }, skip_validations: true)
|
|
|
|
expect(post.errors).to be_empty
|
|
|
|
end
|
|
|
|
end
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|
2021-04-21 07:41:36 -04:00
|
|
|
|
|
|
|
context 'when the review_every_post setting is enabled' do
|
|
|
|
let(:post) { Fabricate(:post, post_args) }
|
|
|
|
let(:revisor) { PostRevisor.new(post) }
|
|
|
|
|
|
|
|
before { SiteSetting.review_every_post = true }
|
|
|
|
|
|
|
|
it 'queues the post when a regular user edits it' do
|
|
|
|
expect {
|
|
|
|
revisor.revise!(post.user, { raw: 'updated body' }, revised_at: post.updated_at + 10.minutes)
|
|
|
|
}.to change(ReviewablePost, :count).by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does nothing when a staff member edits a post' do
|
|
|
|
admin = Fabricate(:admin)
|
|
|
|
|
2022-07-19 10:03:03 -04:00
|
|
|
expect { revisor.revise!(admin, { raw: 'updated body' }) }.not_to change(ReviewablePost, :count)
|
2021-04-21 07:41:36 -04:00
|
|
|
end
|
|
|
|
|
2021-06-30 21:27:11 -04:00
|
|
|
it 'skips grace period edits' do
|
2021-04-21 07:41:36 -04:00
|
|
|
SiteSetting.editing_grace_period = 1.minute
|
|
|
|
|
|
|
|
expect {
|
|
|
|
revisor.revise!(post.user, { raw: 'updated body' }, revised_at: post.updated_at + 10.seconds)
|
2022-07-19 10:03:03 -04:00
|
|
|
}.not_to change(ReviewablePost, :count)
|
2021-04-21 07:41:36 -04:00
|
|
|
end
|
|
|
|
end
|
2013-02-09 10:33:07 -05:00
|
|
|
end
|