From 26ac63d3068fbf0730cd10f4c15b9cf73bd2bd6e Mon Sep 17 00:00:00 2001 From: Manoj Attal Date: Fri, 7 May 2021 06:59:31 +0000 Subject: [PATCH 1/6] Fix issue --- .../InternalServices/FileUploadController.cs | 2 +- .../Mocks/MockComponentProvider.cs | 5 + .../DotNetNuke.Tests.Web.csproj | 5 + .../FileUploadControllerTests.cs | 129 ++++++++++++++++++ 4 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 DNN Platform/Tests/DotNetNuke.Tests.Web/InternalServices/FileUploadControllerTests.cs diff --git a/DNN Platform/DotNetNuke.Web/InternalServices/FileUploadController.cs b/DNN Platform/DotNetNuke.Web/InternalServices/FileUploadController.cs index 0b412dddeb7..991441a9660 100644 --- a/DNN Platform/DotNetNuke.Web/InternalServices/FileUploadController.cs +++ b/DNN Platform/DotNetNuke.Web/InternalServices/FileUploadController.cs @@ -343,7 +343,7 @@ public Task UploadFromLocal(int portalId) foreach (var item in provider.Contents) { var name = item.Headers.ContentDisposition.Name; - switch (name.ToUpper()) + switch (name.ToUpperInvariant()) { case "\"FOLDER\"": folder = item.ReadAsStringAsync().Result ?? string.Empty; diff --git a/DNN Platform/Tests/DotNetNuke.Tests.Utilities/Mocks/MockComponentProvider.cs b/DNN Platform/Tests/DotNetNuke.Tests.Utilities/Mocks/MockComponentProvider.cs index 90372fc9d79..5e126821a42 100644 --- a/DNN Platform/Tests/DotNetNuke.Tests.Utilities/Mocks/MockComponentProvider.cs +++ b/DNN Platform/Tests/DotNetNuke.Tests.Utilities/Mocks/MockComponentProvider.cs @@ -102,6 +102,11 @@ public static Mock CreateRoleProvider() { return CreateNew(); } + + public static Mock CreatePortalController() + { + return CreateNew(); + } public static void ResetContainer() { diff --git a/DNN Platform/Tests/DotNetNuke.Tests.Web/DotNetNuke.Tests.Web.csproj b/DNN Platform/Tests/DotNetNuke.Tests.Web/DotNetNuke.Tests.Web.csproj index d0ff99bcb02..6216eb99b9d 100644 --- a/DNN Platform/Tests/DotNetNuke.Tests.Web/DotNetNuke.Tests.Web.csproj +++ b/DNN Platform/Tests/DotNetNuke.Tests.Web/DotNetNuke.Tests.Web.csproj @@ -108,6 +108,7 @@ + Code @@ -139,6 +140,10 @@ {0FCA217A-5F9A-4F5B-A31B-86D64AE65198} DotNetNuke.DependencyInjection + + {3cd5f6b8-8360-4862-80b6-f402892db7dd} + DotNetNuke.Instrumentation + {04f77171-0634-46e0-a95e-d7477c88712e} DotNetNuke.Log4Net diff --git a/DNN Platform/Tests/DotNetNuke.Tests.Web/InternalServices/FileUploadControllerTests.cs b/DNN Platform/Tests/DotNetNuke.Tests.Web/InternalServices/FileUploadControllerTests.cs new file mode 100644 index 00000000000..22a94c5f685 --- /dev/null +++ b/DNN Platform/Tests/DotNetNuke.Tests.Web/InternalServices/FileUploadControllerTests.cs @@ -0,0 +1,129 @@ +namespace DotNetNuke.Tests.Web.InternalServices +{ + using System.Data; + using System.Globalization; + using System.Linq; + using System.Net.Http; + using System.Threading; + using System.Threading.Tasks; + using Abstractions; + using Abstractions.Application; + using Abstractions.Logging; + + using Common; + using Common.Lists; + using Common.Utilities; + + using Data; + using DotNetNuke.Entities.Portals; + using DotNetNuke.Instrumentation; + using DotNetNuke.Web.InternalServices; + + using Microsoft.Extensions.DependencyInjection; + + using Moq; + + using NUnit.Framework; + + using Services.Cache; + + using Utilities.Mocks; + /// + /// Tests FileUploadController methods + /// + [TestFixture] + public class FileUploadControllerTests + { + private Mock _mockLoggerSource = new Mock(); + private Mock _mockDataProvider; + private Mock _mockCBO = new Mock(); + private FileUploadController _testInstance; + private Mock _mockCachingProvider; + private Mock _mockPortalController; + private TestSynchronizationContext _synchronizationContext = new TestSynchronizationContext(); + + [SetUp] + public void SetUp() + { + LoggerSource.SetTestableInstance(this._mockLoggerSource.Object); + CBO.SetTestableInstance(this._mockCBO.Object); + this.SetupDataProvider(); + this.SetupCachingProvider(); + this.SetupPortalSettings(); + this.SetupServiceProvider(); + this.SetupSynchronizationContext(); + } + + [Test] + public async Task UploadFromLocal_ShouldUploadFile_WithTrCultureAsync() + { + Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR"); + var formDataContent = new MultipartFormDataContent(); + formDataContent.Add(new StringContent("Hello World!"), "\"postfile\"", "\"testing\""); + + var request = new HttpRequestMessage(); + request.Content = formDataContent; + this._testInstance.Request = request; + await this._testInstance.UploadFromLocal(); + + Assert.IsTrue(_synchronizationContext.IsUploadFileCalled()); + } + + private void SetupPortalSettings() + { + _mockPortalController = MockComponentProvider.CreatePortalController(); + _mockPortalController.Setup(x => x.GetCurrentPortalSettings()).Returns(new PortalSettings() { PortalId = 0 }); + PortalController.SetTestableInstance(_mockPortalController.Object); + } + + private void SetupDataProvider() + { + this._mockDataProvider = MockComponentProvider.CreateDataProvider(); + this._mockDataProvider.Setup(x => x.GetListEntriesByListName("ImageTypes", string.Empty, It.IsAny())) + .Returns(Mock.Of()); + } + + private void SetupCachingProvider() + { + this._mockCachingProvider = MockComponentProvider.CreateDataCacheProvider(); + this._mockCachingProvider.Setup(x => x.GetItem(It.IsAny())) + .Returns(Enumerable.Empty()); + } + + private void SetupServiceProvider() + { + var serviceCollection = new ServiceCollection(); + serviceCollection.AddTransient( + container => new Application.ApplicationStatusInfo(Mock.Of())); + serviceCollection.AddTransient(container => Mock.Of()); + serviceCollection.AddTransient(container => Mock.Of()); + Globals.DependencyProvider = serviceCollection.BuildServiceProvider(); + } + + private void SetupSynchronizationContext() + { + _synchronizationContext = new TestSynchronizationContext(); + SynchronizationContext.SetSynchronizationContext(_synchronizationContext); + this._testInstance = new FileUploadController(); + } + + private class TestSynchronizationContext : SynchronizationContext + { + private bool isUploadFileCalled; + public override void Post(SendOrPostCallback d, object state) + { + d(state); + } + + public override void Send(SendOrPostCallback d, object state) + { + isUploadFileCalled = true; + } + + public bool IsUploadFileCalled() + { + return isUploadFileCalled; + } + } + } +} From 33674c5172a60775cf0ef03b83b2512239c17f5e Mon Sep 17 00:00:00 2001 From: Manoj Attal Date: Fri, 7 May 2021 15:42:03 +0000 Subject: [PATCH 2/6] addressing review comment --- .../InternalServices/FileUploadControllerTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DNN Platform/Tests/DotNetNuke.Tests.Web/InternalServices/FileUploadControllerTests.cs b/DNN Platform/Tests/DotNetNuke.Tests.Web/InternalServices/FileUploadControllerTests.cs index 22a94c5f685..712bba74da2 100644 --- a/DNN Platform/Tests/DotNetNuke.Tests.Web/InternalServices/FileUploadControllerTests.cs +++ b/DNN Platform/Tests/DotNetNuke.Tests.Web/InternalServices/FileUploadControllerTests.cs @@ -55,9 +55,9 @@ public void SetUp() } [Test] + [SetCulture("tr-TR")] public async Task UploadFromLocal_ShouldUploadFile_WithTrCultureAsync() { - Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR"); var formDataContent = new MultipartFormDataContent(); formDataContent.Add(new StringContent("Hello World!"), "\"postfile\"", "\"testing\""); From 78b0695dc40bd426e3cc57fa0cb9df22c0726c90 Mon Sep 17 00:00:00 2001 From: Manoj Attal Date: Fri, 7 May 2021 16:28:16 +0000 Subject: [PATCH 3/6] trying to fix failing tests --- .../InternalServices/FileUploadControllerTests.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/DNN Platform/Tests/DotNetNuke.Tests.Web/InternalServices/FileUploadControllerTests.cs b/DNN Platform/Tests/DotNetNuke.Tests.Web/InternalServices/FileUploadControllerTests.cs index 712bba74da2..0dc804301b0 100644 --- a/DNN Platform/Tests/DotNetNuke.Tests.Web/InternalServices/FileUploadControllerTests.cs +++ b/DNN Platform/Tests/DotNetNuke.Tests.Web/InternalServices/FileUploadControllerTests.cs @@ -54,6 +54,13 @@ public void SetUp() this.SetupSynchronizationContext(); } + public void TearDown() + { + PortalController.ClearInstance(); + Globals.DependencyProvider = null; + CBO.ClearInstance(); + } + [Test] [SetCulture("tr-TR")] public async Task UploadFromLocal_ShouldUploadFile_WithTrCultureAsync() @@ -64,7 +71,7 @@ public async Task UploadFromLocal_ShouldUploadFile_WithTrCultureAsync() var request = new HttpRequestMessage(); request.Content = formDataContent; this._testInstance.Request = request; - await this._testInstance.UploadFromLocal(); + await this._testInstance.UploadFromLocal(-1); Assert.IsTrue(_synchronizationContext.IsUploadFileCalled()); } From 4247885c1a4a4f770b897e4ea15822ed45a7646c Mon Sep 17 00:00:00 2001 From: Brian Dukes Date: Fri, 7 May 2021 11:42:23 -0500 Subject: [PATCH 4/6] Add missing attribute --- .../InternalServices/FileUploadControllerTests.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/DNN Platform/Tests/DotNetNuke.Tests.Web/InternalServices/FileUploadControllerTests.cs b/DNN Platform/Tests/DotNetNuke.Tests.Web/InternalServices/FileUploadControllerTests.cs index 0dc804301b0..7890640c362 100644 --- a/DNN Platform/Tests/DotNetNuke.Tests.Web/InternalServices/FileUploadControllerTests.cs +++ b/DNN Platform/Tests/DotNetNuke.Tests.Web/InternalServices/FileUploadControllerTests.cs @@ -54,6 +54,7 @@ public void SetUp() this.SetupSynchronizationContext(); } + [TearDown] public void TearDown() { PortalController.ClearInstance(); From 4d6738d7f0487ddb4a49ff22bf6d729b01af2386 Mon Sep 17 00:00:00 2001 From: Manoj Attal Date: Fri, 7 May 2021 16:56:04 +0000 Subject: [PATCH 5/6] Added the missing attribute --- .../InternalServices/FileUploadControllerTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DNN Platform/Tests/DotNetNuke.Tests.Web/InternalServices/FileUploadControllerTests.cs b/DNN Platform/Tests/DotNetNuke.Tests.Web/InternalServices/FileUploadControllerTests.cs index 0dc804301b0..59d703b6c35 100644 --- a/DNN Platform/Tests/DotNetNuke.Tests.Web/InternalServices/FileUploadControllerTests.cs +++ b/DNN Platform/Tests/DotNetNuke.Tests.Web/InternalServices/FileUploadControllerTests.cs @@ -1,7 +1,6 @@ namespace DotNetNuke.Tests.Web.InternalServices { using System.Data; - using System.Globalization; using System.Linq; using System.Net.Http; using System.Threading; @@ -54,6 +53,7 @@ public void SetUp() this.SetupSynchronizationContext(); } + [TearDown] public void TearDown() { PortalController.ClearInstance(); @@ -101,7 +101,7 @@ private void SetupServiceProvider() { var serviceCollection = new ServiceCollection(); serviceCollection.AddTransient( - container => new Application.ApplicationStatusInfo(Mock.Of())); + container => new Application.ApplicationStatusInfo(Mock.Of())); serviceCollection.AddTransient(container => Mock.Of()); serviceCollection.AddTransient(container => Mock.Of()); Globals.DependencyProvider = serviceCollection.BuildServiceProvider(); From 77e634d665014cdbc2bbc0a2cac8f9f7bfc282cf Mon Sep 17 00:00:00 2001 From: Manoj Attal Date: Sat, 29 May 2021 11:41:31 +0000 Subject: [PATCH 6/6] Fix failing UTs --- .../FileUploadControllerTests.cs | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/DNN Platform/Tests/DotNetNuke.Tests.Web/InternalServices/FileUploadControllerTests.cs b/DNN Platform/Tests/DotNetNuke.Tests.Web/InternalServices/FileUploadControllerTests.cs index 59d703b6c35..e3f5d8251ab 100644 --- a/DNN Platform/Tests/DotNetNuke.Tests.Web/InternalServices/FileUploadControllerTests.cs +++ b/DNN Platform/Tests/DotNetNuke.Tests.Web/InternalServices/FileUploadControllerTests.cs @@ -11,11 +11,9 @@ using Common; using Common.Lists; - using Common.Utilities; using Data; using DotNetNuke.Entities.Portals; - using DotNetNuke.Instrumentation; using DotNetNuke.Web.InternalServices; using Microsoft.Extensions.DependencyInjection; @@ -33,9 +31,7 @@ [TestFixture] public class FileUploadControllerTests { - private Mock _mockLoggerSource = new Mock(); private Mock _mockDataProvider; - private Mock _mockCBO = new Mock(); private FileUploadController _testInstance; private Mock _mockCachingProvider; private Mock _mockPortalController; @@ -44,23 +40,16 @@ public class FileUploadControllerTests [SetUp] public void SetUp() { - LoggerSource.SetTestableInstance(this._mockLoggerSource.Object); - CBO.SetTestableInstance(this._mockCBO.Object); + this.SetupDataProvider(); + this.SetupCachingProvider(); + this.SetupPortalSettings(); this.SetupServiceProvider(); this.SetupSynchronizationContext(); } - [TearDown] - public void TearDown() - { - PortalController.ClearInstance(); - Globals.DependencyProvider = null; - CBO.ClearInstance(); - } - [Test] [SetCulture("tr-TR")] public async Task UploadFromLocal_ShouldUploadFile_WithTrCultureAsync()