|
| 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