Skip to content

Commit

Permalink
Refactor tests to use a base class
Browse files Browse the repository at this point in the history
  • Loading branch information
ljfio committed Jun 27, 2024
1 parent f91e422 commit a042045
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 149 deletions.
137 changes: 49 additions & 88 deletions src/Our.Umbraco.InvisibleNodes.Tests.Integration/Tests/DomainTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,54 @@

using System;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Our.Umbraco.InvisibleNodes.Tests.Integration.Core;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Web.Common.PublishedModels;
using Umbraco.Extensions;

namespace Our.Umbraco.InvisibleNodes.Tests.Integration.Tests;

[Collection("Web")]
public class DomainTests : IDisposable
public class DomainTests : IntegrationTestBase, IDisposable
{
private readonly TestWebApplicationFactory _factory;

public DomainTests(TestWebApplicationFactory factory)
public DomainTests(TestWebApplicationFactory factory) : base(factory)
{
_factory = factory;
}

[Fact]
public void Should_Return_Nested()
{
var contextFactory = _factory.Services.GetRequiredService<IUmbracoContextFactory>();
using var context = contextFactory.EnsureUmbracoContext();

var contentService = _factory.Services.GetRequiredService<IContentService>();
var domainService = _factory.Services.GetRequiredService<IDomainService>();
var localizationService = _factory.Services.GetRequiredService<ILocalizationService>();
var urlProvider = _factory.Services.GetRequiredService<IPublishedUrlProvider>();
using var context = UmbracoContext;

// Content
var homeNode = contentService.Create("Home", Constants.System.Root, HomePage.ModelTypeAlias);
var homePublishResult = contentService.SaveAndPublish(homeNode);
var homeNode = ContentService.Create("Home", Constants.System.Root, HomePage.ModelTypeAlias);
var homePublishResult = ContentService.SaveAndPublish(homeNode);

homePublishResult.Success.Should().BeTrue();

var contentNode = contentService.Create("Content", homeNode, ContentPage.ModelTypeAlias);
var contentPublishResult = contentService.SaveAndPublish(contentNode);
var contentNode = ContentService.Create("Content", homeNode, ContentPage.ModelTypeAlias);
var contentPublishResult = ContentService.SaveAndPublish(contentNode);

contentPublishResult.Success.Should().BeTrue();

var nestedNode = contentService.Create("Nested", contentNode, ContentPage.ModelTypeAlias);
var nestedPublishResult = contentService.SaveAndPublish(nestedNode);
var nestedNode = ContentService.Create("Nested", contentNode, ContentPage.ModelTypeAlias);
var nestedPublishResult = ContentService.SaveAndPublish(nestedNode);

nestedPublishResult.Success.Should().BeTrue();

// Languages
var englishLanguage = localizationService.GetLanguageByIsoCode("en-US")!;
var danishLanguage = localizationService.GetLanguageByIsoCode("da-DK")!;
var englishLanguage = LocalizationService.GetLanguageByIsoCode("en-US")!;
var danishLanguage = LocalizationService.GetLanguageByIsoCode("da-DK")!;

// Domains
var englishDomain = new UmbracoDomain("https://example.org/en")
{
RootContentId = homeNode.Id,
LanguageId = englishLanguage.Id,
};
var englishDomainResult = domainService.Save(englishDomain);
var englishDomainResult = DomainService.Save(englishDomain);

englishDomainResult.Success.Should().BeTrue();

Expand All @@ -72,61 +59,55 @@ public void Should_Return_Nested()
RootContentId = homeNode.Id,
LanguageId = danishLanguage.Id,
};
var danishDomainResult = domainService.Save(danishDomain);
var danishDomainResult = DomainService.Save(danishDomain);

danishDomainResult.Success.Should().BeTrue();

var assigned = domainService.GetAssignedDomains(homeNode.Id, true);
var assigned = DomainService.GetAssignedDomains(homeNode.Id, true);

assigned.Should().HaveCount(2);

var publishedAssigned = context.UmbracoContext.Domains!.GetAssigned(homeNode.Id, true);
var publishedAssigned = UmbracoContext.Domains!.GetAssigned(homeNode.Id, true);

publishedAssigned.Should().HaveCount(2);

// Check pages
var currentUri = new Uri("https://example.org/");

var publishedNode = context.UmbracoContext.Content!.GetById(nestedNode.Id);
var publishedNode = UmbracoContext.Content!.GetById(nestedNode.Id);
publishedNode.Should().NotBeNull();

var url = urlProvider.GetUrl(publishedNode!, UrlMode.Absolute, "da-DK", currentUri);
var url = PublishedUrlProvider.GetUrl(publishedNode!, UrlMode.Absolute, "da-DK", currentUri);
url.Should().Be("https://example.org/da/content/nested/");
}

