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
@@ -0,0 +1,149 @@
using Grand.Business.Core.Interfaces.Checkout.Shipping;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Business.Core.Interfaces.Common.Stores;
using Grand.Domain.Localization;
using Grand.Domain.Stores;
using Grand.Infrastructure.Mapper;
using Grand.Mapping;
using Grand.Web.Admin.Controllers;
using Grand.Web.AdminShared.Interfaces;
using Grand.Web.AdminShared.Mapper;
using Grand.Web.AdminShared.Models.Shipping;
using Grand.Web.AdminShared.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using DeliveryDate = Grand.Domain.Shipping.DeliveryDate;

namespace Grand.Web.Admin.Tests.Controllers;

[TestClass]
public class BaseDeliveryDateControllerTests
{
private Mock<IDeliveryDateService> _deliveryDateServiceMock;
private Mock<ILanguageService> _languageServiceMock;
private Mock<IStoreService> _storeServiceMock;
private Mock<ITranslationService> _translationServiceMock;

[TestInitialize]
public void Setup()
{
var mapperConfig = new MapperConfiguration(cfg => cfg.AddProfile<DeliveryDateProfile>());
AutoMapperConfig.Init(mapperConfig);

_deliveryDateServiceMock = new Mock<IDeliveryDateService>();
_languageServiceMock = new Mock<ILanguageService>();
_languageServiceMock.Setup(l => l.GetAllLanguages(It.IsAny<bool>(), It.IsAny<string>()))
.ReturnsAsync(new List<Language>());
_storeServiceMock = new Mock<IStoreService>();
_storeServiceMock.Setup(s => s.GetAllStores()).ReturnsAsync(new List<Store>());
_translationServiceMock = new Mock<ITranslationService>();
_translationServiceMock.Setup(t => t.GetResource(It.IsAny<string>())).Returns("msg");
}

private DeliveryDateController Build(IAdminDataScope<DeliveryDate> scope) =>
new(_deliveryDateServiceMock.Object, _languageServiceMock.Object, _storeServiceMock.Object,
_translationServiceMock.Object, scope)
{
ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() },
TempData = new Microsoft.AspNetCore.Mvc.ViewFeatures.TempDataDictionary(
new DefaultHttpContext(), Mock.Of<Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataProvider>())
};

[TestMethod]
public async Task Create_GlobalScope_DoesNotForceStoreId()
{
var controller = Build(new GlobalAdminDataScope<DeliveryDate>());
DeliveryDate inserted = null;
_deliveryDateServiceMock.Setup(s => s.InsertDeliveryDate(It.IsAny<DeliveryDate>()))
.Callback<DeliveryDate>(m => inserted = m).Returns(Task.CompletedTask);

var model = new DeliveryDateModel { Name = "DD1" };
await controller.CreateDeliveryDate(model, false);

Assert.AreEqual("", inserted!.StoreId ?? "");
}

[TestMethod]
public async Task Create_StoreScope_ForcesStoreId()
{
var contextAccessor = new Mock<Grand.Infrastructure.IContextAccessor>();
var workContext = new Mock<Grand.Infrastructure.IWorkContext>();
workContext.Setup(w => w.CurrentCustomer)
.Returns(new Grand.Domain.Customers.Customer { StaffStoreId = "store-1" });
contextAccessor.Setup(c => c.WorkContext).Returns(workContext.Object);
var scope = new StoreDeliveryDateDataScope(contextAccessor.Object);
var controller = Build(scope);

DeliveryDate inserted = null;
_deliveryDateServiceMock.Setup(s => s.InsertDeliveryDate(It.IsAny<DeliveryDate>()))
.Callback<DeliveryDate>(m => inserted = m).Returns(Task.CompletedTask);

var model = new DeliveryDateModel { Name = "DD1" };
await controller.CreateDeliveryDate(model, false);

Assert.AreEqual("store-1", inserted!.StoreId);
}

