mirror of https://github.com/abpframework/abp.git
committed by
GitHub
36 changed files with 568 additions and 211 deletions
@ -1,11 +1,10 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Application.Dtos; |
|||
using Volo.Abp.Application.Services; |
|||
using Volo.Abp.Content; |
|||
|
|||
namespace Volo.CmsKit.Admin.Pages; |
|||
|
|||
public interface IPageAdminAppService : ICrudAppService<PageDto, PageDto, Guid, GetPagesInputDto, CreatePageInputDto, UpdatePageInputDto> |
|||
{ |
|||
Task SetAsHomePageAsync(Guid id); |
|||
} |
|||
|
|||
@ -0,0 +1,23 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Runtime.Serialization; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp; |
|||
|
|||
namespace Volo.CmsKit.Domain.Volo.CmsKit.Pages; |
|||
|
|||
[Serializable] |
|||
public class MultipleHomePageException : BusinessException |
|||
{ |
|||
public MultipleHomePageException() |
|||
{ |
|||
Code = CmsKitErrorCodes.Pages.MultipleHomePage; |
|||
} |
|||
|
|||
public MultipleHomePageException(SerializationInfo serializationInfo, StreamingContext context) |
|||
: base(serializationInfo, context) |
|||
{ |
|||
} |
|||
} |
|||
@ -0,0 +1,28 @@ |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Volo.CmsKit.Pages; |
|||
using Volo.CmsKit.Public.Pages; |
|||
|
|||
namespace Volo.CmsKit.Public.Web; |
|||
|
|||
public class DefaultHomePageMiddleware |
|||
{ |
|||
private readonly RequestDelegate _next; |
|||
private readonly IPagePublicAppService _pagePublicAppService; |
|||
|
|||
public DefaultHomePageMiddleware(RequestDelegate next, IPagePublicAppService pagePublicAppService) |
|||
{ |
|||
_next = next; |
|||
_pagePublicAppService = pagePublicAppService; |
|||
} |
|||
public async Task InvokeAsync(HttpContext httpContext) |
|||
{ |
|||
var page = await _pagePublicAppService.FindDefaultHomePageAsync(); |
|||
if (page is not null && httpContext.Request.Path.Value == "/") |
|||
{ |
|||
httpContext.Request.Path = $"{PageConsts.UrlPrefix}{page.Slug}"; |
|||
} |
|||
|
|||
await _next(httpContext); |
|||
} |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
using Microsoft.AspNetCore.Builder; |
|||
|
|||
namespace Volo.CmsKit.Public.Web; |
|||
|
|||
public static class HomePageDefaultMiddlewareExtensions |
|||
{ |
|||
public static IApplicationBuilder UseHomePageDefaultMiddleware(this IApplicationBuilder builder) |
|||
{ |
|||
return builder.UseMiddleware<DefaultHomePageMiddleware>(); |
|||
} |
|||
} |
|||
@ -1,70 +1,111 @@ |
|||
using System.Threading.Tasks; |
|||
using Shouldly; |
|||
using Volo.CmsKit.Tags; |
|||
using Xunit; |
|||
|
|||
namespace Volo.CmsKit.Pages; |
|||
|
|||
public class PageManager_Test : CmsKitDomainTestBase |
|||
{ |
|||
private readonly PageManager pageManager; |
|||
private readonly CmsKitTestData testData; |
|||
private readonly IPageRepository pageRepository; |
|||
|
|||
public PageManager_Test() |
|||
{ |
|||
pageManager = GetRequiredService<PageManager>(); |
|||
testData = GetRequiredService<CmsKitTestData>(); |
|||
pageRepository = GetRequiredService<IPageRepository>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task CreateAsync_ShouldWorkProperly_WithNonExistingSlug() |
|||
{ |
|||
var title = "My awesome page"; |
|||
var slug = "my-awesome-page"; |
|||
var content = "<h1>My Awesome Page</h1><p>This is my awesome page content!</p>"; |
|||
|
|||
var page = await pageManager.CreateAsync(title, slug, content); |
|||
|
|||
page.ShouldNotBeNull(); |
|||
page.Title.ShouldBe(title); |
|||
page.Slug.ShouldBe(slug); |
|||
page.Content.ShouldBe(content); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task CreateAsync_ShouldThrowException_WithExistingSlug() |
|||
{ |
|||
var title = "My awesome page"; |
|||
var slug = testData.Page_1_Slug; |
|||
var content = "<h1>My Awesome Page</h1><p>This is my awesome page content!</p>"; |
|||
|
|||
var exception = await Should.ThrowAsync<PageSlugAlreadyExistsException>(async () => |
|||
await pageManager.CreateAsync(title, slug, content)); |
|||
|
|||
exception.ShouldNotBeNull(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task SetSlugAsync_ShouldWorkProperly_WithNonExistingSlug() |
|||
{ |
|||
var newSlug = "freshly-generated-new-slug"; |
|||
var page = await pageRepository.GetAsync(testData.Page_1_Id); |
|||
|
|||
await pageManager.SetSlugAsync(page, newSlug); |
|||
|
|||
page.Slug.ShouldBe(newSlug); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task SetSlugAsync_ShouldThrowException_WithExistingSlug() |
|||
{ |
|||
var newSlug = testData.Page_2_Slug; |
|||
var page = await pageRepository.GetAsync(testData.Page_1_Id); |
|||
|
|||
var exception = await Should.ThrowAsync<PageSlugAlreadyExistsException>(async () => |
|||
await pageManager.SetSlugAsync(page, newSlug)); |
|||
|
|||
exception.ShouldNotBeNull(); |
|||
} |
|||
private readonly PageManager pageManager; |
|||
private readonly CmsKitTestData testData; |
|||
private readonly IPageRepository pageRepository; |
|||
|
|||
public PageManager_Test() |
|||
{ |
|||
pageManager = GetRequiredService<PageManager>(); |
|||
testData = GetRequiredService<CmsKitTestData>(); |
|||
pageRepository = GetRequiredService<IPageRepository>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task CreateAsync_ShouldWorkProperly_WithNonExistingSlug() |
|||
{ |
|||
var title = "My awesome page"; |
|||
var slug = "my-awesome-page"; |
|||
var content = "<h1>My Awesome Page</h1><p>This is my awesome page content!</p>"; |
|||
|
|||
var page = await pageManager.CreateAsync(title, slug, content); |
|||
|
|||
page.ShouldNotBeNull(); |
|||
page.Title.ShouldBe(title); |
|||
page.Slug.ShouldBe(slug); |
|||
page.Content.ShouldBe(content); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task CreateAsync_ShouldThrowException_WithExistingSlug() |
|||
{ |
|||
var title = "My awesome page"; |
|||
var slug = testData.Page_1_Slug; |
|||
var content = "<h1>My Awesome Page</h1><p>This is my awesome page content!</p>"; |
|||
|
|||
var exception = await Should.ThrowAsync<PageSlugAlreadyExistsException>(async () => |
|||
await pageManager.CreateAsync(title, slug, content)); |
|||
|
|||
exception.ShouldNotBeNull(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task SetSlugAsync_ShouldWorkProperly_WithNonExistingSlug() |
|||
{ |
|||
var newSlug = "freshly-generated-new-slug"; |
|||
var page = await pageRepository.GetAsync(testData.Page_1_Id); |
|||
|
|||
await pageManager.SetSlugAsync(page, newSlug); |
|||
|
|||
page.Slug.ShouldBe(newSlug); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task SetSlugAsync_ShouldThrowException_WithExistingSlug() |
|||
{ |
|||
var newSlug = testData.Page_2_Slug; |
|||
var page = await pageRepository.GetAsync(testData.Page_1_Id); |
|||
|
|||
var exception = await Should.ThrowAsync<PageSlugAlreadyExistsException>(async () => |
|||
await pageManager.SetSlugAsync(page, newSlug)); |
|||
|
|||
exception.ShouldNotBeNull(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task SetHomePageAsync_ShouldWorkProperly_IfExistHomePage() |
|||
{ |
|||
var page = await pageRepository.GetAsync(testData.Page_1_Id); |
|||
|
|||
await pageManager.SetHomePageAsync(page); |
|||
|
|||
await pageRepository.UpdateAsync(page); |
|||
|
|||
page.IsHomePage.ShouldBeTrue(); |
|||
|
|||
var pageSetAsHomePageAsFalse = await pageRepository.GetAsync(testData.Page_2_Id); |
|||
pageSetAsHomePageAsFalse.IsHomePage.ShouldBeFalse(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task SetHomePageAsync_ShouldFix_WhenMultipleHomePageExist() |
|||
{ |
|||
await WithUnitOfWorkAsync(async () => |
|||
{ |
|||
var page1 = await pageRepository.GetAsync(testData.Page_1_Id); |
|||
var page2 = await pageRepository.GetAsync(testData.Page_2_Id); |
|||
|
|||
page1.SetIsHomePage(true); |
|||
page2.SetIsHomePage(true); |
|||
|
|||
await pageRepository.UpdateManyAsync(new[] { page1, page2 }, autoSave: true); |
|||
|
|||
await pageManager.SetHomePageAsync(page1); |
|||
}); |
|||
|
|||
var page1 = await pageRepository.GetAsync(testData.Page_1_Id); |
|||
var page2 = await pageRepository.GetAsync(testData.Page_2_Id); |
|||
|
|||
page2 = await pageRepository.GetAsync(testData.Page_2_Id); |
|||
|
|||
page1.IsHomePage.ShouldBeTrue(); |
|||
page2.IsHomePage.ShouldBeFalse(); |
|||
} |
|||
} |
|||
|
|||
Loading…
Reference in new issue