NicknameTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/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. * @param $input
  49. * @param $expected
  50. * @param null $expectedException
  51. */
  52. public function testBasic($input, $expected, $expectedException = null)
  53. {
  54. $exception = null;
  55. $normalized = false;
  56. try {
  57. $normalized = Nickname::normalize($input);
  58. } catch (NicknameException $e) {
  59. $exception = $e;
  60. }
  61. if ($expected === false) {
  62. if ($expectedException) {
  63. if ($exception) {
  64. $stuff = get_class($exception) . ': ' . $exception->getMessage();
  65. } else {
  66. $stuff = var_export($exception, true);
  67. }
  68. $this->assertTrue($exception && $exception instanceof $expectedException,
  69. "invalid input '$input' expected to fail with $expectedException, " .
  70. "got $stuff");
  71. } else {
  72. $this->assertTrue($normalized == false,
  73. "invalid input '$input' expected to fail");
  74. }
  75. } else {
  76. $msg = "normalized input nickname '$input' expected to normalize to '$expected', got ";
  77. if ($exception) {
  78. $msg .= get_class($exception) . ': ' . $exception->getMessage();
  79. } else {
  80. $msg .= "'$normalized'";
  81. }
  82. $this->assertEquals($expected, $normalized, $msg);
  83. }
  84. }
  85. /**
  86. * Test on the regex matching used in common_find_mentions
  87. * (testing on the full notice rendering is difficult as it needs
  88. * to be able to pull from global state)
  89. *
  90. * @dataProvider provider
  91. * @param $input
  92. * @param $expected
  93. * @param null $expectedException
  94. * @throws NicknameBlacklistedException
  95. * @throws NicknameEmptyException
  96. * @throws NicknameException
  97. * @throws NicknameInvalidException
  98. * @throws NicknamePathCollisionException
  99. * @throws NicknameTakenException
  100. * @throws NicknameTooLongException
  101. */
  102. public function testAtReply($input, $expected, $expectedException = null)
  103. {
  104. if ($expected == false) {
  105. // nothing to do
  106. } else {
  107. $text = "@{$input} awesome! :)";
  108. $matches = common_find_mentions_raw($text);
  109. $this->assertEquals(1, count($matches));
  110. $this->assertEquals($expected, Nickname::normalize($matches[0][0]));
  111. }
  112. }
  113. static public function provider()
  114. {
  115. return array(
  116. array('evan', 'evan'),
  117. // Case and underscore variants
  118. array('Evan', 'evan'),
  119. array('EVAN', 'evan'),
  120. array('ev_an', 'evan'),
  121. array('E__V_an', 'evan'),
  122. array('evan1', 'evan1'),
  123. array('evan_1', 'evan1'),
  124. array('0x20', '0x20'),
  125. array('1234', '1234'), // should this be allowed though? :)
  126. array('12__34', '1234'),
  127. // Some (currently) invalid chars...
  128. array('^#@&^#@', false, 'NicknameInvalidException'), // all invalid :D
  129. array('ev.an', false, 'NicknameInvalidException'),
  130. array('ev/an', false, 'NicknameInvalidException'),
  131. array('ev an', false, 'NicknameInvalidException'),
  132. array('ev-an', false, 'NicknameInvalidException'),
  133. // Non-ASCII letters; currently not allowed, in future
  134. // we'll add them at least with conversion to ASCII.
  135. // Not much use until we have storage of display names,
  136. // though.
  137. array('évan', false, 'NicknameInvalidException'), // so far...
  138. array('Évan', false, 'NicknameInvalidException'), // so far...
  139. // Length checks
  140. array('', false, 'NicknameEmptyException'),
  141. array('___', false, 'NicknameEmptyException'),
  142. array('eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'), // 64 chars
  143. array('eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee_', false, 'NicknameTooLongException'), // the _ is too long...
  144. array('eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', false, 'NicknameTooLongException'), // 65 chars -- too long
  145. );
  146. }
  147. }