Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
D
drupal
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Custom Issue Tracker
Custom Issue Tracker
Labels
Merge Requests
292
Merge Requests
292
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Analytics
Analytics
Code Review
Insights
Issue
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
project
drupal
Commits
264d1d97
Commit
264d1d97
authored
Jun 27, 2013
by
catch
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue
#2016299
by dawehner, jhedstrom: Convert system module's JsonUnitTest to phpunit.
parent
a3fea498
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
188 additions
and
77 deletions
+188
-77
core/includes/common.inc
core/includes/common.inc
+7
-3
core/lib/Drupal/Component/Utility/Json.php
core/lib/Drupal/Component/Utility/Json.php
+50
-0
core/modules/system/lib/Drupal/system/Tests/Common/JsonUnitTest.php
...es/system/lib/Drupal/system/Tests/Common/JsonUnitTest.php
+0
-74
core/tests/Drupal/Tests/Component/Utility/JsonTest.php
core/tests/Drupal/Tests/Component/Utility/JsonTest.php
+131
-0
No files found.
core/includes/common.inc
View file @
264d1d97
<?php
use
Drupal\Component\Utility\Crypt
;
use
Drupal\Component\Utility\Json
;
use
Drupal\Component\Utility\String
;
use
Drupal\Component\Utility\Tags
;
use
Drupal\Component\Utility\UrlValidator
;
...
...
@@ -3900,10 +3901,11 @@ function drupal_clear_js_cache() {
*
* @see drupal_json_decode()
* @ingroup php_wrappers
* @deprecated as of Drupal 8.0. Use Drupal\Component\Utility\Json::encode()
* directly instead.
*/
function
drupal_json_encode
(
$var
)
{
// Encode <, >, ', &, and " using the json_encode() options parameter.
return
json_encode
(
$var
,
JSON_HEX_TAG
|
JSON_HEX_APOS
|
JSON_HEX_AMP
|
JSON_HEX_QUOT
);
return
Json
::
encode
(
$var
);
}
/**
...
...
@@ -3911,9 +3913,11 @@ function drupal_json_encode($var) {
*
* @see drupal_json_encode()
* @ingroup php_wrappers
* @deprecated as of Drupal 8.0. Use Drupal\Component\Utility\Json::decode()
* directly instead.
*/
function
drupal_json_decode
(
$var
)
{
return
json_decode
(
$var
,
TRUE
);
return
Json
::
decode
(
$var
);
}
/**
...
...
core/lib/Drupal/Component/Utility/Json.php
0 → 100644
View file @
264d1d97
<?php
/**
* @file
* Contains \Drupal\Component\Utility\Json.
*/
namespace
Drupal\Component\Utility
;
/**
* Provides helpers for dealing with json.
*/
class
Json
{
/**
* Converts a PHP variable into its JavaScript equivalent.
*
* We use HTML-safe strings, with several characters escaped.
*
* @param mixed $variable
* The variable to encode.
*
* @return string
* Returns the encoded variable.
*
* @see drupal_json_decode()
* @ingroup php_wrappers
*/
public
static
function
encode
(
$variable
)
{
// Encode <, >, ', &, and " using the json_encode() options parameter.
return
json_encode
(
$variable
,
JSON_HEX_TAG
|
JSON_HEX_APOS
|
JSON_HEX_AMP
|
JSON_HEX_QUOT
);
}
/**
* Converts an HTML-safe JSON string into its PHP equivalent.
*
* @param string $string
* The string to decode.
*
* @return mixed
* Returns the decoded string.
*
* @see drupal_json_encode()
* @ingroup php_wrappers
*/
public
static
function
decode
(
$string
)
{
return
json_decode
(
$string
,
TRUE
);
}
}
core/modules/system/lib/Drupal/system/Tests/Common/JsonUnitTest.php
deleted
100644 → 0
View file @
a3fea498
<?php
/**
* @file
* Definition of Drupal\system\Tests\Common\JsonUnitTest.
*/
namespace
Drupal\system\Tests\Common
;
use
Drupal\simpletest\UnitTestBase
;
/**
* Tests the drupal_json_encode() and drupal_json_decode() functions.
*/
class
JsonUnitTest
extends
UnitTestBase
{
public
static
function
getInfo
()
{
return
array
(
'name'
=>
'JSON'
,
'description'
=>
'Tests the drupal_json_encode() and drupal_json_decode() functions to convert PHP variables to JSON strings and back.'
,
'group'
=>
'Common'
,
);
}
/**
* Tests converting PHP variables to JSON strings and back.
*/
function
testJSON
()
{
// Setup a string with the full ASCII table.
// @todo: Add tests for non-ASCII characters and Unicode.
$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
=
drupal_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
=
drupal_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
=
drupal_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
=
drupal_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.'
);
}
}
core/tests/Drupal/Tests/Component/Utility/JsonTest.php
0 → 100644
View file @
264d1d97
<?php
/**
* @file
* Contains \Drupal\Tests\Component\Utility\JsonTest.
*/
namespace
Drupal\Tests\Component\Utility
;
use
Drupal\Component\Utility\Json
;
use
Drupal\Tests\UnitTestCase
;
/**
* Tests the Json::encode() and Json::decode() functions.
*
* @see \Drupal\Component\Utility\Json
*/
class
JsonTest
extends
UnitTestCase
{
/**
* A test string with the full ASCII table.
*
* @var string
*/
protected
$string
;
/**
* An array of unsafe html characters which has to be encoded.
*
* @var array
*/
protected
$htmlUnsafe
;
/**
* An array of unsafe html characters which are already escaped.
*
* @var array
*/
protected
$htmlUnsafeEscaped
;
public
static
function
getInfo
()
{
return
array
(
'name'
=>
'JSON'
,
'description'
=>
'Tests the Json::encode() and Json::decode() functions to convert PHP variables to JSON strings and back.'
,
'group'
=>
'Common'
,
);
}
/**
* {@inheritdoc}
*/
protected
function
setUp
()
{
parent
::
setUp
();
// Setup a string with the full ASCII table.
// @todo: Add tests for non-ASCII characters and Unicode.
$this
->
string
=
''
;
for
(
$i
=
0
;
$i
<
128
;
$i
++
)
{
$this
->
string
.
=
chr
(
$i
);
}
// Characters that must be escaped.
// We check for unescaped " separately.
$this
->
htmlUnsafe
=
array
(
'<'
,
'>'
,
'\''
,
'&'
);
// The following are the encoded forms of: < > ' & "
$this
->
htmlUnsafeEscaped
=
array
(
'\u003C'
,
'\u003E'
,
'\u0027'
,
'\u0026'
,
'\u0022'
);
}
/**
* Tests encoding for every ASCII character.
*/
public
function
testEncodingAscii
()
{
// Verify there aren't character encoding problems with the source string.
$this
->
assertSame
(
strlen
(
$this
->
string
),
128
,
'A string with the full ASCII table has the correct length.'
);
foreach
(
$this
->
htmlUnsafe
as
$char
)
{
$this
->
assertTrue
(
strpos
(
$this
->
string
,
$char
)
>
0
,
sprintf
(
'A string with the full ASCII table includes %s.'
,
$char
));
}
}
/**
* Tests encoding length.
*/
public
function
testEncodingLength
()
{
// Verify that JSON encoding produces a string with all of the characters.
$json
=
Json
::
encode
(
$this
->
string
);
$this
->
assertTrue
(
strlen
(
$json
)
>
strlen
(
$this
->
string
),
'A JSON encoded string is larger than the source string.'
);
}
/**
* Tests end and start of the encoded string.
*/
public
function
testEncodingStartEnd
()
{
$json
=
Json
::
encode
(
$this
->
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 ".'
);
}
/**
* Tests converting PHP variables to JSON strings and back.
*/
public
function
testReversibility
()
{
$json
=
Json
::
encode
(
$this
->
string
);
// Verify that encoding/decoding is reversible.
$json_decoded
=
Json
::
decode
(
$json
);
$this
->
assertSame
(
$this
->
string
,
$json_decoded
,
'Encoding a string to JSON and decoding back results in the original string.'
);
}
/**
* Test the reversibility of structured data
*/
public
function
testStructuredReversibility
()
{
// Verify reversibility for structured data. Also verify that necessary
// characters are escaped.
$source
=
array
(
TRUE
,
FALSE
,
0
,
1
,
'0'
,
'1'
,
$this
->
string
,
array
(
'key1'
=>
$this
->
string
,
'key2'
=>
array
(
'nested'
=>
TRUE
)));
$json
=
Json
::
encode
(
$source
);
foreach
(
$this
->
htmlUnsafe
as
$char
)
{
$this
->
assertTrue
(
strpos
(
$json
,
$char
)
===
FALSE
,
sprintf
(
'A JSON encoded string does not contain %s.'
,
$char
));
}
// Verify that JSON encoding escapes the HTML unsafe characters
foreach
(
$this
->
htmlUnsafeEscaped
as
$char
)
{
$this
->
assertTrue
(
strpos
(
$json
,
$char
)
>
0
,
sprintf
(
'A JSON encoded string contains %s.'
,
$char
));
}
$json_decoded
=
Json
::
decode
(
$json
);
$this
->
assertNotSame
(
$source
,
$json
,
'An array encoded in JSON is identical to the source.'
);
$this
->
assertSame
(
$source
,
$json_decoded
,
'Encoding structured data to JSON and decoding back not results in the original data.'
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment