summaryrefslogtreecommitdiffstats
path: root/tests/Unit/Controller/UserApiControllerTest.php
blob: d562329f310ab2a4aeedfbe8bb5e659a2f4bbdc7 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
 * Nextcloud - News
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author    Alessandro Cosentino <cosenal@gmail.com>
 * @author    Bernhard Posselt <dev@bernhard-posselt.com>
 * @copyright 2012 Alessandro Cosentino
 * @copyright 2012-2014 Bernhard Posselt
 */

namespace OCA\News\Tests\Unit\Controller;

use OCA\News\Controller\UserApiController;

use PHPUnit\Framework\TestCase;

class UserApiControllerTest extends TestCase
{

    private $request;
    private $appName;
    private $rootFolder;
    private $userSession;
    private $controller;
    private $user;
    private $file;

    protected function setUp() 
    {
        $this->appName = 'news';
        $this->request = $this->getMockBuilder(
            '\OCP\IRequest'
        )
            ->disableOriginalConstructor()
            ->getMock();
        $this->rootFolder = $this->getMockBuilder(
            '\OCP\Files\IRootFolder'
        )
            ->disableOriginalConstructor()
            ->getMock();
        $this->file = $this->getMockBuilder(
            '\OCP\Files\File'
        )
            ->disableOriginalConstructor()
            ->getMock();
        $this->userSession = $this->getMockBuilder(
            '\OCP\IUserSession'
        )
            ->disableOriginalConstructor()
            ->getMock();
        $this->user = $this->getMockBuilder(
            '\OCP\IUser'
        )
            ->disableOriginalConstructor()
            ->getMock();
        $this->controller = new UserApiController(
            $this->appName, $this->request, $this->userSession,
            $this->rootFolder
        );


    }

    private function expectUser($uid, $displayName, $lastLogin) 
    {
        $this->userSession->expects($this->any())
            ->method('getUser')
            ->will($this->returnValue($this->user));
        $this->user->expects($this->any())
            ->method('getUID')
            ->will($this->returnValue($uid));
        $this->user->expects($this->any())
            ->method('getLastLogin')
            ->will($this->returnValue($lastLogin));
        $this->user->expects($this->any())
            ->method('getDisplayName')
            ->will($this->returnValue($displayName));
    }

    private function expectImg($isJpg, $isPng, $user, $exists, $data) 
    {
        $jpg = '/' . $user . '/' . 'avatar.jpg';
        $png = '/' . $user . '/' . 'avatar.png';

        $this->rootFolder->expects($this->any())
            ->method('nodeExists')
            ->will(
                $this->returnValueMap(
                    [
                    [$jpg, $isJpg],
                    [$png, $isPng]
                    ]
                )
            );
        $this->rootFolder->expects($this->any())
            ->method('get')
            ->will($this->returnValue($this->file));
        $this->file->expects($this->any())
            ->method('getContent')
            ->will($this->returnValue($data));
    }

    public function testGetJpeg() 
    {
        $this->expectUser('john', 'John', 123);
        $this->expectImg(true, false, 'john', true, 'hi');

        $result = $this->controller->index();
        $expected = [
            'userId' => 'john',
            'displayName' => 'John',
            'lastLoginTimestamp' => 123,
            'avatar' => [
                'data' => base64_encode('hi'),
                'mime' => 'image/jpeg'
            ]
        ];

        $this->assertEquals($expected, $result);
    }

    public function testGetPng() 
    {
        $this->expectUser('john', 'John', 123);
        $this->expectImg(false, true, 'john', false, 'hi');

        $result = $this->controller->index();
        $expected = [
            'userId' => 'john',
            'displayName' => 'John',
            'lastLoginTimestamp' => 123,
            'avatar' => [
                'data' => base64_encode('hi'),
                'mime' => 'image/png'
            ]
        ];

        $this->assertEquals($expected, $result);
    }

    public function testNoAvatar() 
    {
        $this->expectUser('john', 'John', 123);
        $this->expectImg(false, false, 'john', false, 'hi');

        $result = $this->controller->index();
        $expected = [
            'userId' => 'john',
            'displayName' => 'John',
            'lastLoginTimestamp' => 123,
            'avatar' => null
        ];

        $this->assertEquals($expected, $result);
    }

}