From 90d7dd1f056088bca19c4fafb8ec631e51814c13 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Wed, 9 Aug 2017 18:17:54 +0900 Subject: [PATCH] FIX: Ensure that post action moderation post uses the site's default locale. https://meta.discourse.org/t/a-post-in-looking-for-someone-to-customize-discourse-to-create-a-forum-site-requires-staff-attention/67468/5?u=tgxworld --- app/models/post_action.rb | 5 +- .../admin/flags_controller_spec.rb | 36 ------------ spec/integration/admin/flags_spec.rb | 55 +++++++++++++++++++ spec/models/post_action_spec.rb | 6 +- spec/support/integration_helpers.rb | 2 +- 5 files changed, 63 insertions(+), 41 deletions(-) delete mode 100644 spec/controllers/admin/flags_controller_spec.rb create mode 100644 spec/integration/admin/flags_spec.rb diff --git a/app/models/post_action.rb b/app/models/post_action.rb index 73a7e583ff8..00a196e37af 100644 --- a/app/models/post_action.rb +++ b/app/models/post_action.rb @@ -207,7 +207,10 @@ SQL return if staff_already_replied?(related_post.topic) message_key = "flags_dispositions.#{disposition}" message_key << "_and_deleted" if delete_post - related_post.topic.add_moderator_post(moderator, I18n.t(message_key)) + + I18n.with_locale(SiteSetting.default_locale) do + related_post.topic.add_moderator_post(moderator, I18n.t(message_key)) + end end def staff_already_replied?(topic) diff --git a/spec/controllers/admin/flags_controller_spec.rb b/spec/controllers/admin/flags_controller_spec.rb deleted file mode 100644 index fba4dfb61be..00000000000 --- a/spec/controllers/admin/flags_controller_spec.rb +++ /dev/null @@ -1,36 +0,0 @@ -require 'rails_helper' - -describe Admin::FlagsController do - - it "is a subclass of AdminController" do - expect(Admin::FlagsController < Admin::AdminController).to eq(true) - end - - context 'while logged in as an admin' do - before do - @user = log_in(:admin) - end - - context 'index' do - it 'returns empty json when nothing is flagged' do - xhr :get, :index - - data = ::JSON.parse(response.body) - expect(data["users"]).to eq([]) - expect(data["posts"]).to eq([]) - end - - it 'returns a valid json payload when some thing is flagged' do - p = Fabricate(:post) - u = Fabricate(:user) - - PostAction.act(u, p, PostActionType.types[:spam]) - xhr :get, :index - - data = ::JSON.parse(response.body) - data["users"].length == 2 - data["posts"].length == 1 - end - end - end -end diff --git a/spec/integration/admin/flags_spec.rb b/spec/integration/admin/flags_spec.rb new file mode 100644 index 00000000000..8c944276809 --- /dev/null +++ b/spec/integration/admin/flags_spec.rb @@ -0,0 +1,55 @@ +require 'rails_helper' + +RSpec.describe "Managing flags as an admin" do + let(:admin) { Fabricate(:admin) } + let(:post) { Fabricate(:post) } + let(:user) { Fabricate(:user) } + + before do + sign_in(admin) + end + + context 'viewing flags' do + it 'should return the right response when nothing is flagged' do + get '/admin/flags.json' + + expect(response).to be_success + + data = ::JSON.parse(response.body) + expect(data["users"]).to eq([]) + expect(data["posts"]).to eq([]) + end + + it 'should return the right response' do + PostAction.act(user, post, PostActionType.types[:spam]) + + get '/admin/flags.json' + + expect(response).to be_success + + data = ::JSON.parse(response.body) + data["users"].length == 2 + data["posts"].length == 1 + end + end + + context 'agreeing with a flag' do + it 'should work' do + SiteSetting.allow_user_locale = true + post_action = PostAction.act(user, post, PostActionType.types[:spam], message: 'bad') + admin.update!(locale: 'ja') + + xhr :post, "/admin/flags/agree/#{post.id}" + + expect(response).to be_success + + post_action.reload + + expect(post_action.agreed_by_id).to eq(admin.id) + + post = Post.offset(1).last + + expect(post.raw).to eq(I18n.with_locale(:en) { I18n.t('flags_dispositions.agreed') }) + end + end +end diff --git a/spec/models/post_action_spec.rb b/spec/models/post_action_spec.rb index ba66b98ee36..e4f74d5fad1 100644 --- a/spec/models/post_action_spec.rb +++ b/spec/models/post_action_spec.rb @@ -542,7 +542,7 @@ describe PostAction do end end - describe ".add_moderator_post_if_needed" do + describe "#add_moderator_post_if_needed" do it "should not add a moderator post when it's disabled" do post = create_post @@ -552,7 +552,7 @@ describe PostAction do topic = action.related_post.topic expect(topic.posts.count).to eq(1) - SiteSetting.expects(:auto_respond_to_flag_actions).returns(false) + SiteSetting.auto_respond_to_flag_actions = false PostAction.agree_flags!(post, admin) topic.reload @@ -566,7 +566,7 @@ describe PostAction do topic = action.reload.related_post.topic expect(user.notifications.count).to eq(0) - SiteSetting.expects(:auto_respond_to_flag_actions).returns(true) + SiteSetting.auto_respond_to_flag_actions = true PostAction.agree_flags!(post, admin) user_notifications = user.notifications diff --git a/spec/support/integration_helpers.rb b/spec/support/integration_helpers.rb index a2ae8f46d4e..fbd5a13c1a0 100644 --- a/spec/support/integration_helpers.rb +++ b/spec/support/integration_helpers.rb @@ -3,7 +3,7 @@ module IntegrationHelpers password = 'somecomplicatedpassword' user.update!(password: password) Fabricate(:email_token, confirmed: true, user: user) - post "/session.json", login: user.username, password: password + xhr :post, "/session.json", login: user.username, password: password expect(response).to be_success end end