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.

6.7 KB
Raw Blame
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Business.Core.Interfaces.Common.Stores;
using Grand.Business.Core.Interfaces.Messages;
using Grand.Business.Marketing.Services.Campaigns;
using Grand.Data;
using Grand.Data.Tests.MongoDb;
using Grand.Domain.Customers;
using Grand.Domain.Localization;
using Grand.Domain.Messages;
using Grand.Domain.Stores;
using Grand.Infrastructure;
using Grand.Infrastructure.Events;
using Grand.Mediator;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Grand.Business.Marketing.Tests.Services.Campaigns;

[TestClass]
public class CampaignServiceTests
{
    private IRepository<CampaignHistory> _campaignHistoryRepository;
    private IRepository<Campaign> _campaignRepository;
    private CampaignService _campaignService;
    private IRepository<Customer> _customerRepository;
    private Mock<IEmailSender> _emailSenderMock;
    private Mock<ILanguageService> _languageServiceMock;
    private Mock<IMediator> _mediatorMock;
    private IRepository<NewsLetterSubscription> _newsLetterSubscriptionRepository;
    private Mock<IQueuedEmailService> _queuedEmailServiceMock;
    private Mock<IStoreService> _storeServiceMock;
    private Mock<IContextAccessor> _workContextMock;

    [TestInitialize]
    public void Init()
    {
        _campaignRepository = new MongoDBRepositoryTest<Campaign>();
        _campaignHistoryRepository = new MongoDBRepositoryTest<CampaignHistory>();
        _newsLetterSubscriptionRepository = new MongoDBRepositoryTest<NewsLetterSubscription>();
        _customerRepository = new MongoDBRepositoryTest<Customer>();
        _mediatorMock = new Mock<IMediator>();
        _emailSenderMock = new Mock<IEmailSender>();
        _queuedEmailServiceMock = new Mock<IQueuedEmailService>();
        _storeServiceMock = new Mock<IStoreService>();
        _languageServiceMock = new Mock<ILanguageService>();
        _workContextMock = new Mock<IContextAccessor>();
        _campaignService = new CampaignService(_campaignRepository, _campaignHistoryRepository,
            _newsLetterSubscriptionRepository, _customerRepository,
            _emailSenderMock.Object, _queuedEmailServiceMock.Object, _storeServiceMock.Object, _mediatorMock.Object,
            _languageServiceMock.Object, _workContextMock.Object);
    }

    [TestMethod]
    public async Task InsertCampaignTest()
    {
        //Act
        await _campaignService.InsertCampaign(new Campaign());
        //Assert
        _mediatorMock.Verify(c => c.Publish(It.IsAny<EntityInserted<Campaign>>(), default), Times.Once);
        Assert.IsNotEmpty(_campaignRepository.Table);
    }

    [TestMethod]
    public async Task InsertCampaignHistoryTest()
    {
        //Act
        await _campaignService.InsertCampaignHistory(new CampaignHistory());
        //Assert
        Assert.IsNotEmpty(_campaignHistoryRepository.Table);
    }

    [TestMethod]
    public async Task UpdateCampaignTest()
    {
        //Arrange
        var campaign = new Campaign();
        await _campaignService.InsertCampaign(campaign);

        //Act
        campaign.Subject = "test";
        await _campaignService.UpdateCampaign(campaign);
        //Assert
        _mediatorMock.Verify(c => c.Publish(It.IsAny<EntityUpdated<Campaign>>(), default), Times.Once);
        Assert.AreEqual("test", _campaignRepository.Table.FirstOrDefault(x => x.Id == campaign.Id).Subject);
    }

    [TestMethod]
    public async Task DeleteCampaignTest()
    {
        //Arrange
        var campaign = new Campaign();
        await _campaignService.InsertCampaign(campaign);

        //Act
        await _campaignService.DeleteCampaign(campaign);
        //Assert
        _mediatorMock.Verify(c => c.Publish(It.IsAny<EntityDeleted<Campaign>>(), default), Times.Once);
        Assert.IsEmpty(_campaignRepository.Table);
    }

    [TestMethod]
    public async Task GetCampaignByIdTest()
    {
        //Arrange
        var campaign = new Campaign {
            Name = "test"
        };
        await _campaignRepository.InsertAsync(campaign);

        //Act
        var result = await _campaignService.GetCampaignById(campaign.Id);

        //Assert
        Assert.IsNotNull(result);
        Assert.AreEqual("test", result.Name);
    }

    [TestMethod]
    public async Task GetAllCampaignsTest()
    {
        //Arrange
        await _campaignRepository.InsertAsync(new Campaign());
        await _campaignRepository.InsertAsync(new Campaign());
        await _campaignRepository.InsertAsync(new Campaign());

        //Act
        var result = await _campaignService.GetAllCampaigns();

        //Assert
        Assert.HasCount(3, result);
    }

    [TestMethod]
    public async Task GetCampaignHistoryTest()
    {
        //Arrange
        var campaign = new Campaign {
            Name = "test"
        };
        await _campaignRepository.InsertAsync(campaign);
        await _campaignHistoryRepository.InsertAsync(new CampaignHistory { CampaignId = campaign.Id });
        await _campaignHistoryRepository.InsertAsync(new CampaignHistory { CampaignId = campaign.Id });

        //Act
        var result = await _campaignService.GetCampaignHistory(campaign);

        //Assert
        Assert.HasCount(2, result);
    }

    [TestMethod]
    public async Task CustomerSubscriptionsTest()
    {
        //Arrange
        var campaign = new Campaign {
            Name = "test",
            StoreId = "1"
        };
        await _campaignRepository.InsertAsync(campaign);
        await _newsLetterSubscriptionRepository.InsertAsync(new NewsLetterSubscription
            { StoreId = "1", Active = true });
        //Act
        var result = await _campaignService.CustomerSubscriptions(campaign);

        //Assert
        Assert.HasCount(1, result);
    }

    [TestMethod]
    public async Task SendCampaignTest()
    {
        //Arrange
        var campaign = new Campaign {
            Name = "test",
            StoreId = "1"
        };
        await _campaignRepository.InsertAsync(campaign);
        await _newsLetterSubscriptionRepository.InsertAsync(new NewsLetterSubscription
            { StoreId = "1", Active = true, Email = "[email protected]" });

        _languageServiceMock.Setup(x => x.GetLanguageById(It.IsAny<string>()))
            .Returns(Task.FromResult(new Language()));
        _storeServiceMock.Setup(x => x.GetStoreById(It.IsAny<string>()))
            .Returns(Task.FromResult(new Store()));

        //Act
        await _campaignService.SendCampaign(campaign, new EmailAccount { Host = "host.com" }, "[email protected]");

        //Assert
        _emailSenderMock.Verify(
            c => c.SendEmail(It.IsAny<EmailAccount>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(),
                It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), null, null, null, null, null, null, null),
            Times.Once);
    }
}