[Fact]
public void Should_Return_Variant()
{
var contextFactory = _factory.Services.GetRequiredService<IUmbracoContextFactory>();
using var context = contextFactory.EnsureUmbracoContext();

var contentService = _factory.Services.GetRequiredService<IContentService>();
var domainService = _factory.Services.GetRequiredService<IDomainService>();
var localizationService = _factory.Services.GetRequiredService<ILocalizationService>();
var urlProvider = _factory.Services.GetRequiredService<IPublishedUrlProvider>();
using var context = UmbracoContext;

// Content
var homeNode = contentService.Create("Home", Constants.System.Root, HomePage.ModelTypeAlias);
var homePublishResult = contentService.SaveAndPublish(homeNode);
var homeNode = ContentService.Create("Home", Constants.System.Root, HomePage.ModelTypeAlias);
var homePublishResult = ContentService.SaveAndPublish(homeNode);

homePublishResult.Success.Should().BeTrue();

var contentNode = contentService.Create("Content", homeNode, ContentPage.ModelTypeAlias);
var contentPublishResult = contentService.SaveAndPublish(contentNode);
var contentNode = ContentService.Create("Content", homeNode, ContentPage.ModelTypeAlias);
var contentPublishResult = ContentService.SaveAndPublish(contentNode);

contentPublishResult.Success.Should().BeTrue();

// Languages
var englishLanguage = localizationService.GetLanguageByIsoCode("en-US")!;
var danishLanguage = localizationService.GetLanguageByIsoCode("da-DK")!;
var englishLanguage = LocalizationService.GetLanguageByIsoCode("en-US")!;
var danishLanguage = LocalizationService.GetLanguageByIsoCode("da-DK")!;

// Domains
var englishDomain = new UmbracoDomain("https://example.org/")
{
RootContentId = homeNode.Id,
LanguageId = englishLanguage.Id,
};
var englishDomainResult = domainService.Save(englishDomain);
var englishDomainResult = DomainService.Save(englishDomain);

englishDomainResult.Success.Should().BeTrue();

Expand All @@ -135,67 +116,62 @@ public void Should_Return_Variant()
RootContentId = homeNode.Id,
LanguageId = danishLanguage.Id,
};
var danishDomainResult = domainService.Save(danishDomain);
var danishDomainResult = DomainService.Save(danishDomain);

danishDomainResult.Success.Should().BeTrue();

var assigned = domainService.GetAssignedDomains(homeNode.Id, true);
var assigned = DomainService.GetAssignedDomains(homeNode.Id, true);

assigned.Should().HaveCount(2);

var publishedAssigned = context.UmbracoContext.Domains!.GetAssigned(homeNode.Id, true);
var publishedAssigned = UmbracoContext.Domains!.GetAssigned(homeNode.Id, true);

publishedAssigned.Should().HaveCount(2);

// Check node URLs
var publishedNode = context.UmbracoContext.Content!.GetById(contentNode.Id);
var publishedNode = UmbracoContext.Content!.GetById(contentNode.Id);
publishedNode.Should().NotBeNull();

publishedNode!.Url(urlProvider, "en-US").Should().Be("/content/");
publishedNode!.Url(urlProvider, "en-US", UrlMode.Absolute).Should().Be("https://example.org/content/");
publishedNode!.Url(PublishedUrlProvider, "en-US").Should().Be("/content/");
publishedNode!.Url(PublishedUrlProvider, "en-US", UrlMode.Absolute).Should().Be("https://example.org/content/");

publishedNode!.Url(urlProvider, "da-DK").Should().Be("/da/content/");
publishedNode!.Url(urlProvider, "da-DK", UrlMode.Absolute).Should().Be("https://example.org/da/content/");
publishedNode!.Url(PublishedUrlProvider, "da-DK").Should().Be("/da/content/");
publishedNode!.Url(PublishedUrlProvider, "da-DK", UrlMode.Absolute).Should()
.Be("https://example.org/da/content/");
}

