//-----------------------------------------------------------------------
//
// Copyright (c) aliasvault. All rights reserved.
// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
//
//-----------------------------------------------------------------------
namespace AliasVault.UnitTests.Utilities;
using System.Text.RegularExpressions;
using AliasVault.Shared.Utilities;
///
/// Tests for the ConversionUtility class.
///
public class ConversionUtilityTest
{
///
/// Tests the conversion of a simple anchor tag to open in a new tab.
///
[Test]
public void TestAnchorTabConversionSimple()
{
string anchorHtml = "";
string convertedAnchorTags = ConversionUtility.ConvertAnchorTagsToOpenInNewTab(anchorHtml);
// Check that conversion works as expected.
Assert.That(convertedAnchorTags, Does.Contain("target=\"_blank\""));
}
///
/// Tests the conversion of a complex anchor tag with multiple attributes to open in a new tab.
///
[Test]
public void TestAnchorTabConversionComplex1()
{
string anchorHtml = "Start hier met de training >>>";
string convertedAnchorTags = ConversionUtility.ConvertAnchorTagsToOpenInNewTab(anchorHtml);
// Check that conversion works as expected.
Assert.That(convertedAnchorTags, Does.Contain("target=\"_blank\""));
}
///
/// Tests the conversion of a complex anchor tag with nested elements to open in a new tab.
///
[Test]
public void TestAnchorTabConversionComplex2()
{
string anchorHtml = "
";
string convertedAnchorTags = ConversionUtility.ConvertAnchorTagsToOpenInNewTab(anchorHtml);
// Check that conversion works as expected.
Assert.That(convertedAnchorTags, Does.Contain("target=\"_blank\""));
}
///
/// Tests the conversion of a complex anchor tag within a table cell to open in a new tab.
///
[Test]
public void TestAnchorTabConversionComplex3()
{
string anchorHtml = "Ontvang nu jouw prijs > | ";
string convertedAnchorTags = ConversionUtility.ConvertAnchorTagsToOpenInNewTab(anchorHtml);
// Check that conversion works as expected.
Assert.That(convertedAnchorTags, Does.Contain("target=\"_blank\""));
}
///
/// Tests the conversion of a complex anchor tag within a table cell to open in a new tab.
///
[Test]
public void TestAnchorTabConversionComplex4()
{
string anchorHtml = "https://www.maxmind.com/en/account/set-password?token=FEA9D6D78B624D6BB048687F4D0A2DD9";
string convertedAnchorTags = ConversionUtility.ConvertAnchorTagsToOpenInNewTab(anchorHtml);
// Check that conversion works as expected.
Assert.That(convertedAnchorTags, Does.Contain("target=\"_blank\""));
}
///
/// Tests the conversion of a complex anchor tag with existing target="_blank" attribute to
/// not get a second attribute after conversion.
///
[Test]
public void TestAnchorTabConversionComplex5()
{
string anchorHtml = "test anchor text";
string convertedAnchorTags = ConversionUtility.ConvertAnchorTagsToOpenInNewTab(anchorHtml);
int targetBlankCount = Regex.Matches(convertedAnchorTags, "target=\"_blank\"", RegexOptions.NonBacktracking).Count;
Assert.Multiple(() =>
{
// Check that only one target="_blank" appears.
Assert.That(targetBlankCount, Is.EqualTo(1), "There should be exactly one target=\"_blank\" attribute.");
// Ensure other attributes are preserved
Assert.That(convertedAnchorTags, Does.Contain("href=\"test.html\""), "The href attribute should be preserved.");
// Check that the anchor text is preserved
Assert.That(convertedAnchorTags, Does.Contain(">test anchor text"), "The anchor text should be preserved.");
});
}
}