diff --git a/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Comments/MongoCommentRepository.cs b/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Comments/MongoCommentRepository.cs index 9af5a72845..26a7c09a8c 100644 --- a/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Comments/MongoCommentRepository.cs +++ b/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Comments/MongoCommentRepository.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading; using System.Threading.Tasks; using MongoDB.Driver.Linq; @@ -24,17 +25,26 @@ namespace Volo.CmsKit.MongoDB.Comments Check.NotNullOrWhiteSpace(entityType, nameof(entityType)); Check.NotNullOrWhiteSpace(entityId, nameof(entityId)); - var query = from comment in GetMongoQueryable() + var authorsQuery = from comment in GetMongoQueryable() join user in DbContext.CmsUsers on comment.CreatorId equals user.Id where entityType == comment.EntityType && entityId == comment.EntityId orderby comment.CreationTime - select new CommentWithAuthor - { - Comment = comment, - Author = user - }; + select user; - return await query.ToListAsync(); + var authors = await authorsQuery.ToListAsync(); + + var comments = await GetMongoQueryable() + .Where(c => c.EntityId == entityId && c.EntityType == entityType) + .OrderBy(c => c.CreationTime).ToListAsync(); + + return comments + .Select( + comment => + new CommentWithAuthor + { + Comment = comment, + Author = authors.FirstOrDefault(a => a.Id == comment.CreatorId) + }).ToList(); } public override async Task DeleteAsync(Guid id, bool autoSave = false, CancellationToken cancellationToken = default) diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Comments/CommentPublicAppService.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Comments/CommentPublicAppService.cs index 85abb442b7..9e197a29ee 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Comments/CommentPublicAppService.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Comments/CommentPublicAppService.cs @@ -59,7 +59,7 @@ namespace Volo.CmsKit.Public.Comments input.Text, input.RepliedCommentId, user.Id, - CurrentTenant.GetId() + CurrentTenant.Id )); return ObjectMapper.Map(comment); diff --git a/modules/cms-kit/test/Volo.CmsKit.Application.Tests/CmsKitApplicationTestBase.cs b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/CmsKitApplicationTestBase.cs index ada0bb99c2..125629526d 100644 --- a/modules/cms-kit/test/Volo.CmsKit.Application.Tests/CmsKitApplicationTestBase.cs +++ b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/CmsKitApplicationTestBase.cs @@ -1,4 +1,7 @@ -namespace Volo.CmsKit +using System; +using Volo.CmsKit.EntityFrameworkCore; + +namespace Volo.CmsKit { /* Inherit from this class for your application layer tests. * See SampleAppService_Tests for example. @@ -6,5 +9,21 @@ public abstract class CmsKitApplicationTestBase : CmsKitTestBase { + protected virtual void UsingDbContext(Action action) + { + using (var dbContext = GetRequiredService()) + { + action.Invoke(dbContext); + } + } + + protected virtual T UsingDbContext(Func action) + { + using (var dbContext = GetRequiredService()) + { + return action.Invoke(dbContext); + } + } + } -} \ No newline at end of file +} diff --git a/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Comments/CommentPublicAppService_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Comments/CommentPublicAppService_Tests.cs new file mode 100644 index 0000000000..9568f79c6f --- /dev/null +++ b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Comments/CommentPublicAppService_Tests.cs @@ -0,0 +1,101 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using NSubstitute; +using Shouldly; +using Volo.Abp.MultiTenancy; +using Volo.Abp.Users; +using Volo.CmsKit.Public.Comments; +using Xunit; + +namespace Volo.CmsKit.Comments +{ + public class CommentPublicAppService_Tests : CmsKitApplicationTestBase + { + private readonly ICommentPublicAppService _commentAppService; + private ICurrentUser _currentUser; + private readonly CmsKitTestData _cmsKitTestData; + + public CommentPublicAppService_Tests() + { + _commentAppService = GetRequiredService(); + _cmsKitTestData = GetRequiredService(); + } + + protected override void AfterAddApplication(IServiceCollection services) + { + _currentUser = Substitute.For(); + services.AddSingleton(_currentUser); + } + + [Fact] + public async Task GetAllForEntityAsync() + { + var list = await _commentAppService.GetAllForEntityAsync(_cmsKitTestData.EntityType1, _cmsKitTestData.EntityId1); + + list.Items.Count.ShouldBe(2); + list.Items.First().Replies.Count.ShouldBe(2); + } + + [Fact] + public async Task CreateAsync() + { + _currentUser.Id.Returns(_cmsKitTestData.User2Id); + + var newComment = await _commentAppService.CreateAsync(new CreateCommentInput + { + EntityId = _cmsKitTestData.EntityId1, + EntityType = _cmsKitTestData.EntityType1, + RepliedCommentId = null, + Text = "newComment" + }); + + UsingDbContext(context => + { + var comments = context.Comments.Where(x => + x.EntityId == _cmsKitTestData.EntityId1 && x.EntityType == _cmsKitTestData.EntityType1).ToList(); + + comments + .Any(c=>c.Id == newComment.Id && c.CreatorId == newComment.CreatorId && c.Text == "newComment") + .ShouldBeTrue(); + }); + } + + [Fact] + public async Task UpdateAsync() + { + _currentUser.Id.Returns(_cmsKitTestData.User1Id); + + await _commentAppService.UpdateAsync(_cmsKitTestData.CommentWithChildId, new UpdateCommentInput + { + Text = "I'm Updated" + }); + + UsingDbContext(context => + { + var comment = context.Comments.Single(x => + x.Id == _cmsKitTestData.CommentWithChildId); + + comment.Text.ShouldBe("I'm Updated"); + }); + } + + [Fact] + public async Task DeleteAsync() + { + _currentUser.Id.Returns(_cmsKitTestData.User1Id); + + await _commentAppService.DeleteAsync(_cmsKitTestData.CommentWithChildId); + + UsingDbContext(context => + { + var comment = context.Comments.FirstOrDefault(x => + x.Id == _cmsKitTestData.CommentWithChildId); + + comment.ShouldBeNull(); + }); + } + } +} diff --git a/modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Samples/SampleManager_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Samples/SampleManager_Tests.cs deleted file mode 100644 index 2fe5d2ca29..0000000000 --- a/modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Samples/SampleManager_Tests.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.Threading.Tasks; -using Xunit; - -namespace Volo.CmsKit.Samples -{ - public class SampleManager_Tests : CmsKitDomainTestBase - { - //private readonly SampleManager _sampleManager; - - public SampleManager_Tests() - { - //_sampleManager = GetRequiredService(); - } - - [Fact] - public async Task Method1Async() - { - - } - } -} diff --git a/modules/cms-kit/test/Volo.CmsKit.EntityFrameworkCore.Tests/EntityFrameworkCore/Comments/CommentRepository_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.EntityFrameworkCore.Tests/EntityFrameworkCore/Comments/CommentRepository_Tests.cs new file mode 100644 index 0000000000..69b3da3d9e --- /dev/null +++ b/modules/cms-kit/test/Volo.CmsKit.EntityFrameworkCore.Tests/EntityFrameworkCore/Comments/CommentRepository_Tests.cs @@ -0,0 +1,9 @@ +using Volo.CmsKit.Comments; + +namespace Volo.CmsKit.EntityFrameworkCore.Comments +{ + public class CommentRepository_Tests : CommentRepository_Tests + { + + } +} diff --git a/modules/cms-kit/test/Volo.CmsKit.EntityFrameworkCore.Tests/EntityFrameworkCore/Samples/SampleRepository_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.EntityFrameworkCore.Tests/EntityFrameworkCore/Samples/SampleRepository_Tests.cs deleted file mode 100644 index 5e5cb3652f..0000000000 --- a/modules/cms-kit/test/Volo.CmsKit.EntityFrameworkCore.Tests/EntityFrameworkCore/Samples/SampleRepository_Tests.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Volo.CmsKit.Samples; - -namespace Volo.CmsKit.EntityFrameworkCore.Samples -{ - public class SampleRepository_Tests : SampleRepository_Tests - { - /* Don't write custom repository tests here, instead write to - * the base class. - * One exception can be some specific tests related to EF core. - */ - } -} diff --git a/modules/cms-kit/test/Volo.CmsKit.MongoDB.Tests/MongoDB/Comments/CommentRepository_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.MongoDB.Tests/MongoDB/Comments/CommentRepository_Tests.cs new file mode 100644 index 0000000000..cf2e936ca8 --- /dev/null +++ b/modules/cms-kit/test/Volo.CmsKit.MongoDB.Tests/MongoDB/Comments/CommentRepository_Tests.cs @@ -0,0 +1,9 @@ +using Volo.CmsKit.Comments; + +namespace Volo.CmsKit.MongoDB.Comments +{ + public class CommentRepository_Tests : CommentRepository_Tests + { + + } +} diff --git a/modules/cms-kit/test/Volo.CmsKit.MongoDB.Tests/MongoDB/Samples/SampleRepository_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.MongoDB.Tests/MongoDB/Samples/SampleRepository_Tests.cs deleted file mode 100644 index a93724c803..0000000000 --- a/modules/cms-kit/test/Volo.CmsKit.MongoDB.Tests/MongoDB/Samples/SampleRepository_Tests.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Volo.CmsKit.Samples; -using Xunit; - -namespace Volo.CmsKit.MongoDB.Samples -{ - [Collection(MongoTestCollection.Name)] - public class SampleRepository_Tests : SampleRepository_Tests - { - /* Don't write custom repository tests here, instead write to - * the base class. - * One exception can be some specific tests related to MongoDB. - */ - } -} diff --git a/modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs b/modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs index 8b33f127c2..9b2d5ddb5c 100644 --- a/modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs +++ b/modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs @@ -2,26 +2,87 @@ using Volo.Abp.Data; using Volo.Abp.DependencyInjection; using Volo.Abp.Guids; +using Volo.Abp.Users; +using Volo.CmsKit.Comments; +using Volo.CmsKit.Reactions; +using Volo.CmsKit.Users; namespace Volo.CmsKit { public class CmsKitDataSeedContributor : IDataSeedContributor, ITransientDependency { private readonly IGuidGenerator _guidGenerator; + private readonly ICmsUserRepository _cmsUserRepository; + private readonly CmsKitTestData _cmsKitTestData; + private readonly IUserReactionRepository _userReactionRepository; + private readonly ICommentRepository _commentRepository; public CmsKitDataSeedContributor( - IGuidGenerator guidGenerator) + IGuidGenerator guidGenerator, + ICmsUserRepository cmsUserRepository, + CmsKitTestData cmsKitTestData, + IUserReactionRepository userReactionRepository, + ICommentRepository commentRepository) { _guidGenerator = guidGenerator; + _cmsUserRepository = cmsUserRepository; + _cmsKitTestData = cmsKitTestData; + _userReactionRepository = userReactionRepository; + _commentRepository = commentRepository; } - - public Task SeedAsync(DataSeedContext context) + + public async Task SeedAsync(DataSeedContext context) { - /* Instead of returning the Task.CompletedTask, you can insert your test data - * at this point! - */ + await _cmsUserRepository.InsertAsync(new CmsUser(new UserData(_cmsKitTestData.User1Id, "user1","user1@volo.com", "user","1"))); + await _cmsUserRepository.InsertAsync(new CmsUser(new UserData(_cmsKitTestData.User2Id, "user2","user2@volo.com", "user","2"))); + + var comment1 = await _commentRepository.InsertAsync(new Comment(_cmsKitTestData.CommentWithChildId, + _cmsKitTestData.EntityType1, + _cmsKitTestData.EntityId1, + "comment", + null, + _cmsKitTestData.User1Id + )); + + await _commentRepository.InsertAsync(new Comment(_guidGenerator.Create(), + _cmsKitTestData.EntityType1, + _cmsKitTestData.EntityId1, + "reply", + comment1.Id, + _cmsKitTestData.User2Id + )); + + await _commentRepository.InsertAsync(new Comment(_guidGenerator.Create(), + _cmsKitTestData.EntityType1, + _cmsKitTestData.EntityId1, + "reply", + comment1.Id, + _cmsKitTestData.User1Id + )); + + await _commentRepository.InsertAsync(new Comment(_guidGenerator.Create(), + _cmsKitTestData.EntityType1, + _cmsKitTestData.EntityId1, + "comment", + null, + _cmsKitTestData.User2Id + )); + + await _commentRepository.InsertAsync(new Comment(_guidGenerator.Create(), + _cmsKitTestData.EntityType1, + _cmsKitTestData.EntityId2, + "comment", + null, + _cmsKitTestData.User2Id + )); - return Task.CompletedTask; + await _commentRepository.InsertAsync(new Comment(_guidGenerator.Create(), + _cmsKitTestData.EntityType2, + _cmsKitTestData.EntityId1, + "comment", + null, + _cmsKitTestData.User2Id + )); } } -} \ No newline at end of file +} diff --git a/modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitTestData.cs b/modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitTestData.cs new file mode 100644 index 0000000000..4e1472a3d8 --- /dev/null +++ b/modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitTestData.cs @@ -0,0 +1,22 @@ +using System; +using Volo.Abp.DependencyInjection; + +namespace Volo.CmsKit +{ + public class CmsKitTestData : ISingletonDependency + { + public Guid User1Id { get; } = Guid.NewGuid(); + + public Guid User2Id { get; } = Guid.NewGuid(); + + public Guid CommentWithChildId { get; } = Guid.NewGuid(); + + public string EntityType1 { get; } = "EntityName1"; + + public string EntityType2 { get; } = "EntityName2"; + + public string EntityId1 { get; } = "1"; + + public string EntityId2 { get; } = "2"; + } +} diff --git a/modules/cms-kit/test/Volo.CmsKit.TestBase/Comments/CommentRepository_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.TestBase/Comments/CommentRepository_Tests.cs new file mode 100644 index 0000000000..afb7652edf --- /dev/null +++ b/modules/cms-kit/test/Volo.CmsKit.TestBase/Comments/CommentRepository_Tests.cs @@ -0,0 +1,44 @@ +using System.Linq; +using System.Threading.Tasks; +using Shouldly; +using Volo.Abp.Modularity; +using Xunit; + +namespace Volo.CmsKit.Comments +{ + public abstract class CommentRepository_Tests : CmsKitTestBase + where TStartupModule : IAbpModule + { + private readonly CmsKitTestData _cmsKitTestData; + private readonly ICommentRepository _commentRepository; + + public CommentRepository_Tests() + { + _cmsKitTestData = GetRequiredService(); + _commentRepository = GetRequiredService(); + } + + [Fact] + public async Task GetListWithAuthorsAsync() + { + var list = await _commentRepository.GetListWithAuthorsAsync(_cmsKitTestData.EntityType1, + _cmsKitTestData.EntityId1); + + list.Count.ShouldBe(4); + list.Any(x=>x.Comment == null).ShouldBeFalse(); + list.Any(x=>x.Author == null).ShouldBeFalse(); + } + + [Fact] + public async Task DeleteAsync() + { + await _commentRepository.DeleteAsync(_cmsKitTestData.CommentWithChildId); + + var list = await _commentRepository.GetListAsync(); + + list.Any(x=> + x.Id == _cmsKitTestData.CommentWithChildId || x.RepliedCommentId == _cmsKitTestData.CommentWithChildId) + .ShouldBeFalse(); + } + } +} diff --git a/modules/cms-kit/test/Volo.CmsKit.TestBase/Samples/SampleRepository_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.TestBase/Samples/SampleRepository_Tests.cs deleted file mode 100644 index 2568c68810..0000000000 --- a/modules/cms-kit/test/Volo.CmsKit.TestBase/Samples/SampleRepository_Tests.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System.Threading.Tasks; -using Volo.Abp.Modularity; -using Xunit; - -namespace Volo.CmsKit.Samples -{ - /* Write your custom repository tests like that, in this project, as abstract classes. - * Then inherit these abstract classes from EF Core & MongoDB test projects. - * In this way, both database providers are tests with the same set tests. - */ - public abstract class SampleRepository_Tests : CmsKitTestBase - where TStartupModule : IAbpModule - { - //private readonly ISampleRepository _sampleRepository; - - protected SampleRepository_Tests() - { - //_sampleRepository = GetRequiredService(); - } - - [Fact] - public async Task Method1Async() - { - - } - } -}