CommentTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. public function testSettersValidInput() {
  8. $comment = new Comment();
  9. $id = 'comment23';
  10. $parentId = 'comment11.5';
  11. $topMostParentId = 'comment11.0';
  12. $childrenCount = 6;
  13. $message = 'I like to comment comment';
  14. $verb = 'comment';
  15. $actor = ['type' => 'users', 'id' => 'alice'];
  16. $creationDT = new \DateTime();
  17. $latestChildDT = new \DateTime('yesterday');
  18. $object = ['type' => 'files', 'id' => 'file64'];
  19. $comment
  20. ->setId($id)
  21. ->setParentId($parentId)
  22. ->setTopmostParentId($topMostParentId)
  23. ->setChildrenCount($childrenCount)
  24. ->setMessage($message)
  25. ->setVerb($verb)
  26. ->setActor($actor['type'], $actor['id'])
  27. ->setCreationDateTime($creationDT)
  28. ->setLatestChildDateTime($latestChildDT)
  29. ->setObject($object['type'], $object['id']);
  30. $this->assertSame($id, $comment->getId());
  31. $this->assertSame($parentId, $comment->getParentId());
  32. $this->assertSame($topMostParentId, $comment->getTopmostParentId());
  33. $this->assertSame($childrenCount, $comment->getChildrenCount());
  34. $this->assertSame($message, $comment->getMessage());
  35. $this->assertSame($verb, $comment->getVerb());
  36. $this->assertSame($actor['type'], $comment->getActorType());
  37. $this->assertSame($actor['id'], $comment->getActorId());
  38. $this->assertSame($creationDT, $comment->getCreationDateTime());
  39. $this->assertSame($latestChildDT, $comment->getLatestChildDateTime());
  40. $this->assertSame($object['type'], $comment->getObjectType());
  41. $this->assertSame($object['id'], $comment->getObjectId());
  42. }
  43. /**
  44. * @expectedException \OCP\Comments\IllegalIDChangeException
  45. */
  46. public function testSetIdIllegalInput() {
  47. $comment = new Comment();
  48. $comment->setId('c23');
  49. $comment->setId('c17');
  50. }
  51. public function testResetId() {
  52. $comment = new Comment();
  53. $comment->setId('c23');
  54. $comment->setId('');
  55. $this->assertSame('', $comment->getId());
  56. }
  57. public function simpleSetterProvider() {
  58. return [
  59. ['Id', true],
  60. ['TopmostParentId', true],
  61. ['ParentId', true],
  62. ['Message', true],
  63. ['Verb', true],
  64. ['Verb', ''],
  65. ['ChildrenCount', true],
  66. ];
  67. }
  68. /**
  69. * @dataProvider simpleSetterProvider
  70. * @expectedException \InvalidArgumentException
  71. */
  72. public function testSimpleSetterInvalidInput($field, $input) {
  73. $comment = new Comment();
  74. $setter = 'set' . $field;
  75. $comment->$setter($input);
  76. }
  77. public function roleSetterProvider() {
  78. return [
  79. ['Actor', true, true],
  80. ['Actor', 'users', true],
  81. ['Actor', true, 'alice'],
  82. ['Actor', ' ', ' '],
  83. ['Object', true, true],
  84. ['Object', 'files', true],
  85. ['Object', true, 'file64'],
  86. ['Object', ' ', ' '],
  87. ];
  88. }
  89. /**
  90. * @dataProvider roleSetterProvider
  91. * @expectedException \InvalidArgumentException
  92. */
  93. public function testSetRoleInvalidInput($role, $type, $id){
  94. $comment = new Comment();
  95. $setter = 'set' . $role;
  96. $comment->$setter($type, $id);
  97. }
  98. /**
  99. * @expectedException \OCP\Comments\MessageTooLongException
  100. */
  101. public function testSetUberlongMessage() {
  102. $comment = new Comment();
  103. $msg = str_pad('', IComment::MAX_MESSAGE_LENGTH + 1, 'x');
  104. $comment->setMessage($msg);
  105. }
  106. public function mentionsProvider() {
  107. return [
  108. [
  109. '@alice @bob look look, a cook!', ['alice', 'bob']
  110. ],
  111. [
  112. 'no mentions in this message', []
  113. ],
  114. [
  115. '@alice @bob look look, a duplication @alice test @bob!', ['alice', 'bob']
  116. ],
  117. [
  118. '@alice is the author, but notify @bob!', ['bob'], 'alice'
  119. ],
  120. [
  121. '@foobar and @barfoo you should know, @foo@bar.com is valid' .
  122. ' and so is @bar@foo.org@foobar.io I hope that clarifies everything.' .
  123. ' cc @23452-4333-54353-2342 @yolo!',
  124. ['foobar', 'barfoo', 'foo@bar.com', 'bar@foo.org@foobar.io', '23452-4333-54353-2342', 'yolo']
  125. ]
  126. ];
  127. }
  128. /**
  129. * @dataProvider mentionsProvider
  130. */
  131. public function testMentions($message, $expectedUids, $author = null) {
  132. $comment = new Comment();
  133. $comment->setMessage($message);
  134. if(!is_null($author)) {
  135. $comment->setActor('user', $author);
  136. }
  137. $mentions = $comment->getMentions();
  138. while($mention = array_shift($mentions)) {
  139. $uid = array_shift($expectedUids);
  140. $this->assertSame('user', $mention['type']);
  141. $this->assertSame($uid, $mention['id']);
  142. $this->assertNotSame($author, $mention['id']);
  143. }
  144. $this->assertEmpty($mentions);
  145. $this->assertEmpty($expectedUids);
  146. }
  147. }