CommentTest.php 5.2 KB

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