-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve generated content types part 1
- Loading branch information
1 parent
9e6f820
commit 5f3fad9
Showing
10 changed files
with
177 additions
and
5 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
ToSic.Eav.Core.Tests/Data/ContentTypeFactoryTests/ContentTypeFactoryClassTests.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,40 @@ | ||
using System; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using ToSic.Eav.Data; | ||
using ToSic.Eav.Data.Build; | ||
using ToSic.Testing.Shared; | ||
|
||
namespace ToSic.Eav.Core.Tests.Data.ContentTypeFactoryTests; | ||
|
||
[TestClass] | ||
public class ContentTypeFactoryClassTests: TestBaseEavCore | ||
{ | ||
private ContentTypeFactory Factory() => GetService<ContentTypeFactory>(); | ||
|
||
[TestMethod] | ||
public void Basic() => Assert.IsNotNull(Factory()); | ||
|
||
[TestMethod] | ||
public void Create_NoSpecs() | ||
{ | ||
var x = Factory().Create(typeof(TestTypeBasic)); | ||
Assert.AreEqual(nameof(TestTypeBasic), x.Name); | ||
Assert.AreEqual(Scopes.Default, x.Scope); | ||
Assert.AreEqual(Guid.Empty.ToString(), x.NameId); | ||
Assert.AreEqual(ContentTypeFactory.NoAppId, x.AppId); | ||
Assert.AreEqual(null, GetDescription(x)); | ||
} | ||
|
||
[TestMethod] | ||
public void Create_WithSpecs() | ||
{ | ||
var x = Factory().Create(typeof(TestTypeWithSpecs)); | ||
Assert.AreEqual(TestTypeWithSpecs.SpecName, x.Name); | ||
Assert.AreEqual(TestTypeWithSpecs.SpecScope, x.Scope); | ||
Assert.AreEqual(TestTypeWithSpecs.SpecGuid, x.NameId); | ||
Assert.AreEqual(ContentTypeFactory.NoAppId, x.AppId); | ||
Assert.AreEqual(TestTypeWithSpecs.SpecDescription, GetDescription(x)); | ||
} | ||
|
||
private static string GetDescription(IContentType type) => type.Metadata.Description?.Get<string>(nameof(ContentTypeDetails.Description)); | ||
} |
9 changes: 9 additions & 0 deletions
9
ToSic.Eav.Core.Tests/Data/ContentTypeFactoryTests/TestTypeBasic.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,9 @@ | ||
namespace ToSic.Eav.Core.Tests.Data.ContentTypeFactoryTests; | ||
|
||
/// <summary> | ||
/// Very basic test type, without additional decoration. | ||
/// </summary> | ||
internal class TestTypeBasic | ||
{ | ||
public string Name { get; set; } | ||
} |
14 changes: 14 additions & 0 deletions
14
ToSic.Eav.Core.Tests/Data/ContentTypeFactoryTests/TestTypeWithSpecs.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,14 @@ | ||
using ToSic.Eav.Data.ContentTypes.CodeAttributes; | ||
|
||
namespace ToSic.Eav.Core.Tests.Data.ContentTypeFactoryTests; | ||
|
||
[ContentTypeSpecs(Name = SpecName, Guid = SpecGuid, Scope = SpecScope, Description = SpecDescription)] | ||
internal class TestTypeWithSpecs | ||
{ | ||
internal const string SpecName = "TestTypeWithSpecsModified"; | ||
internal const string SpecGuid = "501ee043-1070-4cbc-a07b-8274f24bf5ea"; | ||
internal const string SpecScope = "DemoScope"; | ||
internal const string SpecDescription = "This is a test type with specs"; | ||
|
||
public string Name { get; set; } | ||
} |
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
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
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
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,55 @@ | ||
using ToSic.Eav.Data.ContentTypes.CodeAttributes; | ||
using ToSic.Eav.Plumbing; | ||
|
||
namespace ToSic.Eav.Data.Build; | ||
|
||
public class ContentTypeFactory(ContentTypeBuilder ctBuilder, ContentTypeAttributeBuilder ctAttributeBuilder, EntityBuilder entityBuilder, AttributeBuilder attributeBuilder) | ||
{ | ||
public const int NoAppId = -1; | ||
|
||
public IContentType Create(Type type) => Create(type, null, null, null); | ||
|
||
private IContentType Create(Type type, string name = default, string nameId = default, string scope = default, int appId = NoAppId) | ||
{ | ||
var ctSpecs = type.GetDirectlyAttachedAttribute<ContentTypeSpecsAttribute>(); | ||
var ctName = name ?? ctSpecs?.Name ?? type.Name; | ||
var ctNameId = nameId ?? ctSpecs?.Guid.NullOrGetWith(g => Guid.TryParse(g, out var guid) ? guid.ToString() : null) ?? Guid.Empty.ToString(); | ||
var ctScope = scope ?? ctSpecs?.Scope ?? Scopes.Default; | ||
|
||
// Must be null if no metadata | ||
var ctMetadata = ContentTypeDetails(ctSpecs?.Description)?.ToListOfOne(); | ||
|
||
var contentType = ctBuilder.Create( | ||
appId, | ||
name: ctName, | ||
nameId: ctNameId, | ||
scope: ctScope, | ||
id: 0, | ||
metadataItems: ctMetadata, | ||
isDynamic: true | ||
); | ||
return contentType; | ||
} | ||
|
||
/// <summary> | ||
/// Generate a details entity for a content type. | ||
/// Most properties like icon etc. are not important, so ATM it only does: | ||
/// - Description | ||
/// </summary> | ||
/// <param name="description"></param> | ||
/// <returns></returns> | ||
private IEntity ContentTypeDetails(string description) | ||
{ | ||
if (description == null) | ||
return null; | ||
|
||
// All props | ||
var dic = new Dictionary<string, object> { { nameof(Data.ContentTypeDetails.Description), description } }; | ||
var attributes = attributeBuilder.Create(dic); | ||
|
||
// Create a Description entity | ||
var entity = entityBuilder.Create(NoAppId, ctBuilder.Transient(NoAppId, Data.ContentTypeDetails.ContentTypeTypeName, Data.ContentTypeDetails.ContentTypeTypeName), attributes: attributes); | ||
return entity; | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
ToSic.Eav.Core/Data/ContentTypes/CodeAttributes/ContentTypeAttributeAttribute.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,12 @@ | ||
namespace ToSic.Eav.Data.ContentTypes.CodeAttributes; | ||
|
||
[PrivateApi("WIP")] | ||
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] | ||
public class ContentTypeAttributeAttribute: Attribute | ||
{ | ||
public string Name { get; set; } | ||
public ValueTypes Type { get; set; } | ||
public bool IsTitle { get; set; } | ||
public int SortOrder { get; set; } | ||
public string Description { get; set; } | ||
} |
26 changes: 26 additions & 0 deletions
26
ToSic.Eav.Core/Data/ContentTypes/CodeAttributes/ContentTypeSpecsAttribute.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,26 @@ | ||
namespace ToSic.Eav.Data.ContentTypes.CodeAttributes; | ||
|
||
[PrivateApi("WIP")] | ||
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] | ||
public class ContentTypeSpecsAttribute: Attribute | ||
{ | ||
/// <summary> | ||
/// Content Type Guid, as a string, because GUIDs are not supported in attributes | ||
/// </summary> | ||
public string Guid { get; set; } | ||
|
||
/// <summary> | ||
/// Content type name. | ||
/// </summary> | ||
public string Name { get; set; } | ||
|
||
/// <summary> | ||
/// Content type description. | ||
/// </summary> | ||
public string Description { get; set; } | ||
|
||
/// <summary> | ||
/// Content Type Scope - if blank, will default to "Default" | ||
/// </summary> | ||
public string Scope { get; set; } | ||
} |
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