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