Issue #3091447 by Krzysztof Domański, el7cosmos, longwave, ravi.shankar, Berdir, martin107: Remove Utility component BC layers
parent
5c832d2e3e
commit
34473684b6
|
@ -9,35 +9,6 @@ namespace Drupal\Component\Utility;
|
|||
*/
|
||||
class Crypt {
|
||||
|
||||
/**
|
||||
* Returns a string of highly randomized bytes (over the full 8-bit range).
|
||||
*
|
||||
* This function is better than simply calling mt_rand() or any other built-in
|
||||
* PHP function because it can return a long string of bytes (compared to < 4
|
||||
* bytes normally from mt_rand()) and uses the best available pseudo-random
|
||||
* source.
|
||||
*
|
||||
* In PHP 7 and up, this uses the built-in PHP function random_bytes().
|
||||
* In older PHP versions, this uses the random_bytes() function provided by
|
||||
* the random_compat library, or the fallback hash-based generator from Drupal
|
||||
* 7.x.
|
||||
*
|
||||
* @param int $count
|
||||
* The number of characters (bytes) to return in the string.
|
||||
*
|
||||
* @return string
|
||||
* A randomly generated string.
|
||||
*
|
||||
* @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0.
|
||||
* Use PHP's built-in random_bytes() function instead.
|
||||
*
|
||||
* @see https://www.drupal.org/node/3054488
|
||||
*/
|
||||
public static function randomBytes($count) {
|
||||
@trigger_error(__CLASS__ . '::randomBytes() is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use PHP\'s built-in random_bytes() function instead. See https://www.drupal.org/node/3054488', E_USER_DEPRECATED);
|
||||
return random_bytes($count);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates a base-64 encoded, URL-safe sha-256 hmac.
|
||||
*
|
||||
|
@ -80,27 +51,6 @@ class Crypt {
|
|||
return str_replace(['+', '/', '='], ['-', '_', ''], $hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares strings in constant time.
|
||||
*
|
||||
* @param string $known_string
|
||||
* The expected string.
|
||||
* @param string $user_string
|
||||
* The user supplied string to check.
|
||||
*
|
||||
* @return bool
|
||||
* Returns TRUE when the two strings are equal, FALSE otherwise.
|
||||
*
|
||||
* @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0.
|
||||
* Use PHP's built-in hash_equals() function instead.
|
||||
*
|
||||
* @see https://www.drupal.org/node/3054488
|
||||
*/
|
||||
public static function hashEquals($known_string, $user_string) {
|
||||
@trigger_error(__CLASS__ . '::hashEquals() is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use PHP\'s built-in hash_equals() function instead. See https://www.drupal.org/node/3054488', E_USER_DEPRECATED);
|
||||
return hash_equals($known_string, $user_string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a URL-safe, base64 encoded string of highly randomized bytes.
|
||||
*
|
||||
|
@ -109,8 +59,6 @@ class Crypt {
|
|||
*
|
||||
* @return string
|
||||
* The base64 encoded result will have a length of up to 4 * $count.
|
||||
*
|
||||
* @see \Drupal\Component\Utility\Crypt::randomBytes()
|
||||
*/
|
||||
public static function randomBytesBase64($count = 32) {
|
||||
return str_replace(['+', '/', '='], ['-', '_', ''], base64_encode(random_bytes($count)));
|
||||
|
|
|
@ -1,74 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Component\Utility;
|
||||
|
||||
use Drupal\Component\Render\FormattableMarkup;
|
||||
use Drupal\Component\Render\MarkupInterface;
|
||||
|
||||
/**
|
||||
* Contains deprecated functionality related to sanitization of markup.
|
||||
*
|
||||
* @deprecated Will be removed before Drupal 9.0.0. Use the appropriate
|
||||
* @link sanitization sanitization functions @endlink or the @link theme_render theme and render systems @endlink
|
||||
* so that the output can can be themed, escaped, and altered properly.
|
||||
*
|
||||
* @see https://www.drupal.org/node/2549395
|
||||
*
|
||||
* @see TwigExtension::escapeFilter()
|
||||
* @see twig_render_template()
|
||||
* @see sanitization
|
||||
* @see theme_render
|
||||
*/
|
||||
class SafeMarkup {
|
||||
|
||||
/**
|
||||
* Checks if a string is safe to output.
|
||||
*
|
||||
* @param string|\Drupal\Component\Render\MarkupInterface $string
|
||||
* The content to be checked.
|
||||
* @param string $strategy
|
||||
* (optional) This value is ignored.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if the string has been marked secure, FALSE otherwise.
|
||||
*
|
||||
* @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
|
||||
* Instead, you should just check if a variable is an instance of
|
||||
* \Drupal\Component\Render\MarkupInterface.
|
||||
*
|
||||
* @see https://www.drupal.org/node/2549395
|
||||
*/
|
||||
public static function isSafe($string, $strategy = 'html') {
|
||||
@trigger_error('SafeMarkup::isSafe() is scheduled for removal in Drupal 9.0.0. Instead, you should just check if a variable is an instance of \Drupal\Component\Render\MarkupInterface. See https://www.drupal.org/node/2549395.', E_USER_DEPRECATED);
|
||||
return $string instanceof MarkupInterface;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a string for HTML display by replacing variable placeholders.
|
||||
*
|
||||
* @param string $string
|
||||
* A string containing placeholders. The string itself will not be escaped,
|
||||
* any unsafe content must be in $args and inserted via placeholders.
|
||||
* @param array $args
|
||||
* An array with placeholder replacements, keyed by placeholder. See
|
||||
* \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for
|
||||
* additional information about placeholders.
|
||||
*
|
||||
* @return string|\Drupal\Component\Render\MarkupInterface
|
||||
* The formatted string, which is an instance of MarkupInterface unless
|
||||
* sanitization of an unsafe argument was suppressed (see above).
|
||||
*
|
||||
* @see \Drupal\Component\Render\FormattableMarkup::placeholderFormat()
|
||||
* @see \Drupal\Component\Render\FormattableMarkup
|
||||
*
|
||||
* @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
|
||||
* Use \Drupal\Component\Render\FormattableMarkup.
|
||||
*
|
||||
* @see https://www.drupal.org/node/2549395
|
||||
*/
|
||||
public static function format($string, array $args) {
|
||||
@trigger_error('SafeMarkup::format() is scheduled for removal in Drupal 9.0.0. Use \Drupal\Component\Render\FormattableMarkup. See https://www.drupal.org/node/2549395.', E_USER_DEPRECATED);
|
||||
return new FormattableMarkup($string, $args);
|
||||
}
|
||||
|
||||
}
|
|
@ -109,31 +109,6 @@ EOD;
|
|||
return Unicode::STATUS_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value for multibyte support status for the current environment.
|
||||
*
|
||||
* The following status keys are supported:
|
||||
* - \Drupal\Component\Utility\Unicode::STATUS_MULTIBYTE
|
||||
* Full unicode support using an extension.
|
||||
* - \Drupal\Component\Utility\Unicode::STATUS_SINGLEBYTE
|
||||
* Standard PHP (emulated) unicode support.
|
||||
* - \Drupal\Component\Utility\Unicode::STATUS_ERROR
|
||||
* An error occurred. No unicode support.
|
||||
*
|
||||
* @param int $status
|
||||
* The new status of multibyte support.
|
||||
*
|
||||
* @deprecated in drupal:8.6.0 and is removed from drupal:9.0.0. In
|
||||
* Drupal 9 there will be no way to set the status and in Drupal 8 this
|
||||
* ability has been removed because mb_*() functions are supplied using
|
||||
* Symfony's polyfill.
|
||||
*
|
||||
* @see https://www.drupal.org/node/2850048
|
||||
*/
|
||||
public static function setStatus($status) {
|
||||
@trigger_error('\Drupal\Component\Utility\Unicode::setStatus() is deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. In Drupal 9 there will be no way to set the status and in Drupal 8 this ability has been removed because mb_*() functions are supplied using Symfony\'s polyfill. See https://www.drupal.org/node/2850048.', E_USER_DEPRECATED);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for Unicode support in PHP and sets the proper settings if possible.
|
||||
*
|
||||
|
@ -249,65 +224,6 @@ EOD;
|
|||
return substr($string, 0, $len);
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts the number of characters in a UTF-8 string.
|
||||
*
|
||||
* This is less than or equal to the byte count.
|
||||
*
|
||||
* @param string $text
|
||||
* The string to run the operation on.
|
||||
*
|
||||
* @return int
|
||||
* The length of the string.
|
||||
*
|
||||
* @deprecated in drupal:8.6.0 and is removed from drupal:9.0.0. Use
|
||||
* mb_strlen() instead.
|
||||
*
|
||||
* @see https://www.drupal.org/node/2850048
|
||||
*/
|
||||
public static function strlen($text) {
|
||||
@trigger_error('\Drupal\Component\Utility\Unicode::strlen() is deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. Use mb_strlen() instead. See https://www.drupal.org/node/2850048.', E_USER_DEPRECATED);
|
||||
return mb_strlen($text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a UTF-8 string to uppercase.
|
||||
*
|
||||
* @param string $text
|
||||
* The string to run the operation on.
|
||||
*
|
||||
* @return string
|
||||
* The string in uppercase.
|
||||
*
|
||||
* @deprecated in drupal:8.6.0 and is removed from drupal:9.0.0. Use
|
||||
* mb_strtoupper() instead.
|
||||
*
|
||||
* @see https://www.drupal.org/node/2850048
|
||||
*/
|
||||
public static function strtoupper($text) {
|
||||
@trigger_error('\Drupal\Component\Utility\Unicode::strtoupper() is deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. Use mb_strtoupper() instead. See https://www.drupal.org/node/2850048.', E_USER_DEPRECATED);
|
||||
return mb_strtoupper($text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a UTF-8 string to lowercase.
|
||||
*
|
||||
* @param string $text
|
||||
* The string to run the operation on.
|
||||
*
|
||||
* @return string
|
||||
* The string in lowercase.
|
||||
*
|
||||
* @deprecated in drupal:8.6.0 and is removed from drupal:9.0.0. Use
|
||||
* mb_strtolower() instead.
|
||||
*
|
||||
* @see https://www.drupal.org/node/2850048
|
||||
*/
|
||||
public static function strtolower($text) {
|
||||
@trigger_error('\Drupal\Component\Utility\Unicode::strtolower() is deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. Use mb_strtolower() instead. See https://www.drupal.org/node/2850048.', E_USER_DEPRECATED);
|
||||
return mb_strtolower($text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Capitalizes the first character of a UTF-8 string.
|
||||
*
|
||||
|
@ -355,33 +271,6 @@ EOD;
|
|||
}, $text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cuts off a piece of a string based on character indices and counts.
|
||||
*
|
||||
* Follows the same behavior as PHP's own substr() function. Note that for
|
||||
* cutting off a string at a known character/substring location, the usage of
|
||||
* PHP's normal strpos/substr is safe and much faster.
|
||||
*
|
||||
* @param string $text
|
||||
* The input string.
|
||||
* @param int $start
|
||||
* The position at which to start reading.
|
||||
* @param int $length
|
||||
* The number of characters to read.
|
||||
*
|
||||
* @return string
|
||||
* The shortened string.
|
||||
*
|
||||
* @deprecated in drupal:8.6.0 and is removed from drupal:9.0.0. Use
|
||||
* mb_substr() instead.
|
||||
*
|
||||
* @see https://www.drupal.org/node/2850048
|
||||
*/
|
||||
public static function substr($text, $start, $length = NULL) {
|
||||
@trigger_error('\Drupal\Component\Utility\Unicode::substr() is deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. Use mb_substr() instead. See https://www.drupal.org/node/2850048.', E_USER_DEPRECATED);
|
||||
return mb_substr($text, $start, $length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncates a UTF-8-encoded string safely to a number of characters.
|
||||
*
|
||||
|
@ -548,25 +437,6 @@ EOD;
|
|||
return preg_replace_callback('/=\?([^?]+)\?(Q|B)\?([^?]+|\?(?!=))\?=/', $callback, $header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Flip U+C0-U+DE to U+E0-U+FD and back. Can be used as preg_replace callback.
|
||||
*
|
||||
* @param array $matches
|
||||
* An array of matches by preg_replace_callback().
|
||||
*
|
||||
* @return string
|
||||
* The flipped text.
|
||||
*
|
||||
* @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. There is
|
||||
* no direct replacement.
|
||||
*
|
||||
* @see https://www.drupal.org/node/3057322
|
||||
*/
|
||||
public static function caseFlip($matches) {
|
||||
@trigger_error('\Drupal\Component\Utility\Unicode::caseFlip() is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. There is no direct replacement. See https://www.drupal.org/node/3057322', E_USER_DEPRECATED);
|
||||
return $matches[0][0] . chr(ord($matches[0][1]) ^ 32);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a string is valid UTF-8.
|
||||
*
|
||||
|
@ -601,30 +471,4 @@ EOD;
|
|||
return (preg_match('/^./us', $text) == 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the position of the first occurrence of a string in another string.
|
||||
*
|
||||
* @param string $haystack
|
||||
* The string to search in.
|
||||
* @param string $needle
|
||||
* The string to find in $haystack.
|
||||
* @param int $offset
|
||||
* If specified, start the search at this number of characters from the
|
||||
* beginning (default 0).
|
||||
*
|
||||
* @return int|false
|
||||
* The position where $needle occurs in $haystack, always relative to the
|
||||
* beginning (independent of $offset), or FALSE if not found. Note that
|
||||
* a return value of 0 is not the same as FALSE.
|
||||
*
|
||||
* @deprecated in drupal:8.6.0 and is removed from drupal:9.0.0. Use
|
||||
* mb_strpos() instead.
|
||||
*
|
||||
* @see https://www.drupal.org/node/2850048
|
||||
*/
|
||||
public static function strpos($haystack, $needle, $offset = 0) {
|
||||
@trigger_error('\Drupal\Component\Utility\Unicode::strpos() is deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. Use mb_strpos() instead. See https://www.drupal.org/node/2850048.', E_USER_DEPRECATED);
|
||||
return mb_strpos($haystack, $needle, $offset);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -14,17 +14,6 @@ use PHPUnit\Framework\TestCase;
|
|||
*/
|
||||
class CryptTest extends TestCase {
|
||||
|
||||
/**
|
||||
* Tests random byte generation.
|
||||
*
|
||||
* @covers ::randomBytes
|
||||
* @expectedDeprecation Drupal\Component\Utility\Crypt::randomBytes() is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use PHP's built-in random_bytes() function instead. See https://www.drupal.org/node/3054488
|
||||
* @group legacy
|
||||
*/
|
||||
public function testRandomBytes() {
|
||||
$this->assertSame(16, strlen(Crypt::randomBytes(16)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests hash generation.
|
||||
*
|
||||
|
@ -143,17 +132,4 @@ class CryptTest extends TestCase {
|
|||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Legacy test of Drupal\Component\Utility\Crypt::hashEquals() method.
|
||||
*
|
||||
* @expectedDeprecation Drupal\Component\Utility\Crypt::hashEquals() is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use PHP's built-in hash_equals() function instead. See https://www.drupal.org/node/3054488
|
||||
* @group legacy
|
||||
*/
|
||||
public function testHashEquals() {
|
||||
$a_hash = Crypt::hashBase64('a');
|
||||
$b_hash = Crypt::hashBase64('b');
|
||||
$this->assertTrue(Crypt::hashEquals($a_hash, $a_hash));
|
||||
$this->assertFalse(Crypt::hashEquals($a_hash, $b_hash));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,167 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\Component\Utility\SafeMarkupTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\Component\Utility;
|
||||
|
||||
use Drupal\Component\Render\HtmlEscapedText;
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Component\Render\MarkupInterface;
|
||||
use Drupal\Component\Render\MarkupTrait;
|
||||
use Drupal\Component\Utility\UrlHelper;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests marking strings as safe.
|
||||
*
|
||||
* @group Utility
|
||||
* @group legacy
|
||||
* @coversDefaultClass \Drupal\Component\Utility\SafeMarkup
|
||||
*/
|
||||
class SafeMarkupTest extends TestCase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function tearDown() {
|
||||
parent::tearDown();
|
||||
|
||||
UrlHelper::setAllowedProtocols(['http', 'https']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests SafeMarkup::isSafe() with different objects.
|
||||
*
|
||||
* @covers ::isSafe
|
||||
* @expectedDeprecation SafeMarkup::isSafe() is scheduled for removal in Drupal 9.0.0. Instead, you should just check if a variable is an instance of \Drupal\Component\Render\MarkupInterface. See https://www.drupal.org/node/2549395.
|
||||
*/
|
||||
public function testIsSafe() {
|
||||
$safe_string = $this->getMockBuilder('\Drupal\Component\Render\MarkupInterface')->getMock();
|
||||
$this->assertTrue(SafeMarkup::isSafe($safe_string));
|
||||
$string_object = new SafeMarkupTestString('test');
|
||||
$this->assertFalse(SafeMarkup::isSafe($string_object));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests Drupal\Component\Render\HtmlEscapedText.
|
||||
*
|
||||
* Verifies that the result of SafeMarkup::checkPlain() is the same as using
|
||||
* HtmlEscapedText directly.
|
||||
*
|
||||
* @dataProvider providerCheckPlain
|
||||
*
|
||||
* @param string $text
|
||||
* The text to provide to the HtmlEscapedText constructor.
|
||||
* @param string $expected
|
||||
* The expected output from the function.
|
||||
* @param string $message
|
||||
* The message to provide as output for the test.
|
||||
*/
|
||||
public function testHtmlEscapedText($text, $expected, $message) {
|
||||
$result = new HtmlEscapedText($text);
|
||||
$this->assertEquals($expected, $result, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testEscapeString().
|
||||
*
|
||||
* @see testCheckPlain()
|
||||
*/
|
||||
public function providerCheckPlain() {
|
||||
// Checks that invalid multi-byte sequences are escaped.
|
||||
$tests[] = ["Foo\xC0barbaz", 'Foo<6F>barbaz', 'Escapes invalid sequence "Foo\xC0barbaz"'];
|
||||
$tests[] = ["\xc2\"", '<27>"', 'Escapes invalid sequence "\xc2\""'];
|
||||
$tests[] = ["Fooÿñ", "Fooÿñ", 'Does not escape valid sequence "Fooÿñ"'];
|
||||
|
||||
// Checks that special characters are escaped.
|
||||
$tests[] = [SafeMarkupTestMarkup::create("<script>"), '<script>', 'Escapes <script> even inside an object that implements MarkupInterface.'];
|
||||
$tests[] = ["<script>", '<script>', 'Escapes <script>'];
|
||||
$tests[] = ['<>&"\'', '<>&"'', 'Escapes reserved HTML characters.'];
|
||||
$tests[] = [SafeMarkupTestMarkup::create('<>&"\''), '<>&"'', 'Escapes reserved HTML characters even inside an object that implements MarkupInterface.'];
|
||||
|
||||
return $tests;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests string formatting with SafeMarkup::format().
|
||||
*
|
||||
* @dataProvider providerFormat
|
||||
* @covers ::format
|
||||
* @expectedDeprecation SafeMarkup::format() is scheduled for removal in Drupal 9.0.0. Use \Drupal\Component\Render\FormattableMarkup. See https://www.drupal.org/node/2549395.
|
||||
*
|
||||
* @param string $string
|
||||
* The string to run through SafeMarkup::format().
|
||||
* @param string[] $args
|
||||
* The arguments to pass into SafeMarkup::format().
|
||||
* @param string $expected
|
||||
* The expected result from calling the function.
|
||||
* @param string $message
|
||||
* The message to display as output to the test.
|
||||
* @param bool $expected_is_safe
|
||||
* Whether the result is expected to be safe for HTML display.
|
||||
*/
|
||||
public function testFormat($string, array $args, $expected, $message, $expected_is_safe) {
|
||||
UrlHelper::setAllowedProtocols(['http', 'https', 'mailto']);
|
||||
|
||||
$result = SafeMarkup::format($string, $args);
|
||||
$this->assertEquals($expected, (string) $result, $message);
|
||||
$this->assertEquals($expected_is_safe, $result instanceof MarkupInterface, 'SafeMarkup::format correctly sets the result as safe or not safe.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testFormat().
|
||||
*
|
||||
* @see testFormat()
|
||||
*/
|
||||
public function providerFormat() {
|
||||
$tests[] = ['Simple text', [], 'Simple text', 'SafeMarkup::format leaves simple text alone.', TRUE];
|
||||
$tests[] = ['Escaped text: @value', ['@value' => '<script>'], 'Escaped text: <script>', 'SafeMarkup::format replaces and escapes string.', TRUE];
|
||||
$tests[] = ['Escaped text: @value', ['@value' => SafeMarkupTestMarkup::create('<span>Safe HTML</span>')], 'Escaped text: <span>Safe HTML</span>', 'SafeMarkup::format does not escape an already safe string.', TRUE];
|
||||
$tests[] = ['Placeholder text: %value', ['%value' => '<script>'], 'Placeholder text: <em class="placeholder"><script></em>', 'SafeMarkup::format replaces, escapes and themes string.', TRUE];
|
||||
$tests[] = ['Placeholder text: %value', ['%value' => SafeMarkupTestMarkup::create('<span>Safe HTML</span>')], 'Placeholder text: <em class="placeholder"><span>Safe HTML</span></em>', 'SafeMarkup::format does not escape an already safe string themed as a placeholder.', TRUE];
|
||||
|
||||
$tests['javascript-protocol-url'] = ['Simple text <a href=":url">giraffe</a>', [':url' => 'javascript://example.com?foo&bar'], 'Simple text <a href="//example.com?foo&bar">giraffe</a>', 'Support for filtering bad protocols', TRUE];
|
||||
$tests['external-url'] = ['Simple text <a href=":url">giraffe</a>', [':url' => 'http://example.com?foo&bar'], 'Simple text <a href="http://example.com?foo&bar">giraffe</a>', 'Support for filtering bad protocols', TRUE];
|
||||
$tests['relative-url'] = ['Simple text <a href=":url">giraffe</a>', [':url' => '/node/1?foo&bar'], 'Simple text <a href="/node/1?foo&bar">giraffe</a>', 'Support for filtering bad protocols', TRUE];
|
||||
$tests['fragment-with-special-chars'] = ['Simple text <a href=":url">giraffe</a>', [':url' => 'http://example.com/#<'], 'Simple text <a href="http://example.com/#&lt;">giraffe</a>', 'Support for filtering bad protocols', TRUE];
|
||||
$tests['mailto-protocol'] = ['Hey giraffe <a href=":url">MUUUH</a>', [':url' => 'mailto:test@example.com'], 'Hey giraffe <a href="mailto:test@example.com">MUUUH</a>', '', TRUE];
|
||||
$tests['js-with-fromCharCode'] = ['Hey giraffe <a href=":url">MUUUH</a>', [':url' => "javascript:alert(String.fromCharCode(88,83,83))"], 'Hey giraffe <a href="alert(String.fromCharCode(88,83,83))">MUUUH</a>', '', TRUE];
|
||||
|
||||
// Test some "URL" values that are not RFC 3986 compliant URLs. The result
|
||||
// of SafeMarkup::format() should still be valid HTML (other than the
|
||||
// value of the "href" attribute not being a valid URL), and not
|
||||
// vulnerable to XSS.
|
||||
$tests['non-url-with-colon'] = ['Hey giraffe <a href=":url">MUUUH</a>', [':url' => "llamas: they are not URLs"], 'Hey giraffe <a href=" they are not URLs">MUUUH</a>', '', TRUE];
|
||||
$tests['non-url-with-html'] = ['Hey giraffe <a href=":url">MUUUH</a>', [':url' => "<span>not a url</span>"], 'Hey giraffe <a href="<span>not a url</span>">MUUUH</a>', '', TRUE];
|
||||
|
||||
// Tests non-standard placeholders that will not replace.
|
||||
$tests['non-standard-placeholder'] = ['Hey hey', ['risky' => "<script>alert('foo');</script>"], 'Hey hey', '', TRUE];
|
||||
return $tests;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class SafeMarkupTestString {
|
||||
|
||||
protected $string;
|
||||
|
||||
public function __construct($string) {
|
||||
$this->string = $string;
|
||||
}
|
||||
|
||||
public function __toString() {
|
||||
return $this->string;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks an object's __toString() method as returning markup.
|
||||
*/
|
||||
class SafeMarkupTestMarkup implements MarkupInterface {
|
||||
use MarkupTrait;
|
||||
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\Component\Utility;
|
||||
|
||||
/**
|
||||
* Used by SafeMarkupTest to test that a class with a __toString() method works.
|
||||
*/
|
||||
class TextWrapper {
|
||||
|
||||
/**
|
||||
* The text value.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $text = '';
|
||||
|
||||
/**
|
||||
* Constructs a \Drupal\Tests\Component\Utility\TextWrapper
|
||||
*
|
||||
* @param string $text
|
||||
*/
|
||||
public function __construct($text) {
|
||||
$this->text = $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic method
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString() {
|
||||
return $this->text;
|
||||
}
|
||||
|
||||
}
|
|
@ -14,14 +14,6 @@ use PHPUnit\Framework\TestCase;
|
|||
*/
|
||||
class UnicodeTest extends TestCase {
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
* @expectedDeprecation \Drupal\Component\Utility\Unicode::setStatus() is deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. In Drupal 9 there will be no way to set the status and in Drupal 8 this ability has been removed because mb_*() functions are supplied using Symfony's polyfill. See https://www.drupal.org/node/2850048.
|
||||
*/
|
||||
public function testSetStatus() {
|
||||
Unicode::setStatus(Unicode::STATUS_SINGLEBYTE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests multibyte encoding and decoding.
|
||||
*
|
||||
|
@ -50,62 +42,6 @@ class UnicodeTest extends TestCase {
|
|||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests multibyte strtolower.
|
||||
*
|
||||
* @dataProvider providerStrtolower
|
||||
* @covers ::strtolower
|
||||
* @group legacy
|
||||
* @expectedDeprecation \Drupal\Component\Utility\Unicode::strtolower() is deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. Use mb_strtolower() instead. See https://www.drupal.org/node/2850048.
|
||||
*/
|
||||
public function testStrtolower($text, $expected) {
|
||||
$this->assertEquals($expected, Unicode::strtolower($text));
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testStrtolower().
|
||||
*
|
||||
* @see testStrtolower()
|
||||
*
|
||||
* @return array
|
||||
* An array containing a string and its lowercase version.
|
||||
*/
|
||||
public function providerStrtolower() {
|
||||
return [
|
||||
['tHe QUIcK bRoWn', 'the quick brown'],
|
||||
['FrançAIS is ÜBER-åwesome', 'français is über-åwesome'],
|
||||
['ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ', 'αβγδεζηθικλμνξοσὠ'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests multibyte strtoupper.
|
||||
*
|
||||
* @dataProvider providerStrtoupper
|
||||
* @covers ::strtoupper
|
||||
* @group legacy
|
||||
* @expectedDeprecation \Drupal\Component\Utility\Unicode::strtoupper() is deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. Use mb_strtoupper() instead. See https://www.drupal.org/node/2850048.
|
||||
*/
|
||||
public function testStrtoupper($text, $expected) {
|
||||
$this->assertEquals($expected, Unicode::strtoupper($text));
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testStrtoupper().
|
||||
*
|
||||
* @see testStrtoupper()
|
||||
*
|
||||
* @return array
|
||||
* An array containing a string and its uppercase version.
|
||||
*/
|
||||
public function providerStrtoupper() {
|
||||
return [
|
||||
['tHe QUIcK bRoWn', 'THE QUICK BROWN'],
|
||||
['FrançAIS is ÜBER-åwesome', 'FRANÇAIS IS ÜBER-ÅWESOME'],
|
||||
['αβγδεζηθικλμνξοσὠ', 'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests multibyte ucfirst.
|
||||
*
|
||||
|
@ -195,88 +131,6 @@ class UnicodeTest extends TestCase {
|
|||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests multibyte strlen.
|
||||
*
|
||||
* @dataProvider providerStrlen
|
||||
* @covers ::strlen
|
||||
* @group legacy
|
||||
* @expectedDeprecation \Drupal\Component\Utility\Unicode::strlen() is deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. Use mb_strlen() instead. See https://www.drupal.org/node/2850048.
|
||||
*/
|
||||
public function testStrlen($text, $expected) {
|
||||
$this->assertEquals($expected, Unicode::strlen($text));
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testStrlen().
|
||||
*
|
||||
* @see testStrlen()
|
||||
*
|
||||
* @return array
|
||||
* An array containing a string and its length.
|
||||
*/
|
||||
public function providerStrlen() {
|
||||
return [
|
||||
['tHe QUIcK bRoWn', 15],
|
||||
['ÜBER-åwesome', 12],
|
||||
['以呂波耳・ほへとち。リヌルヲ。', 15],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests multibyte substr.
|
||||
*
|
||||
* @dataProvider providerSubstr
|
||||
* @covers ::substr
|
||||
* @group legacy
|
||||
* @expectedDeprecation \Drupal\Component\Utility\Unicode::substr() is deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. Use mb_substr() instead. See https://www.drupal.org/node/2850048.
|
||||
*/
|
||||
public function testSubstr($text, $start, $length, $expected) {
|
||||
$this->assertEquals($expected, Unicode::substr($text, $start, $length));
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testSubstr().
|
||||
*
|
||||
* @see testSubstr()
|
||||
*
|
||||
* @return array
|
||||
* An array containing:
|
||||
* - The string to test.
|
||||
* - The start number to be processed by substr.
|
||||
* - The length number to be processed by substr.
|
||||
* - The expected string result.
|
||||
*/
|
||||
public function providerSubstr() {
|
||||
return [
|
||||
['frànçAIS is über-åwesome', 0, NULL, 'frànçAIS is über-åwesome'],
|
||||
['frànçAIS is über-åwesome', 0, 0, ''],
|
||||
['frànçAIS is über-åwesome', 0, 1, 'f'],
|
||||
['frànçAIS is über-åwesome', 0, 8, 'frànçAIS'],
|
||||
['frànçAIS is über-åwesome', 0, 23, 'frànçAIS is über-åwesom'],
|
||||
['frànçAIS is über-åwesome', 0, 24, 'frànçAIS is über-åwesome'],
|
||||
['frànçAIS is über-åwesome', 0, 25, 'frànçAIS is über-åwesome'],
|
||||
['frànçAIS is über-åwesome', 0, 100, 'frànçAIS is über-åwesome'],
|
||||
['frànçAIS is über-åwesome', 4, 4, 'çAIS'],
|
||||
['frànçAIS is über-åwesome', 1, 0, ''],
|
||||
['frànçAIS is über-åwesome', 100, 0, ''],
|
||||
['frànçAIS is über-åwesome', -4, 2, 'so'],
|
||||
['frànçAIS is über-åwesome', -4, 3, 'som'],
|
||||
['frànçAIS is über-åwesome', -4, 4, 'some'],
|
||||
['frànçAIS is über-åwesome', -4, 5, 'some'],
|
||||
['frànçAIS is über-åwesome', -7, 10, 'åwesome'],
|
||||
['frànçAIS is über-åwesome', 5, -10, 'AIS is üb'],
|
||||
['frànçAIS is über-åwesome', 0, -10, 'frànçAIS is üb'],
|
||||
['frànçAIS is über-åwesome', 0, -1, 'frànçAIS is über-åwesom'],
|
||||
['frànçAIS is über-åwesome', -7, -2, 'åweso'],
|
||||
['frànçAIS is über-åwesome', -7, -6, 'å'],
|
||||
['frànçAIS is über-åwesome', -7, -7, ''],
|
||||
['frànçAIS is über-åwesome', -7, -8, ''],
|
||||
['...', 0, 2, '..'],
|
||||
['以呂波耳・ほへとち。リヌルヲ。', 1, 3, '呂波耳'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests multibyte truncate.
|
||||
*
|
||||
|
@ -473,43 +327,4 @@ EOF;
|
|||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests multibyte strpos.
|
||||
*
|
||||
* @dataProvider providerStrpos
|
||||
* @covers ::strpos
|
||||
* @group legacy
|
||||
* @expectedDeprecation \Drupal\Component\Utility\Unicode::strpos() is deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. Use mb_strpos() instead. See https://www.drupal.org/node/2850048.
|
||||
*/
|
||||
public function testStrpos($haystack, $needle, $offset, $expected) {
|
||||
$this->assertEquals($expected, Unicode::strpos($haystack, $needle, $offset));
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testStrpos().
|
||||
*
|
||||
* @see testStrpos()
|
||||
*
|
||||
* @return array
|
||||
* An array containing:
|
||||
* - The haystack string to be searched in.
|
||||
* - The needle string to search for.
|
||||
* - The offset integer to start at.
|
||||
* - The expected integer/FALSE result.
|
||||
*/
|
||||
public function providerStrpos() {
|
||||
return [
|
||||
['frànçAIS is über-åwesome', 'frànçAIS is über-åwesome', 0, 0],
|
||||
['frànçAIS is über-åwesome', 'rànçAIS is über-åwesome', 0, 1],
|
||||
['frànçAIS is über-åwesome', 'not in string', 0, FALSE],
|
||||
['frànçAIS is über-åwesome', 'r', 0, 1],
|
||||
['frànçAIS is über-åwesome', 'nçAIS', 0, 3],
|
||||
['frànçAIS is über-åwesome', 'nçAIS', 2, 3],
|
||||
['frànçAIS is über-åwesome', 'nçAIS', 3, 3],
|
||||
['以呂波耳・ほへとち。リヌルヲ。', '波耳', 0, 2],
|
||||
['以呂波耳・ほへとち。リヌルヲ。', '波耳', 1, 2],
|
||||
['以呂波耳・ほへとち。リヌルヲ。', '波耳', 2, 2],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue