Ejemplo n.º 1
0
        public void ThenWillReturnSameIfBothAreUnset()
        {
            var source = new TrainingDateModel();
            var target = new TrainingDateModel();

            Assert.IsTrue(source.Equals(target));
        }
        public ReviewViewModel(ReservationsRouteModel routeModel, TrainingDateModel trainingDate, string courseDescription, string accountLegalEntityName, string accountLegalEntityPublicHashedId)
        {
            ConfirmRouteName = IsEmployerRoute(routeModel) ?
                               RouteNames.EmployerPostReview :
                               RouteNames.ProviderPostReview;

            ChangeCourseRouteName = IsEmployerRoute(routeModel) ?
                                    RouteNames.EmployerSelectCourse :
                                    RouteNames.ProviderApprenticeshipTraining;

            ChangeStartDateRouteName = IsEmployerRoute(routeModel) ?
                                       RouteNames.EmployerApprenticeshipTraining :
                                       RouteNames.ProviderApprenticeshipTraining;

            ViewName = IsEmployerRoute(routeModel) ?
                       ViewNames.EmployerReview :
                       ViewNames.ProviderReview;

            BackLink = IsEmployerRoute(routeModel) ?
                       RouteNames.EmployerApprenticeshipTraining : RouteNames.ProviderApprenticeshipTraining;

            RouteModel                       = routeModel;
            TrainingDate                     = trainingDate;
            CourseDescription                = courseDescription;
            AccountLegalEntityName           = accountLegalEntityName;
            AccountLegalEntityPublicHashedId = accountLegalEntityPublicHashedId;
        }
        [Test, AutoData] //note cannot use moqautodata to construct controller here due to modelmetadata usage.
        public async Task And_Course_Validation_Error_Then_Returns_Validation_Error_Details(
            ReservationsRouteModel routeModel,
            TrainingDateModel trainingDateModel,
            ApprenticeshipTrainingFormModel formModel)
        {
            formModel.StartDate        = JsonConvert.SerializeObject(trainingDateModel);
            formModel.SelectedCourseId = _course.Id;

            _mediator.Setup(mediator =>
                            mediator.Send(It.IsAny <CacheReservationCourseCommand>(), It.IsAny <CancellationToken>()))
            .ThrowsAsync(new ValidationException(
                             new ValidationResult("Failed",
                                                  new List <string> {
                "CourseId|The CourseId field is invalid."
            }), null, null));

            var result = await _controller.PostApprenticeshipTraining(routeModel, formModel);

            Assert.IsNotNull(result);
            var actualViewResult = result as ViewResult;

            Assert.IsNotNull(actualViewResult);
            Assert.IsFalse(actualViewResult.ViewData.ModelState.IsValid);
            Assert.IsTrue(actualViewResult.ViewData.ModelState.ContainsKey("CourseId"));
        }
 public TrainingDateViewModel(TrainingDateModel model, bool isSelected = false)
 {
     Id = $"{model.StartDate:yyyy-MM}";
     SerializedModel = JsonConvert.SerializeObject(model);
     StartDate       = model.StartDate;
     Checked         = isSelected ? "checked" : null;
 }
