UnicodeTest.php 16.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

/**
 * @file
 * Contains \Drupal\Tests\Component\Utility\UnicodeTest.
 */

namespace Drupal\Tests\Component\Utility;

use Drupal\Tests\UnitTestCase;
use Drupal\Component\Utility\Unicode;

/**
14 15
 * @coversDefaultClass \Drupal\Component\Utility\Unicode
 * @group Utility
16 17 18
 */
class UnicodeTest extends UnitTestCase {

19
  protected function setUp() {
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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
    // Initialize unicode component.
    Unicode::check();
  }

  /**
   * Tests Unicode::getStatus() and Unicode::setStatus().
   *
   * @dataProvider providerTestStatus
   */
  public function testStatus($value, $expected, $invalid = FALSE) {
    if ($invalid) {
      $this->setExpectedException('InvalidArgumentException');
    }
    Unicode::setStatus($value);
    $this->assertEquals($expected, Unicode::getStatus());
  }

  /**
   * Data provider for testStatus().
   *
   * @see testStatus()
   *
   * @return array
   *   An array containing:
   *     - The status value to set.
   *     - The status value to expect after setting the new value.
   *     - (optional) Boolean indicating invalid status. Defaults to FALSE.
   */
  public function providerTestStatus() {
    return array(
      array(Unicode::STATUS_SINGLEBYTE, Unicode::STATUS_SINGLEBYTE),
      array(rand(10, 100), Unicode::STATUS_SINGLEBYTE, TRUE),
      array(rand(10, 100), Unicode::STATUS_SINGLEBYTE, TRUE),
      array(Unicode::STATUS_MULTIBYTE, Unicode::STATUS_MULTIBYTE),
      array(rand(10, 100), Unicode::STATUS_MULTIBYTE, TRUE),
      array(Unicode::STATUS_ERROR, Unicode::STATUS_ERROR),
      array(Unicode::STATUS_MULTIBYTE, Unicode::STATUS_MULTIBYTE),
    );
  }

  /**
   * Tests Unicode::mimeHeaderEncode() and Unicode::mimeHeaderDecode().
   *
   * @dataProvider providerTestMimeHeader
   */
  public function testMimeHeader($value, $encoded) {
    $this->assertEquals($encoded, Unicode::mimeHeaderEncode($value));
    $this->assertEquals($value, Unicode::mimeHeaderDecode($encoded));
  }

  /**
   * Data provider for testMimeHeader().
   *
   * @see testMimeHeader()
   *
   * @return array
   *   An array containing a string and its encoded value.
   */
  public function providerTestMimeHeader() {
    return array(
      array('tést.txt', '=?UTF-8?B?dMOpc3QudHh0?='),
81 82
      // Simple ASCII characters.
      array('ASCII', 'ASCII'),
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    );
  }

  /**
   * Tests Unicode::strtolower().
   *
   * @dataProvider providerStrtolower
   */
  public function testStrtolower($text, $expected, $multibyte = FALSE) {
    $status = $multibyte ? Unicode::STATUS_MULTIBYTE : Unicode::STATUS_SINGLEBYTE;
    Unicode::setStatus($status);
    $this->assertEquals($expected, Unicode::strtolower($text));
  }

  /**
   * Data provider for testStrtolower().
   *
   * @see testStrtolower()
   *
   * @return array
   *   An array containing a string, its lowercase version and whether it should
   *   be processed as multibyte.
   */
  public function providerStrtolower() {
    $cases = array(
      array('tHe QUIcK bRoWn', 'the quick brown'),
      array('FrançAIS is ÜBER-åwesome', 'français is über-åwesome'),
    );
    foreach ($cases as $case) {
      // Test the same string both in multibyte and singlebyte conditions.
      array_push($case, TRUE);
      $cases[] = $case;
    }
    // Add a multibyte string.
    $cases[] = array('ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ', 'αβγδεζηθικλμνξοσὠ', TRUE);
    return $cases;
  }

  /**
   * Tests Unicode::strtoupper().
   *
   * @dataProvider providerStrtoupper
   */
  public function testStrtoupper($text, $expected, $multibyte = FALSE) {
    $status = $multibyte ? Unicode::STATUS_MULTIBYTE : Unicode::STATUS_SINGLEBYTE;
    Unicode::setStatus($status);
    $this->assertEquals($expected, Unicode::strtoupper($text));
  }

  /**
   * Data provider for testStrtoupper().
   *
   * @see testStrtoupper()
   *
   * @return array
   *   An array containing a string, its uppercase version and whether it should
   *   be processed as multibyte.
   */
  public function providerStrtoupper() {
    $cases = array(
      array('tHe QUIcK bRoWn', 'THE QUICK BROWN'),
      array('FrançAIS is ÜBER-åwesome', 'FRANÇAIS IS ÜBER-ÅWESOME'),
    );
    foreach ($cases as $case) {
      // Test the same string both in multibyte and singlebyte conditions.
      array_push($case, TRUE);
      $cases[] = $case;
    }
    // Add a multibyte string.
    $cases[] = array('αβγδεζηθικλμνξοσὠ', 'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ', TRUE);
    return $cases;
  }

