Skip to content

Commit

Permalink
Merge pull request #6 from ljfio/feature/integration-tests
Browse files Browse the repository at this point in the history
Multiple Domain Fixes (and Integration Tests)
  • Loading branch information
ljfio authored Jun 8, 2024
2 parents 1926deb + 2c84fa0 commit a998c3d
Show file tree
Hide file tree
Showing 99 changed files with 3,753 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
appsettings*.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2023 Luke Fisher
// SPDX-License-Identifier: Apache-2.0

using System.Collections.Generic;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Configuration;

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

[Collection("Web")]
public class TestWebApplicationFactory : WebApplicationFactory<Program>
{
private readonly string _connectionString = "Data Source=InMemory;Mode=Memory;Cache=Shared;Pooling=True";
private readonly SqliteConnection _sharedConnection;

public TestWebApplicationFactory()
{
_sharedConnection = new SqliteConnection(_connectionString);
_sharedConnection.Open();
}

protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureAppConfiguration(config =>
{
var inlineConfig = new Dictionary<string, string?>()
{
{ "ConnectionStrings:umbracoDbDSN", _connectionString },
};

config.AddInMemoryCollection(inlineConfig);
});

builder.UseEnvironment("Development");
}

protected override void Dispose(bool disposing)
{
_sharedConnection.Dispose();
base.Dispose(disposing);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright 2023 Luke Fisher
// SPDX-License-Identifier: Apache-2.0

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

[CollectionDefinition("Web")]
public class TestWebApplicationFactoryCollection : ICollectionFixture<TestWebApplicationFactory>
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.6" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/>
<PackageReference Include="xunit" Version="2.4.2"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\UmbracoExampleWeb\UmbracoExampleWeb.csproj" />
</ItemGroup>

</Project>
178 changes: 178 additions & 0 deletions src/Our.Umbraco.InvisibleNodes.Tests.Integration/Tests/DomainTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
// Copyright 2023 Luke Fisher
// SPDX-License-Identifier: Apache-2.0

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;

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

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

public DomainTests(TestWebApplicationFactory 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>();

// Content
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);

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

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")!;

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

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

var danishDomain = new UmbracoDomain("https://example.org/da")
{
RootContentId = homeNode.Id,
LanguageId = danishLanguage.Id,
};
var danishDomainResult = domainService.Save(danishDomain);

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

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

assigned.Should().HaveCount(2);

var publishedAssigned = context.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);
publishedNode.Should().NotBeNull();

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

[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>();

// Content
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);

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

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")!;

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

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

var danishDomain = new UmbracoDomain("https://da.example.org/")
{
RootContentId = homeNode.Id,
LanguageId = danishLanguage.Id,
};
var danishDomainResult = domainService.Save(danishDomain);

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

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

assigned.Should().HaveCount(2);

var publishedAssigned = context.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);
publishedNode.Should().NotBeNull();

var url = urlProvider.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);
}
}
Loading

0 comments on commit a998c3d

Please sign in to comment.