public void ContentEditorControllerTest_All_Standard_Values_Bound()
        {
            //Arrange

            var contentEntity = CreateEntityRevision(new RegexPropertyEditor());

            var controller = new ContentEditorController(GetBackOfficeRequestContext());

            controller.InjectDependencies(new Dictionary <string, string>(), new Dictionary <string, string>
            {
                { "Name", "Demo" },
                { "SelectedTemplateId", "1042" },                                   //i know that 1042 is in our mocked template resolver
                { "UtcPublishScheduled", "2013-01-01" },
                { "UtcUnpublishScheduled", "2014-01-01" }
            }, GetBackOfficeRequestContext());

            //Act
            var result = (ViewResult)controller.EditForm(contentEntity.Item.Id, contentEntity.MetaData.Id);
            var model  = (ContentEditorModel)result.Model;

            //Assert
            Assert.AreEqual("Demo", model.Name);
            //Assert.AreEqual(new HiveId(1042), model.SelectedTemplateId);
            Assert.AreEqual(new DateTime(2013, 1, 1, 0, 0, 0), model.UtcPublishScheduled);
            Assert.AreEqual(new DateTime(2014, 1, 1, 0, 0, 0), model.UtcUnpublishScheduled);
        }
        public void ContentEditorControllerTests_Invalid_Model_State_When_Missing_Required_Values()
        {
            //Arrange

            var contentEntity = CreateEntityRevision(new RegexPropertyEditor());

            var controller = new ContentEditorController(GetBackOfficeRequestContext());

            controller.InjectDependencies(new Dictionary <string, string>(), new Dictionary <string, string>
            {
                { "Name", "" }
            }, GetBackOfficeRequestContext());

            //Act
            var result = (ViewResult)controller.EditForm(contentEntity.Item.Id, contentEntity.MetaData.Id);
            var model  = (ContentEditorModel)result.Model;

            //Assert
            Assert.IsFalse(controller.ModelState.IsValidField("Name"));
        }
        public void ContentEditorControllerTests_Content_Unpublished()
        {
            //Arrange

            var contentEntity = CreateEntityRevision(new RegexPropertyEditor(),
                                                     c =>
            {
                c.MetaData.UtcStatusChanged = DateTime.Now;
                c.MetaData.StatusType       = FixedStatusTypes.Published;
            });

            var controller = new ContentEditorController(GetBackOfficeRequestContext());

            controller.InjectDependencies(new Dictionary <string, string>(), new Dictionary <string, string>
            {
                { "Name", "hello" },
                { "submit.Unpublish", "Unpublish" }                                  //set unpublish flag
            }, GetBackOfficeRequestContext(), false);

            //Act

            var result = controller.EditForm(contentEntity.Item.Id, contentEntity.MetaData.Id);

            //Assert

            Assert.IsTrue(result is RedirectToRouteResult);

            using (var uow = RebelApplicationContext.Hive.OpenReader <IContentStore>())
            {
                var snapshot = uow.Repositories.Revisions.GetLatestSnapshot <TypedEntity>(contentEntity.Item.Id);
                if (snapshot == null)
                {
                    Assert.Fail("no snapshot found");
                }

                var contentViewModel = RebelApplicationContext.FrameworkContext.TypeMappers.Map <EntitySnapshot <TypedEntity>, ContentEditorModel>(snapshot);
                Assert.AreEqual(null, contentViewModel.UtcPublishedDate);
                var lastUnpublished = snapshot.GetLatestDate(FixedStatusTypes.Unpublished);
                Assert.IsTrue(DateTimeOffset.UtcNow.Subtract(lastUnpublished) < new TimeSpan(0, 1, 0));
            }
        }
        public void ContentEditorControllerTests_Content_Saved()
        {
            //Arrange

            var contentEntity = CreateEntityRevision(new RegexPropertyEditor());

            var controller = new ContentEditorController(GetBackOfficeRequestContext());

            controller.InjectDependencies(new Dictionary <string, string>(), new Dictionary <string, string>
            {
                { "Name", "Demo" },
                { contentEntity.Item.Attributes[NodeNameAttributeDefinition.AliasValue].Id.GetHtmlId() + ".Name", "test" },
                { contentEntity.Item.Attributes[NodeNameAttributeDefinition.AliasValue].Id.GetHtmlId() + ".UrlName", "test" },
                { contentEntity.Item.Attributes["bodyText"].Id.GetHtmlId() + ".Value", "1234" },
                { contentEntity.Item.Attributes["siteName"].Id.GetHtmlId() + ".Value", "4321" },
                { "submit.Save", "Save" }                                  //set save flag
            }, GetBackOfficeRequestContext(), false);

            //Act

            var result = controller.EditForm(contentEntity.Item.Id, contentEntity.MetaData.Id);

            //Assert

            Assert.IsTrue(result is RedirectToRouteResult);

            using (var uow = RebelApplicationContext.Hive.OpenReader <IContentStore>())
            {
                var snapshot = uow.Repositories.Revisions.GetLatestSnapshot <TypedEntity>(contentEntity.Item.Id);
                if (snapshot == null)
                {
                    Assert.Fail("no snapshot found");
                }

                Assert.AreNotEqual(contentEntity.MetaData.Id, snapshot.Revision.MetaData.Id);
                Assert.IsTrue(contentEntity.MetaData.UtcCreated < snapshot.Revision.MetaData.UtcCreated);
                var contentViewModel = RebelApplicationContext.FrameworkContext.TypeMappers.Map <EntitySnapshot <TypedEntity>, ContentEditorModel>(snapshot);
                Assert.AreEqual(null, contentViewModel.UtcPublishedDate);
            }
        }
        public void ContentEditorControllerTests_Regex_Property_Bound_And_Invalidated()
        {
            //Arrange

            var contentEntity   = CreateEntityRevision(new RegexPropertyEditor());
            var customAttribute = contentEntity.Item.Attributes.Last();
            var controller      = new ContentEditorController(GetBackOfficeRequestContext());

            controller.InjectDependencies(new Dictionary <string, string>(), new Dictionary <string, string>
            {
                //there will be 2 attributes, one for node name and a custom one
                { customAttribute.Id.GetHtmlId() + ".Value", "asd" }
            }, GetBackOfficeRequestContext());

            //Act

            var result = (ViewResult)controller.EditForm(contentEntity.Item.Id, contentEntity.MetaData.Id);
            var model  = (ContentEditorModel)result.Model;

            //Assert

            Assert.IsFalse(controller.ModelState.IsValidField(customAttribute.Id.GetHtmlId() + ".Value"));
        }
        public void ContentEditorControllerTests_Regex_Property_Bound_And_Validated()
        {
            //Arrange

            var contentEntity   = CreateEntityRevision(new RegexPropertyEditor());
            var customAttribute = contentEntity.Item.Attributes.Last();
            var controller      = new ContentEditorController(GetBackOfficeRequestContext());

            controller.InjectDependencies(new Dictionary <string, string>(), new Dictionary <string, string>
            {
                { customAttribute.Id.GetHtmlId() + ".Value", "123" }
            }, GetBackOfficeRequestContext());

            //Act

            var result = (ViewResult)controller.EditForm(contentEntity.Item.Id, contentEntity.MetaData.Id);
            var model  = (ContentEditorModel)result.Model;

            //Assert

            Assert.IsTrue(controller.ModelState.IsValidField(customAttribute.Id.GetHtmlId() + ".Value"));
            Assert.AreEqual("123", model.Properties.Single(x => x.Alias == customAttribute.AttributeDefinition.Alias).PropertyEditorModel.Value);
        }
        public void ContentEditorControllerTest_All_Standard_Values_Bound()
        {
            //Arrange

            var contentEntity = CreateEntityRevision(new RegexPropertyEditor());

            var controller = new ContentEditorController(GetBackOfficeRequestContext());
            controller.InjectDependencies(new Dictionary<string, string>(), new Dictionary<string, string>
                                              {
                                                  { "Name", "Demo"},
                                                  { "SelectedTemplateId", "1042" }, //i know that 1042 is in our mocked template resolver
                                                  { "UtcPublishScheduled", "2013-01-01" },
                                                  { "UtcUnpublishScheduled", "2014-01-01" }
                                              }, GetBackOfficeRequestContext());

            //Act
            var result = (ViewResult)controller.EditForm(contentEntity.Item.Id, contentEntity.MetaData.Id);
            var model = (ContentEditorModel)result.Model;

            //Assert
            Assert.AreEqual("Demo", model.Name);
            //Assert.AreEqual(new HiveId(1042), model.SelectedTemplateId);
            Assert.AreEqual(new DateTime(2013, 1, 1, 0, 0, 0), model.UtcPublishScheduled);
            Assert.AreEqual(new DateTime(2014, 1, 1, 0, 0, 0), model.UtcUnpublishScheduled);
        }
        public void ContentEditorControllerTests_Invalid_Model_State_When_Missing_Required_Values()
        {
            //Arrange

            var contentEntity = CreateEntityRevision(new RegexPropertyEditor());

            var controller = new ContentEditorController(GetBackOfficeRequestContext());
            controller.InjectDependencies(new Dictionary<string, string>(), new Dictionary<string, string>
                                              {
                                                  { "Name", ""}
                                              }, GetBackOfficeRequestContext());

            //Act
            var result = (ViewResult)controller.EditForm(contentEntity.Item.Id, contentEntity.MetaData.Id);
            var model = (ContentEditorModel)result.Model;

            //Assert
            Assert.IsFalse(controller.ModelState.IsValidField("Name"));
        }
        public void ContentEditorControllerTests_Content_Published()
        {
            //Arrange

            var contentEntity = CreateEntityRevision(new RegexPropertyEditor(),
                c =>
                {
                    c.MetaData.UtcStatusChanged = DateTime.Now;
                    c.MetaData.StatusType = FixedStatusTypes.Draft;
                });

            var controller = new ContentEditorController(GetBackOfficeRequestContext());
            controller.InjectDependencies(new Dictionary<string, string>(), new Dictionary<string, string>
                                              {
                                                  { "Name", "hello"},
                                                  { "submit.Publish", "Publish"} //set Publish flag
                                              }, GetBackOfficeRequestContext(), false);

            //Act

            var result = controller.EditForm(contentEntity.Item.Id, contentEntity.MetaData.Id);

            //Assert

            Assert.IsTrue(result is RedirectToRouteResult);

            using (var uow = UmbracoApplicationContext.Hive.OpenReader<IContentStore>())
            {
                var snapshot = uow.Repositories.Revisions.GetLatestSnapshot<TypedEntity>(contentEntity.Item.Id);
                if (snapshot == null)
                    Assert.Fail("no snapshot found");

                var contentViewModel = UmbracoApplicationContext.FrameworkContext.TypeMappers.Map<EntitySnapshot<TypedEntity>, ContentEditorModel>(snapshot);
                Assert.IsNotNull(contentViewModel.UtcPublishedDate);
                Assert.IsTrue(DateTimeOffset.UtcNow.Subtract(contentViewModel.UtcPublishedDate.Value) < new TimeSpan(0, 1, 0));
            }
        }
        public void ContentEditorControllerTests_Content_Saved()
        {
            //Arrange

            var contentEntity = CreateEntityRevision(new RegexPropertyEditor());

            var controller = new ContentEditorController(GetBackOfficeRequestContext());
            controller.InjectDependencies(new Dictionary<string, string>(), new Dictionary<string, string>
                                              {
                                                  { "Name", "Demo"},
                                                  { contentEntity.Item.Attributes[NodeNameAttributeDefinition.AliasValue].Id.GetHtmlId() + ".Name", "test"},
                                                  { contentEntity.Item.Attributes[NodeNameAttributeDefinition.AliasValue].Id.GetHtmlId() + ".UrlName", "test"},
                                                  { contentEntity.Item.Attributes["bodyText"].Id.GetHtmlId() + ".Value", "1234"},
                                                  { contentEntity.Item.Attributes["siteName"].Id.GetHtmlId() + ".Value", "4321"},                                                  
                                                  { "submit.Save", "Save"} //set save flag
                                              }, GetBackOfficeRequestContext(), false);

            //Act

            var result = controller.EditForm(contentEntity.Item.Id, contentEntity.MetaData.Id);

            //Assert

            Assert.IsTrue(result is RedirectToRouteResult);

            using (var uow = UmbracoApplicationContext.Hive.OpenReader<IContentStore>())
            {
                var snapshot = uow.Repositories.Revisions.GetLatestSnapshot<TypedEntity>(contentEntity.Item.Id);
                if (snapshot == null)
                    Assert.Fail("no snapshot found");

                Assert.AreNotEqual(contentEntity.MetaData.Id, snapshot.Revision.MetaData.Id);
                Assert.IsTrue(contentEntity.MetaData.UtcCreated < snapshot.Revision.MetaData.UtcCreated);
                var contentViewModel = UmbracoApplicationContext.FrameworkContext.TypeMappers.Map<EntitySnapshot<TypedEntity>, ContentEditorModel>(snapshot);
                Assert.AreEqual(null, contentViewModel.UtcPublishedDate);
            }
        }
        public void ContentEditorControllerTests_Regex_Property_Bound_And_Invalidated()
        {
            //Arrange

            var contentEntity = CreateEntityRevision(new RegexPropertyEditor());
            var customAttribute = contentEntity.Item.Attributes.Last();
            var controller = new ContentEditorController(GetBackOfficeRequestContext());
            controller.InjectDependencies(new Dictionary<string, string>(), new Dictionary<string, string>
                                              {
                                                  //there will be 2 attributes, one for node name and a custom one
                                                  { customAttribute.Id.GetHtmlId() + ".Value", "asd"}
                                              }, GetBackOfficeRequestContext());

            //Act

            var result = (ViewResult)controller.EditForm(contentEntity.Item.Id, contentEntity.MetaData.Id);
            var model = (ContentEditorModel)result.Model;

            //Assert

            Assert.IsFalse(controller.ModelState.IsValidField(customAttribute.Id.GetHtmlId() + ".Value"));
        }
        public void ContentEditorControllerTests_Regex_Property_Bound_And_Validated()
        {
            //Arrange

            var contentEntity = CreateEntityRevision(new RegexPropertyEditor());
            var customAttribute = contentEntity.Item.Attributes.Last();
            var controller = new ContentEditorController(GetBackOfficeRequestContext());
            controller.InjectDependencies(new Dictionary<string, string>(), new Dictionary<string, string>
                                              {
                                                  { customAttribute.Id.GetHtmlId() + ".Value", "123"}
                                              }, GetBackOfficeRequestContext());

            //Act

            var result = (ViewResult)controller.EditForm(contentEntity.Item.Id, contentEntity.MetaData.Id);
            var model = (ContentEditorModel)result.Model;

            //Assert

            Assert.IsTrue(controller.ModelState.IsValidField(customAttribute.Id.GetHtmlId() + ".Value"));
            Assert.AreEqual("123", model.Properties.Single(x => x.Alias == customAttribute.AttributeDefinition.Alias).PropertyEditorModel.Value);
        }