Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
Migrate 'Reviews' region into BaseProductController (ARCH-001 Phase 1)
KrzysztofPajak4591d1dee8d3
diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseProductControllerTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseProductControllerTests.csindex c5ad5fb6b..803ef43d3 100644--- a/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseProductControllerTests.cs+++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseProductControllerTests.cs@@ -2758,4 +2758,81 @@ public class BaseProductControllerTestsAssert.IsNotNull(gridModel);Assert.AreEqual(1, gridModel.Total);}++ // --- Reviews ---------------------------------------------------------------------------------++ [TestMethod]+ public async Task Reviews_ScopeDeniesAccess_ReturnsErrorJson_DoesNotLoadReviews()+ {+ var product = new Product { Id = "p1" };+ _productServiceMock.Setup(p => p.GetProductById("p1")).ReturnsAsync(product);+ _scopeMock.Setup(s => s.HasAccess(product)).ReturnsAsync(false);+ var productReviewServiceMock = new Mock<IProductReviewService>();++ var result = await _controller.Reviews(+ new Grand.Web.Common.DataSource.DataSourceRequest(), "p1", productReviewServiceMock.Object);++ Assert.IsInstanceOfType<JsonResult>(result);+ _translationServiceMock.Verify(t => t.GetResource("Admin.Catalog.Products.Permissions"), Times.Once);+ productReviewServiceMock.Verify(+ s => s.GetAllProductReviews(It.IsAny<string>(), It.IsAny<bool?>(), It.IsAny<DateTime?>(),+ It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(),+ It.IsAny<int>(), It.IsAny<int>()), Times.Never);+ }++ [TestMethod]+ public async Task Reviews_ScopeGrantsAccess_WithDefaultStoreId_FiltersReviewsByStore()+ {+ var product = new Product { Id = "p1" };+ _productServiceMock.Setup(p => p.GetProductById("p1")).ReturnsAsync(product);+ _scopeMock.Setup(s => s.HasAccess(product)).ReturnsAsync(true);+ // DefaultStoreId stands in for Store's original storeId argument (the staff member's+ // StaffStoreId, used to filter reviews to that store).+ _scopeMock.Setup(s => s.DefaultStoreId).Returns("store-1");+ var review = new ProductReview { Id = "r1", ProductId = "p1" };+ var reviews = new PagedList<ProductReview>(new List<ProductReview> { review }, 0, int.MaxValue);+ var productReviewServiceMock = new Mock<IProductReviewService>();+ productReviewServiceMock+ .Setup(s => s.GetAllProductReviews("", null, null, null, "", "store-1", "p1", 0, int.MaxValue))+ .ReturnsAsync(reviews);++ var result = await _controller.Reviews(+ new Grand.Web.Common.DataSource.DataSourceRequest(), "p1", productReviewServiceMock.Object);++ var json = result as JsonResult;+ Assert.IsNotNull(json);+ var gridModel = json.Value as Grand.Web.Common.DataSource.DataSourceResult;+ Assert.IsNotNull(gridModel);+ Assert.AreEqual(1, gridModel.Total);+ _productViewModelServiceMock.Verify(+ s => s.PrepareProductReviewModel(It.IsAny<ProductReviewModel>(), review, false, true), Times.Once);+ }++ [TestMethod]+ public async Task Reviews_ScopeGrantsAccess_NullDefaultStoreId_PassesEmptyStoreId()+ {+ // Matches both Admin's and Vendor's originals, which both passed "" literally (Vendor scopes by+ // VendorId via the HasAccess check above, not by store - VendorProductDataScope.DefaultStoreId+ // is null).+ var product = new Product { Id = "p1" };+ _productServiceMock.Setup(p => p.GetProductById("p1")).ReturnsAsync(product);+ _scopeMock.Setup(s => s.HasAccess(product)).ReturnsAsync(true);+ _scopeMock.Setup(s => s.DefaultStoreId).Returns((string)null);+ var reviews = new PagedList<ProductReview>(new List<ProductReview>(), 0, int.MaxValue);+ var productReviewServiceMock = new Mock<IProductReviewService>();+ productReviewServiceMock+ .Setup(s => s.GetAllProductReviews("", null, null, null, "", "", "p1", 0, int.MaxValue))+ .ReturnsAsync(reviews);++ var result = await _controller.Reviews(+ new Grand.Web.Common.DataSource.DataSourceRequest(), "p1", productReviewServiceMock.Object);++ var json = result as JsonResult;+ Assert.IsNotNull(json);+ var gridModel = json.Value as Grand.Web.Common.DataSource.DataSourceResult;+ Assert.IsNotNull(gridModel);+ Assert.AreEqual(0, gridModel.Total);+ productReviewServiceMock.Verify(+ s => s.GetAllProductReviews("", null, null, null, "", "", "p1", 0, int.MaxValue), Times.Once);+ }}diff --git a/src/Web/Grand.Web.AdminShared/Controllers/BaseProductController.cs b/src/Web/Grand.Web.AdminShared/Controllers/BaseProductController.csindex 4208f710c..54a723841 100644--- a/src/Web/Grand.Web.AdminShared/Controllers/BaseProductController.cs+++ b/src/Web/Grand.Web.AdminShared/Controllers/BaseProductController.cs@@ -1794,4 +1794,47 @@ public abstract class BaseProductController(}#endregion++ #region Reviews++ [PermissionAuthorizeAction(PermissionActionName.Preview)]+ [HttpPost]+ public async Task<IActionResult> Reviews(DataSourceRequest command, string productId,+ [FromServices] IProductReviewService productReviewService)+ {+ var product = await productService.GetProductById(productId);++ // HasAccess (strict): mirrors Store's CanAccessProduct and Vendor's CheckAccessToProduct gating+ // this action on both hosts. Admin's original had no check at all - GlobalAdminDataScope.HasAccess+ // is a no-op there, so this closes that gap the same way as every other row in this task.+ if (!await scope.HasAccess(product))+ return ErrorForKendoGridJson(translationService.GetResource($"{scope.ResourceKeyPrefix}.Catalog.Products.Permissions"));++ // DefaultStoreId is the staff member's store for Store (matches its original storeId argument,+ // which filtered reviews to the staff member's store) and null - normalized to "" here, matching+ // GetAllProductReviews's expected "no filter" value - for both Admin and Vendor (matches their+ // originals, which both passed "" literally; Vendor scopes by VendorId via the HasAccess check+ // above, not by store, since VendorProductDataScope.DefaultStoreId is null).+ var storeId = scope.DefaultStoreId ?? "";++ var productReviews = await productReviewService.GetAllProductReviews("", null,+ null, null, "", storeId, productId);++ var items = new List<ProductReviewModel>();+ foreach (var item in productReviews.PagedForCommand(command))+ {+ var m = new ProductReviewModel();+ await productViewModelService.PrepareProductReviewModel(m, item, false, true);+ items.Add(m);+ }++ var gridModel = new DataSourceResult {+ Data = items,+ Total = productReviews.Count+ };++ return Json(gridModel);+ }++ #endregion}