Ejemplo n.º 5
0
        private async Task <ApprenticeshipTrainingViewModel> BuildApprenticeshipTrainingViewModel(
            bool isProvider,
            string accountLegalEntityPublicHashedId,
            string courseId = null,
            TrainingDateModel selectedTrainingDate = null,
            bool?routeModelFromReview = false,
            string cohortRef          = "",
            uint?ukPrn       = null,
            string accountId = "")

        {
            var accountLegalEntityId = _encodingService.Decode(
                accountLegalEntityPublicHashedId,
                EncodingType.PublicAccountLegalEntityId);
            var dates = await _trainingDateService.GetTrainingDates(accountLegalEntityId);

            var coursesResult = await _mediator.Send(new GetCoursesQuery());

            return(new ApprenticeshipTrainingViewModel
            {
                RouteName = isProvider ? RouteNames.ProviderCreateApprenticeshipTraining : RouteNames.EmployerCreateApprenticeshipTraining,
                PossibleStartDates = dates.Select(startDateModel => new TrainingDateViewModel(startDateModel, startDateModel.Equals(selectedTrainingDate))).OrderBy(model => model.StartDate),
                Courses = coursesResult.Courses?.Select(course => new CourseViewModel(course, courseId)),
                CourseId = courseId,
                AccountLegalEntityPublicHashedId = accountLegalEntityPublicHashedId,
                IsProvider = isProvider,
                CohortRef = cohortRef,
                FromReview = routeModelFromReview,
                BackLink = isProvider ?
                           GetProviderBackLinkForApprenticeshipTrainingView(routeModelFromReview, cohortRef, ukPrn, accountId)
                    : routeModelFromReview.HasValue && routeModelFromReview.Value ? RouteNames.EmployerReview : RouteNames.EmployerSelectCourse
            });
        }
        public void Then_The_Model_Is_Constructed_With_Correct_Route_Names_Using_Other_Constructor(uint?ukPrn)
        {
            //Arrange
            var startDate = new TrainingDateModel {
                StartDate = DateTime.Now
            };

            var reservationsRouteModel = new ReservationsRouteModel
            {
                AccountLegalEntityPublicHashedId = AccountLegalEntityPublicHashedId,
                UkPrn             = ukPrn,
                Id                = new Guid(),
                EmployerAccountId = "123FDS",
                FromReview        = true
            };

            var postReviewViewModel = new PostReviewViewModel
            {
                AccountLegalEntityName           = AccountLegalEntityName,
                AccountLegalEntityPublicHashedId = AccountLegalEntityPublicHashedId,
                CourseDescription = CourseDescription,
                TrainingDate      = startDate,
                Reserve           = ExpectedReserve
            };

            //Act
            var actual = new ReviewViewModel(reservationsRouteModel, postReviewViewModel);

            //Assert
            AssertAllProperties(actual, ukPrn, startDate);
            Assert.AreEqual(ExpectedReserve, actual.Reserve);
        }
Ejemplo n.º 7
0
        public void ThenWillReturnDifferentIfTargetIsNotSameType()
        {
            var source = new TrainingDateModel {
                StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(1)
            };

            Assert.IsFalse(source.Equals(123));
        }
Ejemplo n.º 8
0
        public void BuildTrainingDateModel(string startMonth)
        {
            var startDate = DateTime.Parse($"{DateTime.UtcNow.Year} {startMonth} 01");

            TrainingDate = new TrainingDateModel
            {
                StartDate = startDate,
                EndDate   = startDate.AddMonths(2)
            };
        }
Ejemplo n.º 9
0
 public ReservationViewModel(Reservation reservation)
 {
     Id           = reservation?.Id ?? Guid.Empty;
     TrainingDate = new TrainingDateModel
     {
         StartDate = reservation?.StartDate.Value ?? default(DateTime),
         EndDate   = reservation?.ExpiryDate.Value ?? default(DateTime)
     };
     CourseDescription = reservation?.Course?.CourseDescription ?? "Unknown";
 }
 public AvailableReservationViewModel(Reservation reservation)
 {
     ReservationId           = reservation.Id;
     CourseDescription       = reservation.Course.CourseDescription;
     TrainingDateDescription = new TrainingDateModel
     {
         StartDate = reservation.StartDate,
         EndDate   = reservation.ExpiryDate
     }.GetGDSDateString();
     CreatedDateDescription = reservation.CreatedDate.GetGDSLongDateString();
 }
Ejemplo n.º 11
0
        public void ThenWillReturnSameIfBothDatesAreSetAndEqual()
        {
            var source = new TrainingDateModel {
                StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(1)
            };
            var target = new TrainingDateModel {
                StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(1)
            };

            Assert.IsTrue(source.Equals(target));
        }
Ejemplo n.º 12
0
        public void ThenWillReturnDifferentIfEndDatesAreSetAndNotEqual()
        {
            var source = new TrainingDateModel {
                StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(1)
            };
            var target = new TrainingDateModel {
                StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(2)
            };

            Assert.IsFalse(source.Equals(target));
        }
Ejemplo n.º 13
0
        public void ThenWillReturnSameIfStartDateSetAndEqual()
        {
            var source = new TrainingDateModel {
                StartDate = DateTime.Now
            };
            var target = new TrainingDateModel {
                StartDate = DateTime.Now
            };

            Assert.IsTrue(source.Equals(target));
        }
Ejemplo n.º 14
0
 public DeleteViewModel(GetReservationResult queryResult)
 {
     Id = queryResult.ReservationId;
     StartDateDescription = new TrainingDateModel
     {
         StartDate = queryResult.StartDate,
         EndDate   = queryResult.ExpiryDate
     }.GetGDSDateString();
     AccountLegalEntityName = queryResult.AccountLegalEntityName;
     CourseDescription      = queryResult.Course?.CourseDescription ??
                              new Course(null, null, 0).CourseDescription;
 }
Ejemplo n.º 15
0
        public void Then_Sets_TrainingDateDescription(
            Reservation reservation)
        {
            var expectedTrainingDateDescription = new TrainingDateModel
            {
                StartDate = reservation.StartDate,
                EndDate   = reservation.ExpiryDate
            }.GetGDSDateString();

            var model = new AvailableReservationViewModel(reservation);

            model.TrainingDateDescription.Should().Be(expectedTrainingDateDescription);
        }
        public async Task Then_Adds_Guid_To_RouteModel(
            ReservationsRouteModel routeModel,
            TrainingDateModel trainingDateModel,
            ApprenticeshipTrainingFormModel formModel)
        {
            formModel.StartDate        = JsonConvert.SerializeObject(trainingDateModel);
            formModel.SelectedCourseId = _course.Id;

            var result = await _controller.PostApprenticeshipTraining(routeModel, formModel) as RedirectToRouteResult;

            result.RouteValues.Should().ContainKey("Id")
            .WhichValue.Should().Be(routeModel.Id);
        }
        public async Task And_Has_Ukprn_Then_Redirects_To_Provider_Route(
            ReservationsRouteModel routeModel,
            TrainingDateModel trainingDateModel,
            ApprenticeshipTrainingFormModel formModel)
        {
            formModel.StartDate        = JsonConvert.SerializeObject(trainingDateModel);
            formModel.SelectedCourseId = null;

            var result = await _controller.PostApprenticeshipTraining(routeModel, formModel) as RedirectToRouteResult;

            result.Should().NotBeNull($"result was not a {typeof(RedirectToRouteResult)}");
            result.RouteName.Should().Be(RouteNames.ProviderReview);
        }
