summaryrefslogtreecommitdiffstats
path: root/tests/Integration/Db/FeedMapperTest.php
blob: b429149a44c44e46af6ab36cf27004443362e9f9 (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
<?php
/**
 * Nextcloud - News
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Bernhard Posselt <dev@bernhard-posselt.com>
 * @author Daniel Opitz <dev@copynpaste.de>
 * @copyright Bernhard Posselt 2015
 * @copyright Daniel Opitz 2017
 */

namespace OCA\News\Db;

use OCA\News\Tests\Integration\Fixtures\FeedFixture;
use OCA\News\Tests\Integration\IntegrationTest;

class FeedMapperTest extends IntegrationTest {

    public function testFind () {
        $feed = new FeedFixture();
        $feed = $this->feedMapper->insert($feed);

        $fetched = $this->feedMapper->find($feed->getId(), $this->user);

        $this->assertInstanceOf(Feed::class, $fetched);
        $this->assertEquals($feed->getLink(), $fetched->getLink());
    }

    /**
     * @expectedException OCP\AppFramework\Db\DoesNotExistException
     */
    public function testFindNotExisting () {
        $this->feedMapper->find(0, $this->user);
    }


    public function testFindAll () {
        $feeds = [
            [
                'userId' => $this->user,
                'items' => []
            ],
            [
                'userId' => 'john',
                'items' => []
            ]
        ];
        $this->loadFeedFixtures($feeds);

        $fetched = $this->feedMapper->findAll();

        $this->assertInternalType('array', $fetched);
        $this->assertCount(2, $fetched);
        $this->assertContainsOnlyInstancesOf(Feed::class, $fetched);

        $this->tearDownUser('john');
    }

    public function testFindAllEmpty () {
        $feeds = $this->feedMapper->findAll();

        $this->assertInternalType('array', $feeds);
        $this->assertCount(0, $feeds);
    }


    public function testFindAllFromUser () {
        $feeds = [
            [
                'userId' => $this->user,
                'items' => []
            ],
            [
                'userId' => 'john',
                'items' => []
            ]
        ];
        $this->loadFeedFixtures($feeds);

        $fetched = $this->feedMapper->findAllFromUser($this->user);

        $this->assertInternalType('array', $fetched);
        $this->assertCount(1, $fetched);
        $this->assertContainsOnlyInstancesOf(Feed::class, $fetched);

        $this->tearDownUser('john');
    }


    public function testFindAllFromUserNotExisting () {
        $fetched = $this->feedMapper->findAllFromUser('notexistinguser');

        $this->assertInternalType('array', $fetched);
        $this->assertCount(0, $fetched);
    }


    public function testFindByUrlHash () {
        $feed = new FeedFixture([
            'urlHash' => 'someTestHash',
            'title' => 'Some Test Title'
        ]);
        $feed = $this->feedMapper->insert($feed);

        $fetched = $this->feedMapper->findByUrlHash($feed->getUrlHash(), $this->user);

        $this->assertInstanceOf(Feed::class, $fetched);
        $this->assertEquals($feed->getTitle(), $fetched->getTitle());
    }

    /**
     * @expectedException OCP\AppFramework\Db\MultipleObjectsReturnedException
     */
    public function testFindByUrlHashMoreThanOneResult () {
        $feed1 = $this->feedMapper->insert(new FeedFixture([
            'urlHash' => 'someTestHash'
        ]));
        $feed2 = $this->feedMapper->insert(new FeedFixture([
            'urlHash' => 'someTestHash'
        ]));

        $this->feedMapper->findByUrlHash($feed1->getUrlHash(), $this->user);
    }


    /**
     * @expectedException OCP\AppFramework\Db\DoesNotExistException
     */
    public function testFindByUrlHashNotExisting () {
        $this->feedMapper->findByUrlHash('some random hash', $this->user);
    }


    public function testDelete () {
        $this->loadFixtures('default');

        $feeds = $this->feedMapper->findAllFromUser($this->user);
        $this->assertCount(4, $feeds);

        $feed = reset($feeds);

        $items = $this->itemMapper->findAllFeed(
            $feed->getId(), 100, 0, true, false, $this->user
        );
        $this->assertCount(7, $items);

        $this->feedMapper->delete($feed);

        $this->assertCount(3, $this->feedMapper->findAllFromUser($this->user));

        $items = $this->itemMapper->findAllFeed(
            $feed->getId(), 100, 0, true, false, $this->user
        );
        $this->assertCount(0, $items);
    }

    public function testGetToDelete () {
        $this->loadFeedFixtures([
            ['deletedAt' => 1],
            ['deletedAt' => 0],
            ['deletedAt' => 1, 'userId' => 'john'],
            ['deletedAt' => 1000]
        ]);

        $fetched = $this->feedMapper->getToDelete();

        $this->assertInternalType('array', $fetched);
        $this->assertCount(3, $fetched);
        $this->assertContainsOnlyInstancesOf(Feed::class, $fetched);

        $this->tearDownUser('john');
    }

    public function testGetToDeleteOlderThan () {
        $this->loadFeedFixtures([
            ['deletedAt' => 1],
            ['deletedAt' => 0],
            ['deletedAt' => 1, 'userId' => 'john'],
            ['deletedAt' => 1000]
        ]);

        $fetched = $this->feedMapper->getToDelete(1000);

        $this->assertInternalType('array', $fetched);
        $this->assertCount(2, $fetched);
        $this->assertContainsOnlyInstancesOf(Feed::class, $fetched);

        $this->tearDownUser('john');
    }

    public function testGetToDeleteUser () {
        $this->loadFeedFixtures([
            ['deletedAt' => 1],
            ['deletedAt' => 0],
            ['deletedAt' => 1, 'userId' => 'john'],
            ['deletedAt' => 1000]
        ]);

        $fetched = $this->feedMapper->getToDelete(2000, $this->user);

        $this->assertInternalType('array', $fetched);
        $this->assertCount(2, $fetched);
        $this->assertContainsOnlyInstancesOf(Feed::class, $fetched);

        $this->tearDownUser('john');
    }

    public function testGetToDeleteEmpty () {
        $fetched = $this->feedMapper->getToDelete();

        $this->assertInternalType('array', $fetched);
        $this->assertCount(0, $fetched);
    }

    public function testDeleteUser () {
        $this->loadFixtures('default');

        $this->assertCount(4, $this->feedMapper->findAllFromUser($this->user));

        $items = $this->itemMapper->findAll(100, 0, 0, true, false, $this->user);
        $this->assertCount(9, $items);

        $this->feedMapper->deleteUser($this->user);

        $this->assertCount(0, $this->feedMapper->findAllFromUser($this->user));

        $items = $this->itemMapper->findAll(100, 0, 0, true, false, $this->user);
        $this->assertCount(0, $items);
    }

    public function testDeleteUserNotExisting () {
        $this->feedMapper->deleteUser('notexistinguser');
    }
}