]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php
Merge pull request #2879 from matteocoder/matteocoder-patch-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, $dispatched = 0)
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 $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
48 ->disableOriginalConstructor()
49 ->getMock();
50
51 $dispatcher
52 ->expects($this->exactly($dispatched))
53 ->method('dispatch');
54
55 $wallabag = new WallabagV2Import($this->em, $this->contentProxy, $dispatcher);
56
57 $this->logHandler = new TestHandler();
58 $logger = new Logger('test', [$this->logHandler]);
59 $wallabag->setLogger($logger);
60
61 if (false === $unsetUser) {
62 $wallabag->setUser($this->user);
63 }
64
65 return $wallabag;
66 }
67
68 public function testInit()
69 {
70 $wallabagV2Import = $this->getWallabagV2Import();
71
72 $this->assertEquals('wallabag v2', $wallabagV2Import->getName());
73 $this->assertNotEmpty($wallabagV2Import->getUrl());
74 $this->assertEquals('import.wallabag_v2.description', $wallabagV2Import->getDescription());
75 }
76
77 public function testImport()
78 {
79 $wallabagV2Import = $this->getWallabagV2Import(false, 2);
80 $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json');
81
82 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
83 ->disableOriginalConstructor()
84 ->getMock();
85
86 $entryRepo->expects($this->exactly(24))
87 ->method('findByUrlAndUserId')
88 ->will($this->onConsecutiveCalls(false, true, false));
89
90 $this->em
91 ->expects($this->any())
92 ->method('getRepository')
93 ->willReturn($entryRepo);
94
95 $this->contentProxy
96 ->expects($this->exactly(2))
97 ->method('updateEntry')
98 ->willReturn(new Entry($this->user));
99
100 $res = $wallabagV2Import->import();
101
102 $this->assertTrue($res);
103 $this->assertEquals(['skipped' => 22, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());
104 }
105
106 public function testImportAndMarkAllAsRead()
107 {
108 $wallabagV2Import = $this->getWallabagV2Import(false, 2);
109 $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2-read.json');
110
111 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
112 ->disableOriginalConstructor()
113 ->getMock();
114
115 $entryRepo->expects($this->exactly(2))
116 ->method('findByUrlAndUserId')
117 ->will($this->onConsecutiveCalls(false, false));
118
119 $this->em
120 ->expects($this->any())
121 ->method('getRepository')
122 ->willReturn($entryRepo);
123
124 $this->contentProxy
125 ->expects($this->exactly(2))
126 ->method('updateEntry')
127 ->willReturn(new Entry($this->user));
128
129 // check that every entry persisted are archived
130 $this->em
131 ->expects($this->any())
132 ->method('persist')
133 ->with($this->callback(function ($persistedEntry) {
134 return $persistedEntry->isArchived();
135 }));
136
137 $res = $wallabagV2Import->setMarkAsRead(true)->import();
138
139 $this->assertTrue($res);
140
141 $this->assertEquals(['skipped' => 0, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());
142 }
143
144 public function testImportWithRabbit()
145 {
146 $wallabagV2Import = $this->getWallabagV2Import();
147 $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json');
148
149 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
150 ->disableOriginalConstructor()
151 ->getMock();
152
153 $entryRepo->expects($this->never())
154 ->method('findByUrlAndUserId');
155
156 $this->em
157 ->expects($this->never())
158 ->method('getRepository');
159
160 $this->contentProxy
161 ->expects($this->never())
162 ->method('updateEntry');
163
164 $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
165 ->disableOriginalConstructor()
166 ->getMock();
167
168 $producer
169 ->expects($this->exactly(24))
170 ->method('publish');
171
172 $wallabagV2Import->setProducer($producer);
173
174 $res = $wallabagV2Import->setMarkAsRead(true)->import();
175
176 $this->assertTrue($res);
177 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 24], $wallabagV2Import->getSummary());
178 }
179
180 public function testImportWithRedis()
181 {
182 $wallabagV2Import = $this->getWallabagV2Import();
183 $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json');
184
185 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
186 ->disableOriginalConstructor()
187 ->getMock();
188
189 $entryRepo->expects($this->never())
190 ->method('findByUrlAndUserId');
191
192 $this->em
193 ->expects($this->never())
194 ->method('getRepository');
195
196 $this->contentProxy
197 ->expects($this->never())
198 ->method('updateEntry');
199
200 $factory = new RedisMockFactory();
201 $redisMock = $factory->getAdapter('Predis\Client', true);
202
203 $queue = new RedisQueue($redisMock, 'wallabag_v2');
204 $producer = new Producer($queue);
205
206 $wallabagV2Import->setProducer($producer);
207
208 $res = $wallabagV2Import->setMarkAsRead(true)->import();
209
210 $this->assertTrue($res);
211 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 24], $wallabagV2Import->getSummary());
212
213 $this->assertNotEmpty($redisMock->lpop('wallabag_v2'));
214 }
215
216 public function testImportBadFile()
217 {
218 $wallabagV1Import = $this->getWallabagV2Import();
219 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.jsonx');
220
221 $res = $wallabagV1Import->import();
222
223 $this->assertFalse($res);
224
225 $records = $this->logHandler->getRecords();
226 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
227 $this->assertEquals('ERROR', $records[0]['level_name']);
228 }
229
230 public function testImportUserNotDefined()
231 {
232 $wallabagV1Import = $this->getWallabagV2Import(true);
233 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json');
234
235 $res = $wallabagV1Import->import();
236
237 $this->assertFalse($res);
238
239 $records = $this->logHandler->getRecords();
240 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
241 $this->assertEquals('ERROR', $records[0]['level_name']);
242 }
243
244 public function testImportEmptyFile()
245 {
246 $wallabagV2Import = $this->getWallabagV2Import();
247 $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2-empty.json');
248
249 $res = $wallabagV2Import->import();
250
251 $this->assertFalse($res);
252 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 0], $wallabagV2Import->getSummary());
253 }
254
255 public function testImportWithExceptionFromGraby()
256 {
257 $wallabagV2Import = $this->getWallabagV2Import(false, 2);
258 $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json');
259
260 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
261 ->disableOriginalConstructor()
262 ->getMock();
263
264 $entryRepo->expects($this->exactly(24))
265 ->method('findByUrlAndUserId')
266 ->will($this->onConsecutiveCalls(false, true, false));
267
268 $this->em
269 ->expects($this->any())
270 ->method('getRepository')
271 ->willReturn($entryRepo);
272
273 $this->contentProxy
274 ->expects($this->exactly(2))
275 ->method('updateEntry')
276 ->will($this->throwException(new \Exception()));
277
278 $res = $wallabagV2Import->import();
279
280 $this->assertTrue($res);
281 $this->assertEquals(['skipped' => 22, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());
282 }
283 }