Fix media type copying and move media type service unit tests to a separate class

This commit is contained in:
Kenn Jacobsen
2018-12-11 17:00:43 +01:00
parent a11b442cfd
commit ec24ebf93c
7 changed files with 209 additions and 140 deletions

View File

@@ -160,5 +160,8 @@ namespace Umbraco.Core.Models
return result;
}
/// <inheritdoc />
IContentType IContentType.DeepCloneWithResetIdentities(string newAlias) => (IContentType)DeepCloneWithResetIdentities(newAlias);
}
}

View File

@@ -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)

View File

@@ -44,29 +44,7 @@ namespace Umbraco.Core.Models
/// <inheritdoc />
public override bool IsPublishing => IsPublishingConst;
/// <summary>
/// Creates a deep clone of the current entity with its identity/alias and it's property identities reset
/// </summary>
/// <returns></returns>
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;
}
/// <inheritdoc />
IMediaType IMediaType.DeepCloneWithResetIdentities(string newAlias) => (IMediaType)DeepCloneWithResetIdentities(newAlias);
}
}

View File

@@ -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

View File

@@ -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<int>();
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<int>();
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<IMedia> 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()
{

View File

@@ -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<int>();
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<int>();
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<IMedia> 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);
}
}
}

View File

@@ -132,6 +132,7 @@
<Compile Include="PublishedContent\NuCacheTests.cs" />
<Compile Include="Services\ContentServicePublishBranchTests.cs" />
<Compile Include="Services\ContentTypeServiceVariantsTests.cs" />
<Compile Include="Services\MediaTypeServiceTests.cs" />
<Compile Include="Testing\Objects\TestDataSource.cs" />
<Compile Include="Published\PublishedSnapshotTestObjects.cs" />
<Compile Include="Published\ModelTypeTests.cs" />