display membership emails in the language of the recipient

pull/10063/head
Christophe Bliard 3 years ago
parent 30efbe426d
commit 8adcb742ba
No known key found for this signature in database
GPG Key ID: 2BC07603210C3FA4
  1. 2
      app/mailers/member_mailer.rb
  2. 33
      spec/mailers/member_mailer_spec.rb

@ -78,8 +78,8 @@ class MemberMailer < ApplicationMailer
end end
def send_mail(current_user, member, subject, message) def send_mail(current_user, member, subject, message)
in_member_locale(member) do
User.execute_as(current_user) do User.execute_as(current_user) do
in_member_locale(member) do
message_id member, current_user message_id member, current_user
@roles = member.roles @roles = member.roles

@ -101,7 +101,12 @@ describe MemberMailer, type: :mailer do
shared_examples_for 'has the expected body' do shared_examples_for 'has the expected body' do
let(:body) { subject.body.parts.detect { |part| part['Content-Type'].value == 'text/html' }.body.to_s } let(:body) { subject.body.parts.detect { |part| part['Content-Type'].value == 'text/html' }.body.to_s }
let(:i18n_params) do
{
project: project ? link_to_project(project, only_path: false) : nil,
user: link_to_user(current_user, only_path: false)
}.compact
end
it 'highlights the roles received' do it 'highlights the roles received' do
expected = <<~MSG expected = <<~MSG
@ -116,17 +121,24 @@ describe MemberMailer, type: :mailer do
.at_path('body/table/tr/td/ul') .at_path('body/table/tr/td/ul')
end end
context 'when current user and principal have different locales' do
let(:principal) { FactoryBot.build_stubbed(:user, language: 'fr') }
let(:current_user) { FactoryBot.build_stubbed(:user, language: 'de') }
it 'is in the locale of the recipient' do
OpenProject::LocaleHelper.with_locale_for(principal) do
i18n_params
end
expect(body).to include(I18n.t(:"#{expected_header}.without_message", locale: :fr, **i18n_params))
end
end
context 'with a custom message' do context 'with a custom message' do
let(:message) { "Some **styled** message" } let(:message) { "Some **styled** message" }
it 'has the expected header' do it 'has the expected header' do
params = {
project: project ? link_to_project(project, only_path: false) : nil,
user: link_to_user(current_user, only_path: false)
}.compact
expect(body) expect(body)
.to include(I18n.t(:"#{expected_header}.with_message", **params)) .to include(I18n.t(:"#{expected_header}.with_message", **i18n_params))
end end
it 'includes the custom message' do it 'includes the custom message' do
@ -138,13 +150,8 @@ describe MemberMailer, type: :mailer do
context 'without a custom message' do context 'without a custom message' do
it 'has the expected header' do it 'has the expected header' do
params = {
project: project ? link_to_project(project, only_path: false) : nil,
user: link_to_user(current_user, only_path: false)
}.compact
expect(body) expect(body)
.to include(I18n.t(:"#{expected_header}.without_message", **params)) .to include(I18n.t(:"#{expected_header}.without_message", **i18n_params))
end end
end end
end end

Loading…
Cancel
Save