|
228 | 228 | expect(copied_form.group).to eq(group) |
229 | 229 | end |
230 | 230 | end |
| 231 | + |
| 232 | + context "when source form has Welsh language version with translated content" do |
| 233 | + let(:source_form) do |
| 234 | + form = create(:form, :live, :with_pages, pages_count: 2, available_languages: %w[en cy]) |
| 235 | + # Set Welsh-specific translations for form |
| 236 | + form.name_cy = "Ffurflen Gymraeg" |
| 237 | + form.privacy_policy_url_cy = "https://example.com/preifatrwydd" |
| 238 | + form.support_email_cy = "cymorth@example.com" |
| 239 | + form.support_phone_cy = "0800 111 222" |
| 240 | + form.support_url_cy = "https://example.com/cymorth" |
| 241 | + form.support_url_text_cy = "Cael cymorth" |
| 242 | + form.declaration_text_cy = "Rwy'n datgan bod hyn yn wir" |
| 243 | + form.what_happens_next_markdown_cy = "Byddwn yn cysylltu â chi" |
| 244 | + form.payment_url_cy = "https://example.com/talu" |
| 245 | + form.save! |
| 246 | + # Set Welsh translations for pages - must save each page individually |
| 247 | + form.pages.first.update!(question_text_cy: "Cwestiwn Cymraeg 1", hint_text_cy: "Awgrym Cymraeg 1", page_heading_cy: "Pennwr Tudalen Cymraeg 1") |
| 248 | + form.pages.last.update!(question_text_cy: "Cwestiwn Cymraeg 2", hint_text_cy: "Awgrym Cymraeg 2") |
| 249 | + # Synchronize live FormDocuments for both languages |
| 250 | + FormDocumentSyncService.new(form).synchronize_live_form |
| 251 | + form.reload |
| 252 | + form |
| 253 | + end |
| 254 | + |
| 255 | + it "copies the Welsh-translated content from the source form" do |
| 256 | + source_welsh = source_form.form_documents.find_by(language: "cy", tag: "live") |
| 257 | + expect(source_welsh).to be_present |
| 258 | + expect(source_welsh.content["name"]).to eq("Ffurflen Gymraeg") |
| 259 | + |
| 260 | + # Copied form should have Welsh FormDocument with translated content |
| 261 | + copied_welsh = copied_form.form_documents.find_by(language: "cy", tag: "draft") |
| 262 | + expect(copied_welsh).to be_present |
| 263 | + expect(copied_welsh.content["name"]).to eq("Copy of Ffurflen Gymraeg") |
| 264 | + expect(copied_welsh.content["privacy_policy_url"]).to eq("https://example.com/preifatrwydd") |
| 265 | + expect(copied_welsh.content["support_email"]).to eq("cymorth@example.com") |
| 266 | + expect(copied_welsh.content["support_phone"]).to eq("0800 111 222") |
| 267 | + expect(copied_welsh.content["support_url"]).to eq("https://example.com/cymorth") |
| 268 | + expect(copied_welsh.content["support_url_text"]).to eq("Cael cymorth") |
| 269 | + expect(copied_welsh.content["declaration_text"]).to eq("Rwy'n datgan bod hyn yn wir") |
| 270 | + expect(copied_welsh.content["what_happens_next_markdown"]).to eq("Byddwn yn cysylltu â chi") |
| 271 | + expect(copied_welsh.content["payment_url"]).to eq("https://example.com/talu") |
| 272 | + end |
| 273 | + |
| 274 | + it "persists Welsh translations on the copied Form model" do |
| 275 | + # Ensure model-level Mobility translations are set correctly |
| 276 | + expect(copied_form.name_cy).to eq("Copy of Ffurflen Gymraeg") |
| 277 | + expect(copied_form.privacy_policy_url_cy).to eq("https://example.com/preifatrwydd") |
| 278 | + expect(copied_form.support_email_cy).to eq("cymorth@example.com") |
| 279 | + expect(copied_form.support_phone_cy).to eq("0800 111 222") |
| 280 | + expect(copied_form.support_url_cy).to eq("https://example.com/cymorth") |
| 281 | + expect(copied_form.support_url_text_cy).to eq("Cael cymorth") |
| 282 | + expect(copied_form.declaration_text_cy).to eq("Rwy'n datgan bod hyn yn wir") |
| 283 | + expect(copied_form.what_happens_next_markdown_cy).to eq("Byddwn yn cysylltu â chi") |
| 284 | + expect(copied_form.payment_url_cy).to eq("https://example.com/talu") |
| 285 | + |
| 286 | + # Check that values are accessible in Welsh locale context |
| 287 | + Mobility.with_locale(:cy) do |
| 288 | + expect(copied_form.name).to eq("Copy of Ffurflen Gymraeg") |
| 289 | + expect(copied_form.privacy_policy_url).to eq("https://example.com/preifatrwydd") |
| 290 | + expect(copied_form.support_email).to eq("cymorth@example.com") |
| 291 | + end |
| 292 | + end |
| 293 | + |
| 294 | + it "persists Welsh translations on copied pages" do |
| 295 | + expect(copied_form.pages.count).to eq(2) |
| 296 | + |
| 297 | + # Reload to get the latest state from the database |
| 298 | + copied_form.reload |
| 299 | + first_page = copied_form.pages.first |
| 300 | + expect(first_page.question_text_cy).to eq("Cwestiwn Cymraeg 1") |
| 301 | + expect(first_page.hint_text_cy).to eq("Awgrym Cymraeg 1") |
| 302 | + expect(first_page.page_heading_cy).to eq("Pennwr Tudalen Cymraeg 1") |
| 303 | + |
| 304 | + last_page = copied_form.pages.last |
| 305 | + expect(last_page.question_text_cy).to eq("Cwestiwn Cymraeg 2") |
| 306 | + expect(last_page.hint_text_cy).to eq("Awgrym Cymraeg 2") |
| 307 | + |
| 308 | + # Check that page values are accessible in Welsh locale context |
| 309 | + Mobility.with_locale(:cy) do |
| 310 | + expect(first_page.question_text).to eq("Cwestiwn Cymraeg 1") |
| 311 | + expect(first_page.hint_text).to eq("Awgrym Cymraeg 1") |
| 312 | + end |
| 313 | + end |
| 314 | + end |
| 315 | + |
| 316 | + context "when Welsh copy fails" do |
| 317 | + let(:source_form) do |
| 318 | + form = create(:form, :live, available_languages: %w[en cy]) |
| 319 | + form.name_cy = "Ffurflen Gymraeg" |
| 320 | + form.save! |
| 321 | + FormDocumentSyncService.new(form).synchronize_live_form |
| 322 | + form |
| 323 | + end |
| 324 | + |
| 325 | + it "rolls back the entire copy if Welsh translation copying fails" do |
| 326 | + # Stub the Welsh copying to raise an AR::RecordInvalid to simulate a failure mid-transaction |
| 327 | + service = described_class.new(source_form, logged_in_user) |
| 328 | + allow(service).to receive(:copy_welsh_translations) |
| 329 | + .and_raise(ActiveRecord::RecordInvalid.new(Form.new)) |
| 330 | + |
| 331 | + expect { |
| 332 | + service.copy(tag: "live") |
| 333 | + }.to raise_error(ActiveRecord::RecordInvalid) |
| 334 | + |
| 335 | + # Ensure no copied form was persisted |
| 336 | + expect(Form.where(copied_from_id: source_form.id)).to be_empty |
| 337 | + end |
| 338 | + end |
231 | 339 | end |
232 | 340 | end |
0 commit comments