Files
Umbraco-CMS/src/Umbraco.Tests/PublishedContent/PublishedContentTestBase.cs
Mole c9ebaadf23 Netcore: File systems rework (#10181)
* Allow IMediaFileSystem to be replace in the DI, or registered with inner filesystem

* Remove GetFileSystem from Filesystems

It was only used by tests.

* Make MediaFileSystem inherit from PhysicalFileSystem directly

* Remove FileSystemWrapper

* Remove inner filesystem from MediaFileSystem

* Add MediaFileManager and bare minimum to make it testable

* Remove MediaFileSystem

* Fix unit tests using MediaFileManager

* Remove IFileSystem and rely only on FileSystem

* Hide dangerous methods in FileSystems and do some cleaning

* Apply stylecop warnings to MediaFileManager

* Add FilesystemsCreator to Tests.Common

This allows you to create an instance if FileSystems with your own specified IFileSystem for testing purposes outside our own test suite.

* Allow the stylesheet filesystem to be replaced.

* Fix tests

* Don't save stylesheetWrapper in a temporary var

* refactor(FileSystems): change how stylesheet filesystem is registered

* fix(FileSystems): unable to overwrite media filesystem

SetMediaFileSystem added the MediaManager as a Singleton instead of
replacing the existing instance.

* fix(FileSystems): calling AddFileSystems replaces MediaManager

When calling AddFileSystems after SetMediaFileSystem the MediaManager
gets replaced by the default PhysicalFileSystem, so instead of calling
SetMediaFileSystem in AddFileSystems we now call TrySetMediaFileSystem
instead. This method will not replace any existing instance of the
MediaManager if there's already a MediaManager registered.

* Use SetMediaFileSystem instead of TrySet, and rename AddFilesystems to ConfigureFileSystems

Also don't call AddFileSystems again in ConfigureFilesystems

* Don't wrap CSS filesystem twice

* Add CreateShadowWrapperInternal to avoid casting

* Throw UnauthorizedAccessException isntead of InvalidOperationException

* Remove ResetShadowId

Co-authored-by: Rasmus John Pedersen <mail@rjp.dk>
2021-04-27 09:52:17 +02:00

90 lines
4.0 KiB
C#

using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.Media;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.PropertyEditors.ValueConverters;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Security;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Core.Templates;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Infrastructure.Serialization;
using Umbraco.Tests.TestHelpers;
namespace Umbraco.Tests.PublishedContent
{
/// <summary>
/// Abstract base class for tests for published content and published media
/// </summary>
public abstract class PublishedContentTestBase : BaseWebTest
{
protected override void Compose()
{
base.Compose();
// FIXME: what about the if (PropertyValueConvertersResolver.HasCurrent == false) ??
// can we risk double - registering and then, what happens?
Builder.WithCollectionBuilder<PropertyValueConverterCollectionBuilder>()
.Clear()
.Append<DatePickerValueConverter>()
.Append<SimpleTinyMceValueConverter>()
.Append<YesNoValueConverter>();
}
protected override void Initialize()
{
base.Initialize();
var converters = Factory.GetRequiredService<PropertyValueConverterCollection>();
var umbracoContextAccessor = Mock.Of<IUmbracoContextAccessor>();
var publishedUrlProvider = Mock.Of<IPublishedUrlProvider>();
var loggerFactory = NullLoggerFactory.Instance;
var serializer = new ConfigurationEditorJsonSerializer();
var imageSourceParser = new HtmlImageSourceParser(publishedUrlProvider);
var mediaFileManager = new MediaFileManager(Mock.Of<IFileSystem>(), Mock.Of<IMediaPathScheme>(),
loggerFactory.CreateLogger<MediaFileManager>(), Mock.Of<IShortStringHelper>());
var pastedImages = new RichTextEditorPastedImages(umbracoContextAccessor, loggerFactory.CreateLogger<RichTextEditorPastedImages>(), HostingEnvironment, Mock.Of<IMediaService>(), Mock.Of<IContentTypeBaseServiceProvider>(), mediaFileManager, ShortStringHelper, publishedUrlProvider, serializer);
var localLinkParser = new HtmlLocalLinkParser(umbracoContextAccessor, publishedUrlProvider);
var dataTypeService = new TestObjects.TestDataTypeService(
new DataType(new RichTextPropertyEditor(
loggerFactory,
Mock.Of<IBackOfficeSecurityAccessor>(),
Mock.Of<IDataTypeService>(),
Mock.Of<ILocalizationService>(),
imageSourceParser,
localLinkParser,
pastedImages,
ShortStringHelper,
IOHelper,
LocalizedTextService,
Mock.Of<IImageUrlGenerator>(),
new JsonNetSerializer()),
serializer) { Id = 1 });
var publishedContentTypeFactory = new PublishedContentTypeFactory(Mock.Of<IPublishedModelFactory>(), converters, dataTypeService);
IEnumerable<IPublishedPropertyType> CreatePropertyTypes(IPublishedContentType contentType)
{
yield return publishedContentTypeFactory.CreatePropertyType(contentType, "content", 1);
}
var type = new AutoPublishedContentType(Guid.NewGuid(), 0, "anything", CreatePropertyTypes);
ContentTypesCache.GetPublishedContentTypeByAlias = alias => type;
var umbracoContext = GetUmbracoContext("/test");
Umbraco.Web.Composing.Current.UmbracoContextAccessor.UmbracoContext = umbracoContext;
}
}
}