NicknameTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. namespace Tests\Unit;
  17. if (!defined('INSTALLDIR')) {
  18. define('INSTALLDIR', dirname(dirname(__DIR__)));
  19. }
  20. if (!defined('GNUSOCIAL')) {
  21. define('GNUSOCIAL', true);
  22. }
  23. if (!defined('STATUSNET')) { // Compatibility
  24. define('STATUSNET', true);
  25. }
  26. use Nickname;
  27. use NicknameBlacklistedException;
  28. use NicknameEmptyException;
  29. use NicknameException;
  30. use NicknameInvalidException;
  31. use NicknamePathCollisionException;
  32. use NicknameTakenException;
  33. use NicknameTooLongException;
  34. use PHPUnit\Framework\TestCase;
  35. require_once INSTALLDIR . '/lib/common.php';
  36. /**
  37. * Test cases for nickname validity and normalization.
  38. */
  39. final class NicknameTest extends TestCase
  40. {
  41. /**
  42. * Basic test using Nickname::normalize()
  43. *
  44. * @dataProvider provider
  45. * @param $input
  46. * @param $expected
  47. * @param null $expectedException
  48. */
  49. public function testBasic($input, $expected, $expectedException = null)
  50. {
  51. $exception = null;
  52. $normalized = false;
  53. try {
  54. $normalized = Nickname::normalize($input);
  55. } catch (NicknameException $e) {
  56. $exception = $e;
  57. }
  58. if ($expected === false) {
  59. if ($expectedException) {
  60. if ($exception) {
  61. $stuff = get_class($exception) . ': ' . $exception->getMessage();
  62. } else {
  63. $stuff = var_export($exception, true);
  64. }
  65. $this->assertTrue($exception && $exception instanceof $expectedException,
  66. "invalid input '$input' expected to fail with $expectedException, " .
  67. "got $stuff");
  68. } else {
  69. $this->assertTrue($normalized == false,
  70. "invalid input '$input' expected to fail");
  71. }
  72. } else {
  73. $msg = "normalized input nickname '$input' expected to normalize to '$expected', got ";
  74. if ($exception) {
  75. $msg .= get_class($exception) . ': ' . $exception->getMessage();
  76. } else {
  77. $msg .= "'$normalized'";
  78. }
  79. $this->assertEquals($expected, $normalized, $msg);
  80. }
  81. }
  82. /**
  83. * Test on the regex matching used in common_find_mentions
  84. * (testing on the full notice rendering is difficult as it needs
  85. * to be able to pull from global state)
  86. *
  87. * @dataProvider provider
  88. * @param $input
  89. * @param $expected
  90. * @param null $expectedException
  91. * @throws NicknameBlacklistedException
  92. * @throws NicknameEmptyException
  93. * @throws NicknameException
  94. * @throws NicknameInvalidException
  95. * @throws NicknamePathCollisionException
  96. * @throws NicknameTakenException
  97. * @throws NicknameTooLongException
  98. */
  99. public function testAtReply($input, $expected, $expectedException = null)
  100. {
  101. if ($expected == false) {
  102. // nothing to do
  103. } else {
  104. $text = "@{$input} awesome! :)";
  105. $matches = common_find_mentions_raw($text);
  106. $this->assertEquals(1, count($matches));
  107. $this->assertEquals($expected, Nickname::normalize($matches[0][0]));
  108. }
  109. }
  110. static public function provider()
  111. {
  112. return array(
  113. array('evan', 'evan'),
  114. // Case and underscore variants
  115. array('Evan', 'evan'),
  116. array('EVAN', 'evan'),
  117. array('ev_an', 'evan'),
  118. array('E__V_an', 'evan'),
  119. array('evan1', 'evan1'),
  120. array('evan_1', 'evan1'),
  121. array('0x20', '0x20'),
  122. array('1234', '1234'), // should this be allowed though? :)
  123. array('12__34', '1234'),
  124. // Some (currently) invalid chars...
  125. array('^#@&^#@', false, 'NicknameInvalidException'), // all invalid :D
  126. array('ev.an', false, 'NicknameInvalidException'),
  127. array('ev/an', false, 'NicknameInvalidException'),
  128. array('ev an', false, 'NicknameInvalidException'),
  129. array('ev-an', false, 'NicknameInvalidException'),
  130. // Non-ASCII letters; currently not allowed, in future
  131. // we'll add them at least with conversion to ASCII.
  132. // Not much use until we have storage of display names,
  133. // though.
  134. array('évan', false, 'NicknameInvalidException'), // so far...
  135. array('Évan', false, 'NicknameInvalidException'), // so far...
  136. // Length checks
  137. array('', false, 'NicknameEmptyException'),
  138. array('___', false, 'NicknameEmptyException'),
  139. array('eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'), // 64 chars
  140. array('eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee_', false, 'NicknameTooLongException'), // the _ is too long...
  141. array('eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', false, 'NicknameTooLongException'), // 65 chars -- too long
  142. );
  143. }
  144. }