annotate core/tests/Drupal/Tests/Component/Serialization/JsonTest.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
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 /**
Chris@0 37 * {@inheritdoc}
Chris@0 38 */
Chris@0 39 protected function setUp() {
Chris@0 40 parent::setUp();
Chris@0 41
Chris@0 42 // Setup a string with the full ASCII table.
Chris@0 43 // @todo: Add tests for non-ASCII characters and Unicode.
Chris@0 44 $this->string = '';
Chris@0 45 for ($i = 1; $i < 128; $i++) {
Chris@0 46 $this->string .= chr($i);
Chris@0 47 }
Chris@0 48
Chris@0 49 // Characters that must be escaped.
Chris@0 50 // We check for unescaped " separately.
Chris@0 51 $this->htmlUnsafe = ['<', '>', '\'', '&'];
Chris@0 52 // The following are the encoded forms of: < > ' & "
Chris@0 53 $this->htmlUnsafeEscaped = ['\u003C', '\u003E', '\u0027', '\u0026', '\u0022'];
Chris@0 54 }
Chris@0 55
Chris@0 56 /**
Chris@0 57 * Tests encoding for every ASCII character.
Chris@0 58 */
Chris@0 59 public function testEncodingAscii() {
Chris@0 60 // Verify there aren't character encoding problems with the source string.
Chris@0 61 $this->assertSame(127, strlen($this->string), 'A string with the full ASCII table has the correct length.');
Chris@0 62 foreach ($this->htmlUnsafe as $char) {
Chris@0 63 $this->assertTrue(strpos($this->string, $char) > 0, sprintf('A string with the full ASCII table includes %s.', $char));
Chris@0 64 }
Chris@0 65 }
Chris@0 66
Chris@0 67 /**
Chris@0 68 * Tests encoding length.
Chris@0 69 */
Chris@0 70 public function testEncodingLength() {
Chris@0 71 // Verify that JSON encoding produces a string with all of the characters.
Chris@0 72 $json = Json::encode($this->string);
Chris@0 73 $this->assertTrue(strlen($json) > strlen($this->string), 'A JSON encoded string is larger than the source string.');
Chris@0 74 }
Chris@0 75
Chris@0 76 /**
Chris@0 77 * Tests end and start of the encoded string.
Chris@0 78 */
Chris@0 79 public function testEncodingStartEnd() {
Chris@0 80 $json = Json::encode($this->string);
Chris@0 81 // The first and last characters should be ", and no others.
Chris@0 82 $this->assertTrue($json[0] == '"', 'A JSON encoded string begins with ".');
Chris@0 83 $this->assertTrue($json[strlen($json) - 1] == '"', 'A JSON encoded string ends with ".');
Chris@0 84 $this->assertTrue(substr_count($json, '"') == 2, 'A JSON encoded string contains exactly two ".');
Chris@0 85 }
Chris@0 86
Chris@0 87 /**
Chris@0 88 * Tests converting PHP variables to JSON strings and back.
Chris@0 89 */
Chris@0 90 public function testReversibility() {
Chris@0 91 $json = Json::encode($this->string);
Chris@0 92 // Verify that encoding/decoding is reversible.
Chris@0 93 $json_decoded = Json::decode($json);
Chris@0 94 $this->assertSame($this->string, $json_decoded, 'Encoding a string to JSON and decoding back results in the original string.');
Chris@0 95 }
Chris@0 96
Chris@0 97 /**
Chris@0 98 * Test the reversibility of structured data
Chris@0 99 */
Chris@0 100 public function testStructuredReversibility() {
Chris@0 101 // Verify reversibility for structured data. Also verify that necessary
Chris@0 102 // characters are escaped.
Chris@0 103 $source = [TRUE, FALSE, 0, 1, '0', '1', $this->string, ['key1' => $this->string, 'key2' => ['nested' => TRUE]]];
Chris@0 104 $json = Json::encode($source);
Chris@0 105 foreach ($this->htmlUnsafe as $char) {
Chris@0 106 $this->assertTrue(strpos($json, $char) === FALSE, sprintf('A JSON encoded string does not contain %s.', $char));
Chris@0 107 }
Chris@0 108 // Verify that JSON encoding escapes the HTML unsafe characters
Chris@0 109 foreach ($this->htmlUnsafeEscaped as $char) {
Chris@0 110 $this->assertTrue(strpos($json, $char) > 0, sprintf('A JSON encoded string contains %s.', $char));
Chris@0 111 }
Chris@0 112 $json_decoded = Json::decode($json);
Chris@0 113 $this->assertNotSame($source, $json, 'An array encoded in JSON is identical to the source.');
Chris@0 114 $this->assertSame($source, $json_decoded, 'Encoding structured data to JSON and decoding back not results in the original data.');
Chris@0 115 }
Chris@0 116
Chris@0 117 }