  /**
   * Tests Unicode::ucfirst().
   *
   * @dataProvider providerUcfirst
   */
  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() {
    return array(
      array('tHe QUIcK bRoWn', 'THe QUIcK bRoWn'),
      array('françAIS', 'FrançAIS'),
      array('über', 'Über'),
      array('åwesome', 'Åwesome'),
      // A multibyte string.
      array('σion', 'Σion'),
    );
  }

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
  /**
   * Tests Unicode::lcfirst().
   *
   * @dataProvider providerLcfirst
   */
  public function testLcfirst($text, $expected, $multibyte = FALSE) {
    $status = $multibyte ? Unicode::STATUS_MULTIBYTE : Unicode::STATUS_SINGLEBYTE;
    Unicode::setStatus($status);
    $this->assertEquals($expected, Unicode::lcfirst($text));
  }

  /**
   * Data provider for testLcfirst().
   *
   * @see testLcfirst()
   *
   * @return array
   *   An array containing a string, its lowercase version and whether it should
   *   be processed as multibyte.
   */
  public function providerLcfirst() {
    return array(
      array('tHe QUIcK bRoWn', 'tHe QUIcK bRoWn'),
      array('FrançAIS is ÜBER-åwesome', 'françAIS is ÜBER-åwesome'),
      array('Über', 'über'),
      array('Åwesome', 'åwesome'),
      // Add a multibyte string.
      array('ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ', 'αΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ', TRUE),
    );
  }

  /**
   * Tests Unicode::ucwords().
   *
   * @dataProvider providerUcwords
   */
  public function testUcwords($text, $expected, $multibyte = FALSE) {
    $status = $multibyte ? Unicode::STATUS_MULTIBYTE : Unicode::STATUS_SINGLEBYTE;
    Unicode::setStatus($status);
    $this->assertEquals($expected, Unicode::ucwords($text));
  }

  /**
   * Data provider for testUcwords().
   *
   * @see testUcwords()
   *
   * @return array
   *   An array containing a string, its capitalized version and whether it should
   *   be processed as multibyte.
   */
  public function providerUcwords() {
    return array(
      array('tHe QUIcK bRoWn', 'THe QUIcK BRoWn'),
      array('françAIS', 'FrançAIS'),
      array('über', 'Über'),
      array('åwesome', 'Åwesome'),
      // Make sure we don't mangle extra spaces.
      array('frànçAIS is  über-åwesome', 'FrànçAIS Is  Über-Åwesome'),
      // Add a multibyte string.
      array('σion', 'Σion', TRUE),
    );
  }

248 249 250 251 252 253
  /**
   * Tests Unicode::strlen().
   *
   * @dataProvider providerStrlen
   */
  public function testStrlen($text, $expected) {
254 255 256 257 258
    // 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);
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
    $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 array(
      array('tHe QUIcK bRoWn', 15),
      array('ÜBER-åwesome', 12),
    );
  }

  /**
   * Tests Unicode::substr().
   *
   * @dataProvider providerSubstr
   */
  public function testSubstr($text, $start, $length, $expected) {
283 284 285 286 287
    // 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);
288 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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 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 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
    $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 array(
      array('frànçAIS is über-åwesome', 0, 0, ''),
      array('frànçAIS is über-åwesome', 0, 1, 'f'),
      array('frànçAIS is über-åwesome', 0, 8, 'frànçAIS'),
      array('frànçAIS is über-åwesome', 0, 23, 'frànçAIS is über-åwesom'),
      array('frànçAIS is über-åwesome', 0, 24, 'frànçAIS is über-åwesome'),
      array('frànçAIS is über-åwesome', 0, 25, 'frànçAIS is über-åwesome'),
      array('frànçAIS is über-åwesome', 0, 100, 'frànçAIS is über-åwesome'),
      array('frànçAIS is über-åwesome', 4, 4, 'çAIS'),
      array('frànçAIS is über-åwesome', 1, 0, ''),
      array('frànçAIS is über-åwesome', 100, 0, ''),
      array('frànçAIS is über-åwesome', -4, 2, 'so'),
      array('frànçAIS is über-åwesome', -4, 3, 'som'),
      array('frànçAIS is über-åwesome', -4, 4, 'some'),
      array('frànçAIS is über-åwesome', -4, 5, 'some'),
      array('frànçAIS is über-åwesome', -7, 10, 'åwesome'),
      array('frànçAIS is über-åwesome', 5, -10, 'AIS is üb'),
      array('frànçAIS is über-åwesome', 0, -10, 'frànçAIS is üb'),
      array('frànçAIS is über-åwesome', 0, -1, 'frànçAIS is über-åwesom'),
      array('frànçAIS is über-åwesome', -7, -2, 'åweso'),
      array('frànçAIS is über-åwesome', -7, -6, 'å'),
      array('frànçAIS is über-åwesome', -7, -7, ''),
      array('frànçAIS is über-åwesome', -7, -8, ''),
      array('...', 0, 2, '..'),
      array('以呂波耳・ほへとち。リヌルヲ。', 1, 3, '呂波耳'),
    );
  }

