Skip to content

Commit a417a28

Browse files
authored
Merge pull request #175 from magento-commerce/MQE-3325
MQE-3325: Invalid UTF-8 chars returned from magentoCLI break Allure reporting
2 parents e348160 + e50871e commit a417a28

File tree

5 files changed

+93
-4
lines changed

5 files changed

+93
-4
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"ext-intl": "*",
1616
"ext-json": "*",
1717
"ext-openssl": "*",
18+
"ext-iconv": "*",
1819
"allure-framework/allure-codeception": "^1.4",
1920
"aws/aws-sdk-php": "^3.132",
2021
"codeception/codeception": "^4.1",

composer.lock

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
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: 4 additions & 1 deletion
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,9 @@ public function magentoCLI($command, $timeout = null, $arguments = null)
584585
$response = $executor->read();
585586
$executor->close();
586587

587-
return $response;
588+
$util = new ModuleUtils();
589+
$response = trim($util->utf8SafeControlCharacterTrim($response));
590+
return $response != "" ? $response : "CLI did not return output.";
588591
}
589592

590593
/**
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\FunctionalTestingFramework\Module\Util;
8+
9+
class ModuleUtils
10+
{
11+
/**
12+
* Module util function that returns UTF-8 encoding string with control/invisible characters removed,
13+
* and it returns the original string when on error.
14+
*
15+
* @param string $input
16+
* @return string
17+
*/
18+
public function utf8SafeControlCharacterTrim(string $input): string
19+
{
20+
// Convert $input string to UTF-8 encoding
21+
$convInput = iconv("ISO-8859-1", "UTF-8//IGNORE", $input);
22+
if ($convInput !== false) {
23+
// Remove invisible control characters, unused code points and replacement character
24+
// so that they don't break xml test results for Allure
25+
$cleanInput = preg_replace('/[^\PC\s]|\x{FFFD}/u', '', $convInput);
26+
if ($cleanInput !== null) {
27+
return $cleanInput;
28+
} else {
29+
$err = preg_last_error_msg();
30+
print("MagentoCLI response preg_replace() with error $err.\n");
31+
}
32+
}
33+
34+
return $input;
35+
}
36+
}

0 commit comments

Comments
 (0)