N°562 Notifications: Add support for placeholders in HTML hyperlinks (regular href, mailto, ...)

This commit is contained in:
Molkobain
2018-11-06 17:35:17 +01:00
committed by Denis Flaven
parent 0e0d254188
commit 0c8dd6fd1b
2 changed files with 112 additions and 19 deletions

View File

@@ -6893,25 +6893,32 @@ abstract class MetaModel
{
// Expand the parameters for the object
$sName = substr($sSearch, 0, $iPos);
if (preg_match_all('/\\$'.$sName.'-(>|>)([^\\$]+)\\$/', $sInput, $aMatches)) // Support both syntaxes: $this->xxx$ or $this->xxx$ for HTML compatibility
{
foreach($aMatches[2] as $idx => $sPlaceholderAttCode)
{
try
{
$sReplacement = $replace->GetForTemplate($sPlaceholderAttCode);
if ($sReplacement !== null)
{
$aReplacements[] = $sReplacement;
$aSearches[] = '$'.$sName.'-'.$aMatches[1][$idx].$sPlaceholderAttCode.'$';
}
}
catch (Exception $e)
{
// No replacement will occur
}
}
}
$aRegExps = array(
'/(\\$)'.$sName.'-(>|>)([^\\$]+)\\$/', // Support both syntaxes: $this->xxx$ or $this->xxx$ for HTML compatibility
'/(%24)'.$sName.'-(>|>)([^%24]+)%24/', // Support for urlencoded in HTML attributes (%20this->xxx%20)
);
foreach($aRegExps as $sRegExp)
{
if(preg_match_all($sRegExp, $sInput, $aMatches))
{
foreach($aMatches[3] as $idx => $sPlaceholderAttCode)
{
try
{
$sReplacement = $replace->GetForTemplate($sPlaceholderAttCode);
if($sReplacement !== null)
{
$aReplacements[] = $sReplacement;
$aSearches[] = $aMatches[1][$idx] . $sName . '-' . $aMatches[2][$idx] . $sPlaceholderAttCode . $aMatches[1][$idx];
}
}
catch(Exception $e)
{
// No replacement will occur
}
}
}
}
}
else
{

View File

@@ -0,0 +1,86 @@
<?php
namespace Combodo\iTop\Test\UnitTest\Core;
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
use MetaModel;
/**
* Class MetaModelTest
*
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
* @backupGlobals disabled
*
* @since 2.6
* @package Combodo\iTop\Test\UnitTest\Core
*/
class MetaModelTest extends ItopDataTestCase
{
protected static $iDefaultUserOrgId = 1;
protected static $iDefaultUserCallerId = 1;
protected static $sDefaultUserRequestTitle = 'Unit test title';
protected static $sDefaultUserRequestDescription = 'Unit test description';
protected function setUp()
{
parent::setUp();
require_once APPROOT.'/core/metamodel.class.php';
require_once APPROOT.'/core/coreexception.class.inc.php';
}
/**
* @covers MetaModel::ApplyParams()
* @dataProvider ApplyParamsProvider
*
* @param string $sInput
* @param array $aParams
* @param string $sExpectedOutput
*
* @throws \Exception
*/
public function testApplyParams($sInput, $aParams, $sExpectedOutput)
{
$oUserRequest = $this->createObject(
'UserRequest',
array(
'org_id' => static::$iDefaultUserOrgId,
'caller_id' => static::$iDefaultUserCallerId,
'title' => static::$sDefaultUserRequestTitle,
'description' => static::$sDefaultUserRequestDescription,
)
);
$aParams['this->object()'] = $oUserRequest;
$sGeneratedOutput = MetaModel::ApplyParams($sInput, $aParams);
$this->assertEquals($sExpectedOutput, $sGeneratedOutput, "ApplyParams test returned $sGeneratedOutput");
}
public function ApplyParamsProvider()
{
$sTitle = static::$sDefaultUserRequestTitle;
$aParams = array();
return array(
'Object string attribute (text format)' => array(
'Title: $this->title$',
$aParams,
'Title: '.$sTitle,
),
'Object string attribute (html format)' => array(
'Title: <p>$this-&gt;title$</p>',
$aParams,
'Title: <p>'.$sTitle.'</p>',
),
'Object string attribute urlencoded (html format)' => array(
'Title: <a href="http://foo.bar/%24this-&gt;title%24">Hyperlink</a>',
$aParams,
'Title: <a href="http://foo.bar/'.$sTitle.'">Hyperlink</a>',
),
);
}
}