You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommentTest.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace Test\Comments;
  3. use OC\Comments\Comment;
  4. use OCP\Comments\IComment;
  5. use Test\TestCase;
  6. class CommentTest extends TestCase {
  7. /**
  8. * @throws \OCP\Comments\IllegalIDChangeException
  9. */
  10. public function testSettersValidInput() {
  11. $comment = new Comment();
  12. $id = 'comment23';
  13. $parentId = 'comment11.5';
  14. $topMostParentId = 'comment11.0';
  15. $childrenCount = 6;
  16. $message = 'I like to comment comment';
  17. $verb = 'comment';
  18. $actor = ['type' => 'users', 'id' => 'alice'];
  19. $creationDT = new \DateTime();
  20. $latestChildDT = new \DateTime('yesterday');
  21. $object = ['type' => 'files', 'id' => 'file64'];
  22. $referenceId = sha1('referenceId');
  23. $metaData = ['last_edit_actor_id' => 'admin'];
  24. $comment
  25. ->setId($id)
  26. ->setParentId($parentId)
  27. ->setTopmostParentId($topMostParentId)
  28. ->setChildrenCount($childrenCount)
  29. ->setMessage($message)
  30. ->setVerb($verb)
  31. ->setActor($actor['type'], $actor['id'])
  32. ->setCreationDateTime($creationDT)
  33. ->setLatestChildDateTime($latestChildDT)
  34. ->setObject($object['type'], $object['id'])
  35. ->setReferenceId($referenceId)
  36. ->setMetaData($metaData);
  37. $this->assertSame($id, $comment->getId());
  38. $this->assertSame($parentId, $comment->getParentId());
  39. $this->assertSame($topMostParentId, $comment->getTopmostParentId());
  40. $this->assertSame($childrenCount, $comment->getChildrenCount());
  41. $this->assertSame($message, $comment->getMessage());
  42. $this->assertSame($verb, $comment->getVerb());
  43. $this->assertSame($actor['type'], $comment->getActorType());
  44. $this->assertSame($actor['id'], $comment->getActorId());
  45. $this->assertSame($creationDT, $comment->getCreationDateTime());
  46. $this->assertSame($latestChildDT, $comment->getLatestChildDateTime());
  47. $this->assertSame($object['type'], $comment->getObjectType());
  48. $this->assertSame($object['id'], $comment->getObjectId());
  49. $this->assertSame($referenceId, $comment->getReferenceId());
  50. $this->assertSame($metaData, $comment->getMetaData());
  51. }
  52. public function testSetIdIllegalInput() {
  53. $this->expectException(\OCP\Comments\IllegalIDChangeException::class);
  54. $comment = new Comment();
  55. $comment->setId('c23');
  56. $comment->setId('c17');
  57. }
  58. /**
  59. * @throws \OCP\Comments\IllegalIDChangeException
  60. */
  61. public function testResetId() {
  62. $comment = new Comment();
  63. $comment->setId('c23');
  64. $comment->setId('');
  65. $this->assertSame('', $comment->getId());
  66. }
  67. public function simpleSetterProvider() {
  68. return [
  69. ['Id', true],
  70. ['TopmostParentId', true],
  71. ['ParentId', true],
  72. ['Message', true],
  73. ['Verb', true],
  74. ['Verb', ''],
  75. ['ChildrenCount', true],
  76. ];
  77. }
  78. /**
  79. * @dataProvider simpleSetterProvider
  80. */
  81. public function testSimpleSetterInvalidInput($field, $input) {
  82. $this->expectException(\InvalidArgumentException::class);
  83. $comment = new Comment();
  84. $setter = 'set' . $field;
  85. $comment->$setter($input);
  86. }
  87. public function roleSetterProvider() {
  88. return [
  89. ['Actor', true, true],
  90. ['Actor', 'users', true],
  91. ['Actor', true, 'alice'],
  92. ['Actor', ' ', ' '],
  93. ['Object', true, true],
  94. ['Object', 'files', true],
  95. ['Object', true, 'file64'],
  96. ['Object', ' ', ' '],
  97. ];
  98. }
  99. /**
  100. * @dataProvider roleSetterProvider
  101. */
  102. public function testSetRoleInvalidInput($role, $type, $id) {
  103. $this->expectException(\InvalidArgumentException::class);
  104. $comment = new Comment();
  105. $setter = 'set' . $role;
  106. $comment->$setter($type, $id);
  107. }
  108. public function testSetUberlongMessage() {
  109. $this->expectException(\OCP\Comments\MessageTooLongException::class);
  110. $comment = new Comment();
  111. $msg = str_pad('', IComment::MAX_MESSAGE_LENGTH + 1, 'x');
  112. $comment->setMessage($msg);
  113. }
  114. public function mentionsProvider() {
  115. return [
  116. [
  117. '@alice @bob look look, a cook!', ['alice', 'bob']
  118. ],
  119. [
  120. 'no mentions in this message', []
  121. ],
  122. [
  123. '@alice @bob look look, a duplication @alice test @bob!', ['alice', 'bob']
  124. ],
  125. [
  126. '@alice is the author, notify @bob, nevertheless mention her!', ['alice', 'bob'], 'alice'
  127. ],
  128. [
  129. '@foobar and @barfoo you should know, @foo@bar.com is valid' .
  130. ' and so is @bar@foo.org@foobar.io I hope that clarifies everything.' .
  131. ' cc @23452-4333-54353-2342 @yolo!' .
  132. ' however the most important thing to know is that www.croissant.com/@oil is not valid' .
  133. ' and won\'t match anything at all',
  134. ['bar@foo.org@foobar.io', '23452-4333-54353-2342', 'foo@bar.com', 'foobar', 'barfoo', 'yolo']
  135. ],
  136. [
  137. '@@chef is also a valid mention, no matter how strange it looks', ['@chef']
  138. ],
  139. [
  140. 'Also @"user with spaces" are now supported', ['user with spaces']
  141. ],
  142. [
  143. 'Also @"guest/0123456789abcdef" are now supported', [], null, ['guest/0123456789abcdef']
  144. ],
  145. [
  146. 'Also @"group/My Group ID 321" are now supported', [], null, [], ['My Group ID 321']
  147. ],
  148. ];
  149. }
  150. /**
  151. * @dataProvider mentionsProvider
  152. *
  153. * @param string $message
  154. * @param array $expectedUids
  155. * @param string|null $author
  156. * @param array $expectedGuests
  157. */
  158. public function testMentions(string $message, array $expectedUids, ?string $author = null, array $expectedGuests = [], array $expectedGroups = []): void {
  159. $comment = new Comment();
  160. $comment->setMessage($message);
  161. if (!is_null($author)) {
  162. $comment->setActor('user', $author);
  163. }
  164. $mentions = $comment->getMentions();
  165. while ($mention = array_shift($mentions)) {
  166. if ($mention['type'] === 'user') {
  167. $id = array_shift($expectedUids);
  168. } elseif ($mention['type'] === 'guest') {
  169. $id = array_shift($expectedGuests);
  170. } elseif ($mention['type'] === 'group') {
  171. $id = array_shift($expectedGroups);
  172. } else {
  173. $this->fail('Unexpected mention type');
  174. continue;
  175. }
  176. $this->assertSame($id, $mention['id']);
  177. }
  178. $this->assertEmpty($mentions);
  179. $this->assertEmpty($expectedUids);
  180. }
  181. }