NicknameTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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('PUBLICDIR')) {
  21. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  22. }
  23. if (!defined('GNUSOCIAL')) {
  24. define('GNUSOCIAL', true);
  25. }
  26. if (!defined('STATUSNET')) { // Compatibility
  27. define('STATUSNET', true);
  28. }
  29. use Nickname;
  30. use NicknameBlacklistedException;
  31. use NicknameEmptyException;
  32. use NicknameException;
  33. use NicknameInvalidException;
  34. use NicknamePathCollisionException;
  35. use NicknameTakenException;
  36. use NicknameTooLongException;
  37. use PHPUnit\Framework\TestCase;
  38. require_once INSTALLDIR . '/lib/util/common.php';
  39. /**
  40. * Test cases for nickname validity and normalization.
  41. */
  42. final class NicknameTest extends TestCase
  43. {
  44. /**
  45. * Basic test using Nickname::normalize()
  46. *
  47. * @dataProvider provider
  48. *
  49. * @param $input
  50. * @param $expected
  51. * @param null $expectedException
  52. */
  53. public function testBasic($input, $expected, $expectedException = null)
  54. {
  55. $exception = null;
  56. $normalized = false;
  57. try {
  58. $normalized = Nickname::normalize($input);
  59. } catch (NicknameException $e) {
  60. $exception = $e;
  61. }
  62. if ($expected === false) {
  63. if ($expectedException) {
  64. if ($exception) {
  65. $stuff = get_class($exception) . ': ' . $exception->getMessage();
  66. } else {
  67. $stuff = var_export($exception, true);
  68. }
  69. static::assertTrue(
  70. $exception && $exception instanceof $expectedException,
  71. "invalid input '{$input}' expected to fail with {$expectedException}, " .
  72. "got {$stuff}"
  73. );
  74. } else {
  75. static::assertTrue(
  76. $normalized == false,
  77. "invalid input '{$input}' expected to fail"
  78. );
  79. }
  80. } else {
  81. $msg = "normalized input nickname '{$input}' expected to normalize to '{$expected}', got ";
  82. if ($exception) {
  83. $msg .= get_class($exception) . ': ' . $exception->getMessage();
  84. } else {
  85. $msg .= "'{$normalized}'";
  86. }
  87. static::assertSame($expected, $normalized, $msg);
  88. }
  89. }
  90. /**
  91. * Test on the regex matching used in common_find_mentions
  92. * (testing on the full notice rendering is difficult as it needs
  93. * to be able to pull from global state)
  94. *
  95. * @dataProvider provider
  96. *
  97. * @param $input
  98. * @param $expected
  99. * @param null $expectedException
  100. *
  101. * @throws NicknameBlacklistedException
  102. * @throws NicknameEmptyException
  103. * @throws NicknameException
  104. * @throws NicknameInvalidException
  105. * @throws NicknamePathCollisionException
  106. * @throws NicknameTakenException
  107. * @throws NicknameTooLongException
  108. */
  109. public function testAtReply($input, $expected, $expectedException = null)
  110. {
  111. if ($expected == false) {
  112. // nothing to do
  113. } else {
  114. $text = "@{$input} awesome! :)";
  115. $matches = common_find_mentions_raw($text);
  116. static::assertCount(1, $matches);
  117. static::assertSame($expected, Nickname::normalize($matches[0][0]));
  118. }
  119. }
  120. public static function provider()
  121. {
  122. return [
  123. ['evan', 'evan'],
  124. // Case and underscore variants
  125. ['Evan', 'evan'],
  126. ['EVAN', 'evan'],
  127. ['ev_an', 'evan'],
  128. ['E__V_an', 'evan'],
  129. ['evan1', 'evan1'],
  130. ['evan_1', 'evan1'],
  131. ['0x20', '0x20'],
  132. ['1234', '1234'], // should this be allowed though? :)
  133. ['12__34', '1234'],
  134. // Some (currently) invalid chars...
  135. ['^#@&^#@', false, 'NicknameInvalidException'], // all invalid :D
  136. ['ev.an', false, 'NicknameInvalidException'],
  137. ['ev/an', false, 'NicknameInvalidException'],
  138. ['ev an', false, 'NicknameInvalidException'],
  139. ['ev-an', false, 'NicknameInvalidException'],
  140. // Non-ASCII letters; currently not allowed, in future
  141. // we'll add them at least with conversion to ASCII.
  142. // Not much use until we have storage of display names,
  143. // though.
  144. ['évan', false, 'NicknameInvalidException'], // so far...
  145. ['Évan', false, 'NicknameInvalidException'], // so far...
  146. // Length checks
  147. ['', false, 'NicknameEmptyException'],
  148. ['___', false, 'NicknameEmptyException'],
  149. ['eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'], // 64 chars
  150. ['eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee_', false, 'NicknameTooLongException'], // the _ is too long...
  151. ['eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', false, 'NicknameTooLongException'], // 65 chars -- too long
  152. ];
  153. }
  154. }