  /**
   * Tests Unicode::truncate().
   *
   * @dataProvider providerTruncate
   */
  public function testTruncate($text, $max_length, $expected, $wordsafe = FALSE, $add_ellipsis = FALSE) {
    Unicode::check();
    $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() {
    return array(
      array('frànçAIS is über-åwesome', 24, 'frànçAIS is über-åwesome'),
      array('frànçAIS is über-åwesome', 23, 'frànçAIS is über-åwesom'),
      array('frànçAIS is über-åwesome', 17, 'frànçAIS is über-'),
      array('以呂波耳・ほへとち。リヌルヲ。', 6, '以呂波耳・ほ'),
      array('frànçAIS is über-åwesome', 24, 'frànçAIS is über-åwesome', FALSE, TRUE),
      array('frànçAIS is über-åwesome', 23, 'frànçAIS is über-åweso…', FALSE, TRUE),
      array('frànçAIS is über-åwesome', 17, 'frànçAIS is über…', FALSE, TRUE),
      array('123', 1, '…', TRUE, TRUE),
      array('123', 2, '1…', TRUE, TRUE),
      array('123', 3, '123', TRUE, TRUE),
      array('1234', 3, '12…', TRUE, TRUE),
      array('1234567890', 10, '1234567890', TRUE, TRUE),
      array('12345678901', 10, '123456789…', TRUE, TRUE),
      array('12345678901', 11, '12345678901', TRUE, TRUE),
      array('123456789012', 11, '1234567890…', TRUE, TRUE),
      array('12345 7890', 10, '12345 7890', TRUE, TRUE),
      array('12345 7890', 9, '12345…', TRUE, TRUE),
      array('123 567 90', 10, '123 567 90', TRUE, TRUE),
      array('123 567 901', 10, '123 567…', TRUE, TRUE),
      array('Stop. Hammertime.', 17, 'Stop. Hammertime.', TRUE, TRUE),
      array('Stop. Hammertime.', 16, 'Stop…', TRUE, TRUE),
      array('frànçAIS is über-åwesome', 24, 'frànçAIS is über-åwesome', TRUE, TRUE),
      array('frànçAIS is über-åwesome', 23, 'frànçAIS is über…', TRUE, TRUE),
      array('frànçAIS is über-åwesome', 17, 'frànçAIS is über…', TRUE, TRUE),
      array('¿Dónde está el niño?', 20, '¿Dónde está el niño?', TRUE, TRUE),
      array('¿Dónde está el niño?', 19, '¿Dónde está el…', TRUE, TRUE),
      array('¿Dónde está el niño?', 13, '¿Dónde está…', TRUE, TRUE),
      array('¿Dónde está el niño?', 10, '¿Dónde…', TRUE, TRUE),
      array('Help! Help! Help!', 17, 'Help! Help! Help!', TRUE, TRUE),
      array('Help! Help! Help!', 16, 'Help! Help!…', TRUE, TRUE),
      array('Help! Help! Help!', 15, 'Help! Help!…', TRUE, TRUE),
      array('Help! Help! Help!', 14, 'Help! Help!…', TRUE, TRUE),
      array('Help! Help! Help!', 13, 'Help! Help!…', TRUE, TRUE),
      array('Help! Help! Help!', 12, 'Help! Help!…', TRUE, TRUE),
      array('Help! Help! Help!', 11, 'Help! Help…', TRUE, TRUE),
      array('Help! Help! Help!', 10, 'Help!…', TRUE, TRUE),
      array('Help! Help! Help!', 9, 'Help!…', TRUE, TRUE),
      array('Help! Help! Help!', 8, 'Help!…', TRUE, TRUE),
      array('Help! Help! Help!', 7, 'Help!…', TRUE, TRUE),
      array('Help! Help! Help!', 6, 'Help!…', TRUE, TRUE),
      array('Help! Help! Help!', 5, 'Help…', TRUE, TRUE),
      array('Help! Help! Help!', 4, 'Hel…', TRUE, TRUE),
      array('Help! Help! Help!', 3, 'He…', TRUE, TRUE),
      array('Help! Help! Help!', 2, 'H…', TRUE, TRUE),
    );
  }

404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
  /**
   * 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
496
   *   An array of arrays, each containing the parameters to
497 498 499 500
   *   self::testConvertUtf8().  }
   */
  public function providerTestConvertToUtf8() {
    return array(
501 502 503
      array(chr(0x97), 'Windows-1252', '—'),
      array(chr(0x99), 'Windows-1252', '™'),
      array(chr(0x80), 'Windows-1252', '€'),
504 505 506
    );
  }

507
}