using System; using System.IO; using System.Text; using NUnit.Framework; using Umbraco.Core.Hosting; using Umbraco.Core.IO; using Umbraco.Core.IO.MediaPathSchemes; using Umbraco.Tests.Integration.Testing; using Umbraco.Tests.Testing; namespace Umbraco.Tests.IO { [TestFixture] [UmbracoTest] public class FileSystemsTests : UmbracoIntegrationTest { [Test] public void Can_Get_MediaFileSystem() { var fileSystem = GetRequiredService(); Assert.NotNull(fileSystem); } [Test] public void Can_Get_IMediaFileSystem() { var fileSystem = GetRequiredService(); Assert.NotNull(fileSystem); } [Test] public void IMediaFileSystem_Is_Singleton() { var fileSystem1 = GetRequiredService(); var fileSystem2 = GetRequiredService(); Assert.AreSame(fileSystem1, fileSystem2); } [Test] public void Can_Unwrap_MediaFileSystem() { var fileSystem = GetRequiredService(); var unwrapped = fileSystem.Unwrap(); Assert.IsNotNull(unwrapped); var physical = unwrapped as PhysicalFileSystem; Assert.IsNotNull(physical); } [Test] public void Can_Delete_MediaFiles() { var fs = GetRequiredService(); 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 hostingEnvironment = GetRequiredService(); var physPath = hostingEnvironment.MapPathWebRoot(Path.Combine("media", virtPath)); Assert.IsTrue(File.Exists(physPath)); // ~/media/1234/file.txt is gone fs.DeleteMediaFiles(new[] { virtPath }); Assert.IsFalse(File.Exists(physPath)); var scheme = GetRequiredService(); 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)); } // ~/media exists physPath = Path.GetDirectoryName(physPath); Assert.IsTrue(Directory.Exists(physPath)); } // FIXME: don't make sense anymore /* [Test] public void Cannot_Get_InvalidFileSystem() { // throws because InvalidTypedFileSystem does not have the proper attribute with an alias Assert.Throws(() => FileSystems.GetFileSystem()); } [Test] public void Cannot_Get_NonConfiguredFileSystem() { // note: we need to reset the manager between tests else the Accept_Fallback test would corrupt that one // throws because NonConfiguredFileSystem has the proper attribute with an alias, // but then the container cannot find an IFileSystem implementation for that alias Assert.Throws(() => FileSystems.GetFileSystem()); // all we'd need to pass is to register something like: //_container.Register("noconfig", factory => new PhysicalFileSystem("~/foo")); } internal class InvalidFileSystem : FileSystemWrapper { public InvalidFileSystem(IFileSystem innerFileSystem) : base(innerFileSystem) { } } [InnerFileSystem("noconfig")] internal class NonConfiguredFileSystem : FileSystemWrapper { public NonConfiguredFileSystem(IFileSystem innerFileSystem) : base(innerFileSystem) { } } */ } }