Tests string filtering.
Expanded class hierarchy of StringTest
\Drupal\Component\Utility\String
class StringTest extends UnitTestCase {
public static function getInfo() {
return array(
'name' => 'String filtering tests',
'description' => 'Confirm that String::checkPlain() and String::format() work correctly, including invalid multi-byte sequences.',
'group' => 'Common',
);
}
/**
* Tests String::checkPlain().
*
* @dataProvider providerCheckPlain
*
* @param string $text
* The text to provide to String::checkPlain().
* @param string $expected
* The expected output from the function.
* @param string $message
* The message to provide as output for the test.
* @param bool $ignorewarnings
* Whether or not to ignore PHP 5.3+ invalid multibyte sequence warnings.
*/
function testCheckPlain($text, $expected, $message, $ignorewarnings = FALSE) {
$result = $ignorewarnings ? @String::checkPlain($text) : String::checkPlain($text);
$this
->assertEquals($expected, $result, $message);
}
/**
* Data provider for testCheckPlain().
*
* @see testCheckPlain()
*/
function providerCheckPlain() {
// Checks that invalid multi-byte sequences are rejected.
$tests[] = array(
"",
'',
'String::checkPlain() rejects invalid sequence "Foo\\xC0barbaz"',
TRUE,
);
$tests[] = array(
"",
'',
'String::checkPlain() rejects invalid sequence "\\xc2\\""',
TRUE,
);
$tests[] = array(
"Fooÿñ",
"Fooÿñ",
'String::checkPlain() accepts valid sequence "Fooÿñ"',
);
// Checks that special characters are escaped.
$tests[] = array(
"<script>",
'<script>',
'String::checkPlain() escapes <script>',
);
$tests[] = array(
'<>&"\'',
'<>&"'',
'String::checkPlain() escapes reserved HTML characters.',
);
return $tests;
}
/**
* Tests string formatting with String::format().
*
* @dataProvider providerFormat
*
* @param string $string
* The string to run through String::format().
* @param string $args
* The arguments to pass into String::format().
* @param string $expected
* The expected result from calling the function.
* @param string $message
* The message to display as output to the test.
*
* @see String::format()
*/
function testFormat($string, $args, $expected, $message) {
$result = String::format($string, $args);
$this
->assertEquals($expected, $result, $message);
}
/**
* Data provider for testFormat().
*
* @see testFormat()
*/
function providerFormat() {
$tests[] = array(
'Simple text',
array(),
'Simple text',
'String::format leaves simple text alone.',
);
$tests[] = array(
'Escaped text: @value',
array(
'@value' => '<script>',
),
'Escaped text: <script>',
'String::format replaces and escapes string.',
);
$tests[] = array(
'Placeholder text: %value',
array(
'%value' => '<script>',
),
'Placeholder text: <em class="placeholder"><script></em>',
'String::format replaces, escapes and themes string.',
);
$tests[] = array(
'Verbatim text: !value',
array(
'!value' => '<script>',
),
'Verbatim text: <script>',
'String::format replaces verbatim string as-is.',
);
return $tests;
}
/**
* Tests String::placeholder().
*
* @see String::placeholder()
*/
function testPlaceholder() {
$this
->assertEquals('<em class="placeholder">Some text</em>', String::placeholder('Some text'));
}
/**
* Tests String::decodeEntities().
*
* @dataProvider providerDecodeEntities
*/
public function testDecodeEntities($text, $expected) {
$this
->assertEquals($expected, String::decodeEntities($text));
}
/**
* Data provider for testDecodeEntities().
*
* @see testCheckPlain()
*/
public function providerDecodeEntities() {
return array(
array(
'Drupal',
'Drupal',
),
array(
'<script>',
'<script>',
),
array(
'<script>',
'<script>',
),
array(
'<script>',
'<script>',
),
array(
'&lt;script&gt;',
'<script>',
),
array(
'"',
'"',
),
array(
'"',
'"',
),
array(
'&#34;',
'"',
),
array(
'"',
'"',
),
array(
'&quot;',
'"',
),
array(
"'",
"'",
),
array(
''',
"'",
),
array(
'&#39;',
''',
),
array(
'©',
'©',
),
array(
'©',
'©',
),
array(
'©',
'©',
),
array(
'→',
'→',
),
array(
'→',
'→',
),
array(
'➼',
'➼',
),
array(
'➼',
'➼',
),
array(
'€',
'€',
),
);
}
}
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
StringTest:: |
public static | function |
This method exists to support the simpletest UI runner. Overrides UnitTestCase:: |
|
StringTest:: |
function | Data provider for testCheckPlain(). | ||
StringTest:: |
public | function | Data provider for testDecodeEntities(). | |
StringTest:: |
function | Data provider for testFormat(). | ||
StringTest:: |
function | Tests String::checkPlain(). | ||
StringTest:: |
public | function | Tests String::decodeEntities(). | |
StringTest:: |
function | Tests string formatting with String::format(). | ||
StringTest:: |
function | Tests String::placeholder(). | ||
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed in array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
public static | function | Generates a random string containing letters and numbers. |