예제 #1
0
        public void ShouldDeleteCorrectly()
        {
            Mock <IGenericService <Course> >      courseServiceMock = new Mock <IGenericService <Course> >();
            Mock <IActivityLoggerService>         loggerMock        = new Mock <IActivityLoggerService>();
            Mock <IObjectFinderService <Module> > moduleFinderMock  = new Mock <IObjectFinderService <Module> >();
            Mock <IGenericService <Module> >      moduleServiceMock = new Mock <IGenericService <Module> >();
            Mock <IManyToManyMapperService <Course, CourseModule, Module> > mapperServiceMock = new Mock <IManyToManyMapperService <Course, CourseModule, Module> >();
            Mock <IGenericService <ExamProgram> > examProgramServiceMock = new Mock <IGenericService <ExamProgram> >();

            CourseCreateUpdateViewModel course = new CourseCreateUpdateViewModel()
            {
                Id          = 100,
                Name        = "Periode 2.3 Data Science",
                Description = "Alle opdrachten in deze periode zijn gekoppeld aan vakken; dit kunnen individuele of groepsopdrachten zijn; er zijn geen opdrachten die meerdere vakken betreffen. Daarom is een verdere omschrijving hier niet nodig."
            };

            ClaimsPrincipal identity = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
            {
                new Claim(ClaimTypes.NameIdentifier, "123")
            }));

            moduleFinderMock.Setup(m => m.AreIdsValid(It.IsAny <string[]>())).Returns(true);

            moduleFinderMock.Setup(m => m.GetObjects(It.IsAny <int[]>())).Returns((int[] ids) =>
            {
                return(new List <Module>());
            });

            courseServiceMock.Setup(m => m.FindById(It.IsAny <int>(), It.IsAny <string[]>())).Returns(course);

            courseServiceMock.Setup(m => m.Delete(It.IsAny <Course>())).Returns((Course model) =>
            {
                return(1);
            });

            loggerMock.Setup(m => m.Delete(It.IsAny <string>()));

            CourseController controller = new CourseController(courseServiceMock.Object, moduleServiceMock.Object, moduleFinderMock.Object, mapperServiceMock.Object, examProgramServiceMock.Object, null)
            {
                ControllerContext = new ControllerContext()
                {
                    HttpContext = new DefaultHttpContext()
                    {
                        User = identity
                    }
                }
            };

            RedirectToActionResult result = controller.Delete(course.Id) as RedirectToActionResult;

            Assert.Equal("Index", result?.ActionName);
        }
예제 #2
0
        public IActionResult Create(CourseCreateUpdateViewModel course)
        {
            course.Modules     = manyToManyMapper.GetMappedEntities(course, moduleFinder.GetObjects(course.ModuleIds.ToArray()).ToList());
            course.ExamProgram = examprogramService.FindById(course.ExamProgramId);
            course.Mentor      = teacherService.FindById(course.MentorId);
            IActionResult actionResult = View(course);

            if (!ModelState.IsValid || !moduleFinder.AreIdsValid(course.ModuleIds.ToArray()))
            {
                actionResult = View("Error");
            }
            else if (courseService.Insert(course) == 1)
            {
                actionResult = RedirectToAction(nameof(Index));
            }

            return(actionResult);
        }
예제 #3
0
        // GET: Course/Edit/5
        /// <summary>
        /// Represents an <see cref="IActionResult"/> that renders the the <see cref="Edit(int)"/> view to the response.
        /// </summary>
        /// <param name="id">The ID of the course to edit.</param>
        /// <returns>A view of the <see cref="Edit(int)"/> action method.</returns>
        public IActionResult Edit(int id)
        {
            IActionResult actionResult = View();

            ViewData["Examprograms"] = examprogramService.FindAll();
            ViewData["Modules"]      = moduleService.FindAll();
            ViewData["Teachers"]     = teacherService.FindAll();

            Course resultObject = courseService.FindById(id, new string[] { "Mentor", "ExamProgram", "Modules" });
            CourseCreateUpdateViewModel result = new CourseCreateUpdateViewModel()
            {
                Name        = resultObject.Name,
                Description = resultObject.Description,
                ExamProgram = resultObject.ExamProgram,
                Mentor      = resultObject.Mentor,
                StudyYear   = resultObject.StudyYear,
                Modules     = resultObject.Modules
            };

            actionResult = View(result);

            return(actionResult);
        }
예제 #4
0
        public void CannotEditWithMissingValues()
        {
            Mock <IGenericService <Course> >      courseServiceMock = new Mock <IGenericService <Course> >();
            Mock <IActivityLoggerService>         loggerMock        = new Mock <IActivityLoggerService>();
            Mock <IObjectFinderService <Module> > moduleFinderMock  = new Mock <IObjectFinderService <Module> >();
            Mock <IGenericService <Module> >      moduleServiceMock = new Mock <IGenericService <Module> >();
            Mock <IManyToManyMapperService <Course, CourseModule, Module> > mapperServiceMock = new Mock <IManyToManyMapperService <Course, CourseModule, Module> >();
            Mock <IGenericService <ExamProgram> > examProgramServiceMock = new Mock <IGenericService <ExamProgram> >();
            Mock <IGenericService <Teacher> >     teacherServiceMock     = new Mock <IGenericService <Teacher> >();

            CourseCreateUpdateViewModel course = new CourseCreateUpdateViewModel()
            {
                Id          = 100,
                Name        = null,
                Description = "Alle opdrachten in deze periode zijn gekoppeld aan vakken; dit kunnen individuele of groepsopdrachten zijn; er zijn geen opdrachten die meerdere vakken betreffen. Daarom is een verdere omschrijving hier niet nodig."
            };

            ClaimsPrincipal identity = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
            {
                new Claim(ClaimTypes.NameIdentifier, "123")
            }));

            teacherServiceMock.Setup(m => m.FindById(It.IsAny <int>(), It.IsAny <string[]>())).Returns(new Teacher());

            moduleFinderMock.Setup(m => m.AreIdsValid(It.IsAny <string[]>())).Returns(true);

            moduleFinderMock.Setup(m => m.GetObjects(It.IsAny <int[]>())).Returns((int[] ids) =>
            {
                return(new List <Module>());
            });

            mapperServiceMock.Setup(m => m.GetMappedEntities(It.IsAny <Course>(), It.IsAny <ICollection <Module> >())).Returns <Course, ICollection <Module> >((model, modules) =>
            {
                return(new List <CourseModule>());
            });

            courseServiceMock.Setup(m => m.Update(It.IsAny <Course>())).Returns((Course model) =>
            {
                if (!string.IsNullOrWhiteSpace(model.Name))
                {
                    return(1);
                }

                return(0);
            });

            loggerMock.Setup(m => m.Create(It.IsAny <string>()));

            CourseController controller = new CourseController(courseServiceMock.Object, moduleServiceMock.Object, moduleFinderMock.Object, mapperServiceMock.Object, examProgramServiceMock.Object, teacherServiceMock.Object)
            {
                ControllerContext = new ControllerContext()
                {
                    HttpContext = new DefaultHttpContext()
                    {
                        User = identity
                    }
                }
            };

            ViewResult result = controller.Edit(course) as ViewResult;

            Assert.NotNull(result);
            Assert.NotNull(result.Model);
            Assert.True(string.IsNullOrEmpty(result.ViewName) || result.ViewName == "Edit");
        }