Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Umbraco.Core/Extensions/UdiGetterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public static GuidUdi GetUdi(this IContentBase entity)
return entity switch
{
IContent content => content.GetUdi(),
IElement element => element.GetUdi(),
IMedia media => media.GetUdi(),
IMember member => member.GetUdi(),
_ => throw new NotSupportedException($"Content base type {entity.GetType().FullName} is not supported."),
Expand All @@ -128,6 +129,20 @@ public static GuidUdi GetUdi(this IContent entity)
return new GuidUdi(entityType, entity.Key).EnsureClosed();
}

/// <summary>
/// Gets the entity identifier of the entity.
/// </summary>
/// <param name="entity">The entity.</param>
/// <returns>
/// The entity identifier of the entity.
/// </returns>
public static GuidUdi GetUdi(this IElement entity)
{
ArgumentNullException.ThrowIfNull(entity);

return new GuidUdi(Constants.UdiEntityType.Element, entity.Key).EnsureClosed();
}

/// <summary>
/// Gets the entity identifier of the entity.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Umbraco.

Check notice on line 1 in tests/Umbraco.Tests.UnitTests/Umbraco.Core/Extensions/UdiGetterExtensionsTests.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Code Health Review (release/18.0)

✅ Getting better: String Heavy Function Arguments

The ratio of strings in function arguments decreases from 57.14% to 56.82%, threshold = 39.0%. The functions in this file have a high ratio of strings as arguments. Avoid adding more.
// See LICENSE for more details.

using NUnit.Framework;
Expand All @@ -18,6 +18,7 @@
[TestCase(Constants.ObjectTypes.Strings.DataType, "6ad82c70-685c-4e04-9b36-d81bd779d16f", "umb://data-type-container/6ad82c70685c4e049b36d81bd779d16f")]
[TestCase(Constants.ObjectTypes.Strings.DocumentBlueprint, "6ad82c70-685c-4e04-9b36-d81bd779d16f", "umb://document-blueprint-container/6ad82c70685c4e049b36d81bd779d16f")]
[TestCase(Constants.ObjectTypes.Strings.DocumentType, "6ad82c70-685c-4e04-9b36-d81bd779d16f", "umb://document-type-container/6ad82c70685c4e049b36d81bd779d16f")]
[TestCase(Constants.ObjectTypes.Strings.Element, "6ad82c70-685c-4e04-9b36-d81bd779d16f", "umb://element-container/6ad82c70685c4e049b36d81bd779d16f")]
[TestCase(Constants.ObjectTypes.Strings.MediaType, "6ad82c70-685c-4e04-9b36-d81bd779d16f", "umb://media-type-container/6ad82c70685c4e049b36d81bd779d16f")]
[TestCase(Constants.ObjectTypes.Strings.MemberType, "6ad82c70-685c-4e04-9b36-d81bd779d16f", "umb://member-type-container/6ad82c70685c4e049b36d81bd779d16f")]
public void GetUdiForEntityContainer(Guid containedObjectType, Guid key, string expected)
Expand Down Expand Up @@ -169,6 +170,24 @@
Assert.AreEqual(expected, udi.ToString());
}

[TestCase("6ad82c70-685c-4e04-9b36-d81bd779d16f", "umb://element/6ad82c70685c4e049b36d81bd779d16f")]
public void GetUdiForElement(Guid key, string expected)
{
Element entity = new ElementBuilder()
.WithKey(key)
.WithContentType(ContentTypeBuilder.CreateBasicContentType())
.Build();

Udi udi = entity.GetUdi();
Assert.AreEqual(expected, udi.ToString());

udi = ((IContentBase)entity).GetUdi();
Assert.AreEqual(expected, udi.ToString());

udi = ((IEntity)entity).GetUdi();
Assert.AreEqual(expected, udi.ToString());
}

[TestCase("en-US", "umb://language/en-US")]
[TestCase("en", "umb://language/en")]
public void GetUdiForLanguage(string isoCode, string expected)
Expand Down
Loading