Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EZP-27092: Remove eZAutoLink operator usage of preg_replace and PREG_REPLACE_EVAL #1286

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
6 changes: 3 additions & 3 deletions kernel/common/ezautolinkoperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ function formatUri( $url, $max )
*/
function addURILinks( $text, $max, $methods = 'http|https|ftp' )
{
return preg_replace(
"`(?<!href=\"|href='|src=\"|src='|value=\"|value=')($methods):\/\/[\w]+(.[\w]+)([\w\-\.,@?^=%&:\/~\+#;*\(\)\!]*[\w\-\@?^=%&\/~\+#;*\(\)\!])?`e",
'eZAutoLinkOperator::formatUri("$0", '. $max. ')',
return preg_replace_callback(
"`(?<!href=\"|href='|src=\"|src='|value=\"|value=')($methods):\/\/[\w]+(.[\w]+)([\w\-\.,@?^=%&:\/~\+#;*\(\)\!]*[\w\-\@?^=%&\/~\+#;*\(\)\!])?`",
function($matches) use ($max) { return eZAutoLinkOperator::formatUri( $matches[0], $max ); },
$text
);
}
Expand Down
131 changes: 131 additions & 0 deletions tests/tests/kernel/common/ezautolinkoperator_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
/**
* File containing the eZAutoLinkOperatorTest class
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
* @package tests
*/

/**
* Test case for eZAutoLinkOperator class
*/
class eZAutoLinkOperatorTest extends ezpTestCase
{
public function __construct()
{
parent::__construct();
$this->setName( "eZAutoLinkOperator Tests" );
}

public function setUp()
{
}

public function tearDown()
{
}

/**
* test autolink operator with simple http url
*/
public function testeZAutoLinkOperatorSimpleHttpFullUrlLink()
{
$maxChars = 32;
$argument = 'Heath ( http://example.com/p/10979 )';
$expectedResult = 'Heath ( <a href="http://example.com/p/10979" title="http://example.com/p/10979">http://example.com/p/10979</a> )';

$this->runModify( 'autolink', $maxChars, $argument, $expectedResult );
}

/**
* test autolink operator with simple https url
*/
public function testeZAutoLinkOperatorSimpleHttpsFullUrlLink()
{
$maxChars = 32;
$argument = 'Heath ( https://example.com/p/10979 )';
$expectedResult = 'Heath ( <a href="https://example.com/p/10979" title="https://example.com/p/10979">https://example.com/p/10979</a> )';

$this->runModify( 'autolink', $maxChars, $argument, $expectedResult );
}

/**
* test autolink operator with simple email address
*/
public function testeZAutoLinkOperatorSimpleEmailLink()
{
$maxChars = 72;
$argument = 'Heath ( [email protected] )';
$expectedResult = "Heath ( <a href='mailto:[email protected]'>[email protected]</a> )";

$this->runModify( 'autolink', $maxChars, $argument, $expectedResult );
}

/**
* test autolink operator with simple ftp url
*/
public function testeZAutoLinkOperatorSimpleFtpFullUrlLink()
{
$maxChars = 72;
$argument = 'Heath ( ftp://example.com/pub/mockbinaryfile.tar.gz )';
$expectedResult = 'Heath ( <a href="ftp://example.com/pub/mockbinaryfile.tar.gz" title="ftp://example.com/pub/mockbinaryfile.tar.gz">ftp://example.com/pub/mockbinaryfile.tar.gz</a> )';

$this->runModify( 'autolink', $maxChars, $argument, $expectedResult );
}

/**
* test autolink operator function
*/
private function runModify( $operatorName, $maxChars, $argument, $expectedResult )
{
// TEST SETUP --------------------------------------------------------
$ini = eZINI::instance();
$defaultAccess = $ini->variable( 'SiteSettings', 'DefaultAccess' );
$this->setSiteAccess( $defaultAccess );

// Make sure to preserve ini settings in case other tests depend on them
$orgRemoveSiteaccess = $ini->variable( 'SiteAccessSettings', 'RemoveSiteAccessIfDefaultAccess' );

// ENABLE RemoveSiteAccessIfDefaultAccess
$ini->setVariable( 'SiteAccessSettings', 'RemoveSiteAccessIfDefaultAccess', 'enabled' );
// -------------------------------------------------------------------

// TEST --------------------------------------------------------------
$operator = new eZAutoLinkOperator();
$tpl = eZTemplate::instance();

$operatorValue = $argument;

$operatorParameters = array();

$namedParameters = array(
'max_chars' => $maxChars
);

$operator->modify(
$tpl, $operatorName, $operatorParameters, '', '', $operatorValue, $namedParameters, false
);

$this->assertEquals( $expectedResult, $operatorValue );
// -------------------------------------------------------------------
// TEST TEAR DOWN ----------------------------------------------------
$ini->setVariable( 'SiteAccessSettings', 'RemoveSiteAccessIfDefaultAccess', $orgRemoveSiteaccess );
eZSys::clearAccessPath();
// -------------------------------------------------------------------
}

/* -----------------------------------------------------------------------
* HELPER FUNCTIONS
* -----------------------------------------------------------------------
*/
private function setSiteAccess( $accessName )
{
eZSiteAccess::change( array( 'name' => $accessName,
'type' => eZSiteAccess::TYPE_URI,
'uri_part' => array( $accessName ) ) );
}
}

?>
1 change: 1 addition & 0 deletions tests/tests/kernel/suite.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function __construct()
$this->addTestSuite( 'eZBinaryFileTypeRegression' );
$this->addTestSuite( 'eZContentClassRegression' );
$this->addTestSuite( 'eZURLOperatorTest' );
$this->addTestSuite( 'eZAutoLinkOperatorTest' );
$this->addTestSuite( 'eZNamePatternResolverRegression' );

$this->addTestSuite( 'ezpTopologicalSortTest' );
Expand Down