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.

3.4 KB
Raw Blame
using Grand.Domain.Common;
using Grand.Domain.Directory;
using Grand.Domain.Orders;
using Grand.Domain.Stores;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Grand.Domain.Tests.Orders;

[TestClass]
public class GiftVoucherExtensionsTests
{
    private readonly GiftVoucher giftVoucher = new() {
        Amount = 10,
        Code = "123456",
        CurrencyCode = "USD",
        IsGiftVoucherActivated = true
    };

    [TestMethod]
    public void AddGiftVoucherAttributeTest()
    {
        IList<CustomAttribute> customAttributes = new List<CustomAttribute>();
        GiftVoucherExtensions.AddGiftVoucherAttribute(customAttributes, "Johny", "[email protected]", "My name",
            "[email protected]", "my sample message");

        GiftVoucherExtensions.GetGiftVoucherAttribute(customAttributes, out var giftVoucherRecipientName,
            out var giftVoucherRecipientEmail,
            out var giftVoucherSenderName, out var giftVoucherSenderEmail, out var giftVoucherMessage);

        Assert.AreEqual("Johny", giftVoucherRecipientName);
        Assert.AreEqual("[email protected]", giftVoucherRecipientEmail);
        Assert.AreEqual("My name", giftVoucherSenderName);
        Assert.AreEqual("[email protected]", giftVoucherSenderEmail);
        Assert.AreEqual("my sample message", giftVoucherMessage);
    }

    [TestMethod]
    public void GetGiftVoucherAttributeTest()
    {
        IList<CustomAttribute> customAttributes = new List<CustomAttribute>();
        customAttributes.Add(new CustomAttribute { Key = "RecipientName", Value = "Johny" });
        customAttributes.Add(new CustomAttribute { Key = "RecipientEmail", Value = "[email protected]" });
        customAttributes.Add(new CustomAttribute { Key = "SenderName", Value = "My name" });
        customAttributes.Add(new CustomAttribute { Key = "SenderEmail", Value = "[email protected]" });
        customAttributes.Add(new CustomAttribute { Key = "Message", Value = "my sample message" });
        customAttributes.Add(new CustomAttribute { Key = "Test", Value = "my test" });

        GiftVoucherExtensions.GetGiftVoucherAttribute(customAttributes, out var giftVoucherRecipientName,
            out var giftVoucherRecipientEmail,
            out var giftVoucherSenderName, out var giftVoucherSenderEmail, out var giftVoucherMessage);

        Assert.AreEqual("Johny", giftVoucherRecipientName);
        Assert.AreEqual("[email protected]", giftVoucherRecipientEmail);
        Assert.AreEqual("My name", giftVoucherSenderName);
        Assert.AreEqual("[email protected]", giftVoucherSenderEmail);
        Assert.AreEqual("my sample message", giftVoucherMessage);
    }

    [TestMethod]
    public void GetGiftVoucherRemainingAmountTest()
    {
        giftVoucher.GiftVoucherUsageHistory.Add(new GiftVoucherUsageHistory { UsedValue = 3 });
        giftVoucher.GiftVoucherUsageHistory.Add(new GiftVoucherUsageHistory { UsedValue = 4 });
        Assert.AreEqual(3, giftVoucher.GetGiftVoucherRemainingAmount());
    }

    [TestMethod]
    public void IsGiftVoucherValidTest_ValidTo_True()
    {
        giftVoucher.ValidTo = DateTime.UtcNow.AddDays(1);
        Assert.IsTrue(giftVoucher.IsGiftVoucherValid(new Currency { CurrencyCode = "USD" }, new Store()));
    }

    [TestMethod]
    public void IsGiftVoucherValidTest_False()
    {
        giftVoucher.ValidTo = DateTime.UtcNow.AddDays(-1);
        Assert.IsFalse(giftVoucher.IsGiftVoucherValid(new Currency { CurrencyCode = "USD" }, new Store()));
    }
}