2015-11-23 23:53:55 +01:00
< ? php
2024-05-10 15:09:14 +02:00
/**
* SPDX - FileCopyrightText : 2016 - 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX - FileCopyrightText : 2016 ownCloud , Inc .
* SPDX - License - Identifier : AGPL - 3.0 - only
*/
2015-12-03 17:23:22 +01:00
namespace Test\Comments ;
2016-10-14 00:19:31 +02:00
use OC\Comments\Comment ;
2016-02-09 03:14:30 +01:00
use OCP\Comments\IComment ;
2015-12-03 17:23:22 +01:00
use Test\TestCase ;
2016-05-18 18:55:44 +02:00
class CommentTest extends TestCase {
2018-01-17 13:48:43 +01:00
/**
* @ throws \OCP\Comments\IllegalIDChangeException
*/
2015-11-23 23:53:55 +01:00
public function testSettersValidInput () : void {
2016-10-14 00:19:31 +02:00
$comment = new Comment ();
2015-11-23 23:53:55 +01:00
$id = 'comment23' ;
$parentId = 'comment11.5' ;
2016-09-29 22:41:37 +02:00
$topMostParentId = 'comment11.0' ;
2015-11-23 23:53:55 +01:00
$childrenCount = 6 ;
$message = 'I like to comment comment' ;
$verb = 'comment' ;
2016-02-03 19:28:15 +01:00
$actor = [ 'type' => 'users' , 'id' => 'alice' ];
2015-11-23 23:53:55 +01:00
$creationDT = new \DateTime ();
$latestChildDT = new \DateTime ( 'yesterday' );
2016-02-03 19:28:15 +01:00
$object = [ 'type' => 'files' , 'id' => 'file64' ];
2023-12-13 13:29:12 +01:00
$referenceId = sha1 ( 'referenceId' );
$metaData = [ 'last_edit_actor_id' => 'admin' ];
2015-11-23 23:53:55 +01:00
$comment
-> setId ( $id )
-> setParentId ( $parentId )
2016-09-29 22:41:37 +02:00
-> setTopmostParentId ( $topMostParentId )
2015-11-23 23:53:55 +01:00
-> setChildrenCount ( $childrenCount )
-> setMessage ( $message )
-> setVerb ( $verb )
-> setActor ( $actor [ 'type' ], $actor [ 'id' ])
-> setCreationDateTime ( $creationDT )
-> setLatestChildDateTime ( $latestChildDT )
2023-12-13 13:29:12 +01:00
-> setObject ( $object [ 'type' ], $object [ 'id' ])
-> setReferenceId ( $referenceId )
-> setMetaData ( $metaData );
2015-11-23 23:53:55 +01:00
$this -> assertSame ( $id , $comment -> getId ());
$this -> assertSame ( $parentId , $comment -> getParentId ());
2016-09-29 22:41:37 +02:00
$this -> assertSame ( $topMostParentId , $comment -> getTopmostParentId ());
2015-11-23 23:53:55 +01: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 ());
2023-12-13 13:29:12 +01:00
$this -> assertSame ( $referenceId , $comment -> getReferenceId ());
$this -> assertSame ( $metaData , $comment -> getMetaData ());
2015-11-23 23:53:55 +01:00
}
2021-01-29 13:09:37 +01:00
2015-11-23 23:53:55 +01:00
public function testSetIdIllegalInput () : void {
2019-11-27 15:27:18 +01:00
$this -> expectException ( \OCP\Comments\IllegalIDChangeException :: class );
2016-10-14 00:19:31 +02:00
$comment = new Comment ();
2015-11-23 23:53:55 +01:00
$comment -> setId ( 'c23' );
$comment -> setId ( 'c17' );
}
2018-01-17 13:48:43 +01:00
/**
* @ throws \OCP\Comments\IllegalIDChangeException
*/
2015-11-23 23:53:55 +01:00
public function testResetId () : void {
2016-10-14 00:19:31 +02:00
$comment = new Comment ();
2015-11-23 23:53:55 +01:00
$comment -> setId ( 'c23' );
$comment -> setId ( '' );
2015-12-10 09:29:24 +01:00
$this -> assertSame ( '' , $comment -> getId ());
2015-11-23 23:53:55 +01:00
}
public function simpleSetterProvider () {
return [
2015-12-04 11:13:39 +01:00
[ 'Id' , true ],
2016-09-29 22:41:37 +02:00
[ 'TopmostParentId' , true ],
2015-12-04 11:13:39 +01:00
[ 'ParentId' , true ],
[ 'Message' , true ],
[ 'Verb' , true ],
[ 'Verb' , '' ],
[ 'ChildrenCount' , true ],
2015-11-23 23:53:55 +01:00
];
}
/**
* @ dataProvider simpleSetterProvider
*/
2015-12-04 11:13:39 +01:00
public function testSimpleSetterInvalidInput ( $field , $input ) : void {
2019-11-27 15:27:18 +01:00
$this -> expectException ( \InvalidArgumentException :: class );
2016-10-14 00:19:31 +02:00
$comment = new Comment ();
2015-11-23 23:53:55 +01:00
$setter = 'set' . $field ;
2015-12-04 11:13:39 +01:00
$comment -> $setter ( $input );
2015-11-23 23:53:55 +01:00
}
public function roleSetterProvider () {
return [
[ 'Actor' , true , true ],
2016-02-03 19:28:15 +01:00
[ 'Actor' , 'users' , true ],
2015-11-23 23:53:55 +01:00
[ 'Actor' , true , 'alice' ],
2015-12-04 11:13:39 +01:00
[ 'Actor' , ' ' , ' ' ],
2015-11-23 23:53:55 +01:00
[ 'Object' , true , true ],
2016-02-03 19:28:15 +01:00
[ 'Object' , 'files' , true ],
2015-11-23 23:53:55 +01:00
[ 'Object' , true , 'file64' ],
2015-12-04 11:13:39 +01:00
[ 'Object' , ' ' , ' ' ],
2015-11-23 23:53:55 +01:00
];
}
/**
* @ dataProvider roleSetterProvider
*/
2020-04-09 13:53:40 +02:00
public function testSetRoleInvalidInput ( $role , $type , $id ) : void {
2019-11-27 15:27:18 +01:00
$this -> expectException ( \InvalidArgumentException :: class );
2016-10-14 00:19:31 +02:00
$comment = new Comment ();
2015-11-23 23:53:55 +01:00
$setter = 'set' . $role ;
$comment -> $setter ( $type , $id );
}
2021-01-29 13:09:37 +01:00
2016-02-09 03:14:30 +01:00
public function testSetUberlongMessage () : void {
2019-11-27 15:27:18 +01:00
$this -> expectException ( \OCP\Comments\MessageTooLongException :: class );
2016-10-14 00:19:31 +02:00
$comment = new Comment ();
2016-02-09 03:14:30 +01:00
$msg = str_pad ( '' , IComment :: MAX_MESSAGE_LENGTH + 1 , 'x' );
$comment -> setMessage ( $msg );
}
2024-02-27 17:11:23 +01:00
public function mentionsProvider () : array {
2016-10-14 00:19:31 +02:00
return [
[
2024-02-27 17:11:23 +01:00
'@alice @bob look look, a cook!' ,
[[ 'type' => 'user' , 'id' => 'alice' ], [ 'type' => 'user' , 'id' => 'bob' ]],
2016-10-14 00:19:31 +02:00
],
[
2024-02-27 17:11:23 +01:00
'no mentions in this message' ,
[]
2016-10-14 00:19:31 +02:00
],
[
2024-02-27 17:11:23 +01:00
'@alice @bob look look, a duplication @alice test @bob!' ,
[[ 'type' => 'user' , 'id' => 'alice' ], [ 'type' => 'user' , 'id' => 'bob' ]],
2016-10-14 00:19:31 +02:00
],
[
2024-02-27 17:11:23 +01:00
'@alice is the author, notify @bob, nevertheless mention her!' ,
[[ 'type' => 'user' , 'id' => 'alice' ], [ 'type' => 'user' , 'id' => 'bob' ]],
/* author: */ 'alice'
2016-10-14 00:19:31 +02: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 17:15:41 +02: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' ,
2024-02-27 17:11:23 +01:00
[
[ 'type' => 'user' , 'id' => 'bar@foo.org@foobar.io' ],
[ 'type' => 'user' , 'id' => '23452-4333-54353-2342' ],
[ 'type' => 'user' , 'id' => 'foo@bar.com' ],
[ 'type' => 'user' , 'id' => 'foobar' ],
[ 'type' => 'user' , 'id' => 'barfoo' ],
[ 'type' => 'user' , 'id' => 'yolo' ],
],
2018-06-08 17:21:46 +02:00
],
[
2024-02-27 17:11:23 +01:00
'@@chef is also a valid mention, no matter how strange it looks' ,
[[ 'type' => 'user' , 'id' => '@chef' ]],
2018-10-12 14:25:46 +02:00
],
[
2024-02-27 17:11:23 +01:00
'Also @"user with spaces" are now supported' ,
[[ 'type' => 'user' , 'id' => 'user with spaces' ]],
2018-10-12 14:25:46 +02:00
],
2019-07-11 10:24:27 +02:00
[
2024-02-27 17:11:23 +01:00
'Also @"guest/0123456789abcdef" are now supported' ,
[[ 'type' => 'guest' , 'id' => 'guest/0123456789abcdef' ]],
2019-07-11 10:24:27 +02:00
],
2021-10-12 14:05:38 +02:00
[
2024-02-27 17:11:23 +01:00
'Also @"group/My Group ID 321" are now supported' ,
[[ 'type' => 'group' , 'id' => 'My Group ID 321' ]],
],
[
'Welcome federation @"federated_group/My Group ID 321" @"federated_team/Former Cirle" @"federated_user/cloudId@http://example.tld:8080/nextcloud"! Now freshly supported' ,
[
[ 'type' => 'federated_user' , 'id' => 'cloudId@http://example.tld:8080/nextcloud' ],
[ 'type' => 'federated_group' , 'id' => 'My Group ID 321' ],
[ 'type' => 'federated_team' , 'id' => 'Former Cirle' ],
],
2021-10-12 14:05:38 +02:00
],
2024-10-18 07:39:41 +02:00
[
'Emails are supported since 30.0.2 right? @"email/aa23d315de327cfc330f0401ea061005b2b0cdd45ec8346f12664dd1f34cb886"' ,
[
[ 'type' => 'email' , 'id' => 'aa23d315de327cfc330f0401ea061005b2b0cdd45ec8346f12664dd1f34cb886' ],
],
],
2016-10-14 00:19:31 +02:00
];
}
/**
* @ dataProvider mentionsProvider
2019-07-11 10:24:27 +02:00
*
* @ param string $message
2024-02-27 17:11:23 +01:00
* @ param array $expectedMentions
* @ param ? string $author
2016-10-14 00:19:31 +02:00
*/
2024-02-27 17:11:23 +01:00
public function testMentions ( string $message , array $expectedMentions , ? string $author = null ) : void {
2016-10-14 00:19:31 +02:00
$comment = new Comment ();
$comment -> setMessage ( $message );
if ( ! is_null ( $author )) {
$comment -> setActor ( 'user' , $author );
}
$mentions = $comment -> getMentions ();
2024-02-27 17:11:23 +01:00
$this -> assertSame ( $expectedMentions , $mentions );
2016-10-14 00:19:31 +02:00
}
2015-11-23 23:53:55 +01:00
}