Skip to content

Commit a6c8409

Browse files
committed
Added missing StringUtils. Also, resolved the errors in the InlineSvgTagHelperTests file by adding nulls in place of the newly added dependencies within the constructor.
1 parent 1222c65 commit a6c8409

File tree

2 files changed

+97
-10
lines changed

2 files changed

+97
-10
lines changed

Our.Umbraco.TagHelpers.Tests/InlineSvgTagHelperTests.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void Setup()
4141
[Test]
4242
public void NoOutputIfNoMediaOrFileSet()
4343
{
44-
var tagHelper = new InlineSvgTagHelper(null, null, null);
44+
var tagHelper = new InlineSvgTagHelper(null, null, null, null, null);
4545

4646
tagHelper.Process(_context, _output);
4747

@@ -52,7 +52,7 @@ public void NoOutputIfNoMediaOrFileSet()
5252
public void NoOutputIfBothMediaAndFileSet()
5353
{
5454
var umbContent = Mock.Of<IPublishedContent>(c => c.ContentType.ItemType == PublishedItemType.Media);
55-
var tagHelper = new InlineSvgTagHelper(null, null, null)
55+
var tagHelper = new InlineSvgTagHelper(null, null, null, null, null)
5656
{
5757
FileSource = "test.svg",
5858
MediaItem = umbContent
@@ -66,7 +66,7 @@ public void NoOutputIfBothMediaAndFileSet()
6666
[Test]
6767
public void NoOutputIfFileNotSvg()
6868
{
69-
var tagHelper = new InlineSvgTagHelper(null, null, null)
69+
var tagHelper = new InlineSvgTagHelper(null, null, null, null, null)
7070
{
7171
FileSource = "test.notsvg"
7272
};
@@ -82,7 +82,7 @@ public void NoOutputIfFileNotFound()
8282
var fileProvider = new Mock<IFileProvider>();
8383
fileProvider.Setup(p => p.GetFileInfo(It.IsAny<string>())).Returns(Mock.Of<IFileInfo>(f => !f.Exists));
8484
var hostEnv = Mock.Of<IWebHostEnvironment>(e => e.WebRootFileProvider == fileProvider.Object);
85-
var tagHelper = new InlineSvgTagHelper(null, hostEnv, null)
85+
var tagHelper = new InlineSvgTagHelper(null, hostEnv, null, null, null)
8686
{
8787
FileSource = "test.svg"
8888
};
@@ -98,7 +98,7 @@ public void ExpectedOutputIfValidFile()
9898
var fileProvider = new Mock<IFileProvider>();
9999
fileProvider.Setup(p => p.GetFileInfo(It.IsAny<string>())).Returns(Mock.Of<IFileInfo>(f => f.Exists && f.CreateReadStream() == new MemoryStream(Encoding.UTF8.GetBytes("test svg"))));
100100
var hostEnv = Mock.Of<IWebHostEnvironment>(e => e.WebRootFileProvider == fileProvider.Object);
101-
var tagHelper = new InlineSvgTagHelper(null, hostEnv, null)
101+
var tagHelper = new InlineSvgTagHelper(null, hostEnv, null, null, null)
102102
{
103103
FileSource = "test.svg"
104104
};
@@ -116,7 +116,7 @@ public void NoOutputIfMediaUrlNull()
116116
{
117117
var urlProvider = new Mock<IPublishedUrlProvider>();
118118
urlProvider.Setup(p => p.GetMediaUrl(It.IsAny<IPublishedContent>(), It.IsAny<UrlMode>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Uri>())).Returns((string)null!);
119-
var tagHelper = new InlineSvgTagHelper(null, null, urlProvider.Object)
119+
var tagHelper = new InlineSvgTagHelper(null, null, urlProvider.Object, null, null)
120120
{
121121
MediaItem = Mock.Of<IPublishedContent>(c => c.ContentType.ItemType == PublishedItemType.Media)
122122
};
@@ -132,7 +132,7 @@ public void NoOutputIfMediaNotSvg()
132132
var umbContent = Mock.Of<IPublishedContent>(c => c.ContentType.ItemType == PublishedItemType.Media);
133133
var urlProvider = new Mock<IPublishedUrlProvider>();
134134
urlProvider.Setup(p => p.GetMediaUrl(umbContent, It.IsAny<UrlMode>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Uri>())).Returns("test.notsvg");
135-
var tagHelper = new InlineSvgTagHelper(null, null, urlProvider.Object)
135+
var tagHelper = new InlineSvgTagHelper(null, null, urlProvider.Object, null, null)
136136
{
137137
MediaItem = umbContent
138138
};
@@ -152,7 +152,9 @@ public void NoOutputIfMediaNotFound()
152152
var tagHelper = new InlineSvgTagHelper(
153153
new MediaFileManager(fileSystem, null, null, null, null, Mock.Of<IOptions<ContentSettings>>()),
154154
null,
155-
urlProvider.Object)
155+
urlProvider.Object,
156+
null,
157+
null)
156158
{
157159
MediaItem = umbContent
158160
};
@@ -172,7 +174,9 @@ public void ExpectedOutputIfValidMedia()
172174
var tagHelper = new InlineSvgTagHelper(
173175
new MediaFileManager(fileSystem, null, null, null, null, Mock.Of<IOptions<ContentSettings>>()),
174176
null,
175-
urlProvider.Object)
177+
urlProvider.Object,
178+
null,
179+
null)
176180
{
177181
MediaItem = umbContent
178182
};
@@ -193,7 +197,7 @@ public void SanitizesJavascript()
193197
.Setup(p => p.GetFileInfo(It.IsAny<string>()))
194198
.Returns(Mock.Of<IFileInfo>(f => f.Exists && f.CreateReadStream() == new MemoryStream(Encoding.UTF8.GetBytes("<a xlink:href=\"javascript:alert('test');\">Click here</a><script attr=\"test\">test</script>end"))));
195199
var hostEnv = Mock.Of<IWebHostEnvironment>(e => e.WebRootFileProvider == fileProvider.Object);
196-
var tagHelper = new InlineSvgTagHelper(null, hostEnv, null)
200+
var tagHelper = new InlineSvgTagHelper(null, hostEnv, null, null, null)
197201
{
198202
FileSource = "test.svg"
199203
};
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Our.Umbraco.TagHelpers.Utils
8+
{
9+
internal static class StringUtils
10+
{
11+
internal static double GetDouble(object value, double defaultValue = 0)
12+
{
13+
double num;
14+
if (!IsDouble(value))
15+
{
16+
return defaultValue;
17+
}
18+
try
19+
{
20+
num = Convert.ToDouble(value);
21+
}
22+
catch
23+
{
24+
num = defaultValue;
25+
}
26+
return num;
27+
}
28+
29+
internal static decimal GetDecimal(object input, decimal defaultValue = 0)
30+
{
31+
decimal value = decimal.MinValue;
32+
decimal.TryParse(input != null ? input.ToString().Replace("£", "") : "", out value);
33+
34+
if (value > decimal.MinValue)
35+
{
36+
return value;
37+
}
38+
39+
return defaultValue;
40+
}
41+
42+
internal static int GetInteger(object input, int defaultValue = 0)
43+
{
44+
var value = 0;
45+
int.TryParse(input != null ? input.ToString() : "", out value);
46+
47+
if (value > 0)
48+
{
49+
return value;
50+
}
51+
52+
return defaultValue;
53+
}
54+
55+
internal static bool IsDouble(object value)
56+
{
57+
double num;
58+
if (IsNull(value))
59+
{
60+
return false;
61+
}
62+
if (value is double)
63+
{
64+
return true;
65+
}
66+
string str = Convert.ToString(value);
67+
if (double.TryParse(str, out num))
68+
{
69+
return true;
70+
}
71+
return false;
72+
}
73+
74+
internal static bool IsNull(object value)
75+
{
76+
if (value == null)
77+
{
78+
return true;
79+
}
80+
return value == DBNull.Value;
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)