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