]>
Commit | Line | Data |
---|---|---|
b1d05721 JB |
1 | <?php |
2 | ||
23634d5d | 3 | namespace Tests\Wallabag\ImportBundle\Import; |
b1d05721 | 4 | |
f808b016 | 5 | use M6Web\Component\RedisMock\RedisMockFactory; |
b1d05721 | 6 | use Monolog\Handler\TestHandler; |
f808b016 | 7 | use Monolog\Logger; |
b3437d58 | 8 | use Simpleue\Queue\RedisQueue; |
f808b016 JB |
9 | use Wallabag\CoreBundle\Entity\Entry; |
10 | use Wallabag\ImportBundle\Import\WallabagV1Import; | |
11 | use Wallabag\ImportBundle\Redis\Producer; | |
12 | use Wallabag\UserBundle\Entity\User; | |
b1d05721 JB |
13 | |
14 | class WallabagV1ImportTest extends \PHPUnit_Framework_TestCase | |
15 | { | |
16 | protected $user; | |
17 | protected $em; | |
18 | protected $logHandler; | |
0783c99a | 19 | protected $contentProxy; |
6bc6fb1f TC |
20 | protected $tagsAssigner; |
21 | protected $uow; | |
704803e1 JC |
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>.'; | |
0783c99a | 24 | |
b1d05721 JB |
25 | public function testInit() |
26 | { | |
27 | $wallabagV1Import = $this->getWallabagV1Import(); | |
28 | ||
f808b016 | 29 | $this->assertSame('wallabag v1', $wallabagV1Import->getName()); |
7019c7cf | 30 | $this->assertNotEmpty($wallabagV1Import->getUrl()); |
f808b016 | 31 | $this->assertSame('import.wallabag_v1.description', $wallabagV1Import->getDescription()); |
b1d05721 JB |
32 | } |
33 | ||
34 | public function testImport() | |
35 | { | |
cf05a1ae | 36 | $wallabagV1Import = $this->getWallabagV1Import(false, 1); |
f808b016 | 37 | $wallabagV1Import->setFilepath(__DIR__ . '/../fixtures/wallabag-v1.json'); |
b1d05721 JB |
38 | |
39 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
40 | ->disableOriginalConstructor() | |
41 | ->getMock(); | |
42 | ||
cf05a1ae | 43 | $entryRepo->expects($this->exactly(2)) |
78833672 | 44 | ->method('findByUrlAndUserId') |
eaf9dad7 | 45 | ->will($this->onConsecutiveCalls(false, true, false, false)); |
b1d05721 JB |
46 | |
47 | $this->em | |
48 | ->expects($this->any()) | |
49 | ->method('getRepository') | |
50 | ->willReturn($entryRepo); | |
51 | ||
eaf9dad7 TC |
52 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') |
53 | ->disableOriginalConstructor() | |
54 | ->getMock(); | |
55 | ||
56 | $this->contentProxy | |
cf05a1ae | 57 | ->expects($this->exactly(1)) |
6acadf8e | 58 | ->method('updateEntry') |
eaf9dad7 TC |
59 | ->willReturn($entry); |
60 | ||
b1d05721 JB |
61 | $res = $wallabagV1Import->import(); |
62 | ||
63 | $this->assertTrue($res); | |
f808b016 | 64 | $this->assertSame(['skipped' => 1, 'imported' => 1, 'queued' => 0], $wallabagV1Import->getSummary()); |
79d0e38e JB |
65 | } |
66 | ||
67 | public function testImportAndMarkAllAsRead() | |
68 | { | |
7816eb62 | 69 | $wallabagV1Import = $this->getWallabagV1Import(false, 3); |
f808b016 | 70 | $wallabagV1Import->setFilepath(__DIR__ . '/../fixtures/wallabag-v1-read.json'); |
79d0e38e JB |
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 | ||
4d0ec0e7 JB |
85 | $this->contentProxy |
86 | ->expects($this->exactly(3)) | |
6acadf8e | 87 | ->method('updateEntry') |
4d0ec0e7 JB |
88 | ->willReturn(new Entry($this->user)); |
89 | ||
79d0e38e JB |
90 | // check that every entry persisted are archived |
91 | $this->em | |
92 | ->expects($this->any()) | |
93 | ->method('persist') | |
cebb4223 | 94 | ->with($this->callback(function ($persistedEntry) { |
79d0e38e JB |
95 | return $persistedEntry->isArchived(); |
96 | })); | |
97 | ||
98 | $res = $wallabagV1Import->setMarkAsRead(true)->import(); | |
99 | ||
100 | $this->assertTrue($res); | |
101 | ||
f808b016 | 102 | $this->assertSame(['skipped' => 0, 'imported' => 3, 'queued' => 0], $wallabagV1Import->getSummary()); |
13470c35 JB |
103 | } |
104 | ||
105 | public function testImportWithRabbit() | |
106 | { | |
107 | $wallabagV1Import = $this->getWallabagV1Import(); | |
f808b016 | 108 | $wallabagV1Import->setFilepath(__DIR__ . '/../fixtures/wallabag-v1.json'); |
13470c35 JB |
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()) | |
6acadf8e | 127 | ->method('updateEntry'); |
13470c35 JB |
128 | |
129 | $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer') | |
130 | ->disableOriginalConstructor() | |
131 | ->getMock(); | |
132 | ||
133 | $producer | |
cf05a1ae | 134 | ->expects($this->exactly(2)) |
13470c35 JB |
135 | ->method('publish'); |
136 | ||
b3437d58 | 137 | $wallabagV1Import->setProducer($producer); |
13470c35 JB |
138 | |
139 | $res = $wallabagV1Import->setMarkAsRead(true)->import(); | |
140 | ||
141 | $this->assertTrue($res); | |
f808b016 | 142 | $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 2], $wallabagV1Import->getSummary()); |
b1d05721 JB |
143 | } |
144 | ||
b3437d58 JB |
145 | public function testImportWithRedis() |
146 | { | |
147 | $wallabagV1Import = $this->getWallabagV1Import(); | |
f808b016 | 148 | $wallabagV1Import->setFilepath(__DIR__ . '/../fixtures/wallabag-v1.json'); |
b3437d58 JB |
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()) | |
6acadf8e | 167 | ->method('updateEntry'); |
b3437d58 JB |
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); | |
f808b016 | 180 | $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 2], $wallabagV1Import->getSummary()); |
b3437d58 JB |
181 | |
182 | $this->assertNotEmpty($redisMock->lpop('wallabag_v1')); | |
183 | } | |
184 | ||
b1d05721 JB |
185 | public function testImportBadFile() |
186 | { | |
187 | $wallabagV1Import = $this->getWallabagV1Import(); | |
f808b016 | 188 | $wallabagV1Import->setFilepath(__DIR__ . '/../fixtures/wallabag-v1.jsonx'); |
b1d05721 JB |
189 | |
190 | $res = $wallabagV1Import->import(); | |
191 | ||
192 | $this->assertFalse($res); | |
193 | ||
194 | $records = $this->logHandler->getRecords(); | |
6785f4aa | 195 | $this->assertContains('WallabagImport: unable to read file', $records[0]['message']); |
f808b016 | 196 | $this->assertSame('ERROR', $records[0]['level_name']); |
b1d05721 JB |
197 | } |
198 | ||
199 | public function testImportUserNotDefined() | |
200 | { | |
201 | $wallabagV1Import = $this->getWallabagV1Import(true); | |
f808b016 | 202 | $wallabagV1Import->setFilepath(__DIR__ . '/../fixtures/wallabag-v1.json'); |
b1d05721 JB |
203 | |
204 | $res = $wallabagV1Import->import(); | |
205 | ||
206 | $this->assertFalse($res); | |
207 | ||
208 | $records = $this->logHandler->getRecords(); | |
6785f4aa | 209 | $this->assertContains('WallabagImport: user is not defined', $records[0]['message']); |
f808b016 JB |
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; | |
b1d05721 JB |
269 | } |
270 | } |