diff --git a/src/Business/Grand.Business.Common/Services/Directory/CountryService.cs b/src/Business/Grand.Business.Common/Services/Directory/CountryService.cs index e30261081..ce5754145 100644 --- a/src/Business/Grand.Business.Common/Services/Directory/CountryService.cs +++ b/src/Business/Grand.Business.Common/Services/Directory/CountryService.cs @@ -232,9 +232,12 @@ public virtual async Task> GetStateProvincesByCountryId(str if (string.IsNullOrEmpty(countryId)) return new List(); + //a country deleted meanwhile has no states either, as a country not chosen yet var country = await GetCountryById(countryId); + if (country == null) + return new List(); - return country?.StateProvinces.OrderBy(c => c.DisplayOrder) + return country.StateProvinces.OrderBy(c => c.DisplayOrder) .ThenBy(c => c.GetTranslation(x => x.Name, languageId)) .ToList(); } diff --git a/src/Tests/Grand.Business.Common.Tests/Services/Directory/CountryServiceTests.cs b/src/Tests/Grand.Business.Common.Tests/Services/Directory/CountryServiceTests.cs index 34a997889..05c7ac343 100644 --- a/src/Tests/Grand.Business.Common.Tests/Services/Directory/CountryServiceTests.cs +++ b/src/Tests/Grand.Business.Common.Tests/Services/Directory/CountryServiceTests.cs @@ -170,6 +170,15 @@ public async Task GetStateProvincesByCountryIdTest() Assert.IsTrue(result.Any()); } + [TestMethod] + public async Task GetStateProvincesByCountryId_UnknownCountry_ReturnsNoStates() + { + var result = await _countryService.GetStateProvincesByCountryId("0123456789abcdef01234567"); + + Assert.IsNotNull(result); + Assert.AreEqual(0, result.Count); + } + [TestMethod] public async Task InsertStateProvinceTest() { diff --git a/src/Tests/Grand.Web.Tests/Controllers/ProductControllerReviewHelpfulnessTests.cs b/src/Tests/Grand.Web.Tests/Controllers/ProductControllerReviewHelpfulnessTests.cs new file mode 100644 index 000000000..027c193b6 --- /dev/null +++ b/src/Tests/Grand.Web.Tests/Controllers/ProductControllerReviewHelpfulnessTests.cs @@ -0,0 +1,63 @@ +using Grand.Business.Core.Interfaces.Catalog.Products; +using Grand.Business.Core.Interfaces.Checkout.Orders; +using Grand.Business.Core.Interfaces.Common.Directory; +using Grand.Business.Core.Interfaces.Common.Security; +using Grand.Business.Core.Interfaces.Customers; +using Grand.Domain.Catalog; +using Grand.Domain.Common; +using Grand.Domain.Customers; +using Grand.Infrastructure; +using Grand.Mediator; +using Grand.Web.Common.Localization; +using Grand.Web.Common.RequestCustomerResolvers; +using Grand.Web.Controllers; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; + +namespace Grand.Web.Tests.Controllers; + +[TestClass] +public class ProductControllerReviewHelpfulnessTests +{ + private ProductController _controller; + + [TestInitialize] + public void Init() + { + var workContextMock = new Mock(); + workContextMock.Setup(x => x.CurrentCustomer).Returns(new Customer { Id = "c1" }); + var contextAccessorMock = new Mock(); + contextAccessorMock.Setup(x => x.WorkContext).Returns(workContextMock.Object); + + _controller = new ProductController( + new Mock().Object, + contextAccessorMock.Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new CatalogSettings(), + new CaptchaSettings(), + new Mock().Object, + new Mock>().Object) { + ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() } + }; + } + + [TestMethod] + public async Task SetProductReviewHelpfulness_ReviewDeletedMeanwhile_ReturnsNotFound() + { + var reviewService = new Mock(); + reviewService.Setup(x => x.GetProductReviewById("gone")).ReturnsAsync((ProductReview)null); + + var result = await _controller.SetProductReviewHelpfulness("gone", "p1", true, + new Mock().Object, new Mock().Object, reviewService.Object); + + Assert.IsInstanceOfType(result, typeof(NotFoundResult)); + } +} diff --git a/src/Web/Grand.Web/Controllers/OutOfStockSubscriptionController.cs b/src/Web/Grand.Web/Controllers/OutOfStockSubscriptionController.cs index 771578fef..64f2e035f 100644 --- a/src/Web/Grand.Web/Controllers/OutOfStockSubscriptionController.cs +++ b/src/Web/Grand.Web/Controllers/OutOfStockSubscriptionController.cs @@ -98,9 +98,10 @@ public virtual async Task SubscribeButton(string productId, strin [HttpPost] public virtual async Task SubscribePopup(ProductModel model) { + //the product may have been deleted while the page was open var product = await _productService.GetProductById(model.ProductId); if (product == null) - throw new ArgumentException("No product found with the specified id"); + return NotFound(); var customer = _contextAccessor.WorkContext.CurrentCustomer; var warehouseId = _shoppingCartSettings.AllowToSelectWarehouse ? model.WarehouseId : diff --git a/src/Web/Grand.Web/Controllers/ProductController.cs b/src/Web/Grand.Web/Controllers/ProductController.cs index 3e2b88cfa..17bc9e530 100644 --- a/src/Web/Grand.Web/Controllers/ProductController.cs +++ b/src/Web/Grand.Web/Controllers/ProductController.cs @@ -671,9 +671,10 @@ public virtual async Task SetProductReviewHelpfulness(string prod [FromServices] IGroupService groupService, [FromServices] IProductReviewService productReviewService) { + //the review may have been deleted while the page was open var productReview = await productReviewService.GetProductReviewById(productReviewId); if (productReview == null) - throw new ArgumentException("No product review found with the specified id"); + return NotFound(); if (await groupService.IsGuest(_contextAccessor.WorkContext.CurrentCustomer) && !_catalogSettings.AllowAnonymousUsersToReviewProduct)