summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2016-10-26 14:02:49 +0200
committerGitHub <noreply@github.com>2016-10-26 14:02:49 +0200
commitcde7f535bd9fd95325545a68e4c0e8926b726a2e (patch)
tree94a1525dd139de54e5e6db6534ed0aefe5435766 /tests
parentb358b4eebc5079416c9cb437ff54705686cb215c (diff)
parentb12b52b73bb225c29f4009f9b9095cacc093ea7e (diff)
downloadnextcloud-server-cde7f535bd9fd95325545a68e4c0e8926b726a2e.tar.gz
nextcloud-server-cde7f535bd9fd95325545a68e4c0e8926b726a2e.zip
Merge pull request #1738 from nextcloud/comments-provide-displaynames-with-mentions
comment mentions: show displayname not uid
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/Comments/CommentTest.php57
-rw-r--r--tests/lib/Comments/FakeManager.php4
-rw-r--r--tests/lib/Comments/ManagerTest.php78
3 files changed, 133 insertions, 6 deletions
diff --git a/tests/lib/Comments/CommentTest.php b/tests/lib/Comments/CommentTest.php
index ea10c52ae17..10ec4bae7d5 100644
--- a/tests/lib/Comments/CommentTest.php
+++ b/tests/lib/Comments/CommentTest.php
@@ -2,13 +2,14 @@
namespace Test\Comments;
+use OC\Comments\Comment;
use OCP\Comments\IComment;
use Test\TestCase;
class CommentTest extends TestCase {
public function testSettersValidInput() {
- $comment = new \OC\Comments\Comment();
+ $comment = new Comment();
$id = 'comment23';
$parentId = 'comment11.5';
@@ -51,14 +52,14 @@ class CommentTest extends TestCase {
* @expectedException \OCP\Comments\IllegalIDChangeException
*/
public function testSetIdIllegalInput() {
- $comment = new \OC\Comments\Comment();
+ $comment = new Comment();
$comment->setId('c23');
$comment->setId('c17');
}
public function testResetId() {
- $comment = new \OC\Comments\Comment();
+ $comment = new Comment();
$comment->setId('c23');
$comment->setId('');
@@ -82,7 +83,7 @@ class CommentTest extends TestCase {
* @expectedException \InvalidArgumentException
*/
public function testSimpleSetterInvalidInput($field, $input) {
- $comment = new \OC\Comments\Comment();
+ $comment = new Comment();
$setter = 'set' . $field;
$comment->$setter($input);
@@ -106,7 +107,7 @@ class CommentTest extends TestCase {
* @expectedException \InvalidArgumentException
*/
public function testSetRoleInvalidInput($role, $type, $id){
- $comment = new \OC\Comments\Comment();
+ $comment = new Comment();
$setter = 'set' . $role;
$comment->$setter($type, $id);
}
@@ -115,11 +116,55 @@ class CommentTest extends TestCase {
* @expectedException \OCP\Comments\MessageTooLongException
*/
public function testSetUberlongMessage() {
- $comment = new \OC\Comments\Comment();
+ $comment = new Comment();
$msg = str_pad('', IComment::MAX_MESSAGE_LENGTH + 1, 'x');
$comment->setMessage($msg);
}
+ 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']
+ ],
+ [
+ '@alice is the author, but notify @bob!', ['bob'], 'alice'
+ ],
+ [
+ '@foobar and @barfoo you should know, @foo@bar.com is valid' .
+ ' and so is @bar@foo.org@foobar.io I hope that clarifies everything.' .
+ ' cc @23452-4333-54353-2342 @yolo!',
+ ['foobar', 'barfoo', 'foo@bar.com', 'bar@foo.org@foobar.io', '23452-4333-54353-2342', 'yolo']
+ ]
+
+ ];
+ }
+
+ /**
+ * @dataProvider mentionsProvider
+ */
+ public function testMentions($message, $expectedUids, $author = null) {
+ $comment = new Comment();
+ $comment->setMessage($message);
+ if(!is_null($author)) {
+ $comment->setActor('user', $author);
+ }
+ $mentions = $comment->getMentions();
+ while($mention = array_shift($mentions)) {
+ $uid = array_shift($expectedUids);
+ $this->assertSame('user', $mention['type']);
+ $this->assertSame($uid, $mention['id']);
+ $this->assertNotSame($author, $mention['id']);
+ }
+ $this->assertEmpty($mentions);
+ $this->assertEmpty($expectedUids);
+ }
+
}
diff --git a/tests/lib/Comments/FakeManager.php b/tests/lib/Comments/FakeManager.php
index 7cd146e7cb2..dfb8f21b64b 100644
--- a/tests/lib/Comments/FakeManager.php
+++ b/tests/lib/Comments/FakeManager.php
@@ -40,4 +40,8 @@ class FakeManager implements \OCP\Comments\ICommentsManager {
public function deleteReadMarksOnObject($objectType, $objectId) {}
public function registerEventHandler(\Closure $closure) {}
+
+ public function registerDisplayNameResolver($type, \Closure $closure) {}
+
+ public function resolveDisplayName($type, $id) {}
}
diff --git a/tests/lib/Comments/ManagerTest.php b/tests/lib/Comments/ManagerTest.php
index 5bacc794ba7..a320366f29e 100644
--- a/tests/lib/Comments/ManagerTest.php
+++ b/tests/lib/Comments/ManagerTest.php
@@ -664,4 +664,82 @@ class ManagerTest extends TestCase {
$manager->delete($comment->getId());
}
+ public function testResolveDisplayName() {
+ $manager = $this->getManager();
+
+ $planetClosure = function($name) {
+ return ucfirst($name);
+ };
+
+ $galaxyClosure = function($name) {
+ return strtoupper($name);
+ };
+
+ $manager->registerDisplayNameResolver('planet', $planetClosure);
+ $manager->registerDisplayNameResolver('galaxy', $galaxyClosure);
+
+ $this->assertSame('Neptune', $manager->resolveDisplayName('planet', 'neptune'));
+ $this->assertSame('SOMBRERO', $manager->resolveDisplayName('galaxy', 'sombrero'));
+ }
+
+ /**
+ * @expectedException \OutOfBoundsException
+ */
+ public function testRegisterResolverDuplicate() {
+ $manager = $this->getManager();
+
+ $planetClosure = function($name) {
+ return ucfirst($name);
+ };
+ $manager->registerDisplayNameResolver('planet', $planetClosure);
+ $manager->registerDisplayNameResolver('planet', $planetClosure);
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testRegisterResolverInvalidType() {
+ $manager = $this->getManager();
+
+ $planetClosure = function($name) {
+ return ucfirst($name);
+ };
+ $manager->registerDisplayNameResolver(1337, $planetClosure);
+ }
+
+ /**
+ * @expectedException \OutOfBoundsException
+ */
+ public function testResolveDisplayNameUnregisteredType() {
+ $manager = $this->getManager();
+
+ $planetClosure = function($name) {
+ return ucfirst($name);
+ };
+
+ $manager->registerDisplayNameResolver('planet', $planetClosure);
+ $manager->resolveDisplayName('galaxy', 'sombrero');
+ }
+
+ public function testResolveDisplayNameDirtyResolver() {
+ $manager = $this->getManager();
+
+ $planetClosure = function() { return null; };
+
+ $manager->registerDisplayNameResolver('planet', $planetClosure);
+ $this->assertTrue(is_string($manager->resolveDisplayName('planet', 'neptune')));
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testResolveDisplayNameInvalidType() {
+ $manager = $this->getManager();
+
+ $planetClosure = function() { return null; };
+
+ $manager->registerDisplayNameResolver('planet', $planetClosure);
+ $this->assertTrue(is_string($manager->resolveDisplayName(1337, 'neptune')));
+ }
+
}