[TestMethod]
public async Task Edit_StoreScope_CrossStoreDeliveryDate_Denied()
{
var contextAccessor = new Mock<Grand.Infrastructure.IContextAccessor>();
var workContext = new Mock<Grand.Infrastructure.IWorkContext>();
workContext.Setup(w => w.CurrentCustomer)
.Returns(new Grand.Domain.Customers.Customer { StaffStoreId = "store-1" });
contextAccessor.Setup(c => c.WorkContext).Returns(workContext.Object);
var scope = new StoreDeliveryDateDataScope(contextAccessor.Object);
var controller = Build(scope);

_deliveryDateServiceMock.Setup(s => s.GetDeliveryDateById("dd-2"))
.ReturnsAsync(new DeliveryDate { Id = "dd-2", StoreId = "store-2" });

var result = await controller.EditDeliveryDate("dd-2") as RedirectToActionResult;

Assert.IsNotNull(result);
Assert.AreEqual("DeliveryDates", result.ActionName);
_deliveryDateServiceMock.Verify(s => s.UpdateDeliveryDate(It.IsAny<DeliveryDate>()), Times.Never);
}

[TestMethod]
public async Task CreateDeliveryDate_Get_DefaultsColorSquaresRgb()
{
var controller = Build(new GlobalAdminDataScope<DeliveryDate>());

var result = await controller.CreateDeliveryDate() as ViewResult;

var model = result!.Model as DeliveryDateModel;
Assert.IsNotNull(model);
Assert.AreEqual("#000000", model.ColorSquaresRgb);
}

[TestMethod]
public async Task EditDeliveryDate_Get_FillsBlankColorSquaresRgb_WithDefault()
{
var controller = Build(new GlobalAdminDataScope<DeliveryDate>());
_deliveryDateServiceMock.Setup(s => s.GetDeliveryDateById("dd-1"))
.ReturnsAsync(new DeliveryDate { Id = "dd-1", ColorSquaresRgb = "" });

var result = await controller.EditDeliveryDate("dd-1") as ViewResult;

var model = result!.Model as DeliveryDateModel;
Assert.IsNotNull(model);
Assert.AreEqual("#000000", model.ColorSquaresRgb);
}

[TestMethod]
public async Task EditDeliveryDate_Get_PreservesExistingColorSquaresRgb()
{
var controller = Build(new GlobalAdminDataScope<DeliveryDate>());
_deliveryDateServiceMock.Setup(s => s.GetDeliveryDateById("dd-1"))
.ReturnsAsync(new DeliveryDate { Id = "dd-1", ColorSquaresRgb = "#ff0000" });

var result = await controller.EditDeliveryDate("dd-1") as ViewResult;

var model = result!.Model as DeliveryDateModel;
Assert.IsNotNull(model);
Assert.AreEqual("#ff0000", model.ColorSquaresRgb);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using Grand.Business.Core.Interfaces.Checkout.Shipping;
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Business.Core.Interfaces.Common.Stores;
using Grand.Domain;
using Grand.Domain.Directory;
using Grand.Domain.Shipping;
using Grand.Domain.Stores;
using Grand.Infrastructure.Mapper;
using Grand.Mapping;
using Grand.Web.Admin.Controllers;
using Grand.Web.AdminShared.Interfaces;
using Grand.Web.AdminShared.Mapper;
using Grand.Web.AdminShared.Models.Shipping;
using Grand.Web.AdminShared.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using PickupPoint = Grand.Domain.Shipping.PickupPoint;

namespace Grand.Web.Admin.Tests.Controllers;

[TestClass]
public class BasePickupPointControllerTests
{
private Mock<IPickupPointService> _pickupPointServiceMock;
private Mock<IWarehouseService> _warehouseServiceMock;
private Mock<ICountryService> _countryServiceMock;
private Mock<IStoreService> _storeServiceMock;
private Mock<ITranslationService> _translationServiceMock;

[TestInitialize]
public void Setup()
{
var mapperConfig = new MapperConfiguration(cfg => cfg.AddProfile<PickupPointProfile>());
AutoMapperConfig.Init(mapperConfig);

_pickupPointServiceMock = new Mock<IPickupPointService>();
_warehouseServiceMock = new Mock<IWarehouseService>();
_warehouseServiceMock.Setup(w => w.GetAllWarehouses(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()))
.ReturnsAsync(new PagedList<Warehouse>());
_countryServiceMock = new Mock<ICountryService>();
_countryServiceMock.Setup(c => c.GetAllCountries(It.IsAny<string>(), It.IsAny<string>(), true))
.ReturnsAsync(new List<Country>());
_storeServiceMock = new Mock<IStoreService>();
_storeServiceMock.Setup(s => s.GetAllStores()).ReturnsAsync(new List<Store>());
_translationServiceMock = new Mock<ITranslationService>();
_translationServiceMock.Setup(t => t.GetResource(It.IsAny<string>())).Returns("msg");
}

private PickupPointController Build(IAdminDataScope<PickupPoint> scope) =>
new(_pickupPointServiceMock.Object, _warehouseServiceMock.Object, _countryServiceMock.Object,
_storeServiceMock.Object, _translationServiceMock.Object, scope)
{
ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() },
TempData = new Microsoft.AspNetCore.Mvc.ViewFeatures.TempDataDictionary(
new DefaultHttpContext(), Mock.Of<Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataProvider>())
};

[TestMethod]
public async Task Create_GlobalScope_DoesNotForceStoreId()
{
var controller = Build(new GlobalAdminDataScope<PickupPoint>());
PickupPoint inserted = null;
_pickupPointServiceMock.Setup(s => s.InsertPickupPoint(It.IsAny<PickupPoint>()))
.Callback<PickupPoint>(p => inserted = p).Returns(Task.CompletedTask);

var model = new PickupPointModel { Name = "PP1", Address = new() };
await controller.CreatePickupPoint(model, false);

Assert.AreEqual("", inserted!.StoreId ?? "");
}

[TestMethod]
public async Task Create_StoreScope_ForcesStoreId()
{
var contextAccessor = new Mock<Grand.Infrastructure.IContextAccessor>();
var workContext = new Mock<Grand.Infrastructure.IWorkContext>();
workContext.Setup(w => w.CurrentCustomer)
.Returns(new Grand.Domain.Customers.Customer { StaffStoreId = "store-1" });
contextAccessor.Setup(c => c.WorkContext).Returns(workContext.Object);
var scope = new StorePickupPointDataScope(contextAccessor.Object);
var controller = Build(scope);

PickupPoint inserted = null;
_pickupPointServiceMock.Setup(s => s.InsertPickupPoint(It.IsAny<PickupPoint>()))
.Callback<PickupPoint>(p => inserted = p).Returns(Task.CompletedTask);

var model = new PickupPointModel { Name = "PP1", Address = new() };
await controller.CreatePickupPoint(model, false);

Assert.AreEqual("store-1", inserted!.StoreId);
}

[TestMethod]
public async Task Edit_StoreScope_CrossStorePickupPoint_Denied()
{
var contextAccessor = new Mock<Grand.Infrastructure.IContextAccessor>();
var workContext = new Mock<Grand.Infrastructure.IWorkContext>();
workContext.Setup(w => w.CurrentCustomer)
.Returns(new Grand.Domain.Customers.Customer { StaffStoreId = "store-1" });
contextAccessor.Setup(c => c.WorkContext).Returns(workContext.Object);
var scope = new StorePickupPointDataScope(contextAccessor.Object);
var controller = Build(scope);

_pickupPointServiceMock.Setup(s => s.GetPickupPointById("pp-2"))
.ReturnsAsync(new PickupPoint { Id = "pp-2", StoreId = "store-2" });

var result = await controller.EditPickupPoint("pp-2") as RedirectToActionResult;

Assert.IsNotNull(result);
Assert.AreEqual("PickupPoints", result.ActionName);
_pickupPointServiceMock.Verify(s => s.UpdatePickupPoint(It.IsAny<PickupPoint>()), Times.Never);
}

[TestMethod]
public async Task CreateGet_StoreScope_WarehouseDropdownScopedToOwnStore()
{
var contextAccessor = new Mock<Grand.Infrastructure.IContextAccessor>();
var workContext = new Mock<Grand.Infrastructure.IWorkContext>();
workContext.Setup(w => w.CurrentCustomer)
.Returns(new Grand.Domain.Customers.Customer { StaffStoreId = "store-1" });
contextAccessor.Setup(c => c.WorkContext).Returns(workContext.Object);
var scope = new StorePickupPointDataScope(contextAccessor.Object);
var controller = Build(scope);

await controller.CreatePickupPoint();

_warehouseServiceMock.Verify(s => s.GetAllWarehouses("store-1", 0, int.MaxValue), Times.Once);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using Grand.Business.Core.Interfaces.Checkout.Shipping;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Business.Core.Interfaces.Common.Stores;
using Grand.Domain.Localization;
using Grand.Domain.Stores;
using Grand.Infrastructure.Mapper;
using Grand.Mapping;
using Grand.Web.Admin.Controllers;
using Grand.Web.AdminShared.Interfaces;
using Grand.Web.AdminShared.Mapper;
using Grand.Web.AdminShared.Models.Shipping;
using Grand.Web.AdminShared.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using ShippingMethod = Grand.Domain.Shipping.ShippingMethod;

namespace Grand.Web.Admin.Tests.Controllers;

[TestClass]
public class BaseShippingMethodControllerTests
{
private Mock<IShippingMethodService> _shippingMethodServiceMock;
private Mock<ILanguageService> _languageServiceMock;
private Mock<IStoreService> _storeServiceMock;
private Mock<ITranslationService> _translationServiceMock;

[TestInitialize]
public void Setup()
{
var mapperConfig = new MapperConfiguration(cfg => cfg.AddProfile<ShippingMethodProfile>());
AutoMapperConfig.Init(mapperConfig);

_shippingMethodServiceMock = new Mock<IShippingMethodService>();
_languageServiceMock = new Mock<ILanguageService>();
_languageServiceMock.Setup(l => l.GetAllLanguages(It.IsAny<bool>(), It.IsAny<string>()))
.ReturnsAsync(new List<Language>());
_storeServiceMock = new Mock<IStoreService>();
_storeServiceMock.Setup(s => s.GetAllStores()).ReturnsAsync(new List<Store>());
_translationServiceMock = new Mock<ITranslationService>();
_translationServiceMock.Setup(t => t.GetResource(It.IsAny<string>())).Returns("msg");
}

private ShippingMethodController Build(IAdminDataScope<ShippingMethod> scope) =>
new(_shippingMethodServiceMock.Object, _languageServiceMock.Object, _storeServiceMock.Object,
_translationServiceMock.Object, scope)
{
ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() },
TempData = new Microsoft.AspNetCore.Mvc.ViewFeatures.TempDataDictionary(
new DefaultHttpContext(), Mock.Of<Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataProvider>())
};

[TestMethod]
public async Task Create_GlobalScope_DoesNotForceStoreId()
{
var controller = Build(new GlobalAdminDataScope<ShippingMethod>());
ShippingMethod inserted = null;
_shippingMethodServiceMock.Setup(s => s.InsertShippingMethod(It.IsAny<ShippingMethod>()))
.Callback<ShippingMethod>(m => inserted = m).Returns(Task.CompletedTask);

var model = new ShippingMethodModel { Name = "SM1" };
await controller.CreateMethod(model, false);

Assert.AreEqual("", inserted!.StoreId ?? "");
}

[TestMethod]
public async Task Create_StoreScope_ForcesStoreId()
{
var contextAccessor = new Mock<Grand.Infrastructure.IContextAccessor>();
var workContext = new Mock<Grand.Infrastructure.IWorkContext>();
workContext.Setup(w => w.CurrentCustomer)
.Returns(new Grand.Domain.Customers.Customer { StaffStoreId = "store-1" });
contextAccessor.Setup(c => c.WorkContext).Returns(workContext.Object);
var scope = new StoreShippingMethodDataScope(contextAccessor.Object);
var controller = Build(scope);

ShippingMethod inserted = null;
_shippingMethodServiceMock.Setup(s => s.InsertShippingMethod(It.IsAny<ShippingMethod>()))
.Callback<ShippingMethod>(m => inserted = m).Returns(Task.CompletedTask);

var model = new ShippingMethodModel { Name = "SM1" };
await controller.CreateMethod(model, false);

Assert.AreEqual("store-1", inserted!.StoreId);
}

[TestMethod]
public async Task Edit_StoreScope_CrossStoreMethod_Denied()
{
var contextAccessor = new Mock<Grand.Infrastructure.IContextAccessor>();
var workContext = new Mock<Grand.Infrastructure.IWorkContext>();
workContext.Setup(w => w.CurrentCustomer)
.Returns(new Grand.Domain.Customers.Customer { StaffStoreId = "store-1" });
contextAccessor.Setup(c => c.WorkContext).Returns(workContext.Object);
var scope = new StoreShippingMethodDataScope(contextAccessor.Object);
var controller = Build(scope);

_shippingMethodServiceMock.Setup(s => s.GetShippingMethodById("sm-2"))
.ReturnsAsync(new ShippingMethod { Id = "sm-2", StoreId = "store-2" });

var result = await controller.EditMethod("sm-2") as RedirectToActionResult;

Assert.IsNotNull(result);
Assert.AreEqual("Methods", result.ActionName);
_shippingMethodServiceMock.Verify(s => s.UpdateShippingMethod(It.IsAny<ShippingMethod>()), Times.Never);
}
}
Loading
Loading