From bdc142454e1ed6752d988e60447cc9de9193720e Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 30 Apr 2020 09:27:15 +0800 Subject: [PATCH 1/2] Compare the value of the property to determine whether it is changed. Resolve #3726 --- .../EntityHistory/EntityHistoryHelper.cs | 5 ++- ...ithDisableAuditingAndPropertyHasAudited.cs | 6 ++- .../Volo/Abp/Auditing/Auditing_Tests.cs | 39 +++++++++++++++++-- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs index af728686fd..8db091eb30 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs @@ -32,7 +32,7 @@ namespace Volo.Abp.EntityFrameworkCore.EntityHistory IAuditingStore auditingStore, IOptions options, IClock clock, - IJsonSerializer jsonSerializer, + IJsonSerializer jsonSerializer, IAuditingHelper auditingHelper) { _clock = clock; @@ -241,7 +241,8 @@ namespace Volo.Abp.EntityFrameworkCore.EntityHistory } } - if (propertyEntry.IsModified) + var isModified = !(propertyEntry.OriginalValue?.Equals(propertyEntry.CurrentValue) ?? propertyEntry.CurrentValue == null); + if (isModified) { return true; } diff --git a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/Entities/AppEntityWithDisableAuditingAndPropertyHasAudited.cs b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/Entities/AppEntityWithDisableAuditingAndPropertyHasAudited.cs index 28a98bac8e..f9fcc878b1 100644 --- a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/Entities/AppEntityWithDisableAuditingAndPropertyHasAudited.cs +++ b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/Entities/AppEntityWithDisableAuditingAndPropertyHasAudited.cs @@ -11,16 +11,20 @@ namespace Volo.Abp.Auditing.App.Entities } - public AppEntityWithDisableAuditingAndPropertyHasAudited(Guid id, string name, string name2) + public AppEntityWithDisableAuditingAndPropertyHasAudited(Guid id, string name, string name2, string name3) : base(id) { Name = name; Name2 = name2; + Name3 = name3; } [Audited] public string Name { get; set; } public string Name2 { get; set; } + + [Audited] + public string Name3 { get; set; } } } diff --git a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs index 502ba6dcc8..f9f8da6180 100644 --- a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs +++ b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs @@ -7,6 +7,7 @@ using NSubstitute; using Volo.Abp.Auditing.App.Entities; using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Repositories; +using Volo.Abp.Uow; using Xunit; namespace Volo.Abp.Auditing @@ -15,10 +16,12 @@ namespace Volo.Abp.Auditing { private IAuditingStore _auditingStore; private IAuditingManager _auditingManager; + private IUnitOfWorkManager _unitOfWorkManager; public Auditing_Tests() { _auditingManager = GetRequiredService(); + _unitOfWorkManager = GetRequiredService(); } protected override void AfterAddApplication(IServiceCollection services) @@ -158,18 +161,46 @@ namespace Volo.Abp.Auditing using (var scope = _auditingManager.BeginScope()) { var repository = ServiceProvider.GetRequiredService>(); - await repository.InsertAsync(new AppEntityWithDisableAuditingAndPropertyHasAudited(Guid.NewGuid(), "test name", "test name2")); + await repository.InsertAsync(new AppEntityWithDisableAuditingAndPropertyHasAudited(Guid.NewGuid(), "test name", "test name2", "test name3")); await scope.SaveAsync(); } #pragma warning disable 4014 _auditingStore.Received().SaveAsync(Arg.Is(x => - x.EntityChanges.Count == 1 && x.EntityChanges[0].PropertyChanges.Count == 1 && - x.EntityChanges[0].PropertyChanges[0].PropertyName == - nameof(AppEntityWithDisableAuditingAndPropertyHasAudited.Name))); + x.EntityChanges.Count == 1 && x.EntityChanges[0].PropertyChanges.Count == 2 && + x.EntityChanges[0].PropertyChanges[0].PropertyName == nameof(AppEntityWithDisableAuditingAndPropertyHasAudited.Name) && + x.EntityChanges[0].PropertyChanges[1].PropertyName == nameof(AppEntityWithDisableAuditingAndPropertyHasAudited.Name3))); #pragma warning restore 4014 } + [Fact] + public virtual async Task Should_Write_AuditLog_For_Entity_That_Property_Has_Audited_Attribute_Even_Entity_Has_DisableAuditing_Attribute2() + { + var entityId = Guid.NewGuid(); + var repository = ServiceProvider.GetRequiredService>(); + await repository.InsertAsync(new AppEntityWithDisableAuditingAndPropertyHasAudited(entityId, "test name", "test name2", "test name3")); + + using (var scope = _auditingManager.BeginScope()) + { + using (var uow = _unitOfWorkManager.Begin()) + { + var entity = await repository.GetAsync(entityId); + entity.Name = "new name1"; + + await repository.UpdateAsync(entity); + + await uow.CompleteAsync(); + } + + await scope.SaveAsync(); + } + +#pragma warning disable 4014 + _auditingStore.Received().SaveAsync(Arg.Is(x => + x.EntityChanges.Count == 1 && x.EntityChanges[0].PropertyChanges.Count == 1 && + x.EntityChanges[0].PropertyChanges[0].PropertyName == nameof(AppEntityWithDisableAuditingAndPropertyHasAudited.Name))); +#pragma warning restore 4014 + } [Fact] public virtual async Task Should_Write_AuditLog_If_There_No_Action_And_No_EntityChanges() From 61f8bc32d9f8418e618a3b7aa9a0514bbd1a2b2c Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 30 Apr 2020 09:34:01 +0800 Subject: [PATCH 2/2] Rename unit test method name. --- .../Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs index f9f8da6180..2887eb8510 100644 --- a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs +++ b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs @@ -174,7 +174,7 @@ namespace Volo.Abp.Auditing } [Fact] - public virtual async Task Should_Write_AuditLog_For_Entity_That_Property_Has_Audited_Attribute_Even_Entity_Has_DisableAuditing_Attribute2() + public virtual async Task Should_Write_AuditLog_For_Entity_That_Property_Has_Audited_Attribute_And_Has_Changed_Even_Entity_Has_DisableAuditing_Attribute() { var entityId = Guid.NewGuid(); var repository = ServiceProvider.GetRequiredService>();