Ejemplo n.º 18
0
        public void Then_Sets_StartDateDescription(
            GetReservationResult getReservationResult)
        {
            var expectedDateDescription = new TrainingDateModel
            {
                StartDate = getReservationResult.StartDate,
                EndDate   = getReservationResult.ExpiryDate
            }.GetGDSDateString();

            var viewModel = new DeleteViewModel(getReservationResult);

            viewModel.StartDateDescription.Should().Be(expectedDateDescription);
        }
        public async Task Then_The_Model_Is_Validated_And_Confirmation_Returned(
            ApprenticeshipTrainingFormModel model,
            TrainingDateModel trainingDateModel,
            ReservationsRouteModel routeModel)
        {
            model.StartDate = JsonConvert.SerializeObject(trainingDateModel);
            _controller.ModelState.AddModelError("StartDate", "StartDate");

            var actual = await _controller.PostApprenticeshipTraining(routeModel, model);

            var actualModel = actual as ViewResult;

            Assert.IsNotNull(actualModel);
            Assert.AreEqual("ApprenticeshipTraining", actualModel.ViewName);
        }
        public async Task And_CachedReservationNotFoundException_And_Has_Ukprn_Then_Redirect_To_ProviderIndex(
            ReservationsRouteModel routeModel,
            TrainingDateModel trainingDateModel,
            ApprenticeshipTrainingFormModel formModel,
            CachedReservationNotFoundException notFoundException)
        {
            formModel.StartDate        = JsonConvert.SerializeObject(trainingDateModel);
            formModel.SelectedCourseId = _course.Id;

            _mediator
            .Setup(mediator => mediator.Send(It.IsAny <GetCachedReservationQuery>(), It.IsAny <CancellationToken>()))
            .ThrowsAsync(notFoundException);

            var actual = await _controller.PostApprenticeshipTraining(routeModel, formModel) as RedirectToRouteResult;

            actual.Should().NotBeNull();
            actual.RouteName.Should().Be(RouteNames.ProviderIndex);
        }
        public ReservationViewModel(Reservation reservation, string apprenticeUrl, uint?loggedInProviderId) : base(apprenticeUrl)
        {
            Id           = reservation.Id;
            TrainingDate = new TrainingDateModel
            {
                StartDate = reservation.StartDate,
                EndDate   = reservation.ExpiryDate
            };

            Status          = (ReservationStatusViewModel)reservation.Status;
            IsExpired       = reservation.IsExpired;
            CourseName      = reservation.Course != null ? reservation.Course.CourseDescription : "Unknown";
            LegalEntityName = reservation.AccountLegalEntityName;
            CanProviderDeleteReservation = !loggedInProviderId.HasValue || loggedInProviderId == reservation.ProviderId;
            DeleteRouteName = (ReservationStatusViewModel)reservation.Status == ReservationStatusViewModel.Pending && !reservation.IsExpired
                ? (loggedInProviderId == null ? RouteNames.EmployerDelete : RouteNames.ProviderDelete)
                : string.Empty;
        }
        public async Task And_No_Ukprn_Then_Caches_StartDate_Only(
            ReservationsRouteModel routeModel,
            TrainingDateModel trainingDateModel,
            ApprenticeshipTrainingFormModel formModel)
        {
            routeModel.UkPrn           = null;
            formModel.StartDate        = JsonConvert.SerializeObject(trainingDateModel);
            formModel.SelectedCourseId = _course.Id;

            await _controller.PostApprenticeshipTraining(routeModel, formModel);

            _mediator.Verify(mediator => mediator.Send(
                                 It.Is <CacheReservationCourseCommand>(c =>
                                                                       c.SelectedCourseId.Equals(_course.Id)),
                                 It.IsAny <CancellationToken>()), Times.Never);

            _mediator.Verify(mediator => mediator.Send(
                                 It.Is <CacheReservationStartDateCommand>(c =>
                                                                          c.TrainingDate.Equals(trainingDateModel)),
                                 It.IsAny <CancellationToken>()));
        }
        public async Task And_No_Course_Then_Caches_Draft_Reservation(
            ReservationsRouteModel routeModel,
            TrainingDateModel trainingDateModel,
            ApprenticeshipTrainingFormModel formModel)
        {
            formModel.StartDate        = JsonConvert.SerializeObject(trainingDateModel);
            formModel.SelectedCourseId = null;

            await _controller.PostApprenticeshipTraining(routeModel, formModel);

            _mediator.Verify(mediator => mediator.Send(
                                 It.Is <CacheReservationCourseCommand>(c =>
                                                                       c.SelectedCourseId == null),
                                 It.IsAny <CancellationToken>()));

            _mediator.Verify(mediator => mediator.Send(
                                 It.Is <CacheReservationStartDateCommand>(c =>
                                                                          c.TrainingDate.Equals(trainingDateModel) &&
                                                                          c.UkPrn.Equals(routeModel.UkPrn)),
                                 It.IsAny <CancellationToken>()));
        }
        public void Then_The_Model_Is_Constructed_With_Correct_Route_Names(uint?ukPrn)
        {
            //Arrange
            var startDate = new TrainingDateModel {
                StartDate = DateTime.Now
            };

            var reservationsRouteModel = new ReservationsRouteModel
            {
                AccountLegalEntityPublicHashedId = AccountLegalEntityPublicHashedId,
                UkPrn             = ukPrn,
                Id                = new Guid(),
                EmployerAccountId = "123FDS",
                FromReview        = true
            };

            //Act
            var actual = new ReviewViewModel(reservationsRouteModel, startDate, CourseDescription, AccountLegalEntityName, AccountLegalEntityPublicHashedId);

            //Assert
            AssertAllProperties(actual, ukPrn, startDate);
        }
        public async Task Then_If_There_Is_An_Error_The_Model_Is_Correctly_Built_And_Returned_To_The_View_And_The_Back_Link_Is_Correct(
            string cohortDetailsUrl,
            ReservationsRouteModel routeModel,
            TrainingDateModel trainingDateModel,
            ApprenticeshipTrainingFormModel formModel)
        {
            formModel.FromReview = true;
            formModel.CohortRef  = string.Empty;
            formModel.StartDate  = JsonConvert.SerializeObject(trainingDateModel);
            _controller.ModelState.AddModelError("StartDate", "StartDate");

            var result = await _controller.PostApprenticeshipTraining(routeModel, formModel);

            var actualViewResult = result as ViewResult;

            Assert.IsNotNull(actualViewResult);
            Assert.IsFalse(actualViewResult.ViewData.ModelState.IsValid);
            var actualModel = actualViewResult.Model as ApprenticeshipTrainingViewModel;

            Assert.IsNotNull(actualModel);
            Assert.AreEqual(RouteNames.ProviderReview, actualModel.BackLink);
        }
 private void AssertAllProperties(ReviewViewModel actual, uint?ukPrn, TrainingDateModel startDate)
 {
     Assert.AreEqual(AccountLegalEntityName, actual.AccountLegalEntityName);
     Assert.AreEqual(AccountLegalEntityPublicHashedId, actual.AccountLegalEntityPublicHashedId);
     Assert.AreEqual(CourseDescription, actual.CourseDescription);
     if (ukPrn == null)
     {
         Assert.AreEqual(ViewNames.EmployerReview, actual.ViewName);
         Assert.AreEqual(RouteNames.EmployerPostReview, actual.ConfirmRouteName);
         Assert.AreEqual(RouteNames.EmployerSelectCourse, actual.ChangeCourseRouteName);
         Assert.AreEqual(RouteNames.EmployerApprenticeshipTraining, actual.ChangeStartDateRouteName);
         Assert.AreEqual(RouteNames.EmployerApprenticeshipTraining, actual.BackLink);
     }
     else
     {
         Assert.AreEqual(ViewNames.ProviderReview, actual.ViewName);
         Assert.AreEqual(RouteNames.ProviderPostReview, actual.ConfirmRouteName);
         Assert.AreEqual(RouteNames.ProviderApprenticeshipTraining, actual.ChangeCourseRouteName);
         Assert.AreEqual(RouteNames.ProviderApprenticeshipTraining, actual.ChangeStartDateRouteName);
         Assert.AreEqual(RouteNames.ProviderApprenticeshipTraining, actual.BackLink);
     }
     Assert.AreEqual(startDate, actual.TrainingDate);
 }
        public async Task Then_If_There_Is_An_Error_The_Model_Is_Correctly_Built_And_Returned_To_The_View(
            string cohortDetailsUrl,
            ReservationsRouteModel routeModel,
            TrainingDateModel trainingDateModel,
            ApprenticeshipTrainingFormModel formModel)
        {
            formModel.FromReview = false;
            _urlHelper.Setup(helper => helper.GenerateCohortDetailsUrl(routeModel.UkPrn, routeModel.EmployerAccountId, formModel.CohortRef, false))
            .Returns(cohortDetailsUrl);
            formModel.StartDate = JsonConvert.SerializeObject(trainingDateModel);
            _controller.ModelState.AddModelError("StartDate", "StartDate");

            var result = await _controller.PostApprenticeshipTraining(routeModel, formModel);

            var actualViewResult = result as ViewResult;

            Assert.IsNotNull(actualViewResult);
            Assert.IsFalse(actualViewResult.ViewData.ModelState.IsValid);
            var actualModel = actualViewResult.Model as ApprenticeshipTrainingViewModel;

            Assert.IsNotNull(actualModel);
            Assert.AreEqual(cohortDetailsUrl, actualModel.BackLink);
        }
