Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProductReview>(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]
Expand Down
Original file line number Diff line number Diff line change
@@ -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<IMediator> _mediator;
private Mock<IProductReviewService> _productReviewService;
private Mock<IProductService> _productService;
private ProductReviewViewModelService _service;

[TestInitialize]
public void Init()
{
_mediator = new Mock<IMediator>();
_productReviewService = new Mock<IProductReviewService>();
_productService = new Mock<IProductService>();
_productService.Setup(x => x.GetProductById(It.IsAny<string>(), It.IsAny<bool>()))
.ReturnsAsync((string id, bool _) => new Product { Id = id });

_service = new ProductReviewViewModelService(_productService.Object, _productReviewService.Object,
Mock.Of<ICustomerService>(), Mock.Of<IStoreService>(), Mock.Of<IDateTimeService>(),
Mock.Of<IRequestTranslationService>(), _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<UpdateProductReviewTotalsCommand>(c => c.Product.Id == "prod-1"),
It.IsAny<CancellationToken>()), Times.Once);
_productService.Verify(x => x.GetProductById("prod-other", It.IsAny<bool>()), 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<UpdateProductReviewTotalsCommand>(c => c.Product.Id == "prod-1"),
It.IsAny<CancellationToken>()), Times.Once);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public async Task<IActionResult> 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");
}

Expand Down
3 changes: 3 additions & 0 deletions src/Web/Grand.Web.AdminShared/Mapper/ProductReviewProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public class ProductReviewProfile : Profile, IAutoMapperProfile
public ProductReviewProfile()
{
CreateMap<ProductReviewModel, ProductReview>()
.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())
Expand Down
Original file line number Diff line number Diff line change
@@ -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; }

Expand All @@ -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; }

Expand All @@ -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; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,14 @@ public virtual async Task ApproveSelected(IEnumerable<string> 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))
{
var previousIsApproved = productReview.IsApproved;
productReview.IsApproved = true;
await _productReviewService.UpdateProductReview(productReview);
//update product totals
var product = await _productService.GetProductById(productReview.ProductId);
await _mediator.Send(new UpdateProductReviewTotalsCommand { Product = product });


Expand All @@ -175,15 +174,13 @@ public virtual async Task DisapproveSelected(IEnumerable<string> 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 });
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. *@
<form asp-area="@area" asp-controller="ProductReview" asp-action="Edit" method="post" asp-route-ProductId="@HtmlExtensions.HtmlEncodeSafe(Context.Request.Query["ProductId"])">
<form asp-area="@area" asp-controller="ProductReview" asp-action="Edit" method="post">
<admin-page sticky
title="@Loc["Admin.Catalog.ProductReviews.EditProductReviewDetails"]"
icon="bi-box"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
@model ProductReviewModel
<div asp-validation-summary="All"></div>
<input asp-for="Id" type="hidden"/>
<input asp-for="ProductId" type="hidden"/>
<partial name="Partials/WidgetZone.DetailsTop" model="Model"/>
@* 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. *@
Expand Down
Loading