From ec24ebf93c67db5214c4cde2a9cb0c9508713b61 Mon Sep 17 00:00:00 2001 From: Kenn Jacobsen Date: Tue, 11 Dec 2018 17:00:43 +0100 Subject: [PATCH] Fix media type copying and move media type service unit tests to a separate class --- src/Umbraco.Core/Models/ContentType.cs | 3 + src/Umbraco.Core/Models/ContentTypeBase.cs | 4 +- src/Umbraco.Core/Models/MediaType.cs | 26 +-- ...peServiceBaseOfTRepositoryTItemTService.cs | 4 +- .../Services/ContentTypeServiceTests.cs | 116 +---------- .../Services/MediaTypeServiceTests.cs | 195 ++++++++++++++++++ src/Umbraco.Tests/Umbraco.Tests.csproj | 1 + 7 files changed, 209 insertions(+), 140 deletions(-) create mode 100644 src/Umbraco.Tests/Services/MediaTypeServiceTests.cs diff --git a/src/Umbraco.Core/Models/ContentType.cs b/src/Umbraco.Core/Models/ContentType.cs index e6439acade..4b9831682c 100644 --- a/src/Umbraco.Core/Models/ContentType.cs +++ b/src/Umbraco.Core/Models/ContentType.cs @@ -160,5 +160,8 @@ namespace Umbraco.Core.Models return result; } + + /// + IContentType IContentType.DeepCloneWithResetIdentities(string newAlias) => (IContentType)DeepCloneWithResetIdentities(newAlias); } } diff --git a/src/Umbraco.Core/Models/ContentTypeBase.cs b/src/Umbraco.Core/Models/ContentTypeBase.cs index caa63d7526..79c2d4218b 100644 --- a/src/Umbraco.Core/Models/ContentTypeBase.cs +++ b/src/Umbraco.Core/Models/ContentTypeBase.cs @@ -492,9 +492,9 @@ namespace Umbraco.Core.Models } } - public IContentType DeepCloneWithResetIdentities(string alias) + public ContentTypeBase DeepCloneWithResetIdentities(string alias) { - var clone = (ContentType)DeepClone(); + var clone = (ContentTypeBase)DeepClone(); clone.Alias = alias; clone.Key = Guid.Empty; foreach (var propertyGroup in clone.PropertyGroups) diff --git a/src/Umbraco.Core/Models/MediaType.cs b/src/Umbraco.Core/Models/MediaType.cs index 4ae2fd190c..83e1acfbc0 100644 --- a/src/Umbraco.Core/Models/MediaType.cs +++ b/src/Umbraco.Core/Models/MediaType.cs @@ -44,29 +44,7 @@ namespace Umbraco.Core.Models /// public override bool IsPublishing => IsPublishingConst; - /// - /// Creates a deep clone of the current entity with its identity/alias and it's property identities reset - /// - /// - public new IMediaType DeepCloneWithResetIdentities(string alias) - { - var clone = (MediaType)DeepClone(); - clone.Alias = alias; - clone.Key = Guid.Empty; - foreach (var propertyGroup in clone.PropertyGroups) - { - propertyGroup.ResetIdentity(); - propertyGroup.ResetDirtyProperties(false); - } - foreach (var propertyType in clone.PropertyTypes) - { - propertyType.ResetIdentity(); - propertyType.ResetDirtyProperties(false); - } - - clone.ResetIdentity(); - clone.ResetDirtyProperties(false); - return clone; - } + /// + IMediaType IMediaType.DeepCloneWithResetIdentities(string newAlias) => (IMediaType)DeepCloneWithResetIdentities(newAlias); } } diff --git a/src/Umbraco.Core/Services/Implement/ContentTypeServiceBaseOfTRepositoryTItemTService.cs b/src/Umbraco.Core/Services/Implement/ContentTypeServiceBaseOfTRepositoryTItemTService.cs index b74abc03f7..feff81978c 100644 --- a/src/Umbraco.Core/Services/Implement/ContentTypeServiceBaseOfTRepositoryTItemTService.cs +++ b/src/Umbraco.Core/Services/Implement/ContentTypeServiceBaseOfTRepositoryTItemTService.cs @@ -594,7 +594,7 @@ namespace Umbraco.Core.Services.Implement //var originalb = (ContentTypeCompositionBase)original; // but we *know* it has to be a ContentTypeCompositionBase anyways var originalb = (ContentTypeCompositionBase) (object) original; - var clone = (TItem) originalb.DeepCloneWithResetIdentities(alias); + var clone = (TItem) (object) originalb.DeepCloneWithResetIdentities(alias); clone.Name = name; @@ -645,7 +645,7 @@ namespace Umbraco.Core.Services.Implement //var copyingb = (ContentTypeCompositionBase) copying; // but we *know* it has to be a ContentTypeCompositionBase anyways var copyingb = (ContentTypeCompositionBase) (object)copying; - copy = (TItem) copyingb.DeepCloneWithResetIdentities(alias); + copy = (TItem) (object) copyingb.DeepCloneWithResetIdentities(alias); copy.Name = copy.Name + " (copy)"; // might not be unique diff --git a/src/Umbraco.Tests/Services/ContentTypeServiceTests.cs b/src/Umbraco.Tests/Services/ContentTypeServiceTests.cs index bc0854bdb7..30067b3c5a 100644 --- a/src/Umbraco.Tests/Services/ContentTypeServiceTests.cs +++ b/src/Umbraco.Tests/Services/ContentTypeServiceTests.cs @@ -376,46 +376,6 @@ namespace Umbraco.Tests.Services Assert.IsNull(doc2.GetValue("title", "en-US")); } - [Test] - public void Deleting_Media_Type_With_Hierarchy_Of_Media_Items_Moves_Orphaned_Media_To_Recycle_Bin() - { - IMediaType contentType1 = MockedContentTypes.CreateSimpleMediaType("test1", "Test1"); - ServiceContext.MediaTypeService.Save(contentType1); - IMediaType contentType2 = MockedContentTypes.CreateSimpleMediaType("test2", "Test2"); - ServiceContext.MediaTypeService.Save(contentType2); - IMediaType contentType3 = MockedContentTypes.CreateSimpleMediaType("test3", "Test3"); - ServiceContext.MediaTypeService.Save(contentType3); - - var contentTypes = new[] { contentType1, contentType2, contentType3 }; - var parentId = -1; - - var ids = new List(); - - for (int i = 0; i < 2; i++) - { - for (var index = 0; index < contentTypes.Length; index++) - { - var contentType = contentTypes[index]; - var contentItem = MockedMedia.CreateSimpleMedia(contentType, "MyName_" + index + "_" + i, parentId); - ServiceContext.MediaService.Save(contentItem); - parentId = contentItem.Id; - - ids.Add(contentItem.Id); - } - } - - //delete the first content type, all other content of different content types should be in the recycle bin - ServiceContext.MediaTypeService.Delete(contentTypes[0]); - - var found = ServiceContext.MediaService.GetByIds(ids); - - Assert.AreEqual(4, found.Count()); - foreach (var content in found) - { - Assert.IsTrue(content.Trashed); - } - } - [Test] public void Deleting_Content_Type_With_Hierarchy_Of_Content_Items_Moves_Orphaned_Content_To_Recycle_Bin() { @@ -460,60 +420,6 @@ namespace Umbraco.Tests.Services } } - [Test] - public void Deleting_Media_Types_With_Hierarchy_Of_Media_Items_Doesnt_Raise_Trashed_Event_For_Deleted_Items() - { - MediaService.Trashed += MediaServiceOnTrashed; - - try - { - IMediaType contentType1 = MockedContentTypes.CreateSimpleMediaType("test1", "Test1"); - ServiceContext.MediaTypeService.Save(contentType1); - IMediaType contentType2 = MockedContentTypes.CreateSimpleMediaType("test2", "Test2"); - ServiceContext.MediaTypeService.Save(contentType2); - IMediaType contentType3 = MockedContentTypes.CreateSimpleMediaType("test3", "Test3"); - ServiceContext.MediaTypeService.Save(contentType3); - - var contentTypes = new[] { contentType1, contentType2, contentType3 }; - var parentId = -1; - - var ids = new List(); - - for (int i = 0; i < 2; i++) - { - for (var index = 0; index < contentTypes.Length; index++) - { - var contentType = contentTypes[index]; - var contentItem = MockedMedia.CreateSimpleMedia(contentType, "MyName_" + index + "_" + i, parentId); - ServiceContext.MediaService.Save(contentItem); - parentId = contentItem.Id; - - ids.Add(contentItem.Id); - } - } - - foreach (var contentType in contentTypes.Reverse()) - { - ServiceContext.MediaTypeService.Delete(contentType); - } - } - finally - { - MediaService.Trashed -= MediaServiceOnTrashed; - } - } - - private void MediaServiceOnTrashed(IMediaService sender, MoveEventArgs e) - { - foreach (var item in e.MoveInfoCollection) - { - //if this item doesn't exist then Fail! - var exists = ServiceContext.MediaService.GetById(item.Entity.Id); - if (exists == null) - Assert.Fail("The item doesn't exist"); - } - } - [Test] public void Deleting_Content_Types_With_Hierarchy_Of_Content_Items_Doesnt_Raise_Trashed_Event_For_Deleted_Items_1() { @@ -1078,12 +984,13 @@ namespace Umbraco.Tests.Services var metaContentType = MockedContentTypes.CreateMetaContentType(); service.Save(metaContentType); - var simpleContentType = MockedContentTypes.CreateSimpleContentType("category", "Category", metaContentType); + var simpleContentType = MockedContentTypes.CreateSimpleContentType("category", "Category", metaContentType) as IContentType; service.Save(simpleContentType); var categoryId = simpleContentType.Id; // Act var sut = simpleContentType.DeepCloneWithResetIdentities("newcategory"); + Assert.IsNotNull(sut); service.Save(sut); // Assert @@ -1115,11 +1022,12 @@ namespace Umbraco.Tests.Services var parentContentType2 = MockedContentTypes.CreateSimpleContentType("parent2", "Parent2", null, true); service.Save(parentContentType2); - var simpleContentType = MockedContentTypes.CreateSimpleContentType("category", "Category", parentContentType1, true); + var simpleContentType = MockedContentTypes.CreateSimpleContentType("category", "Category", parentContentType1, true) as IContentType; service.Save(simpleContentType); // Act var clone = simpleContentType.DeepCloneWithResetIdentities("newcategory"); + Assert.IsNotNull(clone); clone.RemoveContentType("parent1"); clone.AddContentType(parentContentType2); clone.ParentId = parentContentType2.Id; @@ -2068,22 +1976,6 @@ namespace Umbraco.Tests.Services Assert.IsNull(contentType2.Description); } - [Test] - public void Empty_Description_Is_Always_Null_After_Saving_Media_Type() - { - var service = ServiceContext.MediaTypeService; - var mediaType = MockedContentTypes.CreateSimpleMediaType("mediaType", "Media Type"); - mediaType.Description = null; - service.Save(mediaType); - - var mediaType2 = MockedContentTypes.CreateSimpleMediaType("mediaType2", "Media Type 2"); - mediaType2.Description = string.Empty; - service.Save(mediaType2); - - Assert.IsNull(mediaType.Description); - Assert.IsNull(mediaType2.Description); - } - [Test] public void Variations_In_Compositions() { diff --git a/src/Umbraco.Tests/Services/MediaTypeServiceTests.cs b/src/Umbraco.Tests/Services/MediaTypeServiceTests.cs new file mode 100644 index 0000000000..915dc5ceec --- /dev/null +++ b/src/Umbraco.Tests/Services/MediaTypeServiceTests.cs @@ -0,0 +1,195 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using NUnit.Framework; +using Umbraco.Core.Events; +using Umbraco.Core.Models; +using Umbraco.Core.Services; +using Umbraco.Core.Services.Implement; +using Umbraco.Tests.TestHelpers.Entities; +using Umbraco.Tests.Testing; + +namespace Umbraco.Tests.Services +{ + [TestFixture] + [Apartment(ApartmentState.STA)] + [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, PublishedRepositoryEvents = true)] + public class MediaTypeServiceTests : TestWithSomeContentBase + { + [Test] + public void Empty_Description_Is_Always_Null_After_Saving_Media_Type() + { + var mediaType = MockedContentTypes.CreateSimpleMediaType("mediaType", "Media Type"); + mediaType.Description = null; + ServiceContext.MediaTypeService.Save(mediaType); + + var mediaType2 = MockedContentTypes.CreateSimpleMediaType("mediaType2", "Media Type 2"); + mediaType2.Description = string.Empty; + ServiceContext.MediaTypeService.Save(mediaType2); + + Assert.IsNull(mediaType.Description); + Assert.IsNull(mediaType2.Description); + } + + [Test] + public void Deleting_Media_Type_With_Hierarchy_Of_Media_Items_Moves_Orphaned_Media_To_Recycle_Bin() + { + IMediaType contentType1 = MockedContentTypes.CreateSimpleMediaType("test1", "Test1"); + ServiceContext.MediaTypeService.Save(contentType1); + IMediaType contentType2 = MockedContentTypes.CreateSimpleMediaType("test2", "Test2"); + ServiceContext.MediaTypeService.Save(contentType2); + IMediaType contentType3 = MockedContentTypes.CreateSimpleMediaType("test3", "Test3"); + ServiceContext.MediaTypeService.Save(contentType3); + + var contentTypes = new[] { contentType1, contentType2, contentType3 }; + var parentId = -1; + + var ids = new List(); + + for (int i = 0; i < 2; i++) + { + for (var index = 0; index < contentTypes.Length; index++) + { + var contentType = contentTypes[index]; + var contentItem = MockedMedia.CreateSimpleMedia(contentType, "MyName_" + index + "_" + i, parentId); + ServiceContext.MediaService.Save(contentItem); + parentId = contentItem.Id; + + ids.Add(contentItem.Id); + } + } + + //delete the first content type, all other content of different content types should be in the recycle bin + ServiceContext.MediaTypeService.Delete(contentTypes[0]); + + var found = ServiceContext.MediaService.GetByIds(ids); + + Assert.AreEqual(4, found.Count()); + foreach (var content in found) + { + Assert.IsTrue(content.Trashed); + } + } + + [Test] + public void Deleting_Media_Types_With_Hierarchy_Of_Media_Items_Doesnt_Raise_Trashed_Event_For_Deleted_Items() + { + MediaService.Trashed += MediaServiceOnTrashed; + + try + { + IMediaType contentType1 = MockedContentTypes.CreateSimpleMediaType("test1", "Test1"); + ServiceContext.MediaTypeService.Save(contentType1); + IMediaType contentType2 = MockedContentTypes.CreateSimpleMediaType("test2", "Test2"); + ServiceContext.MediaTypeService.Save(contentType2); + IMediaType contentType3 = MockedContentTypes.CreateSimpleMediaType("test3", "Test3"); + ServiceContext.MediaTypeService.Save(contentType3); + + var contentTypes = new[] { contentType1, contentType2, contentType3 }; + var parentId = -1; + + var ids = new List(); + + for (int i = 0; i < 2; i++) + { + for (var index = 0; index < contentTypes.Length; index++) + { + var contentType = contentTypes[index]; + var contentItem = MockedMedia.CreateSimpleMedia(contentType, "MyName_" + index + "_" + i, parentId); + ServiceContext.MediaService.Save(contentItem); + parentId = contentItem.Id; + + ids.Add(contentItem.Id); + } + } + + foreach (var contentType in contentTypes.Reverse()) + { + ServiceContext.MediaTypeService.Delete(contentType); + } + } + finally + { + MediaService.Trashed -= MediaServiceOnTrashed; + } + } + + private void MediaServiceOnTrashed(IMediaService sender, MoveEventArgs e) + { + foreach (var item in e.MoveInfoCollection) + { + //if this item doesn't exist then Fail! + var exists = ServiceContext.MediaService.GetById(item.Entity.Id); + if (exists == null) + Assert.Fail("The item doesn't exist"); + } + } + + [Test] + public void Can_Copy_MediaType_By_Performing_Clone() + { + // Arrange + var mediaType = MockedContentTypes.CreateImageMediaType("Image2") as IMediaType; + ServiceContext.MediaTypeService.Save(mediaType); + + // Act + var sut = mediaType.DeepCloneWithResetIdentities("Image2_2"); + Assert.IsNotNull(sut); + ServiceContext.MediaTypeService.Save(sut); + + // Assert + Assert.That(sut.HasIdentity, Is.True); + Assert.AreEqual(mediaType.ParentId, sut.ParentId); + Assert.AreEqual(mediaType.Level, sut.Level); + Assert.AreEqual(mediaType.PropertyTypes.Count(), sut.PropertyTypes.Count()); + Assert.AreNotEqual(mediaType.Id, sut.Id); + Assert.AreNotEqual(mediaType.Key, sut.Key); + Assert.AreNotEqual(mediaType.Path, sut.Path); + Assert.AreNotEqual(mediaType.SortOrder, sut.SortOrder); + Assert.AreNotEqual(mediaType.PropertyTypes.First(x => x.Alias.Equals("umbracoFile")).Id, sut.PropertyTypes.First(x => x.Alias.Equals("umbracoFile")).Id); + Assert.AreNotEqual(mediaType.PropertyGroups.First(x => x.Name.Equals("Media")).Id, sut.PropertyGroups.First(x => x.Name.Equals("Media")).Id); + } + + [Test] + public void Can_Copy_MediaType_To_New_Parent_By_Performing_Clone() + { + // Arrange + var parentMediaType1 = MockedContentTypes.CreateSimpleMediaType("parent1", "Parent1"); + ServiceContext.MediaTypeService.Save(parentMediaType1); + var parentMediaType2 = MockedContentTypes.CreateSimpleMediaType("parent2", "Parent2", null, true); + ServiceContext.MediaTypeService.Save(parentMediaType2); + var mediaType = MockedContentTypes.CreateImageMediaType("Image2") as IMediaType; + ServiceContext.MediaTypeService.Save(mediaType); + + // Act + var clone = mediaType.DeepCloneWithResetIdentities("newcategory"); + Assert.IsNotNull(clone); + clone.RemoveContentType("parent1"); + clone.AddContentType(parentMediaType2); + clone.ParentId = parentMediaType2.Id; + ServiceContext.MediaTypeService.Save(clone); + + // Assert + Assert.That(clone.HasIdentity, Is.True); + + var clonedMediaType = ServiceContext.MediaTypeService.Get(clone.Id); + var originalMediaType = ServiceContext.MediaTypeService.Get(mediaType.Id); + + Assert.That(clonedMediaType.CompositionAliases().Any(x => x.Equals("parent2")), Is.True); + Assert.That(clonedMediaType.CompositionAliases().Any(x => x.Equals("parent1")), Is.False); + + Assert.AreEqual(clonedMediaType.Path, "-1," + parentMediaType2.Id + "," + clonedMediaType.Id); + Assert.AreEqual(clonedMediaType.PropertyTypes.Count(), originalMediaType.PropertyTypes.Count()); + + Assert.AreNotEqual(clonedMediaType.ParentId, originalMediaType.ParentId); + Assert.AreEqual(clonedMediaType.ParentId, parentMediaType2.Id); + + Assert.AreNotEqual(clonedMediaType.Id, originalMediaType.Id); + Assert.AreNotEqual(clonedMediaType.Key, originalMediaType.Key); + Assert.AreNotEqual(clonedMediaType.Path, originalMediaType.Path); + + Assert.AreNotEqual(clonedMediaType.PropertyTypes.First(x => x.Alias.StartsWith("umbracoFile")).Id, originalMediaType.PropertyTypes.First(x => x.Alias.StartsWith("umbracoFile")).Id); + Assert.AreNotEqual(clonedMediaType.PropertyGroups.First(x => x.Name.StartsWith("Media")).Id, originalMediaType.PropertyGroups.First(x => x.Name.StartsWith("Media")).Id); + } + } +} diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 7147cc8453..1286248b86 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -132,6 +132,7 @@ +