summaryrefslogtreecommitdiff
path: root/tests/units/Subscriber/LdapUserPhotoSubscriberTest.php
blob: 41f408441e9019d2e0858a164e3534e58cef87d5 (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
<?php

use Kanboard\Core\Security\Role;
use Kanboard\Event\UserProfileSyncEvent;
use Kanboard\Model\UserModel;
use Kanboard\Subscriber\LdapUserPhotoSubscriber;
use Kanboard\User\DatabaseUserProvider;
use Kanboard\User\LdapUserProvider;

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

class LdapUserPhotoSubscriberTest extends Base
{
    public function testWhenTheProviderIsNotLdap()
    {
        $userProvider = new DatabaseUserProvider(array());
        $subscriber = new LdapUserPhotoSubscriber($this->container);
        $userModel = new UserModel($this->container);

        $userModel->update(array('id' => 1, 'avatar_path' => 'my avatar'));
        $user = $userModel->getById(1);

        $subscriber->syncUserPhoto(new UserProfileSyncEvent($user, $userProvider));

        $user = $userModel->getById(1);
        $this->assertEquals('my avatar', $user['avatar_path']);
    }

    public function testWhenTheUserHaveLdapPhoto()
    {
        $userProvider = new LdapUserProvider('dn', 'admin', 'Admin', 'admin@localhost', Role::APP_ADMIN, array(), 'my photo');
        $subscriber = new LdapUserPhotoSubscriber($this->container);
        $userModel = new UserModel($this->container);
        $user = $userModel->getById(1);

        $this->container['objectStorage']
            ->expects($this->once())
            ->method('put')
            ->with($this->anything(), 'my photo');


        $subscriber->syncUserPhoto(new UserProfileSyncEvent($user, $userProvider));

        $user = $userModel->getById(1);
        $this->assertStringStartsWith('avatars', $user['avatar_path']);
    }

    public function testWhenTheUserDoNotHaveLdapPhoto()
    {
        $userProvider = new LdapUserProvider('dn', 'admin', 'Admin', 'admin@localhost', Role::APP_ADMIN, array());
        $subscriber = new LdapUserPhotoSubscriber($this->container);
        $userModel = new UserModel($this->container);
        $user = $userModel->getById(1);

        $this->container['objectStorage']
            ->expects($this->never())
            ->method('put');

        $subscriber->syncUserPhoto(new UserProfileSyncEvent($user, $userProvider));

        $user = $userModel->getById(1);
        $this->assertEmpty($user['avatar_path']);
    }

    public function testWhenTheUserAlreadyHaveAvatar()
    {
        $userProvider = new LdapUserProvider('dn', 'admin', 'Admin', 'admin@localhost', Role::APP_ADMIN, array(), 'my photo');
        $subscriber = new LdapUserPhotoSubscriber($this->container);
        $userModel = new UserModel($this->container);

        $userModel->update(array('id' => 1, 'avatar_path' => 'my avatar'));
        $user = $userModel->getById(1);

        $this->container['objectStorage']
            ->expects($this->never())
            ->method('put');

        $subscriber->syncUserPhoto(new UserProfileSyncEvent($user, $userProvider));

        $user = $userModel->getById(1);
        $this->assertEquals('my avatar', $user['avatar_path']);
    }
}