UnicodeTest.php 17.9 KB
Newer Older
1 2 3 4 5
<?php

namespace Drupal\Tests\Component\Utility;

use Drupal\Component\Utility\Unicode;
6
use PHPUnit\Framework\TestCase;
7 8

/**
9 10
 * Test unicode handling features implemented in Unicode component.
 *
11
 * @group Utility
12 13
 *
 * @coversDefaultClass \Drupal\Component\Utility\Unicode
14
 */
15
class UnicodeTest extends TestCase {
16

17
  /**
18 19
   * @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.
20
   */
21 22
  public function testSetStatus() {
    Unicode::setStatus(Unicode::STATUS_SINGLEBYTE);
23 24 25
  }

  /**
26
   * Tests multibyte encoding.
27 28
   *
   * @dataProvider providerTestMimeHeader
29
   * @covers ::mimeHeaderEncode
30
   */
31
  public function testMimeHeaderEncode($value, $encoded) {
32 33 34 35 36 37 38 39 40 41 42 43
    $this->assertEquals($encoded, Unicode::mimeHeaderEncode($value));
  }

  /**
   * Data provider for testMimeHeader().
   *
   * @see testMimeHeader()
   *
   * @return array
   *   An array containing a string and its encoded value.
   */
  public function providerTestMimeHeader() {
44
    return [
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
      "Base64 encoding" => ['tést.txt', '=?UTF-8?B?dMOpc3QudHh0?='],
      "ASCII characters only" => ['test.txt', 'test.txt'],
    ];
  }

  /**
   * Tests multibyte decoding.
   *
   * @dataProvider providerTestMimeHeaderDecode
   * @covers ::mimeHeaderDecode
   */
  public function testMimeHeaderDecode($value, $encoded) {
    $this->assertEquals($value, Unicode::mimeHeaderDecode($encoded));
  }

  /**
   * Data provider for testMimeHeaderDecode().
   *
   * @return array
   *   An array containing a string and its encoded value.
   */
  public function providerTestMimeHeaderDecode() {
    return [
      'Uppercase base64 encoding' => [
        'tést.txt',
        '=?utf-8?B?dMOpc3QudHh0?=',
      ],
      'Uppercase quoted-printable encoding' => [
        'tést.txt',
        '=?UTF-8?Q?t=C3=A9st.txt?=',
      ],
      'Lowercase base64 encoding' => [
        'tést.txt',
        '=?utf-8?b?dMOpc3QudHh0?=',
      ],
      'Lowercase quoted-printable encoding' => [
        'tést.txt',
        '=?UTF-8?q?t=C3=A9st.txt?=',
      ],
      'ASCII characters only' => [
        'test.txt',
        'test.txt',
      ],
88
    ];
89 90 91
  }

  /**
92
   * Tests multibyte strtolower.
93 94
   *
   * @dataProvider providerStrtolower
95
   * @covers ::strtolower
96 97
   * @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.
98
   */
99
  public function testStrtolower($text, $expected) {
100 101 102 103 104 105 106 107 108
    $this->assertEquals($expected, Unicode::strtolower($text));
  }

  /**
   * Data provider for testStrtolower().
   *
   * @see testStrtolower()
   *
   * @return array
109
   *   An array containing a string and its lowercase version.
110 111
   */
  public function providerStrtolower() {
112
    return [
113 114
      ['tHe QUIcK bRoWn', 'the quick brown'],
      ['FrançAIS is ÜBER-åwesome', 'français is über-åwesome'],
115
      ['ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ', 'αβγδεζηθικλμνξοσὠ'],
116
    ];
117 118 119
  }

  /**
120
   * Tests multibyte strtoupper.
121 122
   *
   * @dataProvider providerStrtoupper
123
   * @covers ::strtoupper
124 125
   * @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.
126
   */
127
  public function testStrtoupper($text, $expected) {
128 129 130 131 132 133 134 135 136
    $this->assertEquals($expected, Unicode::strtoupper($text));
  }

  /**
   * Data provider for testStrtoupper().
   *
   * @see testStrtoupper()
   *
   * @return array
137
   *   An array containing a string and its uppercase version.
138 139
   */
  public function providerStrtoupper() {
140
    return [
141 142
      ['tHe QUIcK bRoWn', 'THE QUICK BROWN'],
      ['FrançAIS is ÜBER-åwesome', 'FRANÇAIS IS ÜBER-ÅWESOME'],
143
      ['αβγδεζηθικλμνξοσὠ', 'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ'],
144
    ];
145 146 147
  }

