1 common.test CommonJSONUnitTestCase::testJSON()

Tests converting PHP variables to JSON strings and back.

File

core/modules/simpletest/tests/common.test, line 3126
Tests for common.inc functionality.

Class

CommonJSONUnitTestCase
Tests the backdrop_json_encode() and backdrop_json_decode() functions.

Code

function testJSON() {
  // Setup a string with the full ASCII table.
  $str = '';
  for ($i = 0; $i < 128; $i++) {
    $str .= chr($i);
  }
  // Characters that must be escaped.
  // We check for unescaped " separately.
  $html_unsafe = array('<', '>', '\'', '&');
  // The following are the encoded forms of: < > ' & "
  $html_unsafe_escaped = array('\u003C', '\u003E', '\u0027', '\u0026', '\u0022');

  // Verify there aren't character encoding problems with the source string.
  $this->assertIdentical(strlen($str), 128, 'A string with the full ASCII table has the correct length.');
  foreach ($html_unsafe as $char) {
    $this->assertTrue(strpos($str, $char) > 0, format_string('A string with the full ASCII table includes @s.', array('@s' => $char)));
  }

  // Verify that JSON encoding produces a string with all of the characters.
  $json = backdrop_json_encode($str);
  $this->assertTrue(strlen($json) > strlen($str), 'A JSON encoded string is larger than the source string.');

  // The first and last characters should be ", and no others.
  $this->assertTrue($json[0] == '"', 'A JSON encoded string begins with ".');
  $this->assertTrue($json[strlen($json) - 1] == '"', 'A JSON encoded string ends with ".');
  $this->assertTrue(substr_count($json, '"') == 2, 'A JSON encoded string contains exactly two ".');

  // Verify that encoding/decoding is reversible.
  $json_decoded = backdrop_json_decode($json);
  $this->assertIdentical($str, $json_decoded, 'Encoding a string to JSON and decoding back results in the original string.');

  // Verify reversibility for structured data. Also verify that necessary
  // characters are escaped.
  $source = array(TRUE, FALSE, 0, 1, '0', '1', '[ ]', '\'', "\"", '\\', $str, array('key1' => $str, 'key2' => array('nested' => TRUE)));
  $json = backdrop_json_encode($source);
  foreach ($html_unsafe as $char) {
    $this->assertTrue(strpos($json, $char) === FALSE, format_string('A JSON encoded string does not contain @s.', array('@s' => $char)));
  }
  // Verify that JSON encoding escapes the HTML unsafe characters.
  foreach ($html_unsafe_escaped as $char) {
    $this->assertTrue(strpos($json, $char) > 0, format_string('A JSON encoded string contains @s.', array('@s' => $char)));
  }
  $json_decoded = backdrop_json_decode($json);
  $this->assertNotIdentical($source, $json, 'An array encoded in JSON is not identical to the source.');
  $this->assertIdentical($source, $json_decoded, 'Encoding structured data to JSON and decoding back results in the original data.');

  // Do an additional check on structured data when pretty printing.
  $json = backdrop_json_encode($source, TRUE);
  $json_decoded = backdrop_json_decode($json);
  $this->assertIdentical($source, $json_decoded, 'Encoding structured data to pretty-printed JSON and decoding back results in the original data.');

  // Unicode test strings.
  // cspell:disable-next-line
  $unicode_decoded = 'üéåâ';
  $unicode_encoded = '\u00fc\u00e9\u00e5\u00e2';
  $unicode_encoded_escaped = '\\\\u00fc\\\\u00e9\\\\u00e5\\\\u00e2';

  // Verify that Unicode characters are not encoded when using pretty print.
  $this->assertIdentical('"' . $unicode_decoded . '"', backdrop_json_encode($unicode_decoded, TRUE), 'UTF-8 characters are not encoded in pretty print JSON.');
  $this->assertIdentical('"' . $unicode_encoded_escaped . '"', backdrop_json_encode($unicode_encoded, TRUE), 'Escaped Unicode characters are double-escaped in pretty print JSON.');

  // Non-pretty print JSON uses encoded characters.
  $this->assertIdentical('"' . $unicode_encoded . '"', backdrop_json_encode($unicode_decoded), 'UTF-8 characters are Unicode encoded in non-pretty JSON.');
  $this->assertIdentical('"' . $unicode_encoded_escaped . '"', backdrop_json_encode($unicode_encoded), 'Escaped Unicode characters are double-escaped in non-pretty JSON.');

  // Verify reversibility of Unicode characters (pretty print).
  $this->assertIdentical($unicode_decoded, backdrop_json_decode(backdrop_json_encode($unicode_decoded, TRUE)), 'UTF-8 characters escaped and then unescaped matches original.');
  $this->assertIdentical($unicode_encoded, backdrop_json_decode(backdrop_json_encode($unicode_encoded, TRUE)), 'Unicode characters escaped and then unescaped matches original.');
  $this->assertIdentical($unicode_encoded_escaped, backdrop_json_decode(backdrop_json_encode($unicode_encoded_escaped, TRUE)), 'Escaped Unicode characters escaped and then unescaped matches original.');

  // Verify reversibility of Unicode characters (non-pretty print).
  $this->assertIdentical($unicode_decoded, backdrop_json_decode(backdrop_json_encode($unicode_decoded)), 'UTF-8 characters escaped and then unescaped matches original.');
  $this->assertIdentical($unicode_encoded, backdrop_json_decode(backdrop_json_encode($unicode_encoded)), 'Unicode characters escaped and then unescaped matches original.');
  $this->assertIdentical($unicode_encoded_escaped, backdrop_json_decode(backdrop_json_encode($unicode_encoded_escaped)), 'Escaped Unicode characters escaped and then unescaped matches original.');
}