Skip to content

Commit 01744ed

Browse files
authored
DEV: Simplify if-statements and test (#227)
This is a refactor to prepare for a fix, so that the fix PR is less messy. Related comment here: #215 (comment)
1 parent 65baebb commit 01744ed

File tree

2 files changed

+57
-76
lines changed

2 files changed

+57
-76
lines changed

lib/discourse_translator/guardian_extension.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@ def can_detect_language?(post)
2121

2222
def can_translate?(post)
2323
return false if !user_group_allow_translate?
24+
return false if post.locale_matches?(I18n.locale)
2425

2526
if SiteSetting.experimental_topic_translation
26-
return false if post.locale_matches?(I18n.locale)
27-
return false if post.translation_for(I18n.locale).present?
28-
true
27+
post.translation_for(I18n.locale).nil?
2928
else
30-
return false if post.locale_matches?(I18n.locale)
3129
poster_group_allow_translate?(post)
3230
end
3331
end

spec/lib/guardian_extension_spec.rb

Lines changed: 55 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,35 @@
128128
end
129129

130130
describe "when translator enabled" do
131-
before { SiteSetting.translator_enabled = true }
131+
before do
132+
SiteSetting.translator_enabled = true
133+
I18n.locale = :pt
134+
end
132135

133-
describe "when experimental_topic_translation enabled" do
134-
before { SiteSetting.experimental_topic_translation = true }
136+
describe "anon user" do
137+
before { SiteSetting.restrict_translation_by_group = "#{Group::AUTO_GROUPS[:everyone]}" }
135138

136-
describe "anon user" do
137-
before { SiteSetting.restrict_translation_by_group = "#{Group::AUTO_GROUPS[:everyone]}" }
139+
it "cannot translate" do
140+
SiteSetting.experimental_topic_translation = true
141+
expect(Guardian.new.can_translate?(post)).to eq(false)
138142

139-
it "cannot translate" do
140-
expect(Guardian.new.can_translate?(post)).to eq(false)
141-
end
143+
SiteSetting.experimental_topic_translation = false
144+
expect(Guardian.new.can_translate?(post)).to eq(false)
142145
end
146+
end
147+
148+
it "cannot translate when post detected locale matches i18n locale" do
149+
post.set_detected_locale("pt")
150+
151+
SiteSetting.experimental_topic_translation = true
152+
expect(guardian.can_translate?(post)).to eq(false)
153+
154+
SiteSetting.experimental_topic_translation = false
155+
expect(guardian.can_translate?(post)).to eq(false)
156+
end
157+
158+
describe "when experimental_topic_translation enabled" do
159+
before { SiteSetting.experimental_topic_translation = true }
143160

144161
describe "logged in user" do
145162
it "cannot translate when user is not in restrict_translation_by_group" do
@@ -151,33 +168,17 @@
151168
describe "user is in restrict_translation_by_group" do
152169
before { SiteSetting.restrict_translation_by_group = "#{group.id}" }
153170

154-
describe "locale is :pt" do
155-
before { I18n.stubs(:locale).returns(:pt) }
156-
157-
it "cannot translate when post detected locale matches i18n locale" do
158-
post.set_detected_locale("pt")
159-
160-
expect(guardian.can_translate?(post)).to eq(false)
161-
end
162-
163-
it "can translate when post's detected locale does not match i18n locale" do
164-
post.set_detected_locale("jp")
165-
166-
expect(guardian.can_translate?(post)).to eq(true)
167-
end
168-
169-
it "cannot translate when post has translation for user locale" do
170-
post.set_detected_locale("jp")
171-
post.set_translation("pt", "Olá, mundo!")
171+
it "cannot translate when post has translation for user locale" do
172+
post.set_detected_locale("jp")
173+
post.set_translation("pt", "Olá, mundo!")
172174

173-
expect(guardian.can_translate?(post)).to eq(false)
174-
end
175+
expect(guardian.can_translate?(post)).to eq(false)
176+
end
175177

176-
it "can translate when post does not have translation for user locale" do
177-
post.set_detected_locale("jp")
178+
it "can translate when post does not have translation for user locale" do
179+
post.set_detected_locale("jp")
178180

179-
expect(guardian.can_translate?(post)).to eq(true)
180-
end
181+
expect(guardian.can_translate?(post)).to eq(true)
181182
end
182183
end
183184
end
@@ -186,56 +187,38 @@
186187
describe "when experimental topic translation disabled" do
187188
before { SiteSetting.experimental_topic_translation = false }
188189

189-
describe "anon user" do
190-
before { SiteSetting.restrict_translation_by_group = "#{Group::AUTO_GROUPS[:everyone]}" }
190+
it "cannot translate when user is not in restrict_translation_by_group" do
191+
SiteSetting.restrict_translation_by_group = "#{group.id + 1}"
191192

192-
it "cannot translate" do
193-
expect(Guardian.new.can_translate?(post)).to eq(false)
194-
end
193+
expect(guardian.can_translate?(post)).to eq(false)
195194
end
196195

197-
describe "logged in user" do
198-
it "cannot translate when user is not in restrict_translation_by_group" do
199-
SiteSetting.restrict_translation_by_group = "#{group.id + 1}"
200-
201-
expect(guardian.can_translate?(post)).to eq(false)
202-
end
203-
204-
describe "user is in restrict_translation_by_group" do
205-
before { SiteSetting.restrict_translation_by_group = "#{group.id}" }
206-
207-
describe "locale is :pt" do
208-
before { I18n.stubs(:locale).returns(:pt) }
209-
210-
it "cannot translate when post detected locale matches i18n locale" do
211-
post.set_detected_locale("pt")
212-
213-
expect(guardian.can_translate?(post)).to eq(false)
214-
end
196+
describe "user is in restrict_translation_by_group" do
197+
before { SiteSetting.restrict_translation_by_group = "#{group.id}" }
215198

216-
it "can translate when post's detected locale does not match i18n locale" do
217-
post.set_detected_locale("jp")
199+
it "can translate when post's detected locale does not match i18n locale, regardless of translation presence" do
200+
post.set_detected_locale("jp")
201+
expect(guardian.can_translate?(post)).to eq(true)
218202

219-
expect(guardian.can_translate?(post)).to eq(true)
220-
end
203+
post.set_translation("pt", "Olá, mundo!")
204+
expect(guardian.can_translate?(post)).to eq(true)
205+
end
221206

222-
it "cannot translate if poster is not in restrict_translation_by_poster_group" do
223-
SiteSetting.restrict_translation_by_poster_group = "#{Group::AUTO_GROUPS[:staff]}"
207+
it "cannot translate if poster is not in restrict_translation_by_poster_group" do
208+
SiteSetting.restrict_translation_by_poster_group = "#{Group::AUTO_GROUPS[:staff]}"
224209

225-
expect(guardian.can_translate?(post)).to eq(false)
226-
end
210+
expect(guardian.can_translate?(post)).to eq(false)
211+
end
227212

228-
it "can translate if poster is in restrict_translation_by_poster_group" do
229-
poster = post.user
230-
poster_group = Fabricate(:group, users: [poster])
213+
it "can translate if poster is in restrict_translation_by_poster_group" do
214+
poster = post.user
215+
poster_group = Fabricate(:group, users: [poster])
231216

232-
SiteSetting.restrict_translation_by_poster_group = "#{poster_group.id}"
233-
expect(guardian.can_translate?(post)).to eq(true)
217+
SiteSetting.restrict_translation_by_poster_group = "#{poster_group.id}"
218+
expect(guardian.can_translate?(post)).to eq(true)
234219

235-
SiteSetting.restrict_translation_by_poster_group = ""
236-
expect(guardian.can_translate?(post)).to eq(true)
237-
end
238-
end
220+
SiteSetting.restrict_translation_by_poster_group = ""
221+
expect(guardian.can_translate?(post)).to eq(true)
239222
end
240223
end
241224
end

0 commit comments

Comments
 (0)