aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorPiotr Niełacny <piotr.nielacny@gmail.com>2026-03-10 16:53:12 +0100
committerPiotr Niełacny <piotr.nielacny@gmail.com>2026-07-29 10:41:41 +0200
commit9693f11fe5507a005fc7fcd0e30ef918ab1f214d (patch)
treead3eba15cc221d3e9d0fc531b4e82e2fc6b40244 /tests
parentafb716566d04102fa6c8e1b12977d175db301081 (diff)
Fix NameStartsWith filter not matching numeric item names
The NameStartsWith, NameStartsWithOrGreater, and NameLessThan API filters compare user input against the internal SortName, which zero-pads numbers to 10 digits for natural sorting. This caused numeric filters like NameStartsWith=1 to fail because "1" doesn't match the padded sort name "0000000012...". Normalize filter input through ModifySortChunks before comparison so that padding is applied consistently. For NameStartsWith, add a TrimStart('0') fallback so that numeric prefixes like "1" correctly match sort names such as "0000000012 years a slave". Add NormalizeSortNameFilter helper on BaseItem and make ModifySortChunks public. Add unit tests covering numeric, alphabetic, and mixed prefix matching for all three filter types. Fixes #1470
Diffstat (limited to 'tests')
-rw-r--r--tests/Jellyfin.Controller.Tests/Entities/UserViewBuilderFilterTests.cs107
1 files changed, 107 insertions, 0 deletions
diff --git a/tests/Jellyfin.Controller.Tests/Entities/UserViewBuilderFilterTests.cs b/tests/Jellyfin.Controller.Tests/Entities/UserViewBuilderFilterTests.cs
new file mode 100644
index 0000000000..b6701f769c
--- /dev/null
+++ b/tests/Jellyfin.Controller.Tests/Entities/UserViewBuilderFilterTests.cs
@@ -0,0 +1,107 @@
+using System.Linq;
+using MediaBrowser.Controller.Configuration;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Model.Configuration;
+using Moq;
+using Xunit;
+
+namespace Jellyfin.Controller.Tests.Entities;
+
+public class UserViewBuilderFilterTests
+{
+ public UserViewBuilderFilterTests()
+ {
+ var configManager = new Mock<IServerConfigurationManager>();
+ configManager.Setup(m => m.Configuration).Returns(new ServerConfiguration());
+ BaseItem.ConfigurationManager = configManager.Object;
+ }
+
+ private static Video CreateVideo(string name) => new Video { Name = name };
+
+ [Theory]
+ // Numeric names: NameStartsWith should match after stripping zero-padding
+ [InlineData("12 Years a Slave", "1", true)]
+ [InlineData("12 Years a Slave", "12", true)]
+ [InlineData("12 Years a Slave", "2", false)]
+ [InlineData("300", "3", true)]
+ [InlineData("300", "30", true)]
+ [InlineData("300", "0", false)]
+ // Alphabetic names: standard prefix match on SortName
+ [InlineData("Avatar", "A", true)]
+ [InlineData("Avatar", "a", true)]
+ [InlineData("Avatar", "B", false)]
+ [InlineData("The Matrix", "m", true)]
+ [InlineData("The Matrix", "t", false)]
+ // Mixed alpha-numeric input: handled via ModifySortChunks padding
+ [InlineData("Apollo 13", "apollo 13", true)]
+ [InlineData("Apollo 13", "apollo 2", false)]
+ // Edge case: "0" should only match items whose name is literally "0"
+ [InlineData("0", "0", true)]
+ [InlineData("1", "0", false)]
+ public void Filter_NameStartsWith_MatchesNumericAndAlphabeticPrefixes(string name, string nameStartsWith, bool expected)
+ {
+ var item = CreateVideo(name);
+
+ var query = new InternalItemsQuery
+ {
+ NameStartsWith = nameStartsWith
+ };
+
+ var result = UserViewBuilder.Filter(new[] { (BaseItem)item }, null!, query, null!, null!).Any();
+
+ Assert.Equal(expected, result);
+ }
+
+ [Fact]
+ public void Filter_NameStartsWith_NullOrEmpty_PassesThrough()
+ {
+ var items = new[] { (BaseItem)CreateVideo("Avatar") };
+
+ var queryNull = new InternalItemsQuery { NameStartsWith = null };
+ var queryEmpty = new InternalItemsQuery { NameStartsWith = string.Empty };
+
+ Assert.True(UserViewBuilder.Filter(items, null!, queryNull, null!, null!).Any());
+ Assert.True(UserViewBuilder.Filter(items, null!, queryEmpty, null!, null!).Any());
+ }
+
+ [Theory]
+ // Input is plain user value — padding is applied internally
+ [InlineData("12 Years a Slave", "12", true)]
+ [InlineData("12 Years a Slave", "13", false)]
+ [InlineData("Avatar", "a", true)]
+ [InlineData("Avatar", "avatar", true)]
+ [InlineData("Avatar", "b", false)]
+ public void Filter_NameStartsWithOrGreater_PadsInputBeforeComparing(string name, string nameStartsWithOrGreater, bool expected)
+ {
+ var item = CreateVideo(name);
+
+ var query = new InternalItemsQuery
+ {
+ NameStartsWithOrGreater = nameStartsWithOrGreater
+ };
+
+ var result = UserViewBuilder.Filter(new[] { (BaseItem)item }, null!, query, null!, null!).Any();
+
+ Assert.Equal(expected, result);
+ }
+
+ [Theory]
+ // Input is plain user value — padding is applied internally
+ [InlineData("12 Years a Slave", "13", true)]
+ [InlineData("12 Years a Slave", "12", false)]
+ [InlineData("Avatar", "b", true)]
+ [InlineData("Avatar", "a", false)]
+ public void Filter_NameLessThan_PadsInputBeforeComparing(string name, string nameLessThan, bool expected)
+ {
+ var item = CreateVideo(name);
+
+ var query = new InternalItemsQuery
+ {
+ NameLessThan = nameLessThan
+ };
+
+ var result = UserViewBuilder.Filter(new[] { (BaseItem)item }, null!, query, null!, null!).Any();
+
+ Assert.Equal(expected, result);
+ }
+}