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.

feat(arch001): add BaseCollectionController skeleton with shared List region

KrzysztofPajak2408ec1b908f
diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseCollectionControllerTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseCollectionControllerTests.cs
new file mode 100644
index 000000000..f8d0b7d27
--- /dev/null
+++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseCollectionControllerTests.cs
@@ -0,0 +1,151 @@
+using Grand.Business.Core.Interfaces.Catalog.Collections;
+using Grand.Business.Core.Interfaces.Catalog.Products;
+using Grand.Business.Core.Interfaces.Common.Localization;
+using Grand.Business.Core.Interfaces.Common.Stores;
+using Grand.Domain.Catalog;
+using Grand.Domain.Stores;
+using Grand.Infrastructure.Mapper;
+using Grand.Mapping;
+using Grand.Web.AdminShared.Controllers;
+using Grand.Web.AdminShared.Interfaces;
+using Grand.Web.AdminShared.Mapper;
+using Grand.Web.AdminShared.Models.Catalog;
+using Grand.Web.Common.DataSource;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Mvc.Routing;
+using Microsoft.AspNetCore.Mvc.ViewFeatures;
+using Microsoft.Extensions.Logging;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using Moq;
+
+namespace Grand.Web.Admin.Tests.Controllers;
+
+// Characterization tests for the merged Collection access-check behavior (ARCH-001 Collection
+// consolidation). Parameterized over a mocked IAdminDataScope<Collection> instead of the two
+// different concrete access mechanisms Admin (none) and Store (AccessToEntityByStore) used before.
+[TestClass]
+public class BaseCollectionControllerTests
+{
+ // BaseCollectionController is abstract; this minimal subclass exists only so actions under
+ // test can be invoked directly. No EditWarningCheck override here (Task 3 adds that on the
+ // real Store subclass) - the base's no-op default is exercised.
+ private class TestCollectionController(
+ ICollectionViewModelService collectionViewModelService,
+ ICollectionService collectionService,
+ IStoreService storeService,
+ ILanguageService languageService,
+ ITranslationService translationService,
+ IPictureViewModelService pictureViewModelService,
+ IProductService productService,
+ IAdminDataScope<Collection> scope)
+ : BaseCollectionController(collectionViewModelService, collectionService, storeService,
+ languageService, translationService, pictureViewModelService, productService, scope);
+
+ private TestCollectionController _controller;
+ private Mock<ICollectionService> _collectionServiceMock;
+ private Mock<ICollectionViewModelService> _collectionViewModelServiceMock;
+ private Mock<IStoreService> _storeServiceMock;
+ private Mock<ITranslationService> _translationServiceMock;
+ private Mock<IAdminDataScope<Collection>> _scopeMock;
+
+ [TestInitialize]
+ public void Setup()
+ {
+ var mapperConfig = new MapperConfiguration(cfg => cfg.AddProfile<CollectionProfile>());
+ AutoMapperConfig.Init(mapperConfig);
+
+ _collectionServiceMock = new Mock<ICollectionService>();
+ _collectionViewModelServiceMock = new Mock<ICollectionViewModelService>();
+ _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("resource");
+
+ _scopeMock = new Mock<IAdminDataScope<Collection>>();
+ _scopeMock.Setup(s => s.DefaultStoreId).Returns((string)null);
+
+ var languageServiceMock = new Mock<ILanguageService>();
+ languageServiceMock.Setup(l => l.GetAllLanguages(true, It.IsAny<string>())).ReturnsAsync(new List<Grand.Domain.Localization.Language>());
+
+ _controller = new TestCollectionController(
+ _collectionViewModelServiceMock.Object,
+ _collectionServiceMock.Object,
+ _storeServiceMock.Object,
+ languageServiceMock.Object,
+ _translationServiceMock.Object,
+ new Mock<IPictureViewModelService>().Object,
+ new Mock<IProductService>().Object,
+ _scopeMock.Object);
+
+ var httpContext = new DefaultHttpContext();
+ var loggerFactoryMock = new Mock<ILoggerFactory>();
+ loggerFactoryMock.Setup(l => l.CreateLogger(It.IsAny<string>())).Returns(new Mock<ILogger>().Object);
+ var urlHelperFactoryMock = new Mock<IUrlHelperFactory>();
+ urlHelperFactoryMock.Setup(f => f.GetUrlHelper(It.IsAny<ActionContext>())).Returns(new Mock<IUrlHelper>().Object);
+ var requestServicesMock = new Mock<IServiceProvider>();
+ requestServicesMock.Setup(s => s.GetService(typeof(ILoggerFactory))).Returns(loggerFactoryMock.Object);
+ requestServicesMock.Setup(s => s.GetService(typeof(IUrlHelperFactory))).Returns(urlHelperFactoryMock.Object);
+ httpContext.RequestServices = requestServicesMock.Object;
+ _controller.ControllerContext = new ControllerContext { HttpContext = httpContext };
+ _controller.TempData = new TempDataDictionary(httpContext, new Mock<ITempDataProvider>().Object);
+ }
+
+ [TestMethod]
+ public async Task ListGet_GlobalScope_PopulatesAvailableStores()
+ {
+ _scopeMock.Setup(s => s.DefaultStoreId).Returns((string)null);
+ _storeServiceMock.Setup(s => s.GetAllStores())
+ .ReturnsAsync(new List<Store> { new() { Id = "s1", Shortcut = "Store 1" } });
+
+ var result = await _controller.List();
+
+ var view = result as ViewResult;
+ Assert.IsNotNull(view);
+ var model = (CollectionListModel)view.Model;
+ // "All" placeholder + the one real store
+ Assert.AreEqual(2, model.AvailableStores.Count);
+ }
+
+ [TestMethod]
+ public async Task ListGet_StoreScope_SkipsAvailableStores()
+ {
+ _scopeMock.Setup(s => s.DefaultStoreId).Returns("store-1");
+
+ var result = await _controller.List();
+
+ var view = result as ViewResult;
+ Assert.IsNotNull(view);
+ var model = (CollectionListModel)view.Model;
+ Assert.AreEqual(0, model.AvailableStores.Count);
+ _storeServiceMock.Verify(s => s.GetAllStores(), Times.Never);
+ }
+
+ [TestMethod]
+ public async Task ListPost_ForcesScopeDefaultStoreIdOntoSearchModel()
+ {
+ _scopeMock.Setup(s => s.DefaultStoreId).Returns("store-1");
+ _collectionServiceMock
+ .Setup(c => c.GetAllCollections(It.IsAny<string>(), "store-1", 0, 10, true))
+ .ReturnsAsync(new Grand.Domain.PagedList<Collection>(new List<Collection>(), 0, 10));
+
+ var model = new CollectionListModel { SearchStoreId = "attacker-supplied-store" };
+ await _controller.List(new DataSourceRequest { Page = 1, PageSize = 10 }, model);
+
+ Assert.AreEqual("store-1", model.SearchStoreId);
+ }
+
+ [TestMethod]
+ public async Task ListPost_GlobalScope_LeavesSubmittedSearchStoreIdUntouched()
+ {
+ _scopeMock.Setup(s => s.DefaultStoreId).Returns((string)null);
+ _collectionServiceMock
+ .Setup(c => c.GetAllCollections(It.IsAny<string>(), "admin-submitted-store", 0, 10, true))
+ .ReturnsAsync(new Grand.Domain.PagedList<Collection>(new List<Collection>(), 0, 10));
+
+ var model = new CollectionListModel { SearchStoreId = "admin-submitted-store" };
+ await _controller.List(new DataSourceRequest { Page = 1, PageSize = 10 }, model);
+
+ Assert.AreEqual("admin-submitted-store", model.SearchStoreId);
+ }
+}
diff --git a/src/Web/Grand.Web.AdminShared/Controllers/BaseCollectionController.cs b/src/Web/Grand.Web.AdminShared/Controllers/BaseCollectionController.cs
new file mode 100644
index 000000000..bd32c6655
--- /dev/null
+++ b/src/Web/Grand.Web.AdminShared/Controllers/BaseCollectionController.cs
@@ -0,0 +1,82 @@
+using Grand.Business.Core.Interfaces.Catalog.Collections;
+using Grand.Business.Core.Interfaces.Catalog.Products;
+using Grand.Business.Core.Interfaces.Common.Localization;
+using Grand.Business.Core.Interfaces.Common.Stores;
+using Grand.Domain.Catalog;
+using Grand.Domain.Permissions;
+using Grand.Web.AdminShared.Extensions.Mapping;
+using Grand.Web.AdminShared.Interfaces;
+using Grand.Web.AdminShared.Models.Catalog;
+using Grand.Web.Common.Controllers;
+using Grand.Web.Common.DataSource;
+using Grand.Web.Common.Security.Authorization;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Mvc.Rendering;
+
+namespace Grand.Web.AdminShared.Controllers;
+
+// [AutoValidateAntiforgeryToken] is restated on each concrete host subclass (Admin/Store
+// CollectionController) too - ASP.NET Core resolves filters from the concrete controller's full
+// type hierarchy at runtime, so every real endpoint is already protected. It's added here as well,
+// mirroring BaseProductController/BaseCategoryController, so static analysis that doesn't follow
+// the attribute across a base/derived project boundary has something to see in the same file as
+// the actions.
+[PermissionAuthorize(PermissionSystemName.Collections)]
+[AutoValidateAntiforgeryToken]
+public abstract class BaseCollectionController(
+ ICollectionViewModelService collectionViewModelService,
+ ICollectionService collectionService,
+ IStoreService storeService,
+ ILanguageService languageService,
+ ITranslationService translationService,
+ IPictureViewModelService pictureViewModelService,
+ IProductService productService,
+ IAdminDataScope<Collection> scope)
+ : BaseController
+{
+ /// <summary>Hook for host-specific UI-copy warnings that aren't access-scope decisions.
+ /// Overridden by the Store subclass (Task 3); no-op everywhere else. Mirrors
+ /// BaseCategoryController.EditWarningCheck.</summary>
+ protected virtual void EditWarningCheck(Collection collection) { }
+
+ // Exposed for host subclasses: primary-constructor parameters are not visible to derived
+ // classes by name in C#, so Store's EditWarningCheck override needs this.
+ protected ITranslationService TranslationService => translationService;
+ protected IAdminDataScope<Collection> Scope => scope;
+
+ #region List
+
+ public IActionResult Index() => RedirectToAction("List");
+
+ public async Task<IActionResult> List()
+ {
+ var model = new CollectionListModel();
+ // Admin only: Store never had this dropdown (it's implicitly single-store).
+ // ShowStoreSelector can't gate this - it's true on both Global and Store scopes.
+ if (scope.DefaultStoreId is null)
+ {
+ model.AvailableStores.Add(new SelectListItem { Text = translationService.GetResource("Admin.Common.All"), Value = "" });
+ foreach (var s in await storeService.GetAllStores())
+ model.AvailableStores.Add(new SelectListItem { Text = s.Shortcut, Value = s.Id });
+ }
+
+ return View(model);
+ }
+
+ [PermissionAuthorizeAction(PermissionActionName.List)]
+ [HttpPost]
+ public async Task<IActionResult> List(DataSourceRequest command, CollectionListModel model)
+ {
+ if (scope.DefaultStoreId is not null) model.SearchStoreId = scope.DefaultStoreId;
+ var collections = await collectionService.GetAllCollections(model.SearchCollectionName,
+ model.SearchStoreId, command.Page - 1, command.PageSize, true);
+ var gridModel = new DataSourceResult {
+ Data = collections.Select(x => x.ToModel()),
+ Total = collections.TotalCount
+ };
+
+ return Json(gridModel);
+ }
+
+ #endregion
+}