Skip to content

Commit

Permalink
Add HideContentTypeLinksDefinition reverse handler #79
Browse files Browse the repository at this point in the history
  • Loading branch information
SubPointSupport committed May 12, 2016
1 parent e7a1156 commit d5c1ab8
Show file tree
Hide file tree
Showing 12 changed files with 439 additions and 257 deletions.
23 changes: 23 additions & 0 deletions M2.Reverse.Coverage.Status.md
Expand Up @@ -480,6 +480,29 @@ In case you have unexpected issues or keen to see new features please contact su
</tbody>
</table>
</div>
<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>
<div>
<h4>CurrencyFieldDefinition</h4>
<table>
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -44,6 +44,7 @@ In case you have unexpected issues or keen to see new features please contact su
* ChoiceFieldDefinition
* ContentTypeDefinition
* ContentTypeFieldLinkDefinition
* ContentTypeLinkDefinition
* CurrencyFieldDefinition
* DateTimeFieldDefinition
* FeatureDefinition
Expand Down
@@ -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
}
}
Expand Up @@ -92,6 +92,7 @@
<Compile Include="ReverseHandlers\ContentTypeLinkReverseHandler.cs" />
<Compile Include="ReverseHandlers\ContentTypeFieldLinkReverseHandler.cs" />
<Compile Include="ReverseHandlers\ContentTypeReverseHandler.cs" />
<Compile Include="ReverseHandlers\ContentTypes\HideContentTypeLinksReverseHandler.cs" />
<Compile Include="ReverseHandlers\ContentTypes\HideContentTypeFieldLinksReverseHandler.cs" />
<Compile Include="ReverseHandlers\ContentTypes\UniqueContentTypeFieldOrderReverseHandler.cs" />
<Compile Include="ReverseHandlers\FeatureReverseHandler.cs" />
Expand Down
Expand Up @@ -60,6 +60,7 @@
<Compile Include="Services\ReverseValidationService.cs" />
<Compile Include="Validation\ContentTypeLinkDefinitionValidator.cs" />
<Compile Include="Validation\ContentTypeFieldLinkDefinitionValidator.cs" />
<Compile Include="Validation\ContentTypes\HideContentTypeLinksDefinitionValidator.cs" />
<Compile Include="Validation\ContentTypes\HideContentTypeFieldLinksDefinitionValidator.cs" />
<Compile Include="Validation\ContentTypes\UniqueContentTypeFieldsOrderDefinitionValidator.cs" />
<Compile Include="Validation\FieldDefinitionValidator.cs" />
Expand Down
Expand Up @@ -43,6 +43,12 @@ public string GetDefinitionIdentityKey(DefinitionBase def)
identityKeyNames.Add("FieldId");
}

if (def is ContentTypeLinkDefinition)
{
identityKeyNames.Clear();
identityKeyNames.Add("ContentTypeName");
}

// url gets transformed by SharePoint to the full one
// so that lookup by identity won't work
// rely only on title for the time being
Expand Down
@@ -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
};
});


}
}
}
@@ -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
}
}
Expand Up @@ -88,6 +88,7 @@
<Compile Include="Base\ReverseTestBase.cs" />
<Compile Include="Impl\Definitions\ContentTypeFieldLinkDefinitionTests.cs" />
<Compile Include="Impl\Definitions\ContentTypeDefinitionTests.cs" />
<Compile Include="Impl\Definitions\ContentTypes\HideContentTypeLinksDefinitionTests.cs" />
<Compile Include="Impl\Definitions\ContentTypes\HideContentTypeFieldLinksDefinitionTests.cs" />
<Compile Include="Impl\Definitions\ContentTypes\UniqueContentTypeFieldsOrderDefinitionTests.cs" />
<Compile Include="Impl\Definitions\FeatureDefinitionTests.cs" />
Expand Down
@@ -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>
Expand Up @@ -468,6 +468,29 @@ <h4>ContentTypeFieldLinkDefinition</h4>
</tbody>
</table>
</div>
<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>
<div>
<h4>CurrencyFieldDefinition</h4>
<table>
Expand Down

0 comments on commit d5c1ab8

Please sign in to comment.