From 8296e529aad720944ded675f3b1cd18761aecf7d Mon Sep 17 00:00:00 2001 From: support Date: Sun, 22 Nov 2026 19:32:31 +0100 Subject: [PATCH 1/2] Keep the product, customer and rating of a review out of the edit form The edit form posted ProductId back (hidden input and route value) and the profile mapped ProductId, CustomerId and Rating onto the stored review. They were not saved, but the review totals were recalculated and the redirect built for the posted product, so a changed or empty ProductId left the real product's totals stale or failed the save. --- ...gTests.ProductReviewModel_ToDomain.verified.txt | 3 --- .../AdminShared/CatalogProductMappingTests.cs | 14 ++++++++++++++ .../Mapper/ProductReviewProfile.cs | 3 +++ .../Models/Catalog/ProductReviewModel.cs | 5 +++++ .../ProductReview/Partials/CreateOrUpdate.cshtml | 1 - 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/Tests/Grand.Mapping.Tests/AdminShared/CatalogProductMappingTests.ProductReviewModel_ToDomain.verified.txt b/src/Tests/Grand.Mapping.Tests/AdminShared/CatalogProductMappingTests.ProductReviewModel_ToDomain.verified.txt index 8ddeffef3..95802453b 100644 --- a/src/Tests/Grand.Mapping.Tests/AdminShared/CatalogProductMappingTests.ProductReviewModel_ToDomain.verified.txt +++ b/src/Tests/Grand.Mapping.Tests/AdminShared/CatalogProductMappingTests.ProductReviewModel_ToDomain.verified.txt @@ -1,11 +1,8 @@ { - CustomerId: cust-001, - ProductId: prod-001, IsApproved: true, Title: Great product, ReviewText: Really impressed!, ReplyText: Thank you!, ConfirmedPurchase: false, - Rating: 5, Id: ObjectId_1 } \ No newline at end of file diff --git a/src/Tests/Grand.Mapping.Tests/AdminShared/CatalogProductMappingTests.cs b/src/Tests/Grand.Mapping.Tests/AdminShared/CatalogProductMappingTests.cs index 74aa98c2d..9871d5bd7 100644 --- a/src/Tests/Grand.Mapping.Tests/AdminShared/CatalogProductMappingTests.cs +++ b/src/Tests/Grand.Mapping.Tests/AdminShared/CatalogProductMappingTests.cs @@ -331,6 +331,20 @@ public Task ProductReviewModel_ToDomain() return Verify(_mapper.Map(model)); } + [TestMethod] + public void ProductReviewModel_ToDomain_KeepsStoredFacts() + { + var review = new ProductReview { ProductId = "prod-001", CustomerId = "cust-001", Rating = 4 }; + var model = new ProductReviewModel { ProductId = "prod-002", CustomerId = "cust-002", Rating = 1, Title = "Edited" }; + + _mapper.Map(model, review); + + Assert.AreEqual("prod-001", review.ProductId); + Assert.AreEqual("cust-001", review.CustomerId); + Assert.AreEqual(4, review.Rating); + Assert.AreEqual("Edited", review.Title); + } + // ── SpecificationAttribute ──────────────────────────────────────────────── [TestMethod] diff --git a/src/Web/Grand.Web.AdminShared/Mapper/ProductReviewProfile.cs b/src/Web/Grand.Web.AdminShared/Mapper/ProductReviewProfile.cs index 0dcb2946f..fd86b1d69 100644 --- a/src/Web/Grand.Web.AdminShared/Mapper/ProductReviewProfile.cs +++ b/src/Web/Grand.Web.AdminShared/Mapper/ProductReviewProfile.cs @@ -10,6 +10,9 @@ public class ProductReviewProfile : Profile, IAutoMapperProfile public ProductReviewProfile() { CreateMap() + .ForMember(dest => dest.ProductId, mo => mo.Ignore()) + .ForMember(dest => dest.CustomerId, mo => mo.Ignore()) + .ForMember(dest => dest.Rating, mo => mo.Ignore()) .ForMember(dest => dest.HelpfulYesTotal, mo => mo.Ignore()) .ForMember(dest => dest.HelpfulNoTotal, mo => mo.Ignore()) .ForMember(dest => dest.CreatedOnUtc, mo => mo.Ignore()) diff --git a/src/Web/Grand.Web.AdminShared/Models/Catalog/ProductReviewModel.cs b/src/Web/Grand.Web.AdminShared/Models/Catalog/ProductReviewModel.cs index 712245273..50d6bba1c 100644 --- a/src/Web/Grand.Web.AdminShared/Models/Catalog/ProductReviewModel.cs +++ b/src/Web/Grand.Web.AdminShared/Models/Catalog/ProductReviewModel.cs @@ -1,10 +1,13 @@ using Grand.Infrastructure.ModelBinding; using Grand.Infrastructure.Models; +using Microsoft.AspNetCore.Mvc.ModelBinding; namespace Grand.Web.AdminShared.Models.Catalog; public class ProductReviewModel : BaseEntityModel { + //facts of the review: shown, never bound - the stored review keeps its product, customer and rating + [BindNever] [GrandResourceDisplayName("Admin.Catalog.ProductReviews.Fields.Product")] public string ProductId { get; set; } @@ -16,6 +19,7 @@ public class ProductReviewModel : BaseEntityModel [GrandResourceDisplayName("Admin.Catalog.ProductReviews.Fields.Store")] public string StoreName { get; set; } + [BindNever] [GrandResourceDisplayName("Admin.Catalog.ProductReviews.Fields.Customer")] public string CustomerId { get; set; } @@ -34,6 +38,7 @@ public class ProductReviewModel : BaseEntityModel [GrandResourceDisplayName("Admin.Catalog.ProductReviews.Fields.Signature")] public string Signature { get; set; } + [BindNever] [GrandResourceDisplayName("Admin.Catalog.ProductReviews.Fields.Rating")] public int Rating { get; set; } diff --git a/src/Web/Grand.Web.AdminShared/Views/AdminShared/ProductReview/Partials/CreateOrUpdate.cshtml b/src/Web/Grand.Web.AdminShared/Views/AdminShared/ProductReview/Partials/CreateOrUpdate.cshtml index dcf2bb02f..c3f309a61 100644 --- a/src/Web/Grand.Web.AdminShared/Views/AdminShared/ProductReview/Partials/CreateOrUpdate.cshtml +++ b/src/Web/Grand.Web.AdminShared/Views/AdminShared/ProductReview/Partials/CreateOrUpdate.cshtml @@ -1,7 +1,6 @@ @model ProductReviewModel
- @* What the customer wrote and the reply are editable; who, where, when and the rating are facts of the review, so they read as facts rather than as labels posing as controls. *@ From 19a573da180acfd87fb3de2142fead92953bbe09 Mon Sep 17 00:00:00 2001 From: support Date: Sun, 22 Nov 2026 20:29:25 +0100 Subject: [PATCH 2/2] Update the totals of a review's own product when approving in bulk ApproveSelected and DisapproveSelected took the product id from the "reviewId:productId" pair the list posts, so another product's totals could be recalculated while the review's own product kept stale ones, and an unknown id failed after the review was saved. The product now comes from the stored review. The edit form no longer carries a ProductId route value nothing reads, and the mapping snapshot no longer sets fields it ignores. --- .../AdminShared/CatalogProductMappingTests.cs | 5 +- .../ProductReviewViewModelServiceTests.cs | 61 +++++++++++++++++++ .../BaseProductReviewController.cs | 2 +- .../Services/ProductReviewViewModelService.cs | 7 +-- .../AdminShared/ProductReview/Edit.cshtml | 2 +- 5 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 src/Tests/Grand.Web.Admin.Tests/Services/ProductReviewViewModelServiceTests.cs diff --git a/src/Tests/Grand.Mapping.Tests/AdminShared/CatalogProductMappingTests.cs b/src/Tests/Grand.Mapping.Tests/AdminShared/CatalogProductMappingTests.cs index 9871d5bd7..429c77723 100644 --- a/src/Tests/Grand.Mapping.Tests/AdminShared/CatalogProductMappingTests.cs +++ b/src/Tests/Grand.Mapping.Tests/AdminShared/CatalogProductMappingTests.cs @@ -320,13 +320,10 @@ public Task PredefinedAttributeValue_ToProductAttributeValue() public Task ProductReviewModel_ToDomain() { var model = new ProductReviewModel { - CustomerId = "cust-001", - ProductId = "prod-001", IsApproved = true, Title = "Great product", ReviewText = "Really impressed!", - ReplyText = "Thank you!", - Rating = 5 + ReplyText = "Thank you!" }; return Verify(_mapper.Map(model)); } diff --git a/src/Tests/Grand.Web.Admin.Tests/Services/ProductReviewViewModelServiceTests.cs b/src/Tests/Grand.Web.Admin.Tests/Services/ProductReviewViewModelServiceTests.cs new file mode 100644 index 000000000..a58bcc0f3 --- /dev/null +++ b/src/Tests/Grand.Web.Admin.Tests/Services/ProductReviewViewModelServiceTests.cs @@ -0,0 +1,61 @@ +using Grand.Business.Core.Commands.Catalog; +using Grand.Business.Core.Interfaces.Catalog.Products; +using Grand.Business.Core.Interfaces.Common.Directory; +using Grand.Business.Core.Interfaces.Common.Stores; +using Grand.Business.Core.Interfaces.Customers; +using Grand.Domain.Catalog; +using Grand.Mediator; +using Grand.Web.AdminShared.Services; +using Grand.Web.Common.Localization; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; + +namespace Grand.Web.Admin.Tests.Services; + +[TestClass] +public class ProductReviewViewModelServiceTests +{ + private Mock _mediator; + private Mock _productReviewService; + private Mock _productService; + private ProductReviewViewModelService _service; + + [TestInitialize] + public void Init() + { + _mediator = new Mock(); + _productReviewService = new Mock(); + _productService = new Mock(); + _productService.Setup(x => x.GetProductById(It.IsAny(), It.IsAny())) + .ReturnsAsync((string id, bool _) => new Product { Id = id }); + + _service = new ProductReviewViewModelService(_productService.Object, _productReviewService.Object, + Mock.Of(), Mock.Of(), Mock.Of(), + Mock.Of(), _mediator.Object); + } + + [TestMethod] + public async Task ApproveSelected_UpdatesTotalsOfReviewsOwnProduct() + { + _productReviewService.Setup(x => x.GetProductReviewById("pr-1")) + .ReturnsAsync(new ProductReview { Id = "pr-1", ProductId = "prod-1", StoreId = "store-1" }); + + await _service.ApproveSelected(["pr-1:prod-other"], "store-1"); + + _mediator.Verify(x => x.Send(It.Is(c => c.Product.Id == "prod-1"), + It.IsAny()), Times.Once); + _productService.Verify(x => x.GetProductById("prod-other", It.IsAny()), Times.Never); + } + + [TestMethod] + public async Task DisapproveSelected_UpdatesTotalsOfReviewsOwnProduct() + { + _productReviewService.Setup(x => x.GetProductReviewById("pr-1")) + .ReturnsAsync(new ProductReview { Id = "pr-1", ProductId = "prod-1", StoreId = "store-1", IsApproved = true }); + + await _service.DisapproveSelected(["pr-1"], "store-1"); + + _mediator.Verify(x => x.Send(It.Is(c => c.Product.Id == "prod-1"), + It.IsAny()), Times.Once); + } +} diff --git a/src/Web/Grand.Web.AdminShared/Controllers/BaseProductReviewController.cs b/src/Web/Grand.Web.AdminShared/Controllers/BaseProductReviewController.cs index e5a0ed57c..658334906 100644 --- a/src/Web/Grand.Web.AdminShared/Controllers/BaseProductReviewController.cs +++ b/src/Web/Grand.Web.AdminShared/Controllers/BaseProductReviewController.cs @@ -81,7 +81,7 @@ public async Task Edit(ProductReviewModel model, bool continueEdi productReview = await productReviewViewModelService.UpdateProductReview(productReview, model); Success(translationService.GetResource("Admin.Catalog.ProductReviews.Updated")); return continueEditing - ? RedirectToAction("Edit", new { productReview.Id, productReview.ProductId }) + ? RedirectToAction("Edit", new { id = productReview.Id }) : RedirectToAction("List"); } diff --git a/src/Web/Grand.Web.AdminShared/Services/ProductReviewViewModelService.cs b/src/Web/Grand.Web.AdminShared/Services/ProductReviewViewModelService.cs index 1a469f981..d35936792 100644 --- a/src/Web/Grand.Web.AdminShared/Services/ProductReviewViewModelService.cs +++ b/src/Web/Grand.Web.AdminShared/Services/ProductReviewViewModelService.cs @@ -151,8 +151,6 @@ public virtual async Task ApproveSelected(IEnumerable selectedIds, strin foreach (var id in selectedIds) { var idReview = id.Split(':').First(); - var idProduct = id.Split(':').Last(); - var product = await _productService.GetProductById(idProduct); var productReview = await _productReviewService.GetProductReviewById(idReview); if (productReview != null && (string.IsNullOrEmpty(storeId) || productReview.StoreId == storeId)) { @@ -160,6 +158,7 @@ public virtual async Task ApproveSelected(IEnumerable selectedIds, strin productReview.IsApproved = true; await _productReviewService.UpdateProductReview(productReview); //update product totals + var product = await _productService.GetProductById(productReview.ProductId); await _mediator.Send(new UpdateProductReviewTotalsCommand { Product = product }); @@ -175,15 +174,13 @@ public virtual async Task DisapproveSelected(IEnumerable selectedIds, st foreach (var id in selectedIds) { var idReview = id.Split(':').First(); - var idProduct = id.Split(':').Last(); - - var product = await _productService.GetProductById(idProduct); var productReview = await _productReviewService.GetProductReviewById(idReview); if (productReview != null && (string.IsNullOrEmpty(storeId) || productReview.StoreId == storeId)) { productReview.IsApproved = false; await _productReviewService.UpdateProductReview(productReview); //update product totals + var product = await _productService.GetProductById(productReview.ProductId); await _mediator.Send(new UpdateProductReviewTotalsCommand { Product = product }); } } diff --git a/src/Web/Grand.Web.AdminShared/Views/AdminShared/ProductReview/Edit.cshtml b/src/Web/Grand.Web.AdminShared/Views/AdminShared/ProductReview/Edit.cshtml index 1417b870a..2fbded8b8 100644 --- a/src/Web/Grand.Web.AdminShared/Views/AdminShared/ProductReview/Edit.cshtml +++ b/src/Web/Grand.Web.AdminShared/Views/AdminShared/ProductReview/Edit.cshtml @@ -6,7 +6,7 @@ } @* The product edit screen's shell: Save as the one primary action, and Delete in the overflow menu rather than beside Save. *@ -
+