summaryrefslogtreecommitdiff
path: root/tests/units/Job/UserMentionJobTest.php
blob: 31d0ddbb05f98432d7b8836945ddf6bed14753cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php

use Kanboard\Core\Security\Role;
use Kanboard\Event\TaskEvent;
use Kanboard\Job\UserMentionJob;
use Kanboard\Model\ProjectModel;
use Kanboard\Model\ProjectUserRoleModel;
use Kanboard\Model\TaskModel;
use Kanboard\Model\UserModel;

require_once __DIR__.'/../Base.php';

class UserMentionJobTest extends Base
{
    public function testGetMentionedUsersWithNoMentions()
    {
        $userModel = new UserModel($this->container);
        $userMentionJob = new UserMentionJob($this->container);

        $this->assertNotFalse($userModel->create(array('username' => 'user1')));
        $this->assertEmpty($userMentionJob->getMentionedUsers('test'));
    }

    public function testGetMentionedUsersWithNotficationDisabled()
    {
        $userModel = new UserModel($this->container);
        $userMentionJob = new UserMentionJob($this->container);

        $this->assertNotFalse($userModel->create(array('username' => 'user1')));
        $this->assertEmpty($userMentionJob->getMentionedUsers('test @user1'));
    }

    public function testGetMentionedUsersWithNotficationEnabled()
    {
        $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');
        $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']);
        $this->assertEquals('', $users[0]['email']);
        $this->assertEquals('', $users[0]['language']);
    }

    public function testGetMentionedUsersWithNotficationEnabledAndUserLoggedIn()
    {
        $this->container['sessionStorage']->user = array('id' => 3);
        $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)));

        $this->assertEmpty($userMentionJob->getMentionedUsers('test @user2'));
    }

    public function testFireEventsWithMultipleMentions()
    {
        $projectUserRoleModel = new ProjectUserRoleModel($this->container);
        $projectModel = new ProjectModel($this->container);
        $userModel = new UserModel($this->container);
        $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)));

        $this->assertEquals(1, $projectModel->create(array('name' => 'P1')));
        $this->assertTrue($projectUserRoleModel->addUser(1, 3, Role::PROJECT_MEMBER));

        $this->container['dispatcher']->addListener(TaskModel::EVENT_USER_MENTION, array($this, 'onUserMention'));

        $userMentionJob->execute('test @user1 @user2', TaskModel::EVENT_USER_MENTION, $event);

        $called = $this->container['dispatcher']->getCalledListeners();
        $this->assertArrayHasKey(TaskModel::EVENT_USER_MENTION.'.UserMentionJobTest::onUserMention', $called);
    }

    public function onUserMention($event)
    {
        $this->assertInstanceOf('Kanboard\Event\GenericEvent', $event);
        $this->assertEquals(array('id' => '3', 'username' => 'user2', 'name' => 'User 2', 'email' => null, 'language' => null), $event['mention']);
    }
}