private void Test(IRegisteredEventsPlugin plugin) { // // Arrange // TestInitializer.InitializeTestSettings(); var contact = new Contact { MobilePhone = "A-1-B-2-C-3" }; // Create Contact to use as target var context = new PluginExecutionContextBuilder(). // Create Context Which is required by the service provider, which is required by the plugin WithRegisteredEvent(plugin.RegisteredEvents.First(e => e.EntityLogicalName == Contact.EntityLogicalName)). // Specifies the plugin event to use in the context WithTarget(contact).Build(); // Sets the Target var provider = new ServiceProviderBuilder(). WithContext(context).Build(); // // Act // plugin.Execute(provider); // Executes the Plugin // // Assert // Assert.AreEqual("123", contact.MobilePhone); }
public void RemovePhoneNumberFormatting_ContactHasFormatting_Should_RemoveFormatting() { // // Arrange // var plugin = new RemovePhoneNumberFormatting(); var contact = new Contact { MobilePhone = "A-1-B-2-C-3" }; // Create Contact to use as target var context = new PluginExecutionContextBuilder() // Create Context Which is required by the service provider, which is required by the plugin .WithRegisteredEvent(plugin.RegisteredEvents.First(e => e.EntityLogicalName == Contact.EntityLogicalName)) // Specifies the plugin event to use in the context .WithTarget(contact).Build(); // Sets the Target var provider = new ServiceProviderBuilder() .WithContext(context).Build(); // // Act // plugin.Execute(provider); // Executes the Plugin // // Assert // Assert.AreEqual("123", contact.MobilePhone); }
protected override void Test(IOrganizationService service) { // // Arrange // var contact = new Contact { Id = Ids.Contact, Address1_Line1 = "742 Evergreen Terrace" }; var plugin = new SyncContactToAccount(); var context = new PluginExecutionContextBuilder() .WithPreOperation("Create", Contact.EntityLogicalName) // Method to register with an IRegisteredEvents Plugin .WithFirstRegisteredEvent(plugin) .WithTarget(contact).Build(); var provider = new ServiceProviderBuilder(service, context, Logger).Build(); // // Act // plugin.Execute(provider); // // Assert // var account = service.GetEntity(Ids.Account); Assert.AreEqual(contact.Address1_Line1, account.Address1_Line1); }
protected override void Test(IOrganizationService service) { service = new OrganizationServiceBuilder(service) .Build(); var plugin = new Sample.FieldEncryptionPlugin(null, null); plugin.Container.Implement <ICache>().Using <Fakes.FakeCacheProvider>().WithOverwrite(); plugin.Container.Implement <ISecretProviderFactory>().Using <Fakes.FakeSecretProviderFactory <Fakes.FakeSecretProvider> >().WithOverwrite(); var target = new Contact() { Id = ExistingIds.Contact.EntityId, Department = TestData.DecryptedValue }; var executionContext = new PluginExecutionContextBuilder() .WithRegisteredEvent(20, "Update", Contact.EntityLogicalName) .WithInputParameter("Target", target) .Build(); var serviceProvider = new ServiceProviderBuilder( service, executionContext, new DebugLogger()).Build(); plugin.Execute(serviceProvider); var contextTarget = executionContext.InputParameters["Target"] as Contact; Assert.AreEqual(TestData.EncryptedValue, contextTarget.Department); }
protected override void Test(IOrganizationService service) { // // Arrange // // The CRM server will create one instance of your plugin, and then run it, multi-threaded, // so here we setup all the contexts, and call them potentially multi-threaded, on a single plugin var plugin = new RaceConditionPlugin(); var builder = new PluginExecutionContextBuilder() .WithFirstRegisteredEvent(plugin) .WithPrimaryEntityName(Contact.EntityLogicalName) .WithPreImage(new Contact()); var providers = new List <IServiceProvider>(); foreach (var contact in EntityIdsByLogicalName[Contact.EntityLogicalName].Cast <Id <Contact> >()) { var context = builder.WithTarget(new Contact { EMailAddress1 = contact.EntityId + "@test.com", [Contact.Fields.AccountId] = contact.Entity.ParentCustomerId }) .WithPrimaryEntityId(contact) .Build(); providers.Add(new ServiceProviderBuilder(service, context, Logger).Build()); } // // Act // var inParallel = false; Execute(providers, plugin, inParallel); // // Assert // var raceConditionFailures = new List <string>(); foreach (var account in service.GetEntitiesById <Account>(EntityIdsByLogicalName[Account.EntityLogicalName].Select(a => a.EntityId))) { if (account.EMailAddress1 == null) { raceConditionFailures.Add($"Account {account.Id} should have had it's email set to it's contact's email but didn't."); } if (account.PrimaryContactId == null) { raceConditionFailures.Add($"Account {account.Id} should have had it's PrimaryContactId set to it's contact's id but didn't."); } } var failures = string.Join(Environment.NewLine, raceConditionFailures); Assert.IsTrue(string.IsNullOrEmpty(failures), "No failures should have occured, but the following were found:" + Environment.NewLine + failures); }
protected override void Test(IOrganizationService service) { service = new OrganizationServiceBuilder(service) .Build(); var plugin = new Sample.FieldEncryptionPlugin(null, null); plugin.Container.Implement <ICache>().Using <Fakes.FakeCacheProvider>().WithOverwrite(); plugin.Container.Implement <ISecretProviderFactory>().Using <Fakes.FakeSecretProviderFactory <Fakes.FakeSecretProvider> >().WithOverwrite(); var testQry = new QueryExpressionBuilder <Contact>() .Select("department") .WhereAll(e => e .IsActive() .WhereAll(e1 => e1 .Attribute("fullname").Is(ConditionOperator.Like, "# " + TestData.DecryptedValue + "%"))) .Build(); var executionContext = new PluginExecutionContextBuilder() .WithRegisteredEvent(20, "RetrieveMultiple", Contact.EntityLogicalName) .WithInputParameter("Query", testQry) .Build(); var serviceProvider = new ServiceProviderBuilder( service, executionContext, new DebugLogger()).Build(); plugin.Execute(serviceProvider); var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); // get query and verify that it is modified as expected. var outQuery = context.InputParameters["Query"] as QueryExpression; var criteria = outQuery.Criteria; Assert.AreEqual(LogicalOperator.Or, criteria.FilterOperator); Assert.AreEqual(0, criteria.Conditions.Count); Assert.AreEqual(1, criteria.Filters.Count); // check sub filter criteria = criteria.Filters[0]; Assert.AreEqual(LogicalOperator.And, criteria.FilterOperator); Assert.AreEqual(2, criteria.Conditions.Count); Assert.AreEqual(0, criteria.Filters.Count); Assert.AreEqual("department", criteria.Conditions[0].AttributeName); Assert.AreEqual(TestData.EncryptedValue, criteria.Conditions[0].Values[0]); Assert.AreEqual("statecode", criteria.Conditions[1].AttributeName); Assert.AreEqual(0, criteria.Conditions[1].Values[0]); }
public void SyncContactToAccount_UpdateContactAddress_Should_UpdateAccountAddress_Dirty() { // // Arrange // TestInitializer.InitializeTestSettings(); var service = TestBase.GetOrganizationService(); var contactId = service.Create(new Contact()); var accountId = service.Create(new Account { PrimaryContactId = new EntityReference(Contact.EntityLogicalName, contactId) }); try { var contact = new Contact { Id = contactId, Address1_Line1 = "742 Evergreen Terrace" }; var plugin = new SyncContactToAccount(); var context = new PluginExecutionContextBuilder(). WithFirstRegisteredEvent(plugin). WithTarget(contact).Build(); var provider = new ServiceProviderBuilder(service, context, new DebugLogger()).Build(); // // Act // plugin.Execute(provider); // // Assert // var account = service.GetEntity <Account>(accountId); Assert.AreEqual(contact.Address1_Line1, account.Address1_Line1); } finally { // // Clean up // service.Delete(Account.EntityLogicalName, accountId); service.Delete(Contact.EntityLogicalName, contactId); } }
protected override void Test(IOrganizationService service) { service = new OrganizationServiceBuilder(service) .Build(); var plugin = new Sample.FieldEncryptionPlugin(null, null); plugin.Container.Implement <ICache>().Using <Fakes.FakeCacheProvider>().WithOverwrite(); plugin.Container.Implement <ISecretProviderFactory>().Using <Fakes.FakeSecretProviderFactory <Fakes.FakeSecretProvider> >().WithOverwrite(); plugin.Container.Implement <ISecretProviderFactory>().Using <Fakes.FakeSecretProviderFactory <Fakes.FakeSecretProvider> >().WithOverwrite(); var target = new Contact() { Id = ExistingIds.Contact.EntityId, Department = TestData.EncryptedValue }; var maskingInstructions = new Dictionary <string, EncryptedFieldService.MaskingInstruction>(); maskingInstructions.Add("department", EncryptedFieldService.MaskingInstruction.Unmask); var executionContext = new PluginExecutionContextBuilder() .WithRegisteredEvent(40, "Retrieve", Contact.EntityLogicalName) .WithInputParameter("Target", target.ToEntityReference()) .WithInputParameter("ColumnSet", new ColumnSet("display")) .WithOutputParameter("Entity", target) .WithSharedVariable("CCLLC.EncryptedFieldService.DecryptColumns", maskingInstructions) .Build(); var serviceProvider = new ServiceProviderBuilder( service, executionContext, new DebugLogger()).Build(); plugin.Execute(serviceProvider); var contextTarget = executionContext.OutputParameters["Entity"] as Contact; Assert.AreEqual(TestData.DecryptedValue, contextTarget.Department); }
public void Extensions_IPluginExecutionContext_GetTarget() { // Currently I believe this to be a issue with CRM, where the Parent Plugin Context contain the actual Target, for the post operation. // https://social.microsoft.com/Forums/en-US/19435dde-31ad-419d-826c-3f4e89ce6370/when-does-the-parent-plugin-context-contain-the-actual-target?forum=crm // // Update Finally understood, changings this to return what it should // Arrange // var target = new Lead { Id = Guid.NewGuid(), StateCode = LeadState.Open, StatusCodeEnum = Lead_StatusCode.CannotContact, FirstName = "Test" }; var parentContext = new PluginExecutionContextBuilder(). WithTarget(target).Build(); var pluginContext = new PluginExecutionContextBuilder(). WithParentContext(parentContext).WithTarget(new Lead { Id = target.Id, StateCode = LeadState.Open, StatusCodeEnum = Lead_StatusCode.CannotContact, ModifiedOn = DateTime.UtcNow, ModifiedBy = new EntityReference(SystemUser.EntityLogicalName, Guid.NewGuid()), ModifiedOnBehalfBy = new EntityReference(SystemUser.EntityLogicalName, Guid.NewGuid()) }). WithRegisteredEvent(new RegisteredEvent(PipelineStage.PostOperation, MessageType.Update)).Build(); // // Act // var pluginTarget = pluginContext.GetTarget <Lead>(); // // Assert // Assert.AreNotEqual(target.FirstName, pluginTarget.FirstName); }
protected override void Test(IOrganizationService service) { // // Arrange // var plugin = new SyncContactToAccount(); var context = new PluginExecutionContextBuilder() .WithTarget(new Contact()) .WithFirstRegisteredEvent(plugin) .Build(); var provider = new ServiceProviderBuilder(service, context, Logger).Build(); // // Act // plugin.Execute(provider); // // Assert // Assert.IsTrue(Logs.Any(l => l.Trace == SyncContactToAccount.AddressNotUpdatedMessage)); }
protected override void Test(IOrganizationService service) { service = new OrganizationServiceBuilder(service) .Build(); var plugin = new Sample.FieldEncryptionPlugin(null, null); plugin.Container.Implement <ICache>().Using <Fakes.FakeCacheProvider>().WithOverwrite(); plugin.Container.Implement <ISecretProviderFactory>().Using <Fakes.FakeSecretProviderFactory <Fakes.FakeSecretProvider> >().WithOverwrite(); var target = new Contact() { Id = ExistingIds.Contact.EntityId, Department = TestData.EncryptedValue }; var executionContext = new PluginExecutionContextBuilder() .WithRegisteredEvent(20, "Retrieve", Contact.EntityLogicalName) .WithInputParameter("Target", target.ToEntityReference()) .WithInputParameter("ColumnSet", new ColumnSet("department", "telephone1")) .WithOutputParameter("Entity", target) .Build(); var serviceProvider = new ServiceProviderBuilder( service, executionContext, new DebugLogger()).Build(); plugin.Execute(serviceProvider); var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); var columns = context.InputParameters["ColumnSet"] as ColumnSet; Assert.AreEqual(1, columns.Columns.Count); Assert.IsTrue(columns.Columns.Contains("department")); }
public void RemovePhoneNumberFormatting_ContactHasFormatting_Should_RemoveFormatting() { // // Arrange // TestInitializer.InitializeTestSettings(); var contact = new Contact { MobilePhone = "A-1-B-2-C-3" }; // Create Contact to use as target var plugin = new RemovePhoneNumberFormatting(); var context = new PluginExecutionContextBuilder(). // Create Context Which is required by the service provider, which is required by the plugin WithRegisteredEvent(plugin.RegisteredEvents.First(e => e.EntityLogicalName == Contact.EntityLogicalName)). // Specifies the plugin event to use in the context WithTarget(contact).Build(); // Sets the Target var provider = new ServiceProviderBuilder(). WithContext(context).Build(); // // Act // plugin.Execute(provider); // Executes the Plugin // // Assert // Assert.AreEqual("123", contact.MobilePhone); }