Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if element type is as requested #15974

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
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ public static bool Has<TElement>(this ContentElement contentElement) where TElem
/// <returns>The content element instance or. <code>null</code> if it doesn't exist.</returns>
public static ContentElement Get(this ContentElement contentElement, Type contentElementType, string name)
{
if (contentElement.Elements.TryGetValue(name, out var element))
if (contentElement.Elements.TryGetValue(name, out var element) &&
element.GetType().IsAssignableTo(contentElementType))
{
return element;
}

var elementData = contentElement.Data[name] as JsonObject;

if (elementData is null)
{
return null;
Expand Down
69 changes: 69 additions & 0 deletions test/OrchardCore.Tests/ContentManagement/ContentElementTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System.Text.Json;
using OrchardCore.ContentManagement;
using OrchardCore.Title.Models;

namespace OrchardCore.Tests.ContentManagement;
public class ContentElementTests
{
[Fact]
public void Get_WhenCastingBaseTypeThenConcreteType_ReturnNewInstance()
{
var contentItem = new ContentItem();
var titlePart = new TitlePart
{
Title = "test"
};

contentItem.Weld(titlePart);

var json = JConvert.SerializeObject(contentItem);

var contentItem2 = JConvert.DeserializeObject<ContentItem>(json);

// act
// The order arrangement of the next two calls are important.
// First cast to ContentPart.
var contentPart = contentItem2.Get<ContentPart>(nameof(TitlePart));

// Second, cast to TitlePart.
var actualPart = contentItem2.Get<TitlePart>(nameof(TitlePart));

// assert
Assert.NotNull(contentPart);
Assert.NotNull(actualPart);

// actualPart should deserialized again, so it must not be same as contentPart.
Assert.NotSame(contentPart, actualPart);
}

[Fact]
public void Get_WhenCastingConcreteTypeThenBaseType_ReturnNewInstance()
{
var contentItem = new ContentItem();
var titlePart = new TitlePart
{
Title = "test"
};

contentItem.Weld(titlePart);

var json = JConvert.SerializeObject(contentItem);

var contentItem2 = JConvert.DeserializeObject<ContentItem>(json);

// act
// The order arrangement of the next two calls are important.
// First cast to TitlePart.
var actualPart = contentItem2.Get<TitlePart>(nameof(TitlePart));

// Second, cast to ContentPart.
var contentPart = contentItem2.Get<ContentPart>(nameof(TitlePart));

// assert
Assert.NotNull(contentPart);
Assert.NotNull(actualPart);

// contentPart should be returned from cache, so it must be same as actualPart.
Assert.Same(contentPart, actualPart);
}
}