[Fact]
public void Should_Return_Unique_HostNames()
{
var contextFactory = _factory.Services.GetRequiredService<IUmbracoContextFactory>();
using var context = contextFactory.EnsureUmbracoContext();

var contentService = _factory.Services.GetRequiredService<IContentService>();
var domainService = _factory.Services.GetRequiredService<IDomainService>();
var localizationService = _factory.Services.GetRequiredService<ILocalizationService>();
var urlProvider = _factory.Services.GetRequiredService<IPublishedUrlProvider>();
using var context = UmbracoContext;

// Content
var homeNode = contentService.Create("Home", Constants.System.Root, HomePage.ModelTypeAlias);
var homePublishResult = contentService.SaveAndPublish(homeNode);
var homeNode = ContentService.Create("Home", Constants.System.Root, HomePage.ModelTypeAlias);
var homePublishResult = ContentService.SaveAndPublish(homeNode);

homePublishResult.Success.Should().BeTrue();

var contentNode = contentService.Create("Content", homeNode, ContentPage.ModelTypeAlias);
var contentPublishResult = contentService.SaveAndPublish(contentNode);
var contentNode = ContentService.Create("Content", homeNode, ContentPage.ModelTypeAlias);
var contentPublishResult = ContentService.SaveAndPublish(contentNode);

contentPublishResult.Success.Should().BeTrue();

var nestedNode = contentService.Create("Nested", contentNode, ContentPage.ModelTypeAlias);
var nestedPublishResult = contentService.SaveAndPublish(nestedNode);
var nestedNode = ContentService.Create("Nested", contentNode, ContentPage.ModelTypeAlias);
var nestedPublishResult = ContentService.SaveAndPublish(nestedNode);

nestedPublishResult.Success.Should().BeTrue();

// Languages
var englishLanguage = localizationService.GetLanguageByIsoCode("en-US")!;
var danishLanguage = localizationService.GetLanguageByIsoCode("da-DK")!;
var englishLanguage = LocalizationService.GetLanguageByIsoCode("en-US")!;
var danishLanguage = LocalizationService.GetLanguageByIsoCode("da-DK")!;

// Domains
var englishDomain = new UmbracoDomain("https://en.example.org/")
{
RootContentId = homeNode.Id,
LanguageId = englishLanguage.Id,
};
var englishDomainResult = domainService.Save(englishDomain);
var englishDomainResult = DomainService.Save(englishDomain);

englishDomainResult.Success.Should().BeTrue();

Expand All @@ -204,40 +180,25 @@ public void Should_Return_Unique_HostNames()
RootContentId = homeNode.Id,
LanguageId = danishLanguage.Id,
};
var danishDomainResult = domainService.Save(danishDomain);
var danishDomainResult = DomainService.Save(danishDomain);

danishDomainResult.Success.Should().BeTrue();

var assigned = domainService.GetAssignedDomains(homeNode.Id, true);
var assigned = DomainService.GetAssignedDomains(homeNode.Id, true);

assigned.Should().HaveCount(2);

var publishedAssigned = context.UmbracoContext.Domains!.GetAssigned(homeNode.Id, true);
var publishedAssigned = UmbracoContext.Domains!.GetAssigned(homeNode.Id, true);

publishedAssigned.Should().HaveCount(2);

// Check pages
var currentUri = new Uri("https://en.example.org/");

var publishedNode = context.UmbracoContext.Content!.GetById(nestedNode.Id);
var publishedNode = UmbracoContext.Content!.GetById(nestedNode.Id);
publishedNode.Should().NotBeNull();

var url = urlProvider.GetUrl(publishedNode!, UrlMode.Absolute, "da-DK", currentUri);
var url = PublishedUrlProvider.GetUrl(publishedNode!, UrlMode.Absolute, "da-DK", currentUri);
url.Should().Be("https://da.example.org/content/nested/");
}

public void Dispose()
{
// Delete all content
var contentService = _factory.Services.GetRequiredService<IContentService>();

foreach (var content in contentService.GetRootContent())
contentService.Delete(content);

// Delete all domains
var domainService = _factory.Services.GetRequiredService<IDomainService>();

foreach (var domain in domainService.GetAll(true))
domainService.Delete(domain);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2023 Luke Fisher
// SPDX-License-Identifier: Apache-2.0

using System;
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection;
using Our.Umbraco.InvisibleNodes.Tests.Integration.Core;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Web;

namespace Our.Umbraco.InvisibleNodes.Tests.Integration.Tests;

public abstract class IntegrationTestBase(TestWebApplicationFactory factory) : IDisposable
{
private IContentService? _contentService;
private IDomainService? _domainService;
private ILocalizationService? _localizationService;

protected IServiceProvider Services => factory.Services;

protected HttpClient HttpClient => factory.CreateClient();

protected IContentService ContentService =>
_contentService ??= Services.GetRequiredService<IContentService>();

protected IDomainService DomainService =>
_domainService ??= Services.GetRequiredService<IDomainService>();

protected ILocalizationService LocalizationService =>
_localizationService ??= Services.GetRequiredService<ILocalizationService>();

protected IPublishedUrlProvider PublishedUrlProvider => Services.GetRequiredService<IPublishedUrlProvider>();

private UmbracoContextReference UmbracoContextReference => Services
.GetRequiredService<IUmbracoContextFactory>()
.EnsureUmbracoContext();

protected IUmbracoContext UmbracoContext => UmbracoContextReference.UmbracoContext;

public void Dispose()
{
// Cleanup content if used
if (_contentService is not null)
foreach (var content in _contentService.GetRootContent())
_contentService.Delete(content);

// Cleanup domains if used
if (_domainService is not null)
foreach (var content in _domainService.GetAll(true))
_domainService.Delete(content);
}
}
Loading

0 comments on commit a042045

Please sign in to comment.