Browse Source

CmsKit - Add more test-cases for BlogPosts

pull/7596/head
enisn 6 years ago
parent
commit
e6db0a1efe
  1. 12
      modules/cms-kit/src/Volo.CmsKit.Admin.Application.Contracts/Volo/CmsKit/Admin/Blogs/BlogPostDto.cs
  2. 8
      modules/cms-kit/src/Volo.CmsKit.Admin.Application.Contracts/Volo/CmsKit/Admin/Blogs/CreateUpdateBlogPostDto.cs
  3. 11
      modules/cms-kit/src/Volo.CmsKit.Admin.Application/Volo/CmsKit/Admin/Blogs/BlogPostAdminAppService.cs
  4. 8
      modules/cms-kit/src/Volo.CmsKit.Admin.Application/Volo/CmsKit/Admin/CmsKitAdminApplicationAutoMapperProfile.cs
  5. 4
      modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Blogs/BlogPostConsts.cs
  6. 3
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/Blog.cs
  7. 4
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/BlogPost.cs
  8. 25
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/BlogPostManager.cs
  9. 6
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/BlogPostUrlSlugAlreadyExistException.cs
  10. 2
      modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/EntityFrameworkCore/CmsKitDbContextModelCreatingExtensions.cs
  11. 199
      modules/cms-kit/test/Volo.CmsKit.Application.Tests/Blogs/BlogPostAdminAppService_Tests.cs
  12. BIN
      modules/cms-kit/test/Volo.CmsKit.Application.Tests/Data/BlogPostSample.png
  13. 8
      modules/cms-kit/test/Volo.CmsKit.Application.Tests/Volo.CmsKit.Application.Tests.csproj
  14. 2
      modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs
  15. 1
      modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitTestData.cs

12
modules/cms-kit/src/Volo.CmsKit.Admin.Application.Contracts/Volo/CmsKit/Admin/Blogs/BlogPostDto.cs

@ -5,16 +5,10 @@ namespace Volo.CmsKit.Admin.Blogs
{ {
public class BlogPostDto : EntityDto<Guid> public class BlogPostDto : EntityDto<Guid>
{ {
public Guid BlogId { get; protected set; } public Guid BlogId { get; set; }
public string Title { get; protected set; } public string Title { get; set; }
public string UrlSlug { get; protected set; } public string UrlSlug { get; set; }
public string CoverImageUrl { get; set; }
public bool IsPublished { get; protected set; }
public DateTime? PublishDate { get; protected set; }
} }
} }

8
modules/cms-kit/src/Volo.CmsKit.Admin.Application.Contracts/Volo/CmsKit/Admin/Blogs/CreateUpdateBlogPostDto.cs

@ -4,12 +4,12 @@ namespace Volo.CmsKit.Admin.Blogs
{ {
public class CreateUpdateBlogPostDto public class CreateUpdateBlogPostDto
{ {
public Guid BlogId { get; protected set; } public Guid BlogId { get; set; }
public string Title { get; protected set; } public string Title { get; set; }
public string UrlSlug { get; protected set; } public string UrlSlug { get; set; }
public string CoverImageUrl { get; set; } public string ShortDescription { get; set; }
} }
} }

11
modules/cms-kit/src/Volo.CmsKit.Admin.Application/Volo/CmsKit/Admin/Blogs/BlogPostAdminAppService.cs

@ -1,8 +1,5 @@
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services; using Volo.Abp.Application.Services;
@ -34,10 +31,12 @@ namespace Volo.CmsKit.Admin.Blogs
public BlogPostAdminAppService( public BlogPostAdminAppService(
IRepository<BlogPost, Guid> repository, IRepository<BlogPost, Guid> repository,
IBlogPostManager blogPostManager, IBlogPostManager blogPostManager,
IBlogPostRepository blogPostRepository) : base(repository) IBlogPostRepository blogPostRepository,
IBlobContainer<BlogPostCoverImageContainer> blobContainer) : base(repository)
{ {
BlogPostManager = blogPostManager; BlogPostManager = blogPostManager;
BlogPostRepository = blogPostRepository; BlogPostRepository = blogPostRepository;
BlobContainer = blobContainer;
GetListPolicyName = CmsKitAdminPermissions.BlogPosts.Default; GetListPolicyName = CmsKitAdminPermissions.BlogPosts.Default;
GetPolicyName = CmsKitAdminPermissions.BlogPosts.Default; GetPolicyName = CmsKitAdminPermissions.BlogPosts.Default;
@ -63,7 +62,7 @@ namespace Volo.CmsKit.Admin.Blogs
input.BlogId, input.BlogId,
input.Title, input.Title,
input.UrlSlug, input.UrlSlug,
input.CoverImageUrl)); input.ShortDescription));
return MapToGetOutputDto(entity); return MapToGetOutputDto(entity);
} }
@ -78,6 +77,8 @@ namespace Volo.CmsKit.Admin.Blogs
MapToEntity(input, entity); MapToEntity(input, entity);
await BlogPostManager.UpdateAsync(entity);
return MapToGetOutputDto(entity); return MapToGetOutputDto(entity);
} }

