summaryrefslogtreecommitdiffstats
path: root/tests/Unit/Service/ShareServiceTest.php
blob: a3c9d81d2889a54a6a7c634faf1ddde234eba8d4 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/**
 * Nextcloud - News
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author    Marco Nassabain <marco.nassabain@hotmail.com>
 */


namespace OCA\News\Tests\Unit\Service;

use FeedIo\Explorer;
use FeedIo\Reader\ReadErrorException;

use OC\L10N\L10N;
use OCA\News\Service\Exceptions\ServiceConflictException;
use OCA\News\Service\Exceptions\ServiceNotFoundException;
use OCP\AppFramework\Db\DoesNotExistException;
use OCA\News\Service\FeedServiceV2;
use OCA\News\Service\ItemServiceV2;
use OCA\News\Service\ShareService;
use OCA\News\Utility\Time;

use OCA\News\Db\Feed;
use OCA\News\Db\Item;

use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IUser;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;


class ShareServiceTest extends TestCase
{
    /**
     * @var MockObject|ItemServiceV2
     */
    private $itemService;

    /**
     * @var MockObject|FeedServiceV2
     */
    private $feedService;

    /**
     * @var MockObject|IURLGenerator
     */
    private $urlGenerator;

    /**
     * @var MockObject|IUserManager
     */
    private $userManager;

    /**
     * @var MockObject|IL10N
     */
    private $l;

    /**
     * @var MockObject|LoggerInterface
     */
    private $logger;

    /** @var ShareService */
    private $class;

    /**
     * @var string
     */
    private $uid;

    /**
     * @var string
     */
    private $recipient;

    protected function setUp(): void
    {
        $this->logger = $this->getMockBuilder(LoggerInterface::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->itemService = $this
            ->getMockBuilder(ItemServiceV2::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->feedService = $this
            ->getMockBuilder(FeedServiceV2::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->urlGenerator = $this
            ->getMockBuilder(IURLGenerator::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->userManager = $this
            ->getMockBuilder(IUserManager::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->l = $this->getMockBuilder(IL10N::class)
            ->disableOriginalConstructor()
            ->getMock();

        $this->time = 333333;

        $this->class = new ShareService(
            $this->feedService,
            $this->itemService,
            $this->urlGenerator,
            $this->userManager,
            $this->l,
            $this->logger
        );

        $this->uid = 'sender';
        $this->recipient = 'recipient';
    }

    public function testShareItemWithUser()
    {
        $feedUrl = 'http://serverurl/news/sharedwithme';
        $itemId = 3;

        // Item to be shared
        $item = new Item();
        $item->setGuid('_guid_')
            ->setGuidHash(md5('_guid_'))
            ->setUrl('_url_')
            ->setTitle('_title_')
            ->setAuthor('_author_')
            ->setUnread(0)
            ->setStarred(1)
            ->setFeedId(10);

        // Shared item
        $sharedItem = clone $item;
        $sharedItem->setUnread(1)       // A newly shared item is unread, ...
            ->setStarred(0)             // ... not starred, ...
            ->setFeedId(100)            // ... placed in the 'Shared with me' feed, ...
            ->setSharedBy($this->uid);  // ... and contains the senders user ID

        // Dummy feed 'Shared with me'
        $feed = new Feed();
        $feed->setId(100);
        $feed->setUserId($this->recipient)
            ->setUrl($feedUrl)
            ->setLink($feedUrl)
            ->setTitle('Shared with me')
            ->setAdded($this->time)
            ->setFolderId(null)
            ->setPreventUpdate(true);


        $this->itemService->expects($this->once())
            ->method('find')
            ->with($this->uid, $itemId)
            ->will($this->returnValue($item));

        $this->urlGenerator->expects($this->once())
            ->method('getBaseUrl')
            ->will($this->returnValue('http://serverurl'));

        $this->feedService->expects($this->once())
            ->method('findByUrl')
            ->with($this->recipient, $feedUrl)
            ->will($this->returnValue($feed));

        // Here we test if the setters worked properly using 'with()'
        $this->itemService->expects($this->once())
            ->method('insertOrUpdate')
            ->with($sharedItem)
            ->will($this->returnValue($sharedItem));


        $this->class->shareItemWithUser($this->uid, $itemId, $this->recipient);
    }

    public function testShareItemWithUserCreatesOwnFeedWhenNotFound()
    {
        $feedUrl = 'http://serverurl/news/sharedwithme';
        $itemId = 3;

        // Item to be shared
        $item = new Item();
        $item->setGuid('_guid_')
            ->setGuidHash(md5('_guid_'))
            ->setUrl('_url_')
            ->setTitle('_title_')
            ->setAuthor('_author_')
            ->setUnread(0)
            ->setStarred(1)
            ->setFeedId(10);

        // Shared item
        $sharedItem = clone $item;
        $sharedItem->setUnread(1)       // A newly shared item is unread, ...
            ->setStarred(0)             // ... not starred, ...
            ->setFeedId(100)            // ... placed in the 'Shared with me' feed, ...
            ->setSharedBy($this->uid);  // ... and contains the senders user ID

        // Dummy feed 'Shared with me'
        $feed = new Feed();
        $feed->setId(100);
        $feed->setUserId($this->recipient)
            ->setUrl($feedUrl)
            ->setLink($feedUrl)
            ->setTitle('Shared with me')
            ->setAdded($this->time)
            ->setFolderId(null)
            ->setPreventUpdate(true);


        $this->itemService->expects($this->once())
            ->method('find')
            ->with($this->uid, $itemId)
            ->will($this->returnValue($item));

        $this->urlGenerator->expects($this->once())
            ->method('getBaseUrl')
            ->will($this->returnValue('http://serverurl'));

        $this->feedService->expects($this->once())
            ->method('findByUrl')
            ->with($this->recipient, $feedUrl)
            ->will($this->returnValue(null));

        $this->l->expects($this->once())
            ->method('t')
            ->with('Shared with me')
            ->will($this->returnValue('Shared with me'));

        $this->feedService->expects($this->once())
            ->method('insert')
            ->will($this->returnValue($feed));

        // Here we test if the setters worked properly using 'with()'
        $this->itemService->expects($this->once())
            ->method('insertOrUpdate')
            ->with($sharedItem)
            ->will($this->returnValue($sharedItem));


        $this->class->shareItemWithUser($this->uid, $itemId, $this->recipient);
    }


    public function testShareItemWithUserItemDoesNotExist()
    {
        $this->expectException(ServiceNotFoundException::class);
        $this->itemService->expects($this->once())
            ->method('find')
            ->will($this->throwException(new ServiceNotFoundException('')));

        $this->class->shareItemWithUser('sender', 1, 'recipient');
    }


    public function testMapSharedByDisplayNames()
    {
        $item1 = new Item();
        $item1->setTitle('Item 1')
              ->setSharedBy('sender');
        $item2 = new Item();
        $item2->setTitle('Item 2')
              ->setSharedBy(null);

        $items = [$item1, $item2];
        $user = $this->getMockBuilder(IUser::class)
                     ->getMock();

        $this->userManager->expects($this->once())
            ->method('get')
            ->with('sender')
            ->will($this->returnValue($user));

        $user->expects($this->once())
            ->method('getDisplayName')
            ->will($this->returnValue('Mr. Sender'));

        $result = $this->class->mapSharedByDisplayNames($items);

        $this->assertEquals('Mr. Sender', $result[0]->getSharedByDisplayName());
        $this->assertEquals(null, $result[1]->getSharedByDisplayName());
    }
}