Example #1
0
        public void IndexTests(CourseFiltersViewModel searchFilter, string resetFilterText, string pageTitle, string courseSearchResultsPage, string courseDetailsPage, CourseSearchOrderBy orderBy, CourseSearchResult courseSearchResponse)
        {
            courseSearchResponse = courseSearchResponse ?? new CourseSearchResult();
            searchFilter         = searchFilter ?? new CourseFiltersViewModel();

            // setupFakes
            A.CallTo(() => fakeCourseSearchService.SearchCoursesAsync(A <CourseSearchProperties> ._)).Returns(courseSearchResponse);

            var searchProperties = new CourseSearchProperties
            {
                OrderedBy = orderBy
            };

            // Assign
            var controller = new CourseSearchResultsController(fakeApplicationLogger, fakeCourseSearchService, asyncHelper, fakeCourseSearchViewModelService, fakeWebAppContext, mapperCfg)
            {
                ResetFilterText            = resetFilterText,
                PageTitle                  = pageTitle,
                CourseSearchResultsPage    = courseSearchResultsPage,
                CourseDetailsPage          = courseDetailsPage,
                RecordsPerPage             = 40,
                AdvancedLoanProviderLabel  = nameof(CourseSearchResultsController.AdvancedLoanProviderLabel),
                LocationLabel              = nameof(CourseSearchResultsController.LocationLabel),
                ProviderLabel              = nameof(CourseSearchResultsController.ProviderLabel),
                StartDateLabel             = nameof(CourseSearchResultsController.StartDateLabel),
                OrderByText                = nameof(CourseSearchResultsController.OrderByText),
                StartDateOrderByText       = nameof(CourseSearchResultsController.StartDateOrderByText),
                DistanceOrderByText        = nameof(CourseSearchResultsController.DistanceOrderByText),
                RelevanceOrderByText       = nameof(CourseSearchResultsController.RelevanceOrderByText),
                NoTrainingCoursesFoundText = nameof(CourseSearchResultsController.NoTrainingCoursesFoundText),
                ApplyFiltersText           = nameof(CourseSearchResultsController.ApplyFiltersText),
                CourseTypeSectionText      = nameof(CourseSearchResultsController.CourseTypeSectionText),
                CourseHoursSectionText     = nameof(CourseSearchResultsController.CourseHoursSectionText),
                StartDateSectionText       = nameof(CourseSearchResultsController.StartDateSectionText),
                SearchForCourseNameText    = nameof(CourseSearchResultsController.SearchForCourseNameText),
                WithinText                 = nameof(CourseSearchResultsController.WithinText),
                Only1619CoursesText        = nameof(CourseSearchResultsController.Only1619CoursesText),
                StartDateExampleText       = nameof(CourseSearchResultsController.StartDateExampleText)
            };

            // Act
            var controllerResult = controller.WithCallTo(contrl =>
                                                         contrl.Index(
                                                             searchFilter,
                                                             searchProperties));

            // Assert
            controllerResult.ShouldRenderView("SearchResults").WithModel <CourseSearchResultsViewModel>(
                vm =>
            {
                vm.PageTitle.Should().BeEquivalentTo(controller.PageTitle);
                vm.ResetFiltersText.Should().BeEquivalentTo(controller.ResetFilterText);
                vm.ResetFilterUrl.OriginalString.Should().NotBeEmpty();
            });

            if (!string.IsNullOrWhiteSpace(searchFilter.SearchTerm))
            {
                A.CallTo(() => fakeCourseSearchService.SearchCoursesAsync(A <CourseSearchProperties> ._)).MustHaveHappened();
                if (courseSearchResponse.Courses.Any())
                {
                    A.CallTo(() => fakeCourseSearchViewModelService.GetOrderByLinks(A <string> ._, A <CourseSearchOrderBy> ._)).MustHaveHappened();
                    A.CallTo(() => fakeCourseSearchViewModelService.SetupViewModelPaging(A <CourseSearchResultsViewModel> ._, A <CourseSearchResult> ._, A <string> ._, A <int> ._)).MustHaveHappened();
                }
            }
            else
            {
                A.CallTo(() => fakeCourseSearchService.SearchCoursesAsync(A <CourseSearchProperties> ._)).MustNotHaveHappened();
                A.CallTo(() => fakeCourseSearchViewModelService.GetOrderByLinks(A <string> ._, A <CourseSearchOrderBy> ._)).MustNotHaveHappened();
                A.CallTo(() => fakeCourseSearchViewModelService.SetupViewModelPaging(A <CourseSearchResultsViewModel> ._, A <CourseSearchResult> ._, A <string> ._, A <int> ._)).MustNotHaveHappened();
            }
        }
        public ActionResult Index(string searchTerm, string attendance, string studyMode, string attendancePattern, string location, string sortBy, string startDate, string provider, bool only1619Courses = false, int page = 1)
        {
            var viewModel = new CourseSearchResultsViewModel {
                SearchTerm = searchTerm
            };

            var cleanCourseName =
                StringManipulationExtension.ReplaceSpecialCharacters(searchTerm, InvalidCharactersRegexPattern);

            if (!string.IsNullOrEmpty(cleanCourseName))
            {
                var courseSearchProperties = new CourseSearchProperties
                {
                    Page      = page,
                    Count     = RecordsPerPage,
                    OrderedBy = GetSortBy(sortBy),
                    Filters   = new CourseSearchFilters
                    {
                        Attendance        = attendance?.Split(','),
                        StudyMode         = studyMode?.Split(','),
                        Only1619Courses   = only1619Courses,
                        AttendancePattern = attendancePattern?.Split(','),
                        Location          = StringManipulationExtension.ReplaceSpecialCharacters(location, InvalidCharactersRegexPattern),
                        Provider          =
                            StringManipulationExtension.ReplaceSpecialCharacters(provider, InvalidCharactersRegexPattern),
                        StartDate = startDate
                    }
                };

                var response = asyncHelper.Synchronise(() => courseSearchService.SearchCoursesAsync(cleanCourseName, courseSearchProperties));
                if (response.Courses.Any())
                {
                    foreach (var course in response.Courses)
                    {
                        course.CourseUrl = $"{CourseDetailsPage}?courseid={course.CourseId}";
                        viewModel.Courses.Add(new CourseListingViewModel
                        {
                            Course = course,
                            AdvancedLoanProviderLabel = AdvancedLoanProviderLabel,
                            LocationLabel             = LocationLabel,
                            ProviderLabel             = ProviderLabel,
                            StartDateLabel            = StartDateLabel
                        });
                    }

                    var pathQuery = Request?.Url?.PathAndQuery;
                    if (!string.IsNullOrWhiteSpace(pathQuery) && pathQuery.IndexOf("&page=", StringComparison.InvariantCultureIgnoreCase) > 0)
                    {
                        pathQuery = pathQuery.Substring(0, pathQuery.IndexOf("&page=", StringComparison.InvariantCultureIgnoreCase));
                    }

                    courseSearchViewModelService.SetupPaging(viewModel, response, pathQuery, RecordsPerPage, CourseSearchResultsPage);
                    SetupSearchLinks(searchTerm, viewModel, pathQuery, response.ResultProperties.OrderedBy);
                }

                // SetupFilterDisplayData(attendance, studymode, qualificationLevel, distance, dfe1619Funded, pattern, location, startDate, provider, viewModel);
            }

            viewModel.NoTrainingCoursesFoundText =
                string.IsNullOrWhiteSpace(searchTerm) ? string.Empty : NoTrainingCoursesFoundText;

            SetupWidgetDefaults(viewModel);
            return(View("SearchResults", viewModel));
        }
        public ActionResult Index(CourseFiltersViewModel filtersInput, CourseSearchProperties inputSearchProperties)
        {
            var cleanedSearchTerm   = filtersInput.SearchTerm.ReplaceSpecialCharacters(Constants.CourseSearchInvalidCharactersRegexPattern);
            var courseSearchResults = new CourseSearchResultsViewModel
            {
                CourseFiltersModel         = filtersInput,
                ResetFilterUrl             = new Uri($"{CourseSearchResultsPage}?{nameof(CourseSearchFilters.SearchTerm)}={filtersInput.SearchTerm}", UriKind.RelativeOrAbsolute),
                NoTrainingCoursesFoundText = NoTrainingCoursesFoundText.Replace(SearchTermTokenToReplace, $"'{HttpUtility.HtmlEncode(filtersInput.SearchTerm)}'"),
            };

            CourseSearchOrderBy originalCourseSearchOrderBy = inputSearchProperties.OrderedBy;

            if (!filtersInput.IsDistanceLocation)
            {
                filtersInput.Town = filtersInput.Location;

                //Make CD API behave the same way as tribal if there is no postcode, the order by relevence if distance is selected.
                if (inputSearchProperties.OrderedBy == CourseSearchOrderBy.Distance)
                {
                    inputSearchProperties.OrderedBy = CourseSearchOrderBy.Relevance;
                }
            }
            else
            {
                filtersInput.Postcode = filtersInput.Location;
            }

            if (!string.IsNullOrEmpty(cleanedSearchTerm))
            {
                //create a new object if invoked from landing page
                var courseSearchProperties = inputSearchProperties ?? new CourseSearchProperties();
                courseSearchProperties.Count   = RecordsPerPage;
                courseSearchProperties.Filters = mapper.Map <CourseSearchFilters>(filtersInput);
                courseSearchProperties.Filters.DistanceSpecified = filtersInput.IsDistanceLocation && (filtersInput.Distance > 0);
                ReplaceSpecialCharactersOnFreeTextFields(courseSearchProperties.Filters);

                var combinedDate = $"{filtersInput.StartDateYear}/{filtersInput.StartDateMonth}/{filtersInput.StartDateDay}";
                if (DateTime.TryParse(combinedDate, out DateTime result))
                {
                    courseSearchProperties.Filters.StartDateFrom = result;
                }

                var response = asyncHelper.Synchronise(() => courseSearchService.SearchCoursesAsync(courseSearchProperties));

                if (response.Courses.Any())
                {
                    foreach (var course in response.Courses)
                    {
                        course.CourseLink = $"{CourseDetailsPage}?{nameof(CourseDetails.CourseId)}={course.CourseId}&r={course.RunId}&referralPath={context.GetUrlEncodedPathAndQuery()}";
                        courseSearchResults.Courses.Add(new CourseListingViewModel
                        {
                            Course = course,
                            AdvancedLoanProviderLabel = AdvancedLoanProviderLabel,
                            LocationLabel             = LocationLabel,
                            ProviderLabel             = ProviderLabel,
                            StartDateLabel            = StartDateLabel
                        });
                    }

                    response.ResultProperties.OrderedBy = originalCourseSearchOrderBy;
                    SetupResultsViewModel(courseSearchResults, response);
                }

                SetupStartDateDisplayData(courseSearchResults);
            }

            SetupWidgetLabelsAndTextDefaults(courseSearchResults);
            return(View("SearchResults", courseSearchResults));
        }
        public void IndexTests(string searchTerm, string filterCourseByText, string pageTitle, string courseSearchResultsPage, string courseDetailsPage, string sortBy, CourseSearchResult courseSearchResponse)
        {
            // setupFakes
            A.CallTo(() => fakeCourseSearchService.SearchCoursesAsync(A <string> ._, A <CourseSearchProperties> ._)).Returns(courseSearchResponse);

            // Assign
            var controller = new CourseSearchResultsController(fakeApplicationLogger, fakeCourseSearchService, asyncHelper, fakeCourseSearchViewModelService, fakeBuildQueryStringService)
            {
                FilterCourseByText         = filterCourseByText,
                PageTitle                  = pageTitle,
                CourseSearchResultsPage    = courseSearchResultsPage,
                CourseDetailsPage          = courseDetailsPage,
                LocationRegex              = nameof(CourseSearchResultsController.LocationRegex),
                RecordsPerPage             = 40,
                AdvancedLoanProviderLabel  = nameof(CourseSearchResultsController.AdvancedLoanProviderLabel),
                LocationLabel              = nameof(CourseSearchResultsController.LocationLabel),
                ProviderLabel              = nameof(CourseSearchResultsController.ProviderLabel),
                StartDateLabel             = nameof(CourseSearchResultsController.StartDateLabel),
                OrderByText                = nameof(CourseSearchResultsController.OrderByText),
                StartDateOrderByText       = nameof(CourseSearchResultsController.StartDateOrderByText),
                DistanceOrderByText        = nameof(CourseSearchResultsController.DistanceOrderByText),
                RelevanceOrderByText       = nameof(CourseSearchResultsController.RelevanceOrderByText),
                NoTrainingCoursesFoundText = nameof(CourseSearchResultsController.NoTrainingCoursesFoundText)
            };

            // Act
            var controllerResult = controller.WithCallTo(contrl =>
                                                         contrl.Index(
                                                             searchTerm,
                                                             string.Empty,
                                                             string.Empty,
                                                             string.Empty,
                                                             string.Empty,
                                                             sortBy,
                                                             string.Empty,
                                                             string.Empty,
                                                             false,
                                                             1));

            // Assert
            controllerResult.ShouldRenderView("SearchResults").WithModel <CourseSearchResultsViewModel>(
                vm =>
            {
                vm.PageTitle.Should().BeEquivalentTo(controller.PageTitle);
                vm.FilterCourseByText.Should().BeEquivalentTo(controller.FilterCourseByText);
            });

            if (!string.IsNullOrWhiteSpace(searchTerm))
            {
                A.CallTo(() => fakeCourseSearchService.SearchCoursesAsync(A <string> ._, A <CourseSearchProperties> ._)).MustHaveHappened();
                if (courseSearchResponse.Courses.Any())
                {
                    A.CallTo(() => fakeCourseSearchViewModelService.GetOrderByLinks(A <string> ._, A <CourseSearchOrderBy> ._)).MustHaveHappened();
                    A.CallTo(() => fakeCourseSearchViewModelService.SetupPaging(A <CourseSearchResultsViewModel> ._, A <CourseSearchResult> ._, A <string> ._, A <int> ._, A <string> ._)).MustHaveHappened();
                }
            }
            else
            {
                A.CallTo(() => fakeCourseSearchService.SearchCoursesAsync(A <string> ._, A <CourseSearchProperties> ._)).MustNotHaveHappened();
                A.CallTo(() => fakeCourseSearchViewModelService.GetOrderByLinks(A <string> ._, A <CourseSearchOrderBy> ._)).MustNotHaveHappened();
                A.CallTo(() => fakeCourseSearchViewModelService.SetupPaging(A <CourseSearchResultsViewModel> ._, A <CourseSearchResult> ._, A <string> ._, A <int> ._, A <string> ._)).MustNotHaveHappened();
            }
        }