Skip to content
This repository was archived by the owner on Jun 16, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions FakeXrmEasy.Shared/XrmFakedContext.Pipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Linq;
using System.Reflection;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;

namespace FakeXrmEasy
Expand Down Expand Up @@ -204,16 +206,32 @@ private IEnumerable<Entity> GetStepsForStage(string method, ProcessingStepStage
};

var entityTypeCode = (int?)entity.GetType().GetField("EntityTypeCode")?.GetValue(entity);
if (!entityTypeCode.HasValue)
{
var response = (RetrieveEntityResponse)this.Service.Execute(new RetrieveEntityRequest { LogicalName = entity.LogicalName, EntityFilters = EntityFilters.Entity });
entityTypeCode = response.EntityMetadata.ObjectTypeCode;
}

var plugins = this.Service.RetrieveMultiple(query).Entities.AsEnumerable();
plugins = plugins.Where(p =>
{
var primaryObjectTypeCode = p.GetAttributeValue<AliasedValue>("sdkmessagefilter.primaryobjecttypecode");

return primaryObjectTypeCode == null || entityTypeCode.HasValue && (int)primaryObjectTypeCode.Value == entityTypeCode.Value;
});
bool consider = primaryObjectTypeCode == null || entityTypeCode.HasValue && (int)primaryObjectTypeCode.Value == entityTypeCode.Value;
if (!consider)
{
return false;
}

// Todo: Filter on attributes
var filteringAttributes = p.GetAttributeValue<string>("filteringattributes");
if (method == "Update" && filteringAttributes != null && !string.IsNullOrEmpty(filteringAttributes.Trim()))
{
var parts = filteringAttributes.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(e => e.Trim()).ToList();
return parts.Any(filter => entity.Attributes.ContainsKey(filter));
}

return true;
});

return plugins;
}
Expand Down
90 changes: 90 additions & 0 deletions FakeXrmEasy.Tests.Shared/Pipeline/PipelineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,96 @@ public void When_PluginIsRegisteredForOtherEntity_And_OtherPluginCreatesAnAccoun
Assert.False(account.Attributes.ContainsKey("accountnumber"));
}

[Fact]
public void When_UsingWeaklyTypedEntity_And_AccountNumberPlugin_Expect_AccountNumberIsSet()
{
var context = new XrmFakedContext() { UsePipelineSimulation = true };

context.InitializeMetadata(typeof(Account).Assembly);
context.RegisterPluginStep<AccountNumberPlugin, Account>("Create");
context.ExecutePluginWith<CreateAccountPlugin>();

var organizationService = context.GetOrganizationService();
var accountId = organizationService.Create(new Entity(Account.EntityLogicalName));
Assert.NotEqual(Guid.Empty, accountId);
var account = organizationService.Retrieve(Account.EntityLogicalName, accountId, new ColumnSet(true));
Assert.True(account.Attributes.ContainsKey("accountnumber"));
}

[Fact]
public void When_UsingFilteringAttributes_And_TheyMatch_Expect_PluginTriggers()
{
// Arange
var context = new XrmFakedContext() { UsePipelineSimulation = true };

var id = Guid.NewGuid();

var entities = new List<Entity>
{
new Contact
{
Id = id
}
};
context.Initialize(entities);

// Act
context.RegisterPluginStep<ValidatePipelinePlugin, Contact>("Update", ProcessingStepStage.Preoperation, ProcessingStepMode.Synchronous, filteringAttributes: new string[] { "address1_city" });

var updatedEntity = new Contact
{
Id = id,
Address1_City = "NY"
};

var service = context.GetOrganizationService();
service.Update(updatedEntity);

// Assert
var trace = context.GetFakeTracingService().DumpTrace().Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

Assert.Equal(5, trace.Length);
Assert.Contains("Message Name: Update", trace);
Assert.Contains("Stage: 20", trace);
Assert.Contains("Mode: 0", trace);
Assert.Contains($"Entity Logical Name: {Contact.EntityLogicalName}", trace);
Assert.Contains($"Entity ID: {id}", trace);
}

[Fact]
public void When_UsingFilteringAttributes_And_TheyDontMatch_Expect_PluginDoesNotTriggers()
{
// Arange
var context = new XrmFakedContext() { UsePipelineSimulation = true };

var id = Guid.NewGuid();

var entities = new List<Entity>
{
new Contact
{
Id = id
}
};
context.Initialize(entities);

// Act
context.RegisterPluginStep<ValidatePipelinePlugin, Contact>("Update", ProcessingStepStage.Preoperation, ProcessingStepMode.Synchronous, filteringAttributes: new string[] { "address1_city" });

var updatedEntity = new Contact
{
Id = id
};

var service = context.GetOrganizationService();
service.Update(updatedEntity);

// Assert
var trace = context.GetFakeTracingService().DumpTrace().Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

Assert.Equal(0, trace.Length);
}

[Fact]
public void When_PluginStepRegisteredAsDeletePreOperationSyncronous_Expect_CorrectValues()
{
Expand Down