Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions core/attributedef.class.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -4259,6 +4259,9 @@ public function GetMaxSize()

public static function RenderWikiHtml($sText, $bWikiOnly = false)
{
// N°8681 - Ensure to have a string value
$sText = $sText ?? '';

if (!$bWikiOnly) {
$sPattern = '/'.str_replace('/', '\/', utils::GetConfig()->Get('url_validation_pattern')).'/i';
if (preg_match_all(
Expand Down
3 changes: 3 additions & 0 deletions core/inlineimage.class.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ public static function OnFormCancel($sTempId): bool
*/
public static function FixUrls($sHtml)
{
// N°8681 - Ensure to have a string value
$sHtml = $sHtml ?? '';

$aNeedles = [];
$aReplacements = [];
// Find img tags with an attribute data-img-id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ class AttributeDefinitionTest extends ItopDataTestCase
{
public const CREATE_TEST_ORG = true;

protected function setUp(): void
{
parent::setUp();
require_once(APPROOT.'core/attributedef.class.inc.php');

}

public function testGetImportColumns()
{
$oAttributeDefinition = MetaModel::GetAttributeDef("ApplicationSolution", "status");
Expand Down
78 changes: 78 additions & 0 deletions tests/php-unit-tests/unitary-tests/core/AttributeTextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/*
* @copyright Copyright (C) 2010-2026 Combodo SAS
* @license http://opensource.org/licenses/AGPL-3.0
*/

namespace Combodo\iTop\Test\UnitTest\Core;

use AttributeText;
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;

class AttributeTextTest extends ItopDataTestCase
{
protected \Organization $oTestOrganizationForAttributeText;

public function setUp(): void
{
parent::setUp();
$this->oTestOrganizationForAttributeText = $this->CreateOrganization('Test for AttributeTextTest');
}

/**
* @covers AttributeText::RenderWikiHtml
*/
public function testRenderWikiHtml_nonWikiUrlVariants()
{
// String value
$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(''));

// Null value
$this->assertEquals('', AttributeText::RenderWikiHtml(null));
}

/**
* @covers AttributeText::RenderWikiHtml
*/
public function testRenderWikiHtml_bWikiOnlyAbsentOrFalse_shouldTransformBothRegularAndWikiHyperlinks()
{
$sInput = 'A regular hyperlink https://combodo.com and a wiki hyperlink to an existing object [[Organization:'.$this->oTestOrganizationForAttributeText->GetKey().']]';

// bWikiOnly default value
$sResult = AttributeText::RenderWikiHtml($sInput);
$this->assertStringContainsString('<a href="https://combodo.com">', $sResult);
$this->assertStringContainsString('class="object-ref-link"', $sResult);

// bWikiOnly = false
$sResult = AttributeText::RenderWikiHtml($sInput, false);
$this->assertStringContainsString('<a href="https://combodo.com">', $sResult);
$this->assertStringContainsString('class="object-ref-link"', $sResult);
}

/**
* @covers AttributeText::RenderWikiHtml
*/
public function testRenderWikiHtml_bWikiOnlyToTrue_shouldNotTransformRegularHyperlinkButTransformWikiHyperlink()
{
$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);
}

/**
* @covers AttributeText::RenderWikiHtml
*/
public function testRenderWikiHtml_shouldTransformWikiHyperlinkForExistingObjectsOnly()
{
$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);
}
}
39 changes: 39 additions & 0 deletions tests/php-unit-tests/unitary-tests/core/InlineImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,43 @@ public function OnFormCancelInvalidTempIdProvider()
],
];
}

/**
* @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);
}
}