function JsonEncoderTest::testInheritsControlCharacterHandling

Tests JSON:API encoder inherits control character handling.

This verifies that the JSON:API encoder correctly inherits the JSON_INVALID_UTF8_SUBSTITUTE flag from the parent serialization encoder. The comprehensive tests for this functionality are in the serialization module's JsonEncoderTest.

See also

\Drupal\Tests\serialization\Unit\Encoder\JsonEncoderTest

https://www.drupal.org/project/drupal/issues/3549107

File

core/modules/jsonapi/tests/src/Unit/Encoder/JsonEncoderTest.php, line 54

Class

JsonEncoderTest
Tests the JSON:API encoder.

Namespace

Drupal\Tests\jsonapi\Unit\Encoder

Code

public function testInheritsControlCharacterHandling() : void {
  // Test that invalid UTF-8 is handled (would fail without the flag).
  $input = "Test\x80Data";
  $encoded = $this->encoder
    ->encode($input, 'api_json');
  // Verify it's valid JSON.
  $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.