From cbca18829f288a5193c3b25c7f91fd4a2d18a459 Mon Sep 17 00:00:00 2001 From: Mauro Cassani Date: Fri, 4 Mar 2022 16:29:33 +0100 Subject: [PATCH 1/3] @@ syntax --- src/Filters/Snails.php | 31 +++++++++++++++ src/MyMemoryFilter.php | 12 ++++-- tests/MateCatSubFilteringTest.php | 3 +- tests/MyMemorySubFilteringTest.php | 60 +++++++++++++++++++++++++++--- tests/TestHandlersOrder.php | 2 +- 5 files changed, 98 insertions(+), 10 deletions(-) create mode 100644 src/Filters/Snails.php diff --git a/src/Filters/Snails.php b/src/Filters/Snails.php new file mode 100644 index 0000000..4ef3954 --- /dev/null +++ b/src/Filters/Snails.php @@ -0,0 +1,31 @@ +\s%]+?@@|(?\s@]+?@/', $segment, $html, PREG_SET_ORDER ); + foreach ( $html as $pos => $snail_variable ) { + //check if inside twig variable there is a tag because in this case shouldn't replace the content with PH tag + if( !strstr($snail_variable[0], Constants::GTPLACEHOLDER) ){ + //replace subsequent elements excluding already encoded + $segment = preg_replace( + '/' . preg_quote( $snail_variable[0], '/' ) . '/', + '', + $segment, + 1 + ); + } + } + + return $segment; + } +} \ No newline at end of file diff --git a/src/MyMemoryFilter.php b/src/MyMemoryFilter.php index 3f6cf9c..ce0ede0 100644 --- a/src/MyMemoryFilter.php +++ b/src/MyMemoryFilter.php @@ -2,6 +2,7 @@ namespace Matecat\SubFiltering; + use Matecat\SubFiltering\Commons\Pipeline; use Matecat\SubFiltering\Filters\CtrlCharsPlaceHoldToAscii; use Matecat\SubFiltering\Filters\EncodeToRawXML; @@ -25,6 +26,8 @@ use Matecat\SubFiltering\Filters\SubFilteredPhToHtml; use Matecat\SubFiltering\Filters\TwigToPh; use Matecat\SubFiltering\Filters\Variables; +use Matecat\SubFiltering\Filters\Snails; +use Matecat\SubFiltering\Filters\DoubleSnail; /** * Class MyMemoryFilter @@ -39,6 +42,7 @@ * @package Matecat\SubFiltering */ class MyMemoryFilter extends AbstractFilter { + /** * Used to transform database raw xml content ( Layer 0 ) to the sub filtered structures, used for server to server ( Ex: TM/MT ) communications ( Layer 1 ) * @@ -47,14 +51,15 @@ class MyMemoryFilter extends AbstractFilter { * * @return mixed */ - public function fromLayer0ToLayer1( $segment, $cid = null ) { + public function fromLayer0ToLayer1( $segment, $cid = null ) + { $channel = new Pipeline( $this->source, $this->target ); $channel->addLast( new StandardPHToMateCatCustomPH() ); $channel->addLast( new PlaceHoldXliffTags() ); $channel->addLast( new LtGtDecode() ); $channel->addLast( new HtmlToPh() ); if ( $cid == 'airbnb' ) { - $channel->addLast( new Variables() ); // SE AIRBNB + $channel->addLast( new Variables() ); $channel->addLast( new SmartCounts() ); } @@ -67,7 +72,8 @@ public function fromLayer0ToLayer1( $segment, $cid = null ) { $channel->remove( new TwigToPh() ); $channel->remove( new SprintfToPH() ); $channel->addAfter( new HtmlToPh(), new RubyOnRailsI18n() ); - $channel->addAfter( new RubyOnRailsI18n(), new Percentages() ); + $channel->addAfter( new RubyOnRailsI18n(), new Snails() ); + $channel->addAfter( new Snails(), new Percentages() ); $channel->addAfter( new Percentages(), new SprintfToPH() ); $channel->addAfter( new SprintfToPH(), new TwigToPh() ); $channel->addAfter( new TwigToPh(), new SingleCurlyBracketsToPh() ); diff --git a/tests/MateCatSubFilteringTest.php b/tests/MateCatSubFilteringTest.php index 8aba6ec..64fa040 100644 --- a/tests/MateCatSubFilteringTest.php +++ b/tests/MateCatSubFilteringTest.php @@ -12,7 +12,8 @@ use Matecat\SubFiltering\Utils\CatUtils; use PHPUnit\Framework\TestCase; -class MateCatSubFilteringTest extends TestCase { +class MateCatSubFilteringTest extends TestCase +{ /** * @return \Matecat\SubFiltering\AbstractFilter * @throws \Exception diff --git a/tests/MyMemorySubFilteringTest.php b/tests/MyMemorySubFilteringTest.php index 5639c8d..dff531e 100644 --- a/tests/MyMemorySubFilteringTest.php +++ b/tests/MyMemorySubFilteringTest.php @@ -3,7 +3,6 @@ namespace Matecat\SubFiltering\Tests; use Matecat\SubFiltering\MyMemoryFilter; -use Matecat\SubFiltering\Tests\Mocks\Features\AirbnbFeature; use Matecat\SubFiltering\Tests\Mocks\FeatureSet; use PHPUnit\Framework\TestCase; @@ -15,11 +14,14 @@ class MyMemorySubFilteringTest extends TestCase */ private function getFilterInstance() { - $featureSet = new FeatureSet([new AirbnbFeature()]); - - return MyMemoryFilter::getInstance($featureSet, 'en-US','it-IT', []); + return MyMemoryFilter::getInstance(new FeatureSet(), 'en-US','it-IT', []); } + /** + * Test for Airbnb + * + * @throws \Exception + */ public function testVariablesWithHTML() { $filter = $this->getFilterInstance(); @@ -28,6 +30,54 @@ public function testVariablesWithHTML() $segment_from_UI = 'Airbnb account.%{}1) From '; $this->assertEquals( $db_segment, $filter->fromLayer1ToLayer0( $segment_from_UI ) ); - $this->assertEquals( $segment_from_UI, $filter->fromLayer0ToLayer1( $db_segment ) ); + $this->assertEquals( $segment_from_UI, $filter->fromLayer0ToLayer1( $db_segment, 'airbnb' ) ); + } + + /** + * Test for skyscanner + * + * @throws \Exception + */ + public function testSingleSnailSyntax() + { + $filter = $this->getFilterInstance(); + + $db_segment = 'This syntax @this is a variable@ is not valid'; + $segment_from_UI = 'This syntax @this is a variable@ is not valid'; + + $this->assertEquals( $db_segment, $filter->fromLayer1ToLayer0( $segment_from_UI ) ); + $this->assertEquals( $segment_from_UI, $filter->fromLayer0ToLayer1( $db_segment, 'skyscanner' ) ); + + $filter = $this->getFilterInstance(); + + $db_segment = 'This syntax @this_is_a_variable@ is valid'; + $segment_from_UI = 'This syntax is valid'; + + $this->assertEquals( $db_segment, $filter->fromLayer1ToLayer0( $segment_from_UI ) ); + $this->assertEquals( $segment_from_UI, $filter->fromLayer0ToLayer1( $db_segment, 'skyscanner' ) ); + } + + /** + * Test for skyscanner + * + * @throws \Exception + */ + public function testDoubleSnailSyntax() + { + $filter = $this->getFilterInstance(); + + $db_segment = 'This syntax @@this is a variable@@ is not valid'; + $segment_from_UI = 'This syntax @@this is a variable@@ is not valid'; + + $this->assertEquals( $db_segment, $filter->fromLayer1ToLayer0( $segment_from_UI ) ); + $this->assertEquals( $segment_from_UI, $filter->fromLayer0ToLayer1( $db_segment, 'skyscanner' ) ); + + $filter = $this->getFilterInstance(); + + $db_segment = 'This syntax @@this_is_a_variable@@ is valid'; + $segment_from_UI = 'This syntax is valid'; + + $this->assertEquals( $db_segment, $filter->fromLayer1ToLayer0( $segment_from_UI ) ); + $this->assertEquals( $segment_from_UI, $filter->fromLayer0ToLayer1( $db_segment, 'skyscanner' ) ); } } \ No newline at end of file diff --git a/tests/TestHandlersOrder.php b/tests/TestHandlersOrder.php index e5583a4..599bcc5 100644 --- a/tests/TestHandlersOrder.php +++ b/tests/TestHandlersOrder.php @@ -54,7 +54,7 @@ public function testReOrder() { $this->channel->addAfter( new SprintfToPH(), new TwigToPh() ); $this->channel->addAfter( new TwigToPh(), new SingleCurlyBracketsToPh() ); - $reflection = new ReflectionClass( $this->channel ); + $reflection = new \ReflectionClass( $this->channel ); $property = $reflection->getProperty( 'handlers' ); $property->setAccessible( true ); $handlersList = $property->getValue( $this->channel ); From 9566804b2735c205ce2faa7bc27179e54ea4ce5d Mon Sep 17 00:00:00 2001 From: Mauro Cassani Date: Fri, 4 Mar 2022 16:43:33 +0100 Subject: [PATCH 2/3] Fixed tests --- src/AbstractFilter.php | 8 ++++++++ tests/MateCatSubFilteringTest.php | 5 ++++- tests/MyMemorySubFilteringTest.php | 2 ++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/AbstractFilter.php b/src/AbstractFilter.php index 097e0c0..e596976 100644 --- a/src/AbstractFilter.php +++ b/src/AbstractFilter.php @@ -65,6 +65,14 @@ protected function setTarget( $target ) $this->target = $target; } + /** + * Destroy the singleton + */ + public static function destroyInstance() + { + static::$_INSTANCE = null; + } + /** * @param string $source * @param string $target diff --git a/tests/MateCatSubFilteringTest.php b/tests/MateCatSubFilteringTest.php index 64fa040..3b26b5d 100644 --- a/tests/MateCatSubFilteringTest.php +++ b/tests/MateCatSubFilteringTest.php @@ -18,7 +18,10 @@ class MateCatSubFilteringTest extends TestCase * @return \Matecat\SubFiltering\AbstractFilter * @throws \Exception */ - private function getFilterInstance() { + private function getFilterInstance() + { + MateCatFilter::destroyInstance(); // for isolation test + return MateCatFilter::getInstance( new FeatureSet(), 'en-US', 'it-IT' ); } diff --git a/tests/MyMemorySubFilteringTest.php b/tests/MyMemorySubFilteringTest.php index dff531e..e4c839c 100644 --- a/tests/MyMemorySubFilteringTest.php +++ b/tests/MyMemorySubFilteringTest.php @@ -14,6 +14,8 @@ class MyMemorySubFilteringTest extends TestCase */ private function getFilterInstance() { + MyMemoryFilter::destroyInstance(); // for isolation test + return MyMemoryFilter::getInstance(new FeatureSet(), 'en-US','it-IT', []); } From 28b31898228f06dd915b54813af89645c4287639 Mon Sep 17 00:00:00 2001 From: Mauro Cassani Date: Fri, 4 Mar 2022 17:30:49 +0100 Subject: [PATCH 3/3] regex --- src/Filters/Snails.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Filters/Snails.php b/src/Filters/Snails.php index 4ef3954..edcee94 100644 --- a/src/Filters/Snails.php +++ b/src/Filters/Snails.php @@ -12,7 +12,7 @@ class Snails extends AbstractHandler */ public function transform( $segment ) { - preg_match_all( '/@@[^<>\s%]+?@@|(?\s@]+?@/', $segment, $html, PREG_SET_ORDER ); + preg_match_all( '/@@[^<>\s]+?@@|@[^<>\s@]+?@/', $segment, $html, PREG_SET_ORDER ); foreach ( $html as $pos => $snail_variable ) { //check if inside twig variable there is a tag because in this case shouldn't replace the content with PH tag if( !strstr($snail_variable[0], Constants::GTPLACEHOLDER) ){