Testing unicode.inc

From: Damien Tournoud <damien@tournoud.net>


---

 modules/simpletest/tests/unicode.test |  140 +++++++++++++++++++++++++++++++++
 1 files changed, 140 insertions(+), 0 deletions(-)
 create mode 100644 modules/simpletest/tests/unicode.test


diff --git modules/simpletest/tests/unicode.test modules/simpletest/tests/unicode.test
new file mode 100644
index 0000000..2d8111c
--- /dev/null
+++ modules/simpletest/tests/unicode.test
@@ -0,0 +1,140 @@
+<?php
+// $Id $
+
+/**
+ * @file
+ * Various unicode handling tests.
+ */
+
+/**
+ * Test unicode handling features implemented in unicode.inc.
+ */
+class UnicodeUnitTest extends DrupalWebTestCase {
+
+  /**
+   * Whether to run the extended version of the tests (including non latin1 characters).
+   *
+   * @var boolean
+   */
+  protected $extendedMode = FALSE;
+
+  function getInfo() {
+    return array(
+      'name' => t('Unicode handling'),
+      'description' => t('Tests Drupal Unicode handling.'),
+      'group' => t('System'),
+    );
+  }
+
+  /**
+   * Test full unicode features implemented using the mbstring extension.
+   */
+  function testMbStringUnicode() {
+    global $multibyte;
+
+    // mbstring was not detected on this installation, there is no way to test
+    // multibyte features. Treat that as an exception.
+    if ($multibyte == UNICODE_SINGLEBYTE) {
+      $this->error(t("Unable to test Multibyte features: mbstring extension was not detected."));
+    }
+
+    $multibyte = UNICODE_MULTIBYTE;
+
+    $this->extendedMode = TRUE;
+
+    $this->helperTestStrToLower();
+    $this->helperTestStrToUpper();
+    $this->helperTestUcFirst();
+    $this->helperTestStrLen();
+    $this->helperTestSubStr();
+  }
+
+  /**
+   * Test emulated unicode features.
+   */
+  function testEmulatedUnicode() {
+    global $multibyte;
+
+    $multibyte = UNICODE_SINGLEBYTE;
+
+    $this->extendedMode = FALSE;
+
+    $this->helperTestStrToLower();
+    $this->helperTestStrToUpper();
+    $this->helperTestUcFirst();
+    $this->helperTestStrLen();
+    $this->helperTestSubStr();
+  }
+
+  function helperTestStrToLower() {
+    $testcase = array(
+      "tHe QUIcK bRoWn" => "the quick brown",
+      "FrançAIS is ÜBER-åwesome" => "français is über-åwesome",
+    );
+    if ($this->extendedMode) {
+      $testcase["aBIΣßΣ"] = "abiσßς";
+    }
+
+    foreach ($testcase as $input => $output) {
+      $this->assertEqual(drupal_strtolower($input), $output, t("%input is lowercased as %output", array('%input' => $input, '%output' => $output)));
+    }
+  }
+
+  function helperTestStrToUpper() {
+    $testcase = array(
+      "tHe QUIcK bRoWn" => "THE QUICK BROWN",
+      "FrançAIS is ÜBER-åwesome" => "FRANÇAIS IS ÜBER-ÅWESOME",
+    );
+    if ($this->extendedMode) {
+      $testcase["aBiσßς"] = "ABIΣSSΣ";
+    }
+
+    foreach ($testcase as $input => $output) {
+      $this->assertEqual(drupal_strtoupper($input), $output, t("%input is uppercased as %output", array('%input' => $input, '%output' => $output)));
+    }
+  }
+
+  function helperTestUcFirst() {
+    $testcase = array(
+      "tHe QUIcK bRoWn" => "THe QUIcK bRoWn",
+      "françAIS" => "FrançAIS",
+      "über" => "Über",
+      "åwesome" => "Åwesome"
+    );
+    if ($this->extendedMode) {
+      $testcase["σion"] = "Σion";
+    }
+
+    foreach ($testcase as $input => $output) {
+      $this->assertEqual(drupal_ucfirst($input), $output, t("%input is ucfirst-ed as %output", array('%input' => $input, '%output' => $output)));
+    }
+  }
+
+  function helperTestStrLen() {
+    $testcase = array(
+      "tHe QUIcK bRoWn" => 15,
+      "ÜBER-åwesome" => 12,
+    );
+
+    foreach ($testcase as $input => $output) {
+      $this->assertEqual(drupal_strlen($input), $output, t("%input length is %output", array('%input' => $input, '%output' => $output)));
+    }
+  }
+
+  function helperTestSubStr() {
+    // Some preliminary tests.
+    $this->assertIdentical('', drupal_substr("test", 1, 0), t("drupal_substr() returns an empty string when zero characters are requested"));
+    $this->assertIdentical('', drupal_substr("test", 100, 10), t("drupal_substr() returns an empty string when past the end of the string"));
+
+    // We call drupal_substr($input, 4, 3).
+    $testcase = array(
+      "tHe QUIcK bRoWn" => "QUI",
+      "frànçAIS" => "çAI",
+      "über-åwesome" => "-åw",
+    );
+
+    foreach ($testcase as $input => $output) {
+      $this->assertEqual(drupal_substr($input, 4, 3), $output, t("%input substring-ed at offset 4 for 3 characters is %output", array('%input' => $input, '%output' => $output)));
+    }
+  }
+}
