# frozen_string_literal: true require "post_revisor" RSpec.describe PostRevisor do fab!(:topic) fab!(:newuser) { Fabricate(:newuser, last_seen_at: Date.today) } fab!(:user) { Fabricate(:user, refresh_auto_groups: true) } fab!(:coding_horror) fab!(:admin) { Fabricate(:admin, refresh_auto_groups: true) } fab!(:moderator) let(:post_args) { { user: newuser, topic: topic } } describe "TopicChanges" do let(:tc) do topic.reload PostRevisor::TopicChanges.new(topic, topic.user) end it "provides a guardian" do expect(tc.guardian).to be_an_instance_of Guardian end it "tracks changes properly" do expect(tc.diff).to eq({}) # it remembers changes we tell it to tc.record_change("height", "180cm", "170cm") expect(tc.diff["height"]).to eq(%w[180cm 170cm]) # it works with arrays of values tc.record_change("colors", nil, %w[red blue]) expect(tc.diff["colors"]).to eq([nil, %w[red blue]]) # it does not record changes to the same val tc.record_change("wat", "js", "js") expect(tc.diff["wat"]).to be_nil tc.record_change("tags", %w[a b], %w[a b]) expect(tc.diff["tags"]).to be_nil end end describe "editing category" do 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 { post.revise(post.user, category_id: category.id) } 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 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 it "does not revise category when the destination category requires topic approval" do new_category = Fabricate(:category) new_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.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) end it "does not revise category if incorrect amount of tags" do SiteSetting.create_tag_allowed_groups = Group::AUTO_GROUPS[:trust_level_0] SiteSetting.tag_topic_allowed_groups = Group::AUTO_GROUPS[:trust_level_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) end it "returns an error if the topic does not have minimum amount of tags that the new category requires" do SiteSetting.create_tag_allowed_groups = Group::AUTO_GROUPS[:trust_level_0] SiteSetting.tag_topic_allowed_groups = Group::AUTO_GROUPS[:trust_level_0] old_category = Fabricate(:category, minimum_required_tags: 0) new_category = Fabricate(:category, minimum_required_tags: 1) post = create_post(category: old_category) topic = post.topic post.revise(post.user, category_id: new_category.id) expect(topic.errors.full_messages).to eq([I18n.t("tags.minimum_required_tags", count: 1)]) end it "returns an error if the topic has tags not allowed in the new category" do SiteSetting.create_tag_allowed_groups = Group::AUTO_GROUPS[:trust_level_0] SiteSetting.tag_topic_allowed_groups = Group::AUTO_GROUPS[:trust_level_0] tag1 = Fabricate(:tag) tag2 = Fabricate(:tag) tag_group = Fabricate(:tag_group, tags: [tag1]) tag_group2 = Fabricate(:tag_group, tags: [tag2]) old_category = Fabricate(:category, tag_groups: [tag_group]) new_category = Fabricate(:category, tag_groups: [tag_group2]) post = create_post(category: old_category, tags: [tag1.name]) topic = post.topic post.revise(post.user, category_id: new_category.id) expect(topic.errors.full_messages).to eq( [ I18n.t( "tags.forbidden.restricted_tags_cannot_be_used_in_category", count: 1, tags: tag1.name, category: new_category.name, ), ], ) end it "returns an error if the topic is missing tags required from a tag group in the new category" do SiteSetting.create_tag_allowed_groups = Group::AUTO_GROUPS[:trust_level_0] SiteSetting.tag_topic_allowed_groups = Group::AUTO_GROUPS[:trust_level_0] tag1 = Fabricate(:tag) tag_group = Fabricate(:tag_group, tags: [tag1]) old_category = Fabricate(:category) new_category = Fabricate( :category, category_required_tag_groups: [ CategoryRequiredTagGroup.new(tag_group: tag_group, min_count: 1), ], ) post = create_post(category: old_category) topic = post.topic post.revise(post.user, category_id: new_category.id) expect(topic.errors.full_messages).to eq( [ I18n.t( "tags.required_tags_from_group", count: 1, tag_group_name: tag_group.name, tags: tag1.name, ), ], ) end end describe "editing tags" do subject(:post_revisor) { PostRevisor.new(post) } fab!(: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 { post_revisor.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 { post_revisor.revise!(admin, tags: ["new-tag"]) }.not_to change { Notification.count } end it "doesn't create a small_action post when create_post_for_category_and_tag_changes is false" do SiteSetting.create_post_for_category_and_tag_changes = false expect { post_revisor.revise!(admin, tags: ["new-tag"]) }.not_to change { Post.count } end describe "when `create_post_for_category_and_tag_changes` site setting is enabled" do fab!(:tag1) { Fabricate(:tag, name: "First tag") } fab!(:tag2) { Fabricate(:tag, name: "Second tag") } before do SiteSetting.create_post_for_category_and_tag_changes = true SiteSetting.whispers_allowed_groups = Group::AUTO_GROUPS[:staff] end it "Creates a small_action post with correct translation when both adding and removing tags" do post.topic.update!(tags: [tag1]) expect { post_revisor.revise!(admin, tags: [tag2.name]) }.to change { Post.where(topic_id: post.topic_id, action_code: "tags_changed").count }.by(1) expect(post.topic.ordered_posts.last.raw).to eq( I18n.t( "topic_tag_changed.added_and_removed", added: "##{tag2.name}", removed: "##{tag1.name}", ), ) end it "Creates a small_action post with correct translation when adding tags" do post.topic.update!(tags: []) expect { post_revisor.revise!(admin, tags: [tag1.name]) }.to change { Post.where(topic_id: post.topic_id, action_code: "tags_changed").count }.by(1) expect(post.topic.ordered_posts.last.raw).to eq( I18n.t("topic_tag_changed.added", added: "##{tag1.name}"), ) end it "Creates a small_action post with correct translation when removing tags" do post.topic.update!(tags: [tag1, tag2]) expect { post_revisor.revise!(admin, tags: []) }.to change { Post.where(topic_id: post.topic_id, action_code: "tags_changed").count }.by(1) expect(post.topic.ordered_posts.last.raw).to eq( I18n.t("topic_tag_changed.removed", removed: "##{tag1.name}, ##{tag2.name}"), ) end it "Creates a small_action post when category is changed" do current_category = post.topic.category category = Fabricate(:category) expect { post_revisor.revise!(admin, category_id: category.id) }.to change { Post.where(topic_id: post.topic_id, action_code: "category_changed").count }.by(1) expect(post.topic.ordered_posts.last.raw).to eq( I18n.t( "topic_category_changed", to: "##{category.slug}", from: "##{current_category.slug}", ), ) end it "Creates a small_action as a whisper when category is changed" do category = Fabricate(:category) expect { post_revisor.revise!(admin, category_id: category.id) }.to change { Post.where(topic_id: post.topic_id, action_code: "category_changed").count }.by(1) expect(post.topic.ordered_posts.last.post_type).to eq(Post.types[:whisper]) end describe "with PMs" do fab!(:pm) { Fabricate(:private_message_topic) } let(:first_post) { create_post(user: admin, topic: pm, allow_uncategorized_topics: false) } fab!(:category) { Fabricate(:category, topic_count: 1) } it "Does not create a category change small_action post when converting to a topic" do expect do TopicConverter.new(first_post.topic, admin).convert_to_public_topic(category.id) end.to change { category.reload.topic_count }.by(1) end end end end describe "revise wiki" do before { SiteSetting.unique_posts_mins = 10 } 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 expect(post.revise(post.user, wiki: true)).to be_truthy post.reload expect(post.wiki).to be_truthy end end describe "revise" do subject(:post_revisor) { PostRevisor.new(post) } let(:post) { Fabricate(:post, post_args) } let(:first_version_at) { post.last_version_at } it "destroys last revision if edit is undone" do old_raw = post.raw post_revisor.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) expect(post_revisor.raw_changed?).to eq(true) post_revisor.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) post_revisor.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 describe "with the same body" do it "doesn't change version" do expect { expect(post_revisor.revise!(post.user, raw: post.raw)).to eq(false) post.reload }.not_to change(post, :version) end end describe "with nil raw contents" do it "doesn't change version" do expect { expect(post_revisor.revise!(post.user, raw: nil)).to eq(false) post.reload }.not_to change(post, :version) end end describe "topic is in slow mode" do before { topic.update!(slow_mode_seconds: 1000) } it "regular edits are not allowed by default" do post_revisor.revise!( post.user, { raw: "updated body" }, revised_at: post.updated_at + 1000.minutes, ) post.reload expect(post.errors.present?).to eq(true) expect(post.errors.messages[:base].first).to be I18n.t("cannot_edit_on_slow_mode") end it "grace period editing is allowed" do SiteSetting.editing_grace_period = 1.minute post_revisor.revise!( post.user, { raw: "updated body" }, revised_at: post.updated_at + 10.seconds, ) post.reload expect(post.errors).to be_empty end it "regular edits are allowed if it was turned on in settings" do SiteSetting.slow_mode_prevents_editing = false post_revisor.revise!( post.user, { raw: "updated body" }, revised_at: post.updated_at + 10.minutes, ) post.reload expect(post.errors).to be_empty end it "staff is allowed to edit posts even if the topic is in slow mode" do admin = Fabricate(:admin) post_revisor.revise!( admin, { raw: "updated body" }, revised_at: post.updated_at + 10.minutes, ) post.reload expect(post.errors).to be_empty end end describe "grace period editing" do it "correctly applies edits" do SiteSetting.editing_grace_period = 1.minute post_revisor.revise!( post.user, { raw: "updated body" }, revised_at: post.updated_at + 10.seconds, ) post.reload 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_time(first_version_at) expect(post_revisor.category_changed).to be_blank end 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("
hello world
") 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) end it "creates a new version when the post is flagged" do SiteSetting.editing_grace_period = 1.minute post = Fabricate(:post, raw: "hello world") Fabricate(:flag_post_action, post: post, user: user) revisor = PostRevisor.new(post) revisor.revise!( post.user, { raw: "hello world, JK" }, revised_at: post.updated_at + 1.second, ) post.reload expect(post.version).to eq(2) expect(post.revisions.count).to eq(1) end it "doesn't create a new version" do SiteSetting.editing_grace_period = 1.minute SiteSetting.editing_grace_period_max_diff = 100 # making a revision post_revisor.revise!( post.user, { raw: "updated body" }, revised_at: post.updated_at + SiteSetting.editing_grace_period + 1.seconds, ) # "roll back" post_revisor.revise!( post.user, { raw: "Hello world" }, revised_at: post.updated_at + SiteSetting.editing_grace_period + 2.seconds, ) post.reload expect(post.version).to eq(1) expect(post.public_version).to eq(1) expect(post.revisions.size).to eq(0) end it "should bump the topic" do expect { post_revisor.revise!( post.user, { raw: "updated body" }, revised_at: post.updated_at + SiteSetting.editing_grace_period + 1.seconds, ) }.to change { post.topic.bumped_at } end it "should bump topic when no topic category" do topic_with_no_category = Fabricate(:topic, category_id: nil) post_from_topic_with_no_category = Fabricate(:post, topic: topic_with_no_category) expect { result = post_revisor.revise!( Fabricate(:admin), raw: post_from_topic_with_no_category.raw, tags: ["foo"], ) expect(result).to eq(true) }.to change { topic.reload.bumped_at } end 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 post_revisor.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 end 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 it "resets the edit_reason attribute in post model" do freeze_time SiteSetting.editing_grace_period = 5.seconds 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") revisor.revise!( post.user, { raw: "hello world4321" }, revised_at: post.updated_at + 7.seconds, ) post.reload expect(post.edit_reason).not_to be_present end 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 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: ["Hello world
", "hello world
"], raw: ["Hello world", "hello world"], ) end end describe "revision much later" do let!(:revised_at) { post.updated_at + 2.minutes } before do SiteSetting.editing_grace_period = 1.minute post_revisor.revise!(post.user, { raw: "updated body" }, revised_at: revised_at) post.reload end it "doesn't update a category" do expect(post_revisor.category_changed).to be_blank end it "updates the versions" do expect(post.version).to eq(2) expect(post.public_version).to eq(2) end it "creates a new revision" do expect(post.revisions.size).to eq(1) end it "updates the last_version_at" do expect(post.last_version_at.to_i).to eq(revised_at.to_i) end describe "new edit window" do before do post_revisor.revise!( post.user, { raw: "yet another updated body" }, revised_at: revised_at, ) post.reload end it "doesn't create a new version if you do another" do expect(post.version).to eq(2) expect(post.public_version).to eq(2) end it "doesn't change last_version_at" do expect(post.last_version_at.to_i).to eq(revised_at.to_i) end it "doesn't update a category" do expect(post_revisor.category_changed).to be_blank end context "after second window" do let!(:new_revised_at) { revised_at + 2.minutes } before do post_revisor.revise!( post.user, { raw: "yet another, another updated body" }, revised_at: new_revised_at, ) post.reload end it "does create a new version after the edit window" do expect(post.version).to eq(3) expect(post.public_version).to eq(3) end it "does create a new version after the edit window" do expect(post.last_version_at.to_i).to eq(new_revised_at.to_i) end end end end describe "category topic" do let!(:category) do category = Fabricate(:category) category.update_column(:topic_id, topic.id) category end let(:new_description) { "this is my new description." } it "should have no description by default" do expect(category.description).to be_blank end context "with one paragraph description" do before do post_revisor.revise!(post.user, raw: new_description) category.reload end it "returns the changed category info" do expect(post_revisor.category_changed).to eq(category) end it "updates the description of the category" do expect(category.description).to eq(new_description) end end context "with multiple paragraph description" do before do post_revisor.revise!(post.user, raw: "#{new_description}\n\nOther content goes here.") category.reload end it "returns the changed category info" do expect(post_revisor.category_changed).to eq(category) end it "updates the description of the category" do expect(category.description).to eq(new_description) end end context "with invalid description without paragraphs" do before do post_revisor.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 context "when updating back to the original paragraph" do before do category.update_column(:description, "this is my description") post_revisor.revise!(post.user, raw: Category.post_template) category.reload end it "puts the description back to nothing" do expect(category.description).to be_blank end it "returns the changed category info" do expect(post_revisor.category_changed).to eq(category) end end end describe "rate limiter" do fab!(:changed_by) { coding_horror } before do RateLimiter.enable SiteSetting.editing_grace_period = 0 end it "triggers a rate limiter" do EditRateLimiter.any_instance.expects(:performed!) post_revisor.revise!(changed_by, raw: "updated body") end it "raises error when a user gets rate limited" do SiteSetting.max_edits_per_day = 1 user = Fabricate(:user, trust_level: 1) post_revisor.revise!(user, raw: "body (edited)") expect do post_revisor.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 { post_revisor.revise!(user, raw: "body (edited)") }.to_not raise_error expect { post_revisor.revise!(user, raw: "body (edited twice)") }.to_not raise_error expect do post_revisor.revise!(user, raw: "body (edited three times) ") end.to raise_error( RateLimiter::LimitExceeded, ) user = Fabricate(:user, trust_level: 3) expect { post_revisor.revise!(user, raw: "body (edited)") }.to_not raise_error expect { post_revisor.revise!(user, raw: "body (edited twice)") }.to_not raise_error expect { post_revisor.revise!(user, raw: "body (edited three times)") }.to_not raise_error expect do post_revisor.revise!(user, raw: "body (edited four times) ") end.to raise_error( RateLimiter::LimitExceeded, ) user = Fabricate(:user, trust_level: 4) expect { post_revisor.revise!(user, raw: "body (edited)") }.to_not raise_error expect { post_revisor.revise!(user, raw: "body (edited twice)") }.to_not raise_error expect { post_revisor.revise!(user, raw: "body (edited three times)") }.to_not raise_error expect { post_revisor.revise!(user, raw: "body (edited four times)") }.to_not raise_error expect do post_revisor.revise!(user, raw: "body (edited five times) ") end.to raise_error( RateLimiter::LimitExceeded, ) end end describe "admin editing a new user's post" do fab!(:changed_by) { Fabricate(:admin) } before do SiteSetting.newuser_max_embedded_media = 0 url = "http://i.imgur.com/wfn7rgU.jpg" Oneboxer.stubs(:onebox).with(url, anything).returns("