Move test projects from src/ to tests/ (#11357)
* Update gitignore * Move csproj * Update project references * Update solutions * Update build scripts * Tests used to share editorconfig with projects in src * Fix broken tests. * Stop copying around .editorconfig merged root one with linting * csharp_style_expression_bodied -> suggestion * Move StyleCop rulesets to matching directories and update shared build properties * Remove legacy build files, update NuGet.cofig and solution files * Restore myget source * Clean up .gitignore * Update .gitignore * Move new test classes to tests after merge * Gitignore + nuget config * Move new test Co-authored-by: Ronald Barendse <ronald@barend.se>
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
// Copyright (c) Umbraco.
|
||||
// See LICENSE for more details.
|
||||
|
||||
using System.Text.Encodings.Web;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using Microsoft.AspNetCore.Mvc.ViewEngines;
|
||||
using Microsoft.AspNetCore.Mvc.ViewFeatures;
|
||||
using Microsoft.AspNetCore.Mvc.ViewFeatures.Buffers;
|
||||
using Microsoft.Extensions.WebEncoders.Testing;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Extensions;
|
||||
|
||||
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common.Extensions
|
||||
{
|
||||
[TestFixture]
|
||||
public class HtmlHelperExtensionMethodsTests
|
||||
{
|
||||
private const string SampleWithAnchorElement = "Hello world, this is some text <a href='blah'>with a link</a>";
|
||||
private const string SampleWithBoldAndAnchorElements = "Hello world, <b>this</b> is some text <a href='blah'>with a link</a>";
|
||||
|
||||
[SetUp]
|
||||
public virtual void Initialize() =>
|
||||
|
||||
// Create an empty HtmlHelper.
|
||||
_htmlHelper = new HtmlHelper(
|
||||
Mock.Of<IHtmlGenerator>(),
|
||||
Mock.Of<ICompositeViewEngine>(),
|
||||
Mock.Of<IModelMetadataProvider>(),
|
||||
Mock.Of<IViewBufferScope>(),
|
||||
new HtmlTestEncoder(),
|
||||
UrlEncoder.Default);
|
||||
|
||||
private HtmlHelper _htmlHelper;
|
||||
|
||||
[Test]
|
||||
public void Truncate_Simple()
|
||||
{
|
||||
var result = _htmlHelper.Truncate(SampleWithAnchorElement, 25).ToString();
|
||||
|
||||
Assert.AreEqual("Hello world, this is some…", result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void When_Truncating_A_String_Ends_With_A_Space_We_Should_Trim_The_Space_Before_Appending_The_Ellipsis()
|
||||
{
|
||||
var result = _htmlHelper.Truncate(SampleWithAnchorElement, 26).ToString();
|
||||
|
||||
Assert.AreEqual("Hello world, this is some…", result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Truncate_Inside_Word()
|
||||
{
|
||||
var result = _htmlHelper.Truncate(SampleWithAnchorElement, 24).ToString();
|
||||
|
||||
Assert.AreEqual("Hello world, this is som…", result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Truncate_With_Tag()
|
||||
{
|
||||
var result = _htmlHelper.Truncate(SampleWithAnchorElement, 35).ToString();
|
||||
|
||||
Assert.AreEqual("Hello world, this is some text <a href='blah'>with…</a>", result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Truncate_By_Words()
|
||||
{
|
||||
var result = _htmlHelper.TruncateByWords(SampleWithAnchorElement, 4).ToString();
|
||||
|
||||
Assert.AreEqual("Hello world, this is…", result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Truncate_By_Words_With_Tag()
|
||||
{
|
||||
var result = _htmlHelper.TruncateByWords(SampleWithBoldAndAnchorElements, 4).ToString();
|
||||
|
||||
Assert.AreEqual("Hello world, <b>this</b> is…", result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Truncate_By_Words_Mid_Tag()
|
||||
{
|
||||
var result = _htmlHelper.TruncateByWords(SampleWithAnchorElement, 7).ToString();
|
||||
|
||||
Assert.AreEqual("Hello world, this is some text <a href='blah'>with…</a>", result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Strip_All_Html()
|
||||
{
|
||||
var result = _htmlHelper.StripHtml(SampleWithBoldAndAnchorElements, null).ToString();
|
||||
|
||||
Assert.AreEqual("Hello world, this is some text with a link", result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Strip_Specific_Html()
|
||||
{
|
||||
string[] tags = { "b" };
|
||||
|
||||
var result = _htmlHelper.StripHtml(SampleWithBoldAndAnchorElements, tags).ToString();
|
||||
|
||||
Assert.AreEqual(SampleWithAnchorElement, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Strip_Invalid_Html()
|
||||
{
|
||||
const string text = "Hello world, <bthis</b> is some text <a href='blah'>with a link</a>";
|
||||
|
||||
var result = _htmlHelper.StripHtml(text).ToString();
|
||||
|
||||
Assert.AreEqual("Hello world, is some text with a link", result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
// Copyright (c) Umbraco.
|
||||
// See LICENSE for more details.
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Extensions;
|
||||
|
||||
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common.Extensions
|
||||
{
|
||||
[TestFixture]
|
||||
public class HttpContextExtensionTests
|
||||
{
|
||||
[Test]
|
||||
public void TryGetBasicAuthCredentials_WithoutHeader_ReturnsFalse()
|
||||
{
|
||||
var httpContext = new DefaultHttpContext();
|
||||
|
||||
var result = httpContext.TryGetBasicAuthCredentials(out string _, out string _);
|
||||
|
||||
Assert.IsFalse(result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TryGetBasicAuthCredentials_WithHeader_ReturnsTrueWithCredentials()
|
||||
{
|
||||
const string Username = "fred";
|
||||
const string Password = "test";
|
||||
|
||||
var httpContext = new DefaultHttpContext();
|
||||
var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{Username}:{Password}"));
|
||||
httpContext.Request.Headers.Add("Authorization", $"Basic {credentials}");
|
||||
|
||||
bool result = httpContext.TryGetBasicAuthCredentials(out string username, out string password);
|
||||
|
||||
Assert.IsTrue(result);
|
||||
Assert.AreEqual(Username, username);
|
||||
Assert.AreEqual(Password, password);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
// Copyright (c) Umbraco.
|
||||
// See LICENSE for more details.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Cms.Core.Media;
|
||||
using Umbraco.Cms.Core.Models;
|
||||
using Umbraco.Cms.Core.PropertyEditors.ValueConverters;
|
||||
using Umbraco.Extensions;
|
||||
|
||||
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common.Extensions
|
||||
{
|
||||
[TestFixture]
|
||||
public class ImageCropperTemplateCoreExtensionsTests
|
||||
{
|
||||
[Test]
|
||||
public void GetCropUrl_WithCropSpecifiedButNotFound_ReturnsNull()
|
||||
{
|
||||
var imageUrl = "/test.jpg";
|
||||
Mock<IImageUrlGenerator> imageUrlGenerator = CreateMockImageUrlGenerator();
|
||||
var result = imageUrl.GetCropUrl(
|
||||
imageUrlGenerator.Object,
|
||||
new ImageCropperValue { },
|
||||
imageCropMode: ImageCropMode.Crop,
|
||||
cropAlias: "Missing");
|
||||
|
||||
Assert.IsNull(result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetCropUrl_WithCropSpecifiedAndUsingCropDimensions_CallsImageGeneratorWithCorrectParameters()
|
||||
{
|
||||
var imageUrl = "/test.jpg";
|
||||
Mock<IImageUrlGenerator> imageUrlGenerator = CreateMockImageUrlGenerator();
|
||||
var result = imageUrl.GetCropUrl(
|
||||
imageUrlGenerator.Object,
|
||||
CreateImageCropperValueWithCrops(),
|
||||
imageCropMode: ImageCropMode.Crop,
|
||||
cropAlias: "TestCrop",
|
||||
useCropDimensions: true);
|
||||
|
||||
imageUrlGenerator
|
||||
.Verify(x => x.GetImageUrl(
|
||||
It.Is<ImageUrlGenerationOptions>(y => y.Width == 100 &&
|
||||
y.Height == 200)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetCropUrl_WithCropSpecifiedAndWidthAndHeightProvided_CallsImageGeneratorWithCorrectParameters()
|
||||
{
|
||||
var imageUrl = "/test.jpg";
|
||||
Mock<IImageUrlGenerator> imageUrlGenerator = CreateMockImageUrlGenerator();
|
||||
var result = imageUrl.GetCropUrl(
|
||||
imageUrlGenerator.Object,
|
||||
CreateImageCropperValueWithCrops(),
|
||||
imageCropMode: ImageCropMode.Crop,
|
||||
cropAlias: "TestCrop",
|
||||
width: 50,
|
||||
height: 80);
|
||||
|
||||
imageUrlGenerator
|
||||
.Verify(x => x.GetImageUrl(
|
||||
It.Is<ImageUrlGenerationOptions>(y => y.Width == 50 &&
|
||||
y.Height == 80)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetCropUrl_WithCropSpecifiedAndWidthOnlyProvided_CallsImageGeneratorWithCorrectParameters()
|
||||
{
|
||||
var imageUrl = "/test.jpg";
|
||||
Mock<IImageUrlGenerator> imageUrlGenerator = CreateMockImageUrlGenerator();
|
||||
var result = imageUrl.GetCropUrl(
|
||||
imageUrlGenerator.Object,
|
||||
CreateImageCropperValueWithCrops(),
|
||||
imageCropMode: ImageCropMode.Crop,
|
||||
cropAlias: "TestCrop",
|
||||
width: 50);
|
||||
|
||||
imageUrlGenerator
|
||||
.Verify(x => x.GetImageUrl(
|
||||
It.Is<ImageUrlGenerationOptions>(y => y.Width == 50 &&
|
||||
y.Height == 100)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetCropUrl_WithCropSpecifiedAndHeightOnlyProvided_CallsImageGeneratorWithCorrectParameters()
|
||||
{
|
||||
var imageUrl = "/test.jpg";
|
||||
Mock<IImageUrlGenerator> imageUrlGenerator = CreateMockImageUrlGenerator();
|
||||
var result = imageUrl.GetCropUrl(
|
||||
imageUrlGenerator.Object,
|
||||
CreateImageCropperValueWithCrops(),
|
||||
imageCropMode: ImageCropMode.Crop,
|
||||
cropAlias: "TestCrop",
|
||||
height: 50);
|
||||
|
||||
imageUrlGenerator
|
||||
.Verify(x => x.GetImageUrl(
|
||||
It.Is<ImageUrlGenerationOptions>(y => y.Width == 25 &&
|
||||
y.Height == 50)));
|
||||
}
|
||||
|
||||
private static Mock<IImageUrlGenerator> CreateMockImageUrlGenerator() => new Mock<IImageUrlGenerator>();
|
||||
|
||||
private static ImageCropperValue CreateImageCropperValueWithCrops() => new ImageCropperValue
|
||||
{
|
||||
Crops = new List<ImageCropperValue.ImageCropperCrop>
|
||||
{
|
||||
new ImageCropperValue.ImageCropperCrop { Alias = "TestCrop", Width = 100, Height = 200 },
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user