Skip to content

Commit 792a051

Browse files
committed
MQE-3267: trim invalid utf-8 characters from response
1 parent c064243 commit 792a051

File tree

3 files changed

+83
-26
lines changed

3 files changed

+83
-26
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace tests\unit\Magento\FunctionalTestFramework\Module\Util;
9+
10+
use Magento\FunctionalTestingFramework\Module\Util\ModuleUtils;
11+
use PHPUnit\Framework\TestCase;
12+
13+
class ModuleUtilTest extends TestCase
14+
{
15+
/**
16+
* Test utf8SafeControlCharacterTrim()
17+
*
18+
* @param string $input
19+
* @param string $output
20+
* @param string $removed
21+
*
22+
* @return void
23+
* @dataProvider inDataProvider
24+
*/
25+
public function testUtf8SafeControlCharacterTrim(string $input, string $output, $removed): void
26+
{
27+
$util = new ModuleUtils();
28+
$this->assertStringContainsString($output, $util->utf8SafeControlCharacterTrim($input));
29+
$this->assertStringNotContainsString($removed, $util->utf8SafeControlCharacterTrim($input));
30+
}
31+
32+
/**
33+
* Data input.
34+
*
35+
* @return array
36+
*/
37+
public function inDataProvider(): array
38+
{
39+
$ctr1 = '‹';
40+
$ctr2 = 'Œ';
41+
$ctr3 = 'Œ ‹';
42+
return [
43+
["some text $ctr1", 'some text', $ctr1],
44+
["some text $ctr2", 'some text', $ctr2],
45+
["some text $ctr3", 'some text', $ctr3]
46+
];
47+
}
48+
}

src/Magento/FunctionalTestingFramework/Module/MagentoWebDriver.php

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Magento\FunctionalTestingFramework\DataTransport\Auth\Tfa\OTP;
2222
use Magento\FunctionalTestingFramework\DataTransport\Protocol\CurlInterface;
2323
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\CredentialStore;
24+
use Magento\FunctionalTestingFramework\Module\Util\ModuleUtils;
2425
use Magento\FunctionalTestingFramework\Util\Path\UrlFormatter;
2526
use Magento\FunctionalTestingFramework\Util\ConfigSanitizerUtil;
2627
use Yandex\Allure\Adapter\AllureException;
@@ -584,7 +585,8 @@ public function magentoCLI($command, $timeout = null, $arguments = null)
584585
$response = $executor->read();
585586
$executor->close();
586587

587-
$response = trim($this->safeUtf8Conversion($response));
588+
$util = new ModuleUtils();
589+
$response = trim($util->utf8SafeControlCharacterTrim($response));
588590
return $response != "" ? $response : "CLI did not return output.";
589591
}
590592

@@ -1060,29 +1062,4 @@ public function pause($pauseOnFail = false)
10601062

10611063
$this->codeceptPause();
10621064
}
1063-
1064-
/**
1065-
* Return UTF-8 encoding string with control/invisible characters removed, return original string on error.
1066-
*
1067-
* @param string $input
1068-
* @return string
1069-
*/
1070-
private function safeUtf8Conversion(string $input): string
1071-
{
1072-
// Convert $input string to UTF-8 encoding
1073-
$convInput = iconv("ISO-8859-1", "UTF-8//IGNORE", $input);
1074-
if ($convInput !== false) {
1075-
// Remove invisible control characters, unused code points and replacement character
1076-
// so that they don't break xml test results for Allure
1077-
$cleanInput = preg_replace('/[^\PC\s]|\x{FFFD}/u', '', $convInput);
1078-
if ($cleanInput !== null) {
1079-
return $cleanInput;
1080-
} else {
1081-
$err = preg_last_error_msg();
1082-
print("MagentoCLI response preg_replace() with error $err.\n");
1083-
}
1084-
}
1085-
1086-
return $input;
1087-
}
10881065
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Magento\FunctionalTestingFramework\Module\Util;
4+
5+
class ModuleUtils
6+
{
7+
/**
8+
* Module util function that returns UTF-8 encoding string with control/invisible characters removed,
9+
* and it returns the original string when on error.
10+
*
11+
* @param string $input
12+
* @return string
13+
*/
14+
public function utf8SafeControlCharacterTrim(string $input): string
15+
{
16+
// Convert $input string to UTF-8 encoding
17+
$convInput = iconv("ISO-8859-1", "UTF-8//IGNORE", $input);
18+
if ($convInput !== false) {
19+
// Remove invisible control characters, unused code points and replacement character
20+
// so that they don't break xml test results for Allure
21+
$cleanInput = preg_replace('/[^\PC\s]|\x{FFFD}/u', '', $convInput);
22+
if ($cleanInput !== null) {
23+
return $cleanInput;
24+
} else {
25+
$err = preg_last_error_msg();
26+
print("MagentoCLI response preg_replace() with error $err.\n");
27+
}
28+
}
29+
30+
return $input;
31+
}
32+
}

0 commit comments

Comments
 (0)