NicknameTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
  3. print "This script must be run from the command line\n";
  4. exit();
  5. }
  6. define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
  7. define('GNUSOCIAL', true);
  8. define('STATUSNET', true); // compatibility
  9. require_once INSTALLDIR . '/lib/common.php';
  10. /**
  11. * Test cases for nickname validity and normalization.
  12. */
  13. class NicknameTest extends PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * Basic test using Nickname::normalize()
  17. *
  18. * @dataProvider provider
  19. */
  20. public function testBasic($input, $expected, $expectedException=null)
  21. {
  22. $exception = null;
  23. $normalized = false;
  24. try {
  25. $normalized = Nickname::normalize($input);
  26. } catch (NicknameException $e) {
  27. $exception = $e;
  28. }
  29. if ($expected === false) {
  30. if ($expectedException) {
  31. if ($exception) {
  32. $stuff = get_class($exception) . ': ' . $exception->getMessage();
  33. } else {
  34. $stuff = var_export($exception, true);
  35. }
  36. $this->assertTrue($exception && $exception instanceof $expectedException,
  37. "invalid input '$input' expected to fail with $expectedException, " .
  38. "got $stuff");
  39. } else {
  40. $this->assertTrue($normalized == false,
  41. "invalid input '$input' expected to fail");
  42. }
  43. } else {
  44. $msg = "normalized input nickname '$input' expected to normalize to '$expected', got ";
  45. if ($exception) {
  46. $msg .= get_class($exception) . ': ' . $exception->getMessage();
  47. } else {
  48. $msg .= "'$normalized'";
  49. }
  50. $this->assertEquals($expected, $normalized, $msg);
  51. }
  52. }
  53. /**
  54. * Test on the regex matching used in common_find_mentions
  55. * (testing on the full notice rendering is difficult as it needs
  56. * to be able to pull from global state)
  57. *
  58. * @dataProvider provider
  59. */
  60. public function testAtReply($input, $expected, $expectedException=null)
  61. {
  62. if ($expected == false) {
  63. // nothing to do
  64. } else {
  65. $text = "@{$input} awesome! :)";
  66. $matches = common_find_mentions_raw($text);
  67. $this->assertEquals(1, count($matches));
  68. $this->assertEquals($expected, Nickname::normalize($matches[0][0]));
  69. }
  70. }
  71. static public function provider()
  72. {
  73. return array(
  74. array('evan', 'evan'),
  75. // Case and underscore variants
  76. array('Evan', 'evan'),
  77. array('EVAN', 'evan'),
  78. array('ev_an', 'evan'),
  79. array('E__V_an', 'evan'),
  80. array('evan1', 'evan1'),
  81. array('evan_1', 'evan1'),
  82. array('0x20', '0x20'),
  83. array('1234', '1234'), // should this be allowed though? :)
  84. array('12__34', '1234'),
  85. // Some (currently) invalid chars...
  86. array('^#@&^#@', false, 'NicknameInvalidException'), // all invalid :D
  87. array('ev.an', false, 'NicknameInvalidException'),
  88. array('ev/an', false, 'NicknameInvalidException'),
  89. array('ev an', false, 'NicknameInvalidException'),
  90. array('ev-an', false, 'NicknameInvalidException'),
  91. // Non-ASCII letters; currently not allowed, in future
  92. // we'll add them at least with conversion to ASCII.
  93. // Not much use until we have storage of display names,
  94. // though.
  95. array('évan', false, 'NicknameInvalidException'), // so far...
  96. array('Évan', false, 'NicknameInvalidException'), // so far...
  97. // Length checks
  98. array('', false, 'NicknameEmptyException'),
  99. array('___', false, 'NicknameEmptyException'),
  100. array('eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'), // 64 chars
  101. array('eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee_', false, 'NicknameTooLongException'), // the _ is too long...
  102. array('eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', false, 'NicknameTooLongException'), // 65 chars -- too long
  103. );
  104. }
  105. }