forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locale_selection_spec.rb
188 lines (156 loc) · 9.08 KB
/
locale_selection_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe LocaleSelection do
class TestClassForMixins
extend LocaleSelection
end
def ls
TestClassForMixins
end
context 'accept-language' do
it "should ignore malformed accept-language headers" do
expect(ls.infer_browser_locale("en not valid", ['en'])).to be_nil
end
it "should match valid locale ranges" do
expect(ls.infer_browser_locale("en", ['en'])).to eql('en')
end
it "should not match invalid locale ranges" do
expect(ls.infer_browser_locale("it", ['en'])).to be_nil
end
it "should do case-insensitive matching" do
expect(ls.infer_browser_locale("en-us", ['en-US'])).to eql('en-US')
end
# see rfc2616 ... en means any en(-.*)? is acceptable
it "should do range prefix-matching" do
expect(ls.infer_browser_locale("en", ['en-US'])).to eql('en-US')
end
# while tag prefix-matching might be desirable (sometimes), it should not
# be done automatically on the server-side (though the user-agent can do
# it). from the rfc:
# [U]sers might [incorrectly] assume that on selecting "en-gb", they
# will be served any kind of English document if British English is not
# available. A user agent might suggest in such a case to add "en" to
# get the best matching behavior.
it "should not do tag prefix-matching" do
expect(ls.infer_browser_locale("en-US", ['en'])).to be_nil
end
it "should assign quality values based on the best match" do
expect(ls.infer_browser_locale("en-US, es;q=0.9, en;q=0.8", ['en-US', 'es'])).to eql('en-US')
# no tag prefix-matching
expect(ls.infer_browser_locale("en-US, es;q=0.9, en;q=0.8", ['en', 'es'])).to eql('es')
# order doesn't matter
expect(ls.infer_browser_locale("es;q=0.9, en", ['en', 'es'])).to eql('en')
# although the en range matches the en-US tag, the en-US range is
# a better (read: longer) match. so the es tag ends up with a higher
# quality value than en-US tag
expect(ls.infer_browser_locale("en, es;q=0.9, en-US;q=0.8", ['en-US', 'es'])).to eql('es')
end
it "should understand wildcards" do
expect(ls.infer_browser_locale("*, pt;q=0.8", ['ru', 'pt'])).to eql('ru')
expect(ls.infer_browser_locale("*, pt;q=0.8, ru;q=0.7", ['ru', 'pt'])).to eql('pt')
# the pt range is explicitly rejected, so we don't get a locale
expect(ls.infer_browser_locale("pt-BR, *;q=0.9, pt;q=0", ['pt'])).to be_nil
# no pt variants supported, so we get the first alternative
expect(ls.infer_browser_locale("pt-BR, pt;q=0.9, *;q=0.8", ['es', 'fr'])).to eql('es')
# equal matches sort by position before alphabetical
expect(ls.infer_browser_locale("en, *", ['ar', 'en'])).to eql('en')
end
end
context "locale matching" do
before do
I18n.config.stubs(:available_locales).returns([:en, :it, :es, :fr, :de, :pt, :zh])
I18n.config.clear_available_locales_set
@root_account = Account.create
@account = Account.create(:parent_account => @root_account)
user
course
@course.account = @account
@course.save
end
it "should use the default locale if there is no other context" do
expect(ls.infer_locale).to eql('en')
expect(ls.infer_locale(:root_account => @root_account)).to eql('en')
expect(ls.infer_locale(:root_account => @root_account, :user => @user)).to eql('en')
expect(ls.infer_locale(:root_account => @root_account, :user => @user, :context => @course)).to eql('en')
end
it "should infer the locale from the accept_language" do
expect(ls.infer_locale(:accept_language => "it", :root_account => @root_account)).to eql('it')
expect(@user.browser_locale).to be_nil
expect(ls.infer_locale(:accept_language => "it", :root_account => @root_account, :user => @user)).to eql('it')
expect(@user.browser_locale).to eql('it')
expect(ls.infer_locale(:accept_language => "it", :root_account => @root_account, :user => @user, :context => @account)).to eql('it')
expect(ls.infer_locale(:accept_language => "it", :root_account => @root_account, :user => @user, :context => @course)).to eql('it')
end
it "should infer the locale from the root account" do
@root_account.update_attribute(:default_locale, 'es')
expect(ls.infer_locale(:accept_language => "it", :root_account => @root_account, :user => @user)).to eql('es')
expect(ls.infer_locale(:accept_language => "it", :root_account => @root_account, :user => @user, :context => @account)).to eql('es')
expect(ls.infer_locale(:accept_language => "it", :root_account => @root_account, :user => @user, :context => @course)).to eql('es')
end
it "should infer the locale from the account" do
@root_account.update_attribute(:default_locale, 'es')
@account.update_attribute(:default_locale, 'fr')
expect(ls.infer_locale(:accept_language => "it", :root_account => @root_account, :user => @user)).to eql('es')
expect(ls.infer_locale(:accept_language => "it", :root_account => @root_account, :user => @user, :context => @account)).to eql('fr')
expect(ls.infer_locale(:accept_language => "it", :root_account => @root_account, :user => @user, :context => @course)).to eql('fr')
end
it "should infer the locale from the user" do
@root_account.update_attribute(:default_locale, 'es')
@account.update_attribute(:default_locale, 'fr')
@user.update_attribute(:locale, 'de')
expect(ls.infer_locale(:accept_language => "it", :root_account => @root_account, :user => @user)).to eql('de')
expect(ls.infer_locale(:accept_language => "it", :root_account => @root_account, :user => @user, :context => @account)).to eql('de')
expect(ls.infer_locale(:accept_language => "it", :root_account => @root_account, :user => @user, :context => @course)).to eql('de')
end
it "should ignore bogus locales" do
@root_account.update_attribute(:default_locale, 'es')
@account.update_attribute(:default_locale, 'fr')
@user.stubs(:locale).returns('bogus')
expect(ls.infer_locale(:accept_language => "it", :root_account => @root_account, :user => @user)).to eql('es')
expect(ls.infer_locale(:accept_language => "it", :root_account => @root_account, :user => @user, :context => @account)).to eql('fr')
expect(ls.infer_locale(:accept_language => "it", :root_account => @root_account, :user => @user, :context => @course)).to eql('fr')
end
it "should infer the locale from the course" do
@root_account.update_attribute(:default_locale, 'es')
@account.update_attribute(:default_locale, 'fr')
@user.update_attribute(:locale, 'de')
@course.update_attribute(:locale, 'pt')
expect(ls.infer_locale(:accept_language => "it", :root_account => @root_account, :user => @user)).to eql('de')
expect(ls.infer_locale(:accept_language => "it", :root_account => @root_account, :user => @user, :context => @account)).to eql('de')
expect(ls.infer_locale(:accept_language => "it", :root_account => @root_account, :user => @user, :context => @course)).to eql('pt')
end
it "should infer the locale of a group from the group's context" do
@course.update_attribute(:locale, 'es')
course_gc = @course.group_categories.create!(:name => "Discussion Groups")
course_gr = Group.create!(:name => "Group 1", :group_category => course_gc, :context => @course)
@account.update_attribute(:default_locale, 'fr')
account_gc = @account.group_categories.create!(:name => "Other Groups")
account_gr = Group.create!(:name => "Group 1", :group_category => account_gc, :context => @account)
expect(ls.infer_locale(:accept_language => "it", :root_account => @root_account, :user => @user, :context => account_gr)).to eql('fr')
expect(ls.infer_locale(:accept_language => "it", :root_account => @root_account, :user => @user, :context => course_gr)).to eql('es')
end
it "should infer the locale from the session" do
@root_account.update_attribute(:default_locale, 'es')
@account.update_attribute(:default_locale, 'fr')
@user.update_attribute(:locale, 'de')
expect(ls.infer_locale(:accept_language => "it", :root_account => @root_account, :context => @account, :session_locale => 'zh')).to eql('zh')
expect(ls.infer_locale(:accept_language => "it", :root_account => @root_account, :context => @account, :user => @user, :session_locale => 'zh')).to eql('de')
end
end
end