]> git.immae.eu Git - github/wallabag/wallabag.git/blame_incremental - tests/Wallabag/ImportBundle/Import/WallabagV1ImportTest.php
Merge pull request #2724 from Simounet/fix/reading-time
[github/wallabag/wallabag.git] / tests / Wallabag / ImportBundle / Import / WallabagV1ImportTest.php
... / ...
CommitLineData
1<?php
2
3namespace Tests\Wallabag\ImportBundle\Import;
4
5use Wallabag\ImportBundle\Import\WallabagV1Import;
6use Wallabag\UserBundle\Entity\User;
7use Wallabag\CoreBundle\Entity\Entry;
8use Wallabag\ImportBundle\Redis\Producer;
9use Monolog\Logger;
10use Monolog\Handler\TestHandler;
11use Simpleue\Queue\RedisQueue;
12use M6Web\Component\RedisMock\RedisMockFactory;
13
14class WallabagV1ImportTest extends \PHPUnit_Framework_TestCase
15{
16 protected $user;
17 protected $em;
18 protected $logHandler;
19 protected $contentProxy;
20
21 private function getWallabagV1Import($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 WallabagV1Import($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 $wallabagV1Import = $this->getWallabagV1Import();
71
72 $this->assertEquals('wallabag v1', $wallabagV1Import->getName());
73 $this->assertNotEmpty($wallabagV1Import->getUrl());
74 $this->assertEquals('import.wallabag_v1.description', $wallabagV1Import->getDescription());
75 }
76
77 public function testImport()
78 {
79 $wallabagV1Import = $this->getWallabagV1Import(false, 3);
80 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json');
81
82 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
83 ->disableOriginalConstructor()
84 ->getMock();
85
86 $entryRepo->expects($this->exactly(4))
87 ->method('findByUrlAndUserId')
88 ->will($this->onConsecutiveCalls(false, true, false, false));
89
90 $this->em
91 ->expects($this->any())
92 ->method('getRepository')
93 ->willReturn($entryRepo);
94
95 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
96 ->disableOriginalConstructor()
97 ->getMock();
98
99 $this->contentProxy
100 ->expects($this->exactly(3))
101 ->method('updateEntry')
102 ->willReturn($entry);
103
104 $res = $wallabagV1Import->import();
105
106 $this->assertTrue($res);
107 $this->assertEquals(['skipped' => 1, 'imported' => 3, 'queued' => 0], $wallabagV1Import->getSummary());
108 }
109
110 public function testImportAndMarkAllAsRead()
111 {
112 $wallabagV1Import = $this->getWallabagV1Import(false, 3);
113 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1-read.json');
114
115 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
116 ->disableOriginalConstructor()
117 ->getMock();
118
119 $entryRepo->expects($this->exactly(3))
120 ->method('findByUrlAndUserId')
121 ->will($this->onConsecutiveCalls(false, false, false));
122
123 $this->em
124 ->expects($this->any())
125 ->method('getRepository')
126 ->willReturn($entryRepo);
127
128 $this->contentProxy
129 ->expects($this->exactly(3))
130 ->method('updateEntry')
131 ->willReturn(new Entry($this->user));
132
133 // check that every entry persisted are archived
134 $this->em
135 ->expects($this->any())
136 ->method('persist')
137 ->with($this->callback(function ($persistedEntry) {
138 return $persistedEntry->isArchived();
139 }));
140
141 $res = $wallabagV1Import->setMarkAsRead(true)->import();
142
143 $this->assertTrue($res);
144
145 $this->assertEquals(['skipped' => 0, 'imported' => 3, 'queued' => 0], $wallabagV1Import->getSummary());
146 }
147
148 public function testImportWithRabbit()
149 {
150 $wallabagV1Import = $this->getWallabagV1Import();
151 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json');
152
153 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
154 ->disableOriginalConstructor()
155 ->getMock();
156
157 $entryRepo->expects($this->never())
158 ->method('findByUrlAndUserId');
159
160 $this->em
161 ->expects($this->never())
162 ->method('getRepository');
163
164 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
165 ->disableOriginalConstructor()
166 ->getMock();
167
168 $this->contentProxy
169 ->expects($this->never())
170 ->method('updateEntry');
171
172 $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
173 ->disableOriginalConstructor()
174 ->getMock();
175
176 $producer
177 ->expects($this->exactly(4))
178 ->method('publish');
179
180 $wallabagV1Import->setProducer($producer);
181
182 $res = $wallabagV1Import->setMarkAsRead(true)->import();
183
184 $this->assertTrue($res);
185 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 4], $wallabagV1Import->getSummary());
186 }
187
188 public function testImportWithRedis()
189 {
190 $wallabagV1Import = $this->getWallabagV1Import();
191 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json');
192
193 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
194 ->disableOriginalConstructor()
195 ->getMock();
196
197 $entryRepo->expects($this->never())
198 ->method('findByUrlAndUserId');
199
200 $this->em
201 ->expects($this->never())
202 ->method('getRepository');
203
204 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
205 ->disableOriginalConstructor()
206 ->getMock();
207
208 $this->contentProxy
209 ->expects($this->never())
210 ->method('updateEntry');
211
212 $factory = new RedisMockFactory();
213 $redisMock = $factory->getAdapter('Predis\Client', true);
214
215 $queue = new RedisQueue($redisMock, 'wallabag_v1');
216 $producer = new Producer($queue);
217
218 $wallabagV1Import->setProducer($producer);
219
220 $res = $wallabagV1Import->setMarkAsRead(true)->import();
221
222 $this->assertTrue($res);
223 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 4], $wallabagV1Import->getSummary());
224
225 $this->assertNotEmpty($redisMock->lpop('wallabag_v1'));
226 }
227
228 public function testImportBadFile()
229 {
230 $wallabagV1Import = $this->getWallabagV1Import();
231 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.jsonx');
232
233 $res = $wallabagV1Import->import();
234
235 $this->assertFalse($res);
236
237 $records = $this->logHandler->getRecords();
238 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
239 $this->assertEquals('ERROR', $records[0]['level_name']);
240 }
241
242 public function testImportUserNotDefined()
243 {
244 $wallabagV1Import = $this->getWallabagV1Import(true);
245 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json');
246
247 $res = $wallabagV1Import->import();
248
249 $this->assertFalse($res);
250
251 $records = $this->logHandler->getRecords();
252 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
253 $this->assertEquals('ERROR', $records[0]['level_name']);
254 }
255}