Skip to content

Commit a76454c

Browse files
committed
[WHIT-2724] Add unit tests for translation block content consistency
Adds the unit tests to cover the acceptance criteria in the linked ticket and attempts to get the tests passing. Also includes a couple more scenarios to cover differences in lead images set between translations and the primary edition record
1 parent 9c20bbc commit a76454c

4 files changed

Lines changed: 145 additions & 5 deletions

File tree

app/models/concerns/edition/translatable.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ class Trait < Edition::Traits::Trait
55
def process_associations_before_save(edition)
66
@edition.translations.each do |translation|
77
I18n.with_locale(translation.locale) do
8-
edition.title = @edition.title
9-
edition.summary = @edition.summary
10-
edition.body = @edition.body
11-
edition.block_content = @edition.block_content
8+
edition.title = translation.title
9+
edition.summary = translation.summary
10+
edition.body = translation.body
11+
edition.write_attribute(:block_content, translation.block_content)
1212
end
1313
end
1414
end

app/models/standard_edition.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ def body
4141
block_content["body"]
4242
end
4343

44+
def body=(_)
45+
nil
46+
end
47+
4448
def can_set_previously_published?
4549
type_instance.settings["backdating_enabled"]
4650
end

app/models/standard_edition/block_content.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def attributes_class_for(schema)
9999
delegate :[], to: :attributes
100100

101101
def to_h
102-
attributes
102+
attributes.compact
103103
end
104104
end
105105
attributes_class.set_temporary_name("Block content attributes")
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
require "test_helper"
2+
3+
class StandardEditionTranslationBlockContentConsistencyTest < ActiveSupport::TestCase
4+
def english_body_content = "English body content"
5+
def translated_body_content(locale) = "Body content for #{locale}"
6+
7+
setup do
8+
ConfigurableDocumentType.setup_test_types(
9+
build_configurable_document_type(
10+
"test", {
11+
"schema" => {
12+
"properties" => {
13+
"body" => { "type" => "string", "format" => "govspeak", "title" => "Body" },
14+
"image" => { "type" => "integer", "format" => "lead_image_select", "title" => "Lead image" },
15+
},
16+
},
17+
"settings" => { "translations_enabled" => true },
18+
}
19+
),
20+
)
21+
end
22+
23+
test "creating a new draft from a published StandardEdition with translations should not duplicate old body field data" do
24+
translated_edition = create_standard_edition(translate_for: %w[cy])
25+
26+
new_draft = translated_edition.create_draft(create(:writer))
27+
28+
english_translation = new_draft.translation_for(:en)
29+
welsh_translation = new_draft.translation_for(:cy)
30+
31+
assert_nil english_translation[:body],
32+
"English translation body column should be nil, but got: #{english_translation[:body]}"
33+
assert_nil welsh_translation[:body],
34+
"Welsh translation body column should be nil, but got: #{welsh_translation[:body]}"
35+
36+
assert_equal english_body_content, english_translation.block_content["body"],
37+
"English body should be in block_content.body"
38+
assert_equal translated_body_content("cy"), welsh_translation.block_content["body"],
39+
"Welsh body should be in block_content.body"
40+
end
41+
42+
test "editions without images should not have image key in block_content" do
43+
translated_edition = create_standard_edition(translate_for: %w[cy])
44+
45+
assert_not translated_edition.translation_for(:en).block_content.key?("image"),
46+
"English translation should not have image key"
47+
assert_not translated_edition.translation_for(:cy).block_content.key?("image"),
48+
"Welsh translation should not have image key"
49+
50+
new_draft = translated_edition.create_draft(create(:writer))
51+
52+
assert_not new_draft.translation_for(:en).block_content.key?("image"),
53+
"Draft English translation should not have image key"
54+
assert_not new_draft.translation_for(:cy).block_content.key?("image"),
55+
"Draft Welsh translation should not have image key"
56+
end
57+
58+
test "primary translation with image should not clone image to non-primary translations" do
59+
standard_edition = create_standard_edition(with_image: true)
60+
create_translated_edition(standard_edition, locale: "cy", with_image: false)
61+
62+
english_translation = standard_edition.translation_for(:en)
63+
welsh_translation = standard_edition.translation_for(:cy)
64+
image_id = english_translation.block_content["image"]
65+
66+
assert_not welsh_translation.block_content.key?("image"),
67+
"Welsh translation should not have image key"
68+
69+
new_draft = standard_edition.create_draft(create(:writer))
70+
71+
assert_equal image_id, new_draft.translation_for(:en).block_content["image"],
72+
"Draft English translation should have image value"
73+
74+
assert new_draft.translation_for(:cy).block_content["image"].blank?,
75+
"Draft Welsh translation should not have a value but found Image ID: #{new_draft.translation_for(:cy).block_content['image']}"
76+
end
77+
78+
test "translated edition with image should not clone to primary translation" do
79+
standard_edition = create_standard_edition(with_image: false)
80+
create_translated_edition(standard_edition, locale: "cy", with_image: true)
81+
82+
english_translation = standard_edition.translation_for(:en)
83+
welsh_translation = standard_edition.translation_for(:cy)
84+
image_id = welsh_translation.block_content["image"]
85+
86+
new_draft = standard_edition.create_draft(create(:writer))
87+
88+
assert english_translation.block_content["image"].blank?,
89+
"Draft English translation should not have image value"
90+
91+
assert_equal image_id, new_draft.translation_for(:cy).block_content["image"],
92+
"Draft Welsh translation should have image value"
93+
end
94+
95+
test "new draft editions with images get the translated image IDs from the parent" do
96+
standard_edition = create_standard_edition(with_image: true)
97+
create_translated_edition(standard_edition, locale: "cy", with_image: true)
98+
99+
english_image_id = standard_edition.translation_for(:en).block_content["image"]
100+
welsh_image_id = standard_edition.translation_for(:cy).block_content["image"]
101+
102+
new_draft = standard_edition.create_draft(create(:writer))
103+
104+
assert_equal english_image_id, new_draft.translation_for(:en).block_content["image"],
105+
"Draft English translation should not have image value"
106+
107+
assert_equal welsh_image_id, new_draft.translation_for(:cy).block_content["image"],
108+
"Draft Welsh translation should have image value"
109+
end
110+
111+
private
112+
113+
def create_standard_edition(with_image: false, translate_for: [])
114+
block_content = { "body" => english_body_content }
115+
block_content["image"] = create(:image).image_data.id if with_image
116+
117+
standard_edition = create(
118+
:published_standard_edition,
119+
configurable_document_type: "test",
120+
title: "English title",
121+
summary: "English summary",
122+
block_content:,
123+
)
124+
125+
translate_for.each { |locale| create_translated_edition(standard_edition, locale:) }
126+
127+
standard_edition
128+
end
129+
130+
def create_translated_edition(standard_edition, locale:, with_image: false)
131+
block_content = { "body" => translated_body_content(locale) }
132+
block_content["image"] = create(:image).image_data.id if with_image
133+
134+
I18n.with_locale(locale) { standard_edition.translations.create!(locale:, block_content:) }
135+
end
136+
end

0 commit comments

Comments
 (0)