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
229
Merge Requests
229
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
9d1c11d6
Commit
9d1c11d6
authored
Sep 04, 2013
by
webchick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue
#2051717
by Mile23, jhedstrom: Expand phpunit tests for \Drupal\Component\Utility\Unicode.
parent
a75b2d56
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
120 additions
and
1 deletion
+120
-1
core/tests/Drupal/Tests/Component/Utility/UnicodeTest.php
core/tests/Drupal/Tests/Component/Utility/UnicodeTest.php
+120
-1
No files found.
core/tests/Drupal/Tests/Component/Utility/UnicodeTest.php
View file @
9d1c11d6
...
...
@@ -12,6 +12,11 @@
/**
* Test unicode handling features implemented in Unicode component.
*
* @see \Drupal\Component\Utility\Unicode
*
* @group Drupal
* @group Unicode
*/
class
UnicodeTest
extends
UnitTestCase
{
...
...
@@ -85,10 +90,11 @@ public function testMimeHeader($value, $encoded) {
public
function
providerTestMimeHeader
()
{
return
array
(
array
(
'tést.txt'
,
'=?UTF-8?B?dMOpc3QudHh0?='
),
// Simple ASCII characters.
array
(
'ASCII'
,
'ASCII'
),
);
}
/**
* Tests Unicode::strtolower().
*
...
...
@@ -193,6 +199,11 @@ public function providerUcfirst() {
* @dataProvider providerStrlen
*/
public
function
testStrlen
(
$text
,
$expected
)
{
// Run through multibyte code path.
Unicode
::
setStatus
(
Unicode
::
STATUS_MULTIBYTE
);
$this
->
assertEquals
(
$expected
,
Unicode
::
strlen
(
$text
));
// Run through singlebyte code path.
Unicode
::
setStatus
(
Unicode
::
STATUS_SINGLEBYTE
);
$this
->
assertEquals
(
$expected
,
Unicode
::
strlen
(
$text
));
}
...
...
@@ -217,6 +228,11 @@ public function providerStrlen() {
* @dataProvider providerSubstr
*/
public
function
testSubstr
(
$text
,
$start
,
$length
,
$expected
)
{
// Run through multibyte code path.
Unicode
::
setStatus
(
Unicode
::
STATUS_MULTIBYTE
);
$this
->
assertEquals
(
$expected
,
Unicode
::
substr
(
$text
,
$start
,
$length
));
// Run through singlebyte code path.
Unicode
::
setStatus
(
Unicode
::
STATUS_SINGLEBYTE
);
$this
->
assertEquals
(
$expected
,
Unicode
::
substr
(
$text
,
$start
,
$length
));
}
...
...
@@ -333,4 +349,107 @@ public function providerTruncate() {
);
}
/**
* Tests Unicode::truncateBytes().
*
* @param string $text
* The string to truncate.
* @param int $max_length
* The upper limit on the returned string length.
* @param string $expected
* The expected return from Unicode::truncateBytes().
*
* @dataProvider providerTestTruncateBytes
*/
public
function
testTruncateBytes
(
$text
,
$max_length
,
$expected
)
{
$this
->
assertEquals
(
$expected
,
Unicode
::
truncateBytes
(
$text
,
$max_length
),
'The string was not correctly truncated.'
);
}
/**
* Provides data for self::testTruncateBytes().
*
* @return array
* An array of arrays, each containing the parameters to
* self::testTruncateBytes().
*/
public
function
providerTestTruncateBytes
()
{
return
array
(
// String shorter than max length.
array
(
'Short string'
,
42
,
'Short string'
),
// Simple string longer than max length.
array
(
'Longer string than previous.'
,
10
,
'Longer str'
),
// Unicode.
array
(
'以呂波耳・ほへとち。リヌルヲ。'
,
10
,
'以呂波'
),
);
}
/**
* Tests Unicode::validateUtf8().
*
* @param string $text
* The text to validate.
* @param boolean $expected
* The expected return value from Unicode::validateUtf8().
* @param string $message
* The message to display on failure.
*
* @dataProvider providerTestValidateUtf8
*/
public
function
testValidateUtf8
(
$text
,
$expected
,
$message
)
{
$this
->
assertEquals
(
$expected
,
Unicode
::
validateUtf8
(
$text
),
$message
);
}
/**
* Provides data for self::testValidateUtf8().
*
* @return array
* An array of arrays, each containing the parameters for
* self::testValidateUtf8().
*
* Invalid UTF-8 examples sourced from http://stackoverflow.com/a/11709412/109119.
*/
public
function
providerTestValidateUtf8
()
{
return
array
(
// Empty string.
array
(
''
,
TRUE
,
'An empty string did not validate.'
),
// Simple text string.
array
(
'Simple text.'
,
TRUE
,
'A simple ASCII text string did not validate.'
),
// Invalid UTF-8, overlong 5 byte encoding.
array
(
chr
(
0xF8
)
.
chr
(
0x80
)
.
chr
(
0x80
)
.
chr
(
0x80
)
.
chr
(
0x80
),
FALSE
,
'Invalid UTF-8 was validated.'
),
// High code-point without trailing characters.
array
(
chr
(
0xD0
)
.
chr
(
0x01
),
FALSE
,
'Invalid UTF-8 was validated.'
),
);
}
/**
* Tests Unicode::convertToUtf8().
*
* @param string $data
* The data to be converted.
* @param string $encoding
* The encoding the data is in.
* @param string|bool $expected
* The expected result.
*
* @dataProvider providerTestConvertToUtf8
*/
public
function
testConvertToUtf8
(
$data
,
$encoding
,
$expected
)
{
$this
->
assertEquals
(
$expected
,
Unicode
::
convertToUtf8
(
$data
,
$encoding
));
}
/**
* Provides data to self::testConvertToUtf8().
*
* @return array
* An array of arrays, each containg the parameters to
* self::testConvertUtf8(). }
*/
public
function
providerTestConvertToUtf8
()
{
return
array
(
array
(
chr
(
0x97
),
'Windows-1250'
,
'—'
),
array
(
chr
(
0x99
),
'Windows-1250'
,
'™'
),
array
(
chr
(
0x80
),
'Windows-1250'
,
'€'
),
);
}
}
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