8
modules/cms-kit/src/Volo.CmsKit.Admin.Application/Volo/CmsKit/Admin/CmsKitAdminApplicationAutoMapperProfile.cs

@ -1,6 +1,8 @@
using AutoMapper; using AutoMapper;
using Volo.CmsKit.Admin.Blogs;
using Volo.CmsKit.Admin.Contents; using Volo.CmsKit.Admin.Contents;
using Volo.CmsKit.Admin.Pages; using Volo.CmsKit.Admin.Pages;
using Volo.CmsKit.Blogs;
using Volo.CmsKit.Contents; using Volo.CmsKit.Contents;
using Volo.CmsKit.Pages; using Volo.CmsKit.Pages;
@ -16,6 +18,12 @@ namespace Volo.CmsKit.Admin
CreateMap<Content, ContentGetListDto>(MemberList.Destination); CreateMap<Content, ContentGetListDto>(MemberList.Destination);
CreateMap<ContentCreateDto, Content>(MemberList.Source); CreateMap<ContentCreateDto, Content>(MemberList.Source);
CreateMap<ContentUpdateDto, Content>(MemberList.Source); CreateMap<ContentUpdateDto, Content>(MemberList.Source);
CreateMap<BlogPost, BlogPostDto>(MemberList.Destination);
CreateMap<CreateUpdateBlogPostDto, BlogPost>(MemberList.Source);
CreateMap<Blog, BlogDto>(MemberList.Destination)
.ReverseMap();
} }
} }
} }

4
modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Blogs/BlogPostConsts.cs

@ -2,10 +2,12 @@
{ {
public static class BlogPostConsts public static class BlogPostConsts
{ {
public static int MaxTitleLength { get; set; } = 256; public static int MaxTitleLength { get; set; } = 64;
public static int MaxUrlSlugLength { get; set; } = 256; public static int MaxUrlSlugLength { get; set; } = 256;
public static int MinUrlSlugLength { get; set; } = 2; public static int MinUrlSlugLength { get; set; } = 2;
public static int MaxShortDescriptionLength { get; set; } = 256;
} }
} }

3
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/Blog.cs

