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..429c77723 100644 --- a/src/Tests/Grand.Mapping.Tests/AdminShared/CatalogProductMappingTests.cs +++ b/src/Tests/Grand.Mapping.Tests/AdminShared/CatalogProductMappingTests.cs @@ -320,17 +320,28 @@ 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)); } + [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/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/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/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. *@ -
+ - @* 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. *@