-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check if element type is as requested (#15974)
--------- Co-authored-by: Mike Alhayek <mike@crestapps.com>
- Loading branch information
1 parent
1ba8423
commit f5542fa
Showing
2 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
test/OrchardCore.Tests/ContentManagement/ContentElementTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |