2012-08-20 09:42:32 -01:00
|
|
|
|
using System;
|
2017-02-17 09:54:50 +01:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Text;
|
2017-01-28 10:04:43 +01:00
|
|
|
|
using Moq;
|
2012-08-20 09:42:32 -01:00
|
|
|
|
using NUnit.Framework;
|
2017-01-28 10:04:43 +01:00
|
|
|
|
using Umbraco.Core;
|
2012-08-20 09:42:32 -01:00
|
|
|
|
using Umbraco.Core.IO;
|
2017-01-28 10:04:43 +01:00
|
|
|
|
using Umbraco.Core.Logging;
|
|
|
|
|
|
using Umbraco.Core.Profiling;
|
2013-09-16 19:33:21 +10:00
|
|
|
|
using Umbraco.Tests.TestHelpers;
|
2012-08-20 09:42:32 -01:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Tests.IO
|
|
|
|
|
|
{
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
|
public class FileSystemProviderManagerTests
|
|
|
|
|
|
{
|
2013-09-16 19:33:21 +10:00
|
|
|
|
[SetUp]
|
|
|
|
|
|
public void Setup()
|
|
|
|
|
|
{
|
|
|
|
|
|
//init the config singleton
|
|
|
|
|
|
var config = SettingsForTests.GetDefault();
|
|
|
|
|
|
SettingsForTests.ConfigureSettings(config);
|
2017-01-28 10:04:43 +01:00
|
|
|
|
|
|
|
|
|
|
// media fs wants this
|
|
|
|
|
|
ApplicationContext.Current = new ApplicationContext(CacheHelper.CreateDisabledCacheHelper(), new ProfilingLogger(Mock.Of<ILogger>(), Mock.Of<IProfiler>()));
|
2017-03-08 11:02:30 +01:00
|
|
|
|
|
|
|
|
|
|
// start clean
|
|
|
|
|
|
// because some tests will create corrupt or weird filesystems
|
2017-09-12 16:05:35 +02:00
|
|
|
|
FileSystemProviderManager.ResetCurrent();
|
2017-03-08 11:02:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[TearDown]
|
|
|
|
|
|
public void TearDown()
|
|
|
|
|
|
{
|
|
|
|
|
|
// stay clean (see note in SetUp)
|
2017-09-12 16:05:35 +02:00
|
|
|
|
FileSystemProviderManager.ResetCurrent();
|
2013-09-16 19:33:21 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-20 09:42:32 -01:00
|
|
|
|
[Test]
|
2012-11-07 09:40:33 +05:00
|
|
|
|
public void Can_Get_Base_File_System()
|
2012-08-20 09:42:32 -01:00
|
|
|
|
{
|
2013-05-14 14:18:41 -10:00
|
|
|
|
var fs = FileSystemProviderManager.Current.GetUnderlyingFileSystemProvider(FileSystemProviderConstants.Media);
|
2012-08-20 09:42:32 -01:00
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(fs);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void Can_Get_Typed_File_System()
|
|
|
|
|
|
{
|
2012-11-07 09:30:40 +05:00
|
|
|
|
var fs = FileSystemProviderManager.Current.GetFileSystemProvider<MediaFileSystem>();
|
2012-08-20 09:42:32 -01:00
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(fs);
|
|
|
|
|
|
}
|
2012-11-07 09:40:33 +05:00
|
|
|
|
|
2017-02-17 09:54:50 +01:00
|
|
|
|
[Test]
|
|
|
|
|
|
public void Media_Fs_Safe_Delete()
|
|
|
|
|
|
{
|
|
|
|
|
|
var fs = FileSystemProviderManager.Current.GetFileSystemProvider<MediaFileSystem>();
|
|
|
|
|
|
var ms = new MemoryStream(Encoding.UTF8.GetBytes("test"));
|
|
|
|
|
|
var virtPath = fs.GetMediaPath("file.txt", Guid.NewGuid(), Guid.NewGuid());
|
|
|
|
|
|
fs.AddFile(virtPath, ms);
|
|
|
|
|
|
|
|
|
|
|
|
// ~/media/1234/file.txt exists
|
|
|
|
|
|
var physPath = IOHelper.MapPath(Path.Combine("media", virtPath));
|
|
|
|
|
|
Assert.IsTrue(File.Exists(physPath));
|
|
|
|
|
|
|
|
|
|
|
|
// ~/media/1234/file.txt is gone
|
|
|
|
|
|
fs.DeleteMediaFiles(new [] { virtPath });
|
|
|
|
|
|
Assert.IsFalse(File.Exists(physPath));
|
|
|
|
|
|
|
|
|
|
|
|
// ~/media/1234 is gone
|
|
|
|
|
|
physPath = Path.GetDirectoryName(physPath);
|
|
|
|
|
|
Assert.IsFalse(Directory.Exists(physPath));
|
|
|
|
|
|
|
|
|
|
|
|
// ~/media exists
|
|
|
|
|
|
physPath = Path.GetDirectoryName(physPath);
|
|
|
|
|
|
Assert.IsTrue(Directory.Exists(physPath));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-08 11:02:30 +01:00
|
|
|
|
public void Singleton_Typed_File_System()
|
|
|
|
|
|
{
|
|
|
|
|
|
var fs1 = FileSystemProviderManager.Current.GetFileSystemProvider<MediaFileSystem>();
|
|
|
|
|
|
var fs2 = FileSystemProviderManager.Current.GetFileSystemProvider<MediaFileSystem>();
|
|
|
|
|
|
|
|
|
|
|
|
Assert.AreSame(fs1, fs2);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-17 09:54:50 +01:00
|
|
|
|
[Test]
|
2012-11-07 09:40:33 +05:00
|
|
|
|
public void Exception_Thrown_On_Invalid_Typed_File_System()
|
|
|
|
|
|
{
|
|
|
|
|
|
Assert.Throws<InvalidOperationException>(() => FileSystemProviderManager.Current.GetFileSystemProvider<InvalidTypedFileSystem>());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-08 11:02:30 +01:00
|
|
|
|
[Test]
|
|
|
|
|
|
public void Exception_Thrown_On_NonConfigured_Typed_File_System()
|
|
|
|
|
|
{
|
|
|
|
|
|
// note: we need to reset the manager between tests else the Accept_Fallback test would corrupt that one
|
|
|
|
|
|
Assert.Throws<ArgumentException>(() => FileSystemProviderManager.Current.GetFileSystemProvider<NonConfiguredTypeFileSystem>());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void Accept_Fallback_On_NonConfigured_Typed_File_System()
|
|
|
|
|
|
{
|
|
|
|
|
|
var fs = FileSystemProviderManager.Current.GetFileSystemProvider<NonConfiguredTypeFileSystem>(() => new PhysicalFileSystem("~/App_Data/foo"));
|
|
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(fs);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Used in unit tests, for a typed file system we need to inherit from FileSystemWrapper and they MUST have a ctor
|
|
|
|
|
|
/// that only accepts a base IFileSystem object
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
internal class InvalidTypedFileSystem : FileSystemWrapper
|
2012-11-07 09:40:33 +05:00
|
|
|
|
{
|
2017-03-08 11:02:30 +01:00
|
|
|
|
public InvalidTypedFileSystem(IFileSystem wrapped, string invalidParam)
|
|
|
|
|
|
: base(wrapped)
|
|
|
|
|
|
{ }
|
2012-11-07 09:40:33 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-08 11:02:30 +01:00
|
|
|
|
[FileSystemProvider("noconfig")]
|
|
|
|
|
|
internal class NonConfiguredTypeFileSystem : FileSystemWrapper
|
|
|
|
|
|
{
|
|
|
|
|
|
public NonConfiguredTypeFileSystem(IFileSystem wrapped)
|
|
|
|
|
|
: base(wrapped)
|
|
|
|
|
|
{ }
|
|
|
|
|
|
}
|
2012-11-07 09:40:33 +05:00
|
|
|
|
}
|
2012-08-20 09:42:32 -01:00
|
|
|
|
}
|