public async Task <ActionResult> AboutPost(string providerCode, OrganisationViewModelForAbout model)
        {
            var ucasProviderEnrichmentGetModel = await _manageApi.GetProviderEnrichment(providerCode);

            var enrichmentModel = ucasProviderEnrichmentGetModel?.EnrichmentModel;

            var aboutAccreditingTrainingProviders = await MergeTrainingProviderViewModels(providerCode, enrichmentModel, model?.AboutTrainingProviders);

            model.AboutTrainingProviders = aboutAccreditingTrainingProviders;

            ValidateAboutModel(model);

            if (!ModelState.IsValid)
            {
                model.ProviderName = (await _manageApi.GetProviderSummaries()).FirstOrDefault(x => x.ProviderCode == providerCode.ToUpperInvariant())?.ProviderName;
                return(View("About", model));
            }

            if (enrichmentModel == null && model.IsEmpty())
            {
                // Draft state is "New" and no changes have been made - don't insert a draft
                return(RedirectToAction("Details", "Organisation", new { providerCode = providerCode }));
            }

            model.MergeIntoEnrichmentModel(ref enrichmentModel);

            var result = await SaveValidatedOrgansation(enrichmentModel, providerCode);

            return(result);
        }
 public void ValidateAboutModel(OrganisationViewModelForAbout model)
 {
     ModelState.Clear();
     TryValidateModel(model);
     ValidateAboutAccreditedTrainingProviders(model.AboutTrainingProviders);
 }
        public void AboutPost_ModelState_WordCount()
        {
            var providerCode   = "PROVIDERCODE";
            var exceed100Words = "";

            for (int i = 0; i < 101; i++)
            {
                exceed100Words += i + " ";
            }
            var providerName = "ProviderName";

            var viewModel = new OrganisationViewModelForAbout
            {
                AboutTrainingProviders = new List <TrainingProviderViewModel>()
                {
                    new TrainingProviderViewModel {
                        Description  = exceed100Words,
                        ProviderName = providerName,
                        ProviderCode = providerCode + 1
                    }
                }
            };

            var providerCourses = new List <Course>
            {
                new Course {
                },
                new Course {
                    AccreditingProvider = new GovUk.Education.ManageCourses.Domain.Models.Provider {
                        ProviderCode = providerCode.ToUpperInvariant()
                    }
                },
                new Course {
                    AccreditingProvider = new GovUk.Education.ManageCourses.Domain.Models.Provider {
                        ProviderCode = providerCode.ToLowerInvariant()
                    }
                },
                new Course {
                    AccreditingProvider = new GovUk.Education.ManageCourses.Domain.Models.Provider {
                        ProviderCode = providerCode
                    }
                },
                new Course {
                    AccreditingProvider = new GovUk.Education.ManageCourses.Domain.Models.Provider {
                        ProviderCode = providerCode + 1, ProviderName = providerName
                    }
                },
                new Course {
                    AccreditingProvider = new GovUk.Education.ManageCourses.Domain.Models.Provider {
                        ProviderCode = providerCode + 2
                    }
                },
            };

            var apiMock = new Mock <IManageApi>();

            apiMock.Setup(x => x.GetCoursesOfProvider(providerCode))
            .ReturnsAsync(providerCourses);


            var enrichmentModel = new ProviderEnrichmentModel {
                AccreditingProviderEnrichments = new List <AccreditingProviderEnrichment> {
                }
            };

            var ucasProviderEnrichmentGetModel = new UcasProviderEnrichmentGetModel()
            {
                EnrichmentModel = enrichmentModel
            };

            apiMock.Setup(x => x.GetProviderSummaries())
            .ReturnsAsync(new List <ProviderSummary> {
                new ProviderSummary()
            });

            apiMock.Setup(x => x.GetProviderEnrichment(providerCode))
            .ReturnsAsync(ucasProviderEnrichmentGetModel);

            var objectValidator = new Mock <IObjectModelValidator>();

            objectValidator.Setup(o => o.Validate(It.IsAny <ActionContext>(),
                                                  It.IsAny <ValidationStateDictionary>(),
                                                  It.IsAny <string>(),
                                                  It.IsAny <Object>()));

            var frontendUrlMock = new Mock <IFrontendUrlService>();

            var controller = new OrganisationController(apiMock.Object, frontendUrlMock.Object);

            controller.ObjectValidator = objectValidator.Object;

            controller.TempData = new Mock <ITempDataDictionary>().Object;

            Assert.IsFalse(controller.ModelState.Any());
            Assert.IsTrue(controller.ModelState.IsValid);
            var result = controller.AboutPost(providerCode, viewModel).Result;


            Assert.IsTrue(controller.ModelState.Any());
            Assert.AreEqual($"Reduce word count for {providerName}", controller.ModelState["AboutTrainingProviders_0__Description"].Errors.First().ErrorMessage);
            Assert.IsFalse(controller.ModelState.IsValid);

            var viewResult = result as ViewResult;

            var organisationViewModel = viewResult.ViewData.Model as OrganisationViewModelForAbout;

            Assert.IsNotNull(viewResult);
            Assert.AreEqual(exceed100Words, organisationViewModel.AboutTrainingProviders.First(x => x.ProviderCode == providerCode + 1).Description);
            Assert.AreEqual(providerName, organisationViewModel.AboutTrainingProviders.First(x => x.ProviderCode == providerCode + 1).ProviderName);
        }
        public async Task AboutPost_SaveOrganisation()
        {
            var providerCode = "PROVIDERCODE";
            var viewModel    = new OrganisationViewModelForAbout
            {
                AboutTrainingProviders = new List <TrainingProviderViewModel>()
            };

            var providerName = "ProviderName";

            var providerCourses = new List <Course>
            {
                new Course {
                },
                new Course {
                    AccreditingProvider = new GovUk.Education.ManageCourses.Domain.Models.Provider {
                        ProviderCode = providerCode.ToUpperInvariant()
                    }
                },
                new Course {
                    AccreditingProvider = new GovUk.Education.ManageCourses.Domain.Models.Provider {
                        ProviderCode = providerCode.ToLowerInvariant()
                    }
                },
                new Course {
                    AccreditingProvider = new GovUk.Education.ManageCourses.Domain.Models.Provider {
                        ProviderCode = providerCode
                    }
                },
                new Course {
                    AccreditingProvider = new GovUk.Education.ManageCourses.Domain.Models.Provider {
                        ProviderCode = providerCode + 1, ProviderName = providerName
                    }
                },
                new Course {
                    AccreditingProvider = new GovUk.Education.ManageCourses.Domain.Models.Provider {
                        ProviderCode = providerCode + 2
                    }
                },
            };

            var apiMock = new Mock <IManageApi>();

            apiMock.Setup(x => x.GetCoursesOfProvider(providerCode))
            .ReturnsAsync(providerCourses);

            var enrichmentModel = new ProviderEnrichmentModel {
                AccreditingProviderEnrichments = new List <AccreditingProviderEnrichment> {
                }
            };

            var ucasProviderEnrichmentGetModel = new UcasProviderEnrichmentGetModel()
            {
                EnrichmentModel = enrichmentModel
            };

            apiMock.Setup(x => x.GetProviderEnrichment(providerCode))
            .ReturnsAsync(ucasProviderEnrichmentGetModel);

            var objectValidator = new Mock <IObjectModelValidator>();

            objectValidator.Setup(o => o.Validate(It.IsAny <ActionContext>(),
                                                  It.IsAny <ValidationStateDictionary>(),
                                                  It.IsAny <string>(),
                                                  It.IsAny <Object>()));

            var frontendUrlMock = new Mock <IFrontendUrlService>();

            var controller = new OrganisationController(apiMock.Object, frontendUrlMock.Object);

            controller.ObjectValidator = objectValidator.Object;

            controller.TempData = new Mock <ITempDataDictionary>().Object;

            var result = await controller.AboutPost(providerCode, viewModel);

            apiMock.Verify(x => x.SaveProviderEnrichment(providerCode, It.IsAny <UcasProviderEnrichmentPostModel>()), Times.Once);

            var actionResult = result as RedirectToActionResult;

            Assert.IsNotNull(actionResult);
            Assert.AreEqual("Details", actionResult.ActionName);
            Assert.AreEqual("Organisation", actionResult.ControllerName);
            Assert.AreEqual(providerCode, actionResult.RouteValues[providerCode]);
        }