  /**
148
   * Tests multibyte ucfirst.
149 150
   *
   * @dataProvider providerUcfirst
151
   * @covers ::ucfirst
152 153 154 155 156 157 158 159 160 161 162 163 164 165
   */
  public function testUcfirst($text, $expected) {
    $this->assertEquals($expected, Unicode::ucfirst($text));
  }

  /**
   * Data provider for testUcfirst().
   *
   * @see testUcfirst()
   *
   * @return array
   *   An array containing a string and its uppercase first version.
   */
  public function providerUcfirst() {
166 167 168 169 170
    return [
      ['tHe QUIcK bRoWn', 'THe QUIcK bRoWn'],
      ['françAIS', 'FrançAIS'],
      ['über', 'Über'],
      ['åwesome', 'Åwesome'],
171
      // A multibyte string.
172 173
      ['σion', 'Σion'],
    ];
174 175
  }

176
  /**
177
   * Tests multibyte lcfirst.
178 179
   *
   * @dataProvider providerLcfirst
180
   * @covers ::lcfirst
181
   */
182
  public function testLcfirst($text, $expected) {
183 184 185 186 187 188 189 190 191
    $this->assertEquals($expected, Unicode::lcfirst($text));
  }

  /**
   * Data provider for testLcfirst().
   *
   * @see testLcfirst()
   *
   * @return array
192
   *   An array containing a string and its lowercase version.
193 194
   */
  public function providerLcfirst() {
195 196 197 198 199
    return [
      ['tHe QUIcK bRoWn', 'tHe QUIcK bRoWn'],
      ['FrançAIS is ÜBER-åwesome', 'françAIS is ÜBER-åwesome'],
      ['Über', 'über'],
      ['Åwesome', 'åwesome'],
200
      // Add a multibyte string.
201
      ['ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ', 'αΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ'],
202
    ];
203 204 205
  }

  /**
206
   * Tests multibyte ucwords.
207 208
   *
   * @dataProvider providerUcwords
209
   * @covers ::ucwords
210
   */
211
  public function testUcwords($text, $expected) {
212 213 214 215 216 217 218 219 220
    $this->assertEquals($expected, Unicode::ucwords($text));
  }

  /**
   * Data provider for testUcwords().
   *
   * @see testUcwords()
   *
   * @return array
221
   *   An array containing a string and its capitalized version.
222 223
   */
  public function providerUcwords() {
224 225 226 227 228
    return [
      ['tHe QUIcK bRoWn', 'THe QUIcK BRoWn'],
      ['françAIS', 'FrançAIS'],
      ['über', 'Über'],
      ['åwesome', 'Åwesome'],
229
      // Make sure we don't mangle extra spaces.
230
      ['frànçAIS is  über-åwesome', 'FrànçAIS Is  Über-Åwesome'],
231
      // Add a multibyte string.
232
      ['σion', 'Σion'],
233
    ];
234 235
  }

236
  /**
237
   * Tests multibyte strlen.
238 239
   *
   * @dataProvider providerStrlen
240
   * @covers ::strlen
241 242
   * @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.
243 244 245 246 247 248 249 250 251 252 253 254 255 256
   */
  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() {
257 258 259 260 261
    return [
      ['tHe QUIcK bRoWn', 15],
      ['ÜBER-åwesome', 12],
      ['以呂波耳・ほへとち。リヌルヲ。', 15],
    ];
262 263 264
  }

  /**
265
   * Tests multibyte substr.
266 267
   *
   * @dataProvider providerSubstr
268
   * @covers ::substr
269 270
   * @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.
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
   */
  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() {
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
    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, '呂波耳'],
    ];
