From 90a94eff0eb52527699b43d3fc8202b7b1f639b4 Mon Sep 17 00:00:00 2001 From: Carl Manson <506508+cmanson@users.noreply.github.com> Date: Wed, 30 Oct 2024 14:04:13 +0000 Subject: [PATCH] GFORMS-2973 - Return English if Welsh is blank (#2307) --- .../gform/sharedmodel/LocalisedString.scala | 6 +++++- .../gform/sharedmodel/SmartStringSpec.scala | 21 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/app/uk/gov/hmrc/gform/sharedmodel/LocalisedString.scala b/app/uk/gov/hmrc/gform/sharedmodel/LocalisedString.scala index 5bd4614ac..8a5c1ab2f 100644 --- a/app/uk/gov/hmrc/gform/sharedmodel/LocalisedString.scala +++ b/app/uk/gov/hmrc/gform/sharedmodel/LocalisedString.scala @@ -20,7 +20,11 @@ import play.api.libs.functional.syntax._ import uk.gov.hmrc.gform.sharedmodel.formtemplate.JsonUtils case class LocalisedString(m: Map[LangADT, String]) { - def value(implicit l: LangADT): String = m.getOrElse(l, m.getOrElse(LangADT.En, "")) + def value(implicit l: LangADT): String = { + val check: String = m.getOrElse(l, "") + if (check.isBlank) m.getOrElse(LangADT.En, "") else check + } + def replace(toReplace: String, replaceWith: String): LocalisedString = copy(m = (m.map { case (lang, message) => (lang, message.replace(toReplace, replaceWith)) })) def transform(fEn: String => String, fCy: String => String): LocalisedString = diff --git a/test/uk/gov/hmrc/gform/sharedmodel/SmartStringSpec.scala b/test/uk/gov/hmrc/gform/sharedmodel/SmartStringSpec.scala index d2a93717d..092f90960 100644 --- a/test/uk/gov/hmrc/gform/sharedmodel/SmartStringSpec.scala +++ b/test/uk/gov/hmrc/gform/sharedmodel/SmartStringSpec.scala @@ -23,6 +23,9 @@ import uk.gov.hmrc.gform.sharedmodel.formtemplate.generators.{ ExprGen, Primitiv class SmartStringSpec extends Spec { + def welsh: String = "Welsh" + def english: String = "English" + "JSON" should "round trip" in { forAll(Gen.asciiStr, Gen.asciiStr, PrimitiveGen.zeroOrMoreGen(ExprGen.exprGen())) { (english, welsh, exprs) => val cEnglish = condition(english) @@ -35,6 +38,24 @@ class SmartStringSpec extends Spec { } } + "SmartString when Welsh requested" should "return English if Welsh not defined" in { + implicit val l: LangADT = LangADT.Cy + val smartString = SmartString(LocalisedString(Map(LangADT.En -> english)), Nil) + smartString.rawDefaultValue shouldBe english + } + + it should "return English if Welsh is defined and blank" in { + implicit val l: LangADT = LangADT.Cy + val smartString = SmartString(LocalisedString(Map(LangADT.En -> english, LangADT.Cy -> "")), Nil) + smartString.rawDefaultValue shouldBe english + } + + it should "return Welsh if Welsh is defined and not blank" in { + implicit val l: LangADT = LangADT.Cy + val smartString = SmartString(LocalisedString(Map(LangADT.En -> english, LangADT.Cy -> welsh)), Nil) + smartString.rawDefaultValue shouldBe welsh + } + private def condition(s: String): String = s.flatMap { c => if (c >= 32 && c <= 127 && c != '"' && c != '\\') Seq(c)