Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

2.1 KB
Raw Blame
using Grand.Business.Cms.Services;
using Grand.Business.Core.Interfaces.Cms;
using Grand.Domain.Common;
using Grand.Domain.Customers;
using Grand.Domain.Stores;
using Grand.Infrastructure.TypeConverters.Converter;
using Grand.SharedKernel.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System.ComponentModel;

namespace Grand.Business.Cms.Tests.Services;

[TestClass]
public class CookiePreferenceTests
{
    private CookiePreference _cookiePreferences;

    [TestInitialize]
    public void Init()
    {
        var cookie1 = new Mock<IConsentCookie>();
        var cookie2 = new Mock<IConsentCookie>();
        cookie1.Setup(c => c.DisplayOrder).Returns(1);
        cookie1.Setup(c => c.SystemName).Returns("cookie1");
        cookie2.Setup(c => c.DisplayOrder).Returns(2);
        cookie2.Setup(c => c.SystemName).Returns("cookie2");

        _cookiePreferences = new CookiePreference(new List<IConsentCookie> { cookie1.Object, cookie2.Object });
    }

    [TestMethod]
    public void GetConsentCookies_ReturnCorectOrder()
    {
        var result = _cookiePreferences.GetConsentCookies();
        Assert.AreEqual(1, result.First().DisplayOrder);
        Assert.AreEqual(2, result.Last().DisplayOrder);
    }

    [TestMethod]
    public async Task IsEnable_ReturnExpectedValue()
    {
        var dic = new Dictionary<string, bool>();
        dic.Add("cookie1", true);
        dic.Add("cookie2", false);

        TypeDescriptor.AddAttributes(typeof(Dictionary<string, bool>),
           new TypeConverterAttribute(typeof(GenericDictionaryTypeConverter<string, bool>)));

        var store = new Store();
        var valueStr = CommonHelper.To<string>(dic);
        var customer = new Customer();
        customer.UserFields.Add(new UserField(){ StoreId = store.Id, Key = SystemCustomerFieldNames.ConsentCookies, Value = valueStr});
        var result1 = await _cookiePreferences.IsEnable(customer, store, "cookie1");
        var result2 = await _cookiePreferences.IsEnable(customer, store, "cookie2");
        Assert.IsTrue(result1.Value);
        Assert.IsFalse(result2.Value);
    }
}