CommentTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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(): array {
  115. return [
  116. [
  117. '@alice @bob look look, a cook!',
  118. [['type' => 'user', 'id' => 'alice'], ['type' => 'user', 'id' => 'bob']],
  119. ],
  120. [
  121. 'no mentions in this message',
  122. []
  123. ],
  124. [
  125. '@alice @bob look look, a duplication @alice test @bob!',
  126. [['type' => 'user', 'id' => 'alice'], ['type' => 'user', 'id' => 'bob']],
  127. ],
  128. [
  129. '@alice is the author, notify @bob, nevertheless mention her!',
  130. [['type' => 'user', 'id' => 'alice'], ['type' => 'user', 'id' => 'bob']],
  131. /* author: */ 'alice'
  132. ],
  133. [
  134. '@foobar and @barfoo you should know, @foo@bar.com is valid' .
  135. ' and so is @bar@foo.org@foobar.io I hope that clarifies everything.' .
  136. ' cc @23452-4333-54353-2342 @yolo!' .
  137. ' however the most important thing to know is that www.croissant.com/@oil is not valid' .
  138. ' and won\'t match anything at all',
  139. [
  140. ['type' => 'user', 'id' => 'bar@foo.org@foobar.io'],
  141. ['type' => 'user', 'id' => '23452-4333-54353-2342'],
  142. ['type' => 'user', 'id' => 'foo@bar.com'],
  143. ['type' => 'user', 'id' => 'foobar'],
  144. ['type' => 'user', 'id' => 'barfoo'],
  145. ['type' => 'user', 'id' => 'yolo'],
  146. ],
  147. ],
  148. [
  149. '@@chef is also a valid mention, no matter how strange it looks',
  150. [['type' => 'user', 'id' => '@chef']],
  151. ],
  152. [
  153. 'Also @"user with spaces" are now supported',
  154. [['type' => 'user', 'id' => 'user with spaces']],
  155. ],
  156. [
  157. 'Also @"guest/0123456789abcdef" are now supported',
  158. [['type' => 'guest', 'id' => 'guest/0123456789abcdef']],
  159. ],
  160. [
  161. 'Also @"group/My Group ID 321" are now supported',
  162. [['type' => 'group', 'id' => 'My Group ID 321']],
  163. ],
  164. [
  165. 'Welcome federation @"federated_group/My Group ID 321" @"federated_team/Former Cirle" @"federated_user/cloudId@http://example.tld:8080/nextcloud"! Now freshly supported',
  166. [
  167. ['type' => 'federated_user', 'id' => 'cloudId@http://example.tld:8080/nextcloud'],
  168. ['type' => 'federated_group', 'id' => 'My Group ID 321'],
  169. ['type' => 'federated_team', 'id' => 'Former Cirle'],
  170. ],
  171. ],
  172. ];
  173. }
  174. /**
  175. * @dataProvider mentionsProvider
  176. *
  177. * @param string $message
  178. * @param array $expectedMentions
  179. * @param ?string $author
  180. */
  181. public function testMentions(string $message, array $expectedMentions, ?string $author = null): void {
  182. $comment = new Comment();
  183. $comment->setMessage($message);
  184. if (!is_null($author)) {
  185. $comment->setActor('user', $author);
  186. }
  187. $mentions = $comment->getMentions();
  188. $this->assertSame($expectedMentions, $mentions);
  189. }
  190. }