Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,39 +47,46 @@ class LandlordController @Inject()(view: LandlordView,
navigator: Navigator
)(implicit appConfig: AppConfig, ec: ExecutionContext) extends FrontendController(mcc) with I18nSupport {

def otherRelationship(form: Form[LandlordForm])(implicit messages: Messages): NGRRadioButtons = NGRRadioButtons(
radioContent = "landlord.radio5",
radioValue = OtherRelationship,

def landlordYesButton(form: Form[LandlordForm])(implicit messages: Messages): NGRRadioButtons = NGRRadioButtons(
radioContent = "service.yes",
radioValue = LandlordYes,
conditionalHtml = Some(ngrCharacterCountComponent(form,
NGRCharacterCount(
id = "landlord-radio-other",
name = "landlord-radio-other",
id = "landlord-relationship",
name = "landlord-relationship",
maxLength = Some(250),
label = Label(
classes = "govuk-label govuk-label--s",
content = Text(Messages("landlord.radio5.dropdown"))
content = Text(Messages("landlord.radio.yes"))
),
hint = Some(
Hint(
id = Some("landlord-relationship-hint"),
classes = "",
attributes = Map.empty,
content = Text(messages("landlord.radio.yes.hint"))
)
)
)))
)

def ngrRadio(form: Form[LandlordForm])(implicit messages: Messages): NGRRadio =
val ngrRadioButtons: Seq[NGRRadioButtons] = Seq(
NGRRadioButtons(radioContent = "landlord.radio1", radioValue = LandLordAndTenant),
NGRRadioButtons(radioContent = "landlord.radio2", radioValue = FamilyMember),
NGRRadioButtons(radioContent = "landlord.radio3", radioValue = CompanyPensionFund),
NGRRadioButtons(radioContent = "landlord.radio4", radioValue = BusinessPartnerOrSharedDirector)
landlordYesButton(form),
NGRRadioButtons(radioContent = "service.no", radioValue = LandlordNo)
)
NGRRadio(
NGRRadioName("landlord-radio"),
ngrTitle = Some(Legend(content = Text(messages("landlord.p2")), classes = "govuk-fieldset__legend--m", isPageHeading = true)),
NGRRadioButtons = ngrRadioButtons :+ otherRelationship(form)
NGRRadioButtons = ngrRadioButtons
)

def show(mode: Mode): Action[AnyContent] = {
(authenticate andThen getData).async { implicit request =>
val preparedForm = request.userAnswers.getOrElse(UserAnswers(request.credId)).get(LandlordPage) match {
case None => form
case Some(value) => form.fill(LandlordForm(value.landlordName,value.landLordType,value.landlordOtherDesc))
case Some(value) => form.fill(LandlordForm(value.landlordName,value.landlordRelationship,value.landlordOtherDesc))
}
Future.successful(Ok(view(selectedPropertyAddress = request.property.addressFull, form = preparedForm, ngrRadio = buildRadios(preparedForm, ngrRadio(preparedForm)), mode))
)
Expand All @@ -96,9 +103,9 @@ class LandlordController @Inject()(view: LandlordView,
val correctedFormErrors = formWithErrors.errors.map { formError =>
(formError.key, formError.messages) match
case ("", messages) if messages.contains("landlord.radio.other.empty.error") =>
formError.copy(key = "landlord-radio-other")
formError.copy(key = "landlord-yes")
case ("", messages) if messages.contains("landlord.radio.other.tooLong.error") =>
formError.copy(key = "landlord-radio-other")
formError.copy(key = "landlord-yes")
case _ =>
formError
}
Expand All @@ -112,7 +119,7 @@ class LandlordController @Inject()(view: LandlordView,
landlordForm =>
for {
updatedAnswers <- Future.fromTry(request.userAnswers.getOrElse(UserAnswers(request.credId))
.set(LandlordPage, Landlord(landlordForm.landlordName, landlordForm.landLordType, landlordForm.landlordOther)))
.set(LandlordPage, Landlord(landlordForm.landlordName, landlordForm.landlordRelationship, landlordForm.landlordYesSelected)))
_ <- sessionRepository.set(updatedAnswers)
} yield Redirect(navigator.nextPage(LandlordPage, mode, updatedAnswers))
)
Expand Down
2 changes: 1 addition & 1 deletion app/uk/gov/hmrc/ngrraldfrontend/models/Landlord.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package uk.gov.hmrc.ngrraldfrontend.models
import play.api.libs.json.{Json, OFormat}

final case class Landlord(landlordName: String,
landLordType: String,
landlordRelationship: Boolean,
landlordOtherDesc: Option[String] = None)

object Landlord {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ case object FamilyMember extends Landlord
case object CompanyPensionFund extends Landlord
case object BusinessPartnerOrSharedDirector extends Landlord
case object OtherRelationship extends Landlord
case object LandlordYes extends Landlord
case object LandlordNo extends Landlord


sealed trait RentBasedOn extends RadioEntry
Expand Down
30 changes: 15 additions & 15 deletions app/uk/gov/hmrc/ngrraldfrontend/models/forms/LandlordForm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,44 @@ import play.api.libs.json.{Json, OFormat}
import uk.gov.hmrc.ngrraldfrontend.models.forms.WhatIsYourRentBasedOnForm.firstError
import uk.gov.hmrc.ngrraldfrontend.models.forms.mappings.Mappings

final case class LandlordForm(landlordName: String, landLordType: String, landlordOther: Option[String])
final case class LandlordForm(landlordName: String, landlordRelationship: Boolean, landlordYesSelected: Option[String])

object LandlordForm extends CommonFormValidators with Mappings{
implicit val format: OFormat[LandlordForm] = Json.format[LandlordForm]

private lazy val landlordNameEmptyError = "landlord.name.empty.error"
private lazy val landlordNameTooLongError = "landlord.name.empty.tooLong.error"
private lazy val radioUnselectedError = "landlord.radio.empty.error"
private lazy val otherRadioEmptyError = "landlord.radio.other.empty.error"
private lazy val otherRadioTooLongError = "landlord.radio.other.tooLong.error"
private lazy val landlordEmptyError = "landlord.radio.other.empty.error"
private lazy val landlordTooLongError = "landlord.radio.other.tooLong.error"

private val landlord = "landlord-name-value"
private val landlordRadio = "landlord-radio"
private val landlordOther = "landlord-radio-other"
private val landlordYesSelected = "landlord-relationship"


val messagesApi: MessagesApi = new DefaultMessagesApi()
val lang: Lang = Lang.defaultLang
val messages: Messages = MessagesImpl(lang, messagesApi)


def unapply(landlordForm: LandlordForm): Option[(String, String, Option[String])] =
Some((landlordForm.landlordName, landlordForm.landLordType, landlordForm.landlordOther))
def unapply(landlordForm: LandlordForm): Option[(String, Boolean, Option[String])] =
Some((landlordForm.landlordName, landlordForm.landlordRelationship, landlordForm.landlordYesSelected))

private def isOtherTextEmpty[A]: Constraint[A] =
Constraint((input: A) =>
val rentBasedOnForm = input.asInstanceOf[LandlordForm]
if (rentBasedOnForm.landLordType.equals("OtherRelationship") && rentBasedOnForm.landlordOther.getOrElse("").isBlank)
Invalid(otherRadioEmptyError)
val landlordForm = input.asInstanceOf[LandlordForm]
if (landlordForm.landlordRelationship.equals("LandlordYes") && landlordForm.landlordYesSelected.getOrElse("").isBlank)
Invalid(landlordEmptyError)
else
Valid
)

private def otherTextMaxLength[A]: Constraint[A] =
Constraint((input: A) =>
val rentBasedOnForm = input.asInstanceOf[LandlordForm]
if (rentBasedOnForm.landLordType.equals("OtherRelationship") && rentBasedOnForm.landlordOther.getOrElse("").length > 250)
Invalid(otherRadioTooLongError)
val landlordForm = input.asInstanceOf[LandlordForm]
if (landlordForm.landlordRelationship.equals("LandlordYes") && landlordForm.landlordYesSelected.getOrElse("").length > 250)
Invalid(landlordTooLongError)
else
Valid
)
Expand All @@ -78,15 +78,15 @@ object LandlordForm extends CommonFormValidators with Mappings{

),
landlordRadio -> radioText(radioUnselectedError),
landlordOther -> optional(
landlordYesSelected -> optional(
play.api.data.Forms.text
.transform[String](_.strip(), identity)
)
)(LandlordForm.apply)(LandlordForm.unapply)
.verifying(
firstError(
isOtherTextEmpty,
otherTextMaxLength
landlordEmptyError,
landlordTooLongError
)
)
)
Expand Down
8 changes: 2 additions & 6 deletions conf/messages
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,12 @@ typeOfLeaseRenewal.required.error = Select what type of lease renewal it is
landlord.title = Landlord
landlord.p1 = What is the landlord''s full name?
landlord.p2 = What is your relationship with the landlord?
landlord.radio1 = Landlord and tenant relationship only
landlord.radio2 = Family member
landlord.radio3 = Company pension fund
landlord.radio4 = Business partner or shared director
landlord.radio5 = Other relationship
landlord.radio5.dropdown = Can you tell us what your relationship with the landlord is?
landlord.name.empty.error = Enter the landlord''s full name
landlord.name.empty.tooLong.error = Landlord''s full name must be 50 characters or less
landlord.radio.empty.error = Select what your relationship with the landlord is
landlord.radio.other.empty.error = Tell us what your relationship with the landlord is
landlord.radio.yes = Can you tell us what your relationship with the landlord is?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please remove unused ones? eg. landlord.radio1

landlord.radio.yes.hint = For example, the landlord is a family member, business partner, shared director or company pension fund
landlord.radio.other.tooLong.error = Maximum character allowed is 250

#WhatIsYourRentBasedOn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class LandlordControllerSpec extends ControllerSpecSupport {
val content = contentAsString(result)
val document = Jsoup.parse(content)
document.select("input[name=landlord-name-value]").attr("value") mustBe "Joe Bloggs"
document.select("input[name=landlord-radio]").attr("value") mustBe "LandLordAndTenant"
document.select("input[name=landlord-radio]").attr("value") mustBe "LandlordYes"

}
"Return NotFoundException when property is not found in the mongo" in {
Expand Down Expand Up @@ -148,8 +148,8 @@ class LandlordControllerSpec extends ControllerSpecSupport {
val result = controllerProperty.submit(NormalMode)(AuthenticatedUserRequest(FakeRequest(routes.LandlordController.submit(NormalMode))
.withFormUrlEncodedBody(
"landlord-name-value" -> "Bob",
"landlord-radio" -> "OtherRelationship",
"landlordOther" -> "",
"landlord-radio" -> "LandlordYes",
"landlord-yes" -> "",
)
.withHeaders(HeaderNames.authorisation -> "Bearer 1"), None, None, None, Some(property), credId = Some(credId.value), None, None, nino = Nino(true, Some(""))))
result.map(result => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,29 @@ class LandlordFormSpec extends AnyFlatSpec with Matchers {
boundForm.errors shouldBe List(FormError("landlord-radio", List("landlord.radio.empty.error"), List()))
}

it should "fail when 'OtherRelationship' is selected but no description is provided" in {
it should "fail when 'Yes' is selected but no description is provided" in {
val data = Map(
"landlord-name-value" -> "Jane Doe",
"landlord-radio" -> "OtherRelationship",
"landlord-radio-other" -> " " // whitespace only
"landlord-radio" -> "LandlordYes",
"landlord-yes" -> " " // whitespace only
)

val boundForm = LandlordForm.form.bind(data)

boundForm.errors shouldBe List(FormError("", List("landlord.radio.other.empty.error"), List()))
}

it should "pass when 'OtherRelationship' is selected and description is provided" in {
it should "pass when 'Yes' is selected and description is provided" in {
val data = Map(
"landlord-name-value" -> "Jane Doe",
"landlord-radio" -> "OtherRelationship",
"landlord-radio-other" -> "Friend of the family"
"landlord-radio" -> "LandlordYes",
"landlord-yes" -> "Friend of the family"
)

val boundForm = LandlordForm.form.bind(data)

boundForm.errors shouldBe empty
boundForm.value shouldBe Some(LandlordForm("Jane Doe", "OtherRelationship", Some("Friend of the family")))
boundForm.value shouldBe Some(LandlordForm("Jane Doe", "LandlordYes", Some("Friend of the family")))
}

"LandlordForm.unapply" should "extract fields correctly" in {
Expand All @@ -92,45 +92,44 @@ class LandlordFormSpec extends AnyFlatSpec with Matchers {
val form = LandlordForm("Jane Smith", "LandLordAndTenant", None)
val result = LandlordForm.unapply(form)

result shouldBe Some(("Jane Smith", "LandLordAndTenant", None))
result shouldBe Some(("Jane Smith", "LandlordYes", None))
}

"LandlordForm.format" should "serialize to JSON correctly" in {
val form = LandlordForm("John Doe", "OtherRelationship", Some("Other info"))
val form = LandlordForm("John Doe", "LandlordYes", Some("Other info"))
val json = Json.toJson(form)

json shouldBe Json.obj(
"landlordName" -> "John Doe",
"landLordType" -> "OtherRelationship",
"landlordOther" -> "Other info"
"landLordType" -> "LandlordYes",
"landlordYesSelected" -> "Other info"
)
}

it should "deserialize from JSON correctly" in {
val json = Json.obj(
"landlordName" -> "Jane Smith",
"landLordType" -> "LandLordAndTenant",
"landlordOther" -> JsNull
"landLordType" -> "LandlordYes",
"landlordYes" -> JsNull
)

val result = json.validate[LandlordForm]
result.isSuccess shouldBe true
result.get shouldBe LandlordForm("Jane Smith", "LandLordAndTenant", None)
result.get shouldBe LandlordForm("Jane Smith", "LandlordYes", None)
}

def validate(form: LandlordForm): Boolean = {
form.landLordType != "OtherRelationship" ||
form.landlordOther.forall(_.length <= 250)
form.landlordYesSelected.forall(_.length <= 250)
}

it should "pass if landLordType is OtherRelationship and landlordOther is <= 250 characters" in {
val form = LandlordForm("Jane Smith","OtherRelationship", Some("This is a valid description."))
val form = LandlordForm("Jane Smith","LandlordYes", Some("This is a valid description."))
validate(form) shouldBe true
}

it should "fail if landLordType is OtherRelationship and landlordOther is > 250 characters" in {
val longText = "x" * 251
val form = LandlordForm("Jane Smith", "OtherRelationship", Some(longText))
val form = LandlordForm("Jane Smith", "LandlordYes", Some(longText))
validate(form) shouldBe false
}

Expand Down
43 changes: 13 additions & 30 deletions test/uk/gov/hmrc/ngrraldfrontend/views/LandlordViewSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ class LandlordViewSpec extends ViewBaseSpec {
val heading = "Landlord"
val textInputLabel = "What is the landlord's full name?"
val radioLabel = "What is your relationship with the landlord?"
val radio1 = "Landlord and tenant relationship only"
val radio2 = "Family member"
val radio3 = "Company pension fund"
val radio4 = "Business partner or shared director"
val radio5 = "Other relationship"
val radio1 = "Yes"
val radio2 = "No"
// val radio3 = "Company pension fund"
// val radio4 = "Business partner or shared director"
// val radio5 = "Other relationship"
val continue = "Continue"


Expand All @@ -52,28 +52,23 @@ class LandlordViewSpec extends ViewBaseSpec {
val radioLabel = "#main-content > div > div > form > div:nth-child(5) > fieldset > legend > h1"
val radio1 = "#main-content > div > div > form > div:nth-child(5) > fieldset > div > div:nth-child(1) > label"
val radio2 = "#main-content > div > div > form > div:nth-child(5) > fieldset > div > div:nth-child(2) > label"
val radio3 = "#main-content > div > div > form > div:nth-child(5) > fieldset > div > div:nth-child(3) > label"
val radio4 = "#main-content > div > div > form > div:nth-child(5) > fieldset > div > div:nth-child(4) > label"
val radio5 = "#main-content > div > div > form > div:nth-child(5) > fieldset > div > div:nth-child(5) > label"
val continue = "#continue"
}

val address = "5 Brixham Marina, Berry Head Road, Brixham, Devon, TQ5 9BW"
private val landLordAndTennant: NGRRadioButtons = NGRRadioButtons("Landlord and tenant relationship only", LandLordAndTenant)
private val familyMember: NGRRadioButtons = NGRRadioButtons("Family member", FamilyMember)
private val companyPensionFund: NGRRadioButtons = NGRRadioButtons("Company pension fund", CompanyPensionFund)
private val businessPartnerOrSharedDirector: NGRRadioButtons = NGRRadioButtons("Company pension fund", BusinessPartnerOrSharedDirector)
private val landLordAndTennant: NGRRadioButtons = NGRRadioButtons("Yes", LandlordYes)
private val familyMember: NGRRadioButtons = NGRRadioButtons("No", LandlordNo)

private def otherRelationship(form: Form[LandlordForm])(implicit messages: Messages): NGRRadioButtons = NGRRadioButtons(
radioContent = "landlord.radio5",
radioValue = OtherRelationship,
radioContent = "service.yes",
radioValue = LandlordYes,
conditionalHtml = Some(ngrCharacterCountComponent(form,
NGRCharacterCount(
id = "landlord-radio-other",
name = "landlord-radio-other",
id = "landlord-yes",
name = "landlord-yes",
label = Label(
classes = "govuk-label govuk-label--m",
content = Text(Messages("landlord.radio5.dropdown"))
content = Text(Messages("landlord.radio.yes"))
),
errorMessage = Some(ErrorMessage(
id = Some("radio-other-error"),
Expand All @@ -83,7 +78,7 @@ class LandlordViewSpec extends ViewBaseSpec {
)
)

private val ngrRadio: NGRRadio = NGRRadio(NGRRadioName("what-type-of-agreement-radio"), Seq(landLordAndTennant, familyMember, companyPensionFund, businessPartnerOrSharedDirector, otherRelationship(form)))
private val ngrRadio: NGRRadio = NGRRadio(NGRRadioName("what-type-of-agreement-radio"), Seq(landLordAndTennant, familyMember))
val form = LandlordForm.form.fillAndValidate(LandlordForm(landlordName = "Bob", landLordType = "FamilyMember", None))
val radio: Radios = buildRadios(form, ngrRadio)

Expand Down Expand Up @@ -126,18 +121,6 @@ class LandlordViewSpec extends ViewBaseSpec {
elementText(Selectors.radio2) mustBe Strings.radio2
}

"show correct radio 3" in {
elementText(Selectors.radio3) mustBe Strings.radio3
}

"show correct radio 4" in {
elementText(Selectors.radio4) mustBe Strings.radio4
}

"show correct radio 5" in {
elementText(Selectors.radio5) mustBe Strings.radio5
}

"show correct continue button" in {
elementText(Selectors.continue) mustBe Strings.continue
}
Expand Down