Ejemplo n.º 28
0
        public async Task <IActionResult> PostApprenticeshipTraining(ReservationsRouteModel routeModel, ApprenticeshipTrainingFormModel formModel)
        {
            var isProvider = routeModel.UkPrn != null;
            TrainingDateModel trainingDateModel = null;

            try
            {
                if (!string.IsNullOrWhiteSpace(formModel.StartDate))
                {
                    trainingDateModel = JsonConvert.DeserializeObject <TrainingDateModel>(formModel.StartDate);
                }

                if (!ModelState.IsValid)
                {
                    var model = await BuildApprenticeshipTrainingViewModel(
                        isProvider,
                        formModel.AccountLegalEntityPublicHashedId,
                        formModel.SelectedCourseId,
                        trainingDateModel,
                        formModel.FromReview,
                        formModel.CohortRef,
                        routeModel.UkPrn,
                        routeModel.EmployerAccountId);

                    return(View("ApprenticeshipTraining", model));
                }

                var cachedReservation = await _mediator.Send(new GetCachedReservationQuery { Id = routeModel.Id.GetValueOrDefault() });

                if (isProvider)
                {
                    var courseCommand = new CacheReservationCourseCommand
                    {
                        Id = cachedReservation.Id,
                        SelectedCourseId = formModel.SelectedCourseId,
                        UkPrn            = routeModel.UkPrn
                    };

                    await _mediator.Send(courseCommand);
                }

                var startDateCommand = new CacheReservationStartDateCommand
                {
                    Id           = cachedReservation.Id,
                    TrainingDate = trainingDateModel,
                    UkPrn        = routeModel.UkPrn
                };

                await _mediator.Send(startDateCommand);
            }
            catch (ValidationException e)
            {
                foreach (var member in e.ValidationResult.MemberNames)
                {
                    ModelState.AddModelError(member.Split('|')[0], member.Split('|')[1]);
                }

                var model = await BuildApprenticeshipTrainingViewModel(
                    isProvider,
                    formModel.AccountLegalEntityPublicHashedId,
                    formModel.SelectedCourseId,
                    trainingDateModel,
                    formModel.FromReview,
                    formModel.CohortRef,
                    routeModel.UkPrn,
                    routeModel.EmployerAccountId);

                return(View("ApprenticeshipTraining", model));
            }
            catch (CachedReservationNotFoundException ex)
            {
                _logger.LogWarning(ex, "Expected a cached reservation but did not find one.");
                return(RedirectToRoute(routeModel.UkPrn.HasValue ? RouteNames.ProviderIndex : RouteNames.EmployerIndex, routeModel));
            }

            var reviewRouteName = isProvider ?
                                  RouteNames.ProviderReview :
                                  RouteNames.EmployerReview;

            return(RedirectToRoute(reviewRouteName, routeModel));
        }
Ejemplo n.º 29
0
 public static string GetGDSDateString(this TrainingDateModel model)
 {
     return(model.EndDate == default(DateTime) ?
            $"{model.StartDate.GetGDSLongDateString()}" :
            $"{model.StartDate.GetGDSShortDateString()} to {model.EndDate.GetGDSShortDateString()}");
 }
Ejemplo n.º 30
0
        public void And_Null_StartDate_Then_Checked_Is_Null(TrainingDateModel model)
        {
            var viewModel = new TrainingDateViewModel(model);

            viewModel.Checked.Should().BeNull();
        }