316 317 318
  }

  /**
319
   * Tests multibyte truncate.
320 321
   *
   * @dataProvider providerTruncate
322
   * @covers ::truncate
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
   */
  public function testTruncate($text, $max_length, $expected, $wordsafe = FALSE, $add_ellipsis = FALSE) {
    $this->assertEquals($expected, Unicode::truncate($text, $max_length, $wordsafe, $add_ellipsis));
  }

  /**
   * Data provider for testTruncate().
   *
   * @see testTruncate()
   *
   * @return array
   *   An array containing:
   *     - The string to test.
   *     - The max length to truncate this string to.
   *     - The expected string result.
   *     - (optional) Boolean for the $wordsafe flag. Defaults to FALSE.
   *     - (optional) Boolean for the $add_ellipsis flag. Defaults to FALSE.
   */
  public function providerTruncate() {
342
    $tests = [
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
      ['frànçAIS is über-åwesome', 24, 'frànçAIS is über-åwesome'],
      ['frànçAIS is über-åwesome', 23, 'frànçAIS is über-åwesom'],
      ['frànçAIS is über-åwesome', 17, 'frànçAIS is über-'],
      ['以呂波耳・ほへとち。リヌルヲ。', 6, '以呂波耳・ほ'],
      ['frànçAIS is über-åwesome', 24, 'frànçAIS is über-åwesome', FALSE, TRUE],
      ['frànçAIS is über-åwesome', 23, 'frànçAIS is über-åweso…', FALSE, TRUE],
      ['frànçAIS is über-åwesome', 17, 'frànçAIS is über…', FALSE, TRUE],
      ['123', 1, '…', TRUE, TRUE],
      ['123', 2, '1…', TRUE, TRUE],
      ['123', 3, '123', TRUE, TRUE],
      ['1234', 3, '12…', TRUE, TRUE],
      ['1234567890', 10, '1234567890', TRUE, TRUE],
      ['12345678901', 10, '123456789…', TRUE, TRUE],
      ['12345678901', 11, '12345678901', TRUE, TRUE],
      ['123456789012', 11, '1234567890…', TRUE, TRUE],
      ['12345 7890', 10, '12345 7890', TRUE, TRUE],
      ['12345 7890', 9, '12345…', TRUE, TRUE],
      ['123 567 90', 10, '123 567 90', TRUE, TRUE],
      ['123 567 901', 10, '123 567…', TRUE, TRUE],
      ['Stop. Hammertime.', 17, 'Stop. Hammertime.', TRUE, TRUE],
      ['Stop. Hammertime.', 16, 'Stop…', TRUE, TRUE],
      ['frànçAIS is über-åwesome', 24, 'frànçAIS is über-åwesome', TRUE, TRUE],
      ['frànçAIS is über-åwesome', 23, 'frànçAIS is über…', TRUE, TRUE],
      ['frànçAIS is über-åwesome', 17, 'frànçAIS is über…', TRUE, TRUE],
      ['¿Dónde está el niño?', 20, '¿Dónde está el niño?', TRUE, TRUE],
      ['¿Dónde está el niño?', 19, '¿Dónde está el…', TRUE, TRUE],
      ['¿Dónde está el niño?', 13, '¿Dónde está…', TRUE, TRUE],
      ['¿Dónde está el niño?', 10, '¿Dónde…', TRUE, TRUE],
      ['Help! Help! Help!', 17, 'Help! Help! Help!', TRUE, TRUE],
      ['Help! Help! Help!', 16, 'Help! Help!…', TRUE, TRUE],
      ['Help! Help! Help!', 15, 'Help! Help!…', TRUE, TRUE],
      ['Help! Help! Help!', 14, 'Help! Help!…', TRUE, TRUE],
      ['Help! Help! Help!', 13, 'Help! Help!…', TRUE, TRUE],
      ['Help! Help! Help!', 12, 'Help! Help!…', TRUE, TRUE],
      ['Help! Help! Help!', 11, 'Help! Help…', TRUE, TRUE],
      ['Help! Help! Help!', 10, 'Help!…', TRUE, TRUE],
      ['Help! Help! Help!', 9, 'Help!…', TRUE, TRUE],
      ['Help! Help! Help!', 8, 'Help!…', TRUE, TRUE],
      ['Help! Help! Help!', 7, 'Help!…', TRUE, TRUE],
      ['Help! Help! Help!', 6, 'Help!…', TRUE, TRUE],
      ['Help! Help! Help!', 5, 'Help…', TRUE, TRUE],
      ['Help! Help! Help!', 4, 'Hel…', TRUE, TRUE],
      ['Help! Help! Help!', 3, 'He…', TRUE, TRUE],
      ['Help! Help! Help!', 2, 'H…', TRUE, TRUE],
    ];
388

389
    // Test truncate on text with multiple lines.
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
    $multi_line = <<<EOF
This is a text that spans multiple lines.
Line 2 goes here.
EOF;
    $multi_line_wordsafe = <<<EOF
This is a text that spans multiple lines.
Line 2
EOF;
    $multi_line_non_wordsafe = <<<EOF
This is a text that spans multiple lines.
Line 2 go
EOF;
    $tests[] = [$multi_line, 51, $multi_line_wordsafe, TRUE];
    $tests[] = [$multi_line, 51, $multi_line_non_wordsafe, FALSE];

    return $tests;
406 407
  }

