65552cf5121da12b45a9644ff52535188aa986b7
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Serialization / JsonTest.php
1 <?php
2
3 namespace Drupal\Tests\Component\Serialization;
4
5 use Drupal\Component\Serialization\Json;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * @coversDefaultClass \Drupal\Component\Serialization\Json
10  * @group Serialization
11  */
12 class JsonTest extends UnitTestCase {
13
14   /**
15    * A test string with the full ASCII table.
16    *
17    * @var string
18    */
19   protected $string;
20
21   /**
22    * An array of unsafe html characters which has to be encoded.
23    *
24    * @var array
25    */
26   protected $htmlUnsafe;
27
28   /**
29    * An array of unsafe html characters which are already escaped.
30    *
31    * @var array
32    */
33   protected $htmlUnsafeEscaped;
34
35
36   /**
37    * {@inheritdoc}
38    */
39   protected function setUp() {
40     parent::setUp();
41
42     // Setup a string with the full ASCII table.
43     // @todo: Add tests for non-ASCII characters and Unicode.
44     $this->string = '';
45     for ($i = 1; $i < 128; $i++) {
46       $this->string .= chr($i);
47     }
48
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'];
54   }
55
56   /**
57    * Tests encoding for every ASCII character.
58    */
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));
64     }
65   }
66
67   /**
68    * Tests encoding length.
69    */
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.');
74   }
75
76   /**
77    * Tests end and start of the encoded string.
78    */
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 ".');
85   }
86
87   /**
88    * Tests converting PHP variables to JSON strings and back.
89    */
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.');
95   }
96
97   /**
98    * Test the reversibility of structured data
99    */
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));
107     }
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));
111     }
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.');
115   }
116
117 }