2017-06-10 17:44:02 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
2024-09-04 15:12:25 +10:00
|
|
|
RSpec.describe Localized do
|
2023-07-27 22:27:21 +10:00
|
|
|
controller(ApplicationController) do
|
2017-06-10 17:44:02 +10:00
|
|
|
include Localized
|
|
|
|
|
|
|
|
def success
|
2019-07-22 02:08:02 +10:00
|
|
|
render plain: I18n.locale, status: 200
|
2017-06-10 17:44:02 +10:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
routes.draw { get 'success' => 'anonymous#success' }
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'default locale' do
|
2020-05-11 09:09:21 +10:00
|
|
|
it 'sets available and preferred language' do
|
|
|
|
request.headers['Accept-Language'] = 'sr-Latn'
|
|
|
|
get 'success'
|
|
|
|
expect(response.body).to eq 'sr-Latn'
|
|
|
|
end
|
|
|
|
|
2018-03-04 19:21:35 +11:00
|
|
|
it 'sets available and preferred language' do
|
|
|
|
request.headers['Accept-Language'] = 'ca-ES, fa'
|
|
|
|
get 'success'
|
2020-05-11 09:09:21 +10:00
|
|
|
expect(response.body).to eq 'ca'
|
2018-03-04 19:21:35 +11:00
|
|
|
end
|
2017-06-10 17:44:02 +10:00
|
|
|
|
2018-09-14 08:53:09 +10:00
|
|
|
it 'sets available and compatible language if none of available languages are preferred' do
|
2018-03-04 19:21:35 +11:00
|
|
|
request.headers['Accept-Language'] = 'fa-IR'
|
|
|
|
get 'success'
|
2019-07-22 02:08:02 +10:00
|
|
|
expect(response.body).to eq 'fa'
|
2017-06-10 17:44:02 +10:00
|
|
|
end
|
|
|
|
|
2018-03-04 19:21:35 +11:00
|
|
|
it 'sets default locale if none of available languages are compatible' do
|
|
|
|
request.headers['Accept-Language'] = ''
|
|
|
|
get 'success'
|
2019-07-22 02:08:02 +10:00
|
|
|
expect(response.body).to eq 'en'
|
2017-06-10 17:44:02 +10:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 13:49:08 +10:00
|
|
|
context 'with a user with valid locale has signed in' do
|
2017-06-10 17:44:02 +10:00
|
|
|
it "sets user's locale" do
|
|
|
|
user = Fabricate(:user, locale: :ca)
|
|
|
|
|
|
|
|
sign_in(user)
|
|
|
|
get 'success'
|
|
|
|
|
2019-07-22 02:08:02 +10:00
|
|
|
expect(response.body).to eq 'ca'
|
2017-06-10 17:44:02 +10:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 13:49:08 +10:00
|
|
|
context 'with a user with invalid locale has signed in' do
|
2017-06-10 17:44:02 +10:00
|
|
|
before do
|
|
|
|
user = Fabricate.build(:user, locale: :invalid)
|
|
|
|
user.save!(validate: false)
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'default locale'
|
|
|
|
end
|
|
|
|
|
2023-05-04 13:49:08 +10:00
|
|
|
context 'with a user who has not signed in' do
|
2017-06-10 17:44:02 +10:00
|
|
|
include_examples 'default locale'
|
|
|
|
end
|
|
|
|
end
|