3 namespace Drupal\Tests\Component\Serialization;
5 use Drupal\Component\Serialization\Json;
6 use Drupal\Tests\UnitTestCase;
9 * @coversDefaultClass \Drupal\Component\Serialization\Json
10 * @group Serialization
12 class JsonTest extends UnitTestCase {
15 * A test string with the full ASCII table.
22 * An array of unsafe html characters which has to be encoded.
26 protected $htmlUnsafe;
29 * An array of unsafe html characters which are already escaped.
33 protected $htmlUnsafeEscaped;
39 protected function setUp() {
42 // Setup a string with the full ASCII table.
43 // @todo: Add tests for non-ASCII characters and Unicode.
45 for ($i = 1; $i < 128; $i++) {
46 $this->string .= chr($i);
49 // Characters that must be escaped.
50 // We check for unescaped " separately.
51 $this->htmlUnsafe = ['<', '>', '\'', '&'];
52 // The following are the encoded forms of: < > ' & "
53 $this->htmlUnsafeEscaped = ['\u003C', '\u003E', '\u0027', '\u0026', '\u0022'];
57 * Tests encoding for every ASCII character.
59 public function testEncodingAscii() {
60 // Verify there aren't character encoding problems with the source string.
61 $this->assertSame(strlen($this->string), 127, 'A string with the full ASCII table has the correct length.');
62 foreach ($this->htmlUnsafe as $char) {
63 $this->assertTrue(strpos($this->string, $char) > 0, sprintf('A string with the full ASCII table includes %s.', $char));
68 * Tests encoding length.
70 public function testEncodingLength() {
71 // Verify that JSON encoding produces a string with all of the characters.
72 $json = Json::encode($this->string);
73 $this->assertTrue(strlen($json) > strlen($this->string), 'A JSON encoded string is larger than the source string.');
77 * Tests end and start of the encoded string.
79 public function testEncodingStartEnd() {
80 $json = Json::encode($this->string);
81 // The first and last characters should be ", and no others.
82 $this->assertTrue($json[0] == '"', 'A JSON encoded string begins with ".');
83 $this->assertTrue($json[strlen($json) - 1] == '"', 'A JSON encoded string ends with ".');
84 $this->assertTrue(substr_count($json, '"') == 2, 'A JSON encoded string contains exactly two ".');
88 * Tests converting PHP variables to JSON strings and back.
90 public function testReversibility() {
91 $json = Json::encode($this->string);
92 // Verify that encoding/decoding is reversible.
93 $json_decoded = Json::decode($json);
94 $this->assertSame($this->string, $json_decoded, 'Encoding a string to JSON and decoding back results in the original string.');
98 * Test the reversibility of structured data
100 public function testStructuredReversibility() {
101 // Verify reversibility for structured data. Also verify that necessary
102 // characters are escaped.
103 $source = [TRUE, FALSE, 0, 1, '0', '1', $this->string, ['key1' => $this->string, 'key2' => ['nested' => TRUE]]];
104 $json = Json::encode($source);
105 foreach ($this->htmlUnsafe as $char) {
106 $this->assertTrue(strpos($json, $char) === FALSE, sprintf('A JSON encoded string does not contain %s.', $char));
108 // Verify that JSON encoding escapes the HTML unsafe characters
109 foreach ($this->htmlUnsafeEscaped as $char) {
110 $this->assertTrue(strpos($json, $char) > 0, sprintf('A JSON encoded string contains %s.', $char));
112 $json_decoded = Json::decode($json);
113 $this->assertNotSame($source, $json, 'An array encoded in JSON is identical to the source.');
114 $this->assertSame($source, $json_decoded, 'Encoding structured data to JSON and decoding back not results in the original data.');