408
  /**
409 410 411 412
   * Tests multibyte truncate bytes.
   *
   * @dataProvider providerTestTruncateBytes
   * @covers ::truncateBytes
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
   *
   * @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().
   */
  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() {
433
    return [
434
      // String shorter than max length.
435
      ['Short string', 42, 'Short string'],
436
      // Simple string longer than max length.
437
      ['Longer string than previous.', 10, 'Longer str'],
438
      // Unicode.
439 440
      ['以呂波耳・ほへとち。リヌルヲ。', 10, '以呂波'],
    ];
441 442 443
  }

  /**
444 445 446 447
   * Tests UTF-8 validation.
   *
   * @dataProvider providerTestValidateUtf8
   * @covers ::validateUtf8
448 449 450
   *
   * @param string $text
   *   The text to validate.
451
   * @param bool $expected
452 453 454 455 456 457 458 459 460 461 462
   *   The expected return value from Unicode::validateUtf8().
   * @param string $message
   *   The message to display on failure.
   */
  public function testValidateUtf8($text, $expected, $message) {
    $this->assertEquals($expected, Unicode::validateUtf8($text), $message);
  }

  /**
   * Provides data for self::testValidateUtf8().
   *
463 464
   * Invalid UTF-8 examples sourced from http://stackoverflow.com/a/11709412/109119.
   *
465 466 467 468 469
   * @return array
   *   An array of arrays, each containing the parameters for
   *   self::testValidateUtf8().
   */
  public function providerTestValidateUtf8() {
470
    return [
471
      // Empty string.
472
      ['', TRUE, 'An empty string did not validate.'],
473
      // Simple text string.
474
      ['Simple text.', TRUE, 'A simple ASCII text string did not validate.'],
475
      // Invalid UTF-8, overlong 5 byte encoding.
476
      [chr(0xF8) . chr(0x80) . chr(0x80) . chr(0x80) . chr(0x80), FALSE, 'Invalid UTF-8 was validated.'],
477
      // High code-point without trailing characters.
478 479
      [chr(0xD0) . chr(0x01), FALSE, 'Invalid UTF-8 was validated.'],
    ];
480 481 482
  }

  /**
483 484 485 486
   * Tests UTF-8 conversion.
   *
   * @dataProvider providerTestConvertToUtf8
   * @covers ::convertToUtf8
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
   *
   * @param string $data
   *   The data to be converted.
   * @param string $encoding
   *   The encoding the data is in.
   * @param string|bool $expected
   *   The expected result.
   */
  public function testConvertToUtf8($data, $encoding, $expected) {
    $this->assertEquals($expected, Unicode::convertToUtf8($data, $encoding));
  }

  /**
   * Provides data to self::testConvertToUtf8().
   *
   * @return array
503
   *   An array of arrays, each containing the parameters to
504 505 506
   *   self::testConvertUtf8().  }
   */
  public function providerTestConvertToUtf8() {
507 508 509 510 511
    return [
      [chr(0x97), 'Windows-1252', '—'],
      [chr(0x99), 'Windows-1252', '™'],
      [chr(0x80), 'Windows-1252', '€'],
    ];
512 513
  }

514 515 516 517 518
  /**
   * Tests multibyte strpos.
   *
   * @dataProvider providerStrpos
   * @covers ::strpos
519 520
   * @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.
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
   */
  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() {
539 540 541 542 543 544 545 546 547 548 549 550
    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],
    ];
551 552
  }

553
}