Skip to content
Open
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
Expand Up @@ -232,9 +232,12 @@ public virtual async Task<IList<StateProvince>> GetStateProvincesByCountryId(str
if (string.IsNullOrEmpty(countryId))
return new List<StateProvince>();

//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<StateProvince>();

return country?.StateProvinces.OrderBy(c => c.DisplayOrder)
return country.StateProvinces.OrderBy(c => c.DisplayOrder)
.ThenBy(c => c.GetTranslation(x => x.Name, languageId))
.ToList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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<IWorkContext>();
workContextMock.Setup(x => x.CurrentCustomer).Returns(new Customer { Id = "c1" });
var contextAccessorMock = new Mock<IContextAccessor>();
contextAccessorMock.Setup(x => x.WorkContext).Returns(workContextMock.Object);

_controller = new ProductController(
new Mock<IProductService>().Object,
contextAccessorMock.Object,
new Mock<IRequestTranslationService>().Object,
new Mock<IRecentlyViewedProductsService>().Object,
new Mock<IShoppingCartService>().Object,
new Mock<IAclService>().Object,
new Mock<IPermissionService>().Object,
new Mock<IMediator>().Object,
new CatalogSettings(),
new CaptchaSettings(),
new Mock<IGuestCustomerPersistence>().Object,
new Mock<ILogger<ProductController>>().Object) {
ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() }
};
}

[TestMethod]
public async Task SetProductReviewHelpfulness_ReviewDeletedMeanwhile_ReturnsNotFound()
{
var reviewService = new Mock<IProductReviewService>();
reviewService.Setup(x => x.GetProductReviewById("gone")).ReturnsAsync((ProductReview)null);

var result = await _controller.SetProductReviewHelpfulness("gone", "p1", true,
new Mock<ICustomerService>().Object, new Mock<IGroupService>().Object, reviewService.Object);

Assert.IsInstanceOfType(result, typeof(NotFoundResult));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ public virtual async Task<IActionResult> SubscribeButton(string productId, strin
[HttpPost]
public virtual async Task<IActionResult> 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 :
Expand Down
3 changes: 2 additions & 1 deletion src/Web/Grand.Web/Controllers/ProductController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -671,9 +671,10 @@ public virtual async Task<IActionResult> 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)
Expand Down
Loading