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