diff --git a/app/uk/gov/hmrc/ngrraldfrontend/controllers/DidYouGetMoneyFromLandlordController.scala b/app/uk/gov/hmrc/ngrraldfrontend/controllers/DidYouGetMoneyFromLandlordController.scala new file mode 100644 index 0000000..8777783 --- /dev/null +++ b/app/uk/gov/hmrc/ngrraldfrontend/controllers/DidYouGetMoneyFromLandlordController.scala @@ -0,0 +1,80 @@ +/* + * Copyright 2025 HM Revenue & Customs + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package uk.gov.hmrc.ngrraldfrontend.controllers + +import play.api.i18n.I18nSupport +import play.api.mvc.{Action, AnyContent, MessagesControllerComponents} +import uk.gov.hmrc.ngrraldfrontend.actions.{AuthRetrievals, DataRetrievalAction} +import uk.gov.hmrc.ngrraldfrontend.config.AppConfig +import uk.gov.hmrc.ngrraldfrontend.models.components.NGRRadio.buildRadios +import uk.gov.hmrc.ngrraldfrontend.models.forms.DidYouGetMoneyFromLandlordForm +import uk.gov.hmrc.ngrraldfrontend.models.forms.DidYouGetMoneyFromLandlordForm.form +import uk.gov.hmrc.ngrraldfrontend.models.{Mode, UserAnswers} +import uk.gov.hmrc.ngrraldfrontend.navigation.Navigator +import uk.gov.hmrc.ngrraldfrontend.pages.DidYouGetMoneyFromLandlordPage +import uk.gov.hmrc.ngrraldfrontend.repo.SessionRepository +import uk.gov.hmrc.ngrraldfrontend.views.html.DidYouGetMoneyFromLandlordView +import uk.gov.hmrc.play.bootstrap.frontend.controller.FrontendController + +import javax.inject.Inject +import scala.concurrent.{ExecutionContext, Future} + +class DidYouGetMoneyFromLandlordController @Inject()(didYouGetMoneyFromLandlordView: DidYouGetMoneyFromLandlordView, + authenticate: AuthRetrievals, + getData: DataRetrievalAction, + sessionRepository: SessionRepository, + navigator: Navigator, + mcc: MessagesControllerComponents)(implicit appConfig: AppConfig, ec: ExecutionContext) + extends FrontendController(mcc) with I18nSupport { + + + def show(mode: Mode): Action[AnyContent] = { + (authenticate andThen getData).async { implicit request => + val preparedForm = request.userAnswers.getOrElse(UserAnswers(request.credId)).get(DidYouGetMoneyFromLandlordPage) match { + case None => form + case Some(value) => form.fill(DidYouGetMoneyFromLandlordForm(value.toString)) + + } + Future.successful(Ok(didYouGetMoneyFromLandlordView( + selectedPropertyAddress = request.property.addressFull, + form = preparedForm, + ngrRadio = buildRadios(preparedForm, DidYouGetMoneyFromLandlordForm.moneyLandlordRadio), + mode = mode + ))) + } + } + + def submit(mode: Mode): Action[AnyContent] = + (authenticate andThen getData).async { implicit request => + form.bindFromRequest().fold( + formWithErrors => { + Future.successful(BadRequest(didYouGetMoneyFromLandlordView( + form = formWithErrors, + ngrRadio = buildRadios(formWithErrors, DidYouGetMoneyFromLandlordForm.moneyLandlordRadio), + selectedPropertyAddress = request.property.addressFull, + mode = mode + ))) + }, + radioValue => + for { + updatedAnswers <- Future.fromTry(request.userAnswers.getOrElse(UserAnswers(request.credId)).set(DidYouGetMoneyFromLandlordPage, radioValue.radio.toBoolean)) + _ <- sessionRepository.set(updatedAnswers) + } yield Redirect(navigator.nextPage(DidYouGetMoneyFromLandlordPage, mode, updatedAnswers)) + + ) + } +} diff --git a/app/uk/gov/hmrc/ngrraldfrontend/models/forms/DidYouGetMoneyFromLandlordForm.scala b/app/uk/gov/hmrc/ngrraldfrontend/models/forms/DidYouGetMoneyFromLandlordForm.scala new file mode 100644 index 0000000..69d13f3 --- /dev/null +++ b/app/uk/gov/hmrc/ngrraldfrontend/models/forms/DidYouGetMoneyFromLandlordForm.scala @@ -0,0 +1,62 @@ +/* + * Copyright 2025 HM Revenue & Customs + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package uk.gov.hmrc.ngrraldfrontend.models.forms + +import play.api.data.Form +import play.api.data.Forms.{mapping, optional} +import play.api.data.validation.{Constraint, Invalid, Valid} +import play.api.i18n.* +import play.api.libs.json.{Json, OFormat} +import uk.gov.hmrc.govukfrontend.views.Aliases.Text +import uk.gov.hmrc.govukfrontend.views.viewmodels.fieldset.Legend +import uk.gov.hmrc.ngrraldfrontend.models.components.NGRRadio +import uk.gov.hmrc.ngrraldfrontend.models.components.NGRRadio.{ngrRadio, noButton, yesButton} +import uk.gov.hmrc.ngrraldfrontend.models.forms.mappings.Mappings + +final case class DidYouGetMoneyFromLandlordForm(radio: String) + +object DidYouGetMoneyFromLandlordForm extends Mappings{ + implicit val format: OFormat[DidYouGetMoneyFromLandlordForm] = Json.format[DidYouGetMoneyFromLandlordForm] + + private lazy val radioUnselectedError = "didYouGetMoneyFromLandlord.empty.error" + private val moneyFromLandlordRadio = "didYouGetMoneyFromLandlord-radio-value" + + val messagesApi: MessagesApi = new DefaultMessagesApi() + val lang: Lang = Lang.defaultLang + val messages: Messages = MessagesImpl(lang, messagesApi) + + def unapply(didYouGetMoneyFromLandlordForm: DidYouGetMoneyFromLandlordForm): Option[String] = Some(didYouGetMoneyFromLandlordForm.radio) + + def form: Form[DidYouGetMoneyFromLandlordForm] = { + Form( + mapping( + moneyFromLandlordRadio -> radioText(radioUnselectedError), + )(DidYouGetMoneyFromLandlordForm.apply)(DidYouGetMoneyFromLandlordForm.unapply) + ) + } + + def moneyLandlordRadio(implicit messages: Messages): NGRRadio = + ngrRadio( + radioName = moneyFromLandlordRadio, + radioButtons = Seq( + yesButton(), + noButton() + ), + ngrTitle = "didYouGetMoneyFromLandlord.title", + ngrTitleClass = "govuk-fieldset__legend--l" + ) +} diff --git a/app/uk/gov/hmrc/ngrraldfrontend/models/forms/LandlordForm.scala b/app/uk/gov/hmrc/ngrraldfrontend/models/forms/LandlordForm.scala index 0e1cd0a..0b9cf7b 100644 --- a/app/uk/gov/hmrc/ngrraldfrontend/models/forms/LandlordForm.scala +++ b/app/uk/gov/hmrc/ngrraldfrontend/models/forms/LandlordForm.scala @@ -40,7 +40,7 @@ object LandlordForm extends CommonFormValidators with Mappings{ 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 landlordRelationshipEmptyError = "landlord.relationship.empty.error" + private lazy val landlordRelationshipEmptyError = "landlord.relationship.emptyText.error" private lazy val landlordRelationshipTooLongError = "landlord.radio.tooLong.error" private val landlord = "landlord-name-value" diff --git a/app/uk/gov/hmrc/ngrraldfrontend/navigation/Navigator.scala b/app/uk/gov/hmrc/ngrraldfrontend/navigation/Navigator.scala index 3171fbe..2bd9b27 100644 --- a/app/uk/gov/hmrc/ngrraldfrontend/navigation/Navigator.scala +++ b/app/uk/gov/hmrc/ngrraldfrontend/navigation/Navigator.scala @@ -149,6 +149,9 @@ class Navigator @Inject()() { case None => uk.gov.hmrc.ngrraldfrontend.controllers.routes.CheckRentFreePeriodController.show(NormalMode) } case RentFreePeriodPage => _ => uk.gov.hmrc.ngrraldfrontend.controllers.routes.RentDatesAgreeStartController.show(NormalMode) + case ConfirmBreakClausePage => _ => uk.gov.hmrc.ngrraldfrontend.controllers.routes.LandlordController.show(NormalMode) //TODO This needs to be amended when the journey is completed + case DidYouGetMoneyFromLandlordPage => _ => uk.gov.hmrc.ngrraldfrontend.controllers.routes.LandlordController.show(NormalMode) //TODO This needs to be amended when the journey is completed + case DoYouPayExtraForParkingSpacesPage => answers => answers.get(DoYouPayExtraForParkingSpacesPage) match { case Some(value) => value match { diff --git a/app/uk/gov/hmrc/ngrraldfrontend/pages/DidYouGetMoneyFromLandlordPage.scala b/app/uk/gov/hmrc/ngrraldfrontend/pages/DidYouGetMoneyFromLandlordPage.scala new file mode 100644 index 0000000..cffbd69 --- /dev/null +++ b/app/uk/gov/hmrc/ngrraldfrontend/pages/DidYouGetMoneyFromLandlordPage.scala @@ -0,0 +1,27 @@ +/* + * Copyright 2025 HM Revenue & Customs + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package uk.gov.hmrc.ngrraldfrontend.pages + +import play.api.libs.json.JsPath + +case object DidYouGetMoneyFromLandlordPage extends QuestionPage[Boolean] { + + override def toString: String = "confirmBreakClause" + + override def path: JsPath = JsPath \ toString + +} diff --git a/app/uk/gov/hmrc/ngrraldfrontend/views/DidYouGetMoneyFromLandlordView.scala.html b/app/uk/gov/hmrc/ngrraldfrontend/views/DidYouGetMoneyFromLandlordView.scala.html new file mode 100644 index 0000000..c014c78 --- /dev/null +++ b/app/uk/gov/hmrc/ngrraldfrontend/views/DidYouGetMoneyFromLandlordView.scala.html @@ -0,0 +1,46 @@ +@* + * Copyright 2025 HM Revenue & Customs + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *@ + +@import uk.gov.hmrc.ngrraldfrontend.config.AppConfig + +@import uk.gov.hmrc.govukfrontend.views.html.components._ +@import uk.gov.hmrc.govukfrontend.views.Aliases._ +@import uk.gov.hmrc.ngrraldfrontend.views.html.components._ +@import uk.gov.hmrc.ngrraldfrontend.viewmodels.govuk.all._ +@import uk.gov.hmrc.ngrraldfrontend.config.AppConfig +@import uk.gov.hmrc.ngrraldfrontend.models.components.NavigationBarContent +@import uk.gov.hmrc.ngrraldfrontend.models.forms.DidYouGetMoneyFromLandlordForm + +@this( + layout: Layout, + formHelper: FormWithCSRF, + govukErrorSummary: GovukErrorSummary, + govukRadios : GovukRadios, + saveAndContinueButton: saveAndContinueButton +) + +@(selectedPropertyAddress: String, form: Form[DidYouGetMoneyFromLandlordForm], ngrRadio: Radios, mode: Mode)(implicit request: RequestHeader, messages: Messages, appConfig: AppConfig) + +@layout(pageTitle = Some(messages("didYouGetMoneyFromLandlord.title")), showBackLink = true, fullWidth = false) { + @formHelper(action = uk.gov.hmrc.ngrraldfrontend.controllers.routes.DidYouGetMoneyFromLandlordController.submit(mode), Symbol("autoComplete") -> "off") { + @if(form.errors.nonEmpty) { + @govukErrorSummary(ErrorSummaryViewModel(form)) + } + @selectedPropertyAddress + @govukRadios(ngrRadio) + @saveAndContinueButton(msg = messages("service.continue"), isStartButton = false) + } +} \ No newline at end of file diff --git a/conf/app.routes b/conf/app.routes index 7ddf8e8..2bb0ada 100644 --- a/conf/app.routes +++ b/conf/app.routes @@ -28,6 +28,12 @@ POST /landlord uk.gov.hmrc.ngrraldfront GET /landlord/change uk.gov.hmrc.ngrraldfrontend.controllers.LandlordController.show(mode: Mode = CheckMode) POST /landlord/change uk.gov.hmrc.ngrraldfrontend.controllers.LandlordController.submit(mode: Mode = CheckMode) +#Did you get money from landlord +GET /did-you-get-money-from-landlord uk.gov.hmrc.ngrraldfrontend.controllers.DidYouGetMoneyFromLandlordController.show(mode: Mode = NormalMode) +POST /did-you-get-money-from-landlord uk.gov.hmrc.ngrraldfrontend.controllers.DidYouGetMoneyFromLandlordController.submit(mode: Mode = NormalMode) +GET /did-you-get-money-from-landlord/change uk.gov.hmrc.ngrraldfrontend.controllers.DidYouGetMoneyFromLandlordController.show(mode: Mode = CheckMode) +POST /did-you-get-money-from-landlord/change uk.gov.hmrc.ngrraldfrontend.controllers.DidYouGetMoneyFromLandlordController.submit(mode: Mode = CheckMode) + #What is your rent based on GET /what-is-your-rent-based-on uk.gov.hmrc.ngrraldfrontend.controllers.WhatIsYourRentBasedOnController.show(mode: Mode = NormalMode) POST /what-is-your-rent-based-on uk.gov.hmrc.ngrraldfrontend.controllers.WhatIsYourRentBasedOnController.submit(mode: Mode = NormalMode) diff --git a/conf/messages b/conf/messages index e63b729..391912c 100644 --- a/conf/messages +++ b/conf/messages @@ -60,7 +60,7 @@ landlord.p2 = Do you have a relationship with the landlord other than as a tenan 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 yes if you have any relationship with landlord -landlord.relationship.empty.error = Tell us what your relationship with the landlord is +landlord.relationship.emptyText.error = Tell us what your relationship with the landlord is landlord.radio.tooLong.error = Maximum character allowed is 250 landlord.radio.yes = Can you tell us what your relationship with the landlord is? landlord.radio.yes.hint = For example, the landlord is a family member, business partner, shared director or company pension fund @@ -393,6 +393,10 @@ repairsAndInsurance.internalRepairs.radio.required.error = Select who pays for i repairsAndInsurance.externalRepairs.radio.required.error = Select who pays for external repairs repairsAndInsurance.buildingInsurance.radio.required.error = Select who pays for buildings insurance +#DidYouGetMoneyFromLandlord +didYouGetMoneyFromLandlord.title = Did you get any money from the landlord or previous tenant to take on the lease? +didYouGetMoneyFromLandlord.empty.error = Select yes if you got any money from the landlord or previous tenant to take on the lease + #Rent review rentReview.months = Months rentReview.years = Years @@ -438,4 +442,4 @@ parkingSpacesOrGaragesNotIncludedInYourRent.agreementDate.month.required.error = parkingSpacesOrGaragesNotIncludedInYourRent.agreementDate.monthAndYear.required.error = Date this payment was agreed for parking and garages must include a month and year parkingSpacesOrGaragesNotIncludedInYourRent.agreementDate.year.required.error = Date this payment was agreed for parking and garages must include a year parkingSpacesOrGaragesNotIncludedInYourRent.agreementDate.invalid.error = Date this payment was agreed for parking and garages must be a real date -parkingSpacesOrGaragesNotIncludedInYourRent.agreementDate.before.1900.error = Year payment was agreed for parking and garages must be 1900 or after +parkingSpacesOrGaragesNotIncludedInYourRent.agreementDate.before.1900.error = Year payment was agreed for parking and garages must be 1900 or after \ No newline at end of file diff --git a/test/uk/gov/hmrc/ngrraldfrontend/controllers/DidYouGetMoneyFromLandlordControllerSpec.scala b/test/uk/gov/hmrc/ngrraldfrontend/controllers/DidYouGetMoneyFromLandlordControllerSpec.scala new file mode 100644 index 0000000..3c21e42 --- /dev/null +++ b/test/uk/gov/hmrc/ngrraldfrontend/controllers/DidYouGetMoneyFromLandlordControllerSpec.scala @@ -0,0 +1,123 @@ +/* + * Copyright 2025 HM Revenue & Customs + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package uk.gov.hmrc.ngrraldfrontend.controllers + +import org.jsoup.Jsoup +import org.mockito.ArgumentMatchers.any +import org.mockito.Mockito.when +import play.api.http.Status.{BAD_REQUEST, OK, SEE_OTHER} +import play.api.test.FakeRequest +import play.api.test.Helpers.{await, contentAsString, defaultAwaitTimeout, redirectLocation, status} +import uk.gov.hmrc.auth.core.Nino +import uk.gov.hmrc.http.{HeaderNames, NotFoundException} +import uk.gov.hmrc.ngrraldfrontend.helpers.ControllerSpecSupport +import uk.gov.hmrc.ngrraldfrontend.models.AgreementType.NewAgreement +import uk.gov.hmrc.ngrraldfrontend.models.registration.CredId +import uk.gov.hmrc.ngrraldfrontend.models.{AuthenticatedUserRequest, NormalMode, UserAnswers} +import uk.gov.hmrc.ngrraldfrontend.pages.{DidYouGetMoneyFromLandlordPage, DoesYourRentIncludeParkingPage} +import uk.gov.hmrc.ngrraldfrontend.views.html.DidYouGetMoneyFromLandlordView + +import scala.concurrent.Future + +class DidYouGetMoneyFromLandlordControllerSpec extends ControllerSpecSupport { + val pageTitle = "Did you get any money from the landlord or previous tenant to take on the lease?" + val view: DidYouGetMoneyFromLandlordView = inject[DidYouGetMoneyFromLandlordView] + val controllerNoProperty: DidYouGetMoneyFromLandlordController = new DidYouGetMoneyFromLandlordController(view, fakeAuth, fakeData(None), mockSessionRepository, mockNavigator, mcc)(mockConfig, ec) + val controllerProperty: Option[UserAnswers] => DidYouGetMoneyFromLandlordController = answers => new DidYouGetMoneyFromLandlordController(view, fakeAuth, fakeDataProperty(Some(property),answers), mockSessionRepository, mockNavigator, mcc)(mockConfig, ec) + val didYouGetMoneyFromLandlordAnswers: Option[UserAnswers] = UserAnswers("id").set(DidYouGetMoneyFromLandlordPage, true).toOption + + + "DidYouGetMoneyFromLandlord Controller" must { + "method show" must { + "Return OK and the correct view" in { + val result = controllerProperty(None).show(NormalMode)(authenticatedFakeRequest) + status(result) mustBe OK + val content = contentAsString(result) + content must include(pageTitle) + } + "return OK and the correct view with prepopulated data" in { + val result = controllerProperty(didYouGetMoneyFromLandlordAnswers).show(NormalMode)(authenticatedFakeRequest) + status(result) mustBe OK + val content = contentAsString(result) + val document = Jsoup.parse(content) + document.select("input[type=radio][name=didYouGetMoneyFromLandlord-radio-value][value=true]").hasAttr("checked") mustBe true + document.select("input[type=radio][name=didYouGetMoneyFromLandlord-radio-value][value=false]").hasAttr("checked") mustBe false + } + "Return NotFoundException when property is not found in the mongo" in { + when(mockNGRConnector.getLinkedProperty(any[CredId])(any())).thenReturn(Future.successful(None)) + val exception = intercept[NotFoundException] { + await(controllerNoProperty.show(NormalMode)(authenticatedFakeRequest)) + } + exception.getMessage contains "Could not find answers in backend mongo" mustBe true + } + } + + "method submit" must { + "Return See_Other and the correct view after submitting yes" in { + when(mockSessionRepository.set(any())).thenReturn(Future.successful(true)) + val result = controllerProperty(None).submit(NormalMode)(AuthenticatedUserRequest(FakeRequest(routes.DidYouGetMoneyFromLandlordController.submit(NormalMode)) + .withFormUrlEncodedBody( + "didYouGetMoneyFromLandlord-radio-value" -> "true", + ) + .withHeaders(HeaderNames.authorisation -> "Bearer 1"), None, None, None, Some(property), credId = Some(credId.value), None, None, nino = Nino(true, Some("")))) + result.map(result => { + result.header.headers.get("Location") mustBe Some("/ngr-rald-frontend/landlord") //TODO this is currently going to the wrong page as the journey hasn't yet been completed + }) + status(result) mustBe SEE_OTHER + redirectLocation(result) mustBe Some(routes.LandlordController.show(NormalMode).url) + } + "Return See_Other and the correct view after submitting no" in { + when(mockSessionRepository.set(any())).thenReturn(Future.successful(true)) + val result = controllerProperty(None).submit(NormalMode)(AuthenticatedUserRequest(FakeRequest(routes.DidYouGetMoneyFromLandlordController.submit(NormalMode)) + .withFormUrlEncodedBody( + "didYouGetMoneyFromLandlord-radio-value" -> "false", + ) + .withHeaders(HeaderNames.authorisation -> "Bearer 1"), None, None, None, Some(property), credId = Some(credId.value), None, None, nino = Nino(true, Some("")))) + result.map(result => { + result.header.headers.get("Location") mustBe Some("/ngr-rald-frontend/landlord") //TODO this is currently going to the wrong page as the journey hasn't yet been completed + }) + status(result) mustBe SEE_OTHER + redirectLocation(result) mustBe Some(routes.LandlordController.show(NormalMode).url) + } + "Return Form with Errors when no radio selection is input" in { + val result = controllerProperty(None).submit(NormalMode)(AuthenticatedUserRequest(FakeRequest(routes.DidYouGetMoneyFromLandlordController.submit(NormalMode)) + .withFormUrlEncodedBody( + "didYouGetMoneyFromLandlord-radio-value" -> "", + ) + .withHeaders(HeaderNames.authorisation -> "Bearer 1"), None, None, None, Some(property), credId = Some(credId.value), None, None, nino = Nino(true, Some("")))) + result.map(result => { + result.header.headers.get("Location") mustBe Some("/ngr-rald-frontend/landlord") //TODO this is currently going to the wrong page as the journey hasn't yet been completed + }) + status(result) mustBe BAD_REQUEST + val content = contentAsString(result) + content must include(pageTitle) + content must include("Select yes if you got any money from the landlord or previous tenant to take on the lease") + } + + "Return Exception if no address is in the mongo" in { + when(mockNGRConnector.getLinkedProperty(any[CredId])(any())).thenReturn(Future.successful(None)) + val exception = intercept[NotFoundException] { + await(controllerNoProperty.submit(NormalMode)(AuthenticatedUserRequest(FakeRequest(routes.LandlordController.submit(NormalMode)) + .withFormUrlEncodedBody(("what-type-of-agreement-radio", "")) + .withHeaders(HeaderNames.authorisation -> "Bearer 1"), None, None, None, Some(property), credId = Some(credId.value), None, None, nino = Nino(true, Some(""))))) + } + exception.getMessage contains "Could not find answers in backend mongo" mustBe true + } + } + } +} + diff --git a/test/uk/gov/hmrc/ngrraldfrontend/models/forms/DidYouGetMoneyFromLandlordFormSpec.scala b/test/uk/gov/hmrc/ngrraldfrontend/models/forms/DidYouGetMoneyFromLandlordFormSpec.scala new file mode 100644 index 0000000..ed0f9db --- /dev/null +++ b/test/uk/gov/hmrc/ngrraldfrontend/models/forms/DidYouGetMoneyFromLandlordFormSpec.scala @@ -0,0 +1,81 @@ +/* + * Copyright 2025 HM Revenue & Customs + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package uk.gov.hmrc.ngrraldfrontend.models.forms + +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers +import play.api.data.FormError +import play.api.libs.json.Json + +class DidYouGetMoneyFromLandlordFormSpec extends AnyFlatSpec with Matchers { + + val validData = Map( + "didYouGetMoneyFromLandlord-radio-value" -> "true" + ) + + "didYouGetMoneyFromLandlordForm" should "bind valid data successfully" in { + val boundForm = DidYouGetMoneyFromLandlordForm.form.bind(validData) + + boundForm.errors shouldBe empty + boundForm.value shouldBe Some(DidYouGetMoneyFromLandlordForm("true")) + } + + it should "fail when didYouGetMoneyFromLandlord (radio) is missing" in { + val data = validData - "didYouGetMoneyFromLandlord-radio-value" + val boundForm = DidYouGetMoneyFromLandlordForm.form.bind(data) + + boundForm.errors shouldBe List(FormError("didYouGetMoneyFromLandlord-radio-value", List("didYouGetMoneyFromLandlord.empty.error"), List())) + } + + it should "fail to bind when didYouGetMoneyFromLandlord is empty" in { + val data = Map("didYouGetMoneyFromLandlord-radio-value" -> "") + val boundForm = DidYouGetMoneyFromLandlordForm.form.bind(data) + + boundForm.hasErrors shouldBe true + boundForm.errors should contain(FormError("didYouGetMoneyFromLandlord-radio-value", List("didYouGetMoneyFromLandlord.empty.error"))) + } + + it should "fail when no is selected" in { + val data = Map( + "didYouGetMoneyFromLandlord-radio-value" -> "false", + ) + + val boundForm = DidYouGetMoneyFromLandlordForm.form.bind(data) + + boundForm.errors shouldBe empty + boundForm.value shouldBe Some(DidYouGetMoneyFromLandlordForm("false")) + } + + "DoesYourRentIncludeParkingForm.format" should "serialize to JSON correctly" in { + val form = DidYouGetMoneyFromLandlordForm("Yes") + val json = Json.toJson(form) + + json shouldBe Json.obj( + "radio" -> "Yes", + ) + } + + it should "deserialize from JSON correctly" in { + val json = Json.obj( + "radio" -> "No", + ) + + val result = json.validate[DidYouGetMoneyFromLandlordForm] + result.isSuccess shouldBe true + result.get shouldBe DidYouGetMoneyFromLandlordForm("No") + } +} diff --git a/test/uk/gov/hmrc/ngrraldfrontend/models/forms/LandlordFormSpec.scala b/test/uk/gov/hmrc/ngrraldfrontend/models/forms/LandlordFormSpec.scala index f469f47..63d9852 100644 --- a/test/uk/gov/hmrc/ngrraldfrontend/models/forms/LandlordFormSpec.scala +++ b/test/uk/gov/hmrc/ngrraldfrontend/models/forms/LandlordFormSpec.scala @@ -46,7 +46,7 @@ class LandlordFormSpec extends AnyFlatSpec with Matchers { val boundForm = LandlordForm.form.bind(data) boundForm.errors shouldBe List(FormError("landlord-name-value", List("landlord.name.empty.error"), ArraySeq("landlord-name-value")), - FormError("landlord-relationship", List("landlord.relationship.empty.error"))) + FormError("landlord-relationship", List("landlord.relationship.emptyText.error"))) } it should "fail when landlord type (radio) is missing" in { @@ -65,7 +65,7 @@ class LandlordFormSpec extends AnyFlatSpec with Matchers { val boundForm = LandlordForm.form.bind(data) - boundForm.errors shouldBe List(FormError("landlord-relationship", List("landlord.relationship.empty.error"), List())) + boundForm.errors shouldBe List(FormError("landlord-relationship", List("landlord.relationship.emptyText.error"), List())) } it should "pass when 'Yes' is selected and description is provided" in { diff --git a/test/uk/gov/hmrc/ngrraldfrontend/views/DidYouGetMoneyFromLandlordViewSpec.scala b/test/uk/gov/hmrc/ngrraldfrontend/views/DidYouGetMoneyFromLandlordViewSpec.scala new file mode 100644 index 0000000..91f51ae --- /dev/null +++ b/test/uk/gov/hmrc/ngrraldfrontend/views/DidYouGetMoneyFromLandlordViewSpec.scala @@ -0,0 +1,90 @@ +/* + * Copyright 2025 HM Revenue & Customs + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package uk.gov.hmrc.ngrraldfrontend.views + +import org.jsoup.Jsoup +import org.jsoup.nodes.Document +import uk.gov.hmrc.govukfrontend.views.Aliases.Text +import uk.gov.hmrc.govukfrontend.views.viewmodels.fieldset.Legend +import uk.gov.hmrc.govukfrontend.views.viewmodels.radios.Radios +import uk.gov.hmrc.ngrraldfrontend.helpers.ViewBaseSpec +import uk.gov.hmrc.ngrraldfrontend.models.NormalMode +import uk.gov.hmrc.ngrraldfrontend.models.components.* +import uk.gov.hmrc.ngrraldfrontend.models.components.NGRRadio.buildRadios +import uk.gov.hmrc.ngrraldfrontend.models.forms.DidYouGetMoneyFromLandlordForm +import uk.gov.hmrc.ngrraldfrontend.views.html.DidYouGetMoneyFromLandlordView + +class DidYouGetMoneyFromLandlordViewSpec extends ViewBaseSpec { + lazy val view: DidYouGetMoneyFromLandlordView = inject[DidYouGetMoneyFromLandlordView] + + object Strings { + val heading = "Did you get any money from the landlord or previous tenant to take on the lease?" + val radio1 = "Yes" + val radio2 = "No" + val continue = "Continue" + } + + object Selectors { + val heading = "#main-content > div > div.govuk-grid-column-two-thirds > form > div > fieldset > legend > h1" + val hint = "#main-content > div > div.govuk-grid-column-two-thirds > form > p" + val radio1 = "#main-content > div > div.govuk-grid-column-two-thirds > form > div > fieldset > div > div:nth-child(1) > label" + val radio2 = "#main-content > div > div.govuk-grid-column-two-thirds > form > div > fieldset > div > div:nth-child(2) > label" + val continue = "#continue" + } + + val address = "5 Brixham Marina, Berry Head Road, Brixham, Devon, TQ5 9BW" + private val ngrRadio: NGRRadio = DidYouGetMoneyFromLandlordForm.moneyLandlordRadio + val form = DidYouGetMoneyFromLandlordForm.form.fillAndValidate(DidYouGetMoneyFromLandlordForm("Yes")) + val radio: Radios = buildRadios(form, ngrRadio) + + "DidYouGetMoneyFromLandlordView" must { + val didYouGetMoneyFromLandlordView = view(address, form, radio, NormalMode) + lazy implicit val document: Document = Jsoup.parse(didYouGetMoneyFromLandlordView.body) + val htmlApply = view.apply(address, form, radio, NormalMode).body + val htmlRender = view.render(address, form, radio, NormalMode, request, messages, mockConfig).body + lazy val htmlF = view.f(address, form, radio, NormalMode) + + "htmlF is not empty" in { + htmlF.toString() must not be empty + } + + "apply must be the same as render" in { + htmlApply mustBe htmlRender + } + + "render is not empty" in { + htmlRender must not be empty + } + + "show correct heading" in { + elementText(Selectors.heading) mustBe Strings.heading + } + + "show correct radio 1" in { + elementText(Selectors.radio1) mustBe Strings.radio1 + } + + "show correct radio 2" in { + elementText(Selectors.radio2) mustBe Strings.radio2 + } + + "show correct continue button" in { + elementText(Selectors.continue) mustBe Strings.continue + } + } +} +