2021-01-18 07:34:59 +01:00
|
|
|
|
// Copyright (c) Umbraco.
|
2020-12-23 11:35:49 +01:00
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2017-05-12 14:49:44 +02:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using NUnit.Framework;
|
2021-02-18 11:06:02 +01:00
|
|
|
|
using Umbraco.Cms.Core.Hosting;
|
|
|
|
|
|
using Umbraco.Cms.Core.IO;
|
|
|
|
|
|
using Umbraco.Cms.Core.IO.MediaPathSchemes;
|
|
|
|
|
|
using Umbraco.Cms.Tests.Common.Testing;
|
|
|
|
|
|
using Umbraco.Cms.Tests.Integration.Testing;
|
|
|
|
|
|
using Umbraco.Extensions;
|
2017-05-12 14:49:44 +02:00
|
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
|
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.IO
|
2017-05-12 14:49:44 +02:00
|
|
|
|
{
|
|
|
|
|
|
[TestFixture]
|
2020-12-23 11:35:49 +01:00
|
|
|
|
[UmbracoTest]
|
2020-11-26 15:29:18 +01:00
|
|
|
|
public class FileSystemsTests : UmbracoIntegrationTest
|
2017-05-12 14:49:44 +02:00
|
|
|
|
{
|
|
|
|
|
|
[Test]
|
2021-04-27 09:52:17 +02:00
|
|
|
|
public void Can_Get_MediaFileManager()
|
2017-05-12 14:49:44 +02:00
|
|
|
|
{
|
2021-04-27 09:52:17 +02:00
|
|
|
|
MediaFileManager fileSystem = GetRequiredService<MediaFileManager>();
|
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]
|
2021-04-27 09:52:17 +02:00
|
|
|
|
public void MediaFileManager_Is_Singleton()
|
2018-10-26 15:06:53 +02:00
|
|
|
|
{
|
2021-04-27 09:52:17 +02:00
|
|
|
|
MediaFileManager fileManager1 = GetRequiredService<MediaFileManager>();
|
|
|
|
|
|
MediaFileManager fileManager2 = GetRequiredService<MediaFileManager>();
|
|
|
|
|
|
Assert.AreSame(fileManager1, fileManager2);
|
2019-01-08 12:52:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2021-04-27 09:52:17 +02:00
|
|
|
|
MediaFileManager mediaFileManager = GetRequiredService<MediaFileManager>();
|
|
|
|
|
|
var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes("test"));
|
|
|
|
|
|
string virtualPath = mediaFileManager.GetMediaPath("file.txt", Guid.NewGuid(), Guid.NewGuid());
|
|
|
|
|
|
mediaFileManager.FileSystem.AddFile(virtualPath, memoryStream);
|
2017-05-12 14:49:44 +02:00
|
|
|
|
|
|
|
|
|
|
// ~/media/1234/file.txt exists
|
2020-12-23 11:35:49 +01:00
|
|
|
|
IHostingEnvironment hostingEnvironment = GetRequiredService<IHostingEnvironment>();
|
2021-04-27 09:52:17 +02:00
|
|
|
|
string physPath = hostingEnvironment.MapPathWebRoot(Path.Combine("media", virtualPath));
|
2017-05-12 14:49:44 +02:00
|
|
|
|
Assert.IsTrue(File.Exists(physPath));
|
|
|
|
|
|
|
|
|
|
|
|
// ~/media/1234/file.txt is gone
|
2021-04-27 09:52:17 +02:00
|
|
|
|
mediaFileManager.DeleteMediaFiles(new[] { virtualPath });
|
2017-05-12 14:49:44 +02:00
|
|
|
|
Assert.IsFalse(File.Exists(physPath));
|
|
|
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
|
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));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-27 01:17:32 -05:00
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
}
|