1
0

CommentTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. /**
  47. * @expectedException \OCP\Comments\IllegalIDChangeException
  48. */
  49. public function testSetIdIllegalInput() {
  50. $comment = new Comment();
  51. $comment->setId('c23');
  52. $comment->setId('c17');
  53. }
  54. /**
  55. * @throws \OCP\Comments\IllegalIDChangeException
  56. */
  57. public function testResetId() {
  58. $comment = new Comment();
  59. $comment->setId('c23');
  60. $comment->setId('');
  61. $this->assertSame('', $comment->getId());
  62. }
  63. public function simpleSetterProvider() {
  64. return [
  65. ['Id', true],
  66. ['TopmostParentId', true],
  67. ['ParentId', true],
  68. ['Message', true],
  69. ['Verb', true],
  70. ['Verb', ''],
  71. ['ChildrenCount', true],
  72. ];
  73. }
  74. /**
  75. * @dataProvider simpleSetterProvider
  76. * @expectedException \InvalidArgumentException
  77. */
  78. public function testSimpleSetterInvalidInput($field, $input) {
  79. $comment = new Comment();
  80. $setter = 'set' . $field;
  81. $comment->$setter($input);
  82. }
  83. public function roleSetterProvider() {
  84. return [
  85. ['Actor', true, true],
  86. ['Actor', 'users', true],
  87. ['Actor', true, 'alice'],
  88. ['Actor', ' ', ' '],
  89. ['Object', true, true],
  90. ['Object', 'files', true],
  91. ['Object', true, 'file64'],
  92. ['Object', ' ', ' '],
  93. ];
  94. }
  95. /**
  96. * @dataProvider roleSetterProvider
  97. * @expectedException \InvalidArgumentException
  98. */
  99. public function testSetRoleInvalidInput($role, $type, $id){
  100. $comment = new Comment();
  101. $setter = 'set' . $role;
  102. $comment->$setter($type, $id);
  103. }
  104. /**
  105. * @expectedException \OCP\Comments\MessageTooLongException
  106. */
  107. public function testSetUberlongMessage() {
  108. $comment = new Comment();
  109. $msg = str_pad('', IComment::MAX_MESSAGE_LENGTH + 1, 'x');
  110. $comment->setMessage($msg);
  111. }
  112. public function mentionsProvider() {
  113. return [
  114. [
  115. '@alice @bob look look, a cook!', ['alice', 'bob']
  116. ],
  117. [
  118. 'no mentions in this message', []
  119. ],
  120. [
  121. '@alice @bob look look, a duplication @alice test @bob!', ['alice', 'bob']
  122. ],
  123. [
  124. '@alice is the author, notify @bob, nevertheless mention her!', ['alice', 'bob'], 'alice'
  125. ],
  126. [
  127. '@foobar and @barfoo you should know, @foo@bar.com is valid' .
  128. ' and so is @bar@foo.org@foobar.io I hope that clarifies everything.' .
  129. ' cc @23452-4333-54353-2342 @yolo!' .
  130. ' however the most important thing to know is that www.croissant.com/@oil is not valid' .
  131. ' and won\'t match anything at all',
  132. ['foobar', 'barfoo', 'foo@bar.com', 'bar@foo.org@foobar.io', '23452-4333-54353-2342', 'yolo']
  133. ],
  134. [
  135. '@@chef is also a valid mention, no matter how strange it looks', ['@chef']
  136. ],
  137. [
  138. 'Also @"user with spaces" are now supported', ['user with spaces']
  139. ],
  140. ];
  141. }
  142. /**
  143. * @dataProvider mentionsProvider
  144. */
  145. public function testMentions($message, $expectedUids, $author = null) {
  146. $comment = new Comment();
  147. $comment->setMessage($message);
  148. if(!is_null($author)) {
  149. $comment->setActor('user', $author);
  150. }
  151. $mentions = $comment->getMentions();
  152. while($mention = array_shift($mentions)) {
  153. $uid = array_shift($expectedUids);
  154. $this->assertSame('user', $mention['type']);
  155. $this->assertSame($uid, $mention['id']);
  156. }
  157. $this->assertEmpty($mentions);
  158. $this->assertEmpty($expectedUids);
  159. }
  160. }