2020-12-20 08:36:11 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2018-01-15 11:32:30 +01:00
|
|
|
using Moq;
|
|
|
|
|
using NUnit.Framework;
|
2021-02-12 12:53:00 +01:00
|
|
|
using Umbraco.Cms.Core.Models;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core.Models.Entities;
|
2021-02-10 14:45:44 +01:00
|
|
|
using Umbraco.Cms.Tests.Common.Builders;
|
|
|
|
|
using Umbraco.Cms.Tests.Common.Builders.Extensions;
|
2018-01-15 11:32:30 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Models;
|
2020-04-16 12:18:06 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[TestFixture]
|
|
|
|
|
public class PathValidationTests
|
|
|
|
|
{
|
|
|
|
|
[SetUp]
|
|
|
|
|
public void SetUp() => _builder = new EntitySlimBuilder();
|
2018-01-15 11:32:30 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
private EntitySlimBuilder _builder;
|
2018-01-15 11:32:30 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Validate_Path()
|
|
|
|
|
{
|
|
|
|
|
var entity = _builder
|
|
|
|
|
.WithoutIdentity()
|
|
|
|
|
.Build();
|
2018-01-15 11:32:30 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// it's empty with no id so we need to allow it
|
|
|
|
|
Assert.IsTrue(entity.ValidatePath());
|
2018-01-15 11:32:30 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
entity.Id = 1234;
|
2018-01-15 11:32:30 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// it has an id but no path, so we can't allow it
|
|
|
|
|
Assert.IsFalse(entity.ValidatePath());
|
2018-01-15 11:32:30 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
entity.Path = "-1";
|
2018-01-15 11:32:30 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// invalid path
|
|
|
|
|
Assert.IsFalse(entity.ValidatePath());
|
2018-01-15 11:32:30 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
entity.Path = string.Concat("-1,", entity.Id);
|
2018-01-15 11:32:30 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// valid path
|
|
|
|
|
Assert.IsTrue(entity.ValidatePath());
|
|
|
|
|
}
|
2018-01-15 11:32:30 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Ensure_Path_Throws_Without_Id()
|
|
|
|
|
{
|
|
|
|
|
var entity = _builder
|
|
|
|
|
.WithoutIdentity()
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
// no id assigned
|
|
|
|
|
Assert.Throws<InvalidOperationException>(() => entity.EnsureValidPath(
|
|
|
|
|
Mock.Of<ILogger<EntitySlim>>(),
|
|
|
|
|
umbracoEntity => new EntitySlim(),
|
|
|
|
|
umbracoEntity =>
|
|
|
|
|
{
|
|
|
|
|
}));
|
|
|
|
|
}
|
2018-01-15 11:32:30 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Ensure_Path_Throws_Without_Parent()
|
|
|
|
|
{
|
|
|
|
|
var entity = _builder
|
|
|
|
|
.WithId(1234)
|
|
|
|
|
.WithNoParentId()
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
// no parent found
|
|
|
|
|
Assert.Throws<NullReferenceException>(() =>
|
|
|
|
|
entity.EnsureValidPath(Mock.Of<ILogger<EntitySlim>>(), umbracoEntity => null, umbracoEntity => { }));
|
|
|
|
|
}
|
2018-01-15 11:32:30 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Ensure_Path_Entity_At_Root()
|
|
|
|
|
{
|
|
|
|
|
var entity = _builder
|
|
|
|
|
.WithId(1234)
|
|
|
|
|
.Build();
|
2018-01-15 11:32:30 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
entity.EnsureValidPath(Mock.Of<ILogger<EntitySlim>>(), umbracoEntity => null, umbracoEntity => { });
|
2018-01-15 11:32:30 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// works because it's under the root
|
|
|
|
|
Assert.AreEqual("-1,1234", entity.Path);
|
|
|
|
|
}
|
2018-01-15 11:32:30 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Ensure_Path_Entity_Valid_Parent()
|
|
|
|
|
{
|
|
|
|
|
var entity = _builder
|
|
|
|
|
.WithId(1234)
|
|
|
|
|
.WithParentId(888)
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
entity.EnsureValidPath(
|
|
|
|
|
Mock.Of<ILogger<EntitySlim>>(),
|
|
|
|
|
umbracoEntity => umbracoEntity.ParentId == 888 ? new EntitySlim { Id = 888, Path = "-1,888" } : null,
|
|
|
|
|
umbracoEntity => { });
|
|
|
|
|
|
|
|
|
|
// works because the parent was found
|
|
|
|
|
Assert.AreEqual("-1,888,1234", entity.Path);
|
|
|
|
|
}
|
2018-01-15 11:32:30 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Ensure_Path_Entity_Valid_Recursive_Parent()
|
|
|
|
|
{
|
|
|
|
|
var parentA = _builder
|
|
|
|
|
.WithId(999)
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
// Re-creating the class-level builder as we need to reset before usage when creating multiple entities.
|
|
|
|
|
_builder = new EntitySlimBuilder();
|
|
|
|
|
var parentB = _builder
|
|
|
|
|
.WithId(888)
|
|
|
|
|
.WithParentId(999)
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
_builder = new EntitySlimBuilder();
|
|
|
|
|
var parentC = _builder
|
|
|
|
|
.WithId(777)
|
|
|
|
|
.WithParentId(888)
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
_builder = new EntitySlimBuilder();
|
|
|
|
|
var entity = _builder
|
|
|
|
|
.WithId(1234)
|
|
|
|
|
.WithParentId(777)
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
IUmbracoEntity GetParent(IUmbracoEntity umbracoEntity)
|
2018-01-15 11:32:30 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
switch (umbracoEntity.ParentId)
|
2018-01-15 11:32:30 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
case 999:
|
|
|
|
|
return parentA;
|
|
|
|
|
case 888:
|
|
|
|
|
return parentB;
|
|
|
|
|
case 777:
|
|
|
|
|
return parentC;
|
|
|
|
|
case 1234:
|
|
|
|
|
return entity;
|
|
|
|
|
default:
|
|
|
|
|
return null;
|
2020-12-20 08:36:11 +01:00
|
|
|
}
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
2018-01-15 11:32:30 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// this will recursively fix all paths
|
|
|
|
|
entity.EnsureValidPath(Mock.Of<ILogger<IUmbracoEntity>>(), GetParent, umbracoEntity => { });
|
2018-01-15 11:32:30 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.AreEqual("-1,999", parentA.Path);
|
|
|
|
|
Assert.AreEqual("-1,999,888", parentB.Path);
|
|
|
|
|
Assert.AreEqual("-1,999,888,777", parentC.Path);
|
|
|
|
|
Assert.AreEqual("-1,999,888,777,1234", entity.Path);
|
2018-01-15 11:32:30 +01:00
|
|
|
}
|
2020-04-11 17:21:27 +02:00
|
|
|
}
|