unicode.test

Various unicode handling tests.

File

drupal-7.x/modules/simpletest/tests/unicode.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * Various unicode handling tests.
  5. */
  6. /**
  7. * Test unicode handling features implemented in unicode.inc.
  8. */
  9. class UnicodeUnitTest extends DrupalUnitTestCase {
  10. /**
  11. * Whether to run the extended version of the tests (including non latin1 characters).
  12. *
  13. * @var boolean
  14. */
  15. protected $extendedMode = FALSE;
  16. public static function getInfo() {
  17. return array(
  18. 'name' => 'Unicode handling',
  19. 'description' => 'Tests Drupal Unicode handling.',
  20. 'group' => 'System',
  21. );
  22. }
  23. /**
  24. * Test full unicode features implemented using the mbstring extension.
  25. */
  26. function testMbStringUnicode() {
  27. global $multibyte;
  28. // mbstring was not detected on this installation, there is no way to test
  29. // multibyte features. Treat that as an exception.
  30. if ($multibyte == UNICODE_SINGLEBYTE) {
  31. $this->error(t('Unable to test Multibyte features: mbstring extension was not detected.'));
  32. }
  33. $multibyte = UNICODE_MULTIBYTE;
  34. $this->extendedMode = TRUE;
  35. $this->pass(t('Testing in mbstring mode'));
  36. $this->helperTestStrToLower();
  37. $this->helperTestStrToUpper();
  38. $this->helperTestUcFirst();
  39. $this->helperTestStrLen();
  40. $this->helperTestSubStr();
  41. $this->helperTestTruncate();
  42. }
  43. /**
  44. * Test emulated unicode features.
  45. */
  46. function testEmulatedUnicode() {
  47. global $multibyte;
  48. $multibyte = UNICODE_SINGLEBYTE;
  49. $this->extendedMode = FALSE;
  50. $this->pass(t('Testing in emulated (best-effort) mode'));
  51. $this->helperTestStrToLower();
  52. $this->helperTestStrToUpper();
  53. $this->helperTestUcFirst();
  54. $this->helperTestStrLen();
  55. $this->helperTestSubStr();
  56. $this->helperTestTruncate();
  57. }
  58. function helperTestStrToLower() {
  59. $testcase = array(
  60. 'tHe QUIcK bRoWn' => 'the quick brown',
  61. 'FrançAIS is ÜBER-åwesome' => 'français is über-åwesome',
  62. );
  63. if ($this->extendedMode) {
  64. $testcase['ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ'] = 'αβγδεζηθικλμνξοσὠ';
  65. }
  66. foreach ($testcase as $input => $output) {
  67. $this->assertEqual(drupal_strtolower($input), $output, format_string('%input is lowercased as %output', array('%input' => $input, '%output' => $output)));
  68. }
  69. }
  70. function helperTestStrToUpper() {
  71. $testcase = array(
  72. 'tHe QUIcK bRoWn' => 'THE QUICK BROWN',
  73. 'FrançAIS is ÜBER-åwesome' => 'FRANÇAIS IS ÜBER-ÅWESOME',
  74. );
  75. if ($this->extendedMode) {
  76. $testcase['αβγδεζηθικλμνξοσὠ'] = 'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ';
  77. }
  78. foreach ($testcase as $input => $output) {
  79. $this->assertEqual(drupal_strtoupper($input), $output, format_string('%input is uppercased as %output', array('%input' => $input, '%output' => $output)));
  80. }
  81. }
  82. function helperTestUcFirst() {
  83. $testcase = array(
  84. 'tHe QUIcK bRoWn' => 'THe QUIcK bRoWn',
  85. 'françAIS' => 'FrançAIS',
  86. 'über' => 'Über',
  87. 'åwesome' => 'Åwesome'
  88. );
  89. if ($this->extendedMode) {
  90. $testcase['σion'] = 'Σion';
  91. }
  92. foreach ($testcase as $input => $output) {
  93. $this->assertEqual(drupal_ucfirst($input), $output, format_string('%input is ucfirst-ed as %output', array('%input' => $input, '%output' => $output)));
  94. }
  95. }
  96. function helperTestStrLen() {
  97. $testcase = array(
  98. 'tHe QUIcK bRoWn' => 15,
  99. 'ÜBER-åwesome' => 12,
  100. );
  101. foreach ($testcase as $input => $output) {
  102. $this->assertEqual(drupal_strlen($input), $output, format_string('%input length is %output', array('%input' => $input, '%output' => $output)));
  103. }
  104. }
  105. function helperTestSubStr() {
  106. $testcase = array(
  107. // 012345678901234567890123
  108. array('frànçAIS is über-åwesome', 0, 0,
  109. ''),
  110. array('frànçAIS is über-åwesome', 0, 1,
  111. 'f'),
  112. array('frànçAIS is über-åwesome', 0, 8,
  113. 'frànçAIS'),
  114. array('frànçAIS is über-åwesome', 0, 23,
  115. 'frànçAIS is über-åwesom'),
  116. array('frànçAIS is über-åwesome', 0, 24,
  117. 'frànçAIS is über-åwesome'),
  118. array('frànçAIS is über-åwesome', 0, 25,
  119. 'frànçAIS is über-åwesome'),
  120. array('frànçAIS is über-åwesome', 0, 100,
  121. 'frànçAIS is über-åwesome'),
  122. array('frànçAIS is über-åwesome', 4, 4,
  123. 'çAIS'),
  124. array('frànçAIS is über-åwesome', 1, 0,
  125. ''),
  126. array('frànçAIS is über-åwesome', 100, 0,
  127. ''),
  128. array('frànçAIS is über-åwesome', -4, 2,
  129. 'so'),
  130. array('frànçAIS is über-åwesome', -4, 3,
  131. 'som'),
  132. array('frànçAIS is über-åwesome', -4, 4,
  133. 'some'),
  134. array('frànçAIS is über-åwesome', -4, 5,
  135. 'some'),
  136. array('frànçAIS is über-åwesome', -7, 10,
  137. 'åwesome'),
  138. array('frànçAIS is über-åwesome', 5, -10,
  139. 'AIS is üb'),
  140. array('frànçAIS is über-åwesome', 0, -10,
  141. 'frànçAIS is üb'),
  142. array('frànçAIS is über-åwesome', 0, -1,
  143. 'frànçAIS is über-åwesom'),
  144. array('frànçAIS is über-åwesome', -7, -2,
  145. 'åweso'),
  146. array('frànçAIS is über-åwesome', -7, -6,
  147. 'å'),
  148. array('frànçAIS is über-åwesome', -7, -7,
  149. ''),
  150. array('frànçAIS is über-åwesome', -7, -8,
  151. ''),
  152. array('...', 0, 2, '..'),
  153. array('以呂波耳・ほへとち。リヌルヲ。', 1, 3,
  154. '呂波耳'),
  155. );
  156. foreach ($testcase as $test) {
  157. list($input, $start, $length, $output) = $test;
  158. $result = drupal_substr($input, $start, $length);
  159. $this->assertEqual($result, $output, format_string('%input substring at offset %offset for %length characters is %output (got %result)', array('%input' => $input, '%offset' => $start, '%length' => $length, '%output' => $output, '%result' => $result)));
  160. }
  161. }
  162. /**
  163. * Test decode_entities().
  164. */
  165. function testDecodeEntities() {
  166. $testcase = array(
  167. 'Drupal' => 'Drupal',
  168. '<script>' => '<script>',
  169. '&lt;script&gt;' => '<script>',
  170. '&#60;script&#62;' => '<script>',
  171. '&amp;lt;script&amp;gt;' => '&lt;script&gt;',
  172. '"' => '"',
  173. '&#34;' => '"',
  174. '&amp;#34;' => '&#34;',
  175. '&quot;' => '"',
  176. '&amp;quot;' => '&quot;',
  177. "'" => "'",
  178. '&#39;' => "'",
  179. '&amp;#39;' => '&#39;',
  180. '©' => '©',
  181. '&copy;' => '©',
  182. '&#169;' => '©',
  183. '→' => '→',
  184. '&#8594;' => '→',
  185. '➼' => '➼',
  186. '&#10172;' => '➼',
  187. '&euro;' => '€',
  188. );
  189. foreach ($testcase as $input => $output) {
  190. $this->assertEqual(decode_entities($input), $output, format_string('Make sure the decoded entity of @input is @output', array('@input' => $input, '@output' => $output)));
  191. }
  192. }
  193. /**
  194. * Tests truncate_utf8().
  195. */
  196. function helperTestTruncate() {
  197. // Each case is an array with input string, length to truncate to, and
  198. // expected return value.
  199. // Test non-wordsafe, non-ellipsis cases.
  200. $non_wordsafe_non_ellipsis_cases = array(
  201. array('frànçAIS is über-åwesome', 24, 'frànçAIS is über-åwesome'),
  202. array('frànçAIS is über-åwesome', 23, 'frànçAIS is über-åwesom'),
  203. array('frànçAIS is über-åwesome', 17, 'frànçAIS is über-'),
  204. array('以呂波耳・ほへとち。リヌルヲ。', 6, '以呂波耳・ほ'),
  205. );
  206. $this->runTruncateTests($non_wordsafe_non_ellipsis_cases, FALSE, FALSE);
  207. // Test non-wordsafe, ellipsis cases.
  208. $non_wordsafe_ellipsis_cases = array(
  209. array('frànçAIS is über-åwesome', 24, 'frànçAIS is über-åwesome'),
  210. array('frànçAIS is über-åwesome', 23, 'frànçAIS is über-åwe...'),
  211. array('frànçAIS is über-åwesome', 17, 'frànçAIS is üb...'),
  212. );
  213. $this->runTruncateTests($non_wordsafe_ellipsis_cases, FALSE, TRUE);
  214. // Test wordsafe, ellipsis cases.
  215. $wordsafe_ellipsis_cases = array(
  216. array('123', 1, '.'),
  217. array('123', 2, '..'),
  218. array('123', 3, '123'),
  219. array('1234', 3, '...'),
  220. array('1234567890', 10, '1234567890'),
  221. array('12345678901', 10, '1234567...'),
  222. array('12345678901', 11, '12345678901'),
  223. array('123456789012', 11, '12345678...'),
  224. array('12345 7890', 10, '12345 7890'),
  225. array('12345 7890', 9, '12345...'),
  226. array('123 567 90', 10, '123 567 90'),
  227. array('123 567 901', 10, '123 567...'),
  228. array('Stop. Hammertime.', 17, 'Stop. Hammertime.'),
  229. array('Stop. Hammertime.', 16, 'Stop....'),
  230. array('frànçAIS is über-åwesome', 24, 'frànçAIS is über-åwesome'),
  231. array('frànçAIS is über-åwesome', 23, 'frànçAIS is über...'),
  232. array('frànçAIS is über-åwesome', 17, 'frànçAIS is...'),
  233. array('¿Dónde está el niño?', 20, '¿Dónde está el niño?'),
  234. array('¿Dónde está el niño?', 19, '¿Dónde está el...'),
  235. array('¿Dónde está el niño?', 15, '¿Dónde está...'),
  236. array('¿Dónde está el niño?', 10, '¿Dónde...'),
  237. array('Help! Help! Help!', 17, 'Help! Help! Help!'),
  238. array('Help! Help! Help!', 16, 'Help! Help!...'),
  239. array('Help! Help! Help!', 15, 'Help! Help!...'),
  240. array('Help! Help! Help!', 14, 'Help! Help!...'),
  241. array('Help! Help! Help!', 13, 'Help! Help...'),
  242. array('Help! Help! Help!', 12, 'Help!...'),
  243. array('Help! Help! Help!', 11, 'Help!...'),
  244. array('Help! Help! Help!', 10, 'Help!...'),
  245. array('Help! Help! Help!', 9, 'Help!...'),
  246. array('Help! Help! Help!', 8, 'Help!...'),
  247. array('Help! Help! Help!', 7, 'Help...'),
  248. array('Help! Help! Help!', 6, 'Hel...'),
  249. array('Help! Help! Help!', 5, 'He...'),
  250. );
  251. $this->runTruncateTests($wordsafe_ellipsis_cases, TRUE, TRUE);
  252. }
  253. /**
  254. * Runs test cases for helperTestTruncate().
  255. *
  256. * Runs each test case through truncate_utf8() and compares the output
  257. * to the expected output.
  258. *
  259. * @param $cases
  260. * Cases array. Each case is an array with the input string, length to
  261. * truncate to, and expected output.
  262. * @param $wordsafe
  263. * TRUE to use word-safe truncation, FALSE to not use word-safe truncation.
  264. * @param $ellipsis
  265. * TRUE to append ... if the input is truncated, FALSE to not append ....
  266. */
  267. function runTruncateTests($cases, $wordsafe, $ellipsis) {
  268. foreach ($cases as $case) {
  269. list($input, $max_length, $expected) = $case;
  270. $output = truncate_utf8($input, $max_length, $wordsafe, $ellipsis);
  271. $this->assertEqual($output, $expected, format_string('%input truncate to %length characters with %wordsafe, %ellipsis is %expected (got %output)', array('%input' => $input, '%length' => $max_length, '%output' => $output, '%expected' => $expected, '%wordsafe' => ($wordsafe ? 'word-safe' : 'not word-safe'), '%ellipsis' => ($ellipsis ? 'ellipsis' : 'not ellipsis'))));
  272. }
  273. }
  274. }