]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/ImportBundle/Import/WallabagV1ImportTest.php
Merge remote-tracking branch 'origin/master' into 2.3
[github/wallabag/wallabag.git] / tests / Wallabag / ImportBundle / Import / WallabagV1ImportTest.php
CommitLineData
b1d05721
JB
1<?php
2
23634d5d 3namespace Tests\Wallabag\ImportBundle\Import;
b1d05721 4
8f336fda 5use Wallabag\ImportBundle\Import\WallabagV1Import;
b1d05721 6use Wallabag\UserBundle\Entity\User;
4d0ec0e7 7use Wallabag\CoreBundle\Entity\Entry;
b3437d58 8use Wallabag\ImportBundle\Redis\Producer;
b1d05721
JB
9use Monolog\Logger;
10use Monolog\Handler\TestHandler;
b3437d58
JB
11use Simpleue\Queue\RedisQueue;
12use M6Web\Component\RedisMock\RedisMockFactory;
b1d05721
JB
13
14class WallabagV1ImportTest extends \PHPUnit_Framework_TestCase
15{
16 protected $user;
17 protected $em;
18 protected $logHandler;
0783c99a 19 protected $contentProxy;
6bc6fb1f
TC
20 protected $tagsAssigner;
21 protected $uow;
704803e1
JC
22 protected $fetchingErrorMessageTitle = 'No title found';
23 protected $fetchingErrorMessage = 'wallabag can\'t retrieve contents for this article. Please <a href="http://doc.wallabag.org/en/master/user/errors_during_fetching.html#how-can-i-help-to-fix-that">troubleshoot this issue</a>.';
0783c99a 24
7816eb62 25 private function getWallabagV1Import($unsetUser = false, $dispatched = 0)
b1d05721
JB
26 {
27 $this->user = new User();
28
29 $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
30 ->disableOriginalConstructor()
31 ->getMock();
32
40113585
JB
33 $this->uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork')
34 ->disableOriginalConstructor()
35 ->getMock();
36
37 $this->em
38 ->expects($this->any())
39 ->method('getUnitOfWork')
40 ->willReturn($this->uow);
41
42 $this->uow
43 ->expects($this->any())
44 ->method('getScheduledEntityInsertions')
45 ->willReturn([]);
46
0783c99a
TC
47 $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
48 ->disableOriginalConstructor()
49 ->getMock();
50
6bc6fb1f
TC
51 $this->tagsAssigner = $this->getMockBuilder('Wallabag\CoreBundle\Helper\TagsAssigner')
52 ->disableOriginalConstructor()
53 ->getMock();
54
7816eb62
JB
55 $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
56 ->disableOriginalConstructor()
57 ->getMock();
58
59 $dispatcher
60 ->expects($this->exactly($dispatched))
61 ->method('dispatch');
62
704803e1
JC
63 $wallabag = new WallabagV1Import(
64 $this->em,
65 $this->contentProxy,
66 $this->tagsAssigner,
67 $dispatcher,
68 $this->fetchingErrorMessageTitle,
69 $this->fetchingErrorMessage
70 );
b1d05721
JB
71
72 $this->logHandler = new TestHandler();
4094ea47 73 $logger = new Logger('test', [$this->logHandler]);
6785f4aa 74 $wallabag->setLogger($logger);
b1d05721
JB
75
76 if (false === $unsetUser) {
6785f4aa 77 $wallabag->setUser($this->user);
b1d05721
JB
78 }
79
6785f4aa 80 return $wallabag;
b1d05721
JB
81 }
82
83 public function testInit()
84 {
85 $wallabagV1Import = $this->getWallabagV1Import();
86
b88cf91f 87 $this->assertEquals('wallabag v1', $wallabagV1Import->getName());
7019c7cf 88 $this->assertNotEmpty($wallabagV1Import->getUrl());
0d42217e 89 $this->assertEquals('import.wallabag_v1.description', $wallabagV1Import->getDescription());
b1d05721
JB
90 }
91
92 public function testImport()
93 {
cf05a1ae 94 $wallabagV1Import = $this->getWallabagV1Import(false, 1);
b1d05721
JB
95 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json');
96
97 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
98 ->disableOriginalConstructor()
99 ->getMock();
100
cf05a1ae 101 $entryRepo->expects($this->exactly(2))
78833672 102 ->method('findByUrlAndUserId')
eaf9dad7 103 ->will($this->onConsecutiveCalls(false, true, false, false));
b1d05721
JB
104
105 $this->em
106 ->expects($this->any())
107 ->method('getRepository')
108 ->willReturn($entryRepo);
109
eaf9dad7
TC
110 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
111 ->disableOriginalConstructor()
112 ->getMock();
113
114 $this->contentProxy
cf05a1ae 115 ->expects($this->exactly(1))
6acadf8e 116 ->method('updateEntry')
eaf9dad7
TC
117 ->willReturn($entry);
118
b1d05721
JB
119 $res = $wallabagV1Import->import();
120
121 $this->assertTrue($res);
cf05a1ae 122 $this->assertEquals(['skipped' => 1, 'imported' => 1, 'queued' => 0], $wallabagV1Import->getSummary());
79d0e38e
JB
123 }
124
125 public function testImportAndMarkAllAsRead()
126 {
7816eb62 127 $wallabagV1Import = $this->getWallabagV1Import(false, 3);
79d0e38e
JB
128 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1-read.json');
129
130 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
131 ->disableOriginalConstructor()
132 ->getMock();
133
134 $entryRepo->expects($this->exactly(3))
135 ->method('findByUrlAndUserId')
136 ->will($this->onConsecutiveCalls(false, false, false));
137
138 $this->em
139 ->expects($this->any())
140 ->method('getRepository')
141 ->willReturn($entryRepo);
142
4d0ec0e7
JB
143 $this->contentProxy
144 ->expects($this->exactly(3))
6acadf8e 145 ->method('updateEntry')
4d0ec0e7
JB
146 ->willReturn(new Entry($this->user));
147
79d0e38e
JB
148 // check that every entry persisted are archived
149 $this->em
150 ->expects($this->any())
151 ->method('persist')
cebb4223 152 ->with($this->callback(function ($persistedEntry) {
79d0e38e
JB
153 return $persistedEntry->isArchived();
154 }));
155
156 $res = $wallabagV1Import->setMarkAsRead(true)->import();
157
158 $this->assertTrue($res);
159
c80cc01a 160 $this->assertEquals(['skipped' => 0, 'imported' => 3, 'queued' => 0], $wallabagV1Import->getSummary());
13470c35
JB
161 }
162
163 public function testImportWithRabbit()
164 {
165 $wallabagV1Import = $this->getWallabagV1Import();
166 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json');
167
168 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
169 ->disableOriginalConstructor()
170 ->getMock();
171
172 $entryRepo->expects($this->never())
173 ->method('findByUrlAndUserId');
174
175 $this->em
176 ->expects($this->never())
177 ->method('getRepository');
178
179 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
180 ->disableOriginalConstructor()
181 ->getMock();
182
183 $this->contentProxy
184 ->expects($this->never())
6acadf8e 185 ->method('updateEntry');
13470c35
JB
186
187 $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
188 ->disableOriginalConstructor()
189 ->getMock();
190
191 $producer
cf05a1ae 192 ->expects($this->exactly(2))
13470c35
JB
193 ->method('publish');
194
b3437d58 195 $wallabagV1Import->setProducer($producer);
13470c35
JB
196
197 $res = $wallabagV1Import->setMarkAsRead(true)->import();
198
199 $this->assertTrue($res);
cf05a1ae 200 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 2], $wallabagV1Import->getSummary());
b1d05721
JB
201 }
202
b3437d58
JB
203 public function testImportWithRedis()
204 {
205 $wallabagV1Import = $this->getWallabagV1Import();
206 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json');
207
208 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
209 ->disableOriginalConstructor()
210 ->getMock();
211
212 $entryRepo->expects($this->never())
213 ->method('findByUrlAndUserId');
214
215 $this->em
216 ->expects($this->never())
217 ->method('getRepository');
218
219 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
220 ->disableOriginalConstructor()
221 ->getMock();
222
223 $this->contentProxy
224 ->expects($this->never())
6acadf8e 225 ->method('updateEntry');
b3437d58
JB
226
227 $factory = new RedisMockFactory();
228 $redisMock = $factory->getAdapter('Predis\Client', true);
229
230 $queue = new RedisQueue($redisMock, 'wallabag_v1');
231 $producer = new Producer($queue);
232
233 $wallabagV1Import->setProducer($producer);
234
235 $res = $wallabagV1Import->setMarkAsRead(true)->import();
236
237 $this->assertTrue($res);
cf05a1ae 238 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 2], $wallabagV1Import->getSummary());
b3437d58
JB
239
240 $this->assertNotEmpty($redisMock->lpop('wallabag_v1'));
241 }
242
b1d05721
JB
243 public function testImportBadFile()
244 {
245 $wallabagV1Import = $this->getWallabagV1Import();
246 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.jsonx');
247
248 $res = $wallabagV1Import->import();
249
250 $this->assertFalse($res);
251
252 $records = $this->logHandler->getRecords();
6785f4aa 253 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
b1d05721
JB
254 $this->assertEquals('ERROR', $records[0]['level_name']);
255 }
256
257 public function testImportUserNotDefined()
258 {
259 $wallabagV1Import = $this->getWallabagV1Import(true);
260 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json');
261
262 $res = $wallabagV1Import->import();
263
264 $this->assertFalse($res);
265
266 $records = $this->logHandler->getRecords();
6785f4aa 267 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
b1d05721
JB
268 $this->assertEquals('ERROR', $records[0]['level_name']);
269 }
270}