|
| 1 | +using System.Linq; |
| 2 | +using Microsoft.Xrm.Sdk; |
| 3 | +using Microsoft.Xrm.Sdk.Messages; |
| 4 | +using Microsoft.Xrm.Sdk.Query; |
| 5 | +using DG.XrmFramework.BusinessDomain.ServiceContext; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace DG.XrmMockupTest |
| 9 | +{ |
| 10 | + public class TestMultipleRequestPluginImages : UnitTestBase |
| 11 | + { |
| 12 | + public TestMultipleRequestPluginImages(XrmMockupFixture fixture) : base(fixture) { } |
| 13 | + |
| 14 | + [Fact] |
| 15 | + public void UpdateMultiple_TriggersUpdatePostOpPlugin_WithPostImages() |
| 16 | + { |
| 17 | + // Arrange: create contacts |
| 18 | + var contact1Id = orgGodService.Create(new Contact { FirstName = "Alice", LastName = "One" }); |
| 19 | + var contact2Id = orgGodService.Create(new Contact { FirstName = "Bob", LastName = "Two" }); |
| 20 | + |
| 21 | + // Act: UpdateMultiple - this previously threw NullReferenceException |
| 22 | + // because plugins with post-images received null images from the Multiple->Single cross-trigger |
| 23 | + var updateMultiple = new UpdateMultipleRequest |
| 24 | + { |
| 25 | + Targets = new EntityCollection |
| 26 | + { |
| 27 | + EntityName = Contact.EntityLogicalName, |
| 28 | + Entities = |
| 29 | + { |
| 30 | + new Contact(contact1Id) { Description = "updated-1" }, |
| 31 | + new Contact(contact2Id) { Description = "updated-2" }, |
| 32 | + } |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | + // Should not throw NullReferenceException |
| 37 | + orgAdminService.Execute(updateMultiple); |
| 38 | + |
| 39 | + // Assert: entities were updated |
| 40 | + var retrieved1 = Contact.Retrieve(orgAdminService, contact1Id); |
| 41 | + var retrieved2 = Contact.Retrieve(orgAdminService, contact2Id); |
| 42 | + Assert.Equal("updated-1", retrieved1.Description); |
| 43 | + Assert.Equal("updated-2", retrieved2.Description); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public void CreateMultiple_DoesNotCrash() |
| 48 | + { |
| 49 | + var createMultiple = new CreateMultipleRequest |
| 50 | + { |
| 51 | + Targets = new EntityCollection |
| 52 | + { |
| 53 | + EntityName = Contact.EntityLogicalName, |
| 54 | + Entities = |
| 55 | + { |
| 56 | + new Contact { FirstName = "Charlie", LastName = "Three" }, |
| 57 | + new Contact { FirstName = "Diana", LastName = "Four" }, |
| 58 | + } |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + // Should not throw |
| 63 | + var response = (CreateMultipleResponse)orgAdminService.Execute(createMultiple); |
| 64 | + Assert.NotNull(response); |
| 65 | + } |
| 66 | + |
| 67 | + [Fact] |
| 68 | + public void SingleUpdate_TriggersUpdateMultiplePlugin() |
| 69 | + { |
| 70 | + // The SetCityOnCreateUpdateMultiple plugin is registered on UpdateMultiple/PreOperation |
| 71 | + // and should fire via the Single->Multiple cross-trigger |
| 72 | + var contactId = orgGodService.Create(new Contact { FirstName = "Eve", Address2_City = "Berlin" }); |
| 73 | + |
| 74 | + orgAdminService.Update(new Contact(contactId) { FirstName = "Eve-Updated" }); |
| 75 | + |
| 76 | + var retrieved = Contact.Retrieve(orgAdminService, contactId); |
| 77 | + Assert.Equal("Copenhagen", retrieved.Address2_City); |
| 78 | + } |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public void SingleCreate_TriggersCreateMultiplePlugin() |
| 82 | + { |
| 83 | + // The SetCityOnCreateUpdateMultiple plugin is registered on CreateMultiple/PreOperation |
| 84 | + var contactId = orgAdminService.Create(new Contact { FirstName = "Frank" }); |
| 85 | + |
| 86 | + var retrieved = Contact.Retrieve(orgAdminService, contactId); |
| 87 | + Assert.Equal("Copenhagen", retrieved.Address2_City); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments