Skip to content
Merged
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 @@ -392,7 +392,9 @@ public virtual async Task DeleteShoppingCartItem(Customer customer, ShoppingCart
/// </summary>
/// <param name="fromCustomer">From customer</param>
/// <param name="toCustomer">To customer</param>
/// <param name="storeId">Store whose selected checkout attributes move along</param>
/// <param name="storeId">
/// Store whose selected checkout attributes move along, when the source customer has selected any there
/// </param>
/// <param name="includeCouponCodes">
/// A value indicating whether to coupon codes (discount and gift voucher) should be also
/// re-applied
Expand Down Expand Up @@ -440,10 +442,13 @@ await AddToCart(toCustomer, sci.ProductId, sci.ShoppingCartTypeId, sci.StoreId,
await _customerService.UpdateUserField(toCustomer, SystemCustomerFieldNames.GiftVoucherCoupons, resultGift);
}

//move selected checkout attributes
//move selected checkout attributes; the source customer's selection is the most recent one,
//but when it has none, the target keeps its own, so logging in from a fresh browser
//does not clear a choice saved on the account
var checkoutAttributes = fromCustomer.GetUserFieldFromEntity<List<CustomAttribute>>(SystemCustomerFieldNames.CheckoutAttributes, storeId);
await _customerService.UpdateUserField(toCustomer, SystemCustomerFieldNames.CheckoutAttributes, checkoutAttributes,
storeId);
if (checkoutAttributes is { Count: > 0 })
await _customerService.UpdateUserField(toCustomer, SystemCustomerFieldNames.CheckoutAttributes, checkoutAttributes,
storeId);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
using Grand.Domain.Stores;
using Grand.Domain.Tax;
using Grand.Infrastructure;
using Grand.Infrastructure.TypeConverters;
using Grand.Mediator;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System.ComponentModel;

namespace Grand.Business.Checkout.Tests.Services.Orders;

Expand All @@ -27,6 +29,13 @@ public class ShoppingCartServiceTests
private ShoppingCartSettings _shoppingCartSettings;
private Mock<IShoppingCartValidator> _shoppingCartValidatorMock;

[ClassInitialize]
public static void InitClass(TestContext context)
{
//user fields are stored as strings; the converters read them back as typed values
new WebTypeConverter().Register();
}

[TestInitialize]
public void Init()
{
Expand Down Expand Up @@ -283,13 +292,76 @@ public async Task MigrateShoppingCart_MovesCheckoutAttributesOfTheGivenStore()
{
//Arrange
var fromCustomer = new Customer();
AddCheckoutAttributes(fromCustomer, "store-b", "gift-wrapping", "yes");
var toCustomer = new Customer();

//Act
await _shoppingCartService.MigrateShoppingCart(fromCustomer, toCustomer, "store-b", false);

//Assert
_customerServiceMock.Verify(x => x.UpdateUserField(toCustomer, SystemCustomerFieldNames.CheckoutAttributes,
It.IsAny<List<CustomAttribute>>(), "store-b"), Times.Once);
It.Is<List<CustomAttribute>>(a => a.Count == 1 && a[0].Key == "gift-wrapping" && a[0].Value == "yes"),
"store-b"), Times.Once);
}

[TestMethod]
public async Task MigrateShoppingCart_GuestWithoutCheckoutAttributes_KeepsTheAccountAttributes()
{
//Arrange
var guest = new Customer();
var customer = new Customer();
AddCheckoutAttributes(customer, "store", "gift-wrapping", "yes");

//Act
await _shoppingCartService.MigrateShoppingCart(guest, customer, "store", false);

//Assert
_customerServiceMock.Verify(x => x.UpdateUserField(It.IsAny<Customer>(),
SystemCustomerFieldNames.CheckoutAttributes, It.IsAny<List<CustomAttribute>>(), It.IsAny<string>()),
Times.Never);
}

[TestMethod]
public async Task MigrateShoppingCart_GuestWithCheckoutAttributes_ReplacesTheAccountAttributes()
{
//Arrange
var guest = new Customer();
AddCheckoutAttributes(guest, "store", "gift-wrapping", "no");
var customer = new Customer();
AddCheckoutAttributes(customer, "store", "gift-wrapping", "yes");

//Act
await _shoppingCartService.MigrateShoppingCart(guest, customer, "store", false);

//Assert
_customerServiceMock.Verify(x => x.UpdateUserField(customer, SystemCustomerFieldNames.CheckoutAttributes,
It.Is<List<CustomAttribute>>(a => a.Count == 1 && a[0].Value == "no"), "store"), Times.Once);
}

[TestMethod]
public async Task MigrateShoppingCart_AccountWithAttributesInAnotherStore_TakesTheGuestAttributes()
{
//Arrange
var guest = new Customer();
AddCheckoutAttributes(guest, "store-b", "gift-wrapping", "yes");
var customer = new Customer();
AddCheckoutAttributes(customer, "store-a", "gift-wrapping", "no");

//Act
await _shoppingCartService.MigrateShoppingCart(guest, customer, "store-b", false);

//Assert
_customerServiceMock.Verify(x => x.UpdateUserField(customer, SystemCustomerFieldNames.CheckoutAttributes,
It.Is<List<CustomAttribute>>(a => a.Count == 1 && a[0].Value == "yes"), "store-b"), Times.Once);
}

private static void AddCheckoutAttributes(Customer customer, string storeId, string key, string value)
{
customer.UserFields.Add(new UserField {
Key = SystemCustomerFieldNames.CheckoutAttributes,
Value = TypeDescriptor.GetConverter(typeof(List<CustomAttribute>))
.ConvertToInvariantString(new List<CustomAttribute> { new() { Key = key, Value = value } }),
StoreId = storeId
});
}
}
Loading