public void UpdateRegistration(Registration registration, RegistrationPostModel registrationPostModel, Student student, ModelStateDictionary modelState, bool adminUpdate = false)
        {
            CopyHelper.CopyRegistrationValues(registrationPostModel.Registration, registration);
            registration.GradTrack = registrationPostModel.GradTrack;

            NullOutBlankFields(registration);
            registration.SpecialNeeds = LoadSpecialNeeds(registrationPostModel.SpecialNeeds);
            UpdateCeremonyParticipations(registration, registrationPostModel.CeremonyParticipations, modelState, adminUpdate);
            AddRegistrationPetitions(registration, registrationPostModel.CeremonyParticipations, modelState);
        }
        public Registration PopulateRegistration(RegistrationPostModel registrationPostModel, Student student, ModelStateDictionary modelState, bool adminUpdate = false)
        {
            // setup variables to be used for this method
            var ceremonyParticipations = registrationPostModel.CeremonyParticipations;
            var specialNeeds = registrationPostModel.SpecialNeeds;
            var term = TermService.GetCurrent();

            var registration = registrationPostModel.Registration;
            registration.TermCode = term;
            registration.Student = student;
            NullOutBlankFields(registration);
            registration.SpecialNeeds = LoadSpecialNeeds(specialNeeds);
            registration.GradTrack = registrationPostModel.GradTrack;

            registration.TicketPassword = GeneratePassword(); //Membership.GeneratePassword(10,3);

            //ValidateCeremonyParticipations(ceremonyParticipations, modelState);
            AddCeremonyParticipations(registration, ceremonyParticipations, modelState, adminUpdate);
            AddRegistrationPetitions(registration, ceremonyParticipations, modelState);

            return registration;
        }
        public void TestPopulateRegistration1()
        {
            #region Arrange
            FakeTermCodeService.LoadTermCodes("201003", TermCodeRepository);
            RegistrationPopulator = new RegistrationPopulator(SpecialNeedRepository, RegistrationPetitionRepository,ParticipationRepository, RegistrationRepository);
            var student = CreateValidEntities.Student(1);
            var registrationPostModel = new RegistrationPostModel();
            var participations = new List<CeremonyParticipation>();
            for (int i = 0; i < 3; i++)
            {
                participations.Add(new CeremonyParticipation());
                participations[i].ParticipationId = i + 1;
            }
            registrationPostModel.CeremonyParticipations = participations;
            var registration = CreateValidEntities.Registration(7);
            registration.Address2 = string.Empty;
            registration.Email = string.Empty;
            registrationPostModel.Registration = registration;
            registrationPostModel.GradTrack = true;
            #endregion Arrange

            #region Act
            var result = RegistrationPopulator.PopulateRegistration(registrationPostModel, student,
                                                                    Controller.ModelState);

            #endregion Act

            #region Assert
            Assert.IsNotNull(result);
            Assert.IsNull(result.Address2);
            Assert.IsNull(result.Email);
            Assert.IsNotNull(result.SpecialNeeds);
            Assert.AreEqual(0, result.SpecialNeeds.Count);
            Assert.IsFalse(Controller.ModelState.IsValid);
            Controller.ModelState.AssertErrorsAre("You have to select one or more ceremonies to participate.");
            #endregion Assert
        }
        public ActionResult RegisterForStudent(Guid id, RegistrationPostModel registrationPostModel, bool supressEmail)
        {
            ModelState.Clear();

            // load the student
            var student = _studentRepository.GetNullableById(id);
            if (student == null)
            {
                Message = StaticValues.Error_StudentNotFound;
                return this.RedirectToAction(a => a.Students(null, null, null, null, null));
            }

            // check for an existing registration
            var registration = Repository.OfType<Registration>().Queryable.SingleOrDefault(a => a.Student == student && a.TermCode == TermService.GetCurrent());

            if (registration == null)
            {
                registration = _registrationPopulator.PopulateRegistration(registrationPostModel, student, ModelState, true);
            }
            else
            {
                _registrationPopulator.UpdateRegistration(registration, registrationPostModel, student, ModelState, true);
            }

            registration.TransferValidationMessagesTo(ModelState);

            if (ModelState.IsValid)
            {
                _registrationRepository.EnsurePersistent(registration);

                if (!supressEmail)
                {
                    try
                    {

                        // add email for registration into queue
                        _emailService.QueueRegistrationConfirmation(registration);
                    }
                    catch (Exception ex)
                    {
                        _errorService.ReportError(ex);
                        Message += StaticValues.Student_Email_Problem;
                    }
                }

                // put message up for student
                Message += StaticValues.Student_Register_Successful;

                return this.RedirectToAction(a => a.StudentDetails(id, false));
            }

            // load the current user's ceremonies, to determine what they can register the student for
            var registrationModel = RegistrationModel.Create(repository: Repository, ceremonies: _ceremonyService.StudentEligibility(student.Majors.ToList(), student.TotalUnits), student: student, registration: registration, edit: true, admin: true);
            var viewModel = AdminRegisterForStudentViewModel.Create(Repository, registrationModel);
            return View(viewModel);
        }
        public void TestPopulateRegistration9()
        {
            //This one has 1 existing participations for student and ceremony and 1 existing petition
            #region Arrange
            var collegeRepository = MockRepository.GenerateStub<IRepositoryWithTypedId<College, string>>();

            FakeTermCodeService.LoadTermCodes("201003", TermCodeRepository);
            ControllerRecordFakes.FakeCeremony(2, CeremonyRepository);
            ControllerRecordFakes.FakeCollege(2, collegeRepository);
            RegistrationPopulator = new RegistrationPopulator(SpecialNeedRepository, RegistrationPetitionRepository, ParticipationRepository, RegistrationRepository);
            var student = CreateValidEntities.Student(1);
            var student2 = CreateValidEntities.Student(2);
            var registrationPostModel = new RegistrationPostModel();
            var participations = new List<CeremonyParticipation>();
            for (int i = 0; i < 2; i++)
            {
                participations.Add(new CeremonyParticipation());
                //participations[i].ParticipationId = i + 1;
                participations[i].Participate = false;
                participations[i].Petition = true;
                participations[i].Ceremony = CeremonyRepository.GetNullableById(i + 1);
                participations[i].Ceremony.RegistrationBegin = DateTime.Now.AddDays(-10);
                participations[i].Ceremony.RegistrationDeadline = DateTime.Now.AddDays(10);
                participations[i].Major = CreateValidEntities.MajorCode(i + 1);
                participations[i].Major.College = collegeRepository.GetNullableById((i + 1).ToString());
            }
            registrationPostModel.CeremonyParticipations = participations;
            var registration = CreateValidEntities.Registration(7);
            registration.Address2 = string.Empty;
            registration.Email = string.Empty;
            registrationPostModel.Registration = registration;
            registrationPostModel.GradTrack = true;

            var registrationPetitions = new List<RegistrationPetition>();
            registrationPetitions.Add(CreateValidEntities.RegistrationPetition(1));
            registrationPetitions.Add(CreateValidEntities.RegistrationPetition(2));
            registrationPetitions[0].Ceremony = CeremonyRepository.GetNullableById(1);
            registrationPetitions[0].Registration = CreateValidEntities.Registration(1);
            registrationPetitions[0].Registration.Student = student2;
            registrationPetitions[1].Ceremony = CeremonyRepository.GetNullableById(2);
            registrationPetitions[1].Registration = CreateValidEntities.Registration(2);
            registrationPetitions[1].Registration.Student = student;
            RegistrationPetitionRepository.Expect(a => a.Queryable)
                .Return(registrationPetitions.AsQueryable()).Repeat.Any();
            var registrationParticipations = new List<RegistrationParticipation>();
            registrationParticipations.Add(CreateValidEntities.RegistrationParticipation(1));
            registrationParticipations.Add(CreateValidEntities.RegistrationParticipation(2));
            registrationParticipations[0].Ceremony = CeremonyRepository.GetNullableById(1);
            registrationParticipations[0].Registration = CreateValidEntities.Registration(1);
            registrationParticipations[0].Registration.Student = student;
            registrationParticipations[1].Ceremony = CeremonyRepository.GetNullableById(2);
            registrationParticipations[1].Registration = CreateValidEntities.Registration(2);
            registrationParticipations[1].Registration.Student = student2;
            ParticipationRepository.Expect(a => a.Queryable)
                .Return(registrationParticipations.AsQueryable()).Repeat.Any();
            #endregion Arrange

            #region Act
            var result = RegistrationPopulator.PopulateRegistration(registrationPostModel, student,
                                                                    Controller.ModelState);

            #endregion Act

            #region Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(Controller.ModelState.IsValid);
            Assert.AreEqual(0, result.RegistrationParticipations.Count);
            Assert.AreEqual(0, result.RegistrationPetitions.Count);
            #endregion Assert
        }
        public void TestPopulateRegistration6()
        {
            #region Arrange

            var collegeRepository = MockRepository.GenerateStub<IRepositoryWithTypedId<College, string>>();

            ControllerRecordFakes.FakeSpecialNeeds(3, SpecialNeedRepository);

            FakeTermCodeService.LoadTermCodes("201003", TermCodeRepository);
            ControllerRecordFakes.FakeCeremony(2, CeremonyRepository);
            ControllerRecordFakes.FakeCollege(2, collegeRepository);
            RegistrationPopulator = new RegistrationPopulator(SpecialNeedRepository, RegistrationPetitionRepository, ParticipationRepository, RegistrationRepository);
            var student = CreateValidEntities.Student(1);
            var registrationPostModel = new RegistrationPostModel();
            var participations = new List<CeremonyParticipation>();
            for (int i = 0; i < 2; i++)
            {
                participations.Add(new CeremonyParticipation());
                participations[i].ParticipationId = i + 1;
                participations[i].Participate = true;
                participations[i].Petition = false;
                participations[i].Ceremony = CeremonyRepository.GetNullableById(i + 1);
                participations[i].Ceremony.RegistrationBegin = DateTime.Now.AddDays(-10);
                participations[i].Ceremony.RegistrationDeadline = DateTime.Now.AddDays(10);
                participations[i].Major = CreateValidEntities.MajorCode(i + 1);
                participations[i].Major.College = collegeRepository.GetNullableById((i + 1).ToString());
            }
            registrationPostModel.CeremonyParticipations = participations;
            var registration = CreateValidEntities.Registration(7);
            registration.Address2 = string.Empty;
            registration.Email = string.Empty;
            registrationPostModel.Registration = registration;
            registrationPostModel.GradTrack = false;
            registrationPostModel.SpecialNeeds = new List<string>{"1", "3"};
            #endregion Arrange

            #region Act
            var result = RegistrationPopulator.PopulateRegistration(registrationPostModel, student,
                                                                    Controller.ModelState);

            #endregion Act

            #region Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(2, result.SpecialNeeds.Count);
            Assert.AreEqual("Name1", result.SpecialNeeds[0].Name);
            Assert.AreEqual("Name3", result.SpecialNeeds[1].Name);
            #endregion Assert
        }
        public void TestPopulateRegistration3()
        {
            #region Arrange

            var collegeRepository = MockRepository.GenerateStub<IRepositoryWithTypedId<College, string>>();

            FakeTermCodeService.LoadTermCodes("201003", TermCodeRepository);
            ControllerRecordFakes.FakeCeremony(2, CeremonyRepository);
            ControllerRecordFakes.FakeCollege(2, collegeRepository);
            RegistrationPopulator = new RegistrationPopulator(SpecialNeedRepository, RegistrationPetitionRepository, ParticipationRepository, RegistrationRepository);
            var student = CreateValidEntities.Student(1);
            var registrationPostModel = new RegistrationPostModel();
            var participations = new List<CeremonyParticipation>();
            for (int i = 0; i < 2; i++)
            {
                participations.Add(new CeremonyParticipation());
                participations[i].ParticipationId = i + 1;
                participations[i].Participate = true;
                participations[i].Petition = false;
                participations[i].Ceremony = CeremonyRepository.GetNullableById(i+1);
                participations[i].Major = CreateValidEntities.MajorCode(i + 1);
                participations[i].Major.College = collegeRepository.GetNullableById("1"); //Same college so error
            }
            registrationPostModel.CeremonyParticipations = participations;
            var registration = CreateValidEntities.Registration(7);
            registration.Address2 = string.Empty;
            registration.Email = string.Empty;
            registrationPostModel.Registration = registration;
            registrationPostModel.GradTrack = true;
            #endregion Arrange

            #region Act
            var result = RegistrationPopulator.PopulateRegistration(registrationPostModel, student,
                                                                    Controller.ModelState);

            #endregion Act

            #region Assert
            Assert.IsNotNull(result);
            Assert.IsFalse(Controller.ModelState.IsValid);
            Controller.ModelState.AssertErrorsAre("You cannot register for two ceremonies within the same college.");
            #endregion Assert
        }
        public void TestRegisterPostWithRegistrationPetitionWithMissingReasonDoesNotSave()
        {
            #region Arrange
            FakeTermCodeService.LoadTermCodes("201003", TermCodeRepository);
            Controller.ControllerContext.HttpContext = new MockHttpContext(1, new[] { "" });
            var students = new List<Student>();
            var student = CreateValidEntities.Student(1);
            student.Majors.Add(CreateValidEntities.MajorCode(2));
            student.Majors.Add(CreateValidEntities.MajorCode(3));
            student.EarnedUnits = 11m;
            student.CurrentUnits = 12m;
            students.Add(student);
            ControllerRecordFakes.FakeStudent(0, StudentRepository, students, null);
            StudentService.Expect(a => a.GetCurrentStudent(Arg<IPrincipal>.Is.Anything)).Return(StudentRepository.Queryable.First()).Repeat.Any();

            var registrations = new List<Registration>();
            var registration = CreateValidEntities.Registration(1);
            registration.Student = CreateValidEntities.Student(99);
            registration.TermCode = TermCodeRepository.Queryable.First();
            var registrationPetition = CreateValidEntities.RegistrationPetition(1);
            registrationPetition.ExceptionReason = " ";
            registration.AddPetition(registrationPetition); //Invalid
            registrations.Add(registration);
            ControllerRecordFakes.FakeRegistration(0, RegistrationRepository, registrations);

            var ceremonies = new List<Ceremony>();
            for (int i = 0; i < 3; i++)
            {
                ceremonies.Add(CreateValidEntities.Ceremony(i + 1));
                ceremonies[i].RegistrationBegin = DateTime.Now.AddDays(7);
            }
            ceremonies[1].RegistrationBegin = DateTime.Now.AddDays(-5);

            CeremonyService.Expect(
                a =>
                a.StudentEligibility(Arg<List<MajorCode>>.Is.Anything, Arg<decimal>.Is.Anything, Arg<TermCode>.Is.Anything, Arg<int?>.Is.Anything)).Return(ceremonies).Repeat.Any();

            ControllerRecordFakes.FakeState(2, Controller.Repository, null);
            ControllerRecordFakes.FakevTermCode(1, VTermCodeRepository);
            ControllerRecordFakes.FakeSpecialNeeds(1, SpecialNeedRepository);

            var registrationPostModel = new RegistrationPostModel {AgreeToDisclaimer = true};
            RegistrationPopulator.Expect(a => a.PopulateRegistration(Arg<RegistrationPostModel>.Is.Anything, Arg<Student>.Is.Anything, Arg<ModelStateDictionary>.Is.Anything, Arg<bool>.Is.Anything))
                .Return(registration)
                .Repeat.Any();

            var participations = new List<RegistrationParticipation>();
            participations.Add(CreateValidEntities.RegistrationParticipation(1));
            participations[0].Registration = CreateValidEntities.Registration(55);
            participations[0].Registration.Student = CreateValidEntities.Student(11);
            participations[0].Registration.Student.Login = "******";
            participations[0].Registration.TermCode = TermCodeRepository.Queryable.Last();
            participations[0].Major = CreateValidEntities.MajorCode(1);
            participations[0].Major.College = CreateValidEntities.College(9);

            ControllerRecordFakes.FakeRegistrationParticipation(0, ParticipationRepository, participations);
            #endregion Arrange

            #region Act

            Controller.Register(registrationPostModel)
                .AssertViewRendered()
                .WithViewData<RegistrationModel>();
            #endregion Act

            #region Assert
            StudentService.AssertWasCalled(a => a.GetCurrentStudent(Arg<IPrincipal>.Is.Anything));
            CeremonyService.AssertWasCalled(a => a.StudentEligibility(Arg<List<MajorCode>>.Is.Anything, Arg<decimal>.Is.Anything, Arg<TermCode>.Is.Anything, Arg<int?>.Is.Anything), x => x.Repeat.Times(2));
            RegistrationPopulator.AssertWasCalled(a => a.PopulateRegistration(Arg<RegistrationPostModel>.Is.Anything, Arg<Student>.Is.Anything, Arg<ModelStateDictionary>.Is.Anything, Arg<bool>.Is.Anything));
            RegistrationRepository.AssertWasNotCalled(a => a.EnsurePersistent(Arg<Registration>.Is.Anything));
            Assert.IsFalse(Controller.ModelState.IsValid);
            Controller.ModelState.AssertErrorsAre("Exception reason is required.");

            #endregion Assert
        }
        public void TestRegisterPostWithValidRegistrationSavesAndErrorIsRecordedIfEmailProblem2()
        {
            #region Arrange
            FakeTermCodeService.LoadTermCodes("201003", TermCodeRepository);
            Controller.ControllerContext.HttpContext = new MockHttpContext(1, new[] { "" });
            var students = new List<Student>();
            var student = CreateValidEntities.Student(1);
            student.Majors.Add(CreateValidEntities.MajorCode(2));
            student.Majors.Add(CreateValidEntities.MajorCode(3));
            student.EarnedUnits = 11m;
            student.CurrentUnits = 12m;
            students.Add(student);
            ControllerRecordFakes.FakeStudent(0, StudentRepository, students, null);
            StudentService.Expect(a => a.GetCurrentStudent(Arg<IPrincipal>.Is.Anything)).Return(StudentRepository.Queryable.First()).Repeat.Any();

            var registrations = new List<Registration>();
            var registration = CreateValidEntities.Registration(1);
            registration.Student = CreateValidEntities.Student(99);
            registration.TermCode = TermCodeRepository.Queryable.First();
            var registrationParticipation = CreateValidEntities.RegistrationParticipation(1);
            registration.RegistrationParticipations.Add(registrationParticipation);
            registrations.Add(registration);
            ControllerRecordFakes.FakeRegistration(0, RegistrationRepository, registrations);

            var ceremonies = new List<Ceremony>();
            for (int i = 0; i < 3; i++)
            {
                ceremonies.Add(CreateValidEntities.Ceremony(i + 1));
                ceremonies[i].RegistrationBegin = DateTime.Now.AddDays(7);
            }
            ceremonies[1].RegistrationBegin = DateTime.Now.AddDays(-5);

            CeremonyService.Expect(
                a =>
                a.StudentEligibility(Arg<List<MajorCode>>.Is.Anything, Arg<decimal>.Is.Anything, Arg<TermCode>.Is.Anything, Arg<int?>.Is.Anything)).Return(ceremonies).Repeat.Any();

            ControllerRecordFakes.FakeState(2, Controller.Repository, null);
            ControllerRecordFakes.FakevTermCode(1, VTermCodeRepository);
            ControllerRecordFakes.FakeSpecialNeeds(1, SpecialNeedRepository);

            var registrationPostModel = new RegistrationPostModel {AgreeToDisclaimer = true};
            RegistrationPopulator.Expect(a => a.PopulateRegistration(Arg<RegistrationPostModel>.Is.Anything, Arg<Student>.Is.Anything, Arg<ModelStateDictionary>.Is.Anything, Arg<bool>.Is.Anything))
                .Return(registration)
                .Repeat.Any();

            EmailService.Expect(a => a.QueueRegistrationConfirmation(Arg<Registration>.Is.Anything)).Throw(new Exception("Test Exception"));

            var participations = new List<RegistrationParticipation>();
            participations.Add(CreateValidEntities.RegistrationParticipation(1));
            participations[0].Registration = CreateValidEntities.Registration(55);
            participations[0].Registration.Student = CreateValidEntities.Student(11);
            participations[0].Registration.Student.Login = "******";
            participations[0].Registration.TermCode = TermCodeRepository.Queryable.Last();
            participations[0].Major = CreateValidEntities.MajorCode(1);
            participations[0].Major.College = CreateValidEntities.College(9);

            ControllerRecordFakes.FakeRegistrationParticipation(0, ParticipationRepository, participations);
            #endregion Arrange

            #region Act

            Controller.Register(registrationPostModel)
                .AssertActionRedirect()
                .ToAction<StudentController>(a => a.DisplayRegistration());
            #endregion Act

            #region Assert
            StudentService.AssertWasCalled(a => a.GetCurrentStudent(Arg<IPrincipal>.Is.Anything));
            CeremonyService.AssertWasCalled(a => a.StudentEligibility(Arg<List<MajorCode>>.Is.Anything, Arg<decimal>.Is.Anything, Arg<TermCode>.Is.Anything, Arg<int?>.Is.Anything), x => x.Repeat.Times(1));
            RegistrationPopulator.AssertWasCalled(a => a.PopulateRegistration(Arg<RegistrationPostModel>.Is.Anything, Arg<Student>.Is.Anything, Arg<ModelStateDictionary>.Is.Anything, Arg<bool>.Is.Anything));
            RegistrationRepository.AssertWasCalled(a => a.EnsurePersistent(Arg<Registration>.Is.Anything));
            Assert.IsTrue(Controller.ModelState.IsValid);
            Assert.AreEqual(" There was a problem sending you an email.  Please print this page for your records.You have successfully registered for commencement.", Controller.Message);
            EmailService.AssertWasNotCalled(a => a.QueueRegistrationPetition(Arg<Registration>.Is.Anything));
            EmailService.AssertWasCalled(a => a.QueueRegistrationConfirmation(Arg<Registration>.Is.Anything));
            ErrorService.AssertWasCalled(a => a.ReportError(Arg<Exception>.Is.Anything));
            #endregion Assert
        }
        public void TestUpdateRegistrationPetitionIsAddedIfNoneExistAndNoParticipations1()
        {
            //Test expected fields get copied
            #region Arrange
            var collegeRepository = MockRepository.GenerateStub<IRepositoryWithTypedId<College, string>>();
            RegistrationPopulator = new RegistrationPopulator(SpecialNeedRepository, RegistrationPetitionRepository, ParticipationRepository, RegistrationRepository);

            var student = CreateValidEntities.Student(1);
            var student2 = CreateValidEntities.Student(2);
            ControllerRecordFakes.FakeCeremony(2, CeremonyRepository);
            ControllerRecordFakes.FakeCollege(2, collegeRepository);
            var registration1 = CreateValidEntities.Registration(1);
            registration1.Student = student;
            registration1.Address1 = "Address11";
            registration1.Address2 = "Address21";
            registration1.City = "City1";
            registration1.Email = "*****@*****.**";
            registration1.GradTrack = false;
            registration1.MailTickets = false;
            registration1.State = CreateValidEntities.State(1);
            registration1.TermCode = CreateValidEntities.TermCode(1);
            registration1.Zip = "1";

            var registration2 = CreateValidEntities.Registration(2);
            registration2.Email = string.Empty;

            var registrationPostModel = new RegistrationPostModel();
            registrationPostModel.Registration = registration2;

            var registrationParticipations = new List<RegistrationParticipation>();
            //registrationParticipations.Add(CreateValidEntities.RegistrationParticipation(1));
            //registrationParticipations.Add(CreateValidEntities.RegistrationParticipation(2));
            //registrationParticipations[0].Ceremony = CeremonyRepository.GetNullableById(1);
            //registrationParticipations[0].Registration = CreateValidEntities.Registration(1);
            //registrationParticipations[0].Registration.Student = student2;
            //registrationParticipations[0].DateUpdated = DateTime.Now.AddDays(-10);
            //registrationParticipations[1].Ceremony = CeremonyRepository.GetNullableById(2);
            //registrationParticipations[1].Ceremony.RegistrationBegin = DateTime.Now.AddDays(2);
            //registrationParticipations[1].Registration = CreateValidEntities.Registration(2);
            //registrationParticipations[1].Registration.Student = student2;
            //registrationParticipations[1].DateUpdated = DateTime.Now.AddDays(-11);
            ControllerRecordFakes.FakeRegistrationParticipation(0, ParticipationRepository, registrationParticipations);

            var participations = new List<CeremonyParticipation>();
            for (int i = 0; i < 2; i++)
            {
                participations.Add(new CeremonyParticipation());
                participations[i].ParticipationId = null;
                participations[i].Participate = false;
                participations[i].Petition = true;
                participations[i].Ceremony = CeremonyRepository.GetNullableById(i + 1);
                participations[i].Major = CreateValidEntities.MajorCode(i + 1);
                participations[i].Major.College = collegeRepository.GetNullableById((i + 1).ToString());
            }
            registrationPostModel.CeremonyParticipations = participations;

            var registrationPetitions = new List<RegistrationPetition>();
            registrationPetitions.Add(CreateValidEntities.RegistrationPetition(1));
            registrationPetitions.Add(CreateValidEntities.RegistrationPetition(2));
            registrationPetitions[0].Ceremony = CeremonyRepository.GetNullableById(1);
            registrationPetitions[0].Registration = CreateValidEntities.Registration(1);
            registrationPetitions[0].Registration.Student = student2;
            registrationPetitions[1].Ceremony = CeremonyRepository.GetNullableById(2);
            registrationPetitions[1].Registration = CreateValidEntities.Registration(2);
            registrationPetitions[1].Registration.Student = student2;
            RegistrationPetitionRepository.Expect(a => a.Queryable)
                .Return(registrationPetitions.AsQueryable()).Repeat.Any();

            registration1.RegistrationParticipations = registrationParticipations;
            #endregion Arrange

            #region Act
            RegistrationPopulator.UpdateRegistration(registration1, registrationPostModel, null, Controller.ModelState);
            #endregion Act

            #region Assert
            Assert.IsTrue(Controller.ModelState.IsValid);
            Assert.AreEqual(2, registration1.RegistrationPetitions.Count);

            #endregion Assert
        }
        public void TestUpdateRegistration6()
        {
            //Test expected fields get copied
            #region Arrange
            var collegeRepository = MockRepository.GenerateStub<IRepositoryWithTypedId<College, string>>();
            RegistrationPopulator = new RegistrationPopulator(SpecialNeedRepository, RegistrationPetitionRepository, ParticipationRepository, RegistrationRepository);

            var student = CreateValidEntities.Student(1);
            var student2 = CreateValidEntities.Student(2);
            ControllerRecordFakes.FakeCeremony(2, CeremonyRepository);
            ControllerRecordFakes.FakeCollege(2, collegeRepository);
            var registration1 = CreateValidEntities.Registration(1);
            registration1.Student = student;
            registration1.Address1 = "Address11";
            registration1.Address2 = "Address21";
            registration1.City = "City1";
            registration1.Email = "*****@*****.**";
            registration1.GradTrack = false;
            registration1.MailTickets = false;
            registration1.State = CreateValidEntities.State(1);
            registration1.TermCode = CreateValidEntities.TermCode(1);
            registration1.Zip = "1";

            var registration2 = CreateValidEntities.Registration(2);
            registration2.Email = string.Empty;

            var registrationPostModel = new RegistrationPostModel();
            registrationPostModel.Registration = registration2;

            var participations = new List<CeremonyParticipation>();
            for (int i = 0; i < 2; i++)
            {
                participations.Add(new CeremonyParticipation());
                participations[i].ParticipationId = i + 1;
                participations[i].Participate = true;
                participations[i].Petition = false;
                participations[i].Ceremony = CeremonyRepository.GetNullableById(i + 1);
                participations[i].Major = CreateValidEntities.MajorCode(i + 1);
                participations[i].Major.College = collegeRepository.GetNullableById("1"); //Same college so error
            }
            registrationPostModel.CeremonyParticipations = participations;

            var registrationPetitions = new List<RegistrationPetition>();
            registrationPetitions.Add(CreateValidEntities.RegistrationPetition(1));
            registrationPetitions.Add(CreateValidEntities.RegistrationPetition(2));
            registrationPetitions[0].Ceremony = CeremonyRepository.GetNullableById(1);
            registrationPetitions[0].Registration = CreateValidEntities.Registration(1);
            registrationPetitions[0].Registration.Student = student2;
            registrationPetitions[1].Ceremony = CeremonyRepository.GetNullableById(2);
            registrationPetitions[1].Registration = CreateValidEntities.Registration(2);
            registrationPetitions[1].Registration.Student = student2;
            RegistrationPetitionRepository.Expect(a => a.Queryable)
                .Return(registrationPetitions.AsQueryable()).Repeat.Any();
            var registrationParticipations = new List<RegistrationParticipation>();
            registrationParticipations.Add(CreateValidEntities.RegistrationParticipation(1));
            registrationParticipations.Add(CreateValidEntities.RegistrationParticipation(2));
            registrationParticipations[0].Ceremony = CeremonyRepository.GetNullableById(1);
            registrationParticipations[0].Registration = CreateValidEntities.Registration(1);
            registrationParticipations[0].Registration.Student = student2;
            registrationParticipations[1].Ceremony = CeremonyRepository.GetNullableById(2);
            registrationParticipations[1].Registration = CreateValidEntities.Registration(2);
            registrationParticipations[1].Registration.Student = student2;
            ParticipationRepository.Expect(a => a.Queryable)
                .Return(registrationParticipations.AsQueryable()).Repeat.Any();
            #endregion Arrange

            #region Act
            RegistrationPopulator.UpdateRegistration(registration1, registrationPostModel, null, Controller.ModelState);
            #endregion Act

            #region Assert
            Assert.IsFalse(Controller.ModelState.IsValid);
            Controller.ModelState.AssertErrorsAre("You cannot register for two ceremonies within the same college.");
            #endregion Assert
        }
        public void TestUpdateRegistration1()
        {
            //Test expected fields get copied
            #region Arrange
            var collegeRepository = MockRepository.GenerateStub<IRepositoryWithTypedId<College, string>>();
            RegistrationPopulator = new RegistrationPopulator(SpecialNeedRepository, RegistrationPetitionRepository, ParticipationRepository, RegistrationRepository);

            var student = CreateValidEntities.Student(1);
            var student2 = CreateValidEntities.Student(2);
            ControllerRecordFakes.FakeCeremony(2, CeremonyRepository);
            ControllerRecordFakes.FakeCollege(2, collegeRepository);
            var registration1 = CreateValidEntities.Registration(1);
            registration1.Student = student;
            registration1.Address1 = "Address11";
            registration1.Address2 = "Address21";
            registration1.City = "City1";
            registration1.Email = "*****@*****.**";
            registration1.GradTrack = false;
            registration1.MailTickets = false;
            registration1.State = CreateValidEntities.State(1);
            registration1.TermCode = CreateValidEntities.TermCode(1);
            registration1.Zip = "1";

            var registration2 = CreateValidEntities.Registration(2);
            registration2.Student = student2; //this will be ignored.
            registration2.Address1 = "Address12";
            registration2.Address2 = "Address22";
            registration2.City = "City2";
            registration2.Email = "*****@*****.**";
            registration2.GradTrack = true; //ViewModel replaces this.
            registration2.MailTickets = true;
            registration2.State = CreateValidEntities.State(2);
            registration2.TermCode = CreateValidEntities.TermCode(2);
            registration2.Zip = "2";

            var registrationPostModel = new RegistrationPostModel();
            registrationPostModel.Registration = registration2;

            var participations = new List<CeremonyParticipation>();
            for (int i = 0; i < 2; i++)
            {
                participations.Add(new CeremonyParticipation());
                //participations[i].ParticipationId = i + 1;
                participations[i].Participate = false;
                participations[i].Petition = true;
                participations[i].Ceremony = CeremonyRepository.GetNullableById(i + 1);
                participations[i].Ceremony.RegistrationBegin = DateTime.Now.AddDays(-1);
                participations[i].Ceremony.RegistrationDeadline = DateTime.Now.AddDays(15);
                participations[i].Major = CreateValidEntities.MajorCode(i + 1);
                participations[i].Major.College = collegeRepository.GetNullableById((i + 1).ToString());
            }
            registrationPostModel.CeremonyParticipations = participations;
            registrationPostModel.GradTrack = true;

            var registrationPetitions = new List<RegistrationPetition>();
            registrationPetitions.Add(CreateValidEntities.RegistrationPetition(1));
            registrationPetitions.Add(CreateValidEntities.RegistrationPetition(2));
            registrationPetitions[0].Ceremony = CeremonyRepository.GetNullableById(1);
            registrationPetitions[0].Registration = CreateValidEntities.Registration(1);
            registrationPetitions[0].Registration.Student = student2;
            registrationPetitions[1].Ceremony = CeremonyRepository.GetNullableById(2);
            registrationPetitions[1].Registration = CreateValidEntities.Registration(2);
            registrationPetitions[1].Registration.Student = student2;
            RegistrationPetitionRepository.Expect(a => a.Queryable)
                .Return(registrationPetitions.AsQueryable()).Repeat.Any();
            var registrationParticipations = new List<RegistrationParticipation>();
            registrationParticipations.Add(CreateValidEntities.RegistrationParticipation(1));
            registrationParticipations.Add(CreateValidEntities.RegistrationParticipation(2));
            registrationParticipations[0].Ceremony = CeremonyRepository.GetNullableById(1);
            registrationParticipations[0].Registration = CreateValidEntities.Registration(1);
            registrationParticipations[0].Registration.Student = student2;
            registrationParticipations[1].Ceremony = CeremonyRepository.GetNullableById(2);
            registrationParticipations[1].Registration = CreateValidEntities.Registration(2);
            registrationParticipations[1].Registration.Student = student2;
            ParticipationRepository.Expect(a => a.Queryable)
                .Return(registrationParticipations.AsQueryable()).Repeat.Any();
            #endregion Arrange

            #region Act
            RegistrationPopulator.UpdateRegistration(registration1, registrationPostModel, null, Controller.ModelState);
            #endregion Act

            #region Assert
            Assert.AreNotEqual(registration1.Student.Pidm, registration2.Student.Pidm);
            Assert.AreEqual("Address12", registration1.Address1);
            Assert.AreEqual("Address22", registration1.Address2);
            Assert.AreEqual("City2", registration1.City);
            Assert.AreEqual("*****@*****.**", registration1.Email);
            Assert.AreEqual("2", registration1.Zip);
            Assert.IsTrue(registration1.GradTrack);
            Assert.IsTrue(registration1.MailTickets);
            Assert.AreEqual(registration2.State.Name, registration1.State.Name);
            Assert.AreNotEqual(registration2.TermCode.Name, registration1.TermCode.Name);
            #endregion Assert
        }
        public ActionResult Register(RegistrationPostModel registrationModel)
        {
            ModelState.Clear();

            var student = GetCurrentStudent();

            if (student == null) return this.RedirectToAction<AdminController>(a => a.Students(null, null, null, null, null));

            // validate they can register, also checks for duplicate registrations
            var redirect = CheckStudentForRegistration();
            if (redirect != null) return redirect;

            var registration = _registrationPopulator.PopulateRegistration(registrationModel, student, ModelState);

            registration.TransferValidationMessagesTo(ModelState);

            if (!registrationModel.AgreeToDisclaimer) ModelState.AddModelError("agreeToDisclaimer", StaticValues.Student_agree_to_disclaimer);
            if (registration.RegistrationPetitions.Any(a=>string.IsNullOrWhiteSpace(a.ExceptionReason))) ModelState.AddModelError("Exception Reason", "Exception/Petition reason is required.");

            if (ModelState.IsValid)
            {
                if (registration.RegistrationParticipations.Count > 0 || registration.RegistrationPetitions.Count > 0)
                {
                    // save registration
                    _registrationRepository.EnsurePersistent(registration);

                    if (registration.RegistrationParticipations.Count > 0)
                    {
                        try
                        {
                            // add email for registration into queue
                            _emailService.QueueRegistrationConfirmation(registration);
                        }
                        catch (Exception ex)
                        {
                            _errorService.ReportError(ex);
                            Message += StaticValues.Student_Email_Problem;
                        }
                        Message += StaticValues.Student_Register_Successful;
                    }

                    if (registration.RegistrationPetitions.Count > 0)
                    {
                        try
                        {
                            _emailService.QueueRegistrationPetition(registration);
                        }
                        catch (Exception ex)
                        {
                            _errorService.ReportError(ex);
                            Message += StaticValues.Student_Email_Problem;
                        }
                        Message += StaticValues.Student_RegistrationPetition_Successful;
                    }

                    // redirect to exit survey if needed
                    var surveyRedirect = SurveyRedirector(registration);
                    if (surveyRedirect != null) return surveyRedirect;
                }

                // exit survey not specified, just display the registration
                return this.RedirectToAction(a => a.DisplayRegistration());
            }

            var viewModel = RegistrationModel.Create(repository: Repository, ceremonies: GetEligibleCeremonies(student), student: student, ceremonyParticipations: registrationModel.CeremonyParticipations, registration: registration);
            return View(viewModel);
        }
        public ActionResult EditRegistration(int id /* Registration Id */, RegistrationPostModel registrationPostModel)
        {
            ModelState.Clear();
            var registrationToEdit = _registrationRepository.GetNullableById(id);
            var student = GetCurrentStudent();

            if (registrationToEdit == null || registrationToEdit.Student != student)
            {
                Message = StaticValues.Student_No_Registration_Found;
                return this.RedirectToAction(a => a.Index());
            }
            if (!registrationToEdit.RegistrationParticipations.Any(a => a.Ceremony.CanRegister()))
            {
                return this.RedirectToAction<ErrorController>(a => a.NotOpen());
            }

            //TODO: Should probably check if this is changed to a petition and if so, required fields are completed (Look at Register above)

            _registrationPopulator.UpdateRegistration(registrationToEdit, registrationPostModel, student, ModelState);

            registrationToEdit.TransferValidationMessagesTo(ModelState);

            if (ModelState.IsValid)
            {
                //Save the registration
                _registrationRepository.EnsurePersistent(registrationToEdit);

                Message = StaticValues.Student_Register_Edit_Successful;

                try
                {
                    _emailService.QueueRegistrationConfirmation(registrationToEdit);
                }
                catch (Exception ex)
                {
                    _errorService.ReportError(ex);
                    Message += StaticValues.Student_Email_Problem;
                }

                //return this.RedirectToAction(x => x.RegistrationConfirmation(registrationToEdit.Id, null));
                return this.RedirectToAction(a => a.DisplayRegistration());
            }

            var viewModel = RegistrationModel.Create(repository: Repository, ceremonies: GetEligibleCeremonies(student), student: student, registration: registrationToEdit, edit: true);
            return View(viewModel);
        }