]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/ImportBundle/Import/WallabagV1ImportTest.php
834b7ef57bcbc1609ea9f97c3292a17c72e735c3
[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 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>.';
24
25 private function getWallabagV1Import($unsetUser = false, $dispatched = 0)
26 {
27 $this->user = new User();
28
29 $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
30 ->disableOriginalConstructor()
31 ->getMock();
32
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
47 $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
48 ->disableOriginalConstructor()
49 ->getMock();
50
51 $this->tagsAssigner = $this->getMockBuilder('Wallabag\CoreBundle\Helper\TagsAssigner')
52 ->disableOriginalConstructor()
53 ->getMock();
54
55 $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
56 ->disableOriginalConstructor()
57 ->getMock();
58
59 $dispatcher
60 ->expects($this->exactly($dispatched))
61 ->method('dispatch');
62
63 $wallabag = new WallabagV1Import(
64 $this->em,
65 $this->contentProxy,
66 $this->tagsAssigner,
67 $dispatcher,
68 $this->fetchingErrorMessageTitle,
69 $this->fetchingErrorMessage
70 );
71
72 $this->logHandler = new TestHandler();
73 $logger = new Logger('test', [$this->logHandler]);
74 $wallabag->setLogger($logger);
75
76 if (false === $unsetUser) {
77 $wallabag->setUser($this->user);
78 }
79
80 return $wallabag;
81 }
82
83 public function testInit()
84 {
85 $wallabagV1Import = $this->getWallabagV1Import();
86
87 $this->assertEquals('wallabag v1', $wallabagV1Import->getName());
88 $this->assertNotEmpty($wallabagV1Import->getUrl());
89 $this->assertEquals('import.wallabag_v1.description', $wallabagV1Import->getDescription());
90 }
91
92 public function testImport()
93 {
94 $wallabagV1Import = $this->getWallabagV1Import(false, 1);
95 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json');
96
97 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
98 ->disableOriginalConstructor()
99 ->getMock();
100
101 $entryRepo->expects($this->exactly(2))
102 ->method('findByUrlAndUserId')
103 ->will($this->onConsecutiveCalls(false, true, false, false));
104
105 $this->em
106 ->expects($this->any())
107 ->method('getRepository')
108 ->willReturn($entryRepo);
109
110 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
111 ->disableOriginalConstructor()
112 ->getMock();
113
114 $this->contentProxy
115 ->expects($this->exactly(1))
116 ->method('updateEntry')
117 ->willReturn($entry);
118
119 $res = $wallabagV1Import->import();
120
121 $this->assertTrue($res);
122 $this->assertEquals(['skipped' => 1, 'imported' => 1, 'queued' => 0], $wallabagV1Import->getSummary());
123 }
124
125 public function testImportAndMarkAllAsRead()
126 {
127 $wallabagV1Import = $this->getWallabagV1Import(false, 3);
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
143 $this->contentProxy
144 ->expects($this->exactly(3))
145 ->method('updateEntry')
146 ->willReturn(new Entry($this->user));
147
148 // check that every entry persisted are archived
149 $this->em
150 ->expects($this->any())
151 ->method('persist')
152 ->with($this->callback(function ($persistedEntry) {
153 return $persistedEntry->isArchived();
154 }));
155
156 $res = $wallabagV1Import->setMarkAsRead(true)->import();
157
158 $this->assertTrue($res);
159
160 $this->assertEquals(['skipped' => 0, 'imported' => 3, 'queued' => 0], $wallabagV1Import->getSummary());
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())
185 ->method('updateEntry');
186
187 $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
188 ->disableOriginalConstructor()
189 ->getMock();
190
191 $producer
192 ->expects($this->exactly(2))
193 ->method('publish');
194
195 $wallabagV1Import->setProducer($producer);
196
197 $res = $wallabagV1Import->setMarkAsRead(true)->import();
198
199 $this->assertTrue($res);
200 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 2], $wallabagV1Import->getSummary());
201 }
202
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())
225 ->method('updateEntry');
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);
238 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 2], $wallabagV1Import->getSummary());
239
240 $this->assertNotEmpty($redisMock->lpop('wallabag_v1'));
241 }
242
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();
253 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
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();
267 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
268 $this->assertEquals('ERROR', $records[0]['level_name']);
269 }
270 }