Skip to content

Commit

Permalink
Merge pull request #11 from matecat/fix-href
Browse files Browse the repository at this point in the history
Fix on SubFilteredPhToHtml
  • Loading branch information
mauretto78 authored Aug 25, 2021
2 parents 02c06c7 + 127bf3c commit ee920c7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
2 changes: 0 additions & 2 deletions src/Filters/SubFilteredPhToHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace Matecat\SubFiltering\Filters;

use Matecat\SubFiltering\Commons\AbstractHandler;
use Matecat\SubFiltering\Commons\Constants;

class SubFilteredPhToHtml extends AbstractHandler {

Expand All @@ -25,7 +24,6 @@ public function transform( $segment ){
preg_match_all( '|<ph id\s*=\s*["\']mtc_[0-9]+["\'] equiv-text\s*=\s*["\']base64:([^"\']+)["\']\s*\/>|siU', $segment, $html, PREG_SET_ORDER ); // Ungreedy
foreach ( $html as $subfilter_tag ) {
$value = base64_decode( $subfilter_tag[ 1 ] );
$value = html_entity_decode( $value, ENT_NOQUOTES | ENT_XML1 );
$segment = str_replace( $subfilter_tag[0], $value, $segment );
}

Expand Down
39 changes: 39 additions & 0 deletions tests/MateCatSubFilteringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -679,4 +679,43 @@ public function testPhTagsWithoutDataRef()

$this->assertEquals($back_to_db_segment, $db_segment);
}

public function testXTags()
{
$Filter = MateCatFilter::getInstance( new FeatureSet(), 'en-EN','et-ET', [] );

$db_segment = '&lt;x id="3"/&gt;Primary Care Facilities&lt;x id="7"/&gt;';
$expected_l1_segment = '<ph id="mtc_1" equiv-text="base64:Jmx0O3ggaWQ9IjMiLyZndDs="/>Primary Care Facilities<ph id="mtc_2" equiv-text="base64:Jmx0O3ggaWQ9IjciLyZndDs="/>';
$expected_l2_segment = '&lt;ph id="mtc_1" equiv-text="base64:Jmx0O3ggaWQ9IjMiLyZndDs="/&gt;Primary Care Facilities&lt;ph id="mtc_2" equiv-text="base64:Jmx0O3ggaWQ9IjciLyZndDs="/&gt;';

$l1_segment = $Filter->fromLayer0ToLayer1( $db_segment );
$l2_segment = $Filter->fromLayer1ToLayer2( $l1_segment );

// $this->assertEquals($l1_segment, $expected_l1_segment);
// $this->assertEquals($l2_segment, $expected_l2_segment);

$back_to_db_segment = $Filter->fromLayer1ToLayer0($l1_segment);

$this->assertEquals($back_to_db_segment, $db_segment);
}

public function testWithComplexUrls()
{
$Filter = MateCatFilter::getInstance( new FeatureSet(), 'en-EN','et-ET', [] );

$db_segment = '&lt;li&gt;Bergamo: &lt;a href="http://www.comune.bergamo.it/servizi/Menu/dinamica.aspx?idSezione=3780&amp;#38;idArea=1182&amp;#38;idCat=555&amp;#38;ID=28395&amp;#38;TipoElemento=categoria" target="_blank"&gt;Italian&lt;/a&gt; &lt;/li&gt;&#10;&#917760;';
$expected_l1_segment = '<ph id="mtc_1" equiv-text="base64:Jmx0O2xpJmd0Ow=="/>Bergamo: <ph id="mtc_2" equiv-text="base64:Jmx0O2EgaHJlZj0iaHR0cDovL3d3dy5jb211bmUuYmVyZ2Ftby5pdC9zZXJ2aXppL01lbnUvZGluYW1pY2EuYXNweD9pZFNlemlvbmU9Mzc4MCZhbXA7IzM4O2lkQXJlYT0xMTgyJmFtcDsjMzg7aWRDYXQ9NTU1JmFtcDsjMzg7SUQ9MjgzOTUmYW1wOyMzODtUaXBvRWxlbWVudG89Y2F0ZWdvcmlhIiB0YXJnZXQ9Il9ibGFuayImZ3Q7"/>Italian<ph id="mtc_3" equiv-text="base64:Jmx0Oy9hJmd0Ow=="/> <ph id="mtc_4" equiv-text="base64:Jmx0Oy9saSZndDs="/>&#10;&#917760;';
$expected_l2_segment = '&lt;ph id="mtc_1" equiv-text="base64:Jmx0O2xpJmd0Ow=="/&gt;Bergamo: &lt;ph id="mtc_2" equiv-text="base64:Jmx0O2EgaHJlZj0iaHR0cDovL3d3dy5jb211bmUuYmVyZ2Ftby5pdC9zZXJ2aXppL01lbnUvZGluYW1pY2EuYXNweD9pZFNlemlvbmU9Mzc4MCZhbXA7IzM4O2lkQXJlYT0xMTgyJmFtcDsjMzg7aWRDYXQ9NTU1JmFtcDsjMzg7SUQ9MjgzOTUmYW1wOyMzODtUaXBvRWxlbWVudG89Y2F0ZWdvcmlhIiB0YXJnZXQ9Il9ibGFuayImZ3Q7"/&gt;Italian&lt;ph id="mtc_3" equiv-text="base64:Jmx0Oy9hJmd0Ow=="/&gt; &lt;ph id="mtc_4" equiv-text="base64:Jmx0Oy9saSZndDs="/&gt;##$_0A$##󠄀';

$l1_segment = $Filter->fromLayer0ToLayer1( $db_segment );
$l2_segment = $Filter->fromLayer1ToLayer2( $l1_segment );

$this->assertEquals($l1_segment, $expected_l1_segment);
$this->assertEquals($l2_segment, $expected_l2_segment);

$back_to_db_segment = $Filter->fromLayer1ToLayer0($l1_segment);

$this->assertEquals($back_to_db_segment, $db_segment);

}
}

0 comments on commit ee920c7

Please sign in to comment.