clears redirects on variation change

This commit is contained in:
Shannon
2018-10-05 11:53:35 +02:00
parent 0ae1d06c2d
commit 2e5d0f85e8
2 changed files with 65 additions and 0 deletions

View File

@@ -274,6 +274,8 @@ AND umbracoNode.id <> @id",
if (compositionBase != null && compositionBase.RemovedContentTypeKeyTracker != null &&
compositionBase.RemovedContentTypeKeyTracker.Any())
{
//TODO: Could we do the below with bulk SQL statements instead of looking everything up and then manipulating?
// find Content based on the current ContentType
var sql = Sql()
.SelectAll()
@@ -292,6 +294,7 @@ AND umbracoNode.id <> @id",
// based on the PropertyTypes that belong to the removed ContentType.
foreach (var contentDto in contentDtos)
{
//TODO: This could be done with bulk SQL statements
foreach (var propertyType in propertyTypes)
{
var nodeId = contentDto.NodeId;
@@ -408,6 +411,7 @@ AND umbracoNode.id <> @id",
{
//we've already looked up the previous version of the content type so we know it's previous variation state
MoveVariantData(entity, (ContentVariation)dtoPk.Variations, entity.Variations);
Clear301Redirects(entity);
}
//track any property types that are changing variation
@@ -492,6 +496,23 @@ AND umbracoNode.id <> @id",
DeletePropertyType(entity.Id, id);
}
/// <summary>
/// Clear any redirects associated with content for a content type
/// </summary>
private void Clear301Redirects(IContentTypeComposition contentType)
{
//first clear out any existing property data that might already exists under the default lang
var sqlSelect = Sql().Select<NodeDto>(x => x.UniqueId)
.From<NodeDto>()
.InnerJoin<ContentDto>().On<ContentDto, NodeDto>(x => x.NodeId, x => x.NodeId)
.Where<ContentDto>(x => x.ContentTypeId == contentType.Id);
var sqlDelete = Sql()
.Delete<RedirectUrlDto>()
.WhereIn((System.Linq.Expressions.Expression<Func<RedirectUrlDto, object>>)(x => x.ContentKey), sqlSelect);
Database.Execute(sqlDelete);
}
/// <summary>
/// Moves variant data for property type changes
/// </summary>

View File

@@ -24,6 +24,50 @@ namespace Umbraco.Tests.Services
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, PublishedRepositoryEvents = true)]
public class ContentTypeServiceTests : TestWithSomeContentBase
{
[Test]
public void Change_Content_Type_Variation_Clears_Redirects()
{
//create content type with a property type that varies by culture
var contentType = MockedContentTypes.CreateBasicContentType();
contentType.Variations = ContentVariation.Nothing;
var contentCollection = new PropertyTypeCollection(true);
contentCollection.Add(new PropertyType("test", ValueStorageType.Ntext)
{
Alias = "title",
Name = "Title",
Description = "",
Mandatory = false,
SortOrder = 1,
DataTypeId = -88,
Variations = ContentVariation.Nothing
});
contentType.PropertyGroups.Add(new PropertyGroup(contentCollection) { Name = "Content", SortOrder = 1 });
ServiceContext.ContentTypeService.Save(contentType);
var contentType2 = MockedContentTypes.CreateBasicContentType("test");
ServiceContext.ContentTypeService.Save(contentType2);
//create some content of this content type
IContent doc = MockedContent.CreateBasicContent(contentType);
doc.Name = "Hello1";
ServiceContext.ContentService.Save(doc);
IContent doc2 = MockedContent.CreateBasicContent(contentType2);
ServiceContext.ContentService.Save(doc2);
ServiceContext.RedirectUrlService.Register("hello/world", doc.Key);
ServiceContext.RedirectUrlService.Register("hello2/world2", doc2.Key);
Assert.AreEqual(1, ServiceContext.RedirectUrlService.GetContentRedirectUrls(doc.Key).Count());
Assert.AreEqual(1, ServiceContext.RedirectUrlService.GetContentRedirectUrls(doc2.Key).Count());
//change variation
contentType.Variations = ContentVariation.Culture;
ServiceContext.ContentTypeService.Save(contentType);
Assert.AreEqual(0, ServiceContext.RedirectUrlService.GetContentRedirectUrls(doc.Key).Count());
Assert.AreEqual(1, ServiceContext.RedirectUrlService.GetContentRedirectUrls(doc2.Key).Count());
}
[Test]
public void Change_Content_Type_From_Invariant_Variant()