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