@ -9,8 +9,9 @@ namespace Volo.CmsKit.Blogs
public class Blog : FullAuditedAggregateRoot<Guid>, IMultiTenant public class Blog : FullAuditedAggregateRoot<Guid>, IMultiTenant
{ {
public Blog( public Blog(
Guid id,
[NotNull] string name, [NotNull] string name,
[CanBeNull] Guid? tenantId = null) [CanBeNull] Guid? tenantId = null) : base(id)
{ {
SetName(name); SetName(name);
TenantId = tenantId; TenantId = tenantId;

4
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/BlogPost.cs

@ -15,10 +15,10 @@ namespace Volo.CmsKit.Blogs
public string Title { get; protected set; } public string Title { get; protected set; }
public string ShortDescription { get; set; }
public string UrlSlug { get; protected set; } public string UrlSlug { get; protected set; }
public string ShortDescription { get; set; }
public Guid? TenantId { get; } public Guid? TenantId { get; }
protected BlogPost() protected BlogPost()

25
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/BlogPostManager.cs

@ -9,33 +9,46 @@ namespace Volo.CmsKit.Blogs
{ {
public class BlogPostManager : DomainService, IBlogPostManager public class BlogPostManager : DomainService, IBlogPostManager
{ {
protected readonly IBlogPostRepository _blogPostRepository; protected readonly IBlogPostRepository blogPostRepository;
protected readonly IBlogRepository blogRepository;
public BlogPostManager(IBlogPostRepository blogPostRepository) public BlogPostManager(
IBlogPostRepository blogPostRepository,
IBlogRepository blogRepository)
{ {
_blogPostRepository = blogPostRepository; this.blogPostRepository = blogPostRepository;
this.blogRepository = blogRepository;
} }
public async Task<BlogPost> CreateAsync(BlogPost blogPost) public async Task<BlogPost> CreateAsync(BlogPost blogPost)
{ {
await CheckBlogExistenceAsync(blogPost.BlogId);
await CheckUrlSlugExistenceAsync(blogPost.UrlSlug); await CheckUrlSlugExistenceAsync(blogPost.UrlSlug);
return await _blogPostRepository.InsertAsync(blogPost); return await blogPostRepository.InsertAsync(blogPost);
} }
public async Task UpdateAsync(BlogPost blogPost) public async Task UpdateAsync(BlogPost blogPost)
{ {
await CheckBlogExistenceAsync(blogPost.BlogId);
await CheckUrlSlugExistenceAsync(blogPost.UrlSlug); await CheckUrlSlugExistenceAsync(blogPost.UrlSlug);
await _blogPostRepository.UpdateAsync(blogPost); await blogPostRepository.UpdateAsync(blogPost);
} }
private async Task CheckUrlSlugExistenceAsync(string urlSlug) private async Task CheckUrlSlugExistenceAsync(string urlSlug)
{ {
if (await _blogPostRepository.SlugExistsAsync(urlSlug)) if (await blogPostRepository.SlugExistsAsync(urlSlug))
{ {
throw new BlogPostUrlSlugAlreadyExistException(urlSlug); throw new BlogPostUrlSlugAlreadyExistException(urlSlug);
} }
} }
private async Task CheckBlogExistenceAsync(Guid blogId)
{
await blogRepository.GetAsync(blogId);
}
} }
} }

6
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/BlogPostUrlSlugAlreadyExistException.cs

@ -19,9 +19,13 @@ namespace Volo.CmsKit.Blogs
public BlogPostUrlSlugAlreadyExistException(string urlSlug) public BlogPostUrlSlugAlreadyExistException(string urlSlug)
{ {
UrlSlug = urlSlug;
Code = CmsKitErrorCodes.Blogs.UrlSlugAlreadyExist; Code = CmsKitErrorCodes.Blogs.UrlSlugAlreadyExist;
WithData(nameof(BlogPost.UrlSlug), urlSlug); WithData(nameof(UrlSlug), UrlSlug);
} }
public string UrlSlug { get; }
} }
} }

2
modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/EntityFrameworkCore/CmsKitDbContextModelCreatingExtensions.cs

@ -229,6 +229,8 @@ namespace Volo.CmsKit.EntityFrameworkCore
b.Property(p => p.UrlSlug).IsRequired().HasMaxLength(BlogPostConsts.MaxUrlSlugLength); b.Property(p => p.UrlSlug).IsRequired().HasMaxLength(BlogPostConsts.MaxUrlSlugLength);
b.Property(p => p.ShortDescription).HasMaxLength(BlogPostConsts.MaxShortDescriptionLength);
b.HasIndex(x => x.UrlSlug); b.HasIndex(x => x.UrlSlug);
}); });
} }

199
modules/cms-kit/test/Volo.CmsKit.Application.Tests/Blogs/BlogPostAdminAppService_Tests.cs

