Skip to content

Commit 9487a81

Browse files
committed
Cooking some test
1 parent 60a6fbb commit 9487a81

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

plugin.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,11 @@ module ::DiscourseTranslator
4848
scope.can_translate?(object)
4949
end
5050

51-
add_to_class :post, :has_translation? do |language|
52-
translated = custom_fields[::DiscourseTranslator::TRANSLATED_CUSTOM_FIELD]
53-
!(language.blank? || translated.blank? || translated[language].blank?)
54-
end
55-
5651
add_to_serializer :post, :cooked, false do
5752
return super() if !SiteSetting.experimental_topic_translation
5853
return super() if scope.request.params["show_original"].present?
59-
return super() if !object.has_translation?(I18n.locale)
6054
translated = object.custom_fields[::DiscourseTranslator::TRANSLATED_CUSTOM_FIELD]
55+
return super() if (translated.blank? || translated[I18n.locale].blank?)
6156
translated[I18n.locale]
6257
end
6358
end

spec/serializers/post_serializer_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,49 @@
8080
end
8181
end
8282
end
83+
84+
describe "#cooked" do
85+
def serialize_post(user: nil, params: {}, cookies: {})
86+
env = {
87+
"action_dispatch.request.parameters" => params,
88+
"REQUEST_METHOD" => "GET",
89+
"rack.input" => {
90+
},
91+
"HTTP_COOKIE" => cookies.map { |k, v| "#{k}=#{v}" }.join(";"),
92+
}
93+
request = ActionDispatch::Request.new(env)
94+
guardian = Guardian.new(user, request)
95+
PostSerializer.new(post, scope: guardian)
96+
end
97+
98+
before { SiteSetting.experimental_topic_translation = true }
99+
100+
it "returns original cooked when experimental_topic_translation is disabled" do
101+
SiteSetting.experimental_topic_translation = false
102+
original_cooked = post.cooked
103+
expect(serialize_post(user: user).cooked).to eq(original_cooked)
104+
end
105+
106+
it "returns original cooked when show_original param is present" do
107+
original_cooked = post.cooked
108+
expect(serialize_post(user: user, params: { "show_original" => "true" }).cooked).to eq(
109+
original_cooked,
110+
)
111+
end
112+
113+
it "returns translated content based on locale" do
114+
I18n.locale = "ja"
115+
post.custom_fields[DiscourseTranslator::TRANSLATED_CUSTOM_FIELD] = {
116+
"ja" => "こんにちは",
117+
"es" => "Hola",
118+
}
119+
expect(serialize_post(user: user).cooked).to eq("こんにちは")
120+
end
121+
122+
it "returns original cooked when plugin is disabled" do
123+
SiteSetting.translator_enabled = false
124+
original_cooked = post.cooked
125+
expect(serialize_post(user: user).cooked).to eq(original_cooked)
126+
end
127+
end
83128
end

0 commit comments

Comments
 (0)