Files
Umbraco-CMS/src/Umbraco.Tests/IO/FileSystemsTests.cs

153 lines
5.0 KiB
C#
Raw Normal View History

2017-05-12 14:49:44 +02:00
using System;
using System.IO;
using System.Text;
using Moq;
using NUnit.Framework;
using Umbraco.Core;
2018-11-27 10:37:33 +01:00
using Umbraco.Core.Components;
2017-05-12 14:49:44 +02:00
using Umbraco.Core.Configuration.UmbracoSettings;
2017-05-30 15:46:25 +02:00
using Umbraco.Core.Composing;
2018-10-26 15:06:53 +02:00
using Umbraco.Core.Composing.Composers;
2017-05-12 14:49:44 +02:00
using Umbraco.Core.IO;
2018-06-29 14:25:48 +02:00
using Umbraco.Core.IO.MediaPathSchemes;
2017-05-12 14:49:44 +02:00
using Umbraco.Core.Logging;
using Umbraco.Core.Services;
using Umbraco.Tests.Components;
2017-05-12 14:49:44 +02:00
using Umbraco.Tests.TestHelpers;
namespace Umbraco.Tests.IO
{
[TestFixture]
public class FileSystemsTests
{
2018-11-28 12:59:40 +01:00
private IRegister _register;
private IFactory _factory;
2017-05-12 14:49:44 +02:00
[SetUp]
public void Setup()
{
2018-11-28 12:59:40 +01:00
_register = RegisterFactory.Create();
2018-07-20 15:45:01 +02:00
var composition = new Composition(_register, new TypeLoader(), Mock.Of<IProfilingLogger>(), ComponentTests.MockRuntimeState(RuntimeLevel.Run));
2018-11-28 12:59:40 +01:00
composition.Register(_ => Mock.Of<ILogger>());
composition.Register(_ => Mock.Of<IDataTypeService>());
composition.Register(_ => Mock.Of<IContentSection>());
2018-11-29 10:35:16 +01:00
composition.RegisterUnique<IMediaPathScheme, OriginalMediaPathScheme>();
2017-05-12 14:49:44 +02:00
2019-01-07 10:43:28 +01:00
composition.Configs.Add(SettingsForTests.GetDefaultGlobalSettings);
composition.Configs.Add(SettingsForTests.GetDefaultUmbracoSettings);
2018-11-27 10:37:33 +01:00
composition.ComposeFileSystems();
2018-10-26 15:06:53 +02:00
2019-01-07 10:43:28 +01:00
composition.Configs.Add(SettingsForTests.GetDefaultUmbracoSettings);
2018-11-28 12:59:40 +01:00
_factory = composition.CreateFactory();
2019-01-07 10:43:28 +01:00
Current.Reset();
Current.Factory = _factory;
2017-05-12 14:49:44 +02:00
// make sure we start clean
// because some tests will create corrupt or weird filesystems
FileSystems.Reset();
}
[TearDown]
public void TearDown()
{
// stay clean (see note in Setup)
FileSystems.Reset();
Current.Reset();
2018-11-28 12:59:40 +01:00
_register.DisposeIfDisposable();
2017-05-12 14:49:44 +02:00
}
2018-11-28 12:59:40 +01:00
private FileSystems FileSystems => _factory.GetInstance<FileSystems>();
2017-05-12 14:49:44 +02:00
[Test]
2018-10-26 15:06:53 +02:00
public void Can_Get_MediaFileSystem()
2017-05-12 14:49:44 +02:00
{
2018-11-28 12:59:40 +01:00
var fileSystem = _factory.GetInstance<IMediaFileSystem>();
2018-10-26 15:06:53 +02:00
Assert.NotNull(fileSystem);
}
2017-05-12 14:49:44 +02:00
2018-10-26 15:06:53 +02:00
[Test]
public void Can_Get_IMediaFileSystem()
{
2018-11-28 12:59:40 +01:00
var fileSystem = _factory.GetInstance<IMediaFileSystem>();
2017-05-12 14:49:44 +02:00
Assert.NotNull(fileSystem);
}
2018-10-26 15:06:53 +02:00
[Test]
public void IMediaFileSystem_Is_Singleton()
{
2018-11-28 12:59:40 +01:00
var fileSystem1 = _factory.GetInstance<IMediaFileSystem>();
var fileSystem2 = _factory.GetInstance<IMediaFileSystem>();
2018-10-26 15:06:53 +02:00
Assert.AreSame(fileSystem1, fileSystem2);
2017-05-12 14:49:44 +02:00
}
[Test]
2018-10-26 15:06:53 +02:00
public void Can_Delete_MediaFiles()
2017-05-12 14:49:44 +02:00
{
2018-11-28 12:59:40 +01:00
var fs = _factory.GetInstance<IMediaFileSystem>();
2017-05-12 14:49:44 +02:00
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));
}
2018-11-19 14:40:59 +01:00
// fixme - don't make sense anymore
/*
2017-05-12 14:49:44 +02:00
[Test]
2018-10-26 15:06:53 +02:00
public void Cannot_Get_InvalidFileSystem()
2017-07-20 11:21:28 +02:00
{
2018-10-26 15:06:53 +02:00
// throws because InvalidTypedFileSystem does not have the proper attribute with an alias
Assert.Throws<InvalidOperationException>(() => FileSystems.GetFileSystem<InvalidFileSystem>());
2017-07-20 11:21:28 +02:00
}
2017-05-12 14:49:44 +02:00
[Test]
2018-10-26 15:06:53 +02:00
public void Cannot_Get_NonConfiguredFileSystem()
2017-05-12 14:49:44 +02:00
{
// note: we need to reset the manager between tests else the Accept_Fallback test would corrupt that one
2018-10-26 15:06:53 +02:00
// throws because NonConfiguredFileSystem has the proper attribute with an alias,
// but then the container cannot find an IFileSystem implementation for that alias
Assert.Throws<InvalidOperationException>(() => FileSystems.GetFileSystem<NonConfiguredFileSystem>());
2017-05-12 14:49:44 +02:00
2018-10-26 15:06:53 +02:00
// all we'd need to pass is to register something like:
//_container.Register<IFileSystem>("noconfig", factory => new PhysicalFileSystem("~/foo"));
2017-05-12 14:49:44 +02:00
}
2018-10-26 15:06:53 +02:00
internal class InvalidFileSystem : FileSystemWrapper
2017-07-20 11:21:28 +02:00
{
2018-10-26 15:06:53 +02:00
public InvalidFileSystem(IFileSystem innerFileSystem)
: base(innerFileSystem)
2017-07-20 11:21:28 +02:00
{ }
}
2017-05-12 14:49:44 +02:00
2018-11-19 14:40:59 +01:00
[InnerFileSystem("noconfig")]
2018-10-26 15:06:53 +02:00
internal class NonConfiguredFileSystem : FileSystemWrapper
2017-05-12 14:49:44 +02:00
{
2018-10-26 15:06:53 +02:00
public NonConfiguredFileSystem(IFileSystem innerFileSystem)
: base(innerFileSystem)
2017-05-12 14:49:44 +02:00
{ }
}
2018-11-19 14:40:59 +01:00
*/
2017-05-12 14:49:44 +02:00
}
}