CommentTest.php 6.3 KB

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