summaryrefslogtreecommitdiffstats
path: root/tests/Unit/Controller/ExportControllerTest.php
blob: cb21822bed3eec6765164b94233f800490c0aed3 (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
<?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\ExportController;
use OCA\News\Service\FeedServiceV2;
use OCA\News\Service\FolderServiceV2;

use OCA\News\Service\ItemServiceV2;
use OCA\News\Service\OpmlService;
use \OCA\News\Utility\OPMLExporter;
use \OCA\News\Db\Item;
use \OCA\News\Db\Feed;
use OCP\AppFramework\Http\DataDownloadResponse;
use OCP\IRequest;

use OCP\IUser;
use OCP\IUserSession;
use PHPUnit\Framework\TestCase;

class ExportControllerTest extends TestCase
{

    private $controller;
    private $user;
    /**
     * @var \PHPUnit\Framework\MockObject\MockObject|IUserSession
     */
    private $userSession;
    /**
     * @var \PHPUnit\Framework\MockObject\MockObject|FeedServiceV2
     */
    private $feedService;
    /**
     * @var \PHPUnit\Framework\MockObject\MockObject|FolderServiceV2
     */
    private $folderService;
    /**
     * @var \PHPUnit\Framework\MockObject\MockObject|ItemServiceV2
     */
    private $itemService;
    /**
     * @var \PHPUnit\Framework\MockObject\MockObject|OpmlService
     */
    private $opmlService;

    /**
     * Gets run before each test
     */
    public function setUp(): void
    {
        $appName = 'news';
        $this->itemService = $this->getMockBuilder(ItemServiceV2::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->feedService = $this->getMockBuilder(FeedServiceV2::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->folderService = $this->getMockBuilder(FolderServiceV2::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->opmlService = $this->getMockBuilder(OpmlService::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->user = $this->getMockBuilder(IUser::class)->getMock();
        $this->user->expects($this->any())
                   ->method('getUID')
                   ->will($this->returnValue('user'));
        $this->userSession = $this->getMockBuilder(IUserSession::class)
                                  ->getMock();
        $this->userSession->expects($this->any())
                          ->method('getUser')
                          ->will($this->returnValue($this->user));
        $request = $this->getMockBuilder(IRequest::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->controller = new ExportController(
            $request,
            $this->folderService,
            $this->feedService,
            $this->itemService,
            $this->opmlService,
            $this->userSession
        );
    }


    public function testOpmlExportNoFeeds()
    {
        $opml =
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" .
        "<opml version=\"2.0\">\n" .
        "  <head>\n" .
        "    <title>Subscriptions</title>\n" .
        "  </head>\n" .
        "  <body/>\n" .
        "</opml>\n";

        $this->opmlService->expects($this->once())
            ->method('export')
            ->with('user')
            ->will($this->returnValue($opml));

        $return = $this->controller->opml();
        $this->assertTrue($return instanceof DataDownloadResponse);
        $this->assertEquals($opml, $return->render());
    }


    public function testGetAllArticles()
    {
        $item1 = new Item();
        $item1->setFeedId(3);
        $item1->setGuid('guid');
        $item2 = new Item();
        $item2->setFeedId(5);
        $item2->setGuid('guid');

        $feed1 = new Feed();
        $feed1->setId(3);
        $feed1->setLink('http://goo');
        $feed2 = new Feed();
        $feed2->setId(5);
        $feed2->setLink('http://gee');
        $feeds = [$feed1, $feed2];

        $articles = [$item1, $item2];

        $this->feedService->expects($this->once())
            ->method('findAllForUser')
            ->with('user')
            ->will($this->returnValue($feeds));
        $this->itemService->expects($this->exactly(2))
            ->method('findAllForUser')
            ->withConsecutive(['user', ['unread' => false, 'starred' => true]], ['user', ['unread' => true]])
            ->willReturnOnConsecutiveCalls($articles, []);


        $return = $this->controller->articles();
        $headers = $return->getHeaders();
        $this->assertEquals(
            'attachment; filename="articles.json"',
            $headers ['Content-Disposition']
        );

        $this->assertEquals(
            '[{"guid":"guid","url":null,"title":null,' .
            '"author":null,"pubDate":null,"updatedDate":null,"body":null,"enclosureMime":null,' .
            '"enclosureLink":null,"mediaThumbnail":null,"mediaDescription":null,'.
            '"unread":false,"starred":false,' .
            '"feedLink":"http:\/\/goo","rtl":false},{"guid":"guid","url":null,' .
            '"title":null,"author":null,"pubDate":null,"updatedDate":null,"body":null,' .
            '"enclosureMime":null,"enclosureLink":null,"mediaThumbnail":null,'.
            '"mediaDescription":null,"unread":false,' .
            '"starred":false,"feedLink":"http:\/\/gee","rtl":false}]',
            $return->render()
        );
    }

}