From 6a43972267b28cbb99265f4f2333342236e4eb19 Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 8 Oct 2013 12:05:24 +1100 Subject: [PATCH] Adds content service tests for tags --- src/Umbraco.Core/Models/PropertyExtensions.cs | 11 +- .../Repositories/ContentRepository.cs | 55 +++++--- .../Services/ContentServiceTests.cs | 122 ++++++++++++++++++ 3 files changed, 164 insertions(+), 24 deletions(-) diff --git a/src/Umbraco.Core/Models/PropertyExtensions.cs b/src/Umbraco.Core/Models/PropertyExtensions.cs index 7474d9fb92..519b022d39 100644 --- a/src/Umbraco.Core/Models/PropertyExtensions.cs +++ b/src/Umbraco.Core/Models/PropertyExtensions.cs @@ -20,6 +20,12 @@ namespace Umbraco.Core.Models return property.ToXml(ApplicationContext.Current.Services.DataTypeService); } + /// + /// Creates the xml representation for the object + /// + /// to generate xml for + /// + /// Xml of the property and its value internal static XElement ToXml(this Property property, IDataTypeService dataTypeService) { var nodeName = UmbracoConfig.For.UmbracoSettings().Content.UseLegacyXmlSchema ? "data" : property.Alias.ToSafeAlias(); @@ -33,10 +39,7 @@ namespace Umbraco.Core.Models xElement.Add(a); } - // * Get the XML result from the property editor if there is one, otherwise just construct a simple - // XML construct from the value returned from the Property Editor. - // More details discussed here: https://groups.google.com/forum/?fromgroups=#!topic/umbraco-dev/fieWZzHj7oY - + //Get the property editor for thsi property and let it convert it to the xml structure var propertyEditor = PropertyEditorResolver.Current.GetByAlias(property.PropertyType.PropertyEditorAlias); if (propertyEditor != null) { diff --git a/src/Umbraco.Core/Persistence/Repositories/ContentRepository.cs b/src/Umbraco.Core/Persistence/Repositories/ContentRepository.cs index f6ca49c57b..eebb332f65 100644 --- a/src/Umbraco.Core/Persistence/Repositories/ContentRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/ContentRepository.cs @@ -319,6 +319,12 @@ namespace Umbraco.Core.Persistence.Repositories property.Id = keyDictionary[property.PropertyTypeId]; } + //lastly, check if we are a creating a published version , then update the tags table + if (entity.Published) + { + UpdatePropertyTags(entity); + } + ((ICanBeDirty)entity).ResetDirtyProperties(); } @@ -458,26 +464,7 @@ namespace Umbraco.Core.Persistence.Repositories //lastly, check if we are a newly published version and then update the tags table if (isNewPublishedVersion) { - foreach (var tagProp in entity.Properties.Where(x => x.TagSupport.Enable)) - { - if (tagProp.TagSupport.Behavior == PropertyTagBehavior.Remove) - { - //remove the specific tags - _tagRepository.RemoveTagsFromProperty( - entity.Id, - tagProp.Alias, - tagProp.TagSupport.Tags.Select(x => new Tag {Text = x.Item1, Group = x.Item2})); - } - else - { - //assign the tags - _tagRepository.AssignTagsToProperty( - entity.Id, - tagProp.Alias, - tagProp.TagSupport.Tags.Select(x => new Tag {Text = x.Item1, Group = x.Item2}), - tagProp.TagSupport.Behavior == PropertyTagBehavior.Replace); - } - } + UpdatePropertyTags(entity); } ((ICanBeDirty)entity).ResetDirtyProperties(); @@ -576,6 +563,34 @@ namespace Umbraco.Core.Persistence.Repositories #endregion + /// + /// Updates the tag repository with any tag enabled properties and their values + /// + /// + private void UpdatePropertyTags(IContentBase entity) + { + foreach (var tagProp in entity.Properties.Where(x => x.TagSupport.Enable)) + { + if (tagProp.TagSupport.Behavior == PropertyTagBehavior.Remove) + { + //remove the specific tags + _tagRepository.RemoveTagsFromProperty( + entity.Id, + tagProp.Alias, + tagProp.TagSupport.Tags.Select(x => new Tag { Text = x.Item1, Group = x.Item2 })); + } + else + { + //assign the tags + _tagRepository.AssignTagsToProperty( + entity.Id, + tagProp.Alias, + tagProp.TagSupport.Tags.Select(x => new Tag { Text = x.Item1, Group = x.Item2 }), + tagProp.TagSupport.Behavior == PropertyTagBehavior.Replace); + } + } + } + /// /// Private method to create a content object from a DocumentDto, which is used by Get and GetByVersion. /// diff --git a/src/Umbraco.Tests/Services/ContentServiceTests.cs b/src/Umbraco.Tests/Services/ContentServiceTests.cs index e15f05817d..e3b6f15a81 100644 --- a/src/Umbraco.Tests/Services/ContentServiceTests.cs +++ b/src/Umbraco.Tests/Services/ContentServiceTests.cs @@ -35,6 +35,128 @@ namespace Umbraco.Tests.Services //TODO Add test to verify there is only ONE newest document/content in cmsDocument table after updating. //TODO Add test to delete specific version (with and without deleting prior versions) and versions by date. + [Test] + public void Does_Not_Create_Tag_Data_For_Non_Published_Version() + { + //Arrange + var contentService = ServiceContext.ContentService; + var contentTypeService = ServiceContext.ContentTypeService; + var contentType = MockedContentTypes.CreateSimpleContentType("umbMandatory", "Mandatory Doc Type", true); + contentType.PropertyGroups.First().PropertyTypes.Add( + new PropertyType("test", DataTypeDatabaseType.Ntext) + { + Alias = "tags", + DataTypeDefinitionId = 1041 + }); + contentTypeService.Save(contentType); + var content = MockedContent.CreateSimpleContent(contentType, "Tagged content", 1046); + content.SetTags("tags", new[] { "hello", "world", "some", "tags" }, true); + contentService.Publish(content); + + // Act + content.SetTags("tags", new[] { "another", "world" }, false); + contentService.Save(content); + + // Assert + + //the value will have changed but the tags db table will not have + Assert.AreEqual(5, content.Properties["tags"].Value.ToString().Split(',').Distinct().Count()); + var propertyTypeId = contentType.PropertyTypes.Single(x => x.Alias == "tags").Id; + Assert.AreEqual(4, DatabaseContext.Database.ExecuteScalar( + "SELECT COUNT(*) FROM cmsTagRelationship WHERE nodeId=@nodeId AND propertyTypeId=@propTypeId", + new { nodeId = content.Id, propTypeId = propertyTypeId })); + } + + [Test] + public void Can_Replace_Tag_Data_To_Published_Content() + { + //Arrange + var contentService = ServiceContext.ContentService; + var contentTypeService = ServiceContext.ContentTypeService; + var contentType = MockedContentTypes.CreateSimpleContentType("umbMandatory", "Mandatory Doc Type", true); + contentType.PropertyGroups.First().PropertyTypes.Add( + new PropertyType("test", DataTypeDatabaseType.Ntext) + { + Alias = "tags", + DataTypeDefinitionId = 1041 + }); + contentTypeService.Save(contentType); + + var content = MockedContent.CreateSimpleContent(contentType, "Tagged content", 1046); + + + // Act + content.SetTags("tags", new[] { "hello", "world", "some", "tags" }, true); + contentService.Publish(content); + + // Assert + Assert.AreEqual(4, content.Properties["tags"].Value.ToString().Split(',').Distinct().Count()); + var propertyTypeId = contentType.PropertyTypes.Single(x => x.Alias == "tags").Id; + Assert.AreEqual(4, DatabaseContext.Database.ExecuteScalar( + "SELECT COUNT(*) FROM cmsTagRelationship WHERE nodeId=@nodeId AND propertyTypeId=@propTypeId", + new {nodeId = content.Id, propTypeId = propertyTypeId})); + } + + [Test] + public void Can_Append_Tag_Data_To_Published_Content() + { + //Arrange + var contentService = ServiceContext.ContentService; + var contentTypeService = ServiceContext.ContentTypeService; + var contentType = MockedContentTypes.CreateSimpleContentType("umbMandatory", "Mandatory Doc Type", true); + contentType.PropertyGroups.First().PropertyTypes.Add( + new PropertyType("test", DataTypeDatabaseType.Ntext) + { + Alias = "tags", + DataTypeDefinitionId = 1041 + }); + contentTypeService.Save(contentType); + var content = MockedContent.CreateSimpleContent(contentType, "Tagged content", 1046); + content.SetTags("tags", new[] { "hello", "world", "some", "tags" }, true); + contentService.Publish(content); + + // Act + content.SetTags("tags", new[] { "another", "world" }, false); + contentService.Publish(content); + + // Assert + Assert.AreEqual(5, content.Properties["tags"].Value.ToString().Split(',').Distinct().Count()); + var propertyTypeId = contentType.PropertyTypes.Single(x => x.Alias == "tags").Id; + Assert.AreEqual(5, DatabaseContext.Database.ExecuteScalar( + "SELECT COUNT(*) FROM cmsTagRelationship WHERE nodeId=@nodeId AND propertyTypeId=@propTypeId", + new { nodeId = content.Id, propTypeId = propertyTypeId })); + } + + [Test] + public void Can_Remove_Tag_Data_To_Published_Content() + { + //Arrange + var contentService = ServiceContext.ContentService; + var contentTypeService = ServiceContext.ContentTypeService; + var contentType = MockedContentTypes.CreateSimpleContentType("umbMandatory", "Mandatory Doc Type", true); + contentType.PropertyGroups.First().PropertyTypes.Add( + new PropertyType("test", DataTypeDatabaseType.Ntext) + { + Alias = "tags", + DataTypeDefinitionId = 1041 + }); + contentTypeService.Save(contentType); + var content = MockedContent.CreateSimpleContent(contentType, "Tagged content", 1046); + content.SetTags("tags", new[] { "hello", "world", "some", "tags" }, true); + contentService.Publish(content); + + // Act + content.RemoveTags("tags", new[] { "some", "world" }); + contentService.Publish(content); + + // Assert + Assert.AreEqual(2, content.Properties["tags"].Value.ToString().Split(',').Distinct().Count()); + var propertyTypeId = contentType.PropertyTypes.Single(x => x.Alias == "tags").Id; + Assert.AreEqual(2, DatabaseContext.Database.ExecuteScalar( + "SELECT COUNT(*) FROM cmsTagRelationship WHERE nodeId=@nodeId AND propertyTypeId=@propTypeId", + new { nodeId = content.Id, propTypeId = propertyTypeId })); + } + [Test] public void Can_Remove_Property_Type() {