1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using AutoFixture.AutoMoq;
using Emby.Naming.Common;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Controller.Resolvers;
using MediaBrowser.Controller.Sorting;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Globalization;
using Moq;
using Xunit;
namespace Jellyfin.Server.Implementations.Tests.Library.LibraryManager;
[Collection("LibraryManagerTests")]
public sealed class ParentalRatingScoreTests : IDisposable
{
private readonly Emby.Server.Implementations.Library.LibraryManager _libraryManager;
private readonly List<BaseItem> _saved = [];
private readonly ILibraryManager? _previousLibraryManager;
private readonly ILocalizationManager? _previousLocalizationManager;
public ParentalRatingScoreTests()
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());
fixture.Register(() => new NamingOptions());
fixture.Freeze<Mock<IServerConfigurationManager>>()
.Setup(c => c.ApplicationPaths.ProgramDataPath).Returns("/data");
fixture.Freeze<Mock<IItemPersistenceService>>()
.Setup(p => p.SaveItems(It.IsAny<IReadOnlyList<BaseItem>>(), It.IsAny<CancellationToken>()))
.Callback<IReadOnlyList<BaseItem>, CancellationToken>((items, _) => _saved.AddRange(items));
_libraryManager = fixture.Build<Emby.Server.Implementations.Library.LibraryManager>()
.Do(s => s.AddParts(
fixture.Create<IEnumerable<IResolverIgnoreRule>>(),
[],
fixture.Create<IEnumerable<IIntroProvider>>(),
fixture.Create<IEnumerable<IBaseItemComparer>>(),
fixture.Create<IEnumerable<ILibraryPostScanTask>>()))
.Create();
// BaseItem resolves these through process-wide statics; restored in Dispose.
_previousLibraryManager = BaseItem.LibraryManager;
_previousLocalizationManager = BaseItem.LocalizationManager;
BaseItem.LibraryManager = _libraryManager;
var localizationManagerMock = new Mock<ILocalizationManager>();
localizationManagerMock.Setup(l => l.GetRatingScore("G", It.IsAny<string>())).Returns(new ParentalRatingScore(0, null));
localizationManagerMock.Setup(l => l.GetRatingScore("XXX", It.IsAny<string>())).Returns(new ParentalRatingScore(1000, null));
BaseItem.LocalizationManager = localizationManagerMock.Object;
Video.RecordingsManager ??= fixture.Create<IRecordingsManager>();
}
public void Dispose()
{
BaseItem.LibraryManager = _previousLibraryManager!;
BaseItem.LocalizationManager = _previousLocalizationManager!;
}
private static Movie CreateRatedMovie() => new()
{
Id = Guid.NewGuid(),
Name = "Up",
PreferredMetadataCountryCode = "us",
OfficialRating = "G",
InheritedParentalRatingValue = 0
};
[Fact]
public async Task UpdateItemAsync_ChangedCustomRating_StoresItsScore()
{
var movie = CreateRatedMovie();
movie.CustomRating = "XXX";
await _libraryManager.UpdateItemAsync(movie, null!, ItemUpdateType.MetadataEdit, CancellationToken.None);
Assert.Equal(1000, Assert.Single(_saved).InheritedParentalRatingValue);
}
[Fact]
public async Task UpdateItemAsync_ClearedRatings_ClearsScore()
{
var movie = CreateRatedMovie();
movie.OfficialRating = null;
await _libraryManager.UpdateItemAsync(movie, null!, ItemUpdateType.MetadataEdit, CancellationToken.None);
Assert.Null(Assert.Single(_saved).InheritedParentalRatingValue);
}
[Fact]
public void CreateItem_RatedItem_StoresItsScore()
{
var movie = CreateRatedMovie();
movie.InheritedParentalRatingValue = null;
movie.CustomRating = "XXX";
_libraryManager.CreateItem(movie, null);
Assert.Equal(1000, _saved.Single().InheritedParentalRatingValue);
}
}
|