Skip to content

Commit 7ef9bd4

Browse files
author
RTLcoil
authored
Fix expression normalisation
1 parent e7998dd commit 7ef9bd4

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

src/Transformation/Expression/ExpressionUtils.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private static function lazyInit()
9191
}
9292

9393
if (empty(self::$IF_REPLACE_RE)) {
94-
self::$IF_REPLACE_RE = '/((\|\||>=|<=|&&|!=|>|=|<|\/|\-|\+|\*|\^)(?=[ _])|(?<![\$\:])(' .
94+
self::$IF_REPLACE_RE = '/((\$_*[^_]+)|(\|\||>=|<=|&&|!=|>|=|<|\/|\-|\+|\*|\^)(?=[ _])|(?<![\$\:])(' .
9595
implode('|', array_keys(self::$PREDEFINED_VARIABLES)) .
9696
'))/';
9797
}

tests/Unit/Transformation/Expression/ExpressionTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Cloudinary\Transformation\Crop;
1414
use Cloudinary\Transformation\Expression\Expression;
1515
use Cloudinary\Transformation\Expression\ExpressionComponent;
16+
use Cloudinary\Transformation\Expression\ExpressionUtils;
1617
use Cloudinary\Transformation\Expression\LogicalOperator;
1718
use Cloudinary\Transformation\Expression\PVar;
1819
use Cloudinary\Transformation\Expression\RelationalOperator;
@@ -117,4 +118,62 @@ public function testRawExpression()
117118

118119
self::assertEquals($expectedExpr . '_gt_997', (string)$rawExpr->greaterThan()->numeric(997));
119120
}
121+
122+
/**
123+
* Check expression normalization
124+
*
125+
* @dataProvider testNormalizationDataProvider
126+
*
127+
* @param mixed $input Value to normalize
128+
* @param null|string $expectedOutput Expected normalized output
129+
*/
130+
public function testExpressionNormalization($input, $expectedOutput)
131+
{
132+
$actual = ExpressionUtils::normalize($input);
133+
self::assertEquals($expectedOutput, $actual);
134+
}
135+
136+
/**
137+
* Data provider for testExpressionNormalization
138+
*
139+
* @return array[]
140+
*/
141+
public static function testNormalizationDataProvider()
142+
{
143+
return [
144+
'null is not affected' => [null, null],
145+
'number replaced with a string value' => [10, '10'],
146+
'empty string is not affected' => ['', ''],
147+
'single space is replaced with a single underscore' => [' ', '_'],
148+
'blank string is replaced with a single underscore' => [' ', '_'],
149+
'underscore is not affected' => ['_', '_'],
150+
'sequence of underscores and spaces is replaced with a single underscore' => [' _ __ _', '_'],
151+
'arbitrary text is not affected' => ['foobar', 'foobar'],
152+
'double ampersand replaced with and operator' => ['foo && bar', 'foo_and_bar'],
153+
'double ampersand with no space at the end is not affected' => ['foo&&bar', 'foo&&bar'],
154+
'width recognized as variable and replaced with w' => ['width', 'w'],
155+
'initial aspect ratio recognized as variable and replaced with iar' => ['initial_aspect_ratio', 'iar'],
156+
'$width recognized as user variable and not affected' => ['$width', '$width'],
157+
'$initial_aspect_ratio recognized as user variable followed by aspect_ratio variable' => [
158+
'$initial_aspect_ratio',
159+
'$initial_ar',
160+
],
161+
'$mywidth recognized as user variable and not affected' => ['$mywidth', '$mywidth'],
162+
'$widthwidth recognized as user variable and not affected' => ['$widthwidth', '$widthwidth'],
163+
'$_width recognized as user variable and not affected' => ['$_width', '$_width'],
164+
'$__width recognized as user variable and not affected' => ['$__width', '$_width'],
165+
'$$width recognized as user variable and not affected' => ['$$width', '$$width'],
166+
'$height recognized as user variable and not affected' => ['$height_100', '$height_100'],
167+
'$heightt_100 recognized as user variable and not affected' => ['$heightt_100', '$heightt_100'],
168+
'$$height_100 recognized as user variable and not affected' => ['$$height_100', '$$height_100'],
169+
'$heightmy_100 recognized as user variable and not affected' => ['$heightmy_100', '$heightmy_100'],
170+
'$myheight_100 recognized as user variable and not affected' => ['$myheight_100', '$myheight_100'],
171+
'$heightheight_100 recognized as user variable and not affected' => [
172+
'$heightheight_100',
173+
'$heightheight_100',
174+
],
175+
'$theheight_100 recognized as user variable and not affected' => ['$theheight_100', '$theheight_100'],
176+
'$__height_100 recognized as user variable and not affected' => ['$__height_100', '$_height_100']
177+
];
178+
}
120179
}

tests/Unit/Transformation/TransformationTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use Cloudinary\Transformation\FocalGravity;
3232
use Cloudinary\Transformation\FocusOn;
3333
use Cloudinary\Transformation\Format;
34+
use Cloudinary\Transformation\GenericResize;
3435
use Cloudinary\Transformation\Gravity;
3536
use Cloudinary\Transformation\ImageTransformation;
3637
use Cloudinary\Transformation\Overlay;
@@ -582,4 +583,26 @@ public function testCoupleSample()
582583
->roundCorners(RoundCorners::byRadius(60))
583584
);
584585
}
586+
587+
/**
588+
* Tests that user variable names containing predefined names are not affected by normalization
589+
*/
590+
public function testUserVariableNamesContainingPredefinedNamesAreNotAffected()
591+
{
592+
$transformation = (new Transformation())
593+
->addVariable('$mywidth', 100)
594+
->addVariable('$aheight', 300)
595+
->resize(
596+
GenericResize::generic(
597+
'',
598+
'3 + $mywidth * 3 + 4 / 2 * initialWidth * $mywidth',
599+
'3 * initialHeight + $aheight'
600+
)
601+
);
602+
603+
self::assertEquals(
604+
'$mywidth_100/$aheight_300/h_3_mul_ih_add_$aheight,w_3_add_$mywidth_mul_3_add_4_div_2_mul_iw_mul_$mywidth',
605+
(string)$transformation
606+
);
607+
}
585608
}

0 commit comments

Comments
 (0)