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