Skip to content

Commit d167a60

Browse files
committed
Correct handling of step values of zero. (#18532)
1 parent b650e79 commit d167a60

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-20
lines changed

src/Umbraco.Core/PropertyEditors/Validation/ValidationHelper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public static bool IsValueValidForStep(decimal value, decimal min, decimal step)
1919
return true; // Outside of the range, so we expect another validator will have picked this up.
2020
}
2121

22+
if (step == 0)
23+
{
24+
return true; // A step of zero would trigger a divide by zero error in evaluating. So we always pass validation for zero, as effectively any step value is valid.
25+
}
26+
2227
return (value - min) % step == 0;
2328
}
2429
}

tests/Umbraco.Tests.UnitTests/Umbraco.Core/PropertyEditors/DecimalValueEditorTests.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,12 @@ public void Validates_Is_Less_Than_Or_Equal_To_Configured_Max(object value, bool
132132
}
133133
}
134134

135-
[TestCase(1.4, false)]
136-
[TestCase(1.5, true)]
137-
public void Validates_Matches_Configured_Step(object value, bool expectedSuccess)
135+
[TestCase(0.2, 1.4, false)]
136+
[TestCase(0.2, 1.5, true)]
137+
[TestCase(0.0, 1.4, true)] // A step of zero would trigger a divide by zero error in evaluating. So we always pass validation for zero, as effectively any step value is valid.
138+
public void Validates_Matches_Configured_Step(double step, object value, bool expectedSuccess)
138139
{
139-
var editor = CreateValueEditor();
140+
var editor = CreateValueEditor(step: step);
140141
var result = editor.Validate(value, false, null, PropertyValidationContext.Empty());
141142
if (expectedSuccess)
142143
{
@@ -164,7 +165,7 @@ public void Validates_Matches_Configured_Step(object value, bool expectedSuccess
164165
return CreateValueEditor().ToEditor(property.Object);
165166
}
166167

167-
private static DecimalPropertyEditor.DecimalPropertyValueEditor CreateValueEditor()
168+
private static DecimalPropertyEditor.DecimalPropertyValueEditor CreateValueEditor(double step = 0.2)
168169
{
169170
var localizedTextServiceMock = new Mock<ILocalizedTextService>();
170171
localizedTextServiceMock.Setup(x => x.Localize(
@@ -184,7 +185,7 @@ private static DecimalPropertyEditor.DecimalPropertyValueEditor CreateValueEdito
184185
{
185186
{ "min", 1.1 },
186187
{ "max", 1.9 },
187-
{ "step", 0.2 }
188+
{ "step", step }
188189
}
189190
};
190191
}

tests/Umbraco.Tests.UnitTests/Umbraco.Core/PropertyEditors/IntegerValueEditorTests.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,12 @@ public void Validates_Is_Less_Than_Or_Equal_To_Configured_Max(object value, bool
132132
}
133133
}
134134

135-
[TestCase(17, false)]
136-
[TestCase(18, true)]
137-
public void Validates_Matches_Configured_Step(object value, bool expectedSuccess)
135+
[TestCase(2, 17, false)]
136+
[TestCase(2, 18, true)]
137+
[TestCase(0, 17, true)] // A step of zero would trigger a divide by zero error in evaluating. So we always pass validation for zero, as effectively any step value is valid.
138+
public void Validates_Matches_Configured_Step(int step, object value, bool expectedSuccess)
138139
{
139-
var editor = CreateValueEditor();
140+
var editor = CreateValueEditor(step: step);
140141
var result = editor.Validate(value, false, null, PropertyValidationContext.Empty());
141142
if (expectedSuccess)
142143
{
@@ -164,7 +165,7 @@ public void Validates_Matches_Configured_Step(object value, bool expectedSuccess
164165
return CreateValueEditor().ToEditor(property.Object);
165166
}
166167

167-
private static IntegerPropertyEditor.IntegerPropertyValueEditor CreateValueEditor()
168+
private static IntegerPropertyEditor.IntegerPropertyValueEditor CreateValueEditor(int step = 2)
168169
{
169170
var localizedTextServiceMock = new Mock<ILocalizedTextService>();
170171
localizedTextServiceMock.Setup(x => x.Localize(
@@ -184,7 +185,7 @@ private static IntegerPropertyEditor.IntegerPropertyValueEditor CreateValueEdito
184185
{
185186
{ "min", 10 },
186187
{ "max", 20 },
187-
{ "step", 2 }
188+
{ "step", step }
188189
}
189190
};
190191
}

tests/Umbraco.Tests.UnitTests/Umbraco.Core/PropertyEditors/SliderValueEditorTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Umbraco.Cms.Core.Models.Editors;
99
using Umbraco.Cms.Core.Models.Validation;
1010
using Umbraco.Cms.Core.PropertyEditors;
11-
using Umbraco.Cms.Core.Serialization;
1211
using Umbraco.Cms.Core.Services;
1312
using Umbraco.Cms.Core.Strings;
1413
using Umbraco.Cms.Infrastructure.Serialization;
@@ -206,17 +205,18 @@ public void Validates_Is_Less_Than_Or_Equal_To_Configured_Max(decimal from, deci
206205
}
207206
}
208207

209-
[TestCase(1.3, 1.7, true)]
210-
[TestCase(1.4, 1.7, false)]
211-
[TestCase(1.3, 1.6, false)]
212-
public void Validates_Matches_Configured_Step(decimal from, decimal to, bool expectedSuccess)
208+
[TestCase(0.2, 1.3, 1.7, true)]
209+
[TestCase(0.2, 1.4, 1.7, false)]
210+
[TestCase(0.2, 1.3, 1.6, false)]
211+
[TestCase(0.0, 1.4, 1.7, true)] // A step of zero would trigger a divide by zero error in evaluating. So we always pass validation for zero, as effectively any step value is valid.
212+
public void Validates_Matches_Configured_Step(decimal step, decimal from, decimal to, bool expectedSuccess)
213213
{
214214
var value = new JsonObject
215215
{
216216
{ "from", from },
217217
{ "to", to },
218218
};
219-
var editor = CreateValueEditor();
219+
var editor = CreateValueEditor(step: step);
220220
var result = editor.Validate(value, false, null, PropertyValidationContext.Empty());
221221
if (expectedSuccess)
222222
{
@@ -244,7 +244,7 @@ public void Validates_Matches_Configured_Step(decimal from, decimal to, bool exp
244244
return CreateValueEditor().ToEditor(property.Object);
245245
}
246246

247-
private static SliderPropertyEditor.SliderPropertyValueEditor CreateValueEditor(bool enableRange = true)
247+
private static SliderPropertyEditor.SliderPropertyValueEditor CreateValueEditor(bool enableRange = true, decimal step = 0.2m)
248248
{
249249
var localizedTextServiceMock = new Mock<ILocalizedTextService>();
250250
localizedTextServiceMock.Setup(x => x.Localize(
@@ -265,7 +265,7 @@ private static SliderPropertyEditor.SliderPropertyValueEditor CreateValueEditor(
265265
EnableRange = enableRange,
266266
MinimumValue = 1.1m,
267267
MaximumValue = 1.9m,
268-
Step = 0.2m
268+
Step = step
269269
},
270270
};
271271
}

0 commit comments

Comments
 (0)