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.9 KB
Raw Blame
using Grand.Business.Cms.Services;
using Grand.Data;
using Grand.Data.Tests.MongoDb;
using Grand.Domain.Common;
using Grand.Domain.Customers;
using Grand.Domain.Stores;
using Grand.Infrastructure;
using Grand.Infrastructure.Caching;
using Grand.Infrastructure.Configuration;
using Grand.Infrastructure.Tests.Caching;
using Grand.Mediator;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Grand.Business.Cms.Tests.Services;

[TestClass]
public class RobotsTxtServiceTests
{
    private MemoryCacheBase _cacheBase;
    private Mock<IMediator> _mediatorMock;

    private IRepository<RobotsTxt> _repository;
    private RobotsTxtService _robotsTxtService;
    private Mock<IWorkContext> _workContextMock;
    private Mock<IStoreContext> _storeContextMock;

    [TestInitialize]
    public void Init()
    {
        _repository = new MongoDBRepositoryTest<RobotsTxt>();

        _mediatorMock = new Mock<IMediator>();
        _workContextMock = new Mock<IWorkContext>();
        _storeContextMock = new Mock<IStoreContext>();
        _cacheBase = new MemoryCacheBase(MemoryCacheTest.Get(), _mediatorMock.Object,
            new CacheConfig { DefaultCacheTimeMinutes = 1 });

        _storeContextMock.Setup(c => c.CurrentStore).Returns(() => new Store { Id = "", Name = "test store" });
        _workContextMock.Setup(c => c.CurrentCustomer).Returns(() => new Customer());

        _robotsTxtService = new RobotsTxtService(_repository, _mediatorMock.Object, _cacheBase);
    }

    [TestMethod]
    public async Task GetRobotsTxtTest()
    {
        //Arrange
        var robotsTxt = new RobotsTxt { StoreId = "1" };
        await _repository.InsertAsync(robotsTxt);
        //Act
        var result = await _robotsTxtService.GetRobotsTxt(robotsTxt.StoreId);
        //Assert
        Assert.IsNotNull(result);
    }

    [TestMethod]
    public async Task InsertRobotsTxtTest()
    {
        //Arrange
        var robotsTxt = new RobotsTxt { StoreId = "1" };
        //Act
        await _robotsTxtService.InsertRobotsTxt(robotsTxt);
        //Assert
        Assert.IsNotNull(_repository.Table.FirstOrDefault(x => x.Id == robotsTxt.Id));
    }

    [TestMethod]
    public async Task UpdateRobotsTxtTest()
    {
        //Arrange
        var robotsTxt = new RobotsTxt { StoreId = "1" };
        await _robotsTxtService.InsertRobotsTxt(robotsTxt);
        //Act
        robotsTxt.Text = "test";
        await _robotsTxtService.UpdateRobotsTxt(robotsTxt);
        //Assert
        Assert.AreEqual("test", _repository.Table.FirstOrDefault(x => x.Id == robotsTxt.Id).Text);
    }

    [TestMethod]
    public async Task DeleteRobotsTxtTest()
    {
        //Arrange
        var robotsTxt = new RobotsTxt { StoreId = "1" };
        await _robotsTxtService.InsertRobotsTxt(robotsTxt);
        //Act
        await _robotsTxtService.DeleteRobotsTxt(robotsTxt);
        //Assert
        Assert.IsNull(_repository.Table.FirstOrDefault(x => x.Id == robotsTxt.Id));
    }
}