N°8681 - Add unit tests

This commit is contained in:
Molkobain
2026-02-15 22:22:42 +01:00
parent f6f94da2d0
commit 1823e8a722
2 changed files with 57 additions and 18 deletions

View File

@@ -25,9 +25,9 @@ class AttributeTextTest extends ItopDataTestCase
public function testRenderWikiHtml_nonWikiUrlVariants()
{
// String value
$input = 'This hyperlink https://combodo.com should be in an anchor tag.';
$expected = 'This hyperlink <a href="https://combodo.com">https://combodo.com</a> should be in an anchor tag.';
$this->assertEquals($expected, AttributeText::RenderWikiHtml($input));
$sInput = 'This hyperlink https://combodo.com should be in an anchor tag.';
$sExpected = 'This hyperlink <a href="https://combodo.com">https://combodo.com</a> should be in an anchor tag.';
$this->assertEquals($sExpected, AttributeText::RenderWikiHtml($sInput));
// Empty string value
$this->assertEquals('', AttributeText::RenderWikiHtml(''));
@@ -41,17 +41,17 @@ class AttributeTextTest extends ItopDataTestCase
*/
public function testRenderWikiHtml_bWikiOnlyAbsentOrFalse_shouldTransformBothRegularAndWikiHyperlinks()
{
$input = 'A regular hyperlink https://combodo.com and a wiki hyperlink to an existing object [[Organization:'.$this->oTestOrganizationForAttributeText->GetKey().']]';
$sInput = 'A regular hyperlink https://combodo.com and a wiki hyperlink to an existing object [[Organization:'.$this->oTestOrganizationForAttributeText->GetKey().']]';
// bWikiOnly default value
$result = AttributeText::RenderWikiHtml($input);
$this->assertStringContainsString('<a href="https://combodo.com">', $result);
$this->assertStringContainsString('class="object-ref-link"', $result);
$sResult = AttributeText::RenderWikiHtml($sInput);
$this->assertStringContainsString('<a href="https://combodo.com">', $sResult);
$this->assertStringContainsString('class="object-ref-link"', $sResult);
// bWikiOnly = false
$result = AttributeText::RenderWikiHtml($input, false);
$this->assertStringContainsString('<a href="https://combodo.com">', $result);
$this->assertStringContainsString('class="object-ref-link"', $result);
$sResult = AttributeText::RenderWikiHtml($sInput, false);
$this->assertStringContainsString('<a href="https://combodo.com">', $sResult);
$this->assertStringContainsString('class="object-ref-link"', $sResult);
}
/**
@@ -59,10 +59,10 @@ class AttributeTextTest extends ItopDataTestCase
*/
public function testRenderWikiHtml_bWikiOnlyToTrue_shouldNotTransformRegularHyperlinkButTransformWikiHyperlink()
{
$input = 'A regular hyperlink https://combodo.com and a wiki hyperlink to an existing object [[Organization:'.$this->oTestOrganizationForAttributeText->GetKey().']]';
$result = AttributeText::RenderWikiHtml($input, true);
$this->assertStringNotContainsString('<a href="https://combodo.com">', $result);
$this->assertStringContainsString('class="object-ref-link"', $result);
$sInput = 'A regular hyperlink https://combodo.com and a wiki hyperlink to an existing object [[Organization:'.$this->oTestOrganizationForAttributeText->GetKey().']]';
$sResult = AttributeText::RenderWikiHtml($sInput, true);
$this->assertStringNotContainsString('<a href="https://combodo.com">', $sResult);
$this->assertStringContainsString('class="object-ref-link"', $sResult);
}
/**
@@ -70,9 +70,9 @@ class AttributeTextTest extends ItopDataTestCase
*/
public function testRenderWikiHtml_shouldTransformWikiHyperlinkForExistingObjectsOnly()
{
$input = 'A wiki hyperlink to a non existing object [[Organization:123456789]] and a wiki hyperlink to an existing object [[Organization:'.$this->oTestOrganizationForAttributeText->GetKey().']]';
$result = AttributeText::RenderWikiHtml($input);
$this->assertStringContainsString('wiki_broken_link', $result);
$this->assertStringContainsString('class="object-ref-link"', $result);
$sInput = 'A wiki hyperlink to a non existing object [[Organization:123456789]] and a wiki hyperlink to an existing object [[Organization:'.$this->oTestOrganizationForAttributeText->GetKey().']]';
$sResult = AttributeText::RenderWikiHtml($sInput);
$this->assertStringContainsString('wiki_broken_link', $sResult);
$this->assertStringContainsString('class="object-ref-link"', $sResult);
}
}

View File

@@ -59,4 +59,43 @@ class InlineImageTest extends ItopDataTestCase
],
];
}
/**
* @covers InlineImage::FixUrls
*/
public function testFixUrls_shouldReturnAnEmptyStringIfNullOrEmptyStringPassed()
{
$sResult = InlineImage::FixUrls(null);
$this->assertEquals('', $sResult);
$sResult = InlineImage::FixUrls('');
$this->assertEquals('', $sResult);
}
/**
* @covers InlineImage::FixUrls
*/
public function testFixUrls_shouldReturnUnchangedValueIfValueContainsNoImage()
{
$sHtml = '<div><p>Texte sans image</p></div>';
$sResult = InlineImage::FixUrls($sHtml);
$this->assertEquals($sHtml, $sResult);
}
/**
* @covers InlineImage::FixUrls
*/
public function testFixUrls_shouldReplaceImagesSrcWithCurrentAppRootUrlAndSecret()
{
$sHtml = <<<HTML
<div>
<img src="/images/test1.png" data-img-id="123" data-img-secret="abc" />
<img src="/images/test2.png" data-img-id="456" data-img-secret="def" />
</div>
HTML;
$sResult = InlineImage::FixUrls($sHtml);
$this->assertStringContainsString('<img', $sResult);
$this->assertStringContainsString(\utils::EscapeHtml(\utils::GetAbsoluteUrlAppRoot().INLINEIMAGE_DOWNLOAD_URL.'123&s=abc'), $sResult);
$this->assertStringContainsString(\utils::EscapeHtml(\utils::GetAbsoluteUrlAppRoot().INLINEIMAGE_DOWNLOAD_URL.'456&s=def'), $sResult);
}
}