From c67c2dc63875e54f75c6ebee74c0263e42e196f8 Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Mon, 14 May 2018 14:47:47 +0200 Subject: [PATCH] FIX: Username update should ignore revisions without raw --- app/jobs/regular/update_username.rb | 8 +++++--- spec/services/username_changer_spec.rb | 22 ++++++++++++---------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/app/jobs/regular/update_username.rb b/app/jobs/regular/update_username.rb index f6138c83ba1..a467e89ece8 100644 --- a/app/jobs/regular/update_username.rb +++ b/app/jobs/regular/update_username.rb @@ -31,9 +31,11 @@ module Jobs def update_revisions PostRevision.where(post_conditions("post_revisions.post_id"), post_condition_args).find_each do |revision| - revision.modifications["raw"].map! { |raw| update_raw(raw) } - revision.modifications["cooked"].map! { |cooked| update_cooked(cooked) } - revision.save! + if revision.modifications.key?("raw") || revision.modifications.key?("cooked") + revision.modifications["raw"]&.map! { |raw| update_raw(raw) } + revision.modifications["cooked"]&.map! { |cooked| update_cooked(cooked) } + revision.save! + end end end diff --git a/spec/services/username_changer_spec.rb b/spec/services/username_changer_spec.rb index ad0fac18313..c02ea460aff 100644 --- a/spec/services/username_changer_spec.rb +++ b/spec/services/username_changer_spec.rb @@ -218,28 +218,30 @@ describe UsernameChanger do end it 'replaces mentions within revisions' do - revisions = [{ raw: "Hello Foo" }, { raw: "Hello @foo!" }, { raw: "Hello @foo!!" }] + revisions = [{ raw: "Hello Foo" }, { title: "new topic title" }, { raw: "Hello @foo!" }, { raw: "Hello @foo!!" }] post = create_post_and_change_username(raw: "Hello @foo", revisions: revisions) expect(post.raw).to eq("Hello @bar!!") expect(post.cooked).to eq(%Q(

Hello @bar!!

)) - expect(post.revisions.count).to eq(3) + expect(post.revisions.count).to eq(4) expect(post.revisions[0].modifications["raw"][0]).to eq("Hello @bar") expect(post.revisions[0].modifications["raw"][1]).to eq("Hello Foo") expect(post.revisions[0].modifications["cooked"][0]).to eq(%Q(

Hello @bar

)) expect(post.revisions[0].modifications["cooked"][1]).to eq(%Q(

Hello Foo

)) - expect(post.revisions[1].modifications["raw"][0]).to eq("Hello Foo") - expect(post.revisions[1].modifications["raw"][1]).to eq("Hello @bar!") - expect(post.revisions[1].modifications["cooked"][0]).to eq(%Q(

Hello Foo

)) - expect(post.revisions[1].modifications["cooked"][1]).to eq(%Q(

Hello @bar!

)) + expect(post.revisions[1].modifications).to include("title") - expect(post.revisions[2].modifications["raw"][0]).to eq("Hello @bar!") - expect(post.revisions[2].modifications["raw"][1]).to eq("Hello @bar!!") - expect(post.revisions[2].modifications["cooked"][0]).to eq(%Q(

Hello @bar!

)) - expect(post.revisions[2].modifications["cooked"][1]).to eq(%Q(

Hello @bar!!

)) + expect(post.revisions[2].modifications["raw"][0]).to eq("Hello Foo") + expect(post.revisions[2].modifications["raw"][1]).to eq("Hello @bar!") + expect(post.revisions[2].modifications["cooked"][0]).to eq(%Q(

Hello Foo

)) + expect(post.revisions[2].modifications["cooked"][1]).to eq(%Q(

Hello @bar!

)) + + expect(post.revisions[3].modifications["raw"][0]).to eq("Hello @bar!") + expect(post.revisions[3].modifications["raw"][1]).to eq("Hello @bar!!") + expect(post.revisions[3].modifications["cooked"][0]).to eq(%Q(

Hello @bar!

)) + expect(post.revisions[3].modifications["cooked"][1]).to eq(%Q(

Hello @bar!!

)) end it 'replaces mentions in posts marked for deletion' do