Skip to content

Commit

Permalink
Migrating tests that depend on Published Cache from the old test proj…
Browse files Browse the repository at this point in the history
…ect (#11242)

* starts cleaning up old test project, removing ones we'll never convert, moves new test to where it should be.

* Makes ContentNodeKit immutable properties, moves first nucache tests over

* Gets the Nucache unit tests working and refactors a bit to use builder pattern for models.

* Migrates first xml based cache test to use nucache.

* Migrates a bunch more

* Migrates remaining tests for PublishedContentTests

* Moves PublishedRouterTests

* Moves PublishedContentExtensionTests

* Moves more tests.

* committing wip

* committing wip

* Gets PublishedContentLanguageVariantTests converted and working.

* Fixes DataTable ext method and moves PublishedContentDataTableTests

* Moves PublishedMediaTests

* wip - moving EntityXmlSerializerTests

* Moves more tests

* moves more tests

* moves more tests

* Move another test

* Moves more tests

* Fix test

* move another test

* Moves more tests

* Moves more tests

* Moves more tests

* wip before merge

* More tests

* More tests

* More tests

* More tests

* More tests

* More tests

* Cleanup and moving classes.

* Remove unused code

* Fixed failing tests, due to new null checks, that did not exist in v8

* Avoid breaking changes

* Unbreak more things, even that it the old solution was crazy..

* Fixed bug where ordering of stream readings was changed..

* cleanup

Co-authored-by: Bjarke Berg <[email protected]>
  • Loading branch information
Shazwazza and bergmania authored Oct 19, 2021
1 parent 49e1aec commit c77dc5d
Show file tree
Hide file tree
Showing 171 changed files with 5,383 additions and 17,927 deletions.
4 changes: 3 additions & 1 deletion src/Umbraco.Core/Cache/FastDictionaryAppCache.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -18,6 +18,8 @@ public class FastDictionaryAppCache : IAppCache
/// </summary>
private readonly ConcurrentDictionary<string, Lazy<object>> _items = new ConcurrentDictionary<string, Lazy<object>>();

public IEnumerable<string> Keys => _items.Keys;

public int Count => _items.Count;

/// <inheritdoc />
Expand Down
4 changes: 2 additions & 2 deletions src/Umbraco.Core/Extensions/PublishedContentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1328,8 +1328,8 @@ private static Dictionary<string, string> GetAliasesAndNames(IContentTypeService
{"NodeTypeAlias", "NodeTypeAlias"},
{"CreateDate", "CreateDate"},
{"UpdateDate", "UpdateDate"},
{"CreatorName", "CreatorName"},
{"WriterName", "WriterName"},
{"CreatorId", "CreatorId"},
{"WriterId", "WriterId"},
{"Url", "Url"}
};

Expand Down
4 changes: 1 addition & 3 deletions src/Umbraco.Core/PropertyEditors/VoidEditor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.Serialization;
Expand All @@ -16,8 +16,6 @@ namespace Umbraco.Cms.Core.PropertyEditors
[HideFromTypeFinder]
public class VoidEditor : DataEditor
{
private readonly IJsonSerializer _jsonSerializer;

/// <summary>
/// Initializes a new instance of the <see cref="VoidEditor"/> class.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Extensions;

namespace Umbraco.Cms.Core.PublishedCache.Internal
{

// TODO: Only used in unit tests, needs to be moved to test project
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class InternalPublishedContent : IPublishedContent
{
public InternalPublishedContent(IPublishedContentType contentType)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Xml;

namespace Umbraco.Cms.Core.PublishedCache.Internal
{
// TODO: Only used in unit tests, needs to be moved to test project
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class InternalPublishedContentCache : PublishedCacheBase, IPublishedContentCache, IPublishedMediaCache
{
private readonly Dictionary<int, IPublishedContent> _content = new Dictionary<int, IPublishedContent>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using Umbraco.Cms.Core.Models.PublishedContent;
using System.ComponentModel;
using Umbraco.Cms.Core.Models.PublishedContent;

namespace Umbraco.Cms.Core.PublishedCache.Internal
{
// TODO: Only used in unit tests, needs to be moved to test project
[EditorBrowsable(EditorBrowsableState.Never)]
public class InternalPublishedProperty : IPublishedProperty
{
public IPublishedPropertyType PropertyType { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System;
using System.ComponentModel;
using Umbraco.Cms.Core.Cache;

namespace Umbraco.Cms.Core.PublishedCache.Internal
{

// TODO: Only used in unit tests, needs to be moved to test project
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class InternalPublishedSnapshot : IPublishedSnapshot
{
public InternalPublishedContentCache InnerContentCache { get; } = new InternalPublishedContentCache();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using Umbraco.Cms.Core.Cache;
using Umbraco.Extensions;

namespace Umbraco.Cms.Core.PublishedCache.Internal
{
// TODO: Only used in unit tests, needs to be moved to test project
[EditorBrowsable(EditorBrowsableState.Never)]
public class InternalPublishedSnapshotService : IPublishedSnapshotService
{
private InternalPublishedSnapshot _snapshot;
Expand Down
160 changes: 104 additions & 56 deletions src/Umbraco.Core/Routing/DefaultUrlProvider.cs
Original file line number Diff line number Diff line change
@@ -1,116 +1,109 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Web.Common.DependencyInjection;
using Umbraco.Extensions;

namespace Umbraco.Cms.Core.Routing
{
/// <summary>
/// Provides urls.
/// Provides urls.
/// </summary>
public class DefaultUrlProvider : IUrlProvider
{
private readonly RequestHandlerSettings _requestSettings;
private readonly ILocalizationService _localizationService;
private readonly ILocalizedTextService _localizedTextService;
private readonly ILogger<DefaultUrlProvider> _logger;
private readonly RequestHandlerSettings _requestSettings;
private readonly ISiteDomainMapper _siteDomainMapper;
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
private readonly UriUtility _uriUtility;

public DefaultUrlProvider(IOptions<RequestHandlerSettings> requestSettings, ILogger<DefaultUrlProvider> logger, ISiteDomainMapper siteDomainMapper, IUmbracoContextAccessor umbracoContextAccessor, UriUtility uriUtility)
[Obsolete("Use ctor with all parameters")]
public DefaultUrlProvider(IOptions<RequestHandlerSettings> requestSettings, ILogger<DefaultUrlProvider> logger,
ISiteDomainMapper siteDomainMapper, IUmbracoContextAccessor umbracoContextAccessor, UriUtility uriUtility)
: this(requestSettings, logger, siteDomainMapper, umbracoContextAccessor, uriUtility,
StaticServiceProvider.Instance.GetRequiredService<ILocalizationService>())
{
}

public DefaultUrlProvider(
IOptions<RequestHandlerSettings> requestSettings,
ILogger<DefaultUrlProvider> logger,
ISiteDomainMapper siteDomainMapper,
IUmbracoContextAccessor umbracoContextAccessor,
UriUtility uriUtility,
ILocalizationService localizationService)
{
_requestSettings = requestSettings.Value;
_logger = logger;
_siteDomainMapper = siteDomainMapper;
_uriUtility = uriUtility;
_umbracoContextAccessor = umbracoContextAccessor;
_uriUtility = uriUtility;
_localizationService = localizationService;
}

#region GetUrl

/// <inheritdoc />
public virtual UrlInfo GetUrl(IPublishedContent content, UrlMode mode, string culture, Uri current)
{
if (!current.IsAbsoluteUri) throw new ArgumentException("Current URL must be absolute.", nameof(current));
var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
// will not use cache if previewing
var route = umbracoContext.Content.GetRouteById(content.Id, culture);

return GetUrlFromRoute(route, umbracoContext, content.Id, current, mode, culture);
}

internal UrlInfo GetUrlFromRoute(string route, IUmbracoContext umbracoContext, int id, Uri current, UrlMode mode, string culture)
{
if (string.IsNullOrWhiteSpace(route))
{
_logger.LogDebug("Couldn't find any page with nodeId={NodeId}. This is most likely caused by the page not being published.", id);
return null;
}

// extract domainUri and path
// route is /<path> or <domainRootId>/<path>
var pos = route.IndexOf('/');
var path = pos == 0 ? route : route.Substring(pos);
var domainUri = pos == 0
? null
: DomainUtilities.DomainForNode(umbracoContext.PublishedSnapshot.Domains, _siteDomainMapper, int.Parse(route.Substring(0, pos), CultureInfo.InvariantCulture), current, culture);

// assemble the URL from domainUri (maybe null) and path
var url = AssembleUrl(domainUri, path, current, mode).ToString();

return UrlInfo.Url(url, culture);
}

#endregion

#region GetOtherUrls

/// <summary>
/// Gets the other URLs of a published content.
/// Gets the other URLs of a published content.
/// </summary>
/// <param name="umbracoContextAccessor">The Umbraco context.</param>
/// <param name="id">The published content id.</param>
/// <param name="current">The current absolute URL.</param>
/// <returns>The other URLs for the published content.</returns>
/// <remarks>
/// <para>Other URLs are those that <c>GetUrl</c> would not return in the current context, but would be valid
/// URLs for the node in other contexts (different domain for current request, umbracoUrlAlias...).</para>
/// <para>
/// Other URLs are those that <c>GetUrl</c> would not return in the current context, but would be valid
/// URLs for the node in other contexts (different domain for current request, umbracoUrlAlias...).
/// </para>
/// </remarks>
public virtual IEnumerable<UrlInfo> GetOtherUrls(int id, Uri current)
{
var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
var node = umbracoContext.Content.GetById(id);
IUmbracoContext umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
IPublishedContent node = umbracoContext.Content.GetById(id);
if (node == null)
{
yield break;
}

// look for domains, walking up the tree
var n = node;
var domainUris = DomainUtilities.DomainsForNode(umbracoContext.PublishedSnapshot.Domains, _siteDomainMapper, n.Id, current, false);
IPublishedContent n = node;
IEnumerable<DomainAndUri> domainUris =
DomainUtilities.DomainsForNode(umbracoContext.PublishedSnapshot.Domains, _siteDomainMapper, n.Id,
current, false);
while (domainUris == null && n != null) // n is null at root
{
n = n.Parent; // move to parent node
domainUris = n == null ? null : DomainUtilities.DomainsForNode(umbracoContext.PublishedSnapshot.Domains, _siteDomainMapper, n.Id, current, excludeDefault: true);
domainUris = n == null
? null
: DomainUtilities.DomainsForNode(umbracoContext.PublishedSnapshot.Domains, _siteDomainMapper, n.Id,
current);
}

// no domains = exit
if (domainUris ==null)
if (domainUris == null)
{
yield break;
}

foreach (var d in domainUris)
foreach (DomainAndUri d in domainUris)
{
var culture = d?.Culture;

// although we are passing in culture here, if any node in this path is invariant, it ignores the culture anyways so this is ok
var route = umbracoContext.Content.GetRouteById(id, culture);
if (route == null) continue;
if (route == null)
{
continue;
}

// need to strip off the leading ID for the route if it exists (occurs if the route is for a node with a domain assigned)
var pos = route.IndexOf('/');
Expand All @@ -124,9 +117,57 @@ public virtual IEnumerable<UrlInfo> GetOtherUrls(int id, Uri current)

#endregion

#region GetUrl

/// <inheritdoc />
public virtual UrlInfo GetUrl(IPublishedContent content, UrlMode mode, string culture, Uri current)
{
if (!current.IsAbsoluteUri)
{
throw new ArgumentException("Current URL must be absolute.", nameof(current));
}

IUmbracoContext umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
// will not use cache if previewing
var route = umbracoContext.Content.GetRouteById(content.Id, culture);

return GetUrlFromRoute(route, umbracoContext, content.Id, current, mode, culture);
}

internal UrlInfo GetUrlFromRoute(string route, IUmbracoContext umbracoContext, int id, Uri current,
UrlMode mode, string culture)
{
if (string.IsNullOrWhiteSpace(route))
{
_logger.LogDebug(
"Couldn't find any page with nodeId={NodeId}. This is most likely caused by the page not being published.",
id);
return null;
}

// extract domainUri and path
// route is /<path> or <domainRootId>/<path>
var pos = route.IndexOf('/');
var path = pos == 0 ? route : route.Substring(pos);
var domainUri = pos == 0
? null
: DomainUtilities.DomainForNode(umbracoContext.PublishedSnapshot.Domains, _siteDomainMapper, int.Parse(route.Substring(0, pos), CultureInfo.InvariantCulture), current, culture);

var defaultCulture = _localizationService.GetDefaultLanguageIsoCode();
if (domainUri is not null || culture == defaultCulture || culture is null)
{
var url = AssembleUrl(domainUri, path, current, mode).ToString();
return UrlInfo.Url(url, culture);
}

return null;
}

#endregion

#region Utilities

Uri AssembleUrl(DomainAndUri domainUri, string path, Uri current, UrlMode mode)
private Uri AssembleUrl(DomainAndUri domainUri, string path, Uri current, UrlMode mode)
{
Uri uri;

Expand All @@ -135,7 +176,9 @@ Uri AssembleUrl(DomainAndUri domainUri, string path, Uri current, UrlMode mode)
if (domainUri == null) // no domain was found
{
if (current == null)
{
mode = UrlMode.Relative; // best we can do
}

switch (mode)
{
Expand All @@ -155,10 +198,15 @@ Uri AssembleUrl(DomainAndUri domainUri, string path, Uri current, UrlMode mode)
if (mode == UrlMode.Auto)
{
//this check is a little tricky, we can't just compare domains
if (current != null && domainUri.Uri.GetLeftPart(UriPartial.Authority) == current.GetLeftPart(UriPartial.Authority))
if (current != null && domainUri.Uri.GetLeftPart(UriPartial.Authority) ==
current.GetLeftPart(UriPartial.Authority))
{
mode = UrlMode.Relative;
}
else
{
mode = UrlMode.Absolute;
}
}

switch (mode)
Expand All @@ -179,9 +227,9 @@ Uri AssembleUrl(DomainAndUri domainUri, string path, Uri current, UrlMode mode)
return _uriUtility.UriFromUmbraco(uri, _requestSettings);
}

string CombinePaths(string path1, string path2)
private string CombinePaths(string path1, string path2)
{
string path = path1.TrimEnd(Constants.CharArrays.ForwardSlash) + path2;
var path = path1.TrimEnd(Constants.CharArrays.ForwardSlash) + path2;
return path == "/" ? path : path.TrimEnd(Constants.CharArrays.ForwardSlash);
}

Expand Down
Loading

0 comments on commit c77dc5d

Please sign in to comment.