private PracticeHomeControllerViewModel GetViewModel()
        {
            var timeZoneId = this.DbPractice.WindowsTimeZoneId;

            var timeZone = Enum.GetValues(typeof(TypeTimeZone))
                           .Cast <TypeTimeZone>()
                           .First(tz => TimeZoneDataAttribute.GetAttributeFromEnumValue(tz).Id == timeZoneId);

            var address = this.DbPractice.Address ?? new Address();

            var viewModel = new PracticeHomeControllerViewModel
            {
                Address = new AddressViewModel
                {
                    CEP           = address.CEP,
                    City          = address.City,
                    Complement    = address.Complement,
                    Neighborhood  = address.Neighborhood,
                    StateProvince = address.StateProvince,
                    Street        = address.Street,
                },
                Email            = this.DbPractice.Email,
                Pabx             = this.DbPractice.PABX,
                PhoneAlt         = this.DbPractice.PhoneAlt,
                PhoneMain        = this.DbPractice.PhoneMain,
                PracticeName     = this.DbPractice.Name,
                PracticeTimeZone = (short)timeZone,
                SiteUrl          = this.DbPractice.SiteUrl,
                Doctors          = GetDoctorViewModelsFromPractice(this.db, this.DbPractice, this.GetPracticeLocalNow()),
                Users            = (from u in this.DbPractice.Users.OrderBy(u => u.Person.FullName).ToList()
                                    select UsersController.GetViewModel(u, u.Practice)).ToList()
            };

            return(viewModel);
        }
        public void EditPost_UserIsOwner_WithInvalidData()
        {
            PracticeHomeController homeController;
            var mr = new MockRepository();

            try
            {
                mr.SetCurrentUser_Andre_CorrectPassword();
                mr.SetRouteData_ConsultorioDrHouse_GregoryHouse(typeof(PracticeHomeController), "Edit");

                homeController = mr.CreateController <PracticeHomeController>(callOnActionExecuting: false);
            }
            catch (Exception ex)
            {
                InconclusiveInit(ex);
                return;
            }

            var viewModel = new PracticeHomeControllerViewModel
            {
                PracticeName     = "", // Cannot set practice name to empty
                PracticeTimeZone = 3
            };

            Mvc3TestHelper.SetModelStateErrors(homeController, viewModel);

            // Execute test: owner must have access to this view.
            var actionResult = Mvc3TestHelper.RunOnAuthorization(homeController, "Edit", "POST")
                               ?? Mvc3TestHelper.RunOnActionExecuting(homeController, "Edit", "POST")
                               ?? homeController.Edit(viewModel);

            // Asserts
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            Assert.AreEqual(null, ((ViewResult)actionResult).View);
        }
        public ActionResult Edit(PracticeHomeControllerViewModel formModel)
        {
            if (this.ModelState.IsValid)
            {
                formModel.PracticeName = Regex.Replace(formModel.PracticeName.Trim(), @"\s+", " ");

                this.DbPractice.Name = formModel.PracticeName;
                this.DbPractice.WindowsTimeZoneId = TimeZoneDataAttribute.GetAttributeFromEnumValue(
                    (TypeTimeZone)formModel.PracticeTimeZone).Id;

                this.DbPractice.PhoneMain = formModel.PhoneMain;
                this.DbPractice.PhoneAlt  = formModel.PhoneAlt;
                this.DbPractice.SiteUrl   = formModel.SiteUrl;
                this.DbPractice.Email     = formModel.Email;

                var address = this.DbPractice.Address;
                if (address == null)
                {
                    this.DbPractice.Address = address = new Address();
                }
                address.PracticeId    = this.DbPractice.Id;
                address.CEP           = formModel.Address.CEP;
                address.City          = formModel.Address.City;
                address.Complement    = formModel.Address.Complement;
                address.Neighborhood  = formModel.Address.Neighborhood;
                address.StateProvince = formModel.Address.StateProvince;
                address.Street        = formModel.Address.Street;

                this.db.SaveChanges();

                return(this.RedirectToAction("Index"));
            }

            return(this.View(formModel));
        }
        public void EditPost_UserIsOwner_WithValidData()
        {
            PracticeHomeController homeController;
            var mr = new MockRepository();

            try
            {
                mr.SetCurrentUser_Andre_CorrectPassword();
                mr.SetRouteData_ConsultorioDrHouse_GregoryHouse(typeof(PracticeHomeController), "Edit");

                homeController = mr.CreateController <PracticeHomeController>(callOnActionExecuting: false);
            }
            catch (Exception ex)
            {
                InconclusiveInit(ex);
                return;
            }

            var viewModel = new PracticeHomeControllerViewModel
            {
                PracticeName     = "K!",
                PracticeTimeZone = 3,
                PhoneMain        = "(32)91272552",
                Address          = new AddressViewModel
                {
                    StateProvince = "MG",
                    CEP           = "36030-000",
                    City          = "Juiz de Fora",
                    Complement    = "Sta Luzia",
                    Street        = "Rua Sem SaĆ­da",
                }
            };

            Mvc3TestHelper.SetModelStateErrors(homeController, viewModel);

            // Execute test: owner must have access to this view.
            var actionResult = Mvc3TestHelper.RunOnAuthorization(homeController, "Edit", "POST")
                               ?? Mvc3TestHelper.RunOnActionExecuting(homeController, "Edit", "POST")
                               ?? homeController.Edit(viewModel);

            // Asserts
            Assert.IsInstanceOfType(actionResult, typeof(RedirectToRouteResult));
            var redirectResult = (RedirectToRouteResult)actionResult;

            Assert.AreEqual(2, redirectResult.RouteValues.Count);
            Assert.AreEqual("practicehome", string.Format("{0}", redirectResult.RouteValues["controller"]), ignoreCase: true);
            Assert.AreEqual("Index", string.Format("{0}", redirectResult.RouteValues["action"]), ignoreCase: true);
        }