From f73d0d2ac9daee5eaa03a7b89c639678fec46467 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Thu, 1 Dec 2016 22:52:58 -0500 Subject: [PATCH] Make user mentions great again --- ChangeLog | 2 + app/Core/Base.php | 2 +- app/Core/Markdown.php | 2 +- app/Event/GenericEvent.php | 22 +++++ app/Event/ProjectFileEvent.php | 8 ++ app/Job/CommentEventJob.php | 4 +- app/Job/TaskEventJob.php | 3 +- app/Job/UserMentionJob.php | 71 ++++++++++++++++ app/Model/UserMentionModel.php | 62 -------------- app/ServiceProvider/ClassProvider.php | 1 - app/ServiceProvider/JobProvider.php | 5 ++ app/Subscriber/NotificationSubscriber.php | 38 ++++----- .../EventBuilder/CommentEventBuilderTest.php | 2 + .../ProjectFileEventBuilderTest.php | 2 + .../EventBuilder/SubtaskEventBuilderTest.php | 2 + .../EventBuilder/TaskEventBuilderTest.php | 2 + .../EventBuilder/TaskFileEventBuilderTest.php | 2 + .../EventBuilder/TaskLinkEventBuilderTest.php | 2 + tests/units/Helper/TextHelperTest.php | 12 +-- tests/units/Job/CommentEventJobTest.php | 42 ++++++++++ tests/units/Job/TaskEventJobTest.php | 40 +++++++++ .../UserMentionJobTest.php} | 84 ++++++++----------- 22 files changed, 270 insertions(+), 140 deletions(-) create mode 100644 app/Job/UserMentionJob.php delete mode 100644 app/Model/UserMentionModel.php rename tests/units/{Model/UserMentionTest.php => Job/UserMentionJobTest.php} (56%) diff --git a/ChangeLog b/ChangeLog index 658e64564..fdb68e790 100644 --- a/ChangeLog +++ b/ChangeLog @@ -6,6 +6,7 @@ Improvements: * Add button to close inline popups * Simplify `.htaccess` to avoid potential issues with possible specific Apache configurations * Replace notifications Javascript code by CSS +* Refactoring of user mentions job Breaking changes: @@ -14,6 +15,7 @@ Breaking changes: Bug fixes: * Fix link generation when user mention is followed by a punctuation mark +* Make user mentions works again Version 1.0.34 -------------- diff --git a/app/Core/Base.php b/app/Core/Base.php index 3dbf47f90..e7ccafaa8 100644 --- a/app/Core/Base.php +++ b/app/Core/Base.php @@ -126,7 +126,6 @@ use Pimple\Container; * @property \Kanboard\Model\TransitionModel $transitionModel * @property \Kanboard\Model\UserModel $userModel * @property \Kanboard\Model\UserLockingModel $userLockingModel - * @property \Kanboard\Model\UserMentionModel $userMentionModel * @property \Kanboard\Model\UserNotificationModel $userNotificationModel * @property \Kanboard\Model\UserNotificationTypeModel $userNotificationTypeModel * @property \Kanboard\Model\UserNotificationFilterModel $userNotificationFilterModel @@ -178,6 +177,7 @@ use Pimple\Container; * @property \Kanboard\Job\ProjectFileEventJob $projectFileEventJob * @property \Kanboard\Job\NotificationJob $notificationJob * @property \Kanboard\Job\ProjectMetricJob $projectMetricJob + * @property \Kanboard\Job\UserMentionJob $userMentionJob * @property \Psr\Log\LoggerInterface $logger * @property \PicoDb\Database $db * @property \Symfony\Component\EventDispatcher\EventDispatcher $dispatcher diff --git a/app/Core/Markdown.php b/app/Core/Markdown.php index b83a3ced5..b4208e9a7 100644 --- a/app/Core/Markdown.php +++ b/app/Core/Markdown.php @@ -90,7 +90,7 @@ class Markdown extends Parsedown $user_id = $this->container['userModel']->getIdByUsername($matches[1]); if (! empty($user_id)) { - $url = $this->container['helper']->url->href('UserViewController', 'profile', array('user_id' => $user_id)); + $url = $this->container['helper']->url->href('UserViewController', 'profile', array('user_id' => $user_id), false, '', true); return array( 'extent' => strlen($matches[0]), diff --git a/app/Event/GenericEvent.php b/app/Event/GenericEvent.php index 94a514798..e87d94816 100644 --- a/app/Event/GenericEvent.php +++ b/app/Event/GenericEvent.php @@ -14,6 +14,28 @@ class GenericEvent extends BaseEvent implements ArrayAccess $this->container = $values; } + public function getTaskId() + { + if (isset($this->container['task']['id'])) { + return $this->container['task']['id']; + } + + if (isset($this->container['task_id'])) { + return $this->container['task_id']; + } + + return null; + } + + public function getProjectId() + { + if (isset($this->container['task']['project_id'])) { + return $this->container['task']['project_id']; + } + + return null; + } + public function getAll() { return $this->container; diff --git a/app/Event/ProjectFileEvent.php b/app/Event/ProjectFileEvent.php index 5d57e4637..e1d29c483 100644 --- a/app/Event/ProjectFileEvent.php +++ b/app/Event/ProjectFileEvent.php @@ -4,4 +4,12 @@ namespace Kanboard\Event; class ProjectFileEvent extends GenericEvent { + public function getProjectId() + { + if (isset($this->container['file']['project_id'])) { + return $this->container['file']['project_id']; + } + + return null; + } } diff --git a/app/Job/CommentEventJob.php b/app/Job/CommentEventJob.php index 47cf8020b..62fae40a9 100644 --- a/app/Job/CommentEventJob.php +++ b/app/Job/CommentEventJob.php @@ -31,7 +31,6 @@ class CommentEventJob extends BaseJob * * @param int $commentId * @param string $eventName - * @return $this */ public function execute($commentId, $eventName) { @@ -43,7 +42,8 @@ class CommentEventJob extends BaseJob $this->dispatcher->dispatch($eventName, $event); if ($eventName === CommentModel::EVENT_CREATE) { - $this->userMentionModel->fireEvents($event['comment']['comment'], CommentModel::EVENT_USER_MENTION, $event); + $userMentionJob = $this->userMentionJob->withParams($event['comment']['comment'], CommentModel::EVENT_USER_MENTION, $event); + $this->queueManager->push($userMentionJob); } } } diff --git a/app/Job/TaskEventJob.php b/app/Job/TaskEventJob.php index 7d026a682..acc7fca34 100644 --- a/app/Job/TaskEventJob.php +++ b/app/Job/TaskEventJob.php @@ -69,7 +69,8 @@ class TaskEventJob extends BaseJob $this->dispatcher->dispatch($eventName, $event); if ($eventName === TaskModel::EVENT_CREATE) { - $this->userMentionModel->fireEvents($event['task']['description'], TaskModel::EVENT_USER_MENTION, $event); + $userMentionJob = $this->userMentionJob->withParams($event['task']['description'], TaskModel::EVENT_USER_MENTION, $event); + $this->queueManager->push($userMentionJob); } } } diff --git a/app/Job/UserMentionJob.php b/app/Job/UserMentionJob.php new file mode 100644 index 000000000..ebb69094d --- /dev/null +++ b/app/Job/UserMentionJob.php @@ -0,0 +1,71 @@ +jobParams = array($text, $eventName, $event); + return $this; + } + + /** + * Execute job + * + * @param string $text + * @param string $eventName + * @param GenericEvent $event + */ + public function execute($text, $eventName, GenericEvent $event) + { + $users = $this->getMentionedUsers($text); + + foreach ($users as $user) { + if ($this->projectPermissionModel->isMember($event->getProjectId(), $user['id'])) { + $event['mention'] = $user; + $this->dispatcher->dispatch($eventName, $event); + } + } + } + + /** + * Get list of mentioned users + * + * @access public + * @param string $text + * @return array + */ + public function getMentionedUsers($text) + { + $users = array(); + + if (preg_match_all('/@([^\s,!.:?]+)/', $text, $matches)) { + $users = $this->db->table(UserModel::TABLE) + ->columns('id', 'username', 'name', 'email', 'language') + ->eq('notifications_enabled', 1) + ->neq('id', $this->userSession->getId()) + ->in('username', array_unique($matches[1])) + ->findAll(); + } + + return $users; + } +} diff --git a/app/Model/UserMentionModel.php b/app/Model/UserMentionModel.php deleted file mode 100644 index cdb9949ea..000000000 --- a/app/Model/UserMentionModel.php +++ /dev/null @@ -1,62 +0,0 @@ -db->table(UserModel::TABLE) - ->columns('id', 'username', 'name', 'email', 'language') - ->eq('notifications_enabled', 1) - ->neq('id', $this->userSession->getId()) - ->in('username', array_unique($matches[1])) - ->findAll(); - } - - return $users; - } - - /** - * Fire events for user mentions - * - * @access public - * @param string $content - * @param string $eventName - * @param GenericEvent $event - */ - public function fireEvents($content, $eventName, GenericEvent $event) - { - if (empty($event['project_id'])) { - $event['project_id'] = $this->taskFinderModel->getProjectId($event['task_id']); - } - - $users = $this->getMentionedUsers($content); - - foreach ($users as $user) { - if ($this->projectPermissionModel->isMember($event['project_id'], $user['id'])) { - $event['mention'] = $user; - $this->dispatcher->dispatch($eventName, $event); - } - } - } -} diff --git a/app/ServiceProvider/ClassProvider.php b/app/ServiceProvider/ClassProvider.php index c5bf0678b..8d471b79b 100644 --- a/app/ServiceProvider/ClassProvider.php +++ b/app/ServiceProvider/ClassProvider.php @@ -91,7 +91,6 @@ class ClassProvider implements ServiceProviderInterface 'TransitionModel', 'UserModel', 'UserLockingModel', - 'UserMentionModel', 'UserNotificationModel', 'UserNotificationFilterModel', 'UserUnreadNotificationModel', diff --git a/app/ServiceProvider/JobProvider.php b/app/ServiceProvider/JobProvider.php index 2194b11c9..4e5e0f1a7 100644 --- a/app/ServiceProvider/JobProvider.php +++ b/app/ServiceProvider/JobProvider.php @@ -10,6 +10,7 @@ use Kanboard\Job\SubtaskEventJob; use Kanboard\Job\TaskEventJob; use Kanboard\Job\TaskFileEventJob; use Kanboard\Job\TaskLinkEventJob; +use Kanboard\Job\UserMentionJob; use Pimple\Container; use Pimple\ServiceProviderInterface; @@ -62,6 +63,10 @@ class JobProvider implements ServiceProviderInterface return new ProjectMetricJob($c); }); + $container['userMentionJob'] = $container->factory(function ($c) { + return new UserMentionJob($c); + }); + return $container; } } diff --git a/app/Subscriber/NotificationSubscriber.php b/app/Subscriber/NotificationSubscriber.php index 7cc68b26f..ad16685b2 100644 --- a/app/Subscriber/NotificationSubscriber.php +++ b/app/Subscriber/NotificationSubscriber.php @@ -15,25 +15,25 @@ class NotificationSubscriber extends BaseSubscriber implements EventSubscriberIn public static function getSubscribedEvents() { return array( - TaskModel::EVENT_USER_MENTION => 'handleEvent', - TaskModel::EVENT_CREATE => 'handleEvent', - TaskModel::EVENT_UPDATE => 'handleEvent', - TaskModel::EVENT_CLOSE => 'handleEvent', - TaskModel::EVENT_OPEN => 'handleEvent', - TaskModel::EVENT_MOVE_COLUMN => 'handleEvent', - TaskModel::EVENT_MOVE_POSITION => 'handleEvent', - TaskModel::EVENT_MOVE_SWIMLANE => 'handleEvent', - TaskModel::EVENT_ASSIGNEE_CHANGE => 'handleEvent', - SubtaskModel::EVENT_CREATE => 'handleEvent', - SubtaskModel::EVENT_UPDATE => 'handleEvent', - SubtaskModel::EVENT_DELETE => 'handleEvent', - CommentModel::EVENT_CREATE => 'handleEvent', - CommentModel::EVENT_UPDATE => 'handleEvent', - CommentModel::EVENT_DELETE => 'handleEvent', - CommentModel::EVENT_USER_MENTION => 'handleEvent', - TaskFileModel::EVENT_CREATE => 'handleEvent', - TaskLinkModel::EVENT_CREATE_UPDATE => 'handleEvent', - TaskLinkModel::EVENT_DELETE => 'handleEvent', + TaskModel::EVENT_USER_MENTION => 'handleEvent', + TaskModel::EVENT_CREATE => 'handleEvent', + TaskModel::EVENT_UPDATE => 'handleEvent', + TaskModel::EVENT_CLOSE => 'handleEvent', + TaskModel::EVENT_OPEN => 'handleEvent', + TaskModel::EVENT_MOVE_COLUMN => 'handleEvent', + TaskModel::EVENT_MOVE_POSITION => 'handleEvent', + TaskModel::EVENT_MOVE_SWIMLANE => 'handleEvent', + TaskModel::EVENT_ASSIGNEE_CHANGE => 'handleEvent', + SubtaskModel::EVENT_CREATE => 'handleEvent', + SubtaskModel::EVENT_UPDATE => 'handleEvent', + SubtaskModel::EVENT_DELETE => 'handleEvent', + CommentModel::EVENT_CREATE => 'handleEvent', + CommentModel::EVENT_UPDATE => 'handleEvent', + CommentModel::EVENT_DELETE => 'handleEvent', + CommentModel::EVENT_USER_MENTION => 'handleEvent', + TaskFileModel::EVENT_CREATE => 'handleEvent', + TaskLinkModel::EVENT_CREATE_UPDATE => 'handleEvent', + TaskLinkModel::EVENT_DELETE => 'handleEvent', ); } diff --git a/tests/units/EventBuilder/CommentEventBuilderTest.php b/tests/units/EventBuilder/CommentEventBuilderTest.php index 2f6a90b5a..ff1c630ea 100644 --- a/tests/units/EventBuilder/CommentEventBuilderTest.php +++ b/tests/units/EventBuilder/CommentEventBuilderTest.php @@ -33,5 +33,7 @@ class CommentEventBuilderTest extends Base $this->assertInstanceOf('Kanboard\Event\CommentEvent', $event); $this->assertNotEmpty($event['comment']); $this->assertNotEmpty($event['task']); + $this->assertEquals(1, $event->getTaskId()); + $this->assertEquals(1, $event->getProjectId()); } } diff --git a/tests/units/EventBuilder/ProjectFileEventBuilderTest.php b/tests/units/EventBuilder/ProjectFileEventBuilderTest.php index 8f5eb87ec..c35950296 100644 --- a/tests/units/EventBuilder/ProjectFileEventBuilderTest.php +++ b/tests/units/EventBuilder/ProjectFileEventBuilderTest.php @@ -29,5 +29,7 @@ class ProjectFileEventBuilderTest extends Base $this->assertInstanceOf('Kanboard\Event\ProjectFileEvent', $event); $this->assertNotEmpty($event['file']); $this->assertNotEmpty($event['project']); + $this->assertNull($event->getTaskId()); + $this->assertEquals(1, $event->getProjectId()); } } diff --git a/tests/units/EventBuilder/SubtaskEventBuilderTest.php b/tests/units/EventBuilder/SubtaskEventBuilderTest.php index fe425cb8c..215b1ed22 100644 --- a/tests/units/EventBuilder/SubtaskEventBuilderTest.php +++ b/tests/units/EventBuilder/SubtaskEventBuilderTest.php @@ -58,5 +58,7 @@ class SubtaskEventBuilderTest extends Base $this->assertCount(2, $event['changes']); $this->assertEquals('new title', $event['changes']['title']); $this->assertEquals(1, $event['changes']['user_id']); + $this->assertEquals(1, $event->getTaskId()); + $this->assertEquals(1, $event->getProjectId()); } } diff --git a/tests/units/EventBuilder/TaskEventBuilderTest.php b/tests/units/EventBuilder/TaskEventBuilderTest.php index c89dcd855..446b862b2 100644 --- a/tests/units/EventBuilder/TaskEventBuilderTest.php +++ b/tests/units/EventBuilder/TaskEventBuilderTest.php @@ -33,6 +33,8 @@ class TaskEventBuilderTest extends Base $this->assertInstanceOf('Kanboard\Event\TaskEvent', $event); $this->assertNotEmpty($event['task']); $this->assertEquals(1, $event['task_id']); + $this->assertEquals(1, $event->getTaskId()); + $this->assertEquals(1, $event->getProjectId()); $this->assertEquals(array('title' => 'after'), $event['changes']); } diff --git a/tests/units/EventBuilder/TaskFileEventBuilderTest.php b/tests/units/EventBuilder/TaskFileEventBuilderTest.php index c90e18d31..1e88b6fb9 100644 --- a/tests/units/EventBuilder/TaskFileEventBuilderTest.php +++ b/tests/units/EventBuilder/TaskFileEventBuilderTest.php @@ -32,5 +32,7 @@ class TaskFileEventBuilderTest extends Base $this->assertInstanceOf('Kanboard\Event\TaskFileEvent', $event); $this->assertNotEmpty($event['file']); $this->assertNotEmpty($event['task']); + $this->assertEquals(1, $event->getTaskId()); + $this->assertEquals(1, $event->getProjectId()); } } diff --git a/tests/units/EventBuilder/TaskLinkEventBuilderTest.php b/tests/units/EventBuilder/TaskLinkEventBuilderTest.php index 185081462..4e38eeb69 100644 --- a/tests/units/EventBuilder/TaskLinkEventBuilderTest.php +++ b/tests/units/EventBuilder/TaskLinkEventBuilderTest.php @@ -33,6 +33,8 @@ class TaskLinkEventBuilderTest extends Base $this->assertInstanceOf('Kanboard\Event\TaskLinkEvent', $event); $this->assertNotEmpty($event['task_link']); $this->assertNotEmpty($event['task']); + $this->assertEquals(1, $event->getTaskId()); + $this->assertEquals(1, $event->getProjectId()); } public function testBuildTitle() diff --git a/tests/units/Helper/TextHelperTest.php b/tests/units/Helper/TextHelperTest.php index 9b1aa94fb..8237c9e49 100644 --- a/tests/units/Helper/TextHelperTest.php +++ b/tests/units/Helper/TextHelperTest.php @@ -47,12 +47,12 @@ class TextHelperTest extends Base public function testMarkdownUserLink() { $h = new TextHelper($this->container); - $this->assertEquals('

Text @admin @notfound

', $h->markdown('Text @admin @notfound')); - $this->assertEquals('

Text @admin,

', $h->markdown('Text @admin,')); - $this->assertEquals('

Text @admin!

', $h->markdown('Text @admin!')); - $this->assertEquals('

Text @admin?

', $h->markdown('Text @admin? ')); - $this->assertEquals('

Text @admin.

', $h->markdown('Text @admin.')); - $this->assertEquals('

Text @admin: test

', $h->markdown('Text @admin: test')); + $this->assertEquals('

Text @admin @notfound

', $h->markdown('Text @admin @notfound')); + $this->assertEquals('

Text @admin,

', $h->markdown('Text @admin,')); + $this->assertEquals('

Text @admin!

', $h->markdown('Text @admin!')); + $this->assertEquals('

Text @admin?

', $h->markdown('Text @admin? ')); + $this->assertEquals('

Text @admin.

', $h->markdown('Text @admin.')); + $this->assertEquals('

Text @admin: test

', $h->markdown('Text @admin: test')); $this->assertEquals('

Text @admin @notfound

', $h->markdown('Text @admin @notfound', true)); } diff --git a/tests/units/Job/CommentEventJobTest.php b/tests/units/Job/CommentEventJobTest.php index 8571af8e7..b830b2b83 100644 --- a/tests/units/Job/CommentEventJobTest.php +++ b/tests/units/Job/CommentEventJobTest.php @@ -49,4 +49,46 @@ class CommentEventJobTest extends Base $this->assertArrayHasKey(CommentModel::EVENT_UPDATE.'.closure', $called); $this->assertArrayHasKey(CommentModel::EVENT_DELETE.'.closure', $called); } + + public function testThatUserMentionJobIsCalled() + { + $comment = 'some comment'; + + $this->container['queueManager'] = $this + ->getMockBuilder('\Kanboard\Core\Queue\QueueManager') + ->setConstructorArgs(array($this->container)) + ->setMethods(array( + 'push', + )) + ->getMock(); + + $this->container['userMentionJob'] = $this + ->getMockBuilder('\Kanboard\Job\UserMentionJob') + ->setConstructorArgs(array($this->container)) + ->setMethods(array( + 'withParams', + )) + ->getMock(); + + $this->container['queueManager'] + ->expects($this->any()) + ->method('push'); + + $this->container['userMentionJob'] + ->expects($this->once()) + ->method('withParams') + ->with($comment, CommentModel::EVENT_USER_MENTION, $this->anything()) + ->will($this->returnValue($this->container['userMentionJob'])); + + $commentModel = new CommentModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $projectModel = new ProjectModel($this->container); + $commentEventJob = new CommentEventJob($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1))); + $this->assertEquals(1, $commentModel->create(array('task_id' => 1, 'comment' => $comment, 'user_id' => 1))); + + $commentEventJob->execute(1, CommentModel::EVENT_CREATE); + } } diff --git a/tests/units/Job/TaskEventJobTest.php b/tests/units/Job/TaskEventJobTest.php index c399faad9..bfd7bc553 100644 --- a/tests/units/Job/TaskEventJobTest.php +++ b/tests/units/Job/TaskEventJobTest.php @@ -186,4 +186,44 @@ class TaskEventJobTest extends Base $called = $this->container['dispatcher']->getCalledListeners(); $this->assertArrayHasKey(TaskModel::EVENT_MOVE_PROJECT.'.closure', $called); } + + public function testThatUserMentionJobIsCalled() + { + $description = 'something'; + + $this->container['queueManager'] = $this + ->getMockBuilder('\Kanboard\Core\Queue\QueueManager') + ->setConstructorArgs(array($this->container)) + ->setMethods(array( + 'push', + )) + ->getMock(); + + $this->container['userMentionJob'] = $this + ->getMockBuilder('\Kanboard\Job\UserMentionJob') + ->setConstructorArgs(array($this->container)) + ->setMethods(array( + 'withParams', + )) + ->getMock(); + + $this->container['queueManager'] + ->expects($this->any()) + ->method('push'); + + $this->container['userMentionJob'] + ->expects($this->once()) + ->method('withParams') + ->with($description, TaskModel::EVENT_USER_MENTION, $this->anything()) + ->will($this->returnValue($this->container['userMentionJob'])); + + $taskCreationModel = new TaskCreationModel($this->container); + $projectModel = new ProjectModel($this->container); + $taskEventJob = new TaskEventJob($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'description' => $description, 'project_id' => 1))); + + $taskEventJob->execute(1, array(TaskModel::EVENT_CREATE)); + } } diff --git a/tests/units/Model/UserMentionTest.php b/tests/units/Job/UserMentionJobTest.php similarity index 56% rename from tests/units/Model/UserMentionTest.php rename to tests/units/Job/UserMentionJobTest.php index b41c92cd6..31d0ddbb0 100644 --- a/tests/units/Model/UserMentionTest.php +++ b/tests/units/Job/UserMentionJobTest.php @@ -1,45 +1,60 @@ container); - $userMentionModel = new UserMentionModel($this->container); + $userMentionJob = new UserMentionJob($this->container); $this->assertNotFalse($userModel->create(array('username' => 'user1'))); - $this->assertEmpty($userMentionModel->getMentionedUsers('test')); + $this->assertEmpty($userMentionJob->getMentionedUsers('test')); } public function testGetMentionedUsersWithNotficationDisabled() { $userModel = new UserModel($this->container); - $userMentionModel = new UserMentionModel($this->container); + $userMentionJob = new UserMentionJob($this->container); $this->assertNotFalse($userModel->create(array('username' => 'user1'))); - $this->assertEmpty($userMentionModel->getMentionedUsers('test @user1')); + $this->assertEmpty($userMentionJob->getMentionedUsers('test @user1')); } public function testGetMentionedUsersWithNotficationEnabled() { $userModel = new UserModel($this->container); - $userMentionModel = new UserMentionModel($this->container); + $userMentionJob = new UserMentionJob($this->container); $this->assertNotFalse($userModel->create(array('username' => 'user1'))); $this->assertNotFalse($userModel->create(array('username' => 'user2', 'name' => 'Foobar', 'notifications_enabled' => 1))); - $users = $userMentionModel->getMentionedUsers('test @user2'); + $users = $userMentionJob->getMentionedUsers('test @user2'); + $this->assertCount(1, $users); + $this->assertEquals('user2', $users[0]['username']); + $this->assertEquals('Foobar', $users[0]['name']); + $this->assertEquals('', $users[0]['email']); + $this->assertEquals('', $users[0]['language']); + } + + public function testGetMentionedUsersWithNotficationEnabledAndPunctuationMarks() + { + $userModel = new UserModel($this->container); + $userMentionJob = new UserMentionJob($this->container); + + $this->assertNotFalse($userModel->create(array('username' => 'user1'))); + $this->assertNotFalse($userModel->create(array('username' => 'user2', 'name' => 'Foobar', 'notifications_enabled' => 1))); + + $users = $userMentionJob->getMentionedUsers('test @user2, test'); $this->assertCount(1, $users); $this->assertEquals('user2', $users[0]['username']); $this->assertEquals('Foobar', $users[0]['name']); @@ -51,12 +66,12 @@ class UserMentionTest extends Base { $this->container['sessionStorage']->user = array('id' => 3); $userModel = new UserModel($this->container); - $userMentionModel = new UserMentionModel($this->container); + $userMentionJob = new UserMentionJob($this->container); $this->assertNotFalse($userModel->create(array('username' => 'user1'))); $this->assertNotFalse($userModel->create(array('username' => 'user2', 'name' => 'Foobar', 'notifications_enabled' => 1))); - $this->assertEmpty($userMentionModel->getMentionedUsers('test @user2')); + $this->assertEmpty($userMentionJob->getMentionedUsers('test @user2')); } public function testFireEventsWithMultipleMentions() @@ -64,8 +79,8 @@ class UserMentionTest extends Base $projectUserRoleModel = new ProjectUserRoleModel($this->container); $projectModel = new ProjectModel($this->container); $userModel = new UserModel($this->container); - $userMentionModel = new UserMentionModel($this->container); - $event = new GenericEvent(array('project_id' => 1)); + $userMentionJob = new UserMentionJob($this->container); + $event = new TaskEvent(array('task' => array('project_id' => 1))); $this->assertEquals(2, $userModel->create(array('username' => 'user1', 'name' => 'User 1', 'notifications_enabled' => 1))); $this->assertEquals(3, $userModel->create(array('username' => 'user2', 'name' => 'User 2', 'notifications_enabled' => 1))); @@ -75,35 +90,10 @@ class UserMentionTest extends Base $this->container['dispatcher']->addListener(TaskModel::EVENT_USER_MENTION, array($this, 'onUserMention')); - $userMentionModel->fireEvents('test @user1 @user2', TaskModel::EVENT_USER_MENTION, $event); + $userMentionJob->execute('test @user1 @user2', TaskModel::EVENT_USER_MENTION, $event); $called = $this->container['dispatcher']->getCalledListeners(); - $this->assertArrayHasKey(TaskModel::EVENT_USER_MENTION.'.UserMentionTest::onUserMention', $called); - } - - public function testFireEventsWithNoProjectId() - { - $projectUserRoleModel = new ProjectUserRoleModel($this->container); - $projectModel = new ProjectModel($this->container); - $taskCreationModel = new TaskCreationModel($this->container); - $userModel = new UserModel($this->container); - $userMentionModel = new UserMentionModel($this->container); - $event = new GenericEvent(array('task_id' => 1)); - - $this->assertEquals(2, $userModel->create(array('username' => 'user1', 'name' => 'User 1', 'notifications_enabled' => 1))); - $this->assertEquals(3, $userModel->create(array('username' => 'user2', 'name' => 'User 2', 'notifications_enabled' => 1))); - - $this->assertEquals(1, $projectModel->create(array('name' => 'P1'))); - $this->assertTrue($projectUserRoleModel->addUser(1, 3, Role::PROJECT_MEMBER)); - - $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'Task 1'))); - - $this->container['dispatcher']->addListener(TaskModel::EVENT_USER_MENTION, array($this, 'onUserMention')); - - $userMentionModel->fireEvents('test @user1 @user2', TaskModel::EVENT_USER_MENTION, $event); - - $called = $this->container['dispatcher']->getCalledListeners(); - $this->assertArrayHasKey(TaskModel::EVENT_USER_MENTION.'.UserMentionTest::onUserMention', $called); + $this->assertArrayHasKey(TaskModel::EVENT_USER_MENTION.'.UserMentionJobTest::onUserMention', $called); } public function onUserMention($event)