Chris@0: string = ''; Chris@0: for ($i = 1; $i < 128; $i++) { Chris@0: $this->string .= chr($i); Chris@0: } Chris@0: Chris@0: // Characters that must be escaped. Chris@0: // We check for unescaped " separately. Chris@0: $this->htmlUnsafe = ['<', '>', '\'', '&']; Chris@0: // The following are the encoded forms of: < > ' & " Chris@0: $this->htmlUnsafeEscaped = ['\u003C', '\u003E', '\u0027', '\u0026', '\u0022']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests encoding for every ASCII character. Chris@0: */ Chris@0: public function testEncodingAscii() { Chris@0: // Verify there aren't character encoding problems with the source string. Chris@14: $this->assertSame(127, strlen($this->string), 'A string with the full ASCII table has the correct length.'); Chris@0: foreach ($this->htmlUnsafe as $char) { Chris@0: $this->assertTrue(strpos($this->string, $char) > 0, sprintf('A string with the full ASCII table includes %s.', $char)); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests encoding length. Chris@0: */ Chris@0: public function testEncodingLength() { Chris@0: // Verify that JSON encoding produces a string with all of the characters. Chris@0: $json = Json::encode($this->string); Chris@0: $this->assertTrue(strlen($json) > strlen($this->string), 'A JSON encoded string is larger than the source string.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests end and start of the encoded string. Chris@0: */ Chris@0: public function testEncodingStartEnd() { Chris@0: $json = Json::encode($this->string); Chris@0: // The first and last characters should be ", and no others. Chris@0: $this->assertTrue($json[0] == '"', 'A JSON encoded string begins with ".'); Chris@0: $this->assertTrue($json[strlen($json) - 1] == '"', 'A JSON encoded string ends with ".'); Chris@0: $this->assertTrue(substr_count($json, '"') == 2, 'A JSON encoded string contains exactly two ".'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests converting PHP variables to JSON strings and back. Chris@0: */ Chris@0: public function testReversibility() { Chris@0: $json = Json::encode($this->string); Chris@0: // Verify that encoding/decoding is reversible. Chris@0: $json_decoded = Json::decode($json); Chris@0: $this->assertSame($this->string, $json_decoded, 'Encoding a string to JSON and decoding back results in the original string.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test the reversibility of structured data Chris@0: */ Chris@0: public function testStructuredReversibility() { Chris@0: // Verify reversibility for structured data. Also verify that necessary Chris@0: // characters are escaped. Chris@0: $source = [TRUE, FALSE, 0, 1, '0', '1', $this->string, ['key1' => $this->string, 'key2' => ['nested' => TRUE]]]; Chris@0: $json = Json::encode($source); Chris@0: foreach ($this->htmlUnsafe as $char) { Chris@0: $this->assertTrue(strpos($json, $char) === FALSE, sprintf('A JSON encoded string does not contain %s.', $char)); Chris@0: } Chris@0: // Verify that JSON encoding escapes the HTML unsafe characters Chris@0: foreach ($this->htmlUnsafeEscaped as $char) { Chris@0: $this->assertTrue(strpos($json, $char) > 0, sprintf('A JSON encoded string contains %s.', $char)); Chris@0: } Chris@0: $json_decoded = Json::decode($json); Chris@0: $this->assertNotSame($source, $json, 'An array encoded in JSON is identical to the source.'); Chris@0: $this->assertSame($source, $json_decoded, 'Encoding structured data to JSON and decoding back not results in the original data.'); Chris@0: } Chris@0: Chris@0: }