function JsonEncoderTest::testStructuredDataSmokeTest
Simple structured data smoke test.
This verifies that the encoder works for nested arrays and that invalid UTF-8 inside the structure is still handled correctly.
File
-
core/
modules/ serialization/ tests/ src/ Unit/ Encoder/ JsonEncoderTest.php, line 90
Class
Namespace
Drupal\Tests\serialization\Unit\EncoderCode
public function testStructuredDataSmokeTest() : void {
$data = [
'title' => 'Example',
'body' => "Content with invalid UTF-8: \x80",
'metadata' => [
'tags' => [
'one',
'two',
],
],
];
$encoded = $this->encoder
->encode($data, 'json');
$this->assertJson($encoded, 'Structured data should produce valid JSON.');
$this->assertStringContainsString('\\ufffd', $encoded, 'Invalid UTF-8 in nested data should be replaced.');
$decoded = json_decode($encoded, TRUE);
$this->assertSame(JSON_ERROR_NONE, json_last_error(), 'Structured data should be decodable.');
$this->assertIsArray($decoded, 'Decoded data should be an array.');
$this->assertArrayHasKey('title', $decoded, 'Decoded data should have title key.');
$this->assertArrayHasKey('metadata', $decoded, 'Decoded data should have metadata key.');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.