diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Reactions/ReactionManager.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Reactions/ReactionManager.cs index 8410f10fcf..894ed10a47 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Reactions/ReactionManager.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Reactions/ReactionManager.cs @@ -74,7 +74,7 @@ namespace Volo.CmsKit.Reactions entityId, reactionName, creatorId, - CurrentTenant.GetId() + CurrentTenant.Id ) ); } 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 9e197a29ee..895d8c5948 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 @@ -31,7 +31,7 @@ namespace Volo.CmsKit.Public.Comments CmsUserLookupService = cmsUserLookupService; } - public async Task> GetAllForEntityAsync(string entityType, string entityId) + public virtual async Task> GetAllForEntityAsync(string entityType, string entityId) { CheckAuthorizationAsync(entityType); @@ -43,7 +43,7 @@ namespace Volo.CmsKit.Public.Comments } [Authorize] - public async Task CreateAsync(CreateCommentInput input) + public virtual async Task CreateAsync(CreateCommentInput input) { var user = await CmsUserLookupService.FindByIdAsync(CurrentUser.GetId()); @@ -66,7 +66,7 @@ namespace Volo.CmsKit.Public.Comments } [Authorize] - public async Task UpdateAsync(Guid id, UpdateCommentInput input) + public virtual async Task UpdateAsync(Guid id, UpdateCommentInput input) { var comment = await CommentRepository.GetAsync(id); @@ -83,7 +83,7 @@ namespace Volo.CmsKit.Public.Comments } [Authorize] - public async Task DeleteAsync(Guid id) + public virtual async Task DeleteAsync(Guid id) { var comment = await CommentRepository.GetAsync(id); 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 125629526d..d07fe0d1d1 100644 --- a/modules/cms-kit/test/Volo.CmsKit.Application.Tests/CmsKitApplicationTestBase.cs +++ b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/CmsKitApplicationTestBase.cs @@ -8,7 +8,6 @@ namespace Volo.CmsKit */ public abstract class CmsKitApplicationTestBase : CmsKitTestBase { - protected virtual void UsingDbContext(Action action) { using (var dbContext = GetRequiredService()) @@ -24,6 +23,5 @@ namespace Volo.CmsKit return action.Invoke(dbContext); } } - } } diff --git a/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Reactions/ReactionPublicAppService_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Reactions/ReactionPublicAppService_Tests.cs new file mode 100644 index 0000000000..d03712ea07 --- /dev/null +++ b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Reactions/ReactionPublicAppService_Tests.cs @@ -0,0 +1,105 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using NSubstitute; +using Shouldly; +using Volo.Abp.Users; +using Volo.CmsKit.Public.Reactions; +using Xunit; + +namespace Volo.CmsKit.Reactions +{ + public class ReactionPublicAppService_Tests : CmsKitApplicationTestBase + { + + private readonly CmsKitTestData _cmsKitTestData; + private readonly ReactionPublicAppService _reactionPublicAppService; + private ICurrentUser _currentUser; + + public ReactionPublicAppService_Tests() + { + _cmsKitTestData = GetRequiredService(); + _reactionPublicAppService = GetRequiredService(); + } + + protected override void AfterAddApplication(IServiceCollection services) + { + _currentUser = Substitute.For(); + services.AddSingleton(_currentUser); + } + + [Fact] + public async Task GetForSelectionAsync() + { + _currentUser.Id.Returns(_cmsKitTestData.User1Id); + + var reactions = await _reactionPublicAppService.GetForSelectionAsync( + _cmsKitTestData.EntityType2, + _cmsKitTestData.EntityId1 + ); + + reactions.Items. + First(r=>r.Reaction.Name == StandardReactions.Rocket).IsSelectedByCurrentUser.ShouldBeTrue(); + reactions.Items. + First(r=>r.Reaction.Name == StandardReactions.Rocket).Count.ShouldBe(1); + + reactions.Items. + Where(r=>r.Reaction.Name != StandardReactions.Rocket).All(r=>!r.IsSelectedByCurrentUser) + .ShouldBeTrue(); + reactions.Items. + Where(r=>r.Reaction.Name != StandardReactions.Rocket).All(r=> r.Count == 0) + .ShouldBeTrue(); + } + + [Fact] + public async Task CreateAsync() + { + _currentUser.Id.Returns(_cmsKitTestData.User1Id); + + await _reactionPublicAppService.CreateAsync(new CreateReactionDto + { + EntityType = _cmsKitTestData.EntityType2, + EntityId = _cmsKitTestData.EntityId2, + ReactionName = StandardReactions.Hooray + }); + + UsingDbContext(context => + { + var reaction = context.UserReactions.FirstOrDefault(x => + x.CreatorId == _cmsKitTestData.User1Id && + x.ReactionName == StandardReactions.Hooray && + x.EntityId == _cmsKitTestData.EntityId2 && + x.EntityType == _cmsKitTestData.EntityType2); + + reaction.ShouldNotBeNull(); + }); + } + + [Fact] + public async Task DeleteAsync() + { + _currentUser.Id.Returns(_cmsKitTestData.User1Id); + + await _reactionPublicAppService.DeleteAsync(new DeleteReactionDto + { + EntityType = _cmsKitTestData.EntityType1, + EntityId = _cmsKitTestData.EntityId1, + ReactionName = StandardReactions.Confused + }); + + UsingDbContext(context => + { + var reaction = context.UserReactions.FirstOrDefault(x => + x.CreatorId == _cmsKitTestData.User1Id && + x.ReactionName == StandardReactions.Confused && + x.EntityId == _cmsKitTestData.EntityId1 && + x.EntityType == _cmsKitTestData.EntityType1); + + reaction.ShouldBeNull(); + }); + } + } +} diff --git a/modules/cms-kit/test/Volo.CmsKit.Domain.Tests/CmsKitDomainTestBase.cs b/modules/cms-kit/test/Volo.CmsKit.Domain.Tests/CmsKitDomainTestBase.cs index d2054cd623..763942e5ac 100644 --- a/modules/cms-kit/test/Volo.CmsKit.Domain.Tests/CmsKitDomainTestBase.cs +++ b/modules/cms-kit/test/Volo.CmsKit.Domain.Tests/CmsKitDomainTestBase.cs @@ -1,10 +1,27 @@ -namespace Volo.CmsKit +using System; +using Volo.CmsKit.EntityFrameworkCore; + +namespace Volo.CmsKit { /* Inherit from this class for your domain layer tests. * See SampleManager_Tests for example. */ public abstract class CmsKitDomainTestBase : 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.Domain.Tests/Reactions/ReactionManager_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Reactions/ReactionManager_Tests.cs new file mode 100644 index 0000000000..6f4fe2425d --- /dev/null +++ b/modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Reactions/ReactionManager_Tests.cs @@ -0,0 +1,82 @@ +using System.Linq; +using System.Threading.Tasks; +using Shouldly; +using Xunit; + +namespace Volo.CmsKit.Reactions +{ + public class ReactionManager_Tests : CmsKitDomainTestBase + { + private readonly CmsKitTestData _cmsKitTestData; + private readonly ReactionManager _reactionManager; + + public ReactionManager_Tests() + { + _cmsKitTestData = GetRequiredService(); + _reactionManager = GetRequiredService(); + } + + [Fact] + public async Task GetReactionsAsync() + { + var reactions = await _reactionManager.GetReactionsAsync(); + + reactions.Count.ShouldBe(8); + + var reactionsByEntityType = await _reactionManager.GetReactionsAsync(_cmsKitTestData.EntityType1); + + reactionsByEntityType.Count.ShouldBe(8); + } + + [Fact] + public async Task GetSummariesAsync() + { + var summary = await _reactionManager.GetSummariesAsync( + _cmsKitTestData.EntityType1, + _cmsKitTestData.EntityId1 + ); + + summary.Single(s=>s.Reaction.Name == StandardReactions.ThumbsUp).Count.ShouldBe(2); + summary.Single(s=>s.Reaction.Name == StandardReactions.Confused).Count.ShouldBe(1); + summary.Single(s=>s.Reaction.Name == StandardReactions.Rocket).Count.ShouldBe(0); + } + + [Fact] + public async Task CreateAsync() + { + var reaction = await _reactionManager.CreateAsync( + _cmsKitTestData.User2Id, + _cmsKitTestData.EntityType1, + _cmsKitTestData.EntityId2, + StandardReactions.Eyes + ); + + reaction.CreatorId.ShouldBe(_cmsKitTestData.User2Id); + reaction.EntityType.ShouldBe(_cmsKitTestData.EntityType1); + reaction.EntityId.ShouldBe(_cmsKitTestData.EntityId2); + reaction.ReactionName.ShouldBe(StandardReactions.Eyes); + } + + [Fact] + public async Task DeleteAsync() + { + await _reactionManager.DeleteAsync( + _cmsKitTestData.User1Id, + _cmsKitTestData.EntityType1, + _cmsKitTestData.EntityId1, + StandardReactions.Confused + ); + + UsingDbContext(context => + { + var reaction = context.UserReactions.FirstOrDefault(x => + x.CreatorId == _cmsKitTestData.User1Id && + x.ReactionName == StandardReactions.Confused && + x.EntityId == _cmsKitTestData.EntityId1 && + x.EntityType == _cmsKitTestData.EntityType1); + + reaction.ShouldBeNull(); + }); + } + } +} diff --git a/modules/cms-kit/test/Volo.CmsKit.EntityFrameworkCore.Tests/EntityFrameworkCore/Reactions/UserReactionRepository_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.EntityFrameworkCore.Tests/EntityFrameworkCore/Reactions/UserReactionRepository_Tests.cs new file mode 100644 index 0000000000..8bb4c68c8e --- /dev/null +++ b/modules/cms-kit/test/Volo.CmsKit.EntityFrameworkCore.Tests/EntityFrameworkCore/Reactions/UserReactionRepository_Tests.cs @@ -0,0 +1,8 @@ +using Volo.CmsKit.Reactions; + +namespace Volo.CmsKit.EntityFrameworkCore.Reactions +{ + public class UserReactionRepository_Tests : UserReactionRepository_Tests + { + } +} diff --git a/modules/cms-kit/test/Volo.CmsKit.MongoDB.Tests/MongoDB/Reactions/UserReactionRepository_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.MongoDB.Tests/MongoDB/Reactions/UserReactionRepository_Tests.cs new file mode 100644 index 0000000000..452ee211c5 --- /dev/null +++ b/modules/cms-kit/test/Volo.CmsKit.MongoDB.Tests/MongoDB/Reactions/UserReactionRepository_Tests.cs @@ -0,0 +1,8 @@ +using Volo.CmsKit.Reactions; + +namespace Volo.CmsKit.MongoDB.Reactions +{ + public class UserReactionRepository_Tests : UserReactionRepository_Tests + { + } +} diff --git a/modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs b/modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs index 9b2d5ddb5c..12c6255623 100644 --- a/modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs +++ b/modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs @@ -14,28 +14,42 @@ namespace Volo.CmsKit private readonly IGuidGenerator _guidGenerator; private readonly ICmsUserRepository _cmsUserRepository; private readonly CmsKitTestData _cmsKitTestData; - private readonly IUserReactionRepository _userReactionRepository; private readonly ICommentRepository _commentRepository; + private readonly ReactionManager _reactionManager; public CmsKitDataSeedContributor( IGuidGenerator guidGenerator, ICmsUserRepository cmsUserRepository, CmsKitTestData cmsKitTestData, - IUserReactionRepository userReactionRepository, - ICommentRepository commentRepository) + ICommentRepository commentRepository, + ReactionManager reactionManager) { _guidGenerator = guidGenerator; _cmsUserRepository = cmsUserRepository; _cmsKitTestData = cmsKitTestData; - _userReactionRepository = userReactionRepository; _commentRepository = commentRepository; + _reactionManager = reactionManager; } public async Task SeedAsync(DataSeedContext context) { - 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"))); + await SeedUsersAsync(); + await SeedCommentsAsync(); + + await SeedReactionsAsync(); + } + + private async Task SeedUsersAsync() + { + 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"))); + } + + private async Task SeedCommentsAsync() + { var comment1 = await _commentRepository.InsertAsync(new Comment(_cmsKitTestData.CommentWithChildId, _cmsKitTestData.EntityType1, _cmsKitTestData.EntityId1, @@ -84,5 +98,38 @@ namespace Volo.CmsKit _cmsKitTestData.User2Id )); } + + private async Task SeedReactionsAsync() + { + await _reactionManager.CreateAsync( + _cmsKitTestData.User1Id, + _cmsKitTestData.EntityType1, + _cmsKitTestData.EntityId1, + StandardReactions.Confused); + + await _reactionManager.CreateAsync( + _cmsKitTestData.User1Id, + _cmsKitTestData.EntityType1, + _cmsKitTestData.EntityId1, + StandardReactions.ThumbsUp); + + await _reactionManager.CreateAsync( + _cmsKitTestData.User1Id, + _cmsKitTestData.EntityType1, + _cmsKitTestData.EntityId2, + StandardReactions.Heart); + + await _reactionManager.CreateAsync( + _cmsKitTestData.User1Id, + _cmsKitTestData.EntityType2, + _cmsKitTestData.EntityId1, + StandardReactions.Rocket); + + await _reactionManager.CreateAsync( + _cmsKitTestData.User2Id, + _cmsKitTestData.EntityType1, + _cmsKitTestData.EntityId1, + StandardReactions.ThumbsUp); + } } } diff --git a/modules/cms-kit/test/Volo.CmsKit.TestBase/Reactions/UserReactionRepository_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.TestBase/Reactions/UserReactionRepository_Tests.cs new file mode 100644 index 0000000000..0499bc264d --- /dev/null +++ b/modules/cms-kit/test/Volo.CmsKit.TestBase/Reactions/UserReactionRepository_Tests.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Shouldly; +using Volo.Abp.Modularity; +using Volo.CmsKit.Comments; +using Xunit; + +namespace Volo.CmsKit.Reactions +{ + public abstract class UserReactionRepository_Tests : CmsKitTestBase + where TStartupModule : IAbpModule + { + private readonly CmsKitTestData _cmsKitTestData; + private readonly IUserReactionRepository __userReactionRepository; + + public UserReactionRepository_Tests() + { + _cmsKitTestData = GetRequiredService(); + __userReactionRepository = GetRequiredService(); + } + + [Fact] + public async Task FindAsync() + { + var reaction = await __userReactionRepository.FindAsync( + _cmsKitTestData.User1Id, + _cmsKitTestData.EntityType1, + _cmsKitTestData.EntityId1, + StandardReactions.Confused + ); + + reaction.ShouldNotBeNull(); + reaction.CreatorId.ShouldBe(_cmsKitTestData.User1Id); + reaction.EntityId.ShouldBe(_cmsKitTestData.EntityId1); + reaction.EntityType.ShouldBe(_cmsKitTestData.EntityType1); + reaction.ReactionName.ShouldBe(StandardReactions.Confused); + } + + [Fact] + public async Task GetListForUserAsync() + { + var reactions = await __userReactionRepository.GetListForUserAsync( + _cmsKitTestData.User1Id, + _cmsKitTestData.EntityType1, + _cmsKitTestData.EntityId1 + ); + + reactions.Count.ShouldBe(2); + reactions.Count(x=>x.ReactionName == StandardReactions.Confused).ShouldBe(1); + reactions.Count(x=>x.ReactionName == StandardReactions.ThumbsUp).ShouldBe(1); + } + + [Fact] + public async Task GetSummariesAsync() + { + var summaries = await __userReactionRepository.GetSummariesAsync( + _cmsKitTestData.EntityType1, + _cmsKitTestData.EntityId1 + ); + + summaries.Count.ShouldBe(2); + summaries.First(x => x.ReactionName == StandardReactions.Confused).Count.ShouldBe(1); + summaries.First(x => x.ReactionName == StandardReactions.ThumbsUp).Count.ShouldBe(2); + + } + } +}