@ -1,16 +1,215 @@
using System; using System;
using System.IO;
using System.Threading.Tasks;
using Castle.DynamicProxy.Generators;
using Shouldly; using Shouldly;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Content;
using Volo.Abp.Domain.Entities;
using Volo.CmsKit.Admin.Blogs; using Volo.CmsKit.Admin.Blogs;
using Xunit;
namespace Volo.CmsKit.Blogs namespace Volo.CmsKit.Blogs
{ {
public class BlogPostAdminAppService_Tests : CmsKitApplicationTestBase public class BlogPostAdminAppService_Tests : CmsKitApplicationTestBase
{ {
private readonly IBlogPostAdminAppService blogPostAdminAppService; private readonly IBlogPostAdminAppService blogPostAdminAppService;
private readonly CmsKitTestData cmsKitTestData;
private readonly IBlogPostRepository blogPostRepository;
public BlogPostAdminAppService_Tests() public BlogPostAdminAppService_Tests()
{ {
blogPostAdminAppService = GetRequiredService<IBlogPostAdminAppService>(); blogPostAdminAppService = GetRequiredService<IBlogPostAdminAppService>();
cmsKitTestData = GetRequiredService<CmsKitTestData>();
blogPostRepository = GetRequiredService<IBlogPostRepository>();
} }
[Fact]
public async Task CreateAsync_ShouldWorkProperly_WithCorrectData()
{
var title = "My awesome new Post";
var urlSlug = "my-awesome-new-post";
var shortDescription = "This blog is all about awesomeness 🤗!";
var created = await blogPostAdminAppService.CreateAsync(new CreateUpdateBlogPostDto
{
BlogId = cmsKitTestData.Blog_Id,
Title = title,
UrlSlug = urlSlug,
ShortDescription = shortDescription
});
created.Id.ShouldNotBe(Guid.Empty);
var blogPost = await blogPostRepository.GetAsync(created.Id);
blogPost.Title.ShouldBe(title);
blogPost.UrlSlug.ShouldBe(urlSlug);
blogPost.ShortDescription.ShouldBe(shortDescription);
}
[Fact]
public async Task CreateAsync_ShouldThrowException_WithNonExistingBlogId()
{
var title = "Another My Awesome New Post";
var urlSlug = "another-my-awesome-new-post";
var shortDescription = "This blog is all about awesomeness 🤗!";
var dto = new CreateUpdateBlogPostDto
{
// Non-existing Id
BlogId = Guid.NewGuid(),
Title = title,
UrlSlug = urlSlug,
ShortDescription = shortDescription
};
var exception = await Should.ThrowAsync<EntityNotFoundException>(async () =>
await blogPostAdminAppService.CreateAsync(dto));
exception.EntityType.ShouldBe(typeof(Blog));
}
[Fact]
public async Task GetAsync_ShouldWorkProperly_WithExistingId()
{
var blogPost = await blogPostAdminAppService.GetAsync(cmsKitTestData.BlogPost_1_Id);
blogPost.Title.ShouldBe(cmsKitTestData.BlogPost_1_Title);
blogPost.UrlSlug.ShouldBe(cmsKitTestData.BlogPost_1_UrlSlug);
}
[Fact]
public async Task GetAsync_ShouldThrowException_WithNonExistingId()
{
var nonExistingId = Guid.NewGuid();
var exception = await Should.ThrowAsync<EntityNotFoundException>(async () =>
await blogPostAdminAppService.GetAsync(nonExistingId));
exception.EntityType.ShouldBe(typeof(BlogPost));
exception.Id.ShouldBe(nonExistingId);
}
[Fact]
public async Task GetByUrlSlugAsync_ShouldWorkProperly_WithExistingUrlSlug()
{
var blogPost = await blogPostAdminAppService.GetByUrlSlugAsync(cmsKitTestData.BlogPost_1_UrlSlug);
blogPost.Id.ShouldBe(cmsKitTestData.BlogPost_1_Id);
blogPost.Title.ShouldBe(cmsKitTestData.BlogPost_1_Title);
}
[Fact]
public async Task GetByUrlSlugAsync_ShouldThrowException_WithNonExistingUrlSlug()
{
var nonExistingUrlSlug = "any-other-url";
var exception = await Should.ThrowAsync<EntityNotFoundException>(async () =>
await blogPostAdminAppService.GetByUrlSlugAsync(cmsKitTestData.Page_1_Url));
exception.EntityType.ShouldBe(typeof(BlogPost));
}
[Fact]
public async Task GetListAsync_ShouldWorkProperly_WithDefaultParameters()
{
var list = await blogPostAdminAppService.GetListAsync(new PagedAndSortedResultRequestDto());
list.ShouldNotBeNull();
list.TotalCount.ShouldBe(2);
list.Items.ShouldNotBeEmpty();
list.Items.Count.ShouldBe(2);
}
[Fact]
public async Task UpdateAsync_ShouldWorkProperly_WithRegularDatas()
{
var shortDescription = "Another short description";
var title = "[Solved] Another Blog Post";
var urlSlug = "another-short-blog-post";
await blogPostAdminAppService.UpdateAsync(cmsKitTestData.BlogPost_2_Id, new CreateUpdateBlogPostDto
{
BlogId = cmsKitTestData.Blog_Id,
ShortDescription = shortDescription,
Title = title,
UrlSlug = urlSlug,
});
var blogPost = await blogPostRepository.GetAsync(cmsKitTestData.BlogPost_2_Id);
blogPost.Title.ShouldBe(title);
blogPost.ShortDescription.ShouldBe(shortDescription);
blogPost.UrlSlug.ShouldBe(urlSlug);
}
[Fact]
public async Task UpdateAsync_ShouldThrowException_WhileChangingWithNonExistingBlogId()
{
var nonExistingId = Guid.NewGuid();
var dto = new CreateUpdateBlogPostDto
{
BlogId = nonExistingId,
Title = cmsKitTestData.Page_2_Title,
UrlSlug = cmsKitTestData.BlogPost_2_UrlSlug
};
var exception = await Should.ThrowAsync<EntityNotFoundException>(async () =>
await blogPostAdminAppService.UpdateAsync(cmsKitTestData.BlogPost_2_Id, dto));
exception.EntityType.ShouldBe(typeof(Blog));
exception.Id.ShouldBe(nonExistingId);
}
[Fact]
public async Task UpdateAsync_ShouldThrowException_WhileUpdatingWithAlreadyExistingSlug()
{
var dto = new CreateUpdateBlogPostDto
{
BlogId = cmsKitTestData.Blog_Id,
Title = "Some new title",
UrlSlug = cmsKitTestData.BlogPost_1_UrlSlug
};
var exception = await Should.ThrowAsync<BlogPostUrlSlugAlreadyExistException>(async () =>
await blogPostAdminAppService.UpdateAsync(cmsKitTestData.BlogPost_2_Id, dto));
exception.UrlSlug.ShouldBe(cmsKitTestData.BlogPost_1_UrlSlug);
}
[Fact]
public async Task DeleteAsync_ShouldWorkProperly_WithExistingId()
{
await blogPostAdminAppService.DeleteAsync(cmsKitTestData.Page_2_Id);
var exception = await Should.ThrowAsync<EntityNotFoundException>(async () =>
await blogPostRepository.GetAsync(cmsKitTestData.Page_2_Id));
exception.EntityType.ShouldBe(typeof(BlogPost));
exception.Id.ShouldBe(cmsKitTestData.Page_2_Id);
}
[Fact]
public async Task SetCoverImage_ShouldNotThrowException_WithCorrectData()
{
using (var imageStream = GetSampleImageStream())
{
await WithUnitOfWorkAsync(async () =>
{
await blogPostAdminAppService.SetCoverImageAsync(
cmsKitTestData.BlogPost_2_Id,
new RemoteStreamContent(imageStream));
});
}
}
private Stream GetSampleImageStream()
{
var assembly = GetType().Assembly;
var resourceName = "Volo.CmsKit.Data.BlogPostSample.png";
return assembly.GetManifestResourceStream(resourceName);
}
// SetCoverImage
} }
} }

BIN
modules/cms-kit/test/Volo.CmsKit.Application.Tests/Data/BlogPostSample.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

8
modules/cms-kit/test/Volo.CmsKit.Application.Tests/Volo.CmsKit.Application.Tests.csproj

@ -5,6 +5,14 @@
<RootNamespace>Volo.CmsKit</RootNamespace> <RootNamespace>Volo.CmsKit</RootNamespace>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<None Remove="Data\BlogPostSample.png" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Data\**\*" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\src\Volo.CmsKit.Application\Volo.CmsKit.Application.csproj" /> <ProjectReference Include="..\..\src\Volo.CmsKit.Application\Volo.CmsKit.Application.csproj" />
<ProjectReference Include="..\Volo.CmsKit.Domain.Tests\Volo.CmsKit.Domain.Tests.csproj" /> <ProjectReference Include="..\Volo.CmsKit.Domain.Tests\Volo.CmsKit.Domain.Tests.csproj" />

2
modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs

@ -276,7 +276,7 @@ namespace Volo.CmsKit
private async Task SeedBlogsAsync() private async Task SeedBlogsAsync()
{ {
var blog = await _blogRepository.InsertAsync(new Blog(_cmsKitTestData.BlogName)); var blog = await _blogRepository.InsertAsync(new Blog(_cmsKitTestData.Blog_Id, _cmsKitTestData.BlogName));
await _blogPostRepository.InsertAsync(new BlogPost(_cmsKitTestData.BlogPost_1_Id, blog.Id, _cmsKitTestData.BlogPost_1_Title, _cmsKitTestData.BlogPost_1_UrlSlug, "Short desc 1")); await _blogPostRepository.InsertAsync(new BlogPost(_cmsKitTestData.BlogPost_1_Id, blog.Id, _cmsKitTestData.BlogPost_1_Title, _cmsKitTestData.BlogPost_1_UrlSlug, "Short desc 1"));

1
modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitTestData.cs

@ -7,6 +7,7 @@ namespace Volo.CmsKit
public class CmsKitTestData : ISingletonDependency public class CmsKitTestData : ISingletonDependency
{ {
public Guid User1Id { get; } = Guid.NewGuid(); public Guid User1Id { get; } = Guid.NewGuid();
public string User1UserName => "fake.user"; public string User1UserName => "fake.user";
public Guid User2Id { get; } = Guid.NewGuid(); public Guid User2Id { get; } = Guid.NewGuid();

Loading…
Cancel
Save