Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HideContentTypeLinksDefinition reverse handler #79
- Loading branch information
1 parent
e7a1156
commit d5c1ab8
Showing
12 changed files
with
439 additions
and
257 deletions.
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
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
95 changes: 95 additions & 0 deletions
95
...e/SPMeta2.Reverse.CSOM/ReverseHandlers/ContentTypes/HideContentTypeLinksReverseHandler.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,95 @@ | ||
using SPMeta2.Reverse.CSOM.ReverseHandlers.Base; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.SharePoint.Client; | ||
using SPMeta2.CSOM.Extensions; | ||
using SPMeta2.Definitions; | ||
using SPMeta2.Definitions.ContentTypes; | ||
using SPMeta2.ModelHosts; | ||
using SPMeta2.Models; | ||
using SPMeta2.Reverse.CSOM.ReverseHosts; | ||
using SPMeta2.Reverse.ReverseHosts; | ||
using SPMeta2.Reverse.Services; | ||
using SPMeta2.Syntax.Default; | ||
using SPMeta2.Utils; | ||
|
||
namespace SPMeta2.Reverse.CSOM.ReverseHandlers.ContentTypes | ||
{ | ||
public class HideContentTypeLinksReverseHandler : ContentTypeLinkReverseHandler | ||
{ | ||
#region properties | ||
public override Type ReverseType | ||
{ | ||
get { return typeof(HideContentTypeLinksDefinition); } | ||
} | ||
|
||
public override IEnumerable<Type> ReverseParentTypes | ||
{ | ||
get | ||
{ | ||
return new[] | ||
{ | ||
typeof(ListDefinition) | ||
}; | ||
} | ||
} | ||
|
||
|
||
#endregion | ||
|
||
#region methods | ||
|
||
public override IEnumerable<ReverseHostBase> ReverseHosts(ReverseHostBase parentHost, ReverseOptions options) | ||
{ | ||
var result = new List<ListReverseHost>(); | ||
|
||
var typedHost = parentHost.WithAssertAndCast<ListReverseHost>("reverseHost", value => value.RequireNotNull()); | ||
|
||
var list = typedHost.HostList; | ||
var context = typedHost.HostClientContext; | ||
|
||
context.Load(list); | ||
context.Load(list, l => l.ContentTypes); | ||
|
||
context.ExecuteQueryWithTrace(); | ||
|
||
result.AddRange(ApplyReverseFilters(new[] { list }, options).ToArray().Select(i => | ||
{ | ||
return ModelHostBase.Inherit<ListReverseHost>(parentHost, h => | ||
{ | ||
h.HostList = i; | ||
}); | ||
})); | ||
|
||
return result; | ||
} | ||
|
||
public override ModelNode ReverseSingleHost(object reverseHost, ReverseOptions options) | ||
{ | ||
var typedHost = (reverseHost as ListReverseHost); | ||
var item = typedHost.HostList; | ||
|
||
var def = new HideContentTypeLinksDefinition(); | ||
|
||
foreach (var ct in item.ContentTypes.ToArray() | ||
.Where(ct => ct.Hidden)) | ||
{ | ||
def.ContentTypes.Add(new ContentTypeLinkValue | ||
{ | ||
ContentTypeName = ct.Name | ||
}); | ||
} | ||
|
||
return new HideContentTypeLinksModelNode | ||
{ | ||
Options = { RequireSelfProcessing = true }, | ||
Value = def | ||
}; | ||
} | ||
|
||
#endregion | ||
} | ||
} |
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
65 changes: 65 additions & 0 deletions
65
...ta2.Reverse.Regression/Validation/ContentTypes/HideContentTypeLinksDefinitionValidator.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,65 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using SPMeta2.Definitions; | ||
using SPMeta2.Reverse.Regression.Base; | ||
using SPMeta2.Definitions.ContentTypes; | ||
using SPMeta2.Utils; | ||
using SPMeta2.Containers.Assertion; | ||
|
||
namespace SPMeta2.Reverse.Regression.Validation.ContentTypes | ||
{ | ||
public class HideContentTypeLinksDefinitionValidator : | ||
TypedReverseDefinitionValidatorBase<HideContentTypeLinksDefinition> | ||
{ | ||
|
||
public override void DeployModel(object modelHost, DefinitionBase model) | ||
{ | ||
var typedModelHost = modelHost.WithAssertAndCast<ReverseValidationModeHost>("modelHost", m => m.RequireNotNull()); | ||
|
||
var originalDefinition = typedModelHost.OriginalModel.Value.WithAssertAndCast<HideContentTypeLinksDefinition>("value", m => m.RequireNotNull()); | ||
var reversedDefinition = typedModelHost.ReversedModel.Value.WithAssertAndCast<HideContentTypeLinksDefinition>("value", m => m.RequireNotNull()); | ||
|
||
var assert = ServiceFactory.AssertService.NewAssert(originalDefinition, reversedDefinition); | ||
|
||
assert.ShouldBeEqual((p, s, d) => | ||
{ | ||
var isValid = true; | ||
var srcProp = s.GetExpressionValue(o => o.ContentTypes); | ||
var dstProp = d.GetExpressionValue(o => o.ContentTypes); | ||
// check the reversed order, should be the same | ||
// these aray should be the same in terms of ordering | ||
var srcArray = s.ContentTypes.Select(l => l.ContentTypeName).ToList(); | ||
var dstArray = d.ContentTypes.Where(f => s.ContentTypes.Any(sl => sl.ContentTypeName == f.ContentTypeName)) | ||
.Select(l => l.ContentTypeName).ToList(); | ||
isValid = srcArray.Count == dstArray.Count; | ||
if (isValid) | ||
{ | ||
for (var i = 0; i < srcArray.Count; i++) | ||
{ | ||
isValid = dstArray.Contains(srcArray[i]); | ||
if (!isValid) | ||
break; | ||
} | ||
} | ||
return new PropertyValidationResult | ||
{ | ||
Tag = p.Tag, | ||
Src = srcProp, | ||
Dst = dstProp, | ||
IsValid = isValid | ||
}; | ||
}); | ||
|
||
|
||
} | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
...PMeta2.Reverse.Tests/Impl/Definitions/ContentTypes/HideContentTypeLinksDefinitionTests.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,103 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using SPMeta2.Containers; | ||
using SPMeta2.Definitions; | ||
using SPMeta2.Reverse.CSOM.ReverseHandlers; | ||
using SPMeta2.Reverse.CSOM.Services; | ||
using SPMeta2.Reverse.Services; | ||
using SPMeta2.Reverse.Tests.Base; | ||
using SPMeta2.Syntax.Default; | ||
using SPMeta2.Enumerations; | ||
using Microsoft.SharePoint.Client; | ||
using SPMeta2.Containers.Extensions; | ||
using SPMeta2.Definitions.ContentTypes; | ||
using SPMeta2.Models; | ||
|
||
namespace SPMeta2.Reverse.Tests.Impl.Definitions.ContentTypes | ||
{ | ||
[TestClass] | ||
public class HideContentTypeLinksDefinitionTests : ReverseTestBase | ||
{ | ||
#region init | ||
|
||
[TestInitialize] | ||
public void InternalInit() | ||
{ | ||
WithCSOMContext(context => DeleteAllSubWebs(context.Site.RootWeb)); | ||
} | ||
|
||
#endregion | ||
|
||
#region tests | ||
|
||
[TestMethod] | ||
[TestCategory("ContentType.HideContentTypeLinks")] | ||
public void Can_Reverse_HideContentTypeLinks() | ||
{ | ||
// only root web | ||
var options = ReverseOptions.Default | ||
.AddDepthOption<WebDefinition>(0); | ||
|
||
var ct1 = Def<ContentTypeDefinition>(); | ||
var ct2 = Def<ContentTypeDefinition>(); | ||
var ct3 = Def<ContentTypeDefinition>(); | ||
var ct4 = Def<ContentTypeDefinition>(); | ||
|
||
var siteModel = SPMeta2Model.NewSiteModel(site => | ||
{ | ||
site.AddContentType(ct1, c => c.RegExcludeFromValidation()); | ||
site.AddContentType(ct2, c => c.RegExcludeFromValidation()); | ||
site.AddContentType(ct3, c => c.RegExcludeFromValidation()); | ||
site.AddContentType(ct4, c => c.RegExcludeFromValidation()); | ||
}); | ||
|
||
var listDef = Def<ListDefinition>(def => | ||
{ | ||
def.TemplateType = BuiltInListTemplateTypeId.DocumentLibrary; | ||
def.Url = null; | ||
def.CustomUrl = Rnd.String(); | ||
def.ContentTypesEnabled = true; | ||
}); | ||
|
||
var model1 = SPMeta2Model.NewWebModel(web => | ||
{ | ||
web.AddList(listDef, list => | ||
{ | ||
list.AddContentTypeLink(ct1); | ||
list.AddContentTypeLink(ct2); | ||
list.AddContentTypeLink(ct3); | ||
list.AddContentTypeLink(ct4); | ||
}); | ||
}); | ||
|
||
|
||
var model2 = SPMeta2Model.NewWebModel(web => | ||
{ | ||
web.AddList(listDef, list => | ||
{ | ||
list.AddHideContentTypeLinks(new HideContentTypeLinksDefinition | ||
{ | ||
ContentTypes = new List<ContentTypeLinkValue> | ||
{ | ||
new ContentTypeLinkValue { ContentTypeName = ct2.Name }, | ||
new ContentTypeLinkValue { ContentTypeName = ct3.Name }, | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
// only giving list, improve the test performance | ||
var listDefs = GetAllDefinitionOfType<ListDefinition>(model1); | ||
|
||
foreach (var listDefinition in listDefs) | ||
{ | ||
options.AddFilterOption<ListDefinition>(l => l.Title == listDefinition.Title); | ||
} | ||
|
||
DeployReverseAndTestModel(new ModelNode[] { siteModel, model1, model2 }, options); | ||
} | ||
|
||
#endregion | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
...e/SPMeta2.Reverse.Tests/_coverage_reports/_m2.ContentTypeLinkDefinition.def-coverage.html
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,23 @@ | ||
<div> | ||
<h4>ContentTypeLinkDefinition</h4> | ||
<table> | ||
<thead> | ||
<td>Property</td> | ||
<td>Support</td> | ||
<td>Comments</td> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td>ContentTypeId</td> | ||
<td>False</td> | ||
<td>Not implemented yet</td> | ||
</tr> | ||
<tr> | ||
<td>ContentTypeName</td> | ||
<td>True</td> | ||
<td> | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</div> |
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
Oops, something went wrong.