function JsonEncoderTest::testEncodeInvalidUtf8IsSubstituted
Tests that invalid UTF-8 is handled via JSON_INVALID_UTF8_SUBSTITUTE.
See also
https://www.drupal.org/project/drupal/issues/3549107
File
-
core/
modules/ serialization/ tests/ src/ Unit/ Encoder/ JsonEncoderTest.php, line 46
Class
Namespace
Drupal\Tests\serialization\Unit\EncoderCode
public function testEncodeInvalidUtf8IsSubstituted() : void {
// A representative invalid UTF-8 sequence that would previously cause
// json_encode() to fail.
$input = "Test\x80Data";
$encoded = $this->encoder
->encode($input, 'json');
// Verify it's valid JSON (would fail without the flag).
$this->assertJson($encoded, 'Encoded output should be valid JSON even with invalid UTF-8.');
// Verify the replacement character is present.
$this->assertStringContainsString('\\ufffd', $encoded, 'Invalid UTF-8 should be replaced with U+FFFD.');
// Verify it can be decoded.
json_decode($encoded);
$this->assertSame(JSON_ERROR_NONE, json_last_error(), 'Encoded JSON should be decodable without errors.');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.