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

126 lines
4.4 KiB
C#
Raw Normal View History

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
2017-05-12 14:49:44 +02:00
using System.IO;
using System.Text;
using NUnit.Framework;
using Umbraco.Core.Hosting;
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;
using Umbraco.Tests.Integration.Testing;
using Umbraco.Tests.Testing;
2017-05-12 14:49:44 +02:00
namespace Umbraco.Tests.IO
{
[TestFixture]
[UmbracoTest]
public class FileSystemsTests : UmbracoIntegrationTest
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
{
IMediaFileSystem fileSystem = GetRequiredService<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()
{
IMediaFileSystem fileSystem = GetRequiredService<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()
{
IMediaFileSystem fileSystem1 = GetRequiredService<IMediaFileSystem>();
IMediaFileSystem fileSystem2 = GetRequiredService<IMediaFileSystem>();
2018-10-26 15:06:53 +02:00
Assert.AreSame(fileSystem1, fileSystem2);
2017-05-12 14:49:44 +02:00
}
2019-01-08 12:52:16 +01:00
[Test]
public void Can_Unwrap_MediaFileSystem()
{
IMediaFileSystem fileSystem = GetRequiredService<IMediaFileSystem>();
IFileSystem unwrapped = fileSystem.Unwrap();
2019-01-08 12:52:16 +01:00
Assert.IsNotNull(unwrapped);
var physical = unwrapped as PhysicalFileSystem;
Assert.IsNotNull(physical);
}
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
{
IMediaFileSystem fs = GetRequiredService<IMediaFileSystem>();
2017-05-12 14:49:44 +02:00
var ms = new MemoryStream(Encoding.UTF8.GetBytes("test"));
string virtPath = fs.GetMediaPath("file.txt", Guid.NewGuid(), Guid.NewGuid());
2017-05-12 14:49:44 +02:00
fs.AddFile(virtPath, ms);
// ~/media/1234/file.txt exists
IHostingEnvironment hostingEnvironment = GetRequiredService<IHostingEnvironment>();
string physPath = hostingEnvironment.MapPathWebRoot(Path.Combine("media", virtPath));
2017-05-12 14:49:44 +02:00
Assert.IsTrue(File.Exists(physPath));
// ~/media/1234/file.txt is gone
fs.DeleteMediaFiles(new[] { virtPath });
Assert.IsFalse(File.Exists(physPath));
IMediaPathScheme scheme = GetRequiredService<IMediaPathScheme>();
2019-02-22 10:42:43 +01:00
if (scheme is UniqueMediaPathScheme)
{
// ~/media/1234 is *not* gone
physPath = Path.GetDirectoryName(physPath);
Assert.IsTrue(Directory.Exists(physPath));
}
else
{
// ~/media/1234 is gone
physPath = Path.GetDirectoryName(physPath);
Assert.IsFalse(Directory.Exists(physPath));
}
2017-05-12 14:49:44 +02:00
// ~/media exists
physPath = Path.GetDirectoryName(physPath);
Assert.IsTrue(Directory.Exists(physPath));
}
// FIXME: don't make sense anymore
2018-11-19 14:40:59 +01:00
/*
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
}
}