From 74dd5df1f07df578ccf3f06919e5777aba6d5b4e Mon Sep 17 00:00:00 2001 From: support Date: Thu, 26 Nov 2026 19:36:20 +0100 Subject: [PATCH] Show a product without a layout in the default layout A product created through the API or an import without a ProductLayoutId failed its storefront page with a 500: the handler rejected the empty id before its fallback to the default layout could run. Categories, brands and collections already fall back the same way. --- .../GetProductLayoutViewPathHandlerTests.cs | 54 +++++++++++++++++++ .../GetProductLayoutViewPathHandler.cs | 3 +- 2 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/Tests/Grand.Web.Tests/Features/Handlers/Products/GetProductLayoutViewPathHandlerTests.cs diff --git a/src/Tests/Grand.Web.Tests/Features/Handlers/Products/GetProductLayoutViewPathHandlerTests.cs b/src/Tests/Grand.Web.Tests/Features/Handlers/Products/GetProductLayoutViewPathHandlerTests.cs new file mode 100644 index 0000000000..43b18920e7 --- /dev/null +++ b/src/Tests/Grand.Web.Tests/Features/Handlers/Products/GetProductLayoutViewPathHandlerTests.cs @@ -0,0 +1,54 @@ +using Grand.Business.Core.Interfaces.Catalog.Products; +using Grand.Domain.Catalog; +using Grand.Web.Features.Handlers.Products; +using Grand.Web.Features.Models.Products; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; + +namespace Grand.Web.Tests.Features.Handlers.Products; + +[TestClass] +public class GetProductLayoutViewPathHandlerTests +{ + private readonly ProductLayout _default = new() { Id = "default", ViewPath = "ProductLayout.Simple" }; + private readonly ProductLayout _grouped = new() { Id = "grouped", ViewPath = "ProductLayout.Grouped" }; + private Mock _layoutServiceMock; + private GetProductLayoutViewPathHandler _handler; + + [TestInitialize] + public void Init() + { + _layoutServiceMock = new Mock(); + _layoutServiceMock.Setup(x => x.GetProductLayoutById("grouped")).ReturnsAsync(_grouped); + _layoutServiceMock.Setup(x => x.GetAllProductLayouts()).ReturnsAsync(new List { _default, _grouped }); + _handler = new GetProductLayoutViewPathHandler(_layoutServiceMock.Object); + } + + [TestMethod] + public async Task Handle_ProductWithLayout_ReturnsItsViewPath() + { + var path = await _handler.Handle(new GetProductLayoutViewPath { ProductLayoutId = "grouped" }, CancellationToken.None); + + Assert.AreEqual("ProductLayout.Grouped", path); + } + + [TestMethod] + [DataRow(null)] + [DataRow("")] + [DataRow("deleted")] + public async Task Handle_ProductWithoutAKnownLayout_ReturnsTheDefaultViewPath(string layoutId) + { + var path = await _handler.Handle(new GetProductLayoutViewPath { ProductLayoutId = layoutId }, CancellationToken.None); + + Assert.AreEqual("ProductLayout.Simple", path); + } + + [TestMethod] + public async Task Handle_NoLayoutAtAll_Throws() + { + _layoutServiceMock.Setup(x => x.GetAllProductLayouts()).ReturnsAsync(new List()); + + await Assert.ThrowsExactlyAsync(() => + _handler.Handle(new GetProductLayoutViewPath { ProductLayoutId = null }, CancellationToken.None)); + } +} diff --git a/src/Web/Grand.Web/Features/Handlers/Products/GetProductLayoutViewPathHandler.cs b/src/Web/Grand.Web/Features/Handlers/Products/GetProductLayoutViewPathHandler.cs index 4e4e56ffc2..cda1398b34 100644 --- a/src/Web/Grand.Web/Features/Handlers/Products/GetProductLayoutViewPathHandler.cs +++ b/src/Web/Grand.Web/Features/Handlers/Products/GetProductLayoutViewPathHandler.cs @@ -15,8 +15,7 @@ public GetProductLayoutViewPathHandler(IProductLayoutService productLayoutServic public async Task Handle(GetProductLayoutViewPath request, CancellationToken cancellationToken) { - ArgumentNullException.ThrowIfNullOrEmpty(request.ProductLayoutId); - + //a product without a layout (made through the API or an import) shows in the default one var layout = await _productLayoutService.GetProductLayoutById(request.ProductLayoutId) ?? (await _productLayoutService.GetAllProductLayouts()).FirstOrDefault(); if (layout == null)