public override void DeployModel(object modelHost, DefinitionBase model) { var host = modelHost.WithAssertAndCast <WebpartPageModelHost>("modelHost", value => value.RequireNotNull()); var definition = model.WithAssertAndCast <WebPartDefinition>("model", value => value.RequireNotNull()); var item = host.PageListItem; WebPartExtensions.WithExistingWebPart(item, definition, (spWebPartManager, spObject) => { //[FALSE] - [Title] // [FALSE] - [Id] // [FALSE] - [ZoneId] // [FALSE] - [ZoneIndex] // [FALSE] - [WebpartFileName] // [FALSE] - [WebpartType] // [FALSE] - [WebpartXmlTemplate] var assert = ServiceFactory.AssertService .NewAssert(definition, spObject) .ShouldNotBeNull(spObject); if (!string.IsNullOrEmpty(definition.ChromeState)) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.ChromeState); var srcValue = (PartChromeState)Enum.Parse(typeof(PartChromeState), s.ChromeState); var isValid = srcValue == d.ChromeState; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.ChromeState, "ChromeType is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.ChromeType)) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.ChromeType); var chromeType = WebPartChromeTypesConvertService.NormilizeValueToPartChromeTypes(s.ChromeType); var srcValue = (PartChromeType)Enum.Parse(typeof(PartChromeType), chromeType); var isValid = srcValue == d.ChromeType; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.ChromeType, "ChromeType is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.Description)) { assert.ShouldBeEqual(m => m.Description, o => o.Description); } else { assert.SkipProperty(m => m.Description, "Description is null or empty. Skipping."); } if (definition.Height.HasValue) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.Height); var isValid = d.Height == new Unit(s.Height.Value); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.Height, "Height is null or empty. Skipping."); } if (definition.Width.HasValue) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.Width); var isValid = d.Width == new Unit(s.Width.Value); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.Width, "Width is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.ImportErrorMessage)) { assert.ShouldBeEqual(m => m.ImportErrorMessage, o => o.ImportErrorMessage); } else { assert.SkipProperty(m => m.ImportErrorMessage, "ImportErrorMessage is null or empty. Skipping."); } if (!DisableTitleUrlValidation) { if (!string.IsNullOrEmpty(definition.TitleUrl)) { assert.ShouldBeEndOf(m => m.TitleUrl, o => o.TitleUrl); } else { assert.SkipProperty(m => m.TitleUrl, "TitleUrl is null or empty. Skipping."); } } if (!string.IsNullOrEmpty(definition.TitleIconImageUrl)) { assert.ShouldBeEndOf(m => m.TitleIconImageUrl, o => o.TitleIconImageUrl); } else { assert.SkipProperty(m => m.TitleIconImageUrl, "TitleIconImageUrl is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.ExportMode)) { assert.ShouldBeEqual(m => m.ExportMode, o => o.GetExportMode()); } else { assert.SkipProperty(m => m.ExportMode, "ExportMode is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.Id)) { assert.ShouldBeEqual(m => m.Id, o => o.ID); } else { assert.SkipProperty(m => m.Id, "Id is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.Title)) { assert.ShouldBeEqual(m => m.Title, o => o.Title); } else { assert.SkipProperty(m => m.Title, "Title is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.ZoneId)) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.ZoneId); //var dstProp = d.GetExpressionValue(ct => ct.GetId()); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = object.Equals(s.ZoneId, spWebPartManager.GetZoneID(d)) }); }); } else { assert.SkipProperty(m => m.ZoneId, "ZoneId is null or empty. Skipping."); } //if (definition.ZoneIndex > 0) // assert.ShouldBeEqual(m => m.ZoneIndex, o => o.ZoneIndex); //else // assert.SkipProperty(m => m.ZoneIndex, "ZoneIndex == 0. Skipping."); assert.SkipProperty(m => m.ZoneIndex, "Skipping."); if (!string.IsNullOrEmpty(definition.WebpartFileName)) { var site = host.PageListItem.Web.Site; var webPartManager = host.SPLimitedWebPartManager; var webpartFileName = definition.WebpartFileName; var rootWeb = site.RootWeb; // load definition from WP catalog var webpartCatalog = rootWeb.GetCatalog(SPListTemplateType.WebPartCatalog); var webpartItem = webpartCatalog.Items.OfType <SPListItem>().FirstOrDefault( i => string.Compare(i.Name, webpartFileName, true) == 0); if (webpartItem == null) { throw new ArgumentException(string.Format("webpartItem. Can't find web part file with name: {0}", webpartFileName)); } using (var streamReader = new MemoryStream(webpartItem.File.OpenBinary())) { using (var xmlReader = XmlReader.Create(streamReader)) { var errMessage = string.Empty; var webpartInstance = webPartManager.ImportWebPart(xmlReader, out errMessage); var webPartInstanceType = webpartInstance.GetType(); assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.WebpartFileName); //var dstProp = d.GetExpressionValue(ct => ct.GetId()); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = webPartInstanceType == d.GetType() }); }); } } } else { assert.SkipProperty(m => m.WebpartFileName, "ZoneIndex == 0. Skipping."); } if (!string.IsNullOrEmpty(definition.WebpartType)) { var webPartInstance = WebPartExtensions.ResolveWebPartInstance(host.PageListItem.Web.Site, host.SPLimitedWebPartManager, definition); var webPartInstanceType = webPartInstance.GetType(); assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.WebpartType); //var dstProp = d.GetExpressionValue(ct => ct.GetId()); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = webPartInstanceType == d.GetType() }); }); } else { assert.SkipProperty(m => m.WebpartType, "WebpartType is empty. Skipping."); } if (!string.IsNullOrEmpty(definition.WebpartXmlTemplate)) { assert.SkipProperty(m => m.WebpartXmlTemplate, "WebpartXmlTemplate is not empty, but no validation is required with SSOM. Skipping."); } else { assert.SkipProperty(m => m.WebpartXmlTemplate, "WebpartXmlTemplate is empty. Skipping."); } if (definition.ParameterBindings.Count == 0) { assert.SkipProperty(m => m.ParameterBindings, "ParameterBindings is null or empty. Skipping."); } else { // TODO } }); }
protected virtual string ProcessCommonWebpartProperties(string webPartXml, WebPartDefinitionBase definition) { var xml = WebpartXmlExtensions.LoadWebpartXmlDocument(webPartXml) .SetTitle(definition.Title) .SetID(definition.Id); if (definition.Width.HasValue) { xml.SetWidth(definition.Width.Value); } if (definition.Height.HasValue) { xml.SetHeight(definition.Height.Value); } if (!string.IsNullOrEmpty(definition.Description)) { xml.SetDescription(definition.Description); } if (!string.IsNullOrEmpty(definition.ImportErrorMessage)) { xml.SetImportErrorMessage(definition.ImportErrorMessage); } if (!string.IsNullOrEmpty(definition.TitleUrl)) { var urlValue = definition.TitleUrl ?? string.Empty; TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Original value: [{0}]", urlValue); urlValue = TokenReplacementService.ReplaceTokens(new TokenReplacementContext { Value = urlValue, Context = CurrentClientContext }).Value; TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Token replaced value: [{0}]", urlValue); xml.SetTitleUrl(urlValue); } if (!string.IsNullOrEmpty(definition.TitleIconImageUrl)) { xml.SetTitleIconImageUrl(definition.TitleIconImageUrl); } if (!string.IsNullOrEmpty(definition.ChromeState)) { xml.SetChromeState(definition.ChromeState); } if (!string.IsNullOrEmpty(definition.ChromeType)) { var chromeType = string.Empty; if (xml.IsV3version()) { chromeType = WebPartChromeTypesConvertService.NormilizeValueToPartChromeTypes(definition.ChromeType); } else if (xml.IsV2version()) { chromeType = WebPartChromeTypesConvertService.NormilizeValueToFrameTypes(definition.ChromeType); } // SetChromeType() sets correct XML props depending on V2/V3 web part XML xml.SetChromeType(chromeType); } if (!string.IsNullOrEmpty(definition.ExportMode)) { xml.SetExportMode(definition.ExportMode); } // bindings ProcessParameterBindings(definition, xml); // properties ProcessWebpartProperties(definition, xml); return(xml.ToString()); }
public override void DeployModel(object modelHost, DefinitionBase model) { var listItemModelHost = modelHost.WithAssertAndCast <ListItemModelHost>("modelHost", value => value.RequireNotNull()); var definition = model.WithAssertAndCast <WebPartDefinition>("model", value => value.RequireNotNull()); var pageFile = listItemModelHost.HostFile; var context = pageFile.Context; context.Load(pageFile); context.ExecuteQueryWithTrace(); var siteServerUrl = listItemModelHost.HostSite.ServerRelativeUrl; var webUrl = listItemModelHost.HostWeb.Url; var serverUrl = context.Url; if (siteServerUrl != "/") { serverUrl = context.Url.Split(new string[] { siteServerUrl }, StringSplitOptions.RemoveEmptyEntries)[0]; } var absItemUrl = UrlUtility.CombineUrl(serverUrl, pageFile.ServerRelativeUrl); WithExistingWebPart(pageFile, definition, (spObject, spObjectDefintion) => { var webpartExportUrl = UrlUtility.CombineUrl(new[] { webUrl, "_vti_bin/exportwp.aspx?pageurl=" + absItemUrl + "&" + "guidstring=" + spObjectDefintion.Id.ToString() }); var assert = ServiceFactory.AssertService .NewAssert(model, definition, spObject) .ShouldNotBeNull(spObject); var webClient = new WebClient(); if (context.Credentials != null) { webClient.Credentials = context.Credentials; webClient.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f"); } else { webClient.UseDefaultCredentials = true; } var webPartXmlString = webClient.DownloadString(webpartExportUrl); CurrentWebPartXml = WebpartXmlExtensions.LoadWebpartXmlDocument(webPartXmlString); assert.ShouldBeEqual(m => m.Title, o => o.Title); // checking the web part type, shoul be as expected // Add regression on 'expected' web part type #690 var currentType = CurrentWebPartXml.GetWebPartAssemblyQualifiedName(); var currentClassName = currentType.Split(',').First().Trim(); var expectedTypeAttr = (definition.GetType().GetCustomAttributes(typeof(ExpectWebpartType)) .FirstOrDefault() as ExpectWebpartType); // NULL can be on generic web part // test should not care about that case, there other tests to enfore that attr usage if (expectedTypeAttr != null) { var expectedType = expectedTypeAttr.WebPartType; var expectedClassName = expectedType.Split(',').First().Trim(); assert.ShouldBeEqual((p, s, d) => { var isValid = true; isValid = currentClassName.ToUpper() == expectedClassName.ToUpper(); return(new PropertyValidationResult { Tag = p.Tag, Src = null, Dst = null, IsValid = isValid }); }); } // props if (definition.Properties.Count > 0) { assert.ShouldBeEqual((p, s, d) => { var isValid = true; foreach (var prop in definition.Properties) { // returns correct one depending on the V2/V3 var value = CurrentWebPartXml.GetProperty(prop.Name); // that True / true issue give a pain // toLower for the time being isValid = value.ToLower() == prop.Value.ToLower(); if (!isValid) { break; } } var srcProp = s.GetExpressionValue(m => m.Properties); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.Properties, "Properties are empty. Skipping."); } if (!string.IsNullOrEmpty(definition.ExportMode)) { var value = CurrentWebPartXml.GetExportMode(); assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.ExportMode); var isValid = definition.ExportMode == value; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.ExportMode, "ExportMode is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.ChromeState)) { var value = CurrentWebPartXml.GetChromeState(); assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.ChromeState); var isValid = definition.ChromeState == value; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.ChromeState, "ChromeState is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.ChromeType)) { // returns correct one depending on the V2/V3 var value = CurrentWebPartXml.GetChromeType(); var chromeType = string.Empty; if (CurrentWebPartXml.IsV3version()) { chromeType = WebPartChromeTypesConvertService.NormilizeValueToPartChromeTypes(definition.ChromeType); } else if (CurrentWebPartXml.IsV2version()) { chromeType = WebPartChromeTypesConvertService.NormilizeValueToFrameTypes(definition.ChromeType); } assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.ChromeType); var isValid = chromeType == value; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.ChromeType, "ChromeType is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.Description)) { var value = CurrentWebPartXml.GetProperty("Description"); assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.Description); var isValid = (srcProp.Value as string) == value; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.Description, "Description is null or empty. Skipping."); } if (definition.Height.HasValue) { var value = ConvertUtils.ToInt(CurrentWebPartXml.GetProperty("Height").Replace("px", string.Empty)); assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.Height); var isValid = definition.Height == value; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.Height, "Height is null or empty. Skipping."); } if (definition.Width.HasValue) { var value = ConvertUtils.ToInt(CurrentWebPartXml.GetProperty("Width").Replace("px", string.Empty)); assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.Width); var isValid = definition.Width == value; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.Width, "Width is NULL, skipping"); } if (!string.IsNullOrEmpty(definition.ImportErrorMessage)) { var value = CurrentWebPartXml.GetImportErrorMessage(); assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.ImportErrorMessage); var isValid = definition.ImportErrorMessage == value; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.ImportErrorMessage, "ImportErrorMessage is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.TitleIconImageUrl)) { var value = CurrentWebPartXml.GetTitleIconImageUrl(); assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.TitleIconImageUrl); var isValid = definition.TitleIconImageUrl == value; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.TitleIconImageUrl, "TitleIconImageUrl is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.TitleUrl)) { var value = CurrentWebPartXml.GetTitleUrl(); var defValue = TokenReplacementService.ReplaceTokens(new TokenReplacementContext { Context = listItemModelHost.HostClientContext, Value = value }).Value; var isValid = defValue.ToUpper() == value.ToUpper(); assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.TitleUrl); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.TitleUrl, "TitleUrl is null or empty. Skipping."); } assert.SkipProperty(m => m.WebpartFileName, "WebpartFileName is null or empty. Skipping."); assert.SkipProperty(m => m.WebpartType, "WebpartType is null or empty. Skipping."); assert.SkipProperty(m => m.WebpartXmlTemplate, "WebpartXmlTemplate is null or empty. Skipping."); assert.SkipProperty(m => m.ZoneId, "ZoneId is null or empty. Skipping."); assert.SkipProperty(m => m.ZoneIndex, "ZoneIndex is null or empty. Skipping."); assert.SkipProperty(m => m.Id, "Id is null or empty. Skipping."); if (definition.ParameterBindings.Count == 0) { assert.SkipProperty(m => m.ParameterBindings, "ParameterBindings is null or empty. Skipping."); } else { // TODO } }); }
public override void DeployModel(object modelHost, DefinitionBase model) { var listItemModelHost = modelHost.WithAssertAndCast <ListItemModelHost>("modelHost", value => value.RequireNotNull()); var definition = model.WithAssertAndCast <WebPartDefinition>("model", value => value.RequireNotNull()); var pageItem = listItemModelHost.HostListItem; var context = pageItem.Context; var siteServerUrl = listItemModelHost.HostSite.ServerRelativeUrl; var webUrl = listItemModelHost.HostWeb.Url; var serverUrl = context.Url; if (siteServerUrl != "/") { serverUrl = context.Url.Split(new string[] { siteServerUrl }, StringSplitOptions.RemoveEmptyEntries)[0]; } var absItemUrl = UrlUtility.CombineUrl(serverUrl, pageItem["FileRef"].ToString()); WithWithExistingWebPart(pageItem, definition, (spObject, spObjectDefintion) => { var webpartExportUrl = UrlUtility.CombineUrl(new[] { webUrl, "_vti_bin/exportwp.aspx?pageurl=" + absItemUrl + "&" + "guidstring=" + spObjectDefintion.Id.ToString() }); var assert = ServiceFactory.AssertService .NewAssert(model, definition, spObject) .ShouldNotBeNull(spObject); var webClient = new WebClient(); if (context.Credentials != null) { webClient.Credentials = context.Credentials; } else { webClient.UseDefaultCredentials = true; } var webPartXmlString = webClient.DownloadString(webpartExportUrl); CurrentWebPartXml = WebpartXmlExtensions.LoadWebpartXmlDocument(webPartXmlString); assert.ShouldBeEqual(m => m.Title, o => o.Title); if (!string.IsNullOrEmpty(definition.ExportMode)) { var value = CurrentWebPartXml.GetExportMode(); assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.ExportMode); var isValid = definition.ExportMode == value; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.ExportMode, "ExportMode is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.ChromeState)) { var value = CurrentWebPartXml.GetChromeState(); assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.ChromeState); var isValid = definition.ChromeState == value; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.ChromeState, "ChromeState is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.ChromeType)) { // returns correct one depending on the V2/V3 var value = CurrentWebPartXml.GetChromeType(); var chromeType = string.Empty; if (CurrentWebPartXml.IsV3version()) { chromeType = WebPartChromeTypesConvertService.NormilizeValueToPartChromeTypes(definition.ChromeType); } else if (CurrentWebPartXml.IsV2version()) { chromeType = WebPartChromeTypesConvertService.NormilizeValueToFrameTypes(definition.ChromeType); } assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.ChromeType); var isValid = chromeType == value; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.ChromeType, "ChromeType is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.Description)) { } else { assert.SkipProperty(m => m.Description, "Description is null or empty. Skipping."); } if (definition.Height.HasValue) { var value = ConvertUtils.ToInt(CurrentWebPartXml.GetProperty("Height").Replace("px", string.Empty)); assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.Height); var isValid = definition.Height == value; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.Height, "Height is null or empty. Skipping."); } if (definition.Width.HasValue) { var value = ConvertUtils.ToInt(CurrentWebPartXml.GetProperty("Width").Replace("px", string.Empty)); assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.Width); var isValid = definition.Width == value; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.Width, "Width is NULL, skipping"); } if (!string.IsNullOrEmpty(definition.ImportErrorMessage)) { var value = CurrentWebPartXml.GetImportErrorMessage(); assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.ImportErrorMessage); var isValid = definition.ImportErrorMessage == value; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.ImportErrorMessage, "ImportErrorMessage is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.TitleIconImageUrl)) { var value = CurrentWebPartXml.GetTitleIconImageUrl(); assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.TitleIconImageUrl); var isValid = definition.TitleIconImageUrl == value; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.TitleIconImageUrl, "TitleIconImageUrl is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.TitleUrl)) { var value = CurrentWebPartXml.GetTitleUrl(); assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.TitleIconImageUrl); var isValid = definition.TitleIconImageUrl == value; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.TitleUrl, "TitleUrl is null or empty. Skipping."); } assert.SkipProperty(m => m.WebpartFileName, "WebpartFileName is null or empty. Skipping."); assert.SkipProperty(m => m.WebpartType, "WebpartType is null or empty. Skipping."); assert.SkipProperty(m => m.WebpartXmlTemplate, "WebpartXmlTemplate is null or empty. Skipping."); assert.SkipProperty(m => m.ZoneId, "ZoneId is null or empty. Skipping."); assert.SkipProperty(m => m.ZoneIndex, "ZoneIndex is null or empty. Skipping."); assert.SkipProperty(m => m.Id, "Id is null or empty. Skipping."); if (definition.ParameterBindings.Count == 0) { assert.SkipProperty(m => m.ParameterBindings, "ParameterBindings is null or empty. Skipping."); } else { // TODO } }); }
public override void DeployModel(object modelHost, DefinitionBase model) { var host = modelHost.WithAssertAndCast <WebpartPageModelHost>("modelHost", value => value.RequireNotNull()); var definition = model.WithAssertAndCast <WebPartDefinition>("model", value => value.RequireNotNull()); var file = host.HostFile; var web = host.HostFile.Web; WebPartExtensions.WithExistingWebPart(file, definition, (spWebPartManager, spObject) => { //[FALSE] - [Title] // [FALSE] - [Id] // [FALSE] - [ZoneId] // [FALSE] - [ZoneIndex] // [FALSE] - [WebpartFileName] // [FALSE] - [WebpartType] // [FALSE] - [WebpartXmlTemplate] var assert = ServiceFactory.AssertService .NewAssert(definition, spObject) .ShouldNotBeNull(spObject); var currentType = spObject.GetType().AssemblyQualifiedName; var currentClassName = currentType.Split(',').First().Trim(); var expectedTypeAttr = (definition.GetType().GetCustomAttributes(typeof(ExpectWebpartType)) .FirstOrDefault() as ExpectWebpartType); // NULL for the generic web part // should not be tested here if (expectedTypeAttr != null) { var expectedType = expectedTypeAttr.WebPartType; var expectedClassName = expectedType.Split(',').First().Trim(); assert.ShouldBeEqual((p, s, d) => { var isValid = true; isValid = currentClassName.ToUpper() == expectedClassName.ToUpper(); return(new PropertyValidationResult { Tag = p.Tag, Src = null, Dst = null, IsValid = isValid }); }); } // props if (definition.Properties.Count > 0) { assert.ShouldBeEqual((p, s, d) => { var isValid = true; foreach (var prop in definition.Properties) { // returns correct one depending on the V2/V3 var value = ReflectionUtils.GetPropertyValue(d, prop.Name); // that True / true issue give a pain // toLower for the time being isValid = value.ToString().ToLower() == prop.Value.ToLower(); if (!isValid) { break; } } var srcProp = s.GetExpressionValue(m => m.Properties); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.Properties, "Properties are empty. Skipping."); } if (!string.IsNullOrEmpty(definition.ChromeState)) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.ChromeState); var srcValue = (PartChromeState)Enum.Parse(typeof(PartChromeState), s.ChromeState); var isValid = srcValue == d.ChromeState; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.ChromeState, "ChromeType is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.ChromeType)) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.ChromeType); var chromeType = WebPartChromeTypesConvertService.NormilizeValueToPartChromeTypes(s.ChromeType); var srcValue = (PartChromeType)Enum.Parse(typeof(PartChromeType), chromeType); var isValid = srcValue == d.ChromeType; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.ChromeType, "ChromeType is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.Description)) { assert.ShouldBeEqual(m => m.Description, o => o.Description); } else { assert.SkipProperty(m => m.Description, "Description is null or empty. Skipping."); } if (definition.Height.HasValue) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.Height); var isValid = d.Height == new Unit(s.Height.Value); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.Height, "Height is null or empty. Skipping."); } if (definition.Width.HasValue) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.Width); var isValid = d.Width == new Unit(s.Width.Value); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.Width, "Width is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.ImportErrorMessage)) { assert.ShouldBeEqual(m => m.ImportErrorMessage, o => o.ImportErrorMessage); } else { assert.SkipProperty(m => m.ImportErrorMessage, "ImportErrorMessage is null or empty. Skipping."); } if (!DisableTitleUrlValidation) { if (!string.IsNullOrEmpty(definition.TitleUrl)) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.TitleUrl); var dstProp = d.GetExpressionValue(o => o.TitleUrl); var srcUrl = srcProp.Value as string; var dstUrl = dstProp.Value as string; var isValid = false; if (definition.TitleUrl.Contains("~sitecollection")) { var siteCollectionUrl = web.Site.ServerRelativeUrl == "/" ? string.Empty : web.Site.ServerRelativeUrl; isValid = srcUrl.Replace("~sitecollection", siteCollectionUrl) == dstUrl; } else if (definition.TitleUrl.Contains("~site")) { var siteCollectionUrl = web.ServerRelativeUrl == "/" ? string.Empty : web.ServerRelativeUrl; isValid = srcUrl.Replace("~site", siteCollectionUrl) == dstUrl; } else { isValid = srcUrl == dstUrl; } return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = dstProp, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.TitleUrl, "TitleUrl is null or empty. Skipping."); } } if (!string.IsNullOrEmpty(definition.TitleIconImageUrl)) { assert.ShouldBeEndOf(m => m.TitleIconImageUrl, o => o.TitleIconImageUrl); } else { assert.SkipProperty(m => m.TitleIconImageUrl, "TitleIconImageUrl is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.ExportMode)) { assert.ShouldBeEqual(m => m.ExportMode, o => o.GetExportMode()); } else { assert.SkipProperty(m => m.ExportMode, "ExportMode is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.Id)) { assert.ShouldBeEqual(m => m.Id, o => o.ID); } else { assert.SkipProperty(m => m.Id, "Id is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.Title)) { assert.ShouldBeEqual(m => m.Title, o => o.Title); } else { assert.SkipProperty(m => m.Title, "Title is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.ZoneId)) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.ZoneId); //var dstProp = d.GetExpressionValue(ct => ct.GetId()); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = object.Equals(s.ZoneId, spWebPartManager.GetZoneID(d)) }); }); } else { assert.SkipProperty(m => m.ZoneId, "ZoneId is null or empty. Skipping."); } //if (definition.ZoneIndex > 0) // assert.ShouldBeEqual(m => m.ZoneIndex, o => o.ZoneIndex); //else // assert.SkipProperty(m => m.ZoneIndex, "ZoneIndex == 0. Skipping."); assert.SkipProperty(m => m.ZoneIndex, "Skipping."); if (!string.IsNullOrEmpty(definition.WebpartFileName)) { var site = host.HostFile.Web.Site; var webPartManager = host.SPLimitedWebPartManager; var webpartFileName = definition.WebpartFileName; var rootWeb = site.RootWeb; // load definition from WP catalog var webpartCatalog = rootWeb.GetCatalog(SPListTemplateType.WebPartCatalog); var webpartItem = webpartCatalog.Items.OfType <SPListItem>().FirstOrDefault( i => string.Compare(i.Name, webpartFileName, true) == 0); if (webpartItem == null) { throw new ArgumentException(string.Format("webpartItem. Can't find web part file with name: {0}", webpartFileName)); } using (var streamReader = new MemoryStream(webpartItem.File.OpenBinary())) { using (var xmlReader = XmlReader.Create(streamReader)) { var errMessage = string.Empty; var webpartInstance = webPartManager.ImportWebPart(xmlReader, out errMessage); var webPartInstanceType = webpartInstance.GetType(); assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.WebpartFileName); //var dstProp = d.GetExpressionValue(ct => ct.GetId()); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = webPartInstanceType == d.GetType() }); }); } } } else { assert.SkipProperty(m => m.WebpartFileName, "ZoneIndex == 0. Skipping."); } if (!string.IsNullOrEmpty(definition.WebpartType)) { var webPartInstance = WebPartExtensions.ResolveWebPartInstance(host.HostFile.Web.Site, host.SPLimitedWebPartManager, definition); var webPartInstanceType = webPartInstance.GetType(); assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.WebpartType); //var dstProp = d.GetExpressionValue(ct => ct.GetId()); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = webPartInstanceType == d.GetType() }); }); } else { assert.SkipProperty(m => m.WebpartType, "WebpartType is empty. Skipping."); } if (!string.IsNullOrEmpty(definition.WebpartXmlTemplate)) { assert.SkipProperty(m => m.WebpartXmlTemplate, "WebpartXmlTemplate is not empty, but no validation is required with SSOM. Skipping."); } else { assert.SkipProperty(m => m.WebpartXmlTemplate, "WebpartXmlTemplate is empty. Skipping."); } if (definition.ParameterBindings.Count == 0) { assert.SkipProperty(m => m.ParameterBindings, "ParameterBindings is null or empty. Skipping."); } else { // TODO } }); }
protected virtual string ProcessCommonWebpartProperties(string webPartXml, WebPartDefinitionBase definition) { var xml = WebpartXmlExtensions.LoadWebpartXmlDocument(webPartXml) .SetTitle(definition.Title) .SetID(definition.Id); if (definition.Width.HasValue) { xml.SetWidth(definition.Width.Value); } if (definition.Height.HasValue) { xml.SetHeight(definition.Height.Value); } if (!string.IsNullOrEmpty(definition.Description)) { xml.SetDescription(definition.Description); } if (!string.IsNullOrEmpty(definition.ImportErrorMessage)) { xml.SetImportErrorMessage(definition.ImportErrorMessage); } if (!string.IsNullOrEmpty(definition.TitleUrl)) { xml.SetTitleUrl(definition.TitleUrl); } if (!string.IsNullOrEmpty(definition.TitleIconImageUrl)) { xml.SetTitleIconImageUrl(definition.TitleIconImageUrl); } if (!string.IsNullOrEmpty(definition.ChromeState)) { xml.SetChromeState(definition.ChromeState); } if (!string.IsNullOrEmpty(definition.ChromeType)) { var chromeType = string.Empty; if (xml.IsV3version()) { chromeType = WebPartChromeTypesConvertService.NormilizeValueToPartChromeTypes(definition.ChromeType); } else if (xml.IsV2version()) { chromeType = WebPartChromeTypesConvertService.NormilizeValueToFrameTypes(definition.ChromeType); } // SetChromeType() sets correct XML props depending on V2/V3 web part XML xml.SetChromeType(chromeType); } if (!string.IsNullOrEmpty(definition.ExportMode)) { xml.SetExportMode(definition.ExportMode); } if (definition.ParameterBindings != null && definition.ParameterBindings.Count > 0) { var parameterBinder = new WebPartParameterBindingsOptions(); foreach (var binding in definition.ParameterBindings) { parameterBinder.AddParameterBinding(binding.Name, binding.Location); } var parameterBindingValue = SecurityElement.Escape(parameterBinder.ParameterBinding); xml.SetOrUpdateProperty("ParameterBindings", parameterBindingValue); } return(xml.ToString()); }