2015-11-24 01:53:55 +03:00
|
|
|
<?php
|
|
|
|
|
2015-12-03 19:23:22 +03:00
|
|
|
namespace Test\Comments;
|
|
|
|
|
2016-10-14 01:19:31 +03:00
|
|
|
use OC\Comments\Comment;
|
2016-02-09 05:14:30 +03:00
|
|
|
use OCP\Comments\IComment;
|
2015-12-03 19:23:22 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
2016-05-18 19:55:44 +03:00
|
|
|
class CommentTest extends TestCase {
|
2015-11-24 01:53:55 +03:00
|
|
|
|
2018-01-17 15:48:43 +03:00
|
|
|
/**
|
|
|
|
* @throws \OCP\Comments\IllegalIDChangeException
|
|
|
|
*/
|
2015-11-24 01:53:55 +03:00
|
|
|
public function testSettersValidInput() {
|
2016-10-14 01:19:31 +03:00
|
|
|
$comment = new Comment();
|
2015-11-24 01:53:55 +03:00
|
|
|
|
|
|
|
$id = 'comment23';
|
|
|
|
$parentId = 'comment11.5';
|
2016-09-29 23:41:37 +03:00
|
|
|
$topMostParentId = 'comment11.0';
|
2015-11-24 01:53:55 +03:00
|
|
|
$childrenCount = 6;
|
|
|
|
$message = 'I like to comment comment';
|
|
|
|
$verb = 'comment';
|
2016-02-03 21:28:15 +03:00
|
|
|
$actor = ['type' => 'users', 'id' => 'alice'];
|
2015-11-24 01:53:55 +03:00
|
|
|
$creationDT = new \DateTime();
|
|
|
|
$latestChildDT = new \DateTime('yesterday');
|
2016-02-03 21:28:15 +03:00
|
|
|
$object = ['type' => 'files', 'id' => 'file64'];
|
2015-11-24 01:53:55 +03:00
|
|
|
|
|
|
|
$comment
|
|
|
|
->setId($id)
|
|
|
|
->setParentId($parentId)
|
2016-09-29 23:41:37 +03:00
|
|
|
->setTopmostParentId($topMostParentId)
|
2015-11-24 01:53:55 +03:00
|
|
|
->setChildrenCount($childrenCount)
|
|
|
|
->setMessage($message)
|
|
|
|
->setVerb($verb)
|
|
|
|
->setActor($actor['type'], $actor['id'])
|
|
|
|
->setCreationDateTime($creationDT)
|
|
|
|
->setLatestChildDateTime($latestChildDT)
|
|
|
|
->setObject($object['type'], $object['id']);
|
|
|
|
|
|
|
|
$this->assertSame($id, $comment->getId());
|
|
|
|
$this->assertSame($parentId, $comment->getParentId());
|
2016-09-29 23:41:37 +03:00
|
|
|
$this->assertSame($topMostParentId, $comment->getTopmostParentId());
|
2015-11-24 01:53:55 +03:00
|
|
|
$this->assertSame($childrenCount, $comment->getChildrenCount());
|
|
|
|
$this->assertSame($message, $comment->getMessage());
|
|
|
|
$this->assertSame($verb, $comment->getVerb());
|
|
|
|
$this->assertSame($actor['type'], $comment->getActorType());
|
|
|
|
$this->assertSame($actor['id'], $comment->getActorId());
|
|
|
|
$this->assertSame($creationDT, $comment->getCreationDateTime());
|
|
|
|
$this->assertSame($latestChildDT, $comment->getLatestChildDateTime());
|
|
|
|
$this->assertSame($object['type'], $comment->getObjectType());
|
|
|
|
$this->assertSame($object['id'], $comment->getObjectId());
|
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2015-11-24 01:53:55 +03:00
|
|
|
public function testSetIdIllegalInput() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\OCP\Comments\IllegalIDChangeException::class);
|
|
|
|
|
2016-10-14 01:19:31 +03:00
|
|
|
$comment = new Comment();
|
2015-11-24 01:53:55 +03:00
|
|
|
|
|
|
|
$comment->setId('c23');
|
|
|
|
$comment->setId('c17');
|
|
|
|
}
|
|
|
|
|
2018-01-17 15:48:43 +03:00
|
|
|
/**
|
|
|
|
* @throws \OCP\Comments\IllegalIDChangeException
|
|
|
|
*/
|
2015-11-24 01:53:55 +03:00
|
|
|
public function testResetId() {
|
2016-10-14 01:19:31 +03:00
|
|
|
$comment = new Comment();
|
2015-11-24 01:53:55 +03:00
|
|
|
$comment->setId('c23');
|
|
|
|
$comment->setId('');
|
2015-12-10 11:29:24 +03:00
|
|
|
|
|
|
|
$this->assertSame('', $comment->getId());
|
2015-11-24 01:53:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function simpleSetterProvider() {
|
|
|
|
return [
|
2015-12-04 13:13:39 +03:00
|
|
|
['Id', true],
|
2016-09-29 23:41:37 +03:00
|
|
|
['TopmostParentId', true],
|
2015-12-04 13:13:39 +03:00
|
|
|
['ParentId', true],
|
|
|
|
['Message', true],
|
|
|
|
['Verb', true],
|
|
|
|
['Verb', ''],
|
|
|
|
['ChildrenCount', true],
|
2015-11-24 01:53:55 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider simpleSetterProvider
|
|
|
|
*/
|
2015-12-04 13:13:39 +03:00
|
|
|
public function testSimpleSetterInvalidInput($field, $input) {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
2016-10-14 01:19:31 +03:00
|
|
|
$comment = new Comment();
|
2015-11-24 01:53:55 +03:00
|
|
|
$setter = 'set' . $field;
|
|
|
|
|
2015-12-04 13:13:39 +03:00
|
|
|
$comment->$setter($input);
|
2015-11-24 01:53:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function roleSetterProvider() {
|
|
|
|
return [
|
|
|
|
['Actor', true, true],
|
2016-02-03 21:28:15 +03:00
|
|
|
['Actor', 'users', true],
|
2015-11-24 01:53:55 +03:00
|
|
|
['Actor', true, 'alice'],
|
2015-12-04 13:13:39 +03:00
|
|
|
['Actor', ' ', ' '],
|
2015-11-24 01:53:55 +03:00
|
|
|
['Object', true, true],
|
2016-02-03 21:28:15 +03:00
|
|
|
['Object', 'files', true],
|
2015-11-24 01:53:55 +03:00
|
|
|
['Object', true, 'file64'],
|
2015-12-04 13:13:39 +03:00
|
|
|
['Object', ' ', ' '],
|
2015-11-24 01:53:55 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider roleSetterProvider
|
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testSetRoleInvalidInput($role, $type, $id) {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
2016-10-14 01:19:31 +03:00
|
|
|
$comment = new Comment();
|
2015-11-24 01:53:55 +03:00
|
|
|
$setter = 'set' . $role;
|
|
|
|
$comment->$setter($type, $id);
|
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2016-02-09 05:14:30 +03:00
|
|
|
public function testSetUberlongMessage() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\OCP\Comments\MessageTooLongException::class);
|
|
|
|
|
2016-10-14 01:19:31 +03:00
|
|
|
$comment = new Comment();
|
2016-02-09 05:14:30 +03:00
|
|
|
$msg = str_pad('', IComment::MAX_MESSAGE_LENGTH + 1, 'x');
|
|
|
|
$comment->setMessage($msg);
|
|
|
|
}
|
|
|
|
|
2016-10-14 01:19:31 +03:00
|
|
|
public function mentionsProvider() {
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'@alice @bob look look, a cook!', ['alice', 'bob']
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'no mentions in this message', []
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'@alice @bob look look, a duplication @alice test @bob!', ['alice', 'bob']
|
|
|
|
],
|
|
|
|
[
|
2018-01-17 15:48:43 +03:00
|
|
|
'@alice is the author, notify @bob, nevertheless mention her!', ['alice', 'bob'], 'alice'
|
2016-10-14 01:19:31 +03:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'@foobar and @barfoo you should know, @foo@bar.com is valid' .
|
|
|
|
' and so is @bar@foo.org@foobar.io I hope that clarifies everything.' .
|
2018-06-08 18:15:41 +03:00
|
|
|
' cc @23452-4333-54353-2342 @yolo!' .
|
|
|
|
' however the most important thing to know is that www.croissant.com/@oil is not valid' .
|
|
|
|
' and won\'t match anything at all',
|
2016-10-14 01:19:31 +03:00
|
|
|
['foobar', 'barfoo', 'foo@bar.com', 'bar@foo.org@foobar.io', '23452-4333-54353-2342', 'yolo']
|
2018-06-08 18:21:46 +03:00
|
|
|
],
|
|
|
|
[
|
2018-10-12 15:25:46 +03:00
|
|
|
'@@chef is also a valid mention, no matter how strange it looks', ['@chef']
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'Also @"user with spaces" are now supported', ['user with spaces']
|
|
|
|
],
|
2019-07-11 11:24:27 +03:00
|
|
|
[
|
|
|
|
'Also @"guest/0123456789abcdef" are now supported', [], null, ['guest/0123456789abcdef']
|
|
|
|
],
|
2016-10-14 01:19:31 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider mentionsProvider
|
2019-07-11 11:24:27 +03:00
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
* @param array $expectedUids
|
|
|
|
* @param string|null $author
|
|
|
|
* @param array $expectedGuests
|
2016-10-14 01:19:31 +03:00
|
|
|
*/
|
2019-07-11 11:24:27 +03:00
|
|
|
public function testMentions(string $message, array $expectedUids, ?string $author = null, array $expectedGuests = []): void {
|
2016-10-14 01:19:31 +03:00
|
|
|
$comment = new Comment();
|
|
|
|
$comment->setMessage($message);
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!is_null($author)) {
|
2016-10-14 01:19:31 +03:00
|
|
|
$comment->setActor('user', $author);
|
|
|
|
}
|
|
|
|
$mentions = $comment->getMentions();
|
2020-04-10 15:19:56 +03:00
|
|
|
while ($mention = array_shift($mentions)) {
|
2019-07-11 11:24:27 +03:00
|
|
|
if ($mention['type'] === 'user') {
|
|
|
|
$id = array_shift($expectedUids);
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($mention['type'] === 'guest') {
|
2019-07-11 11:24:27 +03:00
|
|
|
$id = array_shift($expectedGuests);
|
|
|
|
} else {
|
|
|
|
$this->fail('Unexpected mention type');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$this->assertSame($id, $mention['id']);
|
2016-10-14 01:19:31 +03:00
|
|
|
}
|
|
|
|
$this->assertEmpty($mentions);
|
|
|
|
$this->assertEmpty($expectedUids);
|
|
|
|
}
|
2015-11-24 01:53:55 +03:00
|
|
|
}
|