Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent LBP3 reviews from showing up in LBP2 #1015

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ public async Task<IActionResult> ReviewsFor(int slotId)

List<GameReview> reviews = (await this.database.Reviews
.Where(r => r.SlotId == slotId)
.Select(r => new
{
Review = r,
SlotVersion = r.Slot!.GameVersion,
})
.Where(a => a.SlotVersion <= token.GameVersion)
.Select(a => a.Review)
.OrderByDescending(r => r.ThumbsUp - r.ThumbsDown)
.ThenByDescending(r => r.Timestamp)
.ApplyPagination(pageData)
Expand All @@ -178,6 +185,13 @@ public async Task<IActionResult> ReviewsBy(string username)

List<GameReview> reviews = (await this.database.Reviews
.Where(r => r.ReviewerId == targetUserId)
.Select(r => new
{
Review = r,
SlotVersion = r.Slot!.GameVersion,
})
.Where(a => a.SlotVersion <= token.GameVersion)
.Select(a => a.Review)
.OrderByDescending(r => r.Timestamp)
.ApplyPagination(pageData)
.ToListAsync()).ToSerializableList(r => GameReview.CreateFromEntity(r, token));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Servers.GameServer.Controllers.Slots;
using LBPUnion.ProjectLighthouse.Tests.Helpers;
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
using LBPUnion.ProjectLighthouse.Types.Serialization;
using LBPUnion.ProjectLighthouse.Types.Users;
using Microsoft.AspNetCore.Mvc;
using Xunit;

namespace ProjectLighthouse.Tests.GameApiTests.Unit.Controllers;

public class ReviewControllerTests
{
private static async Task InsertTestData(DatabaseContext database)
{
database.Slots.Add(new SlotEntity
{
SlotId = 1,
CreatorId = 1,
GameVersion = GameVersion.LittleBigPlanet3,
});

database.Slots.Add(new SlotEntity
{
SlotId = 2,
CreatorId = 1,
GameVersion = GameVersion.LittleBigPlanet2,
});

database.Reviews.Add(new ReviewEntity
{
ReviewId = 1,
ReviewerId = 1,
SlotId = 1,
});

database.Reviews.Add(new ReviewEntity
{
ReviewId = 2,
ReviewerId = 1,
SlotId = 2,
});
await database.SaveChangesAsync();
}

[Theory]
[InlineData(GameVersion.LittleBigPlanet2, 1)]
[InlineData(GameVersion.LittleBigPlanet3, 2)]
public async Task ReviewsBy_ShouldNotList_HigherGameVersions(GameVersion version, int expected)
{
GameTokenEntity token = MockHelper.GetUnitTestToken();
token.GameVersion = version;
DatabaseContext database = await MockHelper.GetTestDatabase(new List<GameTokenEntity>
{
token,
});

await InsertTestData(database);

ReviewController controller = new(database);
controller.SetupTestController(token);

IActionResult response = await controller.ReviewsBy("unittest");
ReviewResponse review = response.CastTo<OkObjectResult, ReviewResponse>();

Assert.Equal(expected, review.Reviews.Count);
Assert.True(review.Reviews.All(r => database.Slots.FirstOrDefault(s => s.SlotId == r.Slot.SlotId)?.GameVersion <= version));
}

[Theory]
[InlineData(GameVersion.LittleBigPlanet2, 2, 1)]
[InlineData(GameVersion.LittleBigPlanet2, 1, 0)]
[InlineData(GameVersion.LittleBigPlanet3, 2, 1)]
[InlineData(GameVersion.LittleBigPlanet3, 1, 1)]
public async Task ReviewsFor_ShouldNotList_HigherGameVersions(GameVersion version, int slotId, int expected)
{
GameTokenEntity token = MockHelper.GetUnitTestToken();
token.GameVersion = version;
DatabaseContext database = await MockHelper.GetTestDatabase(new List<GameTokenEntity>
{
token,
});

await InsertTestData(database);

ReviewController controller = new(database);
controller.SetupTestController(token);

IActionResult response = await controller.ReviewsFor(slotId);
ReviewResponse review = response.CastTo<OkObjectResult, ReviewResponse>();

Assert.Equal(expected, review.Reviews.Count);
Assert.True(review.Reviews.All(r => database.Slots.FirstOrDefault(s => s.SlotId == r.Slot.SlotId)?.GameVersion <= version));
}
}
Loading