annotate core/tests/Drupal/Tests/Component/Serialization/JsonTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\Component\Serialization;
Chris@0 4
Chris@0 5 use Drupal\Component\Serialization\Json;
Chris@0 6 use PHPUnit\Framework\TestCase;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * @coversDefaultClass \Drupal\Component\Serialization\Json
Chris@0 10 * @group Serialization
Chris@0 11 */
Chris@0 12 class JsonTest extends TestCase {
Chris@0 13
Chris@0 14 /**
Chris@0 15 * A test string with the full ASCII table.
Chris@0 16 *
Chris@0 17 * @var string
Chris@0 18 */
Chris@0 19 protected $string;
Chris@0 20
Chris@0 21 /**
Chris@0 22 * An array of unsafe html characters which has to be encoded.
Chris@0 23 *
Chris@0 24 * @var array
Chris@0 25 */
Chris@0 26 protected $htmlUnsafe;
Chris@0 27
Chris@0 28 /**
Chris@0 29 * An array of unsafe html characters which are already escaped.
Chris@0 30 *
Chris@0 31 * @var array
Chris@0 32 */
Chris@0 33 protected $htmlUnsafeEscaped;
Chris@0 34
Chris@0 35 /**
Chris@0 36 * {@inheritdoc}
Chris@0 37 */
Chris@0 38 protected function setUp() {
Chris@0 39 parent::setUp();
Chris@0 40
Chris@0 41 // Setup a string with the full ASCII table.
Chris@0 42 // @todo: Add tests for non-ASCII characters and Unicode.
Chris@0 43 $this->string = '';
Chris@0 44 for ($i = 1; $i < 128; $i++) {
Chris@0 45 $this->string .= chr($i);
Chris@0 46 }
Chris@0 47
Chris@0 48 // Characters that must be escaped.
Chris@0 49 // We check for unescaped " separately.
Chris@0 50 $this->htmlUnsafe = ['<', '>', '\'', '&'];
Chris@0 51 // The following are the encoded forms of: < > ' & "
Chris@0 52 $this->htmlUnsafeEscaped = ['\u003C', '\u003E', '\u0027', '\u0026', '\u0022'];
Chris@0 53 }
Chris@0 54
Chris@0 55 /**
Chris@0 56 * Tests encoding for every ASCII character.
Chris@0 57 */
Chris@0 58 public function testEncodingAscii() {
Chris@0 59 // Verify there aren't character encoding problems with the source string.
Chris@14 60 $this->assertSame(127, strlen($this->string), 'A string with the full ASCII table has the correct length.');
Chris@0 61 foreach ($this->htmlUnsafe as $char) {
Chris@0 62 $this->assertTrue(strpos($this->string, $char) > 0, sprintf('A string with the full ASCII table includes %s.', $char));
Chris@0 63 }
Chris@0 64 }
Chris@0 65
Chris@0 66 /**
Chris@0 67 * Tests encoding length.
Chris@0 68 */
Chris@0 69 public function testEncodingLength() {
Chris@0 70 // Verify that JSON encoding produces a string with all of the characters.
Chris@0 71 $json = Json::encode($this->string);
Chris@0 72 $this->assertTrue(strlen($json) > strlen($this->string), 'A JSON encoded string is larger than the source string.');
Chris@0 73 }
Chris@0 74
Chris@0 75 /**
Chris@0 76 * Tests end and start of the encoded string.
Chris@0 77 */
Chris@0 78 public function testEncodingStartEnd() {
Chris@0 79 $json = Json::encode($this->string);
Chris@0 80 // The first and last characters should be ", and no others.
Chris@0 81 $this->assertTrue($json[0] == '"', 'A JSON encoded string begins with ".');
Chris@0 82 $this->assertTrue($json[strlen($json) - 1] == '"', 'A JSON encoded string ends with ".');
Chris@0 83 $this->assertTrue(substr_count($json, '"') == 2, 'A JSON encoded string contains exactly two ".');
Chris@0 84 }
Chris@0 85
Chris@0 86 /**
Chris@0 87 * Tests converting PHP variables to JSON strings and back.
Chris@0 88 */
Chris@0 89 public function testReversibility() {
Chris@0 90 $json = Json::encode($this->string);
Chris@0 91 // Verify that encoding/decoding is reversible.
Chris@0 92 $json_decoded = Json::decode($json);
Chris@0 93 $this->assertSame($this->string, $json_decoded, 'Encoding a string to JSON and decoding back results in the original string.');
Chris@0 94 }
Chris@0 95
Chris@0 96 /**
Chris@0 97 * Test the reversibility of structured data
Chris@0 98 */
Chris@0 99 public function testStructuredReversibility() {
Chris@0 100 // Verify reversibility for structured data. Also verify that necessary
Chris@0 101 // characters are escaped.
Chris@0 102 $source = [TRUE, FALSE, 0, 1, '0', '1', $this->string, ['key1' => $this->string, 'key2' => ['nested' => TRUE]]];
Chris@0 103 $json = Json::encode($source);
Chris@0 104 foreach ($this->htmlUnsafe as $char) {
Chris@0 105 $this->assertTrue(strpos($json, $char) === FALSE, sprintf('A JSON encoded string does not contain %s.', $char));
Chris@0 106 }
Chris@0 107 // Verify that JSON encoding escapes the HTML unsafe characters
Chris@0 108 foreach ($this->htmlUnsafeEscaped as $char) {
Chris@0 109 $this->assertTrue(strpos($json, $char) > 0, sprintf('A JSON encoded string contains %s.', $char));
Chris@0 110 }
Chris@0 111 $json_decoded = Json::decode($json);
Chris@0 112 $this->assertNotSame($source, $json, 'An array encoded in JSON is identical to the source.');
Chris@0 113 $this->assertSame($source, $json_decoded, 'Encoding structured data to JSON and decoding back not results in the original data.');
Chris@0 114 }
Chris@0 115
Chris@0 116 }