-
Notifications
You must be signed in to change notification settings - Fork 550
ARCH-001 Phase 16: News controller/service consolidation (Admin/Store) + Comments tab fix #810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5d8cdd1
ARCH-001 Phase 16 Task 1: routed data scope for NewsItem
KrzysztofPajak eb41bf9
ARCH-001 Phase 16 Task 2: BaseNewsController List/Create/Edit/Delete …
KrzysztofPajak 8f62c5b
ARCH-001 Phase 16 Task 3: BaseNewsController Comments region (fixes S…
KrzysztofPajak 199d4ef
ARCH-001 Phase 16 Task 4: thin NewsController subclasses + attribute …
KrzysztofPajak 6adbe98
ARCH-001 Phase 16 Task 5: migrate News views to AdminShared, unify Li…
KrzysztofPajak e38be04
ARCH-001 Phase 16: final-review fix — add CanView scope check to Comm…
KrzysztofPajak b9e8645
ARCH-001 Phase 16: drop dead-condition re-check in Store NewsControll…
KrzysztofPajak a6a35b9
Merge branch 'develop' into arch001/phase16-news-consolidation
KrzysztofPajak 0f28ae4
ARCH-001 Phase 16: fix Comments(GET) 500 — redirect instead of render…
KrzysztofPajak fc95dda
ARCH-001 Phase 16: update Comments(GET) test for redirect fix
KrzysztofPajak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
421 changes: 421 additions & 0 deletions
421
src/Tests/Grand.Web.Admin.Tests/Controllers/BaseNewsControllerTests.cs
Large diffs are not rendered by default.
Oops, something went wrong.
35 changes: 35 additions & 0 deletions
35
src/Tests/Grand.Web.Admin.Tests/Controllers/NewsControllerTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| using Grand.Web.Admin.Controllers; | ||
| using Grand.Web.AdminShared.Controllers; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using System.Reflection; | ||
|
|
||
| namespace Grand.Web.Admin.Tests.Controllers; | ||
|
|
||
| [TestClass] | ||
| public class NewsControllerTests | ||
| { | ||
| [TestMethod] | ||
| public void NewsController_IsThinSubclassOfBaseNewsController() | ||
| { | ||
| Assert.IsTrue(typeof(BaseNewsController).IsAssignableFrom(typeof(NewsController))); | ||
| Assert.AreNotEqual(typeof(BaseNewsController), typeof(NewsController)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void NewsController_HasRequiredHostAttributes() | ||
| { | ||
| var type = typeof(NewsController); | ||
|
|
||
| var areaAttr = type.GetCustomAttribute<AreaAttribute>(inherit: false); | ||
| Assert.IsNotNull(areaAttr); | ||
| Assert.AreEqual("Admin", areaAttr.RouteValue); | ||
|
|
||
| Assert.IsNotNull(type.GetCustomAttribute<Grand.Web.Common.Filters.AuthorizeMenuAttribute>(inherit: false)); | ||
| Assert.IsNotNull(type.GetCustomAttribute<AutoValidateAntiforgeryTokenAttribute>(inherit: false)); | ||
| // Confirm the actual namespace of AuthorizeAdminAttribute by reading the file that declares it | ||
| // before writing this assertion - Phase 15 found it lives in Grand.Web.Common.Filters, not | ||
| // Grand.Web.Admin.Extensions as might be assumed. | ||
| Assert.IsNotNull(type.GetCustomAttribute<Grand.Web.Common.Filters.AuthorizeAdminAttribute>(inherit: false)); | ||
| } | ||
| } |
66 changes: 66 additions & 0 deletions
66
src/Tests/Grand.Web.Admin.Tests/Controllers/RoutedNewsItemDataScopeTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| using Grand.Domain.News; | ||
| using Grand.Web.AdminShared.Interfaces; | ||
| using Grand.Web.AdminShared.Services; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Routing; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using Moq; | ||
|
|
||
| namespace Grand.Web.Admin.Tests.Controllers; | ||
|
|
||
| [TestClass] | ||
| public class RoutedNewsItemDataScopeTests | ||
| { | ||
| private static RoutedNewsItemDataScope Build(string area, out Mock<IAdminDataScope<NewsItem>> globalMock, out Mock<IAdminDataScope<NewsItem>> storeMock) | ||
| { | ||
| var httpContext = new DefaultHttpContext(); | ||
| if (area != null) | ||
| httpContext.Request.RouteValues = new RouteValueDictionary { ["area"] = area }; | ||
|
|
||
| var httpContextAccessorMock = new Mock<IHttpContextAccessor>(); | ||
| httpContextAccessorMock.Setup(a => a.HttpContext).Returns(httpContext); | ||
|
|
||
| var global = new GlobalAdminDataScope<NewsItem>(); | ||
| var store = new StoreAdminDataScope<NewsItem>(BuildContextAccessor()); | ||
| globalMock = null; | ||
| storeMock = null; | ||
| return new RoutedNewsItemDataScope(httpContextAccessorMock.Object, global, store); | ||
| } | ||
|
|
||
| private static Grand.Infrastructure.IContextAccessor BuildContextAccessor() | ||
| { | ||
| var workContext = new Mock<Grand.Infrastructure.IWorkContext>(); | ||
| workContext.Setup(w => w.CurrentCustomer).Returns(new Grand.Domain.Customers.Customer { StaffStoreId = "store-1" }); | ||
| var contextAccessorMock = new Mock<Grand.Infrastructure.IContextAccessor>(); | ||
| contextAccessorMock.Setup(c => c.WorkContext).Returns(workContext.Object); | ||
| return contextAccessorMock.Object; | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void DefaultStoreId_AdminArea_ResolvesToGlobalScope() | ||
| { | ||
| var routed = Build("Admin", out _, out _); | ||
| Assert.IsNull(routed.DefaultStoreId); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void DefaultStoreId_StoreArea_ResolvesToStoreScope() | ||
| { | ||
| var routed = Build("Store", out _, out _); | ||
| Assert.AreEqual("store-1", routed.DefaultStoreId); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void DefaultStoreId_VendorArea_ThrowsFailClosed() | ||
| { | ||
| var routed = Build("Vendor", out _, out _); | ||
| Assert.ThrowsExactly<InvalidOperationException>(() => _ = routed.DefaultStoreId); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void DefaultStoreId_MissingArea_ThrowsFailClosed() | ||
| { | ||
| var routed = Build(null, out _, out _); | ||
| Assert.ThrowsExactly<InvalidOperationException>(() => _ = routed.DefaultStoreId); | ||
| } | ||
| } | ||
168 changes: 168 additions & 0 deletions
168
src/Tests/Grand.Web.Store.Tests/Controllers/NewsControllerTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| using Grand.Business.Core.Interfaces.Cms; | ||
| using Grand.Business.Core.Interfaces.Common.Directory; | ||
| using Grand.Business.Core.Interfaces.Common.Localization; | ||
| using Grand.Business.Core.Interfaces.Common.Stores; | ||
| using Grand.Domain.News; | ||
| using Grand.Infrastructure.Mapper; | ||
| using Grand.Mapping; | ||
| using Grand.Web.AdminShared.Interfaces; | ||
| using Grand.Web.AdminShared.Mapper; | ||
| using Grand.Web.Store.Controllers; | ||
| 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; | ||
| using System.Reflection; | ||
|
|
||
| namespace Grand.Web.Store.Tests.Controllers; | ||
|
|
||
| [TestClass] | ||
| public class NewsControllerTests | ||
| { | ||
| private NewsController _controller; | ||
| private Mock<INewsService> _newsServiceMock; | ||
| private Mock<IAdminDataScope<NewsItem>> _scopeMock; | ||
|
|
||
| [TestInitialize] | ||
| public void Setup() | ||
| { | ||
| var mapperConfig = new MapperConfiguration(cfg => cfg.AddProfile<NewsItemProfile>()); | ||
| AutoMapperConfig.Init(mapperConfig); | ||
|
|
||
| _newsServiceMock = new Mock<INewsService>(); | ||
| _scopeMock = new Mock<IAdminDataScope<NewsItem>>(); | ||
| _scopeMock.Setup(s => s.DefaultStoreId).Returns("store-1"); | ||
|
|
||
| var languageServiceMock = new Mock<ILanguageService>(); | ||
| languageServiceMock.Setup(l => l.GetAllLanguages(true, It.IsAny<string>())) | ||
| .ReturnsAsync(new List<Grand.Domain.Localization.Language>()); | ||
|
|
||
| _controller = new NewsController( | ||
| new Mock<INewsViewModelService>().Object, | ||
| _newsServiceMock.Object, | ||
| languageServiceMock.Object, | ||
| new Mock<ITranslationService>().Object, | ||
| new Mock<IStoreService>().Object, | ||
| new Mock<IDateTimeService>().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 void NewsController_HasRequiredHostAttributes() | ||
| { | ||
| var type = typeof(NewsController); | ||
|
|
||
| var areaAttr = type.GetCustomAttribute<AreaAttribute>(inherit: false); | ||
| Assert.IsNotNull(areaAttr); | ||
| Assert.AreEqual("Store", areaAttr.RouteValue); | ||
|
|
||
| Assert.IsNotNull(type.GetCustomAttribute<Grand.Web.Common.Filters.AuthorizeStoreAttribute>(inherit: false)); | ||
| Assert.IsNotNull(type.GetCustomAttribute<Grand.Web.Common.Filters.AuthorizeMenuAttribute>(inherit: false)); | ||
| Assert.IsNotNull(type.GetCustomAttribute<AutoValidateAntiforgeryTokenAttribute>(inherit: false)); | ||
| } | ||
|
|
||
| // --- EditWarningCheck truth table ----------------------------------------------------------- | ||
|
|
||
| [TestMethod] | ||
| public async Task EditGet_NotLimitedToStores_WarningFires() | ||
| { | ||
| var newsItem = new NewsItem { Id = "n1", LimitedToStores = false }; | ||
| _newsServiceMock.Setup(n => n.GetNewsById("n1")).ReturnsAsync(newsItem); | ||
| _scopeMock.Setup(s => s.CanView(newsItem)).ReturnsAsync(true); | ||
|
|
||
| await _controller.Edit("n1"); | ||
|
|
||
| Assert.IsTrue(_controller.TempData.ContainsKey("grand.notifications.Warning")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task EditGet_LimitedContainsStore_CountGreaterThanOne_WarningFires() | ||
| { | ||
| var newsItem = new NewsItem { Id = "n1", LimitedToStores = true, Stores = ["store-1", "store-2"] }; | ||
| _newsServiceMock.Setup(n => n.GetNewsById("n1")).ReturnsAsync(newsItem); | ||
| _scopeMock.Setup(s => s.CanView(newsItem)).ReturnsAsync(true); | ||
|
|
||
| await _controller.Edit("n1"); | ||
|
|
||
| Assert.IsTrue(_controller.TempData.ContainsKey("grand.notifications.Warning")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task EditGet_LimitedContainsStore_CountOne_NoWarning() | ||
| { | ||
| var newsItem = new NewsItem { Id = "n1", LimitedToStores = true, Stores = ["store-1"] }; | ||
| _newsServiceMock.Setup(n => n.GetNewsById("n1")).ReturnsAsync(newsItem); | ||
| _scopeMock.Setup(s => s.CanView(newsItem)).ReturnsAsync(true); | ||
|
|
||
| await _controller.Edit("n1"); | ||
|
|
||
| Assert.IsFalse(_controller.TempData.ContainsKey("grand.notifications.Warning")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task EditGet_LimitedDoesNotContainStore_NoWarning() | ||
| { | ||
| var newsItem = new NewsItem { Id = "n1", LimitedToStores = true, Stores = ["other-store"] }; | ||
| _newsServiceMock.Setup(n => n.GetNewsById("n1")).ReturnsAsync(newsItem); | ||
| _scopeMock.Setup(s => s.CanView(newsItem)).ReturnsAsync(true); | ||
|
|
||
| await _controller.Edit("n1"); | ||
|
|
||
| Assert.IsFalse(_controller.TempData.ContainsKey("grand.notifications.Warning")); | ||
| } | ||
|
|
||
| // --- Preview ------------------------------------------------------------------------------------- | ||
|
|
||
| [TestMethod] | ||
| public async Task Preview_NewsItemNotFound_RedirectsToList() | ||
| { | ||
| _newsServiceMock.Setup(n => n.GetNewsById("missing")).ReturnsAsync((NewsItem)null); | ||
|
|
||
| var result = await _controller.Preview("missing"); | ||
|
|
||
| var redirect = result as RedirectToActionResult; | ||
| Assert.IsNotNull(redirect); | ||
| Assert.AreEqual("List", redirect.ActionName); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task Preview_ScopeDeniesAccess_RedirectsToList() | ||
| { | ||
| var newsItem = new NewsItem { Id = "n1" }; | ||
| _newsServiceMock.Setup(n => n.GetNewsById("n1")).ReturnsAsync(newsItem); | ||
| _scopeMock.Setup(s => s.HasAccess(newsItem)).ReturnsAsync(false); | ||
|
|
||
| var result = await _controller.Preview("n1"); | ||
|
|
||
| var redirect = result as RedirectToActionResult; | ||
| Assert.IsNotNull(redirect); | ||
| Assert.AreEqual("List", redirect.ActionName); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task Preview_ScopeGrantsAccess_ReturnsViewWithModel() | ||
| { | ||
| var newsItem = new NewsItem { Id = "n1", Title = "Hello" }; | ||
| _newsServiceMock.Setup(n => n.GetNewsById("n1")).ReturnsAsync(newsItem); | ||
| _scopeMock.Setup(s => s.HasAccess(newsItem)).ReturnsAsync(true); | ||
|
|
||
| var result = await _controller.Preview("n1"); | ||
|
|
||
| Assert.IsInstanceOfType(result, typeof(ViewResult)); | ||
| } | ||
| } |
96 changes: 0 additions & 96 deletions
96
src/Web/Grand.Web.Admin/Areas/Admin/Views/News/Partials/CreateOrUpdate.TabComments.cshtml
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.