From 454f6103d0ebcba3500773ab3ee93a9530068b01 Mon Sep 17 00:00:00 2001 From: Krzysztof Pajak Date: Mon, 23 Nov 2026 05:37:08 +0100 Subject: [PATCH 1/6] Keep the system flag of a customer group out of the edit form (#1085) --- .../Controllers/CustomerGroupController.cs | 11 ++++------- .../Mapper/Profiles/CustomerGroupProfile.cs | 3 ++- .../AdminShared/CustomerMappingTests.cs | 19 +++++++++++++++++++ .../Api/ApiMappingTests.cs | 19 +++++++++++++++++++ .../Partials/CreateOrUpdate.TabInfo.cshtml | 4 +--- .../Controllers/CustomerGroupController.cs | 1 + .../Mapper/CustomerGroupProfile.cs | 3 ++- .../Models/Customers/CustomerGroupModel.cs | 6 ++++++ 8 files changed, 54 insertions(+), 12 deletions(-) diff --git a/src/Modules/Grand.Module.Api/Controllers/CustomerGroupController.cs b/src/Modules/Grand.Module.Api/Controllers/CustomerGroupController.cs index cecbae825..84f7ad6d2 100644 --- a/src/Modules/Grand.Module.Api/Controllers/CustomerGroupController.cs +++ b/src/Modules/Grand.Module.Api/Controllers/CustomerGroupController.cs @@ -104,14 +104,11 @@ public async Task Patch([FromRoute] string key, [FromBody] JsonPa if (!customerGroup.Any()) return NotFound(); var cr = customerGroup.FirstOrDefault(); - model.ApplyTo(cr); - if (cr is { IsSystem: false }) - { - await _mediator.Send(new UpdateCustomerGroupCommand { Model = cr }); - return Ok(); - } + if (cr!.IsSystem) return BadRequest(ModelState); - return BadRequest(ModelState); + model.ApplyTo(cr); + await _mediator.Send(new UpdateCustomerGroupCommand { Model = cr }); + return Ok(); } [EndpointDescription("Delete entity in CustomerGroup")] diff --git a/src/Modules/Grand.Module.Api/Infrastructure/Mapper/Profiles/CustomerGroupProfile.cs b/src/Modules/Grand.Module.Api/Infrastructure/Mapper/Profiles/CustomerGroupProfile.cs index b5bc43bf0..45e2cb8b9 100644 --- a/src/Modules/Grand.Module.Api/Infrastructure/Mapper/Profiles/CustomerGroupProfile.cs +++ b/src/Modules/Grand.Module.Api/Infrastructure/Mapper/Profiles/CustomerGroupProfile.cs @@ -10,7 +10,8 @@ public class CustomerGroupProfile : Profile, IAutoMapperProfile public CustomerGroupProfile() { CreateMap() - .ForMember(dest => dest.UserFields, mo => mo.Ignore()); + .ForMember(dest => dest.UserFields, mo => mo.Ignore()) + .ForMember(dest => dest.IsSystem, mo => mo.Ignore()); CreateMap(); } diff --git a/src/Tests/Grand.Mapping.Tests/AdminShared/CustomerMappingTests.cs b/src/Tests/Grand.Mapping.Tests/AdminShared/CustomerMappingTests.cs index edf927c7c..48c5ee38e 100644 --- a/src/Tests/Grand.Mapping.Tests/AdminShared/CustomerMappingTests.cs +++ b/src/Tests/Grand.Mapping.Tests/AdminShared/CustomerMappingTests.cs @@ -64,6 +64,25 @@ public Task CustomerGroupModel_ToDomain() return Verify(_mapper.Map(model)); } + [TestMethod] + public void CustomerGroupModel_ToDomain_KeepsStoredIsSystem() + { + var group = new CustomerGroup { Id = "cg-001", Name = "Administrators", SystemName = "Administrators", IsSystem = true }; + var model = new CustomerGroupModel { Id = "cg-001", Name = "Administrators", SystemName = "Administrators", IsSystem = false }; + + _mapper.Map(model, group); + + Assert.IsTrue(group.IsSystem); + } + + [TestMethod] + public void CustomerGroupModel_ToDomain_NewGroupIsNotSystem() + { + var model = new CustomerGroupModel { Name = "VIP Customers", SystemName = "vip", IsSystem = true }; + + Assert.IsFalse(_mapper.Map(model).IsSystem); + } + // ── CustomerAttribute ───────────────────────────────────────────────────── [TestMethod] diff --git a/src/Tests/Grand.Mapping.Tests/Api/ApiMappingTests.cs b/src/Tests/Grand.Mapping.Tests/Api/ApiMappingTests.cs index 4ad226175..6661c40a2 100644 --- a/src/Tests/Grand.Mapping.Tests/Api/ApiMappingTests.cs +++ b/src/Tests/Grand.Mapping.Tests/Api/ApiMappingTests.cs @@ -200,6 +200,25 @@ public Task CustomerGroupDto_ToEntity() return Verify(_mapper.Map(dto)); } + [TestMethod] + public void CustomerGroupDto_ToEntity_KeepsStoredIsSystem() + { + var group = new CustomerGroup { Id = "cg-1", Name = "Administrators", SystemName = "Administrators", IsSystem = true }; + var dto = new CustomerGroupDto { Id = "cg-1", Name = "Administrators", SystemName = "Administrators", IsSystem = false }; + + _mapper.Map(dto, group); + + Assert.IsTrue(group.IsSystem); + } + + [TestMethod] + public void CustomerGroupDto_ToEntity_NewGroupIsNotSystem() + { + var dto = new CustomerGroupDto { Name = "VIP Customers", SystemName = "Vip", IsSystem = true }; + + Assert.IsFalse(_mapper.Map(dto).IsSystem); + } + [TestMethod] public Task CustomerGroup_ToDto() { diff --git a/src/Web/Grand.Web.Admin/Areas/Admin/Views/CustomerGroup/Partials/CreateOrUpdate.TabInfo.cshtml b/src/Web/Grand.Web.Admin/Areas/Admin/Views/CustomerGroup/Partials/CreateOrUpdate.TabInfo.cshtml index 927ee46ab..697a510e1 100644 --- a/src/Web/Grand.Web.Admin/Areas/Admin/Views/CustomerGroup/Partials/CreateOrUpdate.TabInfo.cshtml +++ b/src/Web/Grand.Web.Admin/Areas/Admin/Views/CustomerGroup/Partials/CreateOrUpdate.TabInfo.cshtml @@ -1,6 +1,5 @@ @model CustomerGroupModel -@* one card of two-column fields; IsSystem is not editable, so it reads as a fact and posts back - through its hidden input *@ +@* one card of two-column fields; IsSystem is not editable, so it reads as a fact and is never posted *@
@@ -18,5 +17,4 @@
@Model.IsSystem.ToString()
-
diff --git a/src/Web/Grand.Web.Admin/Controllers/CustomerGroupController.cs b/src/Web/Grand.Web.Admin/Controllers/CustomerGroupController.cs index 550fe985d..aca98e729 100644 --- a/src/Web/Grand.Web.Admin/Controllers/CustomerGroupController.cs +++ b/src/Web/Grand.Web.Admin/Controllers/CustomerGroupController.cs @@ -137,6 +137,7 @@ public async Task Edit(CustomerGroupModel model, bool continueEdi } //If we got this far, something failed, redisplay form + model.IsSystem = customerGroup.IsSystem; return View(model); } catch (Exception exc) diff --git a/src/Web/Grand.Web.AdminShared/Mapper/CustomerGroupProfile.cs b/src/Web/Grand.Web.AdminShared/Mapper/CustomerGroupProfile.cs index 8ee19541a..c17cf5170 100644 --- a/src/Web/Grand.Web.AdminShared/Mapper/CustomerGroupProfile.cs +++ b/src/Web/Grand.Web.AdminShared/Mapper/CustomerGroupProfile.cs @@ -11,7 +11,8 @@ public CustomerGroupProfile() { CreateMap(); CreateMap() - .ForMember(dest => dest.Id, mo => mo.Ignore()); + .ForMember(dest => dest.Id, mo => mo.Ignore()) + .ForMember(dest => dest.IsSystem, mo => mo.Ignore()); } public int Order => 0; diff --git a/src/Web/Grand.Web.AdminShared/Models/Customers/CustomerGroupModel.cs b/src/Web/Grand.Web.AdminShared/Models/Customers/CustomerGroupModel.cs index 86d716dbc..82e5eae42 100644 --- a/src/Web/Grand.Web.AdminShared/Models/Customers/CustomerGroupModel.cs +++ b/src/Web/Grand.Web.AdminShared/Models/Customers/CustomerGroupModel.cs @@ -1,5 +1,6 @@ using Grand.Infrastructure.ModelBinding; using Grand.Infrastructure.Models; +using Microsoft.AspNetCore.Mvc.ModelBinding; using System.ComponentModel.DataAnnotations; namespace Grand.Web.AdminShared.Models.Customers; @@ -20,6 +21,11 @@ public class CustomerGroupModel : BaseEntityModel [GrandResourceDisplayName("Admin.Customers.CustomerGroups.Fields.Active")] public bool Active { get; set; } + /// + /// Shown, never edited: the system groups are seeded by the installer. Never bound, so a form + /// cannot take the protection off a system group or give it to a new one. + /// + [BindNever] [GrandResourceDisplayName("Admin.Customers.CustomerGroups.Fields.IsSystem")] public bool IsSystem { get; set; } From a98520b75a9502ce7787ab8cee32214edb08c1a9 Mon Sep 17 00:00:00 2001 From: Krzysztof Pajak Date: Mon, 23 Nov 2026 05:37:13 +0100 Subject: [PATCH 2/6] Keep the product, customer and rating of a review out of the edit form (#1086) --- ...s.ProductReviewModel_ToDomain.verified.txt | 3 - .../AdminShared/CatalogProductMappingTests.cs | 19 ++++-- .../ProductReviewViewModelServiceTests.cs | 61 +++++++++++++++++++ .../BaseProductReviewController.cs | 2 +- .../Mapper/ProductReviewProfile.cs | 3 + .../Models/Catalog/ProductReviewModel.cs | 5 ++ .../Services/ProductReviewViewModelService.cs | 7 +-- .../AdminShared/ProductReview/Edit.cshtml | 2 +- .../Partials/CreateOrUpdate.cshtml | 1 - 9 files changed, 88 insertions(+), 15 deletions(-) create mode 100644 src/Tests/Grand.Web.Admin.Tests/Services/ProductReviewViewModelServiceTests.cs 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 4e51a7664..89f0d636b 100644 --- a/src/Web/Grand.Web.AdminShared/Controllers/BaseProductReviewController.cs +++ b/src/Web/Grand.Web.AdminShared/Controllers/BaseProductReviewController.cs @@ -80,7 +80,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 4a75e29d6..2e778e928 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 b23f134e3..ef0cfd86c 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. *@ From fae14d84f72561757fd58b625a7a033419fa970e Mon Sep 17 00:00:00 2001 From: Krzysztof Pajak Date: Mon, 23 Nov 2026 05:37:18 +0100 Subject: [PATCH 3/6] Keep the name of a message template out of the edit form (#1087) --- .../BaseMessageTemplateControllerTests.cs | 35 +++++++++++++++++++ .../BaseMessageTemplateController.cs | 7 ++-- .../Partials/CreateOrUpdate.TabInfo.cshtml | 1 - 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseMessageTemplateControllerTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseMessageTemplateControllerTests.cs index 877ad6981..b3647f8e3 100644 --- a/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseMessageTemplateControllerTests.cs +++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseMessageTemplateControllerTests.cs @@ -282,6 +282,41 @@ public async Task EditPost_Admin_DoesNotForceLimitedToStores() Assert.IsFalse(saved.LimitedToStores); } + [TestMethod] + public async Task EditPost_KeepsStoredNameRegardlessOfSubmittedModel() + { + var template = new MessageTemplate { Id = "mt-1", Name = "OrderPlaced.CustomerNotification" }; + _messageTemplateService.Setup(s => s.GetMessageTemplateById("mt-1")).ReturnsAsync(template); + _scope.Setup(s => s.HasAccess(template)).ReturnsAsync(true); + + MessageTemplate saved = null; + _messageTemplateService.Setup(s => s.UpdateMessageTemplate(It.IsAny())) + .Callback(t => saved = t) + .Returns(Task.CompletedTask); + + var model = new MessageTemplateModel { Id = "mt-1", Name = "Customer.PasswordRecovery" }; + await CreateController().Edit(model, false); + + Assert.IsNotNull(saved); + Assert.AreEqual("OrderPlaced.CustomerNotification", saved.Name); + } + + [TestMethod] + public async Task EditPost_InvalidModel_KeepsSubmittedAttachmentSwitch() + { + var template = new MessageTemplate { Id = "mt-1", Name = "N", AttachedDownloadId = "dl-1" }; + _messageTemplateService.Setup(s => s.GetMessageTemplateById("mt-1")).ReturnsAsync(template); + _scope.Setup(s => s.HasAccess(template)).ReturnsAsync(true); + + var controller = CreateController(); + controller.ModelState.AddModelError("Subject", "required"); + var model = new MessageTemplateModel { Id = "mt-1", AttachedDownloadId = "dl-1", HasAttachedDownload = false }; + var result = await controller.Edit(model, false) as ViewResult; + + Assert.IsNotNull(result); + Assert.IsFalse(((MessageTemplateModel)result.Model).HasAttachedDownload); + } + [TestMethod] public async Task Delete_HasAccessFalse_DoesNotDelete() { diff --git a/src/Web/Grand.Web.AdminShared/Controllers/BaseMessageTemplateController.cs b/src/Web/Grand.Web.AdminShared/Controllers/BaseMessageTemplateController.cs index 7029562e4..8186bde6b 100644 --- a/src/Web/Grand.Web.AdminShared/Controllers/BaseMessageTemplateController.cs +++ b/src/Web/Grand.Web.AdminShared/Controllers/BaseMessageTemplateController.cs @@ -75,7 +75,6 @@ public async Task Create(MessageTemplateModel model, bool continu if (existingStoreTemplate != null) { ModelState.AddModelError("Name", translationService.GetResource("Admin.Content.MessageTemplates.Fields.Name.AlreadyExists")); - model.HasAttachedDownload = !string.IsNullOrEmpty(model.AttachedDownloadId); model.AllowedTokens = messageTokenProvider.GetListOfAllowedTokens(); foreach (var ea in await emailAccountService.GetAllEmailAccounts(scope.DefaultStoreId ?? "")) model.AvailableEmailAccounts.Add(ea.ToModel()); @@ -108,7 +107,6 @@ public async Task Create(MessageTemplateModel model, bool continu return RedirectToAction("List"); } - model.HasAttachedDownload = !string.IsNullOrEmpty(model.AttachedDownloadId); model.AllowedTokens = messageTokenProvider.GetListOfAllowedTokens(); foreach (var ea in await emailAccountService.GetAllEmailAccounts(scope.DefaultStoreId ?? "")) model.AvailableEmailAccounts.Add(ea.ToModel()); @@ -177,10 +175,13 @@ public async Task Edit(MessageTemplateModel model, bool continueE return RedirectToAction("List"); var prevAttachment = messageTemplate.AttachedDownloadId; + //the name is the key the template is sent by: set on create, never changed by the edit form + var name = messageTemplate.Name; if (ModelState.IsValid) { messageTemplate = model.ToEntity(messageTemplate); + messageTemplate.Name = name; if (!model.HasAttachedDownload) messageTemplate.AttachedDownloadId = ""; if (model.SendImmediately) @@ -212,7 +213,7 @@ public async Task Edit(MessageTemplateModel model, bool continueE return RedirectToAction("List"); } - model.HasAttachedDownload = !string.IsNullOrEmpty(model.AttachedDownloadId); + model.Name = name; model.AllowedTokens = messageTokenProvider.GetListOfAllowedTokens(); foreach (var ea in await emailAccountService.GetAllEmailAccounts(scope.DefaultStoreId ?? "")) model.AvailableEmailAccounts.Add(ea.ToModel()); diff --git a/src/Web/Grand.Web.AdminShared/Views/AdminShared/MessageTemplate/Partials/CreateOrUpdate.TabInfo.cshtml b/src/Web/Grand.Web.AdminShared/Views/AdminShared/MessageTemplate/Partials/CreateOrUpdate.TabInfo.cshtml index 9831d1c37..ad5f3257f 100644 --- a/src/Web/Grand.Web.AdminShared/Views/AdminShared/MessageTemplate/Partials/CreateOrUpdate.TabInfo.cshtml +++ b/src/Web/Grand.Web.AdminShared/Views/AdminShared/MessageTemplate/Partials/CreateOrUpdate.TabInfo.cshtml @@ -48,7 +48,6 @@ @if (!string.IsNullOrEmpty(Model.Id)) {
@Model.Name
- } else { From 3dfe4252511181e3d15cb66f02d611419cefed15 Mon Sep 17 00:00:00 2001 From: support Date: Mon, 23 Nov 2026 05:43:55 +0100 Subject: [PATCH 4/6] Use ITranslationService in the product review service test on 2.4.x --- .../Services/ProductReviewViewModelServiceTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Tests/Grand.Web.Admin.Tests/Services/ProductReviewViewModelServiceTests.cs b/src/Tests/Grand.Web.Admin.Tests/Services/ProductReviewViewModelServiceTests.cs index a58bcc0f3..11e1692e2 100644 --- a/src/Tests/Grand.Web.Admin.Tests/Services/ProductReviewViewModelServiceTests.cs +++ b/src/Tests/Grand.Web.Admin.Tests/Services/ProductReviewViewModelServiceTests.cs @@ -1,12 +1,12 @@ 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.Localization; 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; @@ -31,7 +31,7 @@ public void Init() _service = new ProductReviewViewModelService(_productService.Object, _productReviewService.Object, Mock.Of(), Mock.Of(), Mock.Of(), - Mock.Of(), _mediator.Object); + Mock.Of(), _mediator.Object); } [TestMethod] From 2b7ec80ec4f620a7d2f16e2eadf1c3707c1f7bf8 Mon Sep 17 00:00:00 2001 From: support Date: Mon, 23 Nov 2026 07:10:32 +0100 Subject: [PATCH 5/6] Check the stored customer group, not the posted flag, before an API update --- .../Controllers/CustomerGroupController.cs | 16 ++-- .../CustomerGroupControllerTests.cs | 94 +++++++++++++++++++ 2 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 src/Tests/Grand.Module.Api.Tests/Controllers/CustomerGroupControllerTests.cs diff --git a/src/Modules/Grand.Module.Api/Controllers/CustomerGroupController.cs b/src/Modules/Grand.Module.Api/Controllers/CustomerGroupController.cs index 84f7ad6d2..51f1ec570 100644 --- a/src/Modules/Grand.Module.Api/Controllers/CustomerGroupController.cs +++ b/src/Modules/Grand.Module.Api/Controllers/CustomerGroupController.cs @@ -73,17 +73,21 @@ public async Task Post([FromBody] CustomerGroupDto model) [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(CustomerGroupDto))] [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task Put([FromBody] CustomerGroupDto model) { + if (string.IsNullOrEmpty(model.Id)) + return BadRequest("Key is null or empty"); + if (!await _permissionService.Authorize(PermissionSystemName.Customers)) return Forbid(); - if (!model.IsSystem) - { - model = await _mediator.Send(new UpdateCustomerGroupCommand { Model = model }); - return Ok(model); - } + var customerGroup = await _mediator.Send(new GetGenericQuery(model.Id)); + if (!customerGroup.Any()) return NotFound(); + + if (customerGroup.FirstOrDefault()!.IsSystem) return BadRequest(ModelState); - return BadRequest(ModelState); + model = await _mediator.Send(new UpdateCustomerGroupCommand { Model = model }); + return Ok(model); } [EndpointDescription("Partially update entity in CustomerGroup")] diff --git a/src/Tests/Grand.Module.Api.Tests/Controllers/CustomerGroupControllerTests.cs b/src/Tests/Grand.Module.Api.Tests/Controllers/CustomerGroupControllerTests.cs new file mode 100644 index 000000000..1bee1cf8b --- /dev/null +++ b/src/Tests/Grand.Module.Api.Tests/Controllers/CustomerGroupControllerTests.cs @@ -0,0 +1,94 @@ +using Grand.Business.Core.Interfaces.Common.Security; +using Grand.Domain.Customers; +using Grand.Mediator; +using Grand.Module.Api.Commands.Models.Customers; +using Grand.Module.Api.Controllers; +using Grand.Module.Api.DTOs.Customers; +using Grand.Module.Api.Queries.Models.Common; +using Microsoft.AspNetCore.JsonPatch.SystemTextJson; +using Microsoft.AspNetCore.Mvc; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; + +namespace Grand.Module.Api.Tests.Controllers; + +[TestClass] +public class CustomerGroupControllerTests +{ + private Mock _mediatorMock; + private CustomerGroupController _controller; + + [TestInitialize] + public void Init() + { + _mediatorMock = new Mock(); + var permissionService = new Mock(); + permissionService.Setup(p => p.Authorize(It.IsAny())).ReturnsAsync(true); + _controller = new CustomerGroupController(_mediatorMock.Object, permissionService.Object); + } + + private void SetupStoredGroup(CustomerGroupDto group) + { + _mediatorMock.Setup(m => m.Send(It.IsAny>(), + It.IsAny())) + .ReturnsAsync((group is null ? [] : new[] { group }).AsQueryable()); + } + + [TestMethod] + public async Task Put_StoredSystemGroup_ReturnsBadRequestWithoutUpdating() + { + SetupStoredGroup(new CustomerGroupDto { Id = "cg-1", Name = "Administrators", IsSystem = true }); + + var result = await _controller.Put(new CustomerGroupDto { Id = "cg-1", Name = "Renamed", IsSystem = false }); + + Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult)); + _mediatorMock.Verify(m => m.Send(It.IsAny(), It.IsAny()), + Times.Never); + } + + [TestMethod] + public async Task Put_StoredGroupIsNotSystem_Updates() + { + SetupStoredGroup(new CustomerGroupDto { Id = "cg-2", Name = "VIP" }); + + var result = await _controller.Put(new CustomerGroupDto { Id = "cg-2", Name = "VIP Customers" }); + + Assert.IsInstanceOfType(result, typeof(OkObjectResult)); + _mediatorMock.Verify(m => m.Send(It.Is(c => c.Model.Id == "cg-2"), + It.IsAny()), Times.Once); + } + + [TestMethod] + public async Task Put_UnknownGroup_ReturnsNotFound() + { + SetupStoredGroup(null); + + var result = await _controller.Put(new CustomerGroupDto { Id = "cg-missing", Name = "VIP" }); + + Assert.IsInstanceOfType(result, typeof(NotFoundResult)); + } + + [TestMethod] + public async Task Put_WithoutId_ReturnsBadRequest() + { + var result = await _controller.Put(new CustomerGroupDto { Name = "VIP" }); + + Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult)); + _mediatorMock.Verify(m => m.Send(It.IsAny(), It.IsAny()), + Times.Never); + } + + [TestMethod] + public async Task Patch_StoredSystemGroup_ReturnsBadRequestWithoutUpdating() + { + SetupStoredGroup(new CustomerGroupDto { Id = "cg-1", Name = "Administrators", IsSystem = true }); + var patch = new JsonPatchDocument(); + patch.Replace(x => x.IsSystem, false); + + var result = await _controller.Patch("cg-1", patch); + + Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult)); + _mediatorMock.Verify(m => m.Send(It.IsAny(), It.IsAny()), + Times.Never); + } +} From 59d3f467e4587d7c61d4d989265944423c22fb1f Mon Sep 17 00:00:00 2001 From: support Date: Mon, 23 Nov 2026 07:10:33 +0100 Subject: [PATCH 6/6] Drop the unused product id from the product review edit links --- .../Grand.Web.Admin/Areas/Admin/Views/ProductReview/List.cshtml | 2 +- .../Grand.Web.Store/Areas/Store/Views/ProductReview/List.cshtml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Web/Grand.Web.Admin/Areas/Admin/Views/ProductReview/List.cshtml b/src/Web/Grand.Web.Admin/Areas/Admin/Views/ProductReview/List.cshtml index a8eb3d54a..a01b13b82 100644 --- a/src/Web/Grand.Web.Admin/Areas/Admin/Views/ProductReview/List.cshtml +++ b/src/Web/Grand.Web.Admin/Areas/Admin/Views/ProductReview/List.cshtml @@ -75,7 +75,7 @@ - {{ Title }} + {{ Title }} diff --git a/src/Web/Grand.Web.Store/Areas/Store/Views/ProductReview/List.cshtml b/src/Web/Grand.Web.Store/Areas/Store/Views/ProductReview/List.cshtml index 129fc88ca..6e50c220b 100644 --- a/src/Web/Grand.Web.Store/Areas/Store/Views/ProductReview/List.cshtml +++ b/src/Web/Grand.Web.Store/Areas/Store/Views/ProductReview/List.cshtml @@ -74,7 +74,7 @@ - {